rust: sync: Arc: Implement Debug and Display
cover rust: sync: Arc: Implement Debug and Display
I found that our Arc doesn't implement Debug or Display when I tried to play with them, therefore add these implementation.
With these changes, I could get the following print with the sample code in patch #5:
[..] rust_print: 1
[..] rust_print: UniqueArc { inner: Arc { refcount: 1, data: "hello, world" } }
[..] rust_print: [samples/rust/rust_print.rs:34] c = Arc {
[..] refcount: 2,
[..] data: "hello, world",
[..] }
[..] rust_print: Arc {
[..] refcount: 0x1,
[..] data: "hello, world",
[..] }Note that I make the Debug implementation of Arc also print the current reference count, which I think may be useful: myself sometimes wonder "how many references exist at this point" during my own development. But I'm open to suggestions and changes.
Wedson, I know that you are considering to get rid of ArcBorrow, so the patch #3 may have some conflicts with what you may be working on. I'm happy to wait and rebase since this series is not something urgent ;-)
Suggestions and comments are welcome!
Regards,
Boqun
RFC 1/5 rust: sync: impl Display for {Unique,}Arc
This allows printing the inner data of Arc and its friends if the inner data implements Display. It's useful for logging and debugging purpose.
--- rust/kernel/sync/arc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+)
rust/kernel/sync/arc.rs +13 -0
@@ -22,6 +22,7 @@ use crate::{ };use alloc::boxed::Box ;use core::{ + fmt, marker::{PhantomData ,Unsize }, mem::{ManuallyDrop ,MaybeUninit }, ops::{Deref ,DerefMut },
@@ -522,3 +523,15 @@ impl<T: ?Sized> DerefMut for UniqueArc<T> {unsafe { &mut self.inner.ptr.as_mut ().data } } } + +impl <T : fmt::Display + ?Sized > fmt::Display for UniqueArc <T > { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + fmt::Display ::fmt (self.deref (), f) + } +} + +impl <T : fmt::Display + ?Sized > fmt::Display for Arc <T > { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + fmt::Display ::fmt (self.deref (), f) + } +}
Signature · 1 line
2.39.1
Re: [RFC 1/5] rust: sync: impl Display for {Unique,}Arc
Quoted 5 lines · On Wed, 1 Feb 2023 15:22:40 -0800 Boqun Feng <[email protected]> wrote:
This allows printing the inner data of
Arcand its friends if the inner data implementsDisplay. It's useful for logging and debugging purpose.Signed-off-by Boqun Feng <[email protected]>
Quoted 32 lines
--- rust/kernel/sync/arc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+)rust/kernel/sync/arc.rs +13
-0@@ -22,6 +22,7 @@ use crate::{ };use alloc::boxed::Box ;use core::{ + fmt, marker::{PhantomData ,Unsize }, mem::{ManuallyDrop ,MaybeUninit }, ops::{Deref ,DerefMut },@@ -522,3 +523,15 @@ impl<T: ?Sized> DerefMut for UniqueArc<T> {unsafe { &mut self.inner.ptr.as_mut ().data } } } + +impl <T : fmt::Display + ?Sized > fmt::Display for UniqueArc <T > { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + fmt::Display ::fmt (self.deref (), f) + } +} + +impl <T : fmt::Display + ?Sized > fmt::Display for Arc <T > { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + fmt::Display ::fmt (self.deref (), f) + } +}
Re: [RFC 1/5] rust: sync: impl Display for {Unique,}Arc
In reply to Boqun FengQuoted 5 lines · Boqun Feng
This allows printing the inner data of
Arcand its friends if the inner data implementsDisplay. It's useful for logging and debugging purpose.Signed-off-by Boqun Feng <[email protected]>
Quoted 35 lines
--- rust/kernel/sync/arc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+)rust/kernel/sync/arc.rs +13
-1@@ -22,6 +22,7 @@ use crate::{ };use alloc::boxed::Box ;use core::{ + fmt, marker::{PhantomData ,Unsize }, mem::{ManuallyDrop ,MaybeUninit }, ops::{Deref ,DerefMut },@@ -522,3 +523,15 @@ impl<T: ?Sized> DerefMut for UniqueArc<T> {unsafe { &mut self.inner.ptr.as_mut ().data } } } + +impl <T : fmt::Display + ?Sized > fmt::Display for UniqueArc <T > { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + fmt::Display ::fmt (self.deref (), f) + } +} + +impl <T : fmt::Display + ?Sized > fmt::Display for Arc <T > { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + fmt::Display ::fmt (self.deref (), f) + } +} --2.39.1
Re: [RFC 1/5] rust: sync: impl Display for {Unique,}Arc
In reply to Boqun FengQuoted 5 lines · Boqun Feng
This allows printing the inner data of
Arcand its friends if the inner data implementsDisplay. It's useful for logging and debugging purpose.Signed-off-by Boqun Feng <[email protected]>
Quoted 34 lines
--- rust/kernel/sync/arc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+)rust/kernel/sync/arc.rs +13
-0@@ -22,6 +22,7 @@ use crate::{ };use alloc::boxed::Box ;use core::{ + fmt, marker::{PhantomData ,Unsize }, mem::{ManuallyDrop ,MaybeUninit }, ops::{Deref ,DerefMut },@@ -522,3 +523,15 @@ impl<T: ?Sized> DerefMut for UniqueArc<T> {unsafe { &mut self.inner.ptr.as_mut ().data } } } + +impl <T : fmt::Display + ?Sized > fmt::Display for UniqueArc <T > { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + fmt::Display ::fmt (self.deref (), f) + } +} + +impl <T : fmt::Display + ?Sized > fmt::Display for Arc <T > { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + fmt::Display ::fmt (self.deref (), f) + } +}Signature · 1 line
2.39.1
Re: [RFC 1/5] rust: sync: impl Display for {Unique,}Arc
In reply to Boqun FengQuoted 6 lines · Boqun Feng
This allows printing the inner data of
Arcand its friends if the inner data implementsDisplay. It's useful for logging and debugging purpose.Signed-off-by Boqun Feng <[email protected]>---
Quoted 33 lines
rust/kernel/sync/arc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+)rust/kernel/sync/arc.rs +13
-0@@ -22,6 +22,7 @@ use crate::{ };use alloc::boxed::Box ;use core::{ + fmt, marker::{PhantomData ,Unsize }, mem::{ManuallyDrop ,MaybeUninit }, ops::{Deref ,DerefMut },@@ -522,3 +523,15 @@ impl<T: ?Sized> DerefMut for UniqueArc<T> {unsafe { &mut self.inner.ptr.as_mut ().data } } } + +impl <T : fmt::Display + ?Sized > fmt::Display for UniqueArc <T > { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + fmt::Display ::fmt (self.deref (), f) + } +} + +impl <T : fmt::Display + ?Sized > fmt::Display for Arc <T > { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + fmt::Display ::fmt (self.deref (), f) + } +}Signature · 1 line
2.39.1
RFC 2/5 rust: sync: Arc: Introduces ArcInner::count()
In reply to Boqun FengThis allows reading the current count of a refcount in an ArcInner.
--- rust/helpers.c | 6 ++++++ rust/kernel/sync/arc.rs | 9 +++++++++ 2 files changed, 15 insertions(+)
rust/helpers.c +6 -0
@@ -46,6 +46,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) }EXPORT_SYMBOL_GPL (rust_helper_refcount_dec_and_test); +unsigned int rust_helper_refcount_read (refcount_t *r) +{ +return refcount_read (r); +} +EXPORT_SYMBOL_GPL (rust_helper_refcount_read); +/* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust
rust/kernel/sync/arc.rs +9 -0
@@ -127,6 +127,15 @@ struct ArcInner<T: ?Sized> { data: T, } +impl <T : ?Sized >ArcInner <T > { +/// Returns the current reference count of [`ArcInner`]. +fn count (&self) ->u32 { +// SAFETY: `self.refcount.get()` is always a valid pointer, and `refcount_read()` is a +// normal atomic read (i.e. no data race) only requiring on the address is valid. +unsafe { bindings::refcount_read (self.refcount.get ()) } + } +} +// This is to allow [`Arc`] (and variants) to be used as the type of `self`. impl <T : ?Sized > core::ops::Receiver for Arc <T > {}
Signature · 1 line
2.39.1
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Quoted 31 lines · Boqun Feng
This allows reading the current count of a refcount in an
ArcInner.Signed-off-by Boqun Feng <[email protected]>--- rust/helpers.c | 6 ++++++ rust/kernel/sync/arc.rs | 9 +++++++++ 2 files changed, 15 insertions(+)rust/helpers.c +6
-0@@ -46,6 +46,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) }EXPORT_SYMBOL_GPL (rust_helper_refcount_dec_and_test); +unsigned int rust_helper_refcount_read (refcount_t *r) +{ +return refcount_read (r); +} +EXPORT_SYMBOL_GPL (rust_helper_refcount_read); +/* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust rust/kernel/sync/arc.rs +0
-0@@ -127,6 +127,15 @@ struct ArcInner<T: ?Sized> { data: T,
rust/kernel/sync/arc.rs struct ArcInner<T: ?Sized> { · line 137
Quoted 10 lines
rust/kernel/sync/arc.rs +8
-0} +impl <T : ?Sized >ArcInner <T > { +/// Returns the current reference count of [`ArcInner`]. +fn count (&self) ->u32 { +// SAFETY: `self.refcount.get()` is always a valid pointer, and `refcount_read()` is a +// normal atomic read (i.e. no data race) only requiring on the address is valid. +unsafe { bindings::refcount_read (self.refcount.get ()) } + } +}
This is completely unsafe vs concurrency. In order to enable correct tracing of refcount manipulations we have the __refcount_*(.oldp) API.
Admittedly, I have no idea how to sanely wire that up in this thing, but it seems odd to me to have this 'safe' language display fundamentally unsafe data like this.
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Quoted 34 lines · Peter Zijlstra
Quoted 33 lines · Boqun Feng
This allows reading the current count of a refcount in an
ArcInner.Signed-off-by Boqun Feng <[email protected]>--- rust/helpers.c | 6 ++++++ rust/kernel/sync/arc.rs | 9 +++++++++ 2 files changed, 15 insertions(+)rust/helpers.c +6
-0@@ -46,6 +46,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) }EXPORT_SYMBOL_GPL (rust_helper_refcount_dec_and_test); +unsigned int rust_helper_refcount_read (refcount_t *r) +{ +return refcount_read (r); +} +EXPORT_SYMBOL_GPL (rust_helper_refcount_read); +/* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust rust/kernel/sync/arc.rs +0
-0@@ -127,6 +127,15 @@ struct ArcInner<T: ?Sized> { data: T, }
Quoted 12 lines
rust/kernel/sync/arc.rs struct ArcInner<T: ?Sized> { · line 137
Quoted 8 lines
rust/kernel/sync/arc.rs +8
-0+impl <T : ?Sized >ArcInner <T > { +/// Returns the current reference count of [`ArcInner`]. +fn count (&self) ->u32 { +// SAFETY: `self.refcount.get()` is always a valid pointer, and `refcount_read()` is a +// normal atomic read (i.e. no data race) only requiring on the address is valid. +unsafe { bindings::refcount_read (self.refcount.get ()) } + } +}This is completely unsafe vs concurrency. In order to enable correct tracing of refcount manipulations we have the __refcount_*(.oldp) API.
Interesting.
Quoted 3 lines
Admittedly, I have no idea how to sanely wire that up in this thing, but it seems odd to me to have this 'safe' language display fundamentally unsafe data like this.
Right now the only use of this "data" is for debugging display, the "count" function is private, so no one outside Arc can mis-use it, in that sense, I think it's still safe?
Regards,
Boqun
Gary Guo
maintainerRe: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
In reply to Peter ZijlstraQuoted 34 lines · On Thu, 2 Feb 2023 10:14:06 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 33 lines · Boqun Feng
This allows reading the current count of a refcount in an
ArcInner.Signed-off-by Boqun Feng <[email protected]>--- rust/helpers.c | 6 ++++++ rust/kernel/sync/arc.rs | 9 +++++++++ 2 files changed, 15 insertions(+)rust/helpers.c +6
-0@@ -46,6 +46,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) }EXPORT_SYMBOL_GPL (rust_helper_refcount_dec_and_test); +unsigned int rust_helper_refcount_read (refcount_t *r) +{ +return refcount_read (r); +} +EXPORT_SYMBOL_GPL (rust_helper_refcount_read); +/* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust rust/kernel/sync/arc.rs +0
-0@@ -127,6 +127,15 @@ struct ArcInner<T: ?Sized> { data: T, }
Quoted 11 lines
rust/kernel/sync/arc.rs struct ArcInner<T: ?Sized> { · line 137
Quoted 8 lines
rust/kernel/sync/arc.rs +8
-0+impl <T : ?Sized >ArcInner <T > { +/// Returns the current reference count of [`ArcInner`]. +fn count (&self) ->u32 { +// SAFETY: `self.refcount.get()` is always a valid pointer, and `refcount_read()` is a +// normal atomic read (i.e. no data race) only requiring on the address is valid. +unsafe { bindings::refcount_read (self.refcount.get ()) } + } +}This is completely unsafe vs concurrency. In order to enable correct tracing of refcount manipulations we have the __refcount_*(.oldp) API.
Retrieving the reference count is safe. It's just that in many scenarios it's very hard to use the retrieved reference count correctly, because it might be concurrently changed.
But there are correct ways to use a refcount, e.g. if you own Arc and .count() returns 1, then you know that you are the exclusive owner of the Arc and nobody else is going to touch it. In this case we could get a &mut of the data (Rust standard library's Arc provides such an API, although we don't have it yet).
The Rust standard library's Arc also expose a strong_count function, with this doc:
``` Gets the number of strong (Arc) pointers to this allocation. # Safety This method by itself is safe, but using it correctly requires extra care. Another thread can change the strong count at any time, including potentially between calling this method and acting on the result. ```
Best,
Gary
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Quoted 49 lines · Gary Guo
Quoted 34 lines · On Thu, 2 Feb 2023 10:14:06 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 33 lines · Boqun Feng
This allows reading the current count of a refcount in an
ArcInner.Signed-off-by Boqun Feng <[email protected]>--- rust/helpers.c | 6 ++++++ rust/kernel/sync/arc.rs | 9 +++++++++ 2 files changed, 15 insertions(+)rust/helpers.c +6
-0@@ -46,6 +46,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) }EXPORT_SYMBOL_GPL (rust_helper_refcount_dec_and_test); +unsigned int rust_helper_refcount_read (refcount_t *r) +{ +return refcount_read (r); +} +EXPORT_SYMBOL_GPL (rust_helper_refcount_read); +/* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust rust/kernel/sync/arc.rs +0
-0@@ -127,6 +127,15 @@ struct ArcInner<T: ?Sized> { data: T, }Quoted 11 lines
rust/kernel/sync/arc.rs struct ArcInner<T: ?Sized> { · line 137
Quoted 8 lines
rust/kernel/sync/arc.rs +8
-0+impl <T : ?Sized >ArcInner <T > { +/// Returns the current reference count of [`ArcInner`]. +fn count (&self) ->u32 { +// SAFETY: `self.refcount.get()` is always a valid pointer, and `refcount_read()` is a +// normal atomic read (i.e. no data race) only requiring on the address is valid. +unsafe { bindings::refcount_read (self.refcount.get ()) } + } +}This is completely unsafe vs concurrency. In order to enable correct tracing of refcount manipulations we have the __refcount_*(.oldp) API.
Quoted 3 lines
Retrieving the reference count is safe. It's just that in many scenarios it's very hard to use the retrieved reference count correctly, because it might be concurrently changed.
Yes, so you really should never ever ever care about the value, and that includes printing it out as it will be wrong the instant you read it.
Quoted 3 lines
But there are correct ways to use a refcount, e.g. if you own
Arcand.count()returns 1, then you know that you are the exclusive owner of theArcand nobody else is going to touch it.
But you should never know this, as it is not relevant.
So no, please don't allow printing out of a reference count, that will only cause problems and allow people to think it is safe to do so.
Peter is right, please don't do this.
thanks,
greg k-h
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Quoted 50 lines · Greg KH
Quoted 49 lines · Gary Guo
Quoted 45 lines · On Thu, 2 Feb 2023 10:14:06 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 31 lines · Boqun Feng
This allows reading the current count of a refcount in an
ArcInner.Signed-off-by Boqun Feng <[email protected]>--- rust/helpers.c | 6 ++++++ rust/kernel/sync/arc.rs | 9 +++++++++ 2 files changed, 15 insertions(+)rust/helpers.c +6
-0@@ -46,6 +46,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) }EXPORT_SYMBOL_GPL (rust_helper_refcount_dec_and_test); +unsigned int rust_helper_refcount_read (refcount_t *r) +{ +return refcount_read (r); +} +EXPORT_SYMBOL_GPL (rust_helper_refcount_read); +/* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust rust/kernel/sync/arc.rs +0
-0@@ -127,6 +127,15 @@ struct ArcInner<T: ?Sized> { data: T,rust/kernel/sync/arc.rs struct ArcInner<T: ?Sized> { · line 137
Quoted 10 lines
rust/kernel/sync/arc.rs +8
-0} +impl <T : ?Sized >ArcInner <T > { +/// Returns the current reference count of [`ArcInner`]. +fn count (&self) ->u32 { +// SAFETY: `self.refcount.get()` is always a valid pointer, and `refcount_read()` is a +// normal atomic read (i.e. no data race) only requiring on the address is valid. +unsafe { bindings::refcount_read (self.refcount.get ()) } + } +}This is completely unsafe vs concurrency. In order to enable correct tracing of refcount manipulations we have the __refcount_*(.oldp) API.
Quoted 7 lines
Quoted 3 lines
Retrieving the reference count is safe. It's just that in many scenarios it's very hard to use the retrieved reference count correctly, because it might be concurrently changed.
Yes, so you really should never ever ever care about the value, and that includes printing it out as it will be wrong the instant you read it.
Agreed.
Quoted 9 lines
Quoted 3 lines
But there are correct ways to use a refcount, e.g. if you own
Arcand.count()returns 1, then you know that you are the exclusive owner of theArcand nobody else is going to touch it.But you should never know this, as it is not relevant.
So no, please don't allow printing out of a reference count, that will only cause problems and allow people to think it is safe to do so.
People already do it, even in *security* code,
security/keys/keyring.c:
int key_link(struct key *keyring, struct key *key)
{
...
kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage));
...
}Should we fix that?
Actually It is *safe* to do it, the existence of ArcInner proves the object can not be freed while reading the refcount. This is somewhat similar to the data_race() macro, we know there is a data race, but we don't care because of the way that we are going to use the value won't cause a problem.
What I propose is to print it in debug fmt only, anyway I'm OK to remove it, but I'm really confused about the reasoning here.
Regards,
Boqun
Quoted 5 lines
Peter is right, please don't do this.
thanks,
greg k-h
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Quoted 75 lines · Boqun Feng
Quoted 50 lines · Greg KH
Quoted 49 lines · Gary Guo
Quoted 45 lines · On Thu, 2 Feb 2023 10:14:06 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 31 lines · Boqun Feng
This allows reading the current count of a refcount in an
ArcInner.Signed-off-by Boqun Feng <[email protected]>--- rust/helpers.c | 6 ++++++ rust/kernel/sync/arc.rs | 9 +++++++++ 2 files changed, 15 insertions(+)rust/helpers.c +6
-0@@ -46,6 +46,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) }EXPORT_SYMBOL_GPL (rust_helper_refcount_dec_and_test); +unsigned int rust_helper_refcount_read (refcount_t *r) +{ +return refcount_read (r); +} +EXPORT_SYMBOL_GPL (rust_helper_refcount_read); +/* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust rust/kernel/sync/arc.rs +0
-0@@ -127,6 +127,15 @@ struct ArcInner<T: ?Sized> { data: T,rust/kernel/sync/arc.rs struct ArcInner<T: ?Sized> { · line 137
Quoted 10 lines
rust/kernel/sync/arc.rs +8
-0} +impl <T : ?Sized >ArcInner <T > { +/// Returns the current reference count of [`ArcInner`]. +fn count (&self) ->u32 { +// SAFETY: `self.refcount.get()` is always a valid pointer, and `refcount_read()` is a +// normal atomic read (i.e. no data race) only requiring on the address is valid. +unsafe { bindings::refcount_read (self.refcount.get ()) } + } +}This is completely unsafe vs concurrency. In order to enable correct tracing of refcount manipulations we have the __refcount_*(.oldp) API.
Quoted 7 lines
Quoted 3 lines
Retrieving the reference count is safe. It's just that in many scenarios it's very hard to use the retrieved reference count correctly, because it might be concurrently changed.
Yes, so you really should never ever ever care about the value, and that includes printing it out as it will be wrong the instant you read it.
Agreed.
Quoted 9 lines
Quoted 3 lines
But there are correct ways to use a refcount, e.g. if you own
Arcand.count()returns 1, then you know that you are the exclusive owner of theArcand nobody else is going to touch it.But you should never know this, as it is not relevant.
So no, please don't allow printing out of a reference count, that will only cause problems and allow people to think it is safe to do so.
People already do it, even in *security* code,
security/keys/keyring.c:
Quoted 8 lines
int key_link(struct key *keyring, struct key *key) { ... kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage)); ... }Should we fix that?
Yes. But really, that's debugging code, it probably should all be removed now.
thanks,
greg k-h
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
On Thu, Feb 02, 2023 at 05:17:55PM +0100, Greg KH wrote:
[...]
Quoted 21 lines
Quoted 21 lines
Quoted 9 lines
Quoted 3 lines
But there are correct ways to use a refcount, e.g. if you own
Arcand.count()returns 1, then you know that you are the exclusive owner of theArcand nobody else is going to touch it.But you should never know this, as it is not relevant.
So no, please don't allow printing out of a reference count, that will only cause problems and allow people to think it is safe to do so.
People already do it, even in *security* code,
security/keys/keyring.c:int key_link(struct key *keyring, struct key *key) { ... kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage)); ... }
Quoted 5 lines
Quoted 1 line
Should we fix that?
Yes. But really, that's debugging code, it probably should all be removed now.
Well, there are also printings in proc_keys_show() and proc_key_users_show() in security/keys/proc.c, and I think it's hard to remove these since they are userspace related.
Anyway I realise I could have done a better job explaining what I'm doing here:
I want to read refcount in a later patch, which make Arc<T> implement Debug trait/interface, and that allows user to print Arc<T> for debug purposes, e.g.
pr_info!("{:#x?}", a); // a is an "Arc<T">that's the only usage of the reading from refcount. I could open code an FFI call in that implementation, but I thought maybe I could add a helper function, hence the "count" function. And since "count" is a private function, so no one can use it outside this rust/kernel/sync/arc.rs file, therefore mis-usage by outsiders are impossible.
Maybe I made things confusing because I just learned the language and wanted to try out a few things which made things complicated (for review), hope the above explanation undo some of the confusion I created.
As I said, I'm open to remove the printing of the refcount, and if you and Peter think maybe it's OK to do that after the explanation above, I will improve the patch to make things clear ;-)
Regards,
Boqun
Quoted 3 lines
thanks,
greg k-h
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Quoted 3 lines · Boqun Feng
As I said, I'm open to remove the printing of the refcount, and if you and Peter think maybe it's OK to do that after the explanation above,
Perhaps part of the confusion came from the overloaded "safe" term.
When Gary and Boqun used the term "safe", they meant it in the Rust sense, i.e. calling the method will not allow to introduce undefined behavior. While I think Peter and Greg are using the term to mean something different.
In particular, "safe" in Rust does not mean "hard to misuse" or "OK to use in production" or "avoids functional bugs" or "prevents crashes" and so on. And, yeah, this is a common source of misunderstandings, and some argue a better term could have been chosen.
So it is possible to have perfectly safe Rust functions that are very tricky to use or unintended for common usage. Now, of course, whether having a particular function is a good idea or not is a different story.
Boqun: maybe appending a small paragraph to the doc comments of the function would alleviate concerns.
Cheers,
Miguel
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Quoted 11 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
As I said, I'm open to remove the printing of the refcount, and if you and Peter think maybe it's OK to do that after the explanation above,
Perhaps part of the confusion came from the overloaded "safe" term.
When Gary and Boqun used the term "safe", they meant it in the Rust sense, i.e. calling the method will not allow to introduce undefined behavior. While I think Peter and Greg are using the term to mean something different.
Yes, I mean it in a "this is not giving you the value you think you are getting and you can not rely on it for anything at all as it is going to be incorrect" meaning.
Which in kernel code means "this is not something you should do".
thanks,
greg k-h
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Quoted 13 lines · Greg KH
Quoted 11 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
As I said, I'm open to remove the printing of the refcount, and if you and Peter think maybe it's OK to do that after the explanation above,
Perhaps part of the confusion came from the overloaded "safe" term.
When Gary and Boqun used the term "safe", they meant it in the Rust sense, i.e. calling the method will not allow to introduce undefined behavior. While I think Peter and Greg are using the term to mean something different.
Quoted 6 lines
Yes, I mean it in a "this is not giving you the value you think you are getting and you can not rely on it for anything at all as it is going to be incorrect" meaning.
Which in kernel code means "this is not something you should do".
Now what really confuses me is why kref_read() is safe.. or how this is different than kref_read(). Needless to say that ArcInner::count() can guarantee not reading 0 (because of the type invariants) but kref_read() cannot..
Regards,
Boqun
Quoted 3 lines
thanks,
greg k-h
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Quoted 14 lines · Boqun Feng
Quoted 13 lines · Greg KH
Quoted 11 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
As I said, I'm open to remove the printing of the refcount, and if you and Peter think maybe it's OK to do that after the explanation above,
Perhaps part of the confusion came from the overloaded "safe" term.
When Gary and Boqun used the term "safe", they meant it in the Rust sense, i.e. calling the method will not allow to introduce undefined behavior. While I think Peter and Greg are using the term to mean something different.
Quoted 8 lines
Quoted 6 lines
Yes, I mean it in a "this is not giving you the value you think you are getting and you can not rely on it for anything at all as it is going to be incorrect" meaning.
Which in kernel code means "this is not something you should do".
Now what really confuses me is why
kref_read()is safe..
It isn't, and I hate it and it should be removed from the kernel entirely. But the scsi and drm developers seem to insist that "their locking model ensures it will be safe to use" and I lost that argument :(
Quoted 1 line
or how this is different than
kref_read().
It isn't, but again, I don't like that and do not agree it should be used as it is almost always a sign that the logic in the code is incorrect.
Quoted 1 line
Needless to say that
ArcInner::count()can guarantee not reading 0
How? Because you have an implicit reference on it already? If so, then why does reading from it matter at all, as if you have a reference, you know it isn't 0, and that's all that you can really care about. You don't care about any number other than 0 for a reference count, as by definition, that's what a reference count does :)
Quoted 1 line
(because of the type invariants) but
kref_read()cannot..
I totally agree with you. Let's not mirror bad decisions of legacy subsystems in the kernel written in C with new designs in Rust please.
thanks,
greg k-h
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Quoted 35 lines · Greg KH
Quoted 14 lines · Boqun Feng
Quoted 13 lines · Greg KH
Quoted 11 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
As I said, I'm open to remove the printing of the refcount, and if you and Peter think maybe it's OK to do that after the explanation above,
Perhaps part of the confusion came from the overloaded "safe" term.
When Gary and Boqun used the term "safe", they meant it in the Rust sense, i.e. calling the method will not allow to introduce undefined behavior. While I think Peter and Greg are using the term to mean something different.
Quoted 8 lines
Quoted 6 lines
Yes, I mean it in a "this is not giving you the value you think you are getting and you can not rely on it for anything at all as it is going to be incorrect" meaning.
Which in kernel code means "this is not something you should do".
Now what really confuses me is why
kref_read()is safe..It isn't, and I hate it and it should be removed from the kernel entirely. But the scsi and drm developers seem to insist that "their locking model ensures it will be safe to use" and I lost that argument :(
Quoted 1 line
or how this is different than
kref_read().It isn't, but again, I don't like that and do not agree it should be used as it is almost always a sign that the logic in the code is incorrect.
Quoted 8 lines
Quoted 1 line
Needless to say that
ArcInner::count()can guarantee not reading 0How? Because you have an implicit reference on it already? If so, then why does reading from it matter at all, as if you have a reference, you know it isn't 0, and that's all that you can really care about. You don't care about any number other than 0 for a reference count, as by definition, that's what a reference count does :)
Fair enough!
Quoted 5 lines
Quoted 1 line
(because of the type invariants) but
kref_read()cannot..I totally agree with you. Let's not mirror bad decisions of legacy subsystems in the kernel written in C with new designs in Rust please.
Roger that, will remove this in the next version ;-)
Regards,
Boqun
Quoted 3 lines
thanks,
greg k-h
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
In reply to Greg KHOn Fri, Feb 03, 2023 at 08:38:25AM +0100, Greg KH wrote:
[...]
Quoted 3 lines
Quoted 1 line
Needless to say that
ArcInner::count()can guarantee not reading 0How? Because you have an implicit reference on it already? If so, then
Yes, roughly a reference ("&") in Rust can be treated as a compile-time reference count, so the existence of "&" in fact prevents the underlying data from going away, or in Rust term, being "drop"ped.
To get a "&ArcInner<T>", we need a "&Arc<T>", and as long as there is a reference to an "Arc<T>", the object won't be dropped, that's the proof of the underly object still being referenced.
Other folks may explain this better and accurate, but that's the basic idea ;-)
Regards,
Boqun
Quoted 5 lines
why does reading from it matter at all, as if you have a reference, you know it isn't 0, and that's all that you can really care about. You don't care about any number other than 0 for a reference count, as by definition, that's what a reference count does :)
[...]
Quoted 3 lines
thanks,
greg k-h
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
In reply to Greg KHQuoted 5 lines · Greg KH
How? Because you have an implicit reference on it already? If so, then why does reading from it matter at all, as if you have a reference, you know it isn't 0, and that's all that you can really care about. You don't care about any number other than 0 for a reference count, as by definition, that's what a reference count does :)
There is an additional ability for 1, mentioned up thread -- if you have &mut Arc<T>, and the inner count is 1, then you *know* there aren't any other Arc<T> handles anywhere else. Then it is safe to return an exclusive &mut T, like the upstream Arc::get_mut and Arc::make_mut. This can also be used for owned Arc<T> like the upstream Arc::try_unwrap.
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Quoted 8 lines · Josh Stone
Quoted 5 lines · Greg KH
How? Because you have an implicit reference on it already? If so, then why does reading from it matter at all, as if you have a reference, you know it isn't 0, and that's all that you can really care about. You don't care about any number other than 0 for a reference count, as by definition, that's what a reference count does :)
Quoted 5 lines
There is an additional ability for 1, mentioned up thread -- if you have &mut Arc<T>, and the inner count is 1, then you *know* there aren't any other Arc<T> handles anywhere else. Then it is safe to return an exclusive &mut T, like the upstream
Arc::get_mutandArc::make_mut. This can also be used for owned Arc<T> like the upstreamArc::try_unwrap.
The Arc<T> in the kernel is pinned so we can't return an &mut T, though a Pin<&mut T> would be ok.
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
In reply to Boqun FengQuoted 3 lines · On Wed, 1 Feb 2023 15:22:41 -0800 Boqun Feng <[email protected]> wrote:
This allows reading the current count of a refcount in an
ArcInner.Signed-off-by Boqun Feng <[email protected]>
Quoted 42 lines
--- rust/helpers.c | 6 ++++++ rust/kernel/sync/arc.rs | 9 +++++++++ 2 files changed, 15 insertions(+)rust/helpers.c +6
-0@@ -46,6 +46,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) }EXPORT_SYMBOL_GPL (rust_helper_refcount_dec_and_test); +unsigned int rust_helper_refcount_read (refcount_t *r) +{ +return refcount_read (r); +} +EXPORT_SYMBOL_GPL (rust_helper_refcount_read); +/* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust rust/kernel/sync/arc.rs +9
-0@@ -127,6 +127,15 @@ struct ArcInner<T: ?Sized> { data: T, } +impl <T : ?Sized >ArcInner <T > { +/// Returns the current reference count of [`ArcInner`]. +fn count (&self) ->u32 { +// SAFETY: `self.refcount.get()` is always a valid pointer, and `refcount_read()` is a +// normal atomic read (i.e. no data race) only requiring on the address is valid. +unsafe { bindings::refcount_read (self.refcount.get ()) } + } +} +// This is to allow [`Arc`] (and variants) to be used as the type of `self`. impl <T : ?Sized > core::ops::Receiver for Arc <T > {}
Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
In reply to Boqun FengQuoted 4 lines · Boqun Feng
This allows reading the current count of a refcount in an
ArcInner.Signed-off-by Boqun Feng <[email protected]>---
Quoted 43 lines
rust/helpers.c | 6 ++++++ rust/kernel/sync/arc.rs | 9 +++++++++ 2 files changed, 15 insertions(+)rust/helpers.c +6
-0@@ -46,6 +46,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) }EXPORT_SYMBOL_GPL (rust_helper_refcount_dec_and_test); +unsigned int rust_helper_refcount_read (refcount_t *r) +{ +return refcount_read (r); +} +EXPORT_SYMBOL_GPL (rust_helper_refcount_read); +/* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust rust/kernel/sync/arc.rs +9
-0@@ -127,6 +127,15 @@ struct ArcInner<T: ?Sized> { data: T, } +impl <T : ?Sized >ArcInner <T > { +/// Returns the current reference count of [`ArcInner`]. +fn count (&self) ->u32 { +// SAFETY: `self.refcount.get()` is always a valid pointer, and `refcount_read()` is a +// normal atomic read (i.e. no data race) only requiring on the address is valid. +unsafe { bindings::refcount_read (self.refcount.get ()) } + } +} +// This is to allow [`Arc`] (and variants) to be used as the type of `self`. impl <T : ?Sized > core::ops::Receiver for Arc <T > {}Signature · 1 line
2.39.1
RFC 3/5 rust: sync: Arc: Introduces Arc::get_inner() helper
In reply to Boqun FengGetting a &ArcInner<T> should always be safe from a Arc<T>, therefore add this helper function to avoid duplicate unsafe ptr.as_ref().
--- rust/kernel/sync/arc.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
rust/kernel/sync/arc.rs +10 -9
@@ -201,6 +201,13 @@ impl<T: ?Sized> Arc<T> {// reference can be created. unsafe {ArcBorrow ::new (self.ptr) } } + +/// Returns a reference to the internal [`ArcInner`]. +fn get_inner (&self) -> &ArcInner <T > { +// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is +// safe to dereference it. +unsafe { self.ptr.as_ref () } + } }impl <T : 'static>ForeignOwnable for Arc <T > {
@@ -233,9 +240,7 @@ impl<T: ?Sized> Deref for Arc<T> {type Target =T ;fn deref (&self) -> &Self ::Target { -// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is -// safe to dereference it. -unsafe { &self.ptr.as_ref ().data } + &self.get_inner ().data } }
@@ -244,7 +249,7 @@ impl<T: ?Sized> Clone for Arc<T> {// INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero. // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is // safe to increment the refcount. -unsafe { bindings::refcount_inc (self.ptr.as_ref ().refcount.get ()) }; +unsafe { bindings::refcount_inc (self.get_inner ().refcount.get ()) };// SAFETY: We just incremented the refcount. This increment is now owned by the new `Arc`. unsafe {Self ::from_inner (self.ptr) }
@@ -253,11 +258,7 @@ impl<T: ?Sized> Clone for Arc<T> {impl <T : ?Sized >Drop for Arc <T > {fn drop (&mut self) { -// SAFETY: By the type invariant, there is necessarily a reference to the object. We cannot -// touch `refcount` after it's decremented to a non-zero value because another thread/CPU -// may concurrently decrement it to zero and free it. It is ok to have a raw pointer to -// freed/invalid memory as long as it is never dereferenced. -let refcount =unsafe { self.ptr.as_ref () }.refcount.get (); +let refcount = self.get_inner ().refcount.get ();// INVARIANT: If the refcount reaches zero, there are no other instances of `Arc`, and // this instance is being dropped, so the broken invariant is not observable.
Signature · 1 line
2.39.1
Re: [RFC 3/5] rust: sync: Arc: Introduces Arc::get_inner() helper
Quoted 5 lines · On Wed, 1 Feb 2023 15:22:42 -0800 Boqun Feng <[email protected]> wrote:
Getting a
&ArcInner<T>should always be safe from aArc<T>, therefore add this helper function to avoid duplicate unsafeptr.as_ref().Signed-off-by Boqun Feng <[email protected]>
Quoted 55 lines
--- rust/kernel/sync/arc.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)rust/kernel/sync/arc.rs +10
-9@@ -201,6 +201,13 @@ impl<T: ?Sized> Arc<T> {// reference can be created. unsafe {ArcBorrow ::new (self.ptr) } } + +/// Returns a reference to the internal [`ArcInner`]. +fn get_inner (&self) -> &ArcInner <T > { +// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is +// safe to dereference it. +unsafe { self.ptr.as_ref () } + } }impl <T : 'static>ForeignOwnable for Arc <T > {@@ -233,9 +240,7 @@ impl<T: ?Sized> Deref for Arc<T> {type Target =T ;fn deref (&self) -> &Self ::Target { -// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is -// safe to dereference it. -unsafe { &self.ptr.as_ref ().data } + &self.get_inner ().data } }@@ -244,7 +249,7 @@ impl<T: ?Sized> Clone for Arc<T> {// INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero. // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is // safe to increment the refcount. -unsafe { bindings::refcount_inc (self.ptr.as_ref ().refcount.get ()) }; +unsafe { bindings::refcount_inc (self.get_inner ().refcount.get ()) };// SAFETY: We just incremented the refcount. This increment is now owned by the new `Arc`. unsafe {Self ::from_inner (self.ptr) }@@ -253,11 +258,7 @@ impl<T: ?Sized> Clone for Arc<T> {impl <T : ?Sized >Drop for Arc <T > {fn drop (&mut self) { -// SAFETY: By the type invariant, there is necessarily a reference to the object. We cannot -// touch `refcount` after it's decremented to a non-zero value because another thread/CPU -// may concurrently decrement it to zero and free it. It is ok to have a raw pointer to -// freed/invalid memory as long as it is never dereferenced. -let refcount =unsafe { self.ptr.as_ref () }.refcount.get (); +let refcount = self.get_inner ().refcount.get ();// INVARIANT: If the refcount reaches zero, there are no other instances of `Arc`, and // this instance is being dropped, so the broken invariant is not observable.
Re: [RFC 3/5] rust: sync: Arc: Introduces Arc::get_inner() helper
In reply to Boqun FengQuoted 6 lines · Boqun Feng
Getting a
&ArcInner<T>should always be safe from aArc<T>,therefore add this helper function to avoid duplicate unsafe
ptr.as_ref().Signed-off-by Boqun Feng <[email protected]>
Quoted 57 lines
--- rust/kernel/sync/arc.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)rust/kernel/sync/arc.rs +10
-10@@ -201,6 +201,13 @@ impl<T: ?Sized> Arc<T> {// reference can be created. unsafe {ArcBorrow ::new (self.ptr) } } + +/// Returns a reference to the internal [`ArcInner`]. +fn get_inner (&self) -> &ArcInner <T > { +// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is +// safe to dereference it. +unsafe { self.ptr.as_ref () } + } }impl <T : 'static>ForeignOwnable for Arc <T > {@@ -233,9 +240,7 @@ impl<T: ?Sized> Deref for Arc<T> {type Target =T ;fn deref (&self) -> &Self ::Target { -// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is -// safe to dereference it. -unsafe { &self.ptr.as_ref ().data } + &self.get_inner ().data } }@@ -244,7 +249,7 @@ impl<T: ?Sized> Clone for Arc<T> {// INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero. // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is // safe to increment the refcount. -unsafe { bindings::refcount_inc (self.ptr.as_ref ().refcount.get ()) }; +unsafe { bindings::refcount_inc (self.get_inner ().refcount.get ()) };// SAFETY: We just incremented the refcount. This increment is now owned by the new `Arc`. unsafe {Self ::from_inner (self.ptr) }@@ -253,11 +258,7 @@ impl<T: ?Sized> Clone for Arc<T> {impl <T : ?Sized >Drop for Arc <T > {fn drop (&mut self) { -// SAFETY: By the type invariant, there is necessarily a reference to the object. We cannot -// touch `refcount` after it's decremented to a non-zero value because another thread/CPU -// may concurrently decrement it to zero and free it. It is ok to have a raw pointer to -// freed/invalid memory as long as it is never dereferenced. -let refcount =unsafe { self.ptr.as_ref () }.refcount.get (); +let refcount = self.get_inner ().refcount.get ();// INVARIANT: If the refcount reaches zero, there are no other instances of `Arc`, and // this instance is being dropped, so the broken invariant is not observable. --2.39.1
Re: [RFC 3/5] rust: sync: Arc: Introduces Arc::get_inner() helper
In reply to Boqun FengQuoted 6 lines · Boqun Feng
Getting a
&ArcInner<T>should always be safe from aArc<T>, therefore add this helper function to avoid duplicate unsafeptr.as_ref().Signed-off-by Boqun Feng <[email protected]>---
Quoted 56 lines
rust/kernel/sync/arc.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)rust/kernel/sync/arc.rs +10
-9@@ -201,6 +201,13 @@ impl<T: ?Sized> Arc<T> {// reference can be created. unsafe {ArcBorrow ::new (self.ptr) } } + +/// Returns a reference to the internal [`ArcInner`]. +fn get_inner (&self) -> &ArcInner <T > { +// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is +// safe to dereference it. +unsafe { self.ptr.as_ref () } + } }impl <T : 'static>ForeignOwnable for Arc <T > {@@ -233,9 +240,7 @@ impl<T: ?Sized> Deref for Arc<T> {type Target =T ;fn deref (&self) -> &Self ::Target { -// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is -// safe to dereference it. -unsafe { &self.ptr.as_ref ().data } + &self.get_inner ().data } }@@ -244,7 +249,7 @@ impl<T: ?Sized> Clone for Arc<T> {// INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero. // SAFETY: By the type invariant, there is necessarily a reference to the object, so it is // safe to increment the refcount. -unsafe { bindings::refcount_inc (self.ptr.as_ref ().refcount.get ()) }; +unsafe { bindings::refcount_inc (self.get_inner ().refcount.get ()) };// SAFETY: We just incremented the refcount. This increment is now owned by the new `Arc`. unsafe {Self ::from_inner (self.ptr) }@@ -253,11 +258,7 @@ impl<T: ?Sized> Clone for Arc<T> {impl <T : ?Sized >Drop for Arc <T > {fn drop (&mut self) { -// SAFETY: By the type invariant, there is necessarily a reference to the object. We cannot -// touch `refcount` after it's decremented to a non-zero value because another thread/CPU -// may concurrently decrement it to zero and free it. It is ok to have a raw pointer to -// freed/invalid memory as long as it is never dereferenced. -let refcount =unsafe { self.ptr.as_ref () }.refcount.get (); +let refcount = self.get_inner ().refcount.get ();// INVARIANT: If the refcount reaches zero, there are no other instances of `Arc`, and // this instance is being dropped, so the broken invariant is not observable. Signature · 1 line
2.39.1
RFC 4/5 rust: sync: impl Debug for {Unique,}Arc
In reply to Boqun FengThis allows printing macros to print the data and the refcount nubmer of these struct for debugging purposes.
--- rust/kernel/sync/arc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+)
rust/kernel/sync/arc.rs +13 -0
@@ -474,6 +474,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {/// /// # test().unwrap(); /// ``` +#[derive( Debug )] pub struct UniqueArc <T : ?Sized > { inner:Arc <T >, }
@@ -545,3 +546,15 @@ impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> { fmt::Display ::fmt (self.deref (), f) } } + +impl <T : fmt::Debug + ?Sized > fmt::Debug for Arc <T > { +/// Formats debug output for [`Arc`]. +/// +/// Refcount is also printed for debugging purpose. +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + f.debug_struct ("Arc" ) + .field ("refcount" , &self.get_inner ().count ()) + .field ("data" , &self.deref ()) + .finish () + } +}
Signature · 1 line
2.39.1
Gary Guo
maintainerRe: [RFC 4/5] rust: sync: impl Debug for {Unique,}Arc
Quoted 13 lines · On Wed, 1 Feb 2023 15:22:43 -0800 Boqun Feng <[email protected]> wrote:
This allows printing macros to print the data and the refcount nubmer of these struct for debugging purposes.
Signed-off-by Boqun Feng <[email protected]>--- rust/kernel/sync/arc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+)rust/kernel/sync/arc.rs +0
-0
rust/kernel/sync/arc.rs impl<T: ?Sized> Deref for ArcBorrow<'_, T> { · line 477
Quoted 4 lines
rust/kernel/sync/arc.rs +1
-0@@ -474,6 +474,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {/// /// # test().unwrap(); /// ``` +#[derive( Debug )]
I don't think this should be a #[derive(Debug)]. For UniqueArc the refcount field in Arc is useless, and we should just delegate the Debug impl to that of deref, just like Display does.
Best,
Gary
Re: [RFC 4/5] rust: sync: impl Debug for {Unique,}Arc
Quoted 11 lines · Gary Guo
Quoted 8 lines · On Wed, 1 Feb 2023 15:22:43 -0800 Boqun Feng <[email protected]> wrote:
This allows printing macros to print the data and the refcount nubmer of these struct for debugging purposes.
Signed-off-by Boqun Feng <[email protected]>--- rust/kernel/sync/arc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+)
Quoted 14 lines
rust/kernel/sync/arc.rs impl<T: ?Sized> Deref for ArcBorrow<'_, T> { · line 477
Quoted 9 lines
rust/kernel/sync/arc.rs +1
-0@@ -474,6 +474,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {/// /// # test().unwrap(); /// ``` +#[derive( Debug )] I don't think this should be a
#[derive(Debug)]. ForUniqueArcthe refcount field inArcis useless, and we should just delegate theDebugimpl to that of deref, just likeDisplaydoes.
I was just being lazy ;-) Will change this in v2.
Regards,
Boqun
Quoted 2 lines
Best,
Gary
Re: [RFC 4/5] rust: sync: impl Debug for {Unique,}Arc
In reply to Boqun FengQuoted 5 lines · Boqun Feng
This allows printing macros to print the data and the refcount nubmer of these struct for debugging purposes.
Signed-off-by Boqun Feng <[email protected]>---
Quoted 33 lines
rust/kernel/sync/arc.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+)rust/kernel/sync/arc.rs +13
-0@@ -474,6 +474,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {/// /// # test().unwrap(); /// ``` +#[derive( Debug )] pub struct UniqueArc <T : ?Sized > { inner:Arc <T >, }@@ -545,3 +546,15 @@ impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> { fmt::Display ::fmt (self.deref (), f) } } + +impl <T : fmt::Debug + ?Sized > fmt::Debug for Arc <T > { +/// Formats debug output for [`Arc`]. +/// +/// Refcount is also printed for debugging purpose. +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + f.debug_struct ("Arc" ) + .field ("refcount" , &self.get_inner ().count ()) + .field ("data" , &self.deref ()) + .finish () + } +}Signature · 1 line
2.39.1
RFC 5/5 sample: rust: print: Add sampe code for Arc printing
In reply to Boqun FengThis both demonstrates the usage of different print format in Rust and serves as a selftest for the Display and Debug implementation of Arc and its friends.
--- samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
samples/rust/rust_print.rs +26 -0
@@ -15,6 +15,30 @@ module! {struct RustPrint ; +fn arc_print () ->Result { +use kernel::sync::*; + +let a =Arc ::try_new (1 )?; +let b =UniqueArc ::try_new ("hello, world" )?; + +// Prints the value of data in `a`. +pr_info! ("{}" , a); + +// Uses ":?" to print debug fmt of `b`. +pr_info! ("{:?}" , b); + +let a:Arc <&str > = b.into (); +let c = a.clone (); + +// Uses `dbg` to print, will move `c`. +dbg! (c); + +// Prints debug fmt with pretty-print "#" and number-in-hex "x". +pr_info! ("{:#x?}" , a); + +Ok (()) +} +impl kernel::Module for RustPrint {fn init (_module: &'staticThisModule ) ->Result <Self > {pr_info! ("Rust printing macros sample (init)\n" );
@@ -43,6 +67,8 @@ impl kernel::Module for RustPrint {pr_cont! (" is {}" ,"continued" );pr_cont! (" with {}\n" ,"args" ); +arc_print ()?; +Ok (RustPrint ) } }
Signature · 1 line
2.39.1
Re: [RFC 5/5] sample: rust: print: Add sampe code for Arc printing
Quoted 5 lines · Boqun Feng
This both demonstrates the usage of different print format in Rust and serves as a selftest for the
DisplayandDebugimplementation ofArcand its friends.Signed-off-by Boqun Feng <[email protected]>
Quoted 50 lines
--- samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)samples/rust/rust_print.rs +26
-1@@ -15,6 +15,30 @@ module! {struct RustPrint ; +fn arc_print () ->Result { +use kernel::sync::*; + +let a =Arc ::try_new (1 )?; +let b =UniqueArc ::try_new ("hello, world" )?; + +// Prints the value of data in `a`. +pr_info! ("{}" , a); + +// Uses ":?" to print debug fmt of `b`. +pr_info! ("{:?}" , b); + +let a:Arc <&str > = b.into (); +let c = a.clone (); + +// Uses `dbg` to print, will move `c`. +dbg! (c); + +// Prints debug fmt with pretty-print "#" and number-in-hex "x". +pr_info! ("{:#x?}" , a); + +Ok (()) +} +impl kernel::Module for RustPrint {fn init (_module: &'staticThisModule ) ->Result <Self > {pr_info! ("Rust printing macros sample (init)\n" );@@ -43,6 +67,8 @@ impl kernel::Module for RustPrint {pr_cont! (" is {}" ,"continued" );pr_cont! (" with {}\n" ,"args" ); +arc_print ()?; +Ok (RustPrint ) } } --2.39.1
Re: [RFC 5/5] sample: rust: print: Add sampe code for Arc printing
In reply to Boqun FengQuoted 5 lines · Boqun Feng
This both demonstrates the usage of different print format in Rust and serves as a selftest for the
DisplayandDebugimplementation ofArcand its friends.Signed-off-by Boqun Feng <[email protected]>
Quoted 40 lines
--- samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)samples/rust/rust_print.rs +24
-0@@ -15,6 +15,30 @@ module! {struct RustPrint ; +fn arc_print () ->Result { +use kernel::sync::*; + +let a =Arc ::try_new (1 )?; +let b =UniqueArc ::try_new ("hello, world" )?; + +// Prints the value of data in `a`. +pr_info! ("{}" , a); + +// Uses ":?" to print debug fmt of `b`. +pr_info! ("{:?}" , b); + +let a:Arc <&str > = b.into (); +let c = a.clone (); + +// Uses `dbg` to print, will move `c`. +dbg! (c); + +// Prints debug fmt with pretty-print "#" and number-in-hex "x". +pr_info! ("{:#x?}" , a); + +Ok (()) +} +impl kernel::Module for RustPrint {fn init (_module: &'staticThisModule ) ->Result <Self > {pr_info! ("Rust printing macros sample (init)\n" );
samples/rust/rust_print.rs impl kernel::Module for RustPrint { · line 70
Quoted 4 lines
samples/rust/rust_print.rs +1
-0@@ -43,6 +67,8 @@ impl kernel::Module for RustPrint {pr_cont! (" is {}" ,"continued" );pr_cont! (" with {}\n" ,"args" ); +arc_print ()?;
Wonder if it makes sense to also extract the other printing functions to group usage.
Quoted 6 lines
samples/rust/rust_print.rs +1
-0+Ok (RustPrint ) } }Signature · 1 line
2.39.1
Re: [RFC 5/5] sample: rust: print: Add sampe code for Arc printing
In reply to Boqun FengQuoted 6 lines · Boqun Feng
This both demonstrates the usage of different print format in Rust and serves as a selftest for the
DisplayandDebugimplementation ofArcand its friends.Signed-off-by Boqun Feng <[email protected]>---
Quoted 49 lines
samples/rust/rust_print.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)samples/rust/rust_print.rs +26
-0@@ -15,6 +15,30 @@ module! {struct RustPrint ; +fn arc_print () ->Result { +use kernel::sync::*; + +let a =Arc ::try_new (1 )?; +let b =UniqueArc ::try_new ("hello, world" )?; + +// Prints the value of data in `a`. +pr_info! ("{}" , a); + +// Uses ":?" to print debug fmt of `b`. +pr_info! ("{:?}" , b); + +let a:Arc <&str > = b.into (); +let c = a.clone (); + +// Uses `dbg` to print, will move `c`. +dbg! (c); + +// Prints debug fmt with pretty-print "#" and number-in-hex "x". +pr_info! ("{:#x?}" , a); + +Ok (()) +} +impl kernel::Module for RustPrint {fn init (_module: &'staticThisModule ) ->Result <Self > {pr_info! ("Rust printing macros sample (init)\n" );@@ -43,6 +67,8 @@ impl kernel::Module for RustPrint {pr_cont! (" is {}" ,"continued" );pr_cont! (" with {}\n" ,"args" ); +arc_print ()?; +Ok (RustPrint ) } }Signature · 1 line
2.39.1