Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

drm: rust: rename as_ref() to from_raw() for drm constructors

The prefix as_* should not be used for a constructor. Constructors
usually use the prefix from_* instead.

Some prior art in the stdlib: Box::from_raw, CString::from_raw,
Rc::from_raw, Arc::from_raw, Waker::from_raw, File::from_raw_fd.

There is also prior art in the kernel crate: cpufreq::Policy::from_raw,
fs::File::from_raw_file, Kuid::from_raw, ARef::from_raw,
SeqFile::from_raw, VmaNew::from_raw, Io::from_raw.

Link: https://lore.kernel.org/r/aCd8D5IA0RXZvtcv@pollux
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Link: https://lore.kernel.org/r/20250711-device-as-ref-v2-2-1b16ab6402d7@google.com

authored by

Alice Ryhl and committed by
Danilo Krummrich
917b10d9 667efb34

+15 -15
+1 -1
rust/kernel/drm/device.rs
··· 154 154 /// Additionally, callers must ensure that the `struct device`, `ptr` is pointing to, is 155 155 /// embedded in `Self`. 156 156 #[doc(hidden)] 157 - pub unsafe fn as_ref<'a>(ptr: *const bindings::drm_device) -> &'a Self { 157 + pub unsafe fn from_raw<'a>(ptr: *const bindings::drm_device) -> &'a Self { 158 158 // SAFETY: By the safety requirements of this function `ptr` is a valid pointer to a 159 159 // `struct drm_device` embedded in `Self`. 160 160 let ptr = unsafe { Self::from_drm_device(ptr) };
+4 -4
rust/kernel/drm/file.rs
··· 32 32 /// # Safety 33 33 /// 34 34 /// `raw_file` must be a valid pointer to an open `struct drm_file`, opened through `T::open`. 35 - pub unsafe fn as_ref<'a>(ptr: *mut bindings::drm_file) -> &'a File<T> { 35 + pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_file) -> &'a File<T> { 36 36 // SAFETY: `raw_file` is valid by the safety requirements of this function. 37 37 unsafe { &*ptr.cast() } 38 38 } ··· 61 61 // SAFETY: A callback from `struct drm_driver::open` guarantees that 62 62 // - `raw_dev` is valid pointer to a `struct drm_device`, 63 63 // - the corresponding `struct drm_device` has been registered. 64 - let drm = unsafe { drm::Device::as_ref(raw_dev) }; 64 + let drm = unsafe { drm::Device::from_raw(raw_dev) }; 65 65 66 66 // SAFETY: `raw_file` is a valid pointer to a `struct drm_file`. 67 - let file = unsafe { File::<T>::as_ref(raw_file) }; 67 + let file = unsafe { File::<T>::from_raw(raw_file) }; 68 68 69 69 let inner = match T::open(drm) { 70 70 Err(e) => { ··· 89 89 raw_file: *mut bindings::drm_file, 90 90 ) { 91 91 // SAFETY: This reference won't escape this function 92 - let file = unsafe { File::<T>::as_ref(raw_file) }; 92 + let file = unsafe { File::<T>::from_raw(raw_file) }; 93 93 94 94 // SAFETY: `file.driver_priv` has been created in `open_callback` through `KBox::into_raw`. 95 95 let _ = unsafe { KBox::from_raw(file.driver_priv()) };
+8 -8
rust/kernel/drm/gem/mod.rs
··· 51 51 /// - `self_ptr` must be a valid pointer to `Self`. 52 52 /// - The caller promises that holding the immutable reference returned by this function does 53 53 /// not violate rust's data aliasing rules and remains valid throughout the lifetime of `'a`. 54 - unsafe fn as_ref<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self; 54 + unsafe fn from_raw<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self; 55 55 } 56 56 57 57 // SAFETY: All gem objects are refcounted. ··· 86 86 ) -> core::ffi::c_int { 87 87 // SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`. 88 88 let file = unsafe { 89 - drm::File::<<<U as IntoGEMObject>::Driver as drm::Driver>::File>::as_ref(raw_file) 89 + drm::File::<<<U as IntoGEMObject>::Driver as drm::Driver>::File>::from_raw(raw_file) 90 90 }; 91 91 // SAFETY: `open_callback` is specified in the AllocOps structure for `Object<T>`, ensuring that 92 92 // `raw_obj` is indeed contained within a `Object<T>`. 93 93 let obj = unsafe { 94 - <<<U as IntoGEMObject>::Driver as drm::Driver>::Object as IntoGEMObject>::as_ref(raw_obj) 94 + <<<U as IntoGEMObject>::Driver as drm::Driver>::Object as IntoGEMObject>::from_raw(raw_obj) 95 95 }; 96 96 97 97 match T::open(obj, file) { ··· 106 106 ) { 107 107 // SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`. 108 108 let file = unsafe { 109 - drm::File::<<<U as IntoGEMObject>::Driver as drm::Driver>::File>::as_ref(raw_file) 109 + drm::File::<<<U as IntoGEMObject>::Driver as drm::Driver>::File>::from_raw(raw_file) 110 110 }; 111 111 // SAFETY: `close_callback` is specified in the AllocOps structure for `Object<T>`, ensuring 112 112 // that `raw_obj` is indeed contained within a `Object<T>`. 113 113 let obj = unsafe { 114 - <<<U as IntoGEMObject>::Driver as drm::Driver>::Object as IntoGEMObject>::as_ref(raw_obj) 114 + <<<U as IntoGEMObject>::Driver as drm::Driver>::Object as IntoGEMObject>::from_raw(raw_obj) 115 115 }; 116 116 117 117 T::close(obj, file); ··· 124 124 self.obj.get() 125 125 } 126 126 127 - unsafe fn as_ref<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self { 127 + unsafe fn from_raw<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self { 128 128 let self_ptr: *mut Opaque<bindings::drm_gem_object> = self_ptr.cast(); 129 129 130 130 // SAFETY: `obj` is guaranteed to be in an `Object<T>` via the safety contract of this ··· 170 170 // - A `drm::Driver` can only have a single `File` implementation. 171 171 // - `file` uses the same `drm::Driver` as `Self`. 172 172 // - Therefore, we're guaranteed that `ptr` must be a gem object embedded within `Self`. 173 - // - And we check if the pointer is null befoe calling as_ref(), ensuring that `ptr` is a 173 + // - And we check if the pointer is null befoe calling from_raw(), ensuring that `ptr` is a 174 174 // valid pointer to an initialized `Self`. 175 - let obj = unsafe { Self::as_ref(ptr) }; 175 + let obj = unsafe { Self::from_raw(ptr) }; 176 176 177 177 // SAFETY: 178 178 // - We take ownership of the reference of `drm_gem_object_lookup()`.
+2 -2
rust/kernel/drm/ioctl.rs
··· 134 134 // FIXME: Currently there is nothing enforcing that the types of the 135 135 // dev/file match the current driver these ioctls are being declared 136 136 // for, and it's not clear how to enforce this within the type system. 137 - let dev = $crate::drm::device::Device::as_ref(raw_dev); 137 + let dev = $crate::drm::device::Device::from_raw(raw_dev); 138 138 // SAFETY: The ioctl argument has size `_IOC_SIZE(cmd)`, which we 139 139 // asserted above matches the size of this type, and all bit patterns of 140 140 // UAPI structs must be valid. ··· 142 142 &*(raw_data as *const $crate::types::Opaque<$crate::uapi::$struct>) 143 143 }; 144 144 // SAFETY: This is just the DRM file structure 145 - let file = unsafe { $crate::drm::File::as_ref(raw_file) }; 145 + let file = unsafe { $crate::drm::File::from_raw(raw_file) }; 146 146 147 147 match $func(dev, data, file) { 148 148 Err(e) => e.to_errno(),