Skip to content

Commit ef406dd

Browse files
author
Oliver Mangold
committed
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 <oliver.mangold@pm.me>
1 parent 73a75b5 commit ef406dd

File tree

7 files changed

+64
-31
lines changed

7 files changed

+64
-31
lines changed

rust/kernel/block/mq/request.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
block::mq::Operations,
1010
error::Result,
1111
sync::Refcount,
12-
types::{ARef, AlwaysRefCounted, Opaque},
12+
types::{ARef, AlwaysRefCounted, Opaque, RefCounted},
1313
};
1414
use core::{
1515
marker::PhantomData,
@@ -209,10 +209,10 @@ unsafe impl<T: Operations> Send for Request<T> {}
209209
unsafe impl<T: Operations> Sync for Request<T> {}
210210

211211
// SAFETY: All instances of `Request<T>` are reference counted. This
212-
// implementation of `AlwaysRefCounted` ensure that increments to the ref count
212+
// implementation of `RefCounted` ensure that increments to the ref count
213213
// keeps the object alive in memory at least until a matching reference count
214214
// decrement is executed.
215-
unsafe impl<T: Operations> AlwaysRefCounted for Request<T> {
215+
unsafe impl<T: Operations> RefCounted for Request<T> {
216216
fn inc_ref(&self) {
217217
self.wrapper_ref().refcount().inc();
218218
}
@@ -234,3 +234,7 @@ unsafe impl<T: Operations> AlwaysRefCounted for Request<T> {
234234
}
235235
}
236236
}
237+
238+
// SAFETY: we currently do not implement `Ownable`, thus it is okay to can obtain an `ARef<Request>`
239+
// from a `&Request` (but this will change in the future).
240+
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: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,9 @@ impl<T> Opaque<T> {
402402
}
403403
}
404404

405-
/// Types that are _always_ reference counted.
405+
/// Types that are internally reference counted.
406406
///
407407
/// It allows such types to define their own custom ref increment and decrement functions.
408-
/// Additionally, it allows users to convert from a shared reference `&T` to an owned reference
409-
/// [`ARef<T>`].
410408
///
411409
/// This is usually implemented by wrappers to existing structures on the C side of the code. For
412410
/// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted
@@ -418,9 +416,9 @@ impl<T> Opaque<T> {
418416
/// at least until matching decrements are performed.
419417
///
420418
/// Implementers must also ensure that all instances are reference-counted. (Otherwise they
421-
/// won't be able to honour the requirement that [`AlwaysRefCounted::inc_ref`] keep the object
419+
/// won't be able to honour the requirement that [`RefCounted::inc_ref`] keep the object
422420
/// alive.)
423-
pub unsafe trait AlwaysRefCounted {
421+
pub unsafe trait RefCounted {
424422
/// Increments the reference count on the object.
425423
fn inc_ref(&self);
426424

@@ -433,11 +431,22 @@ pub unsafe trait AlwaysRefCounted {
433431
/// Callers must ensure that there was a previous matching increment to the reference count,
434432
/// and that the object is no longer used after its reference count is decremented (as it may
435433
/// result in the object being freed), unless the caller owns another increment on the refcount
436-
/// (e.g., it calls [`AlwaysRefCounted::inc_ref`] twice, then calls
437-
/// [`AlwaysRefCounted::dec_ref`] once).
434+
/// (e.g., it calls [`RefCounted::inc_ref`] twice, then calls
435+
/// [`RefCounted::dec_ref`] once).
438436
unsafe fn dec_ref(obj: NonNull<Self>);
439437
}
440438

439+
/// An extension to RefCounted, which declares that it is allowed to convert
440+
/// from a shared reference `&T` to an owned reference [`ARef<T>`].
441+
///
442+
/// # Safety
443+
///
444+
/// Implementers must ensure that no safety invariants are violated by upgrading an `&T`
445+
/// to an [`ARef<T>`]. In particular that implies [`AlwaysRefCounted`] and [`Ownable`]
446+
/// cannot be implemented for the same type, as this would allow to violate the uniqueness
447+
/// guarantee of [`Owned<T>`] by derefencing it into an `&T` and obtaining an [`ARef`] from that.
448+
pub unsafe trait AlwaysRefCounted: RefCounted {}
449+
441450
/// An owned reference to an always-reference-counted object.
442451
///
443452
/// The object's reference count is automatically decremented when an instance of [`ARef`] is
@@ -448,7 +457,7 @@ pub unsafe trait AlwaysRefCounted {
448457
///
449458
/// The pointer stored in `ptr` is non-null and valid for the lifetime of the [`ARef`] instance. In
450459
/// particular, the [`ARef`] instance owns an increment on the underlying object's reference count.
451-
pub struct ARef<T: AlwaysRefCounted> {
460+
pub struct ARef<T: RefCounted> {
452461
ptr: NonNull<T>,
453462
_p: PhantomData<T>,
454463
}
@@ -457,16 +466,16 @@ pub struct ARef<T: AlwaysRefCounted> {
457466
// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
458467
// `T` to be `Send` because any thread that has an `ARef<T>` may ultimately access `T` using a
459468
// mutable reference, for example, when the reference count reaches zero and `T` is dropped.
460-
unsafe impl<T: AlwaysRefCounted + Sync + Send> Send for ARef<T> {}
469+
unsafe impl<T: RefCounted + Sync + Send> Send for ARef<T> {}
461470

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

469-
impl<T: AlwaysRefCounted> ARef<T> {
478+
impl<T: RefCounted> ARef<T> {
470479
/// Creates a new instance of [`ARef`].
471480
///
472481
/// It takes over an increment of the reference count on the underlying object.
@@ -495,12 +504,12 @@ impl<T: AlwaysRefCounted> ARef<T> {
495504
///
496505
/// ```
497506
/// use core::ptr::NonNull;
498-
/// use kernel::types::{ARef, AlwaysRefCounted};
507+
/// use kernel::types::{ARef, RefCounted};
499508
///
500509
/// struct Empty {}
501510
///
502511
/// # // SAFETY: TODO.
503-
/// unsafe impl AlwaysRefCounted for Empty {
512+
/// unsafe impl RefCounted for Empty {
504513
/// fn inc_ref(&self) {}
505514
/// unsafe fn dec_ref(_obj: NonNull<Self>) {}
506515
/// }
@@ -518,15 +527,15 @@ impl<T: AlwaysRefCounted> ARef<T> {
518527
}
519528
}
520529

521-
impl<T: AlwaysRefCounted> Clone for ARef<T> {
530+
impl<T: RefCounted> Clone for ARef<T> {
522531
fn clone(&self) -> Self {
523532
self.inc_ref();
524533
// SAFETY: We just incremented the refcount above.
525534
unsafe { Self::from_raw(self.ptr) }
526535
}
527536
}
528537

529-
impl<T: AlwaysRefCounted> Deref for ARef<T> {
538+
impl<T: RefCounted> Deref for ARef<T> {
530539
type Target = T;
531540

532541
fn deref(&self) -> &Self::Target {
@@ -543,10 +552,10 @@ impl<T: AlwaysRefCounted> From<&T> for ARef<T> {
543552
}
544553
}
545554

546-
impl<T: AlwaysRefCounted> Drop for ARef<T> {
555+
impl<T: RefCounted> Drop for ARef<T> {
547556
fn drop(&mut self) {
548-
// SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to
549-
// decrement.
557+
// SAFETY: The type invariants guarantee that the `ARef` owns the reference
558+
// we're about to decrement.
550559
unsafe { T::dec_ref(self.ptr) };
551560
}
552561
}

0 commit comments

Comments
 (0)