This is RFC and just first patch is ready. First I wanna discuss if this is right way to go. I also need to implement this so that build does not break and do better splitting. There is missing signed-off-by's and commit messages so no need to comment those kind of things. Feel free to cc more people if needed.
So currently we have problem that we are not always filling .owner field for file_operations. I think we can enable const_refs_to_static already as that is in 1.78 and is stable in 1.83. So that fits perfecly for us. This also seems to be quite request feature but I did not found that no one has ever suggested that we just enable this. So basic idea is that we will have ThisModule trait which is used kernel side. Module side usually just pass this trait forward if needed. Or they can use THIS_MODULE impl which is now completly private impl for each module and kernel create does not see this. So we can think if we wanna pass other module data which have through this. This could also be used to give local module storage without using static. There is also some of github issues for ThisModule handling: > MiscDevice missing module reference counting causes use-after-free crashes > [1]. This will be tested and fixed in this series > Ensure ThisModule invariants hold Yeap. Now it would be really hard for kernel create to create this by accident. Binder is still using .as_ptr() but that is easily solvable and after that we can make .as_ptr() private so it will be nice that kernel create can just use .as_ptr() modules can just use. > Add ThisModule::this_module() > > Add a new function ThisModule::this_module() -> &'static ThisModule > that is only available when MODULE is set. It should return a static > reference to bindings::__this_module. [3] I do not this is actually good idea. But here we take little bit different approx so basically this is done also. > Suggested that ThisModule should have name() parameter. [4] And that was good idea. We can remove whole ModuleMetadata and usage is lot nicer. It is also possible that we could have example trait PciModule. which THIS_MODULE impl based on type but that is think for another time. > Initialise the owner field in struct file_operations. [5] Yeap. After this is totally possible to do everywhere. This series will already address most of those. [1]: https://github.com/Rust-for-Linux/linux/issues/1182 [2]: https://github.com/Rust-for-Linux/linux/issues/212 [3]: https://github.com/Rust-for-Linux/linux/issues/1176 [4]: https://github.com/Rust-for-Linux/linux/issues/720 [5]: https://github.com/Rust-for-Linux/linux/issues/720 To: Miguel Ojeda <[email protected]> To: Boqun Feng <[email protected]> To: Gary Guo <[email protected]> To: Björn Roy Baron <[email protected]> To: Benno Lossin <[email protected]> To: Andreas Hindborg <[email protected]> To: Alice Ryhl <[email protected]> To: Trevor Gross <[email protected]> To: Danilo Krummrich <[email protected]> To: Alexandre Courbot <[email protected]> Cc: Luis Chamberlain <[email protected]> Cc: Petr Pavlu <[email protected]> Cc: Daniel Gomez <[email protected]> Cc: Sami Tolvanen <[email protected]> Cc: Aaron Tomlin <[email protected]> Cc: Andreas Hindborg <[email protected]> Cc: Jens Axboe <[email protected]> Signed-off-by: Kari Argillander <[email protected]> --- Kari Argillander (6): rust: Enable const_refs_to_static feature rust: WIP: Introduce ThisModule trait and THIS_MODULE impl rust: WIP: use ThisModule trait for initializing rust: WIP: use ThisModule trait to fix some missing owners rust: debugfs: WIP: Use owner for file_operations rust: WIP: Replace ModuleMetadata with THIS_MODULE drivers/android/binder/rust_binder_main.rs | 4 +- drivers/block/rnull/configfs.rs | 2 +- drivers/block/rnull/rnull.rs | 3 +- drivers/gpu/drm/nova/driver.rs | 1 + drivers/gpu/drm/tyr/driver.rs | 1 + drivers/gpu/nova-core/nova_core.rs | 2 +- lib/find_bit_benchmark_rust.rs | 2 +- rust/kernel/auxiliary.rs | 15 +++--- rust/kernel/block/mq/gen_disk.rs | 31 ++---------- rust/kernel/block/mq/operations.rs | 12 +++++ rust/kernel/configfs.rs | 19 ++++--- rust/kernel/debugfs.rs | 79 ++++++++++++++++-------------- rust/kernel/debugfs/file_ops.rs | 50 ++++++++++++++----- rust/kernel/driver.rs | 19 ++----- rust/kernel/drm/device.rs | 2 +- rust/kernel/drm/driver.rs | 3 ++ rust/kernel/drm/gem/mod.rs | 4 +- rust/kernel/firmware.rs | 2 +- rust/kernel/i2c.rs | 10 ++-- rust/kernel/lib.rs | 78 ++++++++++++++++++----------- rust/kernel/miscdevice.rs | 4 ++ rust/kernel/net/phy.rs | 16 +++--- rust/kernel/pci.rs | 11 ++--- rust/kernel/platform.rs | 11 ++--- rust/kernel/usb.rs | 11 ++--- rust/macros/module.rs | 43 +++++++++------- samples/rust/rust_configfs.rs | 2 +- samples/rust/rust_debugfs.rs | 6 +-- samples/rust/rust_driver_auxiliary.rs | 8 +-- samples/rust/rust_driver_faux.rs | 2 +- samples/rust/rust_minimal.rs | 2 +- samples/rust/rust_misc_device.rs | 3 +- samples/rust/rust_print_main.rs | 2 +- 33 files changed, 245 insertions(+), 215 deletions(-) --- base-commit: cc3aa43b44bdb43dfbac0fcb51c56594a11338a8 change-id: 20251230-this_module_fix-a390bff24897 Best regards, -- Kari Argillander <[email protected]>

