nvme: change locking for the per-subsystem controller list
Christoph Hellwig
maintainerRFC PATCH 1/2 nvme: change locking for the per-subsystem controller list
Life becomes a lot simpler if we just use the global nvme_subsystems_lock to protect this list. Given that it is only accessed during controller probing and removal that isn't a scalability problem either.
--- drivers/nvme/host/core.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-)
drivers/nvme/host/core.c +12 -18
@@ -2346,13 +2346,11 @@ static int nvme_active_ctrls(struct nvme_subsystem *subsys)int count =0 ;struct nvme_ctrl *ctrl; -mutex_lock (&subsys->lock);list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) {if (ctrl->state !=NVME_CTRL_DELETING && ctrl->state !=NVME_CTRL_DEAD ) count++; } -mutex_unlock (&subsys->lock);return count; }
@@ -2394,6 +2392,9 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)mutex_lock (&nvme_subsystems_lock); found =__nvme_find_get_subsystem (subsys->subnqn);if (found) { +__nvme_release_subsystem (subsys); + subsys = found; +/* * Verify that the subsystem actually supports multiple * controllers, else bail out.
@@ -2402,14 +2403,10 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) nvme_active_ctrls(found) && !(id->cmic & (1 << 1))) {dev_err (ctrl->device,"ignoring ctrl due to duplicate subnqn (%s).\n" , - found->subnqn); -nvme_put_subsystem (found); + subsys->subnqn); ret = -EINVAL ; - goto out_unlock; + goto out_put_subsystem; } - -__nvme_release_subsystem (subsys); - subsys = found; }else { ret =device_add (&subsys->dev);if (ret) {
@@ -2421,23 +2418,20 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)list_add_tail (&subsys->entry, &nvme_subsystems); } - ctrl->subsys = subsys; -mutex_unlock (&nvme_subsystems_lock); -if (sysfs_create_link (&subsys->dev.kobj, &ctrl->device->kobj,dev_name (ctrl->device))) {dev_err (ctrl->device,"failed to create sysfs link from subsystem.\n" ); -/* the transport driver will eventually put the subsystem */ -return -EINVAL ; + goto out_put_subsystem; } -mutex_lock (&subsys->lock); + ctrl->subsys = subsys;list_add_tail (&ctrl->subsys_entry, &subsys->ctrls); -mutex_unlock (&subsys->lock); - +mutex_unlock (&nvme_subsystems_lock);return 0 ; +out_put_subsystem: +nvme_put_subsystem (subsys); out_unlock:mutex_unlock (&nvme_subsystems_lock);put_device (&subsys->dev);
@@ -3695,10 +3689,10 @@ static void nvme_free_ctrl(struct device *dev)__free_page (ctrl->discard_page);if (subsys) { -mutex_lock (&subsys->lock); +mutex_lock (&nvme_subsystems_lock);list_del (&ctrl->subsys_entry); -mutex_unlock (&subsys->lock);sysfs_remove_link (&subsys->dev.kobj,dev_name (ctrl->device)); +mutex_unlock (&nvme_subsystems_lock); } ctrl->ops->free_ctrl (ctrl);
Signature · 1 line
2.20.1
Christoph Hellwig
maintainerRFC PATCH 2/2 nvme: validate cntlid during controller initialisation
The CNTLID value is required to be unique, and we do rely on this for correct operation. So reject any controller for which a non-unique CNTLID has been detected.
Based on a patch from Hannes Reinecke.
--- drivers/nvme/host/core.c | 41 +++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-)
drivers/nvme/host/core.c +24 -17
@@ -2341,18 +2341,33 @@ static const struct attribute_group *nvme_subsys_attrs_groups[] = { NULL, }; -static int nvme_active_ctrls (struct nvme_subsystem *subsys) +static bool nvme_validate_cntlid (struct nvme_subsystem *subsys, +struct nvme_ctrl *ctrl,struct nvme_id_ctrl *id) { -int count =0 ; -struct nvme_ctrl *ctrl; +struct nvme_ctrl *tmp; -list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) { -if (ctrl->state !=NVME_CTRL_DELETING && - ctrl->state !=NVME_CTRL_DEAD ) - count++; +list_for_each_entry (tmp, &subsys->ctrls, subsys_entry) { +if (ctrl->state ==NVME_CTRL_DELETING || + ctrl->state ==NVME_CTRL_DEAD ) +continue ; + +if (tmp->cntlid == ctrl->cntlid) { +dev_err (ctrl->device, +"Duplicate cntlid %u, rejecting\n" , + ctrl->cntlid); +return false; + } + +if ((id->cmic & (1 <<1 )) || + (ctrl->opts && ctrl->opts->discovery_nqn)) +continue ; + +dev_err (ctrl->device, +"Subsystem does not support multiple controllers\n" ); +return false; } -return count; +return true; }static int nvme_init_subsystem (struct nvme_ctrl *ctrl,struct nvme_id_ctrl *id)
@@ -2395,15 +2410,7 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)__nvme_release_subsystem (subsys); subsys = found; -/* -* Verify that the subsystem actually supports multiple -* controllers, else bail out. -*/ -if (!(ctrl->opts && ctrl->opts->discovery_nqn) && -nvme_active_ctrls (found) && !(id->cmic & (1 <<1 ))) { -dev_err (ctrl->device, -"ignoring ctrl due to duplicate subnqn (%s).\n" , - subsys->subnqn); +if (!nvme_validate_cntlid (subsys, ctrl, id)) { ret = -EINVAL ; goto out_put_subsystem; }
Signature · 1 line
2.20.1
Keith Busch
maintainerRFC PATCH 2/2 nvme: validate cntlid during controller initialisation
drivers/nvme/host/core.c static const struct attribute_group *nvme_subsys_attrs_groups[] = { · line 2357
Quoted 9 lines · Christoph Hellwig
drivers/nvme/host/core.c +9
-0+list_for_each_entry (tmp, &subsys->ctrls, subsys_entry) { +if (ctrl->state ==NVME_CTRL_DELETING || + ctrl->state ==NVME_CTRL_DEAD ) +continue ; + +if (tmp->cntlid == ctrl->cntlid) { +dev_err (ctrl->device, +"Duplicate cntlid %u, rejecting\n" , + ctrl->cntlid);
Patch looks great, but let's make this print more informative by showing tmp's device name too:
dev_err(ctrl->device, "Duplicate cntlid %u with %s, rejecting\n", ctrl->cntlid, dev_name(tmp->device));
Christoph Hellwig
maintainerRFC PATCH 2/2 nvme: validate cntlid during controller initialisation
Quoted 14 lines · Keith Busch
drivers/nvme/host/core.c static const struct attribute_group *nvme_subsys_attrs_groups[] = { · line 2357
Quoted 9 lines · Christoph Hellwig
drivers/nvme/host/core.c +9
-0+list_for_each_entry (tmp, &subsys->ctrls, subsys_entry) { +if (ctrl->state ==NVME_CTRL_DELETING || + ctrl->state ==NVME_CTRL_DEAD ) +continue ; + +if (tmp->cntlid == ctrl->cntlid) { +dev_err (ctrl->device, +"Duplicate cntlid %u, rejecting\n" , + ctrl->cntlid);Patch looks great, but let's make this print more informative by showing tmp's device name too:
Quoted 3 lines
dev_err(ctrl->device, "Duplicate cntlid %u with %s, rejecting\n", ctrl->cntlid, dev_name(tmp->device));
Sure. The printk was taken 1:1 from the existing code, but I could improve it.
RFC PATCH 2/2 nvme: validate cntlid during controller initialisation
In reply to Christoph HellwigQuoted 24 lines · Christoph Hellwig
The CNTLID value is required to be unique, and we do rely on this for correct operation. So reject any controller for which a non-unique CNTLID has been detected.
Based on a patch from Hannes Reinecke.
Signed-off-by Christoph Hellwig <hch at lst.de>--- drivers/nvme/host/core.c | 41 +++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-)drivers/nvme/host/core.c +2
-2@@ -2341,18 +2341,33 @@ static const struct attribute_group *nvme_subsys_attrs_groups[] = { NULL, }; -static int nvme_active_ctrls (struct nvme_subsystem *subsys) +static bool nvme_validate_cntlid (struct nvme_subsystem *subsys, +struct nvme_ctrl *ctrl,struct nvme_id_ctrl *id) { -int count =0 ;
drivers/nvme/host/core.c static const struct attribute_group *nvme_subsys_attrs_groups[] = { · line 2351
Quoted 10 lines
drivers/nvme/host/core.c +4
-5-struct nvme_ctrl *ctrl; +struct nvme_ctrl *tmp; -list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) { -if (ctrl->state !=NVME_CTRL_DELETING && - ctrl->state !=NVME_CTRL_DEAD ) - count++; +list_for_each_entry (tmp, &subsys->ctrls, subsys_entry) { +if (ctrl->state ==NVME_CTRL_DELETING || + ctrl->state ==NVME_CTRL_DEAD )
nvme_change_ctrl_state() is using spinlock to protect the ctrl->state. Do we need to protect above check with the spinlock ?
Quoted 41 lines
drivers/nvme/host/core.c +18
-10+continue ; + +if (tmp->cntlid == ctrl->cntlid) { +dev_err (ctrl->device, +"Duplicate cntlid %u, rejecting\n" , + ctrl->cntlid); +return false; + } + +if ((id->cmic & (1 <<1 )) || + (ctrl->opts && ctrl->opts->discovery_nqn)) +continue ; + +dev_err (ctrl->device, +"Subsystem does not support multiple controllers\n" ); +return false; } -return count; +return true; }static int nvme_init_subsystem (struct nvme_ctrl *ctrl,struct nvme_id_ctrl *id)@@ -2395,15 +2410,7 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)__nvme_release_subsystem (subsys); subsys = found; -/* -* Verify that the subsystem actually supports multiple -* controllers, else bail out. -*/ -if (!(ctrl->opts && ctrl->opts->discovery_nqn) && -nvme_active_ctrls (found) && !(id->cmic & (1 <<1 ))) { -dev_err (ctrl->device, -"ignoring ctrl due to duplicate subnqn (%s).\n" , - subsys->subnqn); +if (!nvme_validate_cntlid (subsys, ctrl, id)) { ret = -EINVAL ; goto out_put_subsystem; }
Christoph Hellwig
maintainerRFC PATCH 2/2 nvme: validate cntlid during controller initialisation
Quoted 5 lines · Chaitanya Kulkarni
drivers/nvme/host/core.c static const struct attribute_group *nvme_subsys_attrs_groups[] = { · line 2351
Quoted 3 lines
drivers/nvme/host/core.c +3
-0+list_for_each_entry (tmp, &subsys->ctrls, subsys_entry) { +if (ctrl->state ==NVME_CTRL_DELETING || + ctrl->state ==NVME_CTRL_DEAD )
nvme_change_ctrl_state()is using spinlock to protect thectrl->state. Do we need to protect above check with the spinlock ?
There isn't really any point in taking a lock for doing a read of a single value from a less than register sized field. As soon as we drop the lock the value might change anyway.
RFC PATCH 1/2 nvme: change locking for the per-subsystem controller list
In reply to Christoph HellwigQuoted 18 lines · Christoph Hellwig
Life becomes a lot simpler if we just use the global
nvme_subsystems_lockto protect this list. Given that it is only accessed during controller probing and removal that isn't a scalability problem either.Signed-off-by Christoph Hellwig <hch at lst.de>--- drivers/nvme/host/core.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-)drivers/nvme/host/core.c +0
-0@@ -2346,13 +2346,11 @@ static int nvme_active_ctrls(struct nvme_subsystem *subsys)int count =0 ;struct nvme_ctrl *ctrl;
drivers/nvme/host/core.c static int nvme_active_ctrls(struct nvme_subsystem *subsys) · line 2356
Quoted 10 lines
drivers/nvme/host/core.c +0
-2-mutex_lock (&subsys->lock);list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) {if (ctrl->state !=NVME_CTRL_DELETING && ctrl->state !=NVME_CTRL_DEAD ) count++; } -mutex_unlock (&subsys->lock);return count; }
Would lockdep_assert_held(&nvme_subsystems_lock); be beneficial here?
Thanks,
Ed
Christoph Hellwig
maintainerRFC PATCH 1/2 nvme: change locking for the per-subsystem controller list
Quoted 10 lines · Edmund Nadolski
Quoted 10 lines
drivers/nvme/host/core.c +0
-0@@ -2346,13 +2346,11 @@ static int nvme_active_ctrls(struct nvme_subsystem *subsys)int count =0 ;struct nvme_ctrl *ctrl; -mutex_lock (&subsys->lock);list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) { if (ctrl->state != NVME_CTRL_DELETING &&
Quoted 8 lines
drivers/nvme/host/core.c static int nvme_active_ctrls(struct nvme_subsystem *subsys) · line 2356
Quoted 6 lines
drivers/nvme/host/core.c +0
-1ctrl->state != NVME_CTRL_DEAD) count++; } -mutex_unlock (&subsys->lock);return count; }Would
lockdep_assert_held(&nvme_subsystems_lock); be beneficial here?
It certainly would not hurt, although it seems a little overkill for a trivial helper with a single caller.
RFC PATCH 1/2 nvme: change locking for the per-subsystem controller list
Quoted 20 lines · Christoph Hellwig
Quoted 10 lines · Edmund Nadolski
Quoted 10 lines
drivers/nvme/host/core.c +0
-0@@ -2346,13 +2346,11 @@ static int nvme_active_ctrls(struct nvme_subsystem *subsys)int count =0 ;struct nvme_ctrl *ctrl; -mutex_lock (&subsys->lock);list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) { if (ctrl->state != NVME_CTRL_DELETING &&Quoted 8 lines
drivers/nvme/host/core.c static int nvme_active_ctrls(struct nvme_subsystem *subsys) · line 2356
Quoted 6 lines
drivers/nvme/host/core.c +0
-1ctrl->state != NVME_CTRL_DEAD) count++; } -mutex_unlock (&subsys->lock);return count; }Would
lockdep_assert_held(&nvme_subsystems_lock); be beneficial here?
Quoted 2 lines
It certainly would not hurt, although it seems a little overkill for a trivial helper with a single caller.
True, the thought is that it would be indicative that subsys->lock is not needed in this context.
Ed
RFC PATCH 1/2 nvme: change locking for the per-subsystem controller list
In reply to Christoph HellwigQuoted 15 lines · Christoph Hellwig
Life becomes a lot simpler if we just use the global
nvme_subsystems_lockto protect this list. Given that it is only accessed during controller probing and removal that isn't a scalability problem either.Signed-off-by Christoph Hellwig <hch at lst.de>--- drivers/nvme/host/core.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-)drivers/nvme/host/core.c +0
-0
drivers/nvme/host/core.c static int nvme_active_ctrls(struct nvme_subsystem *subsys) · line 2355
Quoted 10 lines
drivers/nvme/host/core.c +0
-2@@ -2346,13 +2346,11 @@ static int nvme_active_ctrls(struct nvme_subsystem *subsys)int count =0 ;struct nvme_ctrl *ctrl; -mutex_lock (&subsys->lock);list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) {if (ctrl->state !=NVME_CTRL_DELETING && ctrl->state !=NVME_CTRL_DEAD ) count++; } -mutex_unlock (&subsys->lock);
Please correct me if I'm wrong.
nvme_free_ctrl() seems to be removing the ctrl from subsys->ctrls which gets added in the nvme_init_subsystem() and it is using subsys->lock.
In order to make above change work across nvme_init_subsytem() and nvme_free_ctrl() and it is not in the hot path, do we need to use nvme_subsystems_lock in nvme_free_ctrl() also ?
Quoted 73 lines
drivers/nvme/host/core.c +12
-16return count; }@@ -2394,6 +2392,9 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)mutex_lock (&nvme_subsystems_lock); found =__nvme_find_get_subsystem (subsys->subnqn);if (found) { +__nvme_release_subsystem (subsys); + subsys = found; +/* * Verify that the subsystem actually supports multiple * controllers, else bail out. @@ -2402,14 +2403,10 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) nvme_active_ctrls(found) && !(id->cmic & (1 << 1))) {dev_err (ctrl->device,"ignoring ctrl due to duplicate subnqn (%s).\n" , - found->subnqn); -nvme_put_subsystem (found); + subsys->subnqn); ret = -EINVAL ; - goto out_unlock; + goto out_put_subsystem; } - -__nvme_release_subsystem (subsys); - subsys = found; }else { ret =device_add (&subsys->dev);if (ret) {@@ -2421,23 +2418,20 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)list_add_tail (&subsys->entry, &nvme_subsystems); } - ctrl->subsys = subsys; -mutex_unlock (&nvme_subsystems_lock); -if (sysfs_create_link (&subsys->dev.kobj, &ctrl->device->kobj,dev_name (ctrl->device))) {dev_err (ctrl->device,"failed to create sysfs link from subsystem.\n" ); -/* the transport driver will eventually put the subsystem */ -return -EINVAL ; + goto out_put_subsystem; } -mutex_lock (&subsys->lock); + ctrl->subsys = subsys;list_add_tail (&ctrl->subsys_entry, &subsys->ctrls); -mutex_unlock (&subsys->lock); - +mutex_unlock (&nvme_subsystems_lock);return 0 ; +out_put_subsystem: +nvme_put_subsystem (subsys); out_unlock:mutex_unlock (&nvme_subsystems_lock);put_device (&subsys->dev);@@ -3695,10 +3689,10 @@ static void nvme_free_ctrl(struct device *dev)__free_page (ctrl->discard_page);if (subsys) { -mutex_lock (&subsys->lock); +mutex_lock (&nvme_subsystems_lock);list_del (&ctrl->subsys_entry); -mutex_unlock (&subsys->lock);sysfs_remove_link (&subsys->dev.kobj,dev_name (ctrl->device)); +mutex_unlock (&nvme_subsystems_lock); } ctrl->ops->free_ctrl (ctrl);
RFC PATCH 1/2 nvme: change locking for the per-subsystem controller list
Please ignore my earlier comment, I can see it is already been done.
Quoted 109 lines · Chaitanya Kulkarni
Quoted 15 lines · Christoph Hellwig
Life becomes a lot simpler if we just use the global
nvme_subsystems_lockto protect this list. Given that it is only accessed during controller probing and removal that isn't a scalability problem either.Signed-off-by Christoph Hellwig <hch at lst.de>--- drivers/nvme/host/core.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-)drivers/nvme/host/core.c +0
-0drivers/nvme/host/core.c static int nvme_active_ctrls(struct nvme_subsystem *subsys) · line 2355
Quoted 10 lines
drivers/nvme/host/core.c +0
-2@@ -2346,13 +2346,11 @@ static int nvme_active_ctrls(struct nvme_subsystem *subsys)int count =0 ;struct nvme_ctrl *ctrl; -mutex_lock (&subsys->lock);list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) {if (ctrl->state !=NVME_CTRL_DELETING && ctrl->state !=NVME_CTRL_DEAD ) count++; } -mutex_unlock (&subsys->lock);Please correct me if I'm wrong.
nvme_free_ctrl()seems to be removing the ctrl fromsubsys->ctrlswhich gets added in thenvme_init_subsystem()and it is usingsubsys->lock.In order to make above change work across
nvme_init_subsytem()andnvme_free_ctrl()and it is not in the hot path, do we need to usenvme_subsystems_lockinnvme_free_ctrl()also ?Quoted 73 lines
drivers/nvme/host/core.c +12
-16return count; }@@ -2394,6 +2392,9 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)mutex_lock (&nvme_subsystems_lock); found =__nvme_find_get_subsystem (subsys->subnqn);if (found) { +__nvme_release_subsystem (subsys); + subsys = found; +/* * Verify that the subsystem actually supports multiple * controllers, else bail out. @@ -2402,14 +2403,10 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) nvme_active_ctrls(found) && !(id->cmic & (1 << 1))) {dev_err (ctrl->device,"ignoring ctrl due to duplicate subnqn (%s).\n" , - found->subnqn); -nvme_put_subsystem (found); + subsys->subnqn); ret = -EINVAL ; - goto out_unlock; + goto out_put_subsystem; } - -__nvme_release_subsystem (subsys); - subsys = found; }else { ret =device_add (&subsys->dev);if (ret) {@@ -2421,23 +2418,20 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)list_add_tail (&subsys->entry, &nvme_subsystems); } - ctrl->subsys = subsys; -mutex_unlock (&nvme_subsystems_lock); -if (sysfs_create_link (&subsys->dev.kobj, &ctrl->device->kobj,dev_name (ctrl->device))) {dev_err (ctrl->device,"failed to create sysfs link from subsystem.\n" ); -/* the transport driver will eventually put the subsystem */ -return -EINVAL ; + goto out_put_subsystem; } -mutex_lock (&subsys->lock); + ctrl->subsys = subsys;list_add_tail (&ctrl->subsys_entry, &subsys->ctrls); -mutex_unlock (&subsys->lock); - +mutex_unlock (&nvme_subsystems_lock);return 0 ; +out_put_subsystem: +nvme_put_subsystem (subsys); out_unlock:mutex_unlock (&nvme_subsystems_lock);put_device (&subsys->dev);@@ -3695,10 +3689,10 @@ static void nvme_free_ctrl(struct device *dev)__free_page (ctrl->discard_page);if (subsys) { -mutex_lock (&subsys->lock); +mutex_lock (&nvme_subsystems_lock);list_del (&ctrl->subsys_entry); -mutex_unlock (&subsys->lock);sysfs_remove_link (&subsys->dev.kobj,dev_name (ctrl->device)); +mutex_unlock (&nvme_subsystems_lock); } ctrl->ops->free_ctrl (ctrl);