Skip to content

nvme: apple: run the deferred flush before removing namespaces - #2990

Draft
Alyx-ML wants to merge 1 commit into
aurora-silicon:integration/aurorafrom
Alyx-ML:aurora/nvme-remove-flush
Draft

Alyx-ML wants to merge 1 commit into
aurora-silicon:integration/aurorafrom
Alyx-ML:aurora/nvme-remove-flush

Conversation

@Alyx-ML

@Alyx-ML Alyx-ML commented Nov 23, 2026

Copy link
Copy Markdown

Part of the #2755 split. Draft until it has a run on a Mac (the last Evidence line says which). This includes the race DjDeveloperr found in the #2755 version.

Summary

 apple_nvme_delayed_flush()
+  guard(spinlock_irqsave)(&anv->flush_lock)     # interval check and arming, together
 apple_nvme_stop_deferred_flush()                # new
+  under flush_lock: flush_interval = 0          # no flush can arm the work after this
+  flush_delayed_work()                          # runs the deferred flush while ns exists
 remove()
-  cancel_delayed_work_sync();  nvme_remove_namespaces()
+  apple_nvme_stop_deferred_flush();  nvme_remove_namespaces()
 shutdown()
-  flush_delayed_work()
+  apple_nvme_stop_deferred_flush()

Evidence

A host harness compiles the real apple_nvme_delayed_flush() text with stubbed work calls under
ASan and UBSan, and widens the window between reading the interval and arming:

Build: passed, Apple build and KUnit. There is no nvme KUnit suite. This does not change the default
flush_interval; that is in #2989.

Needed before merging: on a Mac with flush_interval=1000, a shutdown and a reboot during
fsync-heavy writes.

Merge Danger

Door: two-way

Blast Radius: nvme remove and shutdown

The lock is taken only for flush requests on controllers that use the grace period.

Assisted-by: LLM (Claude:Opus 5.5)

remove() cancelled the deferred-flush work and then removed the
namespaces. That loses data and can use freed memory:

 - The cancelled work stood for a flush that queue_rq() had already
   completed to the filesystem. Cancelling it means that flush never
   reaches the drive.
 - Removing a namespace flushes its disk. With the grace period set
   (flush_interval, 1000 ms by default), that flush can arm the work
   again. The work then runs on flush_ns after namespace removal has
   freed it.

Add apple_nvme_stop_deferred_flush(). It sets the interval to 0 and then
runs any deferred flush while its namespace still exists. Later flushes
go straight to the drive. A new flush_lock covers the interval check and
the arming in apple_nvme_delayed_flush(), and the clearing here. So a
flush that read a non-zero interval has finished arming before the work
is flushed, and none can arm it afterwards. The lock is taken only for
flush requests on controllers that use the grace period.

Call it from remove() before nvme_remove_namespaces(), and from
shutdown() in place of the plain flush_delayed_work(), so flushes issued
during shutdown also reach the drive before it is disabled.

Checked with a host harness that compiles the real
apple_nvme_delayed_flush() text with stubbed work calls under ASan and
UBSan, and widens the window between reading the interval and arming.
With an earlier version of this fix (clear with WRITE_ONCE(), then
flush), the work was armed after the namespace was freed in 3 of 3 runs.
With this version, the deferred flush ran before the free and nothing
was armed afterwards, 3 of 3. Not built as a kernel module here, and
not run on hardware.

Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant