rust: aref: make `AlwaysRefCounted::inc_ref` an associated function
Subsystems (13)
PATCH v2 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, inc_ref is now changed to be an associated function.
Modifies all AlwaysRefCounted implementors to work with this change.
---
Changes in v2:
- Don't word wrap the patch
--- 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/fs/file.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/sync/aref.rs | 10 ++++++---- rust/kernel/task.rs | 4 ++-- 16 files changed, 40 insertions(+), 38 deletions(-)
rust/kernel/auxiliary.rs +2 -2
@@ -246,9 +246,9 @@ extern "C" fn release(dev: *mut bindings::device) {// SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate ::types::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
@@ -236,8 +236,8 @@ fn atomic_relaxed_op_unless(target: &AtomicU64, op: impl Fn(u64) -> u64, pred: u// 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) { -let refcount = &self.wrapper_ref ().refcount (); +fn inc_ref (obj: &Self ) { +let refcount = &obj.wrapper_ref ().refcount ();#[cfg_attr(not( CONFIG_DEBUG_MISC ), allow(unused_variables))] let updated =atomic_relaxed_op_unless (refcount, |x| x +1 ,0 );
rust/kernel/cred.rs +2 -2
@@ -76,9 +76,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
@@ -293,9 +293,9 @@ pub fn fwnode(&self) -> Option<&property::FwNode> {// SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate ::types::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
@@ -359,10 +359,10 @@ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {// SAFETY: Instances of `FwNode` are always reference-counted. unsafe impl crate ::types::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
@@ -183,9 +183,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 >AlwaysRefCounted for Device <T > { -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
@@ -56,9 +56,9 @@ pub trait IntoGEMObject: Sized + super::private::Sealed + AlwaysRefCounted {// SAFETY: All gem objects are refcounted. unsafe impl <T :IntoGEMObject >AlwaysRefCounted for T { -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:NonNull <Self >) {
rust/kernel/fs/file.rs +4 -4
@@ -192,9 +192,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]
@@ -228,9 +228,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/mm.rs +4 -4
@@ -56,9 +56,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]
@@ -92,9 +92,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 { -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 ()) }; }unsafe fn dec_ref (obj: ptr::NonNull <Self >) {
rust/kernel/pci.rs +2 -2
@@ -456,9 +456,9 @@ impl crate::dma::Device for Device<device::Core> {}// SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate ::types::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
@@ -46,9 +46,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
@@ -293,9 +293,9 @@ impl crate::dma::Device for Device<device::Core> {}// SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate ::types::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/sync/aref.rs +6 -4
@@ -24,7 +24,9 @@/// alive.) pub unsafe trait AlwaysRefCounted {/// Increments the reference count on the object. -fn inc_ref (&self); +/// +/// This function should not be called lightly. +fn inc_ref (obj: &Self );/// Decrements the reference count on the object. ///
@@ -103,7 +105,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>) {} /// } ///
@@ -122,7 +124,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) } }
@@ -139,7 +141,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 active_pid_ns(&self) -> Option<&PidNamespace> {// SAFETY: The type invariants guarantee that `Task` is always refcounted. unsafe impl crate ::types::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]
Signature · 1 line
2.39.5
Quoted 6 lines · Trevor Chan
rust/kernel/opp.rs +0
-0@@ -1043,9 +1043,9 @@ unsafe impl Sync for OPP {}
rust/kernel/opp.rs unsafe impl Sync for OPP {} · line 1051
Quoted 10 lines
rust/kernel/opp.rs +2
-2/// SAFETY: The type invariants guarantee that [`OPP`] is always refcounted. unsafe impl AlwaysRefCounted for OPP { -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 ()) }; }unsafe fn dec_ref (obj: ptr::NonNull <Self >) {
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,
inc_refis now changed to be an associated function.Modifies all
AlwaysRefCountedimplementors to work with this change.Suggested-by Benno Lossin <[email protected]>Signed-off-by Trevor Chan <[email protected]>
One nit below, but:
Quoted 16 lines
rust/kernel/sync/aref.rs +3
-1@@ -24,7 +24,9 @@/// alive.) pub unsafe trait AlwaysRefCounted {/// Increments the reference count on the object. -fn inc_ref (&self); +/// +/// This function should not be called lightly. +fn inc_ref (obj: &Self );/// Decrements the reference count on the object. ///
rust/kernel/sync/aref.rs pub unsafe fn from_raw(ptr: NonNull<T>) -> Self { · line 109
Quoted 6 lines
rust/kernel/sync/aref.rs +1
-1@@ -103,7 +105,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>) {}
We probably want _obj for inc_ref like we have for dec_ref.
Alice