Skip to contentSkip to conversation

nvme: use ctrl state accessor

PATCH Superseded Keith Busch 3 participants 3 messages Jan 24, 2024 – Jan 25, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme: use ctrl state accessor

From: Keith Busch <[email protected]>

The ctrl->state value is updated in another thread using WRITE_ONCE, so ensure all the readers use the appropriate accessor.

---
 drivers/nvme/host/auth.c      |  2 +-
 drivers/nvme/host/fabrics.h   |  8 +++++---
 drivers/nvme/host/multipath.c | 15 ++++++++-------
 drivers/nvme/host/sysfs.c     |  6 +++---
 4 files changed, 17 insertions(+), 14 deletions(-)
drivers/nvme/host/auth.c +1 -1
index 72c0525c75f50..2a12ee878783f 100644
@@ -897,7 +897,7 @@ static void nvme_ctrl_auth_work(struct work_struct *work)
 	 * If the ctrl is no connected, bail as reconnect will handle
 	 * authentication.
 	 */
-	if (ctrl->state != NVME_CTRL_LIVE)
+	if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE)
 		return;
 
 	/* Authenticate admin queue first */
drivers/nvme/host/fabrics.h +5 -3
index fbaee5a7be196..06cc54851b1be 100644
@@ -185,9 +185,11 @@ static inline bool
 nvmf_ctlr_matches_baseopts(struct nvme_ctrl *ctrl,
 			struct nvmf_ctrl_options *opts)
 {
-	if (ctrl->state == NVME_CTRL_DELETING ||
-	    ctrl->state == NVME_CTRL_DELETING_NOIO ||
-	    ctrl->state == NVME_CTRL_DEAD ||
+	enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
+
+	if (state == NVME_CTRL_DELETING ||
+	    state == NVME_CTRL_DELETING_NOIO ||
+	    state == NVME_CTRL_DEAD ||
 	    strcmp(opts->subsysnqn, ctrl->opts->subsysnqn) ||
 	    strcmp(opts->host->nqn, ctrl->opts->host->nqn) ||
 	    !uuid_equal(&opts->host->id, &ctrl->opts->host->id))
drivers/nvme/host/multipath.c +8 -7
index 2dd4137a08b28..5bc72653013c5 100644
@@ -156,7 +156,7 @@ void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
 		if (!ns->head->disk)
 			continue;
 		kblockd_schedule_work(&ns->head->requeue_work);
-		if (ctrl->state == NVME_CTRL_LIVE)
+		if (nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE)
 			disk_uevent(ns->head->disk, KOBJ_CHANGE);
 	}
 	up_read(&ctrl->namespaces_rwsem);
@@ -223,13 +223,14 @@ void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
 
 static bool nvme_path_is_disabled(struct nvme_ns *ns)
 {
+	enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
+
 	/*
 	 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
 	 * still be able to complete assuming that the controller is connected.
 	 * Otherwise it will fail immediately and return to the requeue list.
 	 */
-	if (ns->ctrl->state != NVME_CTRL_LIVE &&
-	    ns->ctrl->state != NVME_CTRL_DELETING)
+	if (state != NVME_CTRL_LIVE && state != NVME_CTRL_DELETING)
 		return true;
 	if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
 	    !test_bit(NVME_NS_READY, &ns->flags))
@@ -331,7 +332,7 @@ static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
 
 static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
 {
-	return ns->ctrl->state == NVME_CTRL_LIVE &&
+	return nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE &&
 		ns->ana_state == NVME_ANA_OPTIMIZED;
 }
 
@@ -358,7 +359,7 @@ static bool nvme_available_path(struct nvme_ns_head *head)
 	list_for_each_entry_rcu(ns, &head->list, siblings) {
 		if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags))
 			continue;
-		switch (ns->ctrl->state) {
+		switch (nvme_ctrl_state(ns->ctrl)) {
 		case NVME_CTRL_LIVE:
 		case NVME_CTRL_RESETTING:
 		case NVME_CTRL_CONNECTING:
@@ -667,7 +668,7 @@ static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
 	 * controller is ready.
 	 */
 	if (nvme_state_is_live(ns->ana_state) &&
-	    ns->ctrl->state == NVME_CTRL_LIVE)
+	    nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE)
 		nvme_mpath_set_live(ns);
 }
 
@@ -748,7 +749,7 @@ static void nvme_ana_work(struct work_struct *work)
 {
 	struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
 
-	if (ctrl->state != NVME_CTRL_LIVE)
+	if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE)
 		return;
 
 	nvme_read_ana_log(ctrl);
drivers/nvme/host/sysfs.c +3 -3
index 754e911110420..6b2f06f190de3 100644
@@ -311,6 +311,7 @@ static ssize_t nvme_sysfs_show_state(struct device *dev,
 				     char *buf)
 {
 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+	unsigned state = (unsigned)nvme_ctrl_state(ctrl);
 	static const char *const state_name[] = {
 		[NVME_CTRL_NEW]		= "new",
 		[NVME_CTRL_LIVE]	= "live",
@@ -321,9 +322,8 @@ static ssize_t nvme_sysfs_show_state(struct device *dev,
 		[NVME_CTRL_DEAD]	= "dead",
 	};
 
-	if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
-	    state_name[ctrl->state])
-		return sysfs_emit(buf, "%s\n", state_name[ctrl->state]);
+	if (state < ARRAY_SIZE(state_name) && state_name[state])
+		return sysfs_emit(buf, "%s\n", state_name[state]);
 
 	return sysfs_emit(buf, "unknown state\n");
 }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 35 lines · Keith Busch

From: Keith Busch <[email protected]>

The ctrl->state value is updated in another thread using WRITE_ONCE, so ensure all the readers use the appropriate accessor.

---
  drivers/nvme/host/auth.c      |  2 +-
  drivers/nvme/host/fabrics.h   |  8 +++++---
  drivers/nvme/host/multipath.c | 15 ++++++++-------
  drivers/nvme/host/sysfs.c     |  6 +++---
  4 files changed, 17 insertions(+), 14 deletions(-)
drivers/nvme/host/auth.c +1 -1
index 72c0525c75f50..2a12ee878783f 100644
@@ -897,7 +897,7 @@ static void nvme_ctrl_auth_work(struct work_struct *work)
  	 * If the ctrl is no connected, bail as reconnect will handle
  	 * authentication.
  	 */
-	if (ctrl->state != NVME_CTRL_LIVE)
+	if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE)
  		return;
 
  	/* Authenticate admin queue first */
drivers/nvme/host/fabrics.h +0 -1
index fbaee5a7be196..06cc54851b1be 100644
@@ -185,9 +185,11 @@ static inline bool
  nvmf_ctlr_matches_baseopts(struct nvme_ctrl *ctrl,
  			struct nvmf_ctrl_options *opts)
  {
-	if (ctrl->state == NVME_CTRL_DELETING ||

drivers/nvme/host/fabrics.h static inline bool · line 195

Quoted 10 lines
drivers/nvme/host/fabrics.h +5 -2
-	    ctrl->state == NVME_CTRL_DELETING_NOIO ||
-	    ctrl->state == NVME_CTRL_DEAD ||
+	enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
+
+	if (state == NVME_CTRL_DELETING ||
+	    state == NVME_CTRL_DELETING_NOIO ||
+	    state == NVME_CTRL_DEAD ||
  	    strcmp(opts->subsysnqn, ctrl->opts->subsysnqn) ||
  	    strcmp(opts->host->nqn, ctrl->opts->host->nqn) ||
  	    !uuid_equal(&opts->host->id, &ctrl->opts->host->id))

since original code is accessing ctrl->state each time instead of separate variable, why not reading from it from nvme_ctrl_state() that will have READ_ONCE() for each call ?

How about something like this following on the top of yours ? it also removes the need for a variable on the stack :-

drivers/nvme/host/fabrics.h +3 -5
index 06cc54851b1b..78b4cef0cff2 100644
@@ -185,11 +185,9 @@ static inline bool
  nvmf_ctlr_matches_baseopts(struct nvme_ctrl *ctrl,
                         struct nvmf_ctrl_options *opts)
  {
-       enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
-
-       if (state == NVME_CTRL_DELETING ||
-           state == NVME_CTRL_DELETING_NOIO ||
-           state == NVME_CTRL_DEAD ||
+       if (nvme_ctrl_state(ctrl) == NVME_CTRL_DELETING ||
+           nvme_ctrl_state(ctrl) == NVME_CTRL_DELETING_NOIO ||
+           nvme_ctrl_state(ctrl) == NVME_CTRL_DEAD ||
             strcmp(opts->subsysnqn, ctrl->opts->subsysnqn) ||
             strcmp(opts->host->nqn, ctrl->opts->host->nqn) ||
             !uuid_equal(&opts->host->id, &ctrl->opts->host->id))
Quoted 14 lines
drivers/nvme/host/multipath.c +1 -1
index 2dd4137a08b28..5bc72653013c5 100644
@@ -156,7 +156,7 @@ void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
  		if (!ns->head->disk)
  			continue;
  		kblockd_schedule_work(&ns->head->requeue_work);
-		if (ctrl->state == NVME_CTRL_LIVE)
+		if (nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE)
  			disk_uevent(ns->head->disk, KOBJ_CHANGE);
  	}
  	up_read(&ctrl->namespaces_rwsem);

drivers/nvme/host/multipath.c void nvme_mpath_revalidate_paths(struct nvme_ns *ns) · line 227

Quoted 5 lines
drivers/nvme/host/multipath.c +2 -0
@@ -223,13 +223,14 @@ void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
 
  static bool nvme_path_is_disabled(struct nvme_ns *ns)
  {
+	enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
+

nvme_ctrl_state(ns->ctrl) is needed to compile the code, unless I'm totally wrong
here ...

-ck

maintainer
#2 lore raw
Quoted 11 lines · Chaitanya Kulkarni
Quoted 10 lines · Keith Busch
  nvmf_ctlr_matches_baseopts(struct nvme_ctrl *ctrl,
  			struct nvmf_ctrl_options *opts)
  {
(quoted diff) +4 -3
-	if (ctrl->state == NVME_CTRL_DELETING ||
-	    ctrl->state == NVME_CTRL_DELETING_NOIO ||
-	    ctrl->state == NVME_CTRL_DEAD ||
+	enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
+
+	if (state == NVME_CTRL_DELETING ||
+	    state == NVME_CTRL_DELETING_NOIO ||
Quoted 8 lines

drivers/nvme/host/fabrics.h static inline bool · line 193

Quoted 4 lines
drivers/nvme/host/fabrics.h +1 -0
+	    state == NVME_CTRL_DEAD ||
  	    strcmp(opts->subsysnqn, ctrl->opts->subsysnqn) ||
  	    strcmp(opts->host->nqn, ctrl->opts->host->nqn) ||
  	    !uuid_equal(&opts->host->id, &ctrl->opts->host->id))

since original code is accessing ctrl->state each time instead of separate variable, why not reading from it from nvme_ctrl_state() that will have READ_ONCE() for each call ?

I doubt the original compiled to code that really accessed the memory address 3 times. We don't really want to do that either. We just want the snapshot to be consistent throughout these checks.

Quoted 11 lines
Quoted 11 lines
(quoted diff) +1 -1
@@ -156,7 +156,7 @@ void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
  		if (!ns->head->disk)
  			continue;
  		kblockd_schedule_work(&ns->head->requeue_work);
-		if (ctrl->state == NVME_CTRL_LIVE)
+		if (nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE)
  			disk_uevent(ns->head->disk, KOBJ_CHANGE);
  	}
  	up_read(&ctrl->namespaces_rwsem);
@@ -223,13 +223,14 @@ void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
 
Quoted 8 lines

Review void nvme_mpath_revalidate_paths(struct nvme_ns *ns) · line 227

Quoted 4 lines
(quoted diff) +2 -0
  static bool nvme_path_is_disabled(struct nvme_ns *ns)
  {
+	enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
+

nvme_ctrl_state(ns->ctrl) is needed to compile the code, unless I'm totally wrong
here ...

Yep; I tested the wrong config. Will fixup, thanks for the catch.