Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()

From: Boqun Feng
Date: Thu Feb 02 2023 - 11:52:05 EST


On Thu, Feb 02, 2023 at 05:17:55PM +0100, Greg KH wrote:
[...]
> > > > 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.
> > >
> > > 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?
>
> 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

> thanks,
>
> greg k-h