nvdimm/btt: Handle preemption in BTT lane acquisition
Alison Schofield
maintainerPATCH v3 1/1 nvdimm/btt: Handle preemption in BTT lane acquisition
BTT lanes serialize access to per-lane metadata and workspace state during BTT I/O. The btt-check unit test reports data mismatches during BTT writes due to a race in lane acquisition that can lead to silent data corruption.
The existing lane model uses a spinlock together with a per-CPU recursion count. That recursion model stopped being valid after BTT lanes became preemptible: another task can run on the same CPU, observe a non-zero recursion count, bypass locking, and use the same lane concurrently.
BTT lanes are also held across metadata and data updates that can reach nvdimm_flush(). Some provider flush callbacks can sleep, making a spinlock the wrong primitive for the lane lifetime. That issue predates this fix, but becomes more visible now that BTT lanes are preemptible.
Replace the spinlock with a per-lane mutex, remove the per-CPU recursion fast path, and take the lane lock unconditionally.
Add might_sleep() to catch any future atomic-context caller.
Found with the ndctl unit test btt-check.sh.
---
Aboorva: I appreciate your Tested-by tag, yet due to the churn, I did not apply it. Please re-test with this version.
Changes in v3:
- Replace spinlock with a per-lane mutex (Arboorva)*
- Rebase onto 7.1-rc1
- Update commit log
*Arboorva pointed out that BTT write-side lane ownership can reach provider flush callbacks that may sleep, making the existing spinlock-based lane lifetime invalid. My initial thought was to create a small series where the first patch converts the per-lane lock to a mutex so the lane critical section can safely sleep. That left an intermediate bad state, so the changes are kept together in this single patch.
Changes in v2:
Use spin_(un)lock_bh() (Sashiko AI)
Update commit log per softirq re-enty and spinlock change
A new unit test to stress this is under review here:
https://lore.kernel.org/nvdimm/[email protected]/
drivers/nvdimm/nd.h | 3 +-- drivers/nvdimm/region_devs.c | 50 ++++++++++-------------------------- 2 files changed, 14 insertions(+), 39 deletions(-)
drivers/nvdimm/nd.h +1 -2
@@ -366,8 +366,7 @@ unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd); res; res = next, next = next ? next->sibling : NULL)struct nd_percpu_lane { -int count; -spinlock_t lock; +struct mutex lock;/* serialize lane access */ };enum nd_label_flags {
drivers/nvdimm/region_devs.c +13 -37
78 lines not on this page · raw message
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
Signature · 1 line
2.37.3
Alison Schofield
maintainerQuoted 20 lines · Alison Schofield
BTT lanes serialize access to per-lane metadata and workspace state during BTT I/O. The btt-check unit test reports data mismatches during BTT writes due to a race in lane acquisition that can lead to silent data corruption.
The existing lane model uses a spinlock together with a per-CPU recursion count. That recursion model stopped being valid after BTT lanes became preemptible: another task can run on the same CPU, observe a non-zero recursion count, bypass locking, and use the same lane concurrently.
BTT lanes are also held across metadata and data updates that can reach
nvdimm_flush(). Some provider flush callbacks can sleep, making a spinlock the wrong primitive for the lane lifetime. That issue predates this fix, but becomes more visible now that BTT lanes are preemptible.Replace the spinlock with a per-lane mutex, remove the per-CPU recursion fast path, and take the lane lock unconditionally.
Quoted 8 lines
Add
might_sleep()to catch any future atomic-context caller.Found with the ndctl unit test
btt-check.sh.Fixes 36c75ce3bd29 ("nd_btt: Make BTT lanes preemptible")Assisted-by Claude Sonnet 4.5Signed-off-by Alison Schofield <[email protected]>---
Sashiko review offered applicable feedback. With the recursion count removed, the lanes are really just a lock pool indexed by lane number, so the per-cpu allocation no longer makes sense.
Working a v4 where pre-CPU lane storage gets replaced with a dynamically allocated per-lane mutex array.
https://sashiko.dev/#/patchset/20260514002314.65024-1-alison.schofield%40intel.com
snip