Skip to contentSkip to conversation

rust: Move unfit code out of `rust/kernel/types.rs`

PATCH Applied Aliet Exposito Garcia 4 participants 11 messages Sep 18, 2024 – Oct 14, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover rust: Move unfit code out of `rust/kernel/types.rs`

Hello all,
These are the patches for a good first issue that Benno Lossin oppened on https://github.com/Rust-for-Linux/linux/issues/1117
According to the issue description, the file rust/kernel/types.rs is a rather non-descriptive, general file that contains code that might be better suited elsewhere:
- ARef<T> and AlwaysRefCounted should be in rust/kernel/sync/aref.rs
- The FromBytes and AsBytes traits and implementations should be in rust/kernel/transmute.rs

Aliet Exposito Garcia (2):
rust: Move FromBytes and AsBytes traits to a new transmute
module
rust: Move ARef<T> and AlwaysRefCounted to new sync::aref module

 drivers/block/rnull.rs             |   3 +-
 rust/kernel/block/mq.rs            |   6 +-
 rust/kernel/block/mq/operations.rs |   2 +-
 rust/kernel/block/mq/request.rs    |   3 +-
 rust/kernel/device.rs              |   8 +-
 rust/kernel/lib.rs                 |   1 +
 rust/kernel/sync.rs                |   1 +
 rust/kernel/sync/aref.rs           | 152 ++++++++++++++++++++
 rust/kernel/task.rs                |   4 +-
 rust/kernel/transmute.rs           |  67 +++++++++
 rust/kernel/types.rs               | 216 +----------------------------
 rust/kernel/uaccess.rs             |   2 +-
 12 files changed, 236 insertions(+), 229 deletions(-)
 create mode 100644 rust/kernel/sync/aref.rs
 create mode 100644 rust/kernel/transmute.rs

base-commit: a2f11547052001bd448ccec81dd1e68409078fbb

Signature · 1 line
2.46.0
#1 lore raw

PATCH 1/2 rust: Move `FromBytes` and `AsBytes` traits to a new `transmute` module

This patch refactors the FromBytes and AsBytes traits from types.rs into a new transmute.rs module.
The traits and their implementations remain unchanged.
- Adds rust/kernel/transmute.rs with the definitions of FromBytes and AsBytes
- Removes the same trait definitions from rust/kernel/types.rs
- Updates rust/kernel/uaccess.rs to import AsBytes and FromBytes from transmute.rs

---
 rust/kernel/lib.rs       |  1 +
 rust/kernel/transmute.rs | 67 ++++++++++++++++++++++++++++++++++++++++
 rust/kernel/types.rs     | 64 --------------------------------------
 rust/kernel/uaccess.rs   |  2 +-
 4 files changed, 69 insertions(+), 65 deletions(-)
 create mode 100644 rust/kernel/transmute.rs
rust/kernel/lib.rs +1 -0
index f10b06a78..5007ba914 100644
@@ -52,6 +52,7 @@
 pub mod sync;
 pub mod task;
 pub mod time;
+pub mod transmute;
 pub mod types;
 pub mod uaccess;
 pub mod workqueue;
rust/kernel/transmute.rs new file +67 -0

67 lines not on this page · raw message

rust/kernel/types.rs +0 -64

67 lines not on this page · raw message

rust/kernel/uaccess.rs +1 -1
index e9347cff9..0af243482 100644
@@ -9,7 +9,7 @@
     bindings,
     error::Result,
     prelude::*,
-    types::{AsBytes, FromBytes},
+    transmute::{AsBytes, FromBytes},
 };
 use alloc::vec::Vec;
 use core::ffi::{c_ulong, c_void};
Signature · 1 line
2.46.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] rust: Move `FromBytes` and `AsBytes` traits to a new `transmute` module

Quoted 13 lines · Aliet Exposito Garcia

This patch refactors the FromBytes and AsBytes traits from types.rs into a new transmute.rs module.
The traits and their implementations remain unchanged.
- Adds rust/kernel/transmute.rs with the definitions of FromBytes and AsBytes
- Removes the same trait definitions from rust/kernel/types.rs
- Updates rust/kernel/uaccess.rs to import AsBytes and FromBytes from transmute.rs

Quoted 7 lines
---
 rust/kernel/lib.rs       |  1 +
 rust/kernel/transmute.rs | 67 ++++++++++++++++++++++++++++++++++++++++
 rust/kernel/types.rs     | 64 --------------------------------------
 rust/kernel/uaccess.rs   |  2 +-
 4 files changed, 69 insertions(+), 65 deletions(-)
 create mode 100644 rust/kernel/transmute.rs

Thanks for the patch!

---
Cheers,
Benno

Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] rust: Move `FromBytes` and `AsBytes` traits to a new `transmute` module

In reply to Aliet Exposito Garcia
Quoted 9 lines · Aliet Exposito Garcia

This patch refactors the FromBytes and AsBytes traits from types.rs into a new transmute.rs module.
The traits and their implementations remain unchanged.
- Adds rust/kernel/transmute.rs with the definitions of FromBytes and AsBytes
- Removes the same trait definitions from rust/kernel/types.rs
- Updates rust/kernel/uaccess.rs to import AsBytes and FromBytes from transmute.rs

Quoted 4 lines
#4 lore raw

PATCH 2/2 rust: Move `ARef<T>` and `AlwaysRefCounted` to new `sync::aref` module

In reply to Aliet Exposito Garcia

This patch refactors the ARef type and AlwaysRefCounted trait from types.rs into a new sync/aref.rs module.
The type and trait definitions remain unchanged.
- Adds rust/kernel/sync/aref.rs with the definitions of ARef and AlwaysRefCounted
- Removes the same type and trait definitions from rust/kernel/types.rs
- Updates relevant files to import ARef and AlwaysRefCounted from

    `sync/aref.rs`
---
 drivers/block/rnull.rs             |   3 +-
 rust/kernel/block/mq.rs            |   6 +-
 rust/kernel/block/mq/operations.rs |   2 +-
 rust/kernel/block/mq/request.rs    |   3 +-
 rust/kernel/device.rs              |   8 +-
 rust/kernel/sync.rs                |   1 +
 rust/kernel/sync/aref.rs           | 152 +++++++++++++++++++++++++++++
 rust/kernel/task.rs                |   4 +-
 rust/kernel/types.rs               | 152 +----------------------------
 9 files changed, 167 insertions(+), 164 deletions(-)
 create mode 100644 rust/kernel/sync/aref.rs
drivers/block/rnull.rs +1 -2
index b0227cf9d..fd157d390 100644
@@ -20,8 +20,7 @@
     error::Result,
     new_mutex, pr_info,
     prelude::*,
-    sync::{Arc, Mutex},
-    types::ARef,
+    sync::{aref::ARef, Arc, Mutex},
 };
 
 module! {
rust/kernel/block/mq.rs +3 -3
index fb0f393c1..f9e32eecd 100644
@@ -20,7 +20,7 @@
 //! The kernel will interface with the block device driver by calling the method
 //! implementations of the `Operations` trait.
 //!
-//! IO requests are passed to the driver as [`kernel::types::ARef<Request>`]
+//! IO requests are passed to the driver as [`kernel::sync::aref::ARef<Request>`]
 //! instances. The `Request` type is a wrapper around the C `struct request`.
 //! The driver must mark end of processing by calling one of the
 //! `Request::end`, methods. Failure to do so can lead to deadlock or timeout
@@ -61,8 +61,8 @@
 //!     block::mq::*,
 //!     new_mutex,
 //!     prelude::*,
-//!     sync::{Arc, Mutex},
-//!     types::{ARef, ForeignOwnable},
+//!     sync::{Arc, Mutex, aref::{ARef, AlwaysRefCounted}},
+//!     types::ForeignOwnable,
 //! };
 //!
 //! struct MyBlkDevice;
rust/kernel/block/mq/operations.rs +1 -1
index 9ba7fdfeb..31f0f043f 100644
@@ -9,7 +9,7 @@
     block::mq::request::RequestDataWrapper,
     block::mq::Request,
     error::{from_result, Result},
-    types::ARef,
+    sync::aref::ARef,
 };
 use core::{marker::PhantomData, sync::atomic::AtomicU64, sync::atomic::Ordering};
 
rust/kernel/block/mq/request.rs +2 -1
index a0e22827f..1e57cfef6 100644
@@ -8,7 +8,8 @@
     bindings,
     block::mq::Operations,
     error::Result,
-    types::{ARef, AlwaysRefCounted, Opaque},
+    sync::aref::{ARef, AlwaysRefCounted},
+    types::Opaque,
 };
 use core::{
     marker::PhantomData,
rust/kernel/device.rs +3 -5
index 851018eef..21fb733fe 100644
@@ -4,10 +4,8 @@
 //!
 //! C header: [`include/linux/device.h`](srctree/include/linux/device.h)
 
-use crate::{
-    bindings,
-    types::{ARef, Opaque},
-};
+use crate::{bindings, sync::aref::ARef, types::Opaque};
+
 use core::ptr;
 
 /// A reference-counted device.
@@ -85,7 +83,7 @@ pub unsafe fn as_ref<'a>(ptr: *mut bindings::device) -> &'a Self {
 }
 
 // SAFETY: Instances of `Device` are always reference-counted.
-unsafe impl crate::types::AlwaysRefCounted for Device {
+unsafe impl crate::sync::aref::AlwaysRefCounted for Device {
     fn inc_ref(&self) {
         // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
         unsafe { bindings::get_device(self.as_raw()) };
rust/kernel/sync.rs +1 -0
index 0ab20975a..0d0c44f10 100644
@@ -8,6 +8,7 @@
 use crate::types::Opaque;
 
 mod arc;
+pub mod aref;
 mod condvar;
 pub mod lock;
 mod locked_by;
rust/kernel/sync/aref.rs new file +152 -0

152 lines not on this page · raw message

rust/kernel/task.rs +2 -2
index 55dff7e08..0b358acea 100644
@@ -61,7 +61,7 @@ macro_rules! current {
 /// incremented when creating `State` and decremented when it is dropped:
 ///
 /// ```
-/// use kernel::{task::Task, types::ARef};
+/// use kernel::{task::Task, sync::aref::ARef};
 ///
 /// struct State {
 ///     creator: ARef<Task>,
@@ -164,7 +164,7 @@ pub fn wake_up(&self) {
 }
 
 // SAFETY: The type invariants guarantee that `Task` is always refcounted.
-unsafe impl crate::types::AlwaysRefCounted for Task {
+unsafe impl crate::sync::aref::AlwaysRefCounted for Task {
     fn inc_ref(&self) {
         // SAFETY: The existence of a shared reference means that the refcount is nonzero.
         unsafe { bindings::get_task_struct(self.0.get()) };
rust/kernel/types.rs +2 -150

166 lines not on this page · raw message

Signature · 1 line
2.46.0
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] rust: Move `ARef<T>` and `AlwaysRefCounted` to new `sync::aref` module

Quoted 13 lines · Aliet Exposito Garcia

This patch refactors the ARef type and AlwaysRefCounted trait from types.rs into a new sync/aref.rs module.
The type and trait definitions remain unchanged.
- Adds rust/kernel/sync/aref.rs with the definitions of ARef and AlwaysRefCounted
- Removes the same type and trait definitions from rust/kernel/types.rs
- Updates relevant files to import ARef and AlwaysRefCounted from

    `sync/aref.rs`
Quoted 12 lines
---
 drivers/block/rnull.rs             |   3 +-
 rust/kernel/block/mq.rs            |   6 +-
 rust/kernel/block/mq/operations.rs |   2 +-
 rust/kernel/block/mq/request.rs    |   3 +-
 rust/kernel/device.rs              |   8 +-
 rust/kernel/sync.rs                |   1 +
 rust/kernel/sync/aref.rs           | 152 +++++++++++++++++++++++++++++
 rust/kernel/task.rs                |   4 +-
 rust/kernel/types.rs               | 152 +----------------------------
 9 files changed, 167 insertions(+), 164 deletions(-)
 create mode 100644 rust/kernel/sync/aref.rs

---
Cheers,
Benno

Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] rust: Move `ARef<T>` and `AlwaysRefCounted` to new `sync::aref` module

In reply to Aliet Exposito Garcia
Quoted 7 lines · Aliet Exposito Garcia

This patch refactors the ARef type and AlwaysRefCounted trait from types.rs into a new sync/aref.rs module.
The type and trait definitions remain unchanged.
- Adds rust/kernel/sync/aref.rs with the definitions of ARef and AlwaysRefCounted
- Removes the same type and trait definitions from rust/kernel/types.rs
- Updates relevant files to import ARef and AlwaysRefCounted from

Quoted 6 lines
    `sync/aref.rs`

Thanks,
Fiona

Applied
#7 lore raw
In reply to Aliet Exposito Garcia
Quoted 10 lines · Aliet Exposito Garcia

Hello all,
These are the patches for a good first issue that Benno Lossin oppened on https://github.com/Rust-for-Linux/linux/issues/1117
According to the issue description, the file rust/kernel/types.rs is a rather non-descriptive, general file that contains code that might be better suited elsewhere:
- ARef<T> and AlwaysRefCounted should be in rust/kernel/sync/aref.rs
- The FromBytes and AsBytes traits and implementations should be in rust/kernel/transmute.rs

Applied to rust-next -- thanks everyone!

[ Rebased on top of the lints series and slightly reworded. - Miguel ]

[ Rebased on top of the lints series, slightly reworded and removed
unneeded use AlwaysRefCounted in example. - Miguel ]

Aliet: please double-check that you are happy with the rebase.

Cheers,
Miguel

#8 lore raw
Quoted 11 lines · Miguel Ojeda
Quoted 9 lines · Aliet Exposito Garcia

Hello all,
These are the patches for a good first issue that Benno Lossin oppened on https://github.com/Rust-for-Linux/linux/issues/1117
According to the issue description, the file rust/kernel/types.rs is a rather non-descriptive, general file that contains code that might be better suited elsewhere:
- ARef<T> and AlwaysRefCounted should be in rust/kernel/sync/aref.rs
- The FromBytes and AsBytes traits and implementations should be in rust/kernel/transmute.rs

Quoted 8 lines

Applied to rust-next -- thanks everyone!

[ Rebased on top of the lints series and slightly reworded. - Miguel ]

[ Rebased on top of the lints series, slightly reworded and removed
unneeded use AlwaysRefCounted in example. - Miguel ]

Aliet: please double-check that you are happy with the rebase.

Hello Miguel, yes, I have double-checked and I am happy with the rebase.

Quoted 2 lines

Cheers,
Miguel

I want to continue studying Rust so that little by little I can help in the Rust-for-Linux subsystem.

Thank you very much to everyone; you have an excellent community, very welcoming to beginners.

Best regards,

Aliet

#9 lore raw
Quoted 8 lines · Aliet Expósito

Hello Miguel, yes, I have double-checked and I am happy with the rebase.

I want to continue studying Rust so that little by little I can help in the Rust-for-Linux subsystem.

Thank you very much to everyone; you have an excellent community, very welcoming to beginners.

Thanks for double-checking and the kind words -- messages like this may make others decide to contribute! :)

Cheers,
Miguel

#10 lore raw
In reply to Miguel Ojeda
Quoted 7 lines · Miguel Ojeda

Applied to rust-next -- thanks everyone!

[ Rebased on top of the lints series and slightly reworded. - Miguel ]

[ Rebased on top of the lints series, slightly reworded and removed
unneeded use AlwaysRefCounted in example. - Miguel ]

The other day I dropped temporarily the second patch to keep linux-next building and to simplify merging the VFS series. I will put it back later on.

Cheers,
Miguel