FAILED: patch "[PATCH] rust: block: fix GenDisk cleanup paths" failed to apply to 6.12-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x 2957771379fa335103a4b539db57bb2271e12142
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026070953-laurel-zeppelin-239b@gregkh' --subject-prefix 'PATCH 6.12.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 2957771379fa335103a4b539db57bb2271e12142 Mon Sep 17 00:00:00 2001
From: Haoze Xie <[email protected]>
Date: Sat, 30 May 2026 14:11:54 +0800
Subject: [PATCH] rust: block: fix GenDisk cleanup paths
GenDiskBuilder::build() still has fallible work after __blk_mq_alloc_disk(), but its error path only recovers the foreign queue data. That leaks the temporary gendisk and request_queue until later teardown. If the caller moved the last Arc<TagSet<T>> into build(), the leaked queue can retain blk-mq state after the tag set is dropped.
Fix the pre-registration failure path by dropping the temporary gendisk reference with put_disk() before recovering queue_data, so disk_release() can tear down the owned queue.
Also pair GenDisk::drop() with put_disk() after del_gendisk(). Once a Rust GenDisk has been added with device_add_disk(), del_gendisk() only unregisters it; the final gendisk reference still has to be dropped to complete the release path.
rust/kernel/block/mq/gen_disk.rs +19 -1
@@ -150,6 +150,19 @@ pub fn build<T: Operations>(// SAFETY: `gendisk` is a valid pointer as we initialized it above unsafe { (*gendisk).fops = &TABLE }; +let cleanup_failure =ScopeGuard ::new_with_data ((gendisk, data), |(gendisk, data)| { +// SAFETY: `gendisk` came from `__blk_mq_alloc_disk()` above and +// has not been added to the VFS on this cleanup path. +unsafe { bindings::put_disk (gendisk) }; +// SAFETY: `data` came from `into_foreign()` above and has not been +// converted back on this cleanup path. +drop (unsafe {T ::QueueData ::from_foreign (data) }); + }); + +// The failure guard now owns both pieces of cleanup; the early guard +// must not run on this path anymore. + recover_data.dismiss (); + let mut writer = NullTerminatedFormatter::new(// SAFETY: `gendisk` points to a valid and initialized instance. We // have exclusive access, since the disk is not added to the VFS
@@ -172,7 +185,7 @@ pub fn build<T: Operations>( }, )?; - recover_data.dismiss (); + cleanup_failure.dismiss ();// INVARIANT: `gendisk` was initialized above. // INVARIANT: `gendisk` was added to the VFS via `device_add_disk` above.
@@ -215,6 +228,11 @@ fn drop(&mut self) {// to the VFS. unsafe { bindings::del_gendisk (self.gendisk) }; +// SAFETY: By type invariant, `self.gendisk` was added to the VFS, so +// `put_disk()` must follow `del_gendisk()` to drop the final gendisk +// reference and trigger the remaining release path. +unsafe { bindings::put_disk (self.gendisk) }; +// SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build` with // a call to `ForeignOwnable::into_foreign` to create `queuedata`. // `ForeignOwnable::from_foreign` is only called here.
Sasha Levin
maintainerPATCH 1/1 rust: block: fix GenDisk cleanup paths
From: Haoze Xie <[email protected]>
[ Upstream commit 2957771379fa335103a4b539db57bb2271e12142 ]
GenDiskBuilder::build() still has fallible work after __blk_mq_alloc_disk(), but its error path only recovers the foreign queue data. That leaks the temporary gendisk and request_queue until later teardown. If the caller moved the last Arc<TagSet<T>> into build(), the leaked queue can retain blk-mq state after the tag set is dropped.
Fix the pre-registration failure path by dropping the temporary gendisk reference with put_disk() before recovering queue_data, so disk_release() can tear down the owned queue.
Also pair GenDisk::drop() with put_disk() after del_gendisk(). Once a Rust GenDisk has been added with device_add_disk(), del_gendisk() only unregisters it; the final gendisk reference still has to be dropped to complete the release path.
--- rust/kernel/block/mq/gen_disk.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
rust/kernel/block/mq/gen_disk.rs +14 -1
@@ -6,7 +6,7 @@//! C header: [`include/linux/blk-mq.h`](srctree/include/linux/blk-mq.h) use crate ::block::mq::{raw_writer::RawWriter ,Operations ,TagSet }; -use crate ::{bindings, error::from_err_ptr, error::Result , sync::Arc }; +use crate ::{bindings, error::from_err_ptr, error::Result , sync::Arc , types::ScopeGuard };use crate ::{error, static_lock_class};use core::fmt::{self,Write };
@@ -139,6 +139,12 @@ pub fn build<T: Operations>(// SAFETY: `gendisk` is a valid pointer as we initialized it above unsafe { (*gendisk).fops = &TABLE }; +let cleanup_failure =ScopeGuard ::new_with_data (gendisk, |gendisk| { +// SAFETY: `gendisk` came from `__blk_mq_alloc_disk()` above and +// has not been added to the VFS on this cleanup path. +unsafe { bindings::put_disk (gendisk) }; + }); + let mut raw_writer = RawWriter::from_array(// SAFETY: `gendisk` points to a valid and initialized instance. We // have exclusive access, since the disk is not added to the VFS
@@ -161,6 +167,8 @@ pub fn build<T: Operations>( }, )?; + cleanup_failure.dismiss (); +// INVARIANT: `gendisk` was initialized above. // INVARIANT: `gendisk` was added to the VFS via `device_add_disk` above. Ok (GenDisk {
@@ -192,5 +200,10 @@ fn drop(&mut self) {// initialized instance of `struct gendisk`, and it was previously added // to the VFS. unsafe { bindings::del_gendisk (self.gendisk) }; + +// SAFETY: By type invariant, `self.gendisk` was added to the VFS, so +// `put_disk()` must follow `del_gendisk()` to drop the final gendisk +// reference and trigger the remaining release path. +unsafe { bindings::put_disk (self.gendisk) }; } }
Signature · 1 line
2.53.0