Skip to contentSkip to conversation

nvmet: move percpu handling into nvmet_ns_{enable,disable}

PATCH In review [email protected] 6 participants 9 messages Jan 24, 2025 – Jan 28, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet: move percpu handling into nvmet_ns_{enable,disable}

From: Hannes Reinecke <[email protected]>

The namespace percpu counter protects pending I/O, and we can only safely diable the namespace once the counter drop to zero. So we need to init the percpu counter in nvmet_ns_enable(), and wait for it to drop to zero in nvmet_ns_disable() to avoid having I/O pending after the namespace has been disabled.

Fixes 74d16965d7ac ("nvmet-loop: avoid using mutex in IO hotpath")
---
 drivers/nvme/target/core.c | 44 +++++++++++++++++---------------------
 1 file changed, 20 insertions(+), 24 deletions(-)
drivers/nvme/target/core.c +20 -24
index 5f7b5d1f78c0..cdf8e16c1c6d 100644
@@ -611,6 +611,9 @@ int nvmet_ns_enable(struct nvmet_ns *ns)
 			goto out_dev_put;
 	}
 
+	if (percpu_ref_init(&ns->ref, nvmet_destroy_namespace, 0, GFP_KERNEL))
+		goto out_pr_exit;
+
 	nvmet_ns_changed(subsys, ns->nsid);
 	ns->enabled = true;
 	xa_set_mark(&subsys->namespaces, ns->nsid, NVMET_NS_ENABLED);
@@ -618,6 +621,9 @@ int nvmet_ns_enable(struct nvmet_ns *ns)
 out_unlock:
 	mutex_unlock(&subsys->lock);
 	return ret;
+out_pr_exit:
+	if (ns->pr.enable)
+		nvmet_pr_exit_ns(ns);
 out_dev_put:
 	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
 		pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid));
@@ -643,6 +649,19 @@ void nvmet_ns_disable(struct nvmet_ns *ns)
 
 	mutex_unlock(&subsys->lock);
 
+	/*
+	 * Now that we removed the namespaces from the lookup list, we
+	 * can kill the per_cpu ref and wait for any remaining references
+	 * to be dropped, as well as a RCU grace period for anyone only
+	 * using the namepace under rcu_read_lock().  Note that we can't
+	 * use call_rcu here as we need to ensure the namespaces have
+	 * been fully destroyed before unloading the module.
+	 */
+	percpu_ref_kill(&ns->ref);
+	synchronize_rcu();
+	wait_for_completion(&ns->disable_done);
+	percpu_ref_exit(&ns->ref);
+
 	if (ns->pr.enable)
 		nvmet_pr_exit_ns(ns);
 
@@ -660,25 +679,7 @@ void nvmet_ns_free(struct nvmet_ns *ns)
 	nvmet_ns_disable(ns);
 
 	mutex_lock(&subsys->lock);
-
 	xa_erase(&subsys->namespaces, ns->nsid);
-
-	mutex_unlock(&subsys->lock);
-
-	/*
-	 * Now that we removed the namespaces from the lookup list, we
-	 * can kill the per_cpu ref and wait for any remaining references
-	 * to be dropped, as well as a RCU grace period for anyone only
-	 * using the namepace under rcu_read_lock().  Note that we can't
-	 * use call_rcu here as we need to ensure the namespaces have
-	 * been fully destroyed before unloading the module.
-	 */
-	percpu_ref_kill(&ns->ref);
-	synchronize_rcu();
-	wait_for_completion(&ns->disable_done);
-	percpu_ref_exit(&ns->ref);
-
-	mutex_lock(&subsys->lock);
 	subsys->nr_namespaces--;
 	mutex_unlock(&subsys->lock);
 
@@ -708,11 +709,8 @@ struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
 	ns->nsid = nsid;
 	ns->subsys = subsys;
 
-	if (percpu_ref_init(&ns->ref, nvmet_destroy_namespace, 0, GFP_KERNEL))
-		goto out_free;
-
 	if (xa_insert(&subsys->namespaces, ns->nsid, ns, GFP_KERNEL))
-		goto out_exit;
+		goto out_free;
 
 	subsys->nr_namespaces++;
 
@@ -728,8 +726,6 @@ struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
 	ns->csi = NVME_CSI_NVM;
 
 	return ns;
-out_exit:
-	percpu_ref_exit(&ns->ref);
 out_free:
 	kfree(ns);
 out_unlock:
Signature · 1 line
2.35.3
maintainer
#1 lore raw
Quoted 9 lines · On 24/01/2025 10:25, [email protected] wrote:

From: Hannes Reinecke <[email protected]>

The namespace percpu counter protects pending I/O, and we can only safely diable the namespace once the counter drop to zero. So we need to init the percpu counter in nvmet_ns_enable(), and wait for it to drop to zero in nvmet_ns_disable() to avoid having I/O pending after the namespace has been disabled.

Fixes 74d16965d7ac ("nvmet-loop: avoid using mutex in IO hotpath")

Can you please describe the bug and scenario which you are hitting this bug? It is also unclear how the above patch is causing this.

From quick look patch itself looks reasonable.

#2 lore raw
Quoted 12 lines · Sagi Grimberg
Quoted 8 lines · On 24/01/2025 10:25, [email protected] wrote:

From: Hannes Reinecke <[email protected]>

The namespace percpu counter protects pending I/O, and we can only safely diable the namespace once the counter drop to zero. So we need to init the percpu counter in nvmet_ns_enable(), and wait for it to drop to zero in nvmet_ns_disable() to avoid having I/O pending after the namespace has been disabled.

Quoted 6 lines
Quoted 1 line
Fixes 74d16965d7ac ("nvmet-loop: avoid using mutex in IO hotpath")

Can you please describe the bug and scenario which you are hitting this bug? It is also unclear how the above patch is causing this.

From quick look patch itself looks reasonable.

Should've said: this is the fix to the reported blktest failure on nvme/058...

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
Reviewed-by
#3 lore raw
Quoted 15 lines · Hannes Reinecke
Quoted 14 lines · Sagi Grimberg
Quoted 9 lines · On 24/01/2025 10:25, [email protected] wrote:

From: Hannes Reinecke <[email protected]>

The namespace percpu counter protects pending I/O, and we can only safely diable the namespace once the counter drop to zero. So we need to init the percpu counter in nvmet_ns_enable(), and wait for it to drop to zero in nvmet_ns_disable() to avoid having I/O pending after the namespace has been disabled.

Fixes 74d16965d7ac ("nvmet-loop: avoid using mutex in IO hotpath")
Quoted 8 lines
Quoted 4 lines

Can you please describe the bug and scenario which you are hitting this bug? It is also unclear how the above patch is causing this.

 From quick look patch itself looks reasonable.

Should've said: this is the fix to the reported blktest failure on nvme/058...

Thanks Hannes for addressing this issue!

I think while implementing 74d16965d7ac ("nvmet-loop: avoid using mutex in IO hotpath"), I moved initialization of ns->ref from nvmet_ns_enable() to nvmet_ns_alloc() as well moved killing of ns->ref logic from nvmet_ns_disable() to nvmet_ns_free() and that probably caused the observed symptom. I thought we need to init ns->ref while we insert ns in Xarray and kill ns->ref while we erase it from Xarray, but that was not correct. My bad!

Though I was never able to recreate this issue on my setup even after running nvme/058 hundreds of times, still I think the issue reported by Shinichiro appears plausible. So we need this fix and your changes look good to me.

#4 lore raw
Quoted 2 lines · Nilay Shroff

[ .. ]>

Quoted 5 lines

Though I was never able to recreate this issue on my setup even after running nvme/058 hundreds of times, still I think the issue reported by Shinichiro appears plausible. So we need this fix and your changes look good to me.

Yeah, we definitely need this. I was able to trigger it quite consistently with qemu after just a few retries.

And I guess we can now kill the ->enable setting completely, and just use the xarray mark instead.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#5 lore raw
Quoted 12 lines · Hannes Reinecke
Quoted 2 lines · Nilay Shroff

[ .. ]>

Quoted 5 lines

Though I was never able to recreate this issue on my setup even after running nvme/058 hundreds of times, still I think the issue reported by Shinichiro appears plausible. So we need this fix and your changes look good to me.

Yeah, we definitely need this. I was able to trigger it quite consistently with qemu after just a few retries.

I was using PPC machine for testing, on which I couldn't recreate, but I would try using qemu. I saw that Shinichiro also reported it on qemu.

Thanks,
--Nilay

maintainer
#8 lore raw
In reply to [email protected]
Quoted 9 lines · On Fri, Jan 24, 2025 at 09:25:05AM +0100, [email protected] wrote:

From: Hannes Reinecke <[email protected]>

The namespace percpu counter protects pending I/O, and we can only safely diable the namespace once the counter drop to zero. So we need to init the percpu counter in nvmet_ns_enable(), and wait for it to drop to zero in nvmet_ns_disable() to avoid having I/O pending after the namespace has been disabled.

Fixes 74d16965d7ac ("nvmet-loop: avoid using mutex in IO hotpath")

We'll have to wait for after the merge window when the block tree merges up to apply this: the nvme-6.14 branch doesn't contain the offending commit yet.

maintainer
#6 lore raw
In reply to Hannes Reinecke
Quoted 13 lines
Quoted 13 lines · On 24/01/2025 10:25, [email protected] wrote:
Quoted 9 lines

From: Hannes Reinecke <[email protected]>

The namespace percpu counter protects pending I/O, and we can only safely diable the namespace once the counter drop to zero. So we need to init the percpu counter in nvmet_ns_enable(), and wait for it to drop to zero in nvmet_ns_disable() to avoid having I/O pending after the namespace has been disabled.

Fixes 74d16965d7ac ("nvmet-loop: avoid using mutex in IO hotpath")
Quoted 7 lines
Quoted 4 lines

Can you please describe the bug and scenario which you are hitting this bug? It is also unclear how the above patch is causing this.

 From quick look patch itself looks reasonable.

Should've said: this is the fix to the reported blktest failure on nvme/058...

OK, then please resend this patch that explains the exact issues that it solves. Also, please rephrase your patch title to describe that this is a bug fix. i.e. "nvmet: fix ...."

#7 lore raw
In reply to Hannes Reinecke
Quoted 6 lines · Hannes Reinecke
Quoted 3 lines

It is also unclear how the above patch is causing this.

From quick look patch itself looks reasonable.

Should've said: this is the fix to the reported blktest failure on nvme/058...

Can you please resend with that included?