Skip to contentSkip to conversation

nvdimm/btt: Handle preemption in BTT lane acquisition

PATCH Superseded Alison Schofield 1 participant 2 messages Apr 24 – Apr 25 libnvdimm: non-volatile memory device

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Alison Schofield

author maintainer
#0 lore raw

PATCH 1/1 nvdimm/btt: Handle preemption in BTT lane acquisition

BTT (Block Translation Table) makes persistent memory safe for block I/O by guaranteeing atomic sector updates. It uses reserved lanes for in-flight BTT operations, which must be used exclusively.

The btt-check unit test reports data mismatches during BTT I/O due to a race in lane acquisition, leading to silent data corruption.

BTT lane acquisition uses per-CPU recursion tracking with migrate_disable(). However, migrate_disable() does not prevent preemption, so another task can run on the same CPU and share the recursion state. That task can observe a non-zero recursion count, bypass locking, and use the same lane at the same time.

Track lane ownership per task and only allow lockless recursion for the owning task. Otherwise, serialize access with the lane spinlock.

Found with the NDCTL unit test btt-check.sh

Fixes 36c75ce3bd29 ("nd_btt: Make BTT lanes preemptible")
Assisted-by Claude Sonnet 4.5

---

A new unit test to stress this is under review here:
https://lore.kernel.org/nvdimm/[email protected]/

 drivers/nvdimm/nd.h          |  1 +
 drivers/nvdimm/region_devs.c | 48 +++++++++++++++++++++---------------
 2 files changed, 29 insertions(+), 20 deletions(-)
drivers/nvdimm/nd.h +1 -0
index b199eea3260e..424c38ca4960 100644
@@ -368,6 +368,7 @@ unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd);
 struct nd_percpu_lane {
 	int count;
 	spinlock_t lock;
+	struct task_struct *owner;
 };
 
 enum nd_label_flags {
drivers/nvdimm/region_devs.c +28 -20

74 lines not on this page · raw message

base-commit: 028ef9c96e96197026887c0f092424679298aae8

Signature · 1 line
2.37.3
author maintainer
#1 lore raw

On Fri, Apr 24, 2026 at 04:44:03PM -0700, Alison Schofield wrote:

snip

Quoted 41 lines
drivers/nvdimm/region_devs.c +7 -13
index e35c2e18518f..830241b93bf2 100644
@@ -905,11 +905,10 @@ void nd_region_advance_seeds(struct nd_region *nd_region, struct device *dev)
  * @nd_region: region id and number of lanes possible
  *
  * A lane correlates to a BLK-data-window and/or a log slot in the BTT.
- * We optimize for the common case where there are 256 lanes, one
- * per-cpu.  For larger systems we need to lock to share lanes.  For now
- * this implementation assumes the cost of maintaining an allocator for
- * free lanes is on the order of the lock hold time, so it implements a
- * static lane = cpu % num_lanes mapping.
+ * Lanes are shared across CPUs using a static lane = cpu % num_lanes
+ * mapping, with a per-lane spinlock to serialize access when multiple
+ * tasks share a lane (including when preemption causes multiple tasks
+ * to run on the same CPU).
  *
  * In the case of a BTT instance on top of a BLK namespace a lane may be
  * acquired recursively.  We lock on the first instance.
@@ -920,35 +919,44 @@ void nd_region_advance_seeds(struct nd_region *nd_region, struct device *dev)
 unsigned int nd_region_acquire_lane(struct nd_region *nd_region)
 {
 	unsigned int cpu, lane;
+	struct nd_percpu_lane *ndl;
 
 	migrate_disable();
 	cpu = smp_processor_id();
-	if (nd_region->num_lanes < nr_cpu_ids) {
-		struct nd_percpu_lane *ndl_lock, *ndl_count;
-
+	if (nd_region->num_lanes < nr_cpu_ids)
 		lane = cpu % nd_region->num_lanes;
-		ndl_count = per_cpu_ptr(nd_region->lane, cpu);
-		ndl_lock = per_cpu_ptr(nd_region->lane, lane);
-		if (ndl_count->count++ == 0)
-			spin_lock(&ndl_lock->lock);
-	} else
+	else
 		lane = cpu;
 

drivers/nvdimm/region_devs.c void nd_region_advance_seeds(struct nd_region *nd_region, struct device *dev) · line 940

Quoted 10 lines
drivers/nvdimm/region_devs.c +10 -0
+	/*
+	 * migrate_disable() keeps the lane stable, but does not prevent
+	 * preemption. Only the owning task may recurse without taking the
+	 * lock.
+	 */
+	ndl = per_cpu_ptr(nd_region->lane, lane);
+	if (READ_ONCE(ndl->owner) != current) {
+		spin_lock(&ndl->lock);
+		WRITE_ONCE(ndl->owner, current);
+	}

Sashiko-AI review[1] pointed out that softirq re-entry could bypass lane ownership checks and deadlock on the same CPU.

In a v2, I'll use spin_(un)lock_bh() (above and below) to protect against that.

[1] https://sashiko.dev/#/patchset/20260424234405.3762827-1-alison.schofield%40intel.com

Quoted 34 lines
drivers/nvdimm/region_devs.c +11 -7
+	ndl->count++;
+
 	return lane;
 }
 EXPORT_SYMBOL(nd_region_acquire_lane);
 
 void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane)
 {
-	if (nd_region->num_lanes < nr_cpu_ids) {
-		unsigned int cpu = smp_processor_id();
-		struct nd_percpu_lane *ndl_lock, *ndl_count;
+	struct nd_percpu_lane *ndl = per_cpu_ptr(nd_region->lane, lane);
 
-		ndl_count = per_cpu_ptr(nd_region->lane, cpu);
-		ndl_lock = per_cpu_ptr(nd_region->lane, lane);
-		if (--ndl_count->count == 0)
-			spin_unlock(&ndl_lock->lock);
+	if (WARN_ON_ONCE(READ_ONCE(ndl->owner) != current))
+		goto out;
+
+	if (--ndl->count == 0) {
+		WRITE_ONCE(ndl->owner, NULL);
+		spin_unlock(&ndl->lock);
 	}
+
+out:
 	migrate_enable();
 }
 EXPORT_SYMBOL(nd_region_release_lane);

base-commit: 028ef9c96e96197026887c0f092424679298aae8

Signature · 1 line
2.37.3