rust: use vertical import style and remove redundant imports
v2 cover rust: use vertical import style and remove redundant imports
Adopt the vertical import style and drop redundant imports already re-exported via kernel::prelude.
Changes include:
- Convert use statements to vertical import style in miscdevice, samples, and block/mq
- Remove imports covered by prelude and apply minor formatting
---
Changes in v2:
- Reformat use statements and drop redundant imports in
rust/kernel/block/mq
- Drop redundant imports in rust/kernel/miscdevice
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Alvin Sun (5):
rust: miscdevice: use vertical import style
samples: rust_misc_device: use vertical import style
rust: miscdevice: remove redundant imports
rust: block: mq: use vertical import style
rust: block: mq: remove redundant imports and format
rust/kernel/block/mq/gen_disk.rs | 24 ++++++++++++++++++------ rust/kernel/block/mq/operations.rs | 14 ++++++++++---- rust/kernel/block/mq/request.rs | 14 ++++++++++---- rust/kernel/block/mq/tag_set.rs | 28 ++++++++++++++-------------- rust/kernel/miscdevice.rs | 23 +++++++++++++++++------ samples/rust/rust_misc_device.rs | 34 ++++++++++++++++++++++++++++------ 6 files changed, 97 insertions(+), 40 deletions(-)
---
base-commit: 5200f5f493f79f14bbdc349e402a40dfb32f23c8
change-id: 20260519-miscdev-use-format-9ab7e83b1c11
Best regards,
Signature · 1 line
Alvin Sun <[email protected]>
PATCH v2 1/5 rust: miscdevice: use vertical import style
Convert use imports to vertical layout for better readability and maintainability.
--- rust/kernel/miscdevice.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-)
rust/kernel/miscdevice.rs +28 -6
@@ -11,16 +11,38 @@use crate ::{ bindings, device::Device , - error::{to_result,Error ,Result ,VTABLE_DEFAULT_ERROR }, - ffi::{c_int, c_long, c_uint, c_ulong}, - fs::{File ,Kiocb }, - iov::{IovIterDest ,IovIterSource }, + error::{ + to_result, +Error , +Result , +VTABLE_DEFAULT_ERROR ,// + }, + ffi::{ + c_int, + c_long, + c_uint, + c_ulong,// + }, + fs::{ +File , +Kiocb ,// + }, + iov::{ +IovIterDest , +IovIterSource ,// + }, mm::virt::VmaNew , prelude::*, seq_file::SeqFile , - types::{ForeignOwnable ,Opaque }, + types::{ +ForeignOwnable , +Opaque ,// + }, +}; +use core::{ + marker::PhantomData , + pin::Pin ,// }; -use core::{marker::PhantomData , pin::Pin };/// Options for creating a misc device. #[derive( Copy , Clone )]
Signature · 1 line
2.43.0
Re: [PATCH v2 1/5] rust: miscdevice: use vertical import style
Quoted 4 lines · On Wed, 20 May 2026 10:40:08 +0800 Alvin Sun <[email protected]> wrote:
Convert
useimports to vertical layout for better readability and maintainability.Signed-off-by Alvin Sun <[email protected]>
Quoted 58 lines
--- rust/kernel/miscdevice.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-)rust/kernel/miscdevice.rs +28
-6@@ -11,16 +11,38 @@use crate ::{ bindings, device::Device , - error::{to_result,Error ,Result ,VTABLE_DEFAULT_ERROR }, - ffi::{c_int, c_long, c_uint, c_ulong}, - fs::{File ,Kiocb }, - iov::{IovIterDest ,IovIterSource }, + error::{ + to_result, +Error , +Result , +VTABLE_DEFAULT_ERROR ,// + }, + ffi::{ + c_int, + c_long, + c_uint, + c_ulong,// + }, + fs::{ +File , +Kiocb ,// + }, + iov::{ +IovIterDest , +IovIterSource ,// + }, mm::virt::VmaNew , prelude::*, seq_file::SeqFile , - types::{ForeignOwnable ,Opaque }, + types::{ +ForeignOwnable , +Opaque ,// + }, +}; +use core::{ + marker::PhantomData , + pin::Pin ,// }; -use core::{marker::PhantomData , pin::Pin };/// Options for creating a misc device. #[derive( Copy , Clone )] Signature · 2 lines
-- 2.43.0
PATCH v2 2/5 samples: rust_misc_device: use vertical import style
In reply to Alvin SunConvert use imports to vertical layout for better readability and maintainability.
--- samples/rust/rust_misc_device.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-)
samples/rust/rust_misc_device.rs +28 -6
@@ -97,14 +97,36 @@use kernel::{ device::Device , - fs::{File ,Kiocb }, - ioctl::{_IO, _IOC_SIZE, _IOR, _IOW}, - iov::{IovIterDest ,IovIterSource }, - miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + fs::{ +File , +Kiocb ,// + }, + ioctl::{ + _IO, + _IOC_SIZE, + _IOR, + _IOW,// + }, + iov::{ +IovIterDest , +IovIterSource ,// + }, + miscdevice::{ +MiscDevice , +MiscDeviceOptions , +MiscDeviceRegistration ,// + }, new_mutex, prelude::*, - sync::{aref::ARef ,Mutex }, - uaccess::{UserSlice ,UserSliceReader ,UserSliceWriter }, + sync::{ + aref::ARef , +Mutex ,// + }, + uaccess::{ +UserSlice , +UserSliceReader , +UserSliceWriter ,// + }, };const RUST_MISC_DEV_HELLO :u32 =_IO ('|' as u32 ,0x80 );
Signature · 1 line
2.43.0
Re: [PATCH v2 2/5] samples: rust_misc_device: use vertical import style
Quoted 4 lines · On Wed, 20 May 2026 10:40:09 +0800 Alvin Sun <[email protected]> wrote:
Convert
useimports to vertical layout for better readability and maintainability.Signed-off-by Alvin Sun <[email protected]>
Quoted 56 lines
--- samples/rust/rust_misc_device.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-)samples/rust/rust_misc_device.rs +28
-6@@ -97,14 +97,36 @@use kernel::{ device::Device , - fs::{File ,Kiocb }, - ioctl::{_IO, _IOC_SIZE, _IOR, _IOW}, - iov::{IovIterDest ,IovIterSource }, - miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + fs::{ +File , +Kiocb ,// + }, + ioctl::{ + _IO, + _IOC_SIZE, + _IOR, + _IOW,// + }, + iov::{ +IovIterDest , +IovIterSource ,// + }, + miscdevice::{ +MiscDevice , +MiscDeviceOptions , +MiscDeviceRegistration ,// + }, new_mutex, prelude::*, - sync::{aref::ARef ,Mutex }, - uaccess::{UserSlice ,UserSliceReader ,UserSliceWriter }, + sync::{ + aref::ARef , +Mutex ,// + }, + uaccess::{ +UserSlice , +UserSliceReader , +UserSliceWriter ,// + }, };const RUST_MISC_DEV_HELLO :u32 =_IO ('|' as u32 ,0x80 );Signature · 2 lines
-- 2.43.0
PATCH v2 3/5 rust: miscdevice: remove redundant imports
In reply to Alvin SunDrop Error, Result, Pin, c_int, c_long, c_uint, and c_ulong imports already provided by kernel::prelude.
--- rust/kernel/miscdevice.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
rust/kernel/miscdevice.rs +1 -12
@@ -13,16 +13,8 @@ device::Device, error::{ to_result, - Error, - Result, VTABLE_DEFAULT_ERROR, // }, - ffi::{ - c_int, - c_long, - c_uint, - c_ulong, // - }, fs::{File ,Kiocb ,//
@@ -39,10 +31,7 @@ Opaque, // }, }; -use core::{ - marker::PhantomData , - pin::Pin ,// -}; +use core::marker::PhantomData ;/// Options for creating a misc device. #[derive( Copy , Clone )]
Signature · 1 line
2.43.0
Re: [PATCH v2 3/5] rust: miscdevice: remove redundant imports
Quoted 4 lines · On Wed, 20 May 2026 10:40:10 +0800 Alvin Sun <[email protected]> wrote:
Drop
Error,Result,Pin,c_int,c_long,c_uint, andc_ulongimports already provided bykernel::prelude.Signed-off-by Alvin Sun <[email protected]>
Quoted 42 lines
--- rust/kernel/miscdevice.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)rust/kernel/miscdevice.rs +1
-12@@ -13,16 +13,8 @@ device::Device, error::{ to_result, - Error, - Result, VTABLE_DEFAULT_ERROR, // }, - ffi::{ - c_int, - c_long, - c_uint, - c_ulong, // - }, fs::{File ,Kiocb ,// @@ -39,10 +31,7 @@ Opaque, // }, }; -use core::{ - marker::PhantomData , - pin::Pin ,// -}; +use core::marker::PhantomData ;/// Options for creating a misc device. #[derive( Copy , Clone )] Signature · 2 lines
-- 2.43.0
PATCH v2 4/5 rust: block: mq: use vertical import style
In reply to Alvin SunConvert use imports to vertical layout for better readability and maintainability.
--- rust/kernel/block/mq/gen_disk.rs | 21 +++++++++++++++++---- rust/kernel/block/mq/operations.rs | 17 +++++++++++++---- rust/kernel/block/mq/request.rs | 12 +++++++++--- rust/kernel/block/mq/tag_set.rs | 24 +++++++++++++++++++----- 4 files changed, 58 insertions(+), 16 deletions(-)
rust/kernel/block/mq/gen_disk.rs +17 -4
@@ -7,14 +7,27 @@use crate ::{ bindings, - block::mq::{Operations ,TagSet }, - error::{self, from_err_ptr,Result }, - fmt::{self,Write }, + block::mq::{ +Operations , +TagSet ,// + }, + error::{ + self, + from_err_ptr, +Result ,// + }, + fmt::{ + self, +Write ,// + }, prelude::*, static_lock_class, str::NullTerminatedFormatter , sync::Arc , - types::{ForeignOwnable ,ScopeGuard }, + types::{ +ForeignOwnable , +ScopeGuard ,// + }, };/// A builder for [`GenDisk`].
rust/kernel/block/mq/operations.rs +13 -4
@@ -6,11 +6,20 @@use crate ::{ bindings, - block::mq::{request::RequestDataWrapper ,Request }, - error::{from_result,Result }, + block::mq::{ + request::RequestDataWrapper , +Request ,// + }, + error::{ + from_result, +Result ,// + }, prelude::*, - sync::{aref::ARef ,Refcount }, - types::ForeignOwnable , + sync::{ + aref::ARef , +Refcount ,// + }, + types::ForeignOwnable ,// };use core::marker::PhantomData ;
rust/kernel/block/mq/request.rs +9 -3
@@ -9,13 +9,19 @@ block::mq::Operations, error::Result, sync::{ - aref::{ARef, AlwaysRefCounted}, + aref::{ + ARef, + AlwaysRefCounted, // + }, atomic::Relaxed, - Refcount, + Refcount, // }, types::Opaque, }; -use core::{marker::PhantomData , ptr::NonNull }; +use core::{ + marker::PhantomData , + ptr::NonNull ,// +};/// A wrapper around a blk-mq [`struct request`]. This represents an IO request. ///
rust/kernel/block/mq/tag_set.rs +19 -5
@@ -8,13 +8,27 @@use crate ::{ bindings, - block::mq::{operations::OperationsVTable , request::RequestDataWrapper ,Operations }, - error::{self,Result }, + block::mq::{ + operations::OperationsVTable , + request::RequestDataWrapper , +Operations ,// + }, + error::{ + self, +Result ,// + }, prelude::try_pin_init, - types::Opaque , + types::Opaque ,// +}; +use core::{ + convert::TryInto , + marker::PhantomData ,// +}; +use pin_init::{ + pin_data, + pinned_drop, +PinInit ,// }; -use core::{convert::TryInto , marker::PhantomData }; -use pin_init::{pin_data, pinned_drop,PinInit };/// A wrapper for the C `struct blk_mq_tag_set`. ///
Signature · 1 line
2.43.0
Re: [PATCH v2 4/5] rust: block: mq: use vertical import style
Quoted 4 lines · On Wed, 20 May 2026 10:40:11 +0800 Alvin Sun <[email protected]> wrote:
Convert
useimports to vertical layout for better readability and maintainability.Signed-off-by Alvin Sun <[email protected]>
Quoted 141 lines
--- rust/kernel/block/mq/gen_disk.rs | 21 +++++++++++++++++---- rust/kernel/block/mq/operations.rs | 17 +++++++++++++---- rust/kernel/block/mq/request.rs | 12 +++++++++--- rust/kernel/block/mq/tag_set.rs | 24 +++++++++++++++++++----- 4 files changed, 58 insertions(+), 16 deletions(-)rust/kernel/block/mq/gen_disk.rs +17
-4@@ -7,14 +7,27 @@use crate ::{ bindings, - block::mq::{Operations ,TagSet }, - error::{self, from_err_ptr,Result }, - fmt::{self,Write }, + block::mq::{ +Operations , +TagSet ,// + }, + error::{ + self, + from_err_ptr, +Result ,// + }, + fmt::{ + self, +Write ,// + }, prelude::*, static_lock_class, str::NullTerminatedFormatter , sync::Arc , - types::{ForeignOwnable ,ScopeGuard }, + types::{ +ForeignOwnable , +ScopeGuard ,// + }, };/// A builder for [`GenDisk`]. rust/kernel/block/mq/operations.rs +13
-4@@ -6,11 +6,20 @@use crate ::{ bindings, - block::mq::{request::RequestDataWrapper ,Request }, - error::{from_result,Result }, + block::mq::{ + request::RequestDataWrapper , +Request ,// + }, + error::{ + from_result, +Result ,// + }, prelude::*, - sync::{aref::ARef ,Refcount }, - types::ForeignOwnable , + sync::{ + aref::ARef , +Refcount ,// + }, + types::ForeignOwnable ,// };use core::marker::PhantomData ;rust/kernel/block/mq/request.rs +9
-3@@ -9,13 +9,19 @@ block::mq::Operations, error::Result, sync::{ - aref::{ARef, AlwaysRefCounted}, + aref::{ + ARef, + AlwaysRefCounted, // + }, atomic::Relaxed, - Refcount, + Refcount, // }, types::Opaque, }; -use core::{marker::PhantomData , ptr::NonNull }; +use core::{ + marker::PhantomData , + ptr::NonNull ,// +};/// A wrapper around a blk-mq [`struct request`]. This represents an IO request. /// rust/kernel/block/mq/tag_set.rs +19
-5@@ -8,13 +8,27 @@use crate ::{ bindings, - block::mq::{operations::OperationsVTable , request::RequestDataWrapper ,Operations }, - error::{self,Result }, + block::mq::{ + operations::OperationsVTable , + request::RequestDataWrapper , +Operations ,// + }, + error::{ + self, +Result ,// + }, prelude::try_pin_init, - types::Opaque , + types::Opaque ,// +}; +use core::{ + convert::TryInto , + marker::PhantomData ,// +}; +use pin_init::{ + pin_data, + pinned_drop, +PinInit ,// }; -use core::{convert::TryInto , marker::PhantomData }; -use pin_init::{pin_data, pinned_drop,PinInit };/// A wrapper for the C `struct blk_mq_tag_set`. /// Signature · 2 lines
-- 2.43.0
Re: [PATCH v2 4/5] rust: block: mq: use vertical import style
In reply to Alvin SunQuoted 4 lines · Alvin Sun
Convert
useimports to vertical layout for better readability and maintainability.Signed-off-by Alvin Sun <[email protected]>
Best regards,
Andreas Hindborg
Re: [PATCH v2 4/5] rust: block: mq: use vertical import style
Quoted 4 lines · Andreas Hindborg
Acked-by Andreas Hindborg <[email protected]>Cc Jens Axboe <[email protected]>
There is a v3 (may be exactly the same):
https://lore.kernel.org/rust-for-linux/[email protected]/
Cheers,
Miguel
PATCH v2 5/5 rust: block: mq: remove redundant imports and format
In reply to Alvin SunDrop Result, Pin, pin_data, pinned_drop, PinInit, and try_pin_init imports already provided by kernel::prelude.
Simplify error imports and flatten parameters formatting.
--- rust/kernel/block/mq/gen_disk.rs | 7 +++---- rust/kernel/block/mq/operations.rs | 5 +---- rust/kernel/block/mq/request.rs | 2 +- rust/kernel/block/mq/tag_set.rs | 22 ++++------------------ 4 files changed, 9 insertions(+), 27 deletions(-)
rust/kernel/block/mq/gen_disk.rs +3 -4
@@ -12,9 +12,8 @@ TagSet, // }, error::{ - self, from_err_ptr, - Result, // + to_result, // }, fmt::{ self,
@@ -67,7 +66,7 @@ pub fn rotational(mut self, rotational: bool) -> Self {/// and that it is a power of two. pub fn validate_block_size (size:u32 ) ->Result {if !(512 ..=bindings::PAGE_SIZE as u32 ).contains (&size) || !size.is_power_of_two () { -Err (error::code::EINVAL ) +Err (EINVAL ) }else {Ok (()) }
@@ -177,7 +176,7 @@ pub fn build<T: Operations>(// operation, so we will not race. unsafe { bindings::set_capacity (gendisk, self.capacity_sectors) }; -crate ::error::to_result ( +to_result (// SAFETY: `gendisk` points to a valid and initialized instance of // `struct gendisk`. unsafe {
rust/kernel/block/mq/operations.rs +1 -4
@@ -10,10 +10,7 @@ request::RequestDataWrapper, Request, // }, - error::{ - from_result, - Result, // - }, + error::from_result, prelude::*, sync::{ aref::ARef,
rust/kernel/block/mq/request.rs +1 -1
@@ -7,7 +7,7 @@use crate ::{ bindings, block::mq::Operations , - error::Result , + prelude::*, sync::{ aref::{ARef ,
rust/kernel/block/mq/tag_set.rs +4 -18
@@ -4,8 +4,6 @@//! //! C header: [`include/linux/blk-mq.h`](srctree/include/linux/blk-mq.h) -use core::pin::Pin ; -use crate ::{ bindings, block::mq::{
@@ -13,22 +11,14 @@ request::RequestDataWrapper, Operations, // }, - error::{ - self, - Result, // - }, - prelude::try_pin_init, + error::to_result, + prelude::*, types::Opaque, // };use core::{ convert::TryInto , marker::PhantomData ,// }; -use pin_init::{ - pin_data, - pinned_drop, -PinInit ,// -};/// A wrapper for the C `struct blk_mq_tag_set`. ///
@@ -47,11 +37,7 @@ pub struct TagSet<T: Operations> {impl <T :Operations >TagSet <T > {/// Try to create a new tag set -pub fn new ( - nr_hw_queues:u32 , - num_tags:u32 , - num_maps:u32 , - ) ->impl PinInit <Self , error::Error > { +pub fn new (nr_hw_queues:u32 , num_tags:u32 , num_maps:u32 ) ->impl PinInit <Self ,Error > {let tag_set: bindings::blk_mq_tag_set = pin_init::zeroed ();let tag_set:Result <_ > = core::mem::size_of ::<RequestDataWrapper >() .try_into ()
@@ -77,7 +63,7 @@ pub fn new(// SAFETY: we do not move out of `tag_set`. let tag_set: &mut Opaque <_ > =unsafe {Pin ::get_unchecked_mut (tag_set) };// SAFETY: `tag_set` is a reference to an initialized `blk_mq_tag_set`. - error::to_result (unsafe { bindings::blk_mq_alloc_tag_set (tag_set.get ())}) +to_result (unsafe { bindings::blk_mq_alloc_tag_set (tag_set.get ())}) }), _p: PhantomData, })
Signature · 1 line
2.43.0
Re: [PATCH v2 5/5] rust: block: mq: remove redundant imports and format
Quoted 6 lines · On Wed, 20 May 2026 10:40:12 +0800 Alvin Sun <[email protected]> wrote:
Drop
Result,Pin,pin_data,pinned_drop,PinInit, andtry_pin_initimports already provided bykernel::prelude.Simplify
errorimports and flatten parameters formatting.Signed-off-by Alvin Sun <[email protected]>
Quoted 134 lines
--- rust/kernel/block/mq/gen_disk.rs | 7 +++---- rust/kernel/block/mq/operations.rs | 5 +---- rust/kernel/block/mq/request.rs | 2 +- rust/kernel/block/mq/tag_set.rs | 22 ++++------------------ 4 files changed, 9 insertions(+), 27 deletions(-)rust/kernel/block/mq/gen_disk.rs +3
-4@@ -12,9 +12,8 @@ TagSet, // }, error::{ - self, from_err_ptr, - Result, // + to_result, // }, fmt::{ self,@@ -67,7 +66,7 @@ pub fn rotational(mut self, rotational: bool) -> Self {/// and that it is a power of two. pub fn validate_block_size (size:u32 ) ->Result {if !(512 ..=bindings::PAGE_SIZE as u32 ).contains (&size) || !size.is_power_of_two () { -Err (error::code::EINVAL ) +Err (EINVAL ) }else {Ok (()) }@@ -177,7 +176,7 @@ pub fn build<T: Operations>(// operation, so we will not race. unsafe { bindings::set_capacity (gendisk, self.capacity_sectors) }; -crate ::error::to_result ( +to_result (// SAFETY: `gendisk` points to a valid and initialized instance of // `struct gendisk`. unsafe {rust/kernel/block/mq/operations.rs +1
-4@@ -10,10 +10,7 @@ request::RequestDataWrapper, Request, // }, - error::{ - from_result, - Result, // - }, + error::from_result, prelude::*, sync::{ aref::ARef,rust/kernel/block/mq/request.rs +1
-1@@ -7,7 +7,7 @@use crate ::{ bindings, block::mq::Operations , - error::Result , + prelude::*, sync::{ aref::{ARef ,rust/kernel/block/mq/tag_set.rs +4
-18@@ -4,8 +4,6 @@//! //! C header: [`include/linux/blk-mq.h`](srctree/include/linux/blk-mq.h) -use core::pin::Pin ; -use crate ::{ bindings, block::mq::{@@ -13,22 +11,14 @@ request::RequestDataWrapper, Operations, // }, - error::{ - self, - Result, // - }, - prelude::try_pin_init, + error::to_result, + prelude::*, types::Opaque, // };use core::{ convert::TryInto , marker::PhantomData ,// }; -use pin_init::{ - pin_data, - pinned_drop, -PinInit ,// -};/// A wrapper for the C `struct blk_mq_tag_set`. /// @@ -47,11 +37,7 @@ pub struct TagSet<T: Operations> {impl <T :Operations >TagSet <T > {/// Try to create a new tag set -pub fn new ( - nr_hw_queues:u32 , - num_tags:u32 , - num_maps:u32 , - ) ->impl PinInit <Self , error::Error > { +pub fn new (nr_hw_queues:u32 , num_tags:u32 , num_maps:u32 ) ->impl PinInit <Self ,Error > {let tag_set: bindings::blk_mq_tag_set = pin_init::zeroed ();let tag_set:Result <_ > = core::mem::size_of ::<RequestDataWrapper >() .try_into ()@@ -77,7 +63,7 @@ pub fn new(// SAFETY: we do not move out of `tag_set`. let tag_set: &mut Opaque <_ > =unsafe {Pin ::get_unchecked_mut (tag_set) };// SAFETY: `tag_set` is a reference to an initialized `blk_mq_tag_set`. - error::to_result (unsafe { bindings::blk_mq_alloc_tag_set (tag_set.get ())}) +to_result (unsafe { bindings::blk_mq_alloc_tag_set (tag_set.get ())}) }), _p: PhantomData, })Signature · 2 lines
-- 2.43.0
Re: [PATCH v2 5/5] rust: block: mq: remove redundant imports and format
In reply to Alvin SunQuoted 6 lines · Alvin Sun
Drop
Result,Pin,pin_data,pinned_drop,PinInit, andtry_pin_initimports already provided bykernel::prelude.Simplify
errorimports and flatten parameters formatting.Signed-off-by Alvin Sun <[email protected]>
@Jens can you pick 4/5 and 5/5?
Best regards,
Andreas Hindborg