Skip to content

Commit 2f3d8d5

Browse files
wedsonaffbq
authored andcommitted
rust: init: update init module to take allocation flags
This is the last component in the conversion for allocators to take allocation flags as parameters. Reviewed-by: Benno Lossin <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 5ccd2a6 commit 2f3d8d5

File tree

6 files changed

+50
-41
lines changed

6 files changed

+50
-41
lines changed

rust/kernel/init.rs

+25-24
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
//! # a <- new_mutex!(42, "Foo::a"),
6969
//! # b: 24,
7070
//! # });
71-
//! let foo: Result<Pin<Box<Foo>>> = Box::pin_init(foo);
71+
//! let foo: Result<Pin<Box<Foo>>> = Box::pin_init(foo, GFP_KERNEL);
7272
//! ```
7373
//!
7474
//! For more information see the [`pin_init!`] macro.
@@ -80,7 +80,8 @@
8080
//!
8181
//! ```rust
8282
//! # use kernel::sync::{new_mutex, Arc, Mutex};
83-
//! let mtx: Result<Arc<Mutex<usize>>> = Arc::pin_init(new_mutex!(42, "example::mtx"));
83+
//! let mtx: Result<Arc<Mutex<usize>>> =
84+
//! Arc::pin_init(new_mutex!(42, "example::mtx"), GFP_KERNEL);
8485
//! ```
8586
//!
8687
//! To declare an init macro/function you just return an [`impl PinInit<T, E>`]:
@@ -99,7 +100,7 @@
99100
//! fn new() -> impl PinInit<Self, Error> {
100101
//! try_pin_init!(Self {
101102
//! status <- new_mutex!(0, "DriverData::status"),
102-
//! buffer: Box::init(kernel::init::zeroed())?,
103+
//! buffer: Box::init(kernel::init::zeroed(), GFP_KERNEL)?,
103104
//! })
104105
//! }
105106
//! }
@@ -210,7 +211,7 @@
210211
//! [`pin_init!`]: crate::pin_init!
211212
212213
use crate::{
213-
alloc::{box_ext::BoxExt, flags::*},
214+
alloc::{box_ext::BoxExt, Flags},
214215
error::{self, Error},
215216
sync::UniqueArc,
216217
types::{Opaque, ScopeGuard},
@@ -391,7 +392,7 @@ macro_rules! stack_try_pin_init {
391392
/// },
392393
/// });
393394
/// # initializer }
394-
/// # Box::pin_init(demo()).unwrap();
395+
/// # Box::pin_init(demo(), GFP_KERNEL).unwrap();
395396
/// ```
396397
///
397398
/// Arbitrary Rust expressions can be used to set the value of a variable.
@@ -461,7 +462,7 @@ macro_rules! stack_try_pin_init {
461462
/// # })
462463
/// # }
463464
/// # }
464-
/// let foo = Box::pin_init(Foo::new());
465+
/// let foo = Box::pin_init(Foo::new(), GFP_KERNEL);
465466
/// ```
466467
///
467468
/// They can also easily embed it into their own `struct`s:
@@ -601,7 +602,7 @@ macro_rules! pin_init {
601602
/// impl BigBuf {
602603
/// fn new() -> impl PinInit<Self, Error> {
603604
/// try_pin_init!(Self {
604-
/// big: Box::init(init::zeroed())?,
605+
/// big: Box::init(init::zeroed(), GFP_KERNEL)?,
605606
/// small: [0; 1024 * 1024],
606607
/// ptr: core::ptr::null_mut(),
607608
/// }? Error)
@@ -702,7 +703,7 @@ macro_rules! init {
702703
/// impl BigBuf {
703704
/// fn new() -> impl Init<Self, Error> {
704705
/// try_init!(Self {
705-
/// big: Box::init(zeroed())?,
706+
/// big: Box::init(zeroed(), GFP_KERNEL)?,
706707
/// small: [0; 1024 * 1024],
707708
/// }? Error)
708709
/// }
@@ -1014,7 +1015,7 @@ pub fn uninit<T, E>() -> impl Init<MaybeUninit<T>, E> {
10141015
///
10151016
/// ```rust
10161017
/// use kernel::{error::Error, init::init_array_from_fn};
1017-
/// let array: Box<[usize; 1_000]> = Box::init::<Error>(init_array_from_fn(|i| i)).unwrap();
1018+
/// let array: Box<[usize; 1_000]> = Box::init::<Error>(init_array_from_fn(|i| i), GFP_KERNEL).unwrap();
10181019
/// assert_eq!(array.len(), 1_000);
10191020
/// ```
10201021
pub fn init_array_from_fn<I, const N: usize, T, E>(
@@ -1058,7 +1059,7 @@ where
10581059
/// ```rust
10591060
/// use kernel::{sync::{Arc, Mutex}, init::pin_init_array_from_fn, new_mutex};
10601061
/// let array: Arc<[Mutex<usize>; 1_000]> =
1061-
/// Arc::pin_init(pin_init_array_from_fn(|i| new_mutex!(i))).unwrap();
1062+
/// Arc::pin_init(pin_init_array_from_fn(|i| new_mutex!(i)), GFP_KERNEL).unwrap();
10621063
/// assert_eq!(array.len(), 1_000);
10631064
/// ```
10641065
pub fn pin_init_array_from_fn<I, const N: usize, T, E>(
@@ -1116,50 +1117,50 @@ pub trait InPlaceInit<T>: Sized {
11161117
/// type.
11171118
///
11181119
/// If `T: !Unpin` it will not be able to move afterwards.
1119-
fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
1120+
fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Pin<Self>, E>
11201121
where
11211122
E: From<AllocError>;
11221123

11231124
/// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this
11241125
/// type.
11251126
///
11261127
/// If `T: !Unpin` it will not be able to move afterwards.
1127-
fn pin_init<E>(init: impl PinInit<T, E>) -> error::Result<Pin<Self>>
1128+
fn pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> error::Result<Pin<Self>>
11281129
where
11291130
Error: From<E>,
11301131
{
11311132
// SAFETY: We delegate to `init` and only change the error type.
11321133
let init = unsafe {
11331134
pin_init_from_closure(|slot| init.__pinned_init(slot).map_err(|e| Error::from(e)))
11341135
};
1135-
Self::try_pin_init(init)
1136+
Self::try_pin_init(init, flags)
11361137
}
11371138

11381139
/// Use the given initializer to in-place initialize a `T`.
1139-
fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>
1140+
fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
11401141
where
11411142
E: From<AllocError>;
11421143

11431144
/// Use the given initializer to in-place initialize a `T`.
1144-
fn init<E>(init: impl Init<T, E>) -> error::Result<Self>
1145+
fn init<E>(init: impl Init<T, E>, flags: Flags) -> error::Result<Self>
11451146
where
11461147
Error: From<E>,
11471148
{
11481149
// SAFETY: We delegate to `init` and only change the error type.
11491150
let init = unsafe {
11501151
init_from_closure(|slot| init.__pinned_init(slot).map_err(|e| Error::from(e)))
11511152
};
1152-
Self::try_init(init)
1153+
Self::try_init(init, flags)
11531154
}
11541155
}
11551156

11561157
impl<T> InPlaceInit<T> for Box<T> {
11571158
#[inline]
1158-
fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
1159+
fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Pin<Self>, E>
11591160
where
11601161
E: From<AllocError>,
11611162
{
1162-
let mut this = <Box<_> as BoxExt<_>>::new_uninit(GFP_KERNEL)?;
1163+
let mut this = <Box<_> as BoxExt<_>>::new_uninit(flags)?;
11631164
let slot = this.as_mut_ptr();
11641165
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
11651166
// slot is valid and will not be moved, because we pin it later.
@@ -1169,11 +1170,11 @@ impl<T> InPlaceInit<T> for Box<T> {
11691170
}
11701171

11711172
#[inline]
1172-
fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>
1173+
fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
11731174
where
11741175
E: From<AllocError>,
11751176
{
1176-
let mut this = <Box<_> as BoxExt<_>>::new_uninit(GFP_KERNEL)?;
1177+
let mut this = <Box<_> as BoxExt<_>>::new_uninit(flags)?;
11771178
let slot = this.as_mut_ptr();
11781179
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
11791180
// slot is valid.
@@ -1185,11 +1186,11 @@ impl<T> InPlaceInit<T> for Box<T> {
11851186

11861187
impl<T> InPlaceInit<T> for UniqueArc<T> {
11871188
#[inline]
1188-
fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
1189+
fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Pin<Self>, E>
11891190
where
11901191
E: From<AllocError>,
11911192
{
1192-
let mut this = UniqueArc::new_uninit(GFP_KERNEL)?;
1193+
let mut this = UniqueArc::new_uninit(flags)?;
11931194
let slot = this.as_mut_ptr();
11941195
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
11951196
// slot is valid and will not be moved, because we pin it later.
@@ -1199,11 +1200,11 @@ impl<T> InPlaceInit<T> for UniqueArc<T> {
11991200
}
12001201

12011202
#[inline]
1202-
fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>
1203+
fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
12031204
where
12041205
E: From<AllocError>,
12051206
{
1206-
let mut this = UniqueArc::new_uninit(GFP_KERNEL)?;
1207+
let mut this = UniqueArc::new_uninit(flags)?;
12071208
let slot = this.as_mut_ptr();
12081209
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
12091210
// slot is valid.

rust/kernel/sync/arc.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,22 @@ impl<T> Arc<T> {
182182
///
183183
/// If `T: !Unpin` it will not be able to move afterwards.
184184
#[inline]
185-
pub fn pin_init<E>(init: impl PinInit<T, E>) -> error::Result<Self>
185+
pub fn pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> error::Result<Self>
186186
where
187187
Error: From<E>,
188188
{
189-
UniqueArc::pin_init(init).map(|u| u.into())
189+
UniqueArc::pin_init(init, flags).map(|u| u.into())
190190
}
191191

192192
/// Use the given initializer to in-place initialize a `T`.
193193
///
194194
/// This is equivalent to [`Arc<T>::pin_init`], since an [`Arc`] is always pinned.
195195
#[inline]
196-
pub fn init<E>(init: impl Init<T, E>) -> error::Result<Self>
196+
pub fn init<E>(init: impl Init<T, E>, flags: Flags) -> error::Result<Self>
197197
where
198198
Error: From<E>,
199199
{
200-
UniqueArc::init(init).map(|u| u.into())
200+
UniqueArc::init(init, flags).map(|u| u.into())
201201
}
202202
}
203203

@@ -565,13 +565,16 @@ impl<T> UniqueArc<T> {
565565
}
566566

567567
/// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet.
568-
pub fn new_uninit(_flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> {
568+
pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> {
569569
// INVARIANT: The refcount is initialised to a non-zero value.
570-
let inner = Box::try_init::<AllocError>(try_init!(ArcInner {
571-
// SAFETY: There are no safety requirements for this FFI call.
572-
refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
573-
data <- init::uninit::<T, AllocError>(),
574-
}? AllocError))?;
570+
let inner = Box::try_init::<AllocError>(
571+
try_init!(ArcInner {
572+
// SAFETY: There are no safety requirements for this FFI call.
573+
refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
574+
data <- init::uninit::<T, AllocError>(),
575+
}? AllocError),
576+
flags,
577+
)?;
575578
Ok(UniqueArc {
576579
// INVARIANT: The newly-created object has a refcount of 1.
577580
// SAFETY: The pointer from the `Box` is valid.

rust/kernel/sync/condvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub use new_condvar;
7575
/// Box::pin_init(pin_init!(Example {
7676
/// value <- new_mutex!(0),
7777
/// value_changed <- new_condvar!(),
78-
/// }))
78+
/// }), GFP_KERNEL)
7979
/// }
8080
/// ```
8181
///

rust/kernel/sync/lock/mutex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub use new_mutex;
6060
/// }
6161
///
6262
/// // Allocate a boxed `Example`.
63-
/// let e = Box::pin_init(Example::new())?;
63+
/// let e = Box::pin_init(Example::new(), GFP_KERNEL)?;
6464
/// assert_eq!(e.c, 10);
6565
/// assert_eq!(e.d.lock().a, 20);
6666
/// assert_eq!(e.d.lock().b, 30);

rust/kernel/sync/lock/spinlock.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub use new_spinlock;
5858
/// }
5959
///
6060
/// // Allocate a boxed `Example`.
61-
/// let e = Box::pin_init(Example::new())?;
61+
/// let e = Box::pin_init(Example::new(), GFP_KERNEL)?;
6262
/// assert_eq!(e.c, 10);
6363
/// assert_eq!(e.d.lock().a, 20);
6464
/// assert_eq!(e.d.lock().b, 30);

rust/kernel/workqueue.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
//! Arc::pin_init(pin_init!(MyStruct {
5454
//! value,
5555
//! work <- new_work!("MyStruct::work"),
56-
//! }))
56+
//! }), GFP_KERNEL)
5757
//! }
5858
//! }
5959
//!
@@ -101,7 +101,7 @@
101101
//! value_2,
102102
//! work_1 <- new_work!("MyStruct::work_1"),
103103
//! work_2 <- new_work!("MyStruct::work_2"),
104-
//! }))
104+
//! }), GFP_KERNEL)
105105
//! }
106106
//! }
107107
//!
@@ -132,6 +132,7 @@
132132
//!
133133
//! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h)
134134
135+
use crate::alloc::Flags;
135136
use crate::{bindings, prelude::*, sync::Arc, sync::LockClassKey, types::Opaque};
136137
use alloc::alloc::AllocError;
137138
use alloc::boxed::Box;
@@ -210,13 +211,17 @@ impl Queue {
210211
/// Tries to spawn the given function or closure as a work item.
211212
///
212213
/// This method can fail because it allocates memory to store the work item.
213-
pub fn try_spawn<T: 'static + Send + FnOnce()>(&self, func: T) -> Result<(), AllocError> {
214+
pub fn try_spawn<T: 'static + Send + FnOnce()>(
215+
&self,
216+
flags: Flags,
217+
func: T,
218+
) -> Result<(), AllocError> {
214219
let init = pin_init!(ClosureWork {
215220
work <- new_work!("Queue::try_spawn"),
216221
func: Some(func),
217222
});
218223

219-
self.enqueue(Box::pin_init(init).map_err(|_| AllocError)?);
224+
self.enqueue(Box::pin_init(init, flags).map_err(|_| AllocError)?);
220225
Ok(())
221226
}
222227
}

0 commit comments

Comments
 (0)