RE: [RFC] kref, a tiny, sane, reference count object

Peter Kjellerstedt
2004-03-13T09:10:45Z

> -----Original Message-----
> From: linux-kernel-owner@vger.kernel.org
> [mailto:linux-kernel-owner@vger.kernel.org] On Behalf Of Greg KH
> Sent: Saturday, March 13, 2004 09:20
> To: linux-kernel@vger.kernel.org
> Subject: [RFC] kref, a tiny, sane, reference count object
>
> In thinking about people's complaints about the current
> kobject interface, a lot of people don't like the
> "complexity" of what is necessary to use a kobject.
> If all you want is something to handle reference
> counting properly, a kobject can seem a bit "large".
>
> For all of those people, this patch is for you.
> Introducing struct kref. A tiny (only 8 bytes on a
> 32bit platform) that will properly handle reference
> counting any structure you want to use it for. Note
> that you will have to be careful around the cleanup
> period (but that can be easily handled by the user
> with regards to not trying to grab a "new" reference
> if you don't already have one, once the object is
> gone, just like kobjects and sysfs today work.)
>
> I've implemented kobjects using a kref to handle the
> reference counting portion, but will leave that patch
> and change for 2.7, as it will add 4 more bytes (on a
> 32bit platform) to every kobject, and that wouldn't
> be nice this early in the 2.6 series. For now, krefs
> can stand on their own.
>
> I've already found loads of places in the kernel that
> can use this structure to clean up their logic, and
> will probably be converting a number of them over time
> to use them. But no, Al, I will not say this can be
> used to replace the atomic_t count you have in inodes,
> as that count is horribly abused in ways I never really
> wanted to know about (negative counts mean something
> "special"? eeeeeek....)
>
> Anyway, here's a patch against 2.6.4 that adds krefs
> to the kernel. I'll follow up with a patch that
> converts the usb-serial core from using kobjects to
> using krefs instead.
>
> Comments are appreciated and welcomed.
>
> thanks,
>
> greg k-h

Looks simple enough. But I have a small question.
In kref_get() and kref_cleanup(), kref is verified
not to be NULL before being used. However, this is
not done in kref_put(). An oversight, or as intended?

//Peter