nvme: fix srcu locking on error return in nvme_get_ns_from_disk
Christoph Hellwig
maintainerPATCH 1/4 nvme: fix srcu locking on error return in nvme_get_ns_from_disk
If we can't get a namespace don't leak the SRCU lock. nvme_ioctl was working around this, but nvme_pr_command wasn't handling this properly. Just do what callers would usually expect.
--- drivers/nvme/host/core.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/nvme/host/core.c +9 -4
@@ -1361,9 +1361,14 @@ static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk, {#ifdef CONFIG_NVME_MULTIPATH if (disk->fops == &nvme_ns_head_ops) { +struct nvme_ns *ns; + *head = disk->private_data; *srcu_idx =srcu_read_lock (&(*head)->srcu); -return nvme_find_path (*head); + ns =nvme_find_path (*head); +if (!ns) +srcu_read_unlock (&(*head)->srcu, *srcu_idx); +return ns; }#endif *head =NULL ;
@@ -1410,9 +1415,9 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode, ns =nvme_get_ns_from_disk (bdev->bd_disk, &head, &srcu_idx);if (unlikely (!ns)) - ret = -EWOULDBLOCK ; -else - ret =nvme_ns_ioctl (ns, cmd, arg); +return -EWOULDBLOCK ; + + ret =nvme_ns_ioctl (ns, cmd, arg);nvme_put_ns_from_disk (head, srcu_idx);return ret; }
Signature · 1 line
2.20.1
Christoph Hellwig
maintainerPATCH 2/4 nvme: remove the ifdef around nvme_nvm_ioctl
We already have a proper stub if lightnvm is not enabled, so don't bother with the ifdef.
--- drivers/nvme/host/core.c | 2 -- 1 file changed, 2 deletions(-)
drivers/nvme/host/core.c +0 -2
@@ -1395,10 +1395,8 @@ static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned cmd, unsigned long arg)case NVME_IOCTL_SUBMIT_IO :return nvme_submit_io (ns, (void __user *)arg);default : -#ifdef CONFIG_NVM if (ns->ndev)return nvme_nvm_ioctl (ns, cmd, arg); -#endif if (is_sed_ioctl (cmd))return sed_ioctl (ns->ctrl->opal_dev, cmd, (void __user *) arg);
Signature · 1 line
2.20.1
Keith Busch
maintainerPATCH 2/4 nvme: remove the ifdef around nvme_nvm_ioctl
Quoted 4 lines · Christoph Hellwig
We already have a proper stub if lightnvm is not enabled, so don't bother with the ifdef.
Signed-off-by Christoph Hellwig <hch at lst.de>
Looks good.
PATCH 2/4 nvme: remove the ifdef around nvme_nvm_ioctl
In reply to Christoph HellwigLooks good.
Quoted 23 lines · Christoph Hellwig
We already have a proper stub if lightnvm is not enabled, so don't bother with the ifdef.
Signed-off-by Christoph Hellwig <hch at lst.de>--- drivers/nvme/host/core.c | 2 -- 1 file changed, 2 deletions(-)drivers/nvme/host/core.c +0
-2@@ -1395,10 +1395,8 @@ static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned cmd, unsigned long arg)case NVME_IOCTL_SUBMIT_IO :return nvme_submit_io (ns, (void __user *)arg);default : -#ifdef CONFIG_NVM if (ns->ndev)return nvme_nvm_ioctl (ns, cmd, arg); -#endif if (is_sed_ioctl (cmd))return sed_ioctl (ns->ctrl->opal_dev, cmd, (void __user *) arg);
Christoph Hellwig
maintainerPATCH 3/4 nvme: merge nvme_ns_ioctl into nvme_ioctl
In reply to Christoph HellwigMerge the two functions to make future changes a little easier.
--- drivers/nvme/host/core.c | 47 ++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 23 deletions(-)
drivers/nvme/host/core.c +24 -23
@@ -1382,32 +1382,11 @@ static void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx)srcu_read_unlock (&head->srcu, idx); } -static int nvme_ns_ioctl (struct nvme_ns *ns,unsigned cmd,unsigned long arg) -{ -switch (cmd) { -case NVME_IOCTL_ID : -force_successful_syscall_return (); -return ns->head->ns_id; -case NVME_IOCTL_ADMIN_CMD : -return nvme_user_cmd (ns->ctrl,NULL , (void __user *)arg); -case NVME_IOCTL_IO_CMD : -return nvme_user_cmd (ns->ctrl, ns, (void __user *)arg); -case NVME_IOCTL_SUBMIT_IO : -return nvme_submit_io (ns, (void __user *)arg); -default : -if (ns->ndev) -return nvme_nvm_ioctl (ns, cmd, arg); -if (is_sed_ioctl (cmd)) -return sed_ioctl (ns->ctrl->opal_dev, cmd, - (void __user *) arg); -return -ENOTTY ; - } -} -static int nvme_ioctl (struct block_device *bdev,fmode_t mode,unsigned int cmd,unsigned long arg) {struct nvme_ns_head *head =NULL ; +void __user *argp = (void __user *)arg;struct nvme_ns *ns;int srcu_idx, ret;
@@ -1415,7 +1394,29 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode,if (unlikely (!ns))return -EWOULDBLOCK ; - ret =nvme_ns_ioctl (ns, cmd, arg); +switch (cmd) { +case NVME_IOCTL_ID : +force_successful_syscall_return (); + ret = ns->head->ns_id; +break ; +case NVME_IOCTL_ADMIN_CMD : + ret =nvme_user_cmd (ns->ctrl,NULL , argp); +break ; +case NVME_IOCTL_IO_CMD : + ret =nvme_user_cmd (ns->ctrl, ns, argp); +break ; +case NVME_IOCTL_SUBMIT_IO : + ret =nvme_submit_io (ns, argp); +break ; +default : +if (ns->ndev) + ret =nvme_nvm_ioctl (ns, cmd, arg); +else if (is_sed_ioctl (cmd)) + ret =sed_ioctl (ns->ctrl->opal_dev, cmd, argp); +else + ret = -ENOTTY ; + } +nvme_put_ns_from_disk (head, srcu_idx);return ret; }
Signature · 1 line
2.20.1
Keith Busch
maintainerPATCH 3/4 nvme: merge nvme_ns_ioctl into nvme_ioctl
Quoted 3 lines · Christoph Hellwig
Merge the two functions to make future changes a little easier.
Signed-off-by Christoph Hellwig <hch at lst.de>
Looks good.
PATCH 3/4 nvme: merge nvme_ns_ioctl into nvme_ioctl
In reply to Christoph HellwigThanks for simplifying this, looks good.
Quoted 76 lines · Christoph Hellwig
Merge the two functions to make future changes a little easier.
Signed-off-by Christoph Hellwig <hch at lst.de>--- drivers/nvme/host/core.c | 47 ++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 23 deletions(-)drivers/nvme/host/core.c +24
-23@@ -1382,32 +1382,11 @@ static void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx)srcu_read_unlock (&head->srcu, idx); } -static int nvme_ns_ioctl (struct nvme_ns *ns,unsigned cmd,unsigned long arg) -{ -switch (cmd) { -case NVME_IOCTL_ID : -force_successful_syscall_return (); -return ns->head->ns_id; -case NVME_IOCTL_ADMIN_CMD : -return nvme_user_cmd (ns->ctrl,NULL , (void __user *)arg); -case NVME_IOCTL_IO_CMD : -return nvme_user_cmd (ns->ctrl, ns, (void __user *)arg); -case NVME_IOCTL_SUBMIT_IO : -return nvme_submit_io (ns, (void __user *)arg); -default : -if (ns->ndev) -return nvme_nvm_ioctl (ns, cmd, arg); -if (is_sed_ioctl (cmd)) -return sed_ioctl (ns->ctrl->opal_dev, cmd, - (void __user *) arg); -return -ENOTTY ; - } -} -static int nvme_ioctl (struct block_device *bdev,fmode_t mode,unsigned int cmd,unsigned long arg) {struct nvme_ns_head *head =NULL ; +void __user *argp = (void __user *)arg;struct nvme_ns *ns;int srcu_idx, ret;@@ -1415,7 +1394,29 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode,if (unlikely (!ns))return -EWOULDBLOCK ; - ret =nvme_ns_ioctl (ns, cmd, arg); +switch (cmd) { +case NVME_IOCTL_ID : +force_successful_syscall_return (); + ret = ns->head->ns_id; +break ; +case NVME_IOCTL_ADMIN_CMD : + ret =nvme_user_cmd (ns->ctrl,NULL , argp); +break ; +case NVME_IOCTL_IO_CMD : + ret =nvme_user_cmd (ns->ctrl, ns, argp); +break ; +case NVME_IOCTL_SUBMIT_IO : + ret =nvme_submit_io (ns, argp); +break ; +default : +if (ns->ndev) + ret =nvme_nvm_ioctl (ns, cmd, arg); +else if (is_sed_ioctl (cmd)) + ret =sed_ioctl (ns->ctrl->opal_dev, cmd, argp); +else + ret = -ENOTTY ; + } +nvme_put_ns_from_disk (head, srcu_idx);return ret; }
Christoph Hellwig
maintainerPATCH 4/4 nvme: release namespace SRCU protection before performing controller ioctls
In reply to Christoph HellwigHolding the SRCU critical section protecting the namespace list can cause deadlocks when using the per-namespace admin passthrough ioctl to delete as namespace. Release it earlier when performing per-controller ioctls to avoid that.
--- drivers/nvme/host/core.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
drivers/nvme/host/core.c +20 -5
@@ -1394,14 +1394,31 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode,if (unlikely (!ns))return -EWOULDBLOCK ; +/* +* Handle ioctls that apply to the controller instead of the namespace +* seperately and drop the ns SRCU reference early. This avoids a +* deadlock when deleting namespaces using the passthrough interface. +*/ +if (cmd ==NVME_IOCTL_ADMIN_CMD ||is_sed_ioctl (cmd)) { +struct nvme_ctrl *ctrl = ns->ctrl; + +nvme_get_ctrl (ns->ctrl); +nvme_put_ns_from_disk (head, srcu_idx); + +if (cmd ==NVME_IOCTL_ADMIN_CMD ) + ret =nvme_user_cmd (ctrl,NULL , argp); +else + ret =sed_ioctl (ctrl->opal_dev, cmd, argp); + +nvme_get_ctrl (ctrl); +return ret; + } +switch (cmd) {case NVME_IOCTL_ID :force_successful_syscall_return (); ret = ns->head->ns_id;break ; -case NVME_IOCTL_ADMIN_CMD : - ret =nvme_user_cmd (ns->ctrl,NULL , argp); -break ;case NVME_IOCTL_IO_CMD : ret =nvme_user_cmd (ns->ctrl, ns, argp);break ;
@@ -1411,8 +1428,6 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode,default :if (ns->ndev) ret =nvme_nvm_ioctl (ns, cmd, arg); -else if (is_sed_ioctl (cmd)) - ret =sed_ioctl (ns->ctrl->opal_dev, cmd, argp);else ret = -ENOTTY ; }
Signature · 1 line
2.20.1
Keith Busch
maintainerPATCH 4/4 nvme: release namespace SRCU protection before performing controller ioctls
drivers/nvme/host/core.c static int nvme_ioctl(struct block_device *bdev, fmode_t mode, · line 1413
Quoted 6 lines · Christoph Hellwig
drivers/nvme/host/core.c +6
-0+if (cmd ==NVME_IOCTL_ADMIN_CMD ) + ret =nvme_user_cmd (ctrl,NULL , argp); +else + ret =sed_ioctl (ctrl->opal_dev, cmd, argp); + +nvme_get_ctrl (ctrl);
nvme_put_ctrl(ctrl);
PATCH 4/4 nvme: release namespace SRCU protection before performing controller ioctls
In reply to Christoph HellwigLooks good, with couple of small nits.
Quoted 18 lines · Christoph Hellwig
Holding the SRCU critical section protecting the namespace list can cause deadlocks when using the per-namespace admin passthrough ioctl to delete as namespace. Release it earlier when performing per-controller ioctls to avoid that.
Reported-by Kenneth Heitke <kenneth.heitke at intel.com>Signed-off-by Christoph Hellwig <hch at lst.de>--- drivers/nvme/host/core.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)drivers/nvme/host/core.c +0
-0@@ -1394,14 +1394,31 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode,if (unlikely (!ns))return -EWOULDBLOCK ;
drivers/nvme/host/core.c static int nvme_ioctl(struct block_device *bdev, fmode_t mode, · line 1405
Quoted 10 lines
drivers/nvme/host/core.c +9
-0+/* +* Handle ioctls that apply to the controller instead of the namespace +* seperately and drop the ns SRCU reference early. This avoids a +* deadlock when deleting namespaces using the passthrough interface. +*/ +if (cmd ==NVME_IOCTL_ADMIN_CMD ||is_sed_ioctl (cmd)) { +struct nvme_ctrl *ctrl = ns->ctrl; + +nvme_get_ctrl (ns->ctrl);
nit:- just use ctrl instead of ns->ctrl in the above call since we are already initializing it.
drivers/nvme/host/core.c static int nvme_ioctl(struct block_device *bdev, fmode_t mode, · line 1413
Quoted 8 lines
drivers/nvme/host/core.c +8
-0+nvme_put_ns_from_disk (head, srcu_idx); + +if (cmd ==NVME_IOCTL_ADMIN_CMD ) + ret =nvme_user_cmd (ctrl,NULL , argp); +else + ret =sed_ioctl (ctrl->opal_dev, cmd, argp); + +nvme_get_ctrl (ctrl);
As Keith pointed out nvme_put_ctrl().
Quoted 23 lines
drivers/nvme/host/core.c +3
-5+return ret; + } +switch (cmd) {case NVME_IOCTL_ID :force_successful_syscall_return (); ret = ns->head->ns_id;break ; -case NVME_IOCTL_ADMIN_CMD : - ret =nvme_user_cmd (ns->ctrl,NULL , argp); -break ;case NVME_IOCTL_IO_CMD : ret =nvme_user_cmd (ns->ctrl, ns, argp);break ;@@ -1411,8 +1428,6 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode,default :if (ns->ndev) ret =nvme_nvm_ioctl (ns, cmd, arg); -else if (is_sed_ioctl (cmd)) - ret =sed_ioctl (ns->ctrl->opal_dev, cmd, argp);else ret = -ENOTTY ; }
Keith Busch
maintainerPATCH 1/4 nvme: fix srcu locking on error return in nvme_get_ns_from_disk
In reply to Christoph HellwigQuoted 5 lines · Christoph Hellwig
If we can't get a namespace don't leak the SRCU lock.
nvme_ioctlwas working around this, butnvme_pr_commandwasn't handling this properly. Just do what callers would usually expect.Signed-off-by Christoph Hellwig <hch at lst.de>
Looks good.
PATCH 1/4 nvme: fix srcu locking on error return in nvme_get_ns_from_disk
In reply to Christoph HellwigLooks good.
Quoted 42 lines · Christoph Hellwig
If we can't get a namespace don't leak the SRCU lock.
nvme_ioctlwas working around this, butnvme_pr_commandwasn't handling this properly. Just do what callers would usually expect.Signed-off-by Christoph Hellwig <hch at lst.de>--- drivers/nvme/host/core.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)drivers/nvme/host/core.c +9
-4@@ -1361,9 +1361,14 @@ static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk, {#ifdef CONFIG_NVME_MULTIPATH if (disk->fops == &nvme_ns_head_ops) { +struct nvme_ns *ns; + *head = disk->private_data; *srcu_idx =srcu_read_lock (&(*head)->srcu); -return nvme_find_path (*head); + ns =nvme_find_path (*head); +if (!ns) +srcu_read_unlock (&(*head)->srcu, *srcu_idx); +return ns; }#endif *head =NULL ;@@ -1410,9 +1415,9 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode, ns =nvme_get_ns_from_disk (bdev->bd_disk, &head, &srcu_idx);if (unlikely (!ns)) - ret = -EWOULDBLOCK ; -else - ret =nvme_ns_ioctl (ns, cmd, arg); +return -EWOULDBLOCK ; + + ret =nvme_ns_ioctl (ns, cmd, arg);nvme_put_ns_from_disk (head, srcu_idx);return ret; }