Skip to content

Commit a1d0c17

Browse files
Oliver Mangoldintel-lab-lkp
Oliver Mangold
authored andcommitted
rust: Rename AlwaysRefCounted to RefCounted
AlwaysRefCounted will become a marker trait to indicate that it is allowed to obtain an ARef from a `&`, which cannot be allowed for types which are also Ownable. Signed-off-by: Oliver Mangold <[email protected]> Suggested-by: Alice Ryhl <[email protected]>
1 parent cac6062 commit a1d0c17

File tree

7 files changed

+61
-30
lines changed

7 files changed

+61
-30
lines changed

rust/kernel/block/mq/request.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
bindings,
99
block::mq::Operations,
1010
error::Result,
11-
types::{ARef, AlwaysRefCounted, Opaque},
11+
types::{ARef, AlwaysRefCounted, Opaque, RefCounted},
1212
};
1313
use core::{
1414
marker::PhantomData,
@@ -227,10 +227,10 @@ fn atomic_relaxed_op_unless(target: &AtomicU64, op: impl Fn(u64) -> u64, pred: u
227227
}
228228

229229
// SAFETY: All instances of `Request<T>` are reference counted. This
230-
// implementation of `AlwaysRefCounted` ensure that increments to the ref count
230+
// implementation of `RefCounted` ensure that increments to the ref count
231231
// keeps the object alive in memory at least until a matching reference count
232232
// decrement is executed.
233-
unsafe impl<T: Operations> AlwaysRefCounted for Request<T> {
233+
unsafe impl<T: Operations> RefCounted for Request<T> {
234234
fn inc_ref(&self) {
235235
let refcount = &self.wrapper_ref().refcount();
236236

@@ -260,3 +260,7 @@ unsafe impl<T: Operations> AlwaysRefCounted for Request<T> {
260260
}
261261
}
262262
}
263+
264+
// SAFETY: We currently do not implement `Ownable`, thus it is okay to can obtain an `ARef<Request>`
265+
// from a `&Request` (but this will change in the future).
266+
unsafe impl<T: Operations> AlwaysRefCounted for Request<T> {}

rust/kernel/cred.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use crate::{
1212
bindings,
1313
task::Kuid,
14-
types::{AlwaysRefCounted, Opaque},
14+
types::{AlwaysRefCounted, Opaque, RefCounted},
1515
};
1616

1717
/// Wraps the kernel's `struct cred`.
@@ -71,7 +71,7 @@ impl Credential {
7171
}
7272

7373
// SAFETY: The type invariants guarantee that `Credential` is always ref-counted.
74-
unsafe impl AlwaysRefCounted for Credential {
74+
unsafe impl RefCounted for Credential {
7575
fn inc_ref(&self) {
7676
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
7777
unsafe { bindings::get_cred(self.0.get()) };
@@ -83,3 +83,7 @@ unsafe impl AlwaysRefCounted for Credential {
8383
unsafe { bindings::put_cred(obj.cast().as_ptr()) };
8484
}
8585
}
86+
87+
// SAFETY: We do not implement `Ownable`, thus it is okay to can obtain an `ARef<Credential>` from a
88+
// `&Credential`.
89+
unsafe impl AlwaysRefCounted for Credential {}

rust/kernel/device.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use crate::{
88
bindings,
99
str::CStr,
10-
types::{ARef, Opaque},
10+
types::{ARef, AlwaysRefCounted, Opaque, RefCounted},
1111
};
1212
use core::{fmt, ptr};
1313

@@ -190,7 +190,7 @@ impl Device {
190190
}
191191

192192
// SAFETY: Instances of `Device` are always reference-counted.
193-
unsafe impl crate::types::AlwaysRefCounted for Device {
193+
unsafe impl RefCounted for Device {
194194
fn inc_ref(&self) {
195195
// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
196196
unsafe { bindings::get_device(self.as_raw()) };
@@ -202,6 +202,10 @@ unsafe impl crate::types::AlwaysRefCounted for Device {
202202
}
203203
}
204204

205+
// SAFETY: We do not implement `Ownable`, thus it is okay to can obtain an `Device<Task>` from a
206+
// `&Device`.
207+
unsafe impl AlwaysRefCounted for Device {}
208+
205209
// SAFETY: As by the type invariant `Device` can be sent to any thread.
206210
unsafe impl Send for Device {}
207211

rust/kernel/fs/file.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
bindings,
1212
cred::Credential,
1313
error::{code::*, Error, Result},
14-
types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque},
14+
types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque, RefCounted},
1515
};
1616
use core::ptr;
1717

@@ -190,7 +190,7 @@ unsafe impl Sync for File {}
190190

191191
// SAFETY: The type invariants guarantee that `File` is always ref-counted. This implementation
192192
// makes `ARef<File>` own a normal refcount.
193-
unsafe impl AlwaysRefCounted for File {
193+
unsafe impl RefCounted for File {
194194
#[inline]
195195
fn inc_ref(&self) {
196196
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
@@ -205,6 +205,10 @@ unsafe impl AlwaysRefCounted for File {
205205
}
206206
}
207207

208+
// SAFETY: We do not implement `Ownable`, thus it is okay to can obtain an `ARef<File>` from a
209+
/// `&File`.
210+
unsafe impl AlwaysRefCounted for File {}
211+
208212
/// Wraps the kernel's `struct file`. Not thread safe.
209213
///
210214
/// This type represents a file that is not known to be safe to transfer across thread boundaries.
@@ -225,7 +229,7 @@ pub struct LocalFile {
225229

226230
// SAFETY: The type invariants guarantee that `LocalFile` is always ref-counted. This implementation
227231
// makes `ARef<File>` own a normal refcount.
228-
unsafe impl AlwaysRefCounted for LocalFile {
232+
unsafe impl RefCounted for LocalFile {
229233
#[inline]
230234
fn inc_ref(&self) {
231235
// SAFETY: The existence of a shared reference means that the refcount is nonzero.

rust/kernel/pid_namespace.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
use crate::{
1111
bindings,
12-
types::{AlwaysRefCounted, Opaque},
12+
types::{AlwaysRefCounted, RefCounted, Opaque},
1313
};
1414
use core::ptr;
1515

@@ -44,7 +44,7 @@ impl PidNamespace {
4444
}
4545

4646
// SAFETY: Instances of `PidNamespace` are always reference-counted.
47-
unsafe impl AlwaysRefCounted for PidNamespace {
47+
unsafe impl RefCounted for PidNamespace {
4848
#[inline]
4949
fn inc_ref(&self) {
5050
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
@@ -58,6 +58,10 @@ unsafe impl AlwaysRefCounted for PidNamespace {
5858
}
5959
}
6060

61+
// SAFETY: We do not implement `Ownable`, thus it is okay to can obtain an `ARef<PidNamespace>`
62+
// from a `&PidNamespace`.
63+
unsafe impl AlwaysRefCounted for PidNamespace {}
64+
6165
// SAFETY:
6266
// - `PidNamespace::dec_ref` can be called from any thread.
6367
// - It is okay to send ownership of `PidNamespace` across thread boundaries.

rust/kernel/task.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ impl Task {
327327
}
328328

329329
// SAFETY: The type invariants guarantee that `Task` is always refcounted.
330-
unsafe impl crate::types::AlwaysRefCounted for Task {
330+
unsafe impl crate::types::RefCounted for Task {
331331
fn inc_ref(&self) {
332332
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
333333
unsafe { bindings::get_task_struct(self.as_ptr()) };
@@ -339,6 +339,10 @@ unsafe impl crate::types::AlwaysRefCounted for Task {
339339
}
340340
}
341341

342+
// SAFETY: We do not implement `Ownable`, thus it is okay to can obtain an `ARef<Task>` from a
343+
// `&Task`.
344+
unsafe impl crate::types::AlwaysRefCounted for Task {}
345+
342346
impl Kuid {
343347
/// Get the current euid.
344348
#[inline]

rust/kernel/types.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,9 @@ impl<T> Opaque<T> {
388388
}
389389
}
390390

391-
/// Types that are _always_ reference counted.
391+
/// Types that are internally reference counted.
392392
///
393393
/// It allows such types to define their own custom ref increment and decrement functions.
394-
/// Additionally, it allows users to convert from a shared reference `&T` to an owned reference
395-
/// [`ARef<T>`].
396394
///
397395
/// This is usually implemented by wrappers to existing structures on the C side of the code. For
398396
/// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted
@@ -404,9 +402,8 @@ impl<T> Opaque<T> {
404402
/// at least until matching decrements are performed.
405403
///
406404
/// Implementers must also ensure that all instances are reference-counted. (Otherwise they
407-
/// won't be able to honour the requirement that [`AlwaysRefCounted::inc_ref`] keep the object
408-
/// alive.)
409-
pub unsafe trait AlwaysRefCounted {
405+
/// won't be able to honour the requirement that [`RefCounted::inc_ref`] keep the object alive.)
406+
pub unsafe trait RefCounted {
410407
/// Increments the reference count on the object.
411408
fn inc_ref(&self);
412409

@@ -419,11 +416,21 @@ pub unsafe trait AlwaysRefCounted {
419416
/// Callers must ensure that there was a previous matching increment to the reference count,
420417
/// and that the object is no longer used after its reference count is decremented (as it may
421418
/// result in the object being freed), unless the caller owns another increment on the refcount
422-
/// (e.g., it calls [`AlwaysRefCounted::inc_ref`] twice, then calls
423-
/// [`AlwaysRefCounted::dec_ref`] once).
419+
/// (e.g., it calls [`RefCounted::inc_ref`] twice, then calls [`RefCounted::dec_ref`] once).
424420
unsafe fn dec_ref(obj: NonNull<Self>);
425421
}
426422

423+
/// An extension to RefCounted, which declares that it is allowed to convert
424+
/// from a shared reference `&T` to an owned reference [`ARef<T>`].
425+
///
426+
/// # Safety
427+
///
428+
/// Implementers must ensure that no safety invariants are violated by upgrading an `&T`
429+
/// to an [`ARef<T>`]. In particular that implies [`AlwaysRefCounted`] and [`Ownable`]
430+
/// cannot be implemented for the same type, as this would allow to violate the uniqueness
431+
/// guarantee of [`Owned<T>`] by derefencing it into an `&T` and obtaining an [`ARef`] from that.
432+
pub unsafe trait AlwaysRefCounted: RefCounted {}
433+
427434
/// An owned reference to an always-reference-counted object.
428435
///
429436
/// The object's reference count is automatically decremented when an instance of [`ARef`] is
@@ -434,7 +441,7 @@ pub unsafe trait AlwaysRefCounted {
434441
///
435442
/// The pointer stored in `ptr` is non-null and valid for the lifetime of the [`ARef`] instance. In
436443
/// particular, the [`ARef`] instance owns an increment on the underlying object's reference count.
437-
pub struct ARef<T: AlwaysRefCounted> {
444+
pub struct ARef<T: RefCounted> {
438445
ptr: NonNull<T>,
439446
_p: PhantomData<T>,
440447
}
@@ -443,16 +450,16 @@ pub struct ARef<T: AlwaysRefCounted> {
443450
// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
444451
// `T` to be `Send` because any thread that has an `ARef<T>` may ultimately access `T` using a
445452
// mutable reference, for example, when the reference count reaches zero and `T` is dropped.
446-
unsafe impl<T: AlwaysRefCounted + Sync + Send> Send for ARef<T> {}
453+
unsafe impl<T: RefCounted + Sync + Send> Send for ARef<T> {}
447454

448455
// SAFETY: It is safe to send `&ARef<T>` to another thread when the underlying `T` is `Sync`
449456
// because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
450457
// it needs `T` to be `Send` because any thread that has a `&ARef<T>` may clone it and get an
451458
// `ARef<T>` on that thread, so the thread may ultimately access `T` using a mutable reference, for
452459
// example, when the reference count reaches zero and `T` is dropped.
453-
unsafe impl<T: AlwaysRefCounted + Sync + Send> Sync for ARef<T> {}
460+
unsafe impl<T: RefCounted + Sync + Send> Sync for ARef<T> {}
454461

455-
impl<T: AlwaysRefCounted> ARef<T> {
462+
impl<T: RefCounted> ARef<T> {
456463
/// Creates a new instance of [`ARef`].
457464
///
458465
/// It takes over an increment of the reference count on the underlying object.
@@ -481,12 +488,12 @@ impl<T: AlwaysRefCounted> ARef<T> {
481488
///
482489
/// ```
483490
/// use core::ptr::NonNull;
484-
/// use kernel::types::{ARef, AlwaysRefCounted};
491+
/// use kernel::types::{ARef, RefCounted};
485492
///
486493
/// struct Empty {}
487494
///
488495
/// # // SAFETY: TODO.
489-
/// unsafe impl AlwaysRefCounted for Empty {
496+
/// unsafe impl RefCounted for Empty {
490497
/// fn inc_ref(&self) {}
491498
/// unsafe fn dec_ref(_obj: NonNull<Self>) {}
492499
/// }
@@ -504,15 +511,15 @@ impl<T: AlwaysRefCounted> ARef<T> {
504511
}
505512
}
506513

507-
impl<T: AlwaysRefCounted> Clone for ARef<T> {
514+
impl<T: RefCounted> Clone for ARef<T> {
508515
fn clone(&self) -> Self {
509516
self.inc_ref();
510517
// SAFETY: We just incremented the refcount above.
511518
unsafe { Self::from_raw(self.ptr) }
512519
}
513520
}
514521

515-
impl<T: AlwaysRefCounted> Deref for ARef<T> {
522+
impl<T: RefCounted> Deref for ARef<T> {
516523
type Target = T;
517524

518525
fn deref(&self) -> &Self::Target {
@@ -529,7 +536,7 @@ impl<T: AlwaysRefCounted> From<&T> for ARef<T> {
529536
}
530537
}
531538

532-
impl<T: AlwaysRefCounted> Drop for ARef<T> {
539+
impl<T: RefCounted> Drop for ARef<T> {
533540
fn drop(&mut self) {
534541
// SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to
535542
// decrement.

0 commit comments

Comments
 (0)