Skip to contentSkip to conversation

rust: block: gen_disk: set fops.owner from driver module pointer

PATCH In review Alvin Sun 2 participants 3 messages Aug 11

Subsystems (3)
lore
Share & tools
lore

Alvin Sun

author
#0 lore raw

PATCH 1/1 rust: block: gen_disk: set fops.owner from driver module pointer

Set fops.owner from the driver module pointer via this_module::<T::OwnerModule>().as_ptr() instead of defaulting to null, so the module cannot be unloaded while a block device is still in use.

---
The fops.owner fix series [1] covered DRM, miscdevice, configfs and binder, but missed the block layer's GenDisk abstraction.

This patch was meant to be sent separately, but I forgot to send it.

[1] https://lore.kernel.org/r/[email protected]/

---
 rust/kernel/block/mq/gen_disk.rs | 34 ++++++++++------------------------
 1 file changed, 10 insertions(+), 24 deletions(-)
rust/kernel/block/mq/gen_disk.rs +10 -24
index fc97dd8739746..215c407d466df 100644
@@ -125,30 +125,9 @@ pub fn build<T: Operations>(
             )
         })?;
 
-        const TABLE: bindings::block_device_operations = bindings::block_device_operations {
-            submit_bio: None,
-            open: None,
-            release: None,
-            ioctl: None,
-            compat_ioctl: None,
-            check_events: None,
-            unlock_native_capacity: None,
-            getgeo: None,
-            set_read_only: None,
-            swap_slot_free_notify: None,
-            report_zones: None,
-            devnode: None,
-            alternative_gpt_sector: None,
-            get_unique_id: None,
-            // TODO: Set to `THIS_MODULE`.
-            owner: core::ptr::null_mut(),
-            pr_ops: core::ptr::null_mut(),
-            free_disk: None,
-            poll_bio: None,
-        };
-
-        // SAFETY: `gendisk` is a valid pointer as we initialized it above
-        unsafe { (*gendisk).fops = &TABLE };
+        // SAFETY: `gendisk` is a valid pointer. We have exclusive access,
+        // since the disk is not added to the VFS yet.
+        unsafe { (*gendisk).fops = &GenDisk::<T>::VTABLE };
 
         let cleanup_failure = ScopeGuard::new_with_data((gendisk, data), |(gendisk, data)| {
             // SAFETY: `gendisk` came from `__blk_mq_alloc_disk()` above and
@@ -211,6 +190,13 @@ pub struct GenDisk<T: Operations> {
     gendisk: *mut bindings::gendisk,
 }
 
+impl<T: Operations> GenDisk<T> {
+    const VTABLE: bindings::block_device_operations = bindings::block_device_operations {
+        owner: crate::module::this_module::<T::OwnerModule>().as_ptr(),
+        ..pin_init::zeroed()
+    };
+}
+
 // SAFETY: `GenDisk` is an owned pointer to a `struct gendisk` and an `Arc` to a
 // `TagSet` It is safe to send this to other threads as long as T is Send.
 unsafe impl<T: Operations + Send> Send for GenDisk<T> {}

---
base-commit: 2ee859ebf156157609f71060ae472711c8cbc326
change-id: 20260810-fix-gendisk-owner-a84d0725ab2b
prerequisite-patch-id: 347c5a3c6dbef9832bfce8419fc23e6e08ba477f
prerequisite-change-id: 20260519-fix-fops-owner-e3a77bb27c6c:v10
prerequisite-patch-id: 347c5a3c6dbef9832bfce8419fc23e6e08ba477f
prerequisite-patch-id: 190cfd53d3430ade053b15db36cac9e372e2566d
prerequisite-patch-id: fdb2387ea1074c3bf16028b58c3df73e2e1783b1
prerequisite-patch-id: f38222c64f7d29781036ae8673808cf4e9a9d430
prerequisite-patch-id: da94d5d3af25778145b976f65f7eac281dc6ae79
prerequisite-patch-id: e5c034ff639d7eebe730922db0ed02492f401dd0
prerequisite-patch-id: 502035d4e05da3ed939b8bd65801a6fd8a01d788
prerequisite-patch-id: 0ccf22bbdee039964cb064f33a8c7a0925dbd57b
prerequisite-patch-id: e0595d6120868cdf3e87fa335a81e10f0e346c80
prerequisite-patch-id: f27fc645020b296d289c31fc73dbfef993294464
prerequisite-patch-id: 159f88cf73892b94117070090a58c7b68e08b8c1

Best regards,

Signature · 1 line
Alvin Sun <[email protected]>
#1 lore raw
Quoted 2 lines · Alvin Sun

This patch was meant to be sent separately, but I forgot to send it.

Do you mean "not separately"? i.e. in the series?

I asked Andreas for his Acked-by.

Cheers,
Miguel

author
#2 lore raw
Quoted 3 lines · Miguel Ojeda
Quoted 1 line · Alvin Sun

This patch was meant to be sent separately, but I forgot to send it.

Do you mean "not separately"? i.e. in the series?

Hi Miguel,

Thanks for taking a look.

To clarify: when I sent the fops.owner series, I did indeed overlook GenDisk. After the series was out, I realized the gap, prepared this patch, and privately asked Andreas (back in late June) for his thoughts. He suggested sending it standalone with the series as a dependency. I then got sidetracked by other things and forgot to send it out.

Best regards,
Alvin

Quoted 5 lines

I asked Andreas for his Acked-by.

Cheers,
Miguel