rust: aref: make `AlwaysRefCounted::inc_ref` an associated function
Subsystems (22)
PATCH v7 1/1 rust: aref: make `AlwaysRefCounted::inc_ref` an associated function
AlwaysRefCounted::inc_ref is a function that shouldn't be called lightly.
To prevent accidentally calling it, change inc_ref to be an associated function.
Modify all AlwaysRefCounted implementors to work with this change.
---
Changes in v2:
- Don't word wrap the patch
Changes in v3:
- Make argument name of Empty::inc_ref consistent with Empty::dec_ref
Changes in v4:
- Rebase to new rust-next, change new implementors
- Reword explanation for change in AlwaysRefCounted::inc_ref doc comment
Changes in v5:
- Change commit message to be imperative
Changes in v6:
- Change all the implementors
Changes in v7:
- Correct changes for implementors that are conditionally compiled
--- rust/kernel/auxiliary.rs | 4 ++-- rust/kernel/block/mq/request.rs | 4 ++-- rust/kernel/cred.rs | 4 ++-- rust/kernel/device.rs | 4 ++-- rust/kernel/device/property.rs | 4 ++-- rust/kernel/drm/device.rs | 4 ++-- rust/kernel/drm/gem/mod.rs | 4 ++-- rust/kernel/drm/gpuvm/mod.rs | 4 ++-- rust/kernel/drm/gpuvm/vm_bo.rs | 4 ++-- rust/kernel/fs/file.rs | 8 ++++---- rust/kernel/i2c.rs | 8 ++++---- rust/kernel/mm.rs | 8 ++++---- rust/kernel/mm/mmput_async.rs | 4 ++-- rust/kernel/opp.rs | 4 ++-- rust/kernel/pci.rs | 4 ++-- rust/kernel/pid_namespace.rs | 4 ++-- rust/kernel/platform.rs | 4 ++-- rust/kernel/pwm.rs | 4 ++-- rust/kernel/sync/aref.rs | 11 +++++++---- rust/kernel/task.rs | 4 ++-- rust/kernel/usb.rs | 8 ++++---- 21 files changed, 55 insertions(+), 52 deletions(-)
rust/kernel/auxiliary.rs +2 -2
@@ -345,9 +345,9 @@ unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx>// SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. -unsafe { bindings::get_device (self.as_ref ().as_raw ()) }; +unsafe { bindings::get_device (obj.as_ref ().as_raw ()) }; }unsafe fn dec_ref (obj:NonNull <Self >) {
rust/kernel/block/mq/request.rs +2 -2
@@ -234,8 +234,8 @@ unsafe impl<T: Operations> Sync for Request<T> {}// keeps the object alive in memory at least until a matching reference count // decrement is executed. unsafe impl <T :Operations >AlwaysRefCounted for Request <T > { -fn inc_ref (&self) { - self.wrapper_ref ().refcount ().inc (); +fn inc_ref (obj: &Self ) { + obj.wrapper_ref ().refcount ().inc (); }unsafe fn dec_ref (obj: core::ptr::NonNull <Self >) {
rust/kernel/cred.rs +2 -2
@@ -78,9 +78,9 @@ pub fn euid(&self) -> Kuid {// SAFETY: The type invariants guarantee that `Credential` is always ref-counted. unsafe impl AlwaysRefCounted for Credential {#[inline] -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. -unsafe { bindings::get_cred (self.0 .get ()) }; +unsafe { bindings::get_cred (obj.0 .get ()) }; }#[inline]
rust/kernel/device.rs +2 -2
@@ -449,9 +449,9 @@ pub fn name(&self) -> &CStr {// SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. -unsafe { bindings::get_device (self.as_raw ()) }; +unsafe { bindings::get_device (obj.as_raw ()) }; }unsafe fn dec_ref (obj: ptr::NonNull <Self >) {
rust/kernel/device/property.rs +2 -2
@@ -361,10 +361,10 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {// SAFETY: Instances of `FwNode` are always reference-counted. unsafe impl crate ::sync::aref::AlwaysRefCounted for FwNode { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference guarantees that the // refcount is non-zero. -unsafe { bindings::fwnode_handle_get (self.as_raw ()) }; +unsafe { bindings::fwnode_handle_get (obj.as_raw ()) }; }unsafe fn dec_ref (obj: ptr::NonNull <Self >) {
rust/kernel/drm/device.rs +2 -2
@@ -363,9 +363,9 @@ fn deref(&self) -> &Self::Target {// SAFETY: DRM device objects are always reference counted and the get/put functions // satisfy the requirements. unsafe impl <T : drm::Driver ,C :DeviceContext >AlwaysRefCounted for Device <T ,C > { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. -unsafe { bindings::drm_dev_get (self.as_raw ()) }; +unsafe { bindings::drm_dev_get (obj.as_raw ()) }; }unsafe fn dec_ref (obj:NonNull <Self >) {
rust/kernel/drm/gem/mod.rs +2 -2
@@ -52,10 +52,10 @@ unsafe impl $( <$( $tparam_id ),+> )? $crate::sync::aref::AlwaysRefCounted for $ Self: IntoGEMObject, $( $( $bind_param : $bind_trait ),+ )? { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference guarantees that the refcount is // non-zero. -unsafe { bindings::drm_gem_object_get (self.as_raw ()) }; +unsafe { bindings::drm_gem_object_get (obj.as_raw ()) }; }unsafe fn dec_ref (obj: core::ptr::NonNull <Self >) {
rust/kernel/drm/gpuvm/mod.rs +2 -2
@@ -80,9 +80,9 @@ unsafe impl<T: DriverGpuVm> Sync for GpuVm<T> {}// SAFETY: By type invariants, the allocation is managed by the refcount in `self.vm`. unsafe impl <T :DriverGpuVm >AlwaysRefCounted for GpuVm <T > { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: By type invariants, the allocation is managed by the refcount in `self.vm`. -unsafe { bindings::drm_gpuvm_get (self.vm.get ()) }; +unsafe { bindings::drm_gpuvm_get (obj.vm.get ()) }; }unsafe fn dec_ref (obj:NonNull <Self >) {
rust/kernel/drm/gpuvm/vm_bo.rs +2 -2
@@ -21,9 +21,9 @@ pub struct GpuVmBo<T: DriverGpuVm> {// SAFETY: By type invariants, the allocation is managed by the refcount in `self.inner`. unsafe impl <T :DriverGpuVm >AlwaysRefCounted for GpuVmBo <T > { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: By type invariants, the allocation is managed by the refcount in `self.inner`. -unsafe { bindings::drm_gpuvm_bo_get (self.inner.get ()) }; +unsafe { bindings::drm_gpuvm_bo_get (obj.inner.get ()) }; }unsafe fn dec_ref (obj:NonNull <Self >) {
rust/kernel/fs/file.rs +4 -4
@@ -199,9 +199,9 @@ unsafe impl Sync for File {}// makes `ARef<File>` own a normal refcount. unsafe impl AlwaysRefCounted for File {#[inline] -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. -unsafe { bindings::get_file (self.as_ptr ()) }; +unsafe { bindings::get_file (obj.as_ptr ()) }; }#[inline]
@@ -235,9 +235,9 @@ pub struct LocalFile {// makes `ARef<LocalFile>` own a normal refcount. unsafe impl AlwaysRefCounted for LocalFile {#[inline] -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. -unsafe { bindings::get_file (self.as_ptr ()) }; +unsafe { bindings::get_file (obj.as_ptr ()) }; }#[inline]
rust/kernel/i2c.rs +4 -4
@@ -425,9 +425,9 @@ pub fn get(index: i32) -> Result<ARef<Self>> {// SAFETY: Instances of `I2cAdapter` are always reference-counted. unsafe impl AlwaysRefCounted for I2cAdapter { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. -unsafe { bindings::i2c_get_adapter (self.index ()) }; +unsafe { bindings::i2c_get_adapter (obj.index ()) }; }unsafe fn dec_ref (obj:NonNull <Self >) {
@@ -501,9 +501,9 @@ unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for I2cClient<C// SAFETY: Instances of `I2cClient` are always reference-counted. unsafe impl AlwaysRefCounted for I2cClient { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. -unsafe { bindings::get_device (self.as_ref ().as_raw ()) }; +unsafe { bindings::get_device (obj.as_ref ().as_raw ()) }; }unsafe fn dec_ref (obj:NonNull <Self >) {
rust/kernel/mm.rs +4 -4
@@ -57,9 +57,9 @@ unsafe impl Sync for Mm {}// SAFETY: By the type invariants, this type is always refcounted. unsafe impl AlwaysRefCounted for Mm {#[inline] -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The pointer is valid since self is a reference. -unsafe { bindings::mmgrab (self.as_raw ()) }; +unsafe { bindings::mmgrab (obj.as_raw ()) }; }#[inline]
@@ -93,9 +93,9 @@ unsafe impl Sync for MmWithUser {}// SAFETY: By the type invariants, this type is always refcounted. unsafe impl AlwaysRefCounted for MmWithUser {#[inline] -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The pointer is valid since self is a reference. -unsafe { bindings::mmget (self.as_raw ()) }; +unsafe { bindings::mmget (obj.as_raw ()) }; }#[inline]
rust/kernel/mm/mmput_async.rs +2 -2
@@ -36,9 +36,9 @@ unsafe impl Sync for MmWithUserAsync {}// SAFETY: By the type invariants, this type is always refcounted. unsafe impl AlwaysRefCounted for MmWithUserAsync {#[inline] -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The pointer is valid since self is a reference. -unsafe { bindings::mmget (self.as_raw ()) }; +unsafe { bindings::mmget (obj.as_raw ()) }; }#[inline]
rust/kernel/opp.rs +2 -2
@@ -1043,9 +1043,9 @@ unsafe impl Sync for OPP {}/// SAFETY: The type invariants guarantee that [`OPP`] is always refcounted. unsafe impl AlwaysRefCounted for OPP {#[inline] -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. -unsafe { bindings::dev_pm_opp_get (self.0 .get ()) }; +unsafe { bindings::dev_pm_opp_get (obj.0 .get ()) }; }#[inline]
rust/kernel/pci.rs +2 -2
@@ -482,9 +482,9 @@ impl<'a> crate::dma::Device<'a> for Device<device::Core<'a>> {}// SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. -unsafe { bindings::pci_dev_get (self.as_raw ()) }; +unsafe { bindings::pci_dev_get (obj.as_raw ()) }; }unsafe fn dec_ref (obj:NonNull <Self >) {
rust/kernel/pid_namespace.rs +2 -2
@@ -43,9 +43,9 @@ pub unsafe fn from_ptr<'a>(ptr: *const bindings::pid_namespace) -> &'a Self {// SAFETY: Instances of `PidNamespace` are always reference-counted. unsafe impl AlwaysRefCounted for PidNamespace {#[inline] -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. -unsafe { bindings::get_pid_ns (self.as_ptr ()) }; +unsafe { bindings::get_pid_ns (obj.as_ptr ()) }; }#[inline]
rust/kernel/platform.rs +2 -2
@@ -519,9 +519,9 @@ impl<'a> crate::dma::Device<'a> for Device<device::Core<'a>> {}// SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. -unsafe { bindings::get_device (self.as_ref ().as_raw ()) }; +unsafe { bindings::get_device (obj.as_ref ().as_raw ()) }; }unsafe fn dec_ref (obj:NonNull <Self >) {
rust/kernel/pwm.rs +2 -2
@@ -631,10 +631,10 @@ pub fn new<'a>(// SAFETY: Implements refcounting for `Chip` using the embedded `struct device`. unsafe impl <T :PwmOps >AlwaysRefCounted for Chip <T > {#[inline] -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: `self.0.get()` points to a valid `pwm_chip` because `self` exists. // The embedded `dev` is valid. `get_device` increments its refcount. -unsafe { bindings::get_device (&raw mut (*self.0 .get ()).dev) }; +unsafe { bindings::get_device (&raw mut (*obj.0 .get ()).dev) }; }#[inline]
rust/kernel/sync/aref.rs +7 -4
@@ -44,7 +44,10 @@/// alive.) pub unsafe trait AlwaysRefCounted {/// Increments the reference count on the object. -fn inc_ref (&self); +/// +/// This function should not be called accidentally; a type might declare their own `inc_ref` +/// function and it shouldn't be confused with this one. +fn inc_ref (obj: &Self );/// Decrements the reference count on the object. ///
@@ -126,7 +129,7 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {/// /// # // SAFETY: TODO. /// unsafe impl AlwaysRefCounted for Empty { -/// fn inc_ref(&self) {} +/// fn inc_ref(obj: &Self) {} /// unsafe fn dec_ref(_obj: NonNull<Self>) {} /// } ///
@@ -145,7 +148,7 @@ pub fn into_raw(me: Self) -> NonNull<T> {impl <T :AlwaysRefCounted >Clone for ARef <T > {fn clone (&self) ->Self { - self.inc_ref (); +T ::inc_ref (self);// SAFETY: We just incremented the refcount above. unsafe {Self ::from_raw (self.ptr) } }
@@ -162,7 +165,7 @@ fn deref(&self) -> &Self::Target {impl <T :AlwaysRefCounted >From <&T >for ARef <T > {fn from (b: &T ) ->Self { - b.inc_ref (); +T ::inc_ref (b);// SAFETY: We just incremented the refcount above. unsafe {Self ::from_raw (NonNull ::from (b)) } }
rust/kernel/task.rs +2 -2
@@ -349,9 +349,9 @@ pub fn group_leader(&self) -> &Task {// SAFETY: The type invariants guarantee that `Task` is always refcounted. unsafe impl crate ::sync::aref::AlwaysRefCounted for Task {#[inline] -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. -unsafe { bindings::get_task_struct (self.as_ptr ()) }; +unsafe { bindings::get_task_struct (obj.as_ptr ()) }; }#[inline]
rust/kernel/usb.rs +4 -4
@@ -393,11 +393,11 @@ fn as_ref(&self) -> &Device {// SAFETY: Instances of `Interface` are always reference-counted. unsafe impl AlwaysRefCounted for Interface { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The invariants of `Interface` guarantee that `self.as_raw()` // returns a valid `struct usb_interface` pointer, for which we will // acquire a new refcount. -unsafe { bindings::usb_get_intf (self.as_raw ()) }; +unsafe { bindings::usb_get_intf (obj.as_raw ()) }; }unsafe fn dec_ref (obj:NonNull <Self >) {
@@ -444,11 +444,11 @@ fn as_raw(&self) -> *mut bindings::usb_device {// SAFETY: Instances of `Device` are always reference-counted. unsafe impl AlwaysRefCounted for Device { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The invariants of `Device` guarantee that `self.as_raw()` // returns a valid `struct usb_device` pointer, for which we will // acquire a new refcount. -unsafe { bindings::usb_get_dev (self.as_raw ()) }; +unsafe { bindings::usb_get_dev (obj.as_raw ()) }; }unsafe fn dec_ref (obj:NonNull <Self >) {
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] I2cAdapter::inc_ref calls i2c_get_adapter() which sleeps and can fail, leading to sleep-in-atomic panics and Use-After-Free. --
commit dcdb7e5879ef4f6a18646d7f509abd59ae6f10db
Author: Trevor Chan <[email protected]>
rust: aref: make AlwaysRefCounted::inc_ref an associated function
This commit changes the AlwaysRefCounted::inc_ref trait method to be an associated function rather than taking a self reference. This prevents accidental calls to inc_ref on types that implement it, and updates all implementors across various kernel subsystems to match the new signature.
[ ... ]
rust/kernel/i2c.rs pub fn get(index: i32) -> Result<ARef<Self>> { · line 430
Quoted 13 lines
rust/kernel/i2c.rs +2
-2@@ -425,9 +425,9 @@ pub fn get(index: i32) -> Result<ARef<Self>> {// SAFETY: Instances of `I2cAdapter` are always reference-counted. unsafe impl AlwaysRefCounted for I2cAdapter { -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. -unsafe { bindings::i2c_get_adapter (self.index ()) }; +unsafe { bindings::i2c_get_adapter (obj.index ()) };
[Severity: High]
This isn't a bug introduced by this patch, but is it safe for I2cAdapter::inc_ref to call i2c_get_adapter here?
Since i2c_get_adapter acquires a mutex (core_lock), won't cloning an ARef<I2cAdapter> in an atomic context (such as under a spinlock) trigger a sleep-in-atomic panic?
Additionally, does this code handle the case where i2c_get_adapter fails? If the adapter has been unregistered (e.g., the device is hot-unplugged or the driver unbound) while the ARef is still held, the IDR lookup fails and i2c_get_adapter returns NULL.
Because this return value is ignored in inc_ref, the underlying device and module refcounts wouldn't actually be incremented. When the cloned ARef is subsequently dropped, dec_ref will unconditionally decrement the refcounts, which could underflow and cause a use-after-free while other ARef instances still point to it.
Quoted 3 lines
rust/kernel/i2c.rs +0
-0}unsafe fn dec_ref (obj:NonNull <Self >) {
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 6 lines · Trevor Chan
rust/kernel/opp.rs +0
-0@@ -1043,9 +1043,9 @@ unsafe impl Sync for OPP {}/// SAFETY: The type invariants guarantee that [`OPP`] is always refcounted.
rust/kernel/opp.rs unsafe impl Sync for OPP {} · line 1051
Quoted 10 lines
rust/kernel/opp.rs +2
-2unsafe impl AlwaysRefCounted for OPP {#[inline] -fn inc_ref (&self) { +fn inc_ref (obj: &Self ) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. -unsafe { bindings::dev_pm_opp_get (self.0 .get ()) }; +unsafe { bindings::dev_pm_opp_get (obj.0 .get ()) }; }#[inline]
For OPP:
Signature · 1 line
viresh
Quoted 10 lines · Trevor Chan
AlwaysRefCounted::inc_refis a function that shouldn't be called lightly.To prevent accidentally calling it, change
inc_refto be an associated function.Modify all
AlwaysRefCountedimplementors to work with this change.Suggested-by Benno Lossin <[email protected]>Reviewed-by Alice Ryhl <[email protected]>Signed-off-by Trevor Chan <[email protected]>
Best regards,
Andreas Hindborg