mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-08-30 14:33:24 -04:00
rust: treewide: replace __pinned_init with raw_[try_]init
The `__init` method is not designed to be a public API (existence of "__" is a hint for this); replace users with `pin_init::raw_[try_]init` which does the same thing. There are a few users of `__init` which are replaced as well. Acked-by: Miguel Ojeda <ojeda@kernel.org> Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://patch.msgid.link/20260729-merge-init-v2-4-26adf47109e7@garyguo.net Signed-off-by: Gary Guo <gary@garyguo.net>
This commit is contained in:
@@ -645,8 +645,8 @@ fn send_single_command<M>(&mut self, bar: Bar0<'_>, command: M) -> Result
|
||||
// SAFETY: `msg_header` and `cmd` are valid references, and not touched if the initializer
|
||||
// fails.
|
||||
unsafe {
|
||||
msg_element.__init(core::ptr::from_mut(dst.header))?;
|
||||
command.init().__init(core::ptr::from_mut(cmd))?;
|
||||
pin_init::raw_try_init(core::ptr::from_mut(dst.header), msg_element)?;
|
||||
pin_init::raw_try_init(core::ptr::from_mut(cmd), command.init())?;
|
||||
}
|
||||
|
||||
// Fill the variable-length payload, which may be empty.
|
||||
|
||||
@@ -372,13 +372,13 @@ pub fn pin_slice<Func, Item, E>(
|
||||
// - `ptr` is a valid pointer to uninitialized memory.
|
||||
// - `ptr` is not used if an error is returned.
|
||||
// - `ptr` won't be moved until it is dropped, i.e. it is pinned.
|
||||
unsafe { init(i).__pinned_init(ptr)? };
|
||||
unsafe { pin_init::raw_try_init(ptr, init(i))? };
|
||||
|
||||
// SAFETY:
|
||||
// - `i + 1 <= len`, hence we don't exceed the capacity, due to the call to
|
||||
// `with_capacity()` above.
|
||||
// - The new value at index buffer.len() + 1 is the only element being added here, and
|
||||
// it has been initialized above by `init(i).__pinned_init(ptr)`.
|
||||
// it has been initialized above by `raw_try_init(ptr, i)`.
|
||||
unsafe { buffer.inc_len(1) };
|
||||
}
|
||||
|
||||
@@ -463,7 +463,7 @@ fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E
|
||||
let slot = self.as_mut_ptr();
|
||||
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
|
||||
// slot is valid.
|
||||
unsafe { init.__init(slot)? };
|
||||
unsafe { pin_init::raw_try_init(slot, init)? };
|
||||
// SAFETY: All fields have been initialized.
|
||||
Ok(unsafe { Box::assume_init(self) })
|
||||
}
|
||||
@@ -473,7 +473,7 @@ fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Ini
|
||||
let slot = self.as_mut_ptr();
|
||||
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
|
||||
// slot is valid and will not be moved, because we pin it later.
|
||||
unsafe { init.__pinned_init(slot)? };
|
||||
unsafe { pin_init::raw_try_init(slot, init)? };
|
||||
// SAFETY: All fields have been initialized.
|
||||
Ok(unsafe { Box::assume_init(self) }.into())
|
||||
}
|
||||
|
||||
@@ -449,7 +449,7 @@ pub fn init_at<E>(&mut self, i: usize, init: impl Init<T, E>) -> Result
|
||||
// - `T: AsBytes + FromBytes` guarantees all bit patterns are valid, so partial writes on
|
||||
// error cannot leave the element in an invalid state.
|
||||
// - The DMA address has not been exposed yet, so there is no concurrent device access.
|
||||
unsafe { init.__init(ptr)? };
|
||||
unsafe { pin_init::raw_try_init(ptr, init)? };
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -791,10 +791,10 @@ pub fn init_with_attrs<E>(
|
||||
|
||||
// SAFETY:
|
||||
// - `ptr` is valid, properly aligned, and points to exclusively owned memory.
|
||||
// - If `__init` fails, `self` is dropped, which safely frees the underlying `Coherent`'s
|
||||
// DMA memory. `T: AsBytes + FromBytes` ensures there are no complex `Drop` requirements
|
||||
// we are bypassing.
|
||||
unsafe { init.__init(ptr)? };
|
||||
// - If `raw_try_init` fails, `self` is dropped, which safely frees the underlying
|
||||
// `Coherent`'s DMA memory. `T: AsBytes + FromBytes` ensures there are no complex `Drop`
|
||||
// requirements we are bypassing.
|
||||
unsafe { pin_init::raw_try_init(ptr, init)? };
|
||||
|
||||
Ok(dmem)
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ pub fn new(dev: &device::Device, data: impl PinInit<T::Data, Error>) -> Result<S
|
||||
// SAFETY:
|
||||
// - `raw_data` is a valid pointer to uninitialized memory.
|
||||
// - `raw_data` will not move until it is dropped.
|
||||
unsafe { data.__pinned_init(raw_data) }.inspect_err(|_| {
|
||||
unsafe { pin_init::raw_try_init(raw_data, data) }.inspect_err(|_| {
|
||||
// SAFETY: `__drm_dev_alloc()` was successful, hence `drm_dev` must be valid and the
|
||||
// refcount must be non-zero.
|
||||
unsafe { bindings::drm_dev_put(drm_dev) };
|
||||
|
||||
@@ -116,7 +116,7 @@ pub fn new(flags: AllocFlags) -> Result<GpuVaAlloc<T>, AllocError> {
|
||||
pub(super) fn prepare(mut self, va_data: impl PinInit<T::VaData>) -> *mut bindings::drm_gpuva {
|
||||
let va_ptr = MaybeUninit::as_mut_ptr(&mut self.0);
|
||||
// SAFETY: The `data` field is pinned.
|
||||
let Ok(()) = unsafe { va_data.__pinned_init(&raw mut (*va_ptr).data) };
|
||||
unsafe { pin_init::raw_init(&raw mut (*va_ptr).data, va_data) };
|
||||
KBox::into_raw(self.0).cast()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ pub(super) fn new(
|
||||
};
|
||||
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
|
||||
// SAFETY: `ptr->data` is a valid pinned location.
|
||||
let Ok(()) = unsafe { value.__pinned_init(&raw mut (*raw_ptr).data) };
|
||||
unsafe { pin_init::raw_init(&raw mut (*raw_ptr).data, value) };
|
||||
// INVARIANTS: We just created the vm_bo so it's absent from lists, and the data is valid
|
||||
// as we just initialized it.
|
||||
Ok(GpuVmBoAlloc(ptr))
|
||||
|
||||
@@ -158,7 +158,9 @@ fn pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> error::Result<Self::Pi
|
||||
{
|
||||
// SAFETY: We delegate to `init` and only change the error type.
|
||||
let init = unsafe {
|
||||
pin_init_from_closure(|slot| init.__pinned_init(slot).map_err(|e| Error::from(e)))
|
||||
pin_init_from_closure(|slot| {
|
||||
pin_init::raw_try_init(slot, init).map_err(|e| Error::from(e))
|
||||
})
|
||||
};
|
||||
Self::try_pin_init(init, flags)
|
||||
}
|
||||
@@ -176,7 +178,7 @@ fn init<E>(init: impl Init<T, E>, flags: Flags) -> error::Result<Self>
|
||||
{
|
||||
// SAFETY: We delegate to `init` and only change the error type.
|
||||
let init = unsafe {
|
||||
init_from_closure(|slot| init.__pinned_init(slot).map_err(|e| Error::from(e)))
|
||||
init_from_closure(|slot| pin_init::raw_try_init(slot, init).map_err(|e| Error::from(e)))
|
||||
};
|
||||
Self::try_init(init, flags)
|
||||
}
|
||||
|
||||
@@ -600,7 +600,7 @@ pub fn new<'a>(
|
||||
let drvdata_ptr = unsafe { bindings::pwmchip_get_drvdata(c_chip_ptr) };
|
||||
|
||||
// SAFETY: We construct the `T` object in-place in the allocated private memory.
|
||||
unsafe { data.__pinned_init(drvdata_ptr.cast()) }.inspect_err(|_| {
|
||||
unsafe { pin_init::raw_try_init(drvdata_ptr.cast(), data) }.inspect_err(|_| {
|
||||
// SAFETY: It is safe to call `pwmchip_put()` with a valid pointer obtained
|
||||
// from `pwmchip_alloc()`. We will not use pointer after this.
|
||||
unsafe { bindings::pwmchip_put(c_chip_ptr) }
|
||||
|
||||
@@ -717,7 +717,7 @@ fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E
|
||||
let slot = self.as_mut_ptr();
|
||||
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
|
||||
// slot is valid.
|
||||
unsafe { init.__init(slot)? };
|
||||
unsafe { pin_init::raw_try_init(slot, init)? };
|
||||
// SAFETY: All fields have been initialized.
|
||||
Ok(unsafe { self.assume_init() })
|
||||
}
|
||||
@@ -727,7 +727,7 @@ fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Ini
|
||||
let slot = self.as_mut_ptr();
|
||||
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
|
||||
// slot is valid and will not be moved, because we pin it later.
|
||||
unsafe { init.__pinned_init(slot)? };
|
||||
unsafe { pin_init::raw_try_init(slot, init)? };
|
||||
// SAFETY: All fields have been initialized.
|
||||
Ok(unsafe { self.assume_init() }.into())
|
||||
}
|
||||
@@ -795,7 +795,7 @@ pub unsafe fn assume_init(self) -> UniqueArc<T> {
|
||||
#[inline]
|
||||
pub fn init_with<E>(mut self, init: impl Init<T, E>) -> core::result::Result<UniqueArc<T>, E> {
|
||||
// SAFETY: The supplied pointer is valid for initialization.
|
||||
match unsafe { init.__init(self.as_mut_ptr()) } {
|
||||
match unsafe { pin_init::raw_try_init(self.as_mut_ptr(), init) } {
|
||||
// SAFETY: Initialization completed successfully.
|
||||
Ok(()) => Ok(unsafe { self.assume_init() }),
|
||||
Err(err) => Err(err),
|
||||
@@ -810,7 +810,7 @@ pub fn pin_init_with<E>(
|
||||
) -> core::result::Result<Pin<UniqueArc<T>>, E> {
|
||||
// SAFETY: The supplied pointer is valid for initialization and we will later pin the value
|
||||
// to ensure it does not move.
|
||||
match unsafe { init.__pinned_init(self.as_mut_ptr()) } {
|
||||
match unsafe { pin_init::raw_try_init(self.as_mut_ptr(), init) } {
|
||||
// SAFETY: Initialization completed successfully.
|
||||
Ok(()) => Ok(unsafe { self.assume_init() }.into()),
|
||||
Err(err) => Err(err),
|
||||
|
||||
@@ -417,13 +417,13 @@ pub const fn cast_from(this: *const T) -> *const Self {
|
||||
|
||||
impl<T> Wrapper<T> for Opaque<T> {
|
||||
/// Create an opaque pin-initializer from the given pin-initializer.
|
||||
fn pin_init<E>(slot: impl PinInit<T, E>) -> impl PinInit<Self, E> {
|
||||
Self::try_ffi_init(|ptr: *mut T| {
|
||||
fn pin_init<E>(init: impl PinInit<T, E>) -> impl PinInit<Self, E> {
|
||||
Self::try_ffi_init(|slot: *mut T| {
|
||||
// SAFETY:
|
||||
// - `ptr` is a valid pointer to uninitialized memory,
|
||||
// - `slot` is a valid pointer to uninitialized memory,
|
||||
// - `slot` is not accessed on error,
|
||||
// - `slot` is pinned in memory.
|
||||
unsafe { PinInit::<T, E>::__pinned_init(slot, ptr) }
|
||||
unsafe { pin_init::raw_try_init(slot, init) }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -621,7 +621,7 @@ unsafe fn __init() -> ::kernel::ffi::c_int {
|
||||
// SAFETY: No data race, since `__MOD` can only be accessed by this module
|
||||
// and there only `__init` and `__exit` access it. These functions are only
|
||||
// called once and `__exit` cannot be called before or during `__init`.
|
||||
match unsafe { initer.__pinned_init(__MOD.as_mut_ptr()) } {
|
||||
match unsafe { ::pin_init::raw_try_init(__MOD.as_mut_ptr(), initer) } {
|
||||
Ok(m) => 0,
|
||||
Err(e) => e.to_errno(),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user