public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Eric Dumazet <dada1@cosmosbay.com>
To: Greg KH <greg@kroah.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>,
	linux-kernel@vger.kernel.org, vserver@list.linux-vserver.org,
	Herbert Poetzl <herbert@13thfloor.at>,
	"Serge E. Hallyn" <serue@us.ibm.com>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Dave Hansen <haveblue@us.ibm.com>,
	Arjan van de Ven <arjan@infradead.org>,
	Suleiman Souhlal <ssouhlal@FreeBSD.org>,
	Hubertus Franke <frankeh@watson.ibm.com>,
	Cedric Le Goater <clg@fr.ibm.com>,
	Kyle Moffett <mrmacman_g4@mac.com>
Subject: Re: [PATCH 1/5] pid: Implement task references.
Date: Mon, 30 Jan 2006 06:19:35 +0100	[thread overview]
Message-ID: <43DDA1E7.5010109@cosmosbay.com> (raw)
In-Reply-To: <20060130045153.GC13244@kroah.com>

[-- Attachment #1: Type: text/plain, Size: 2088 bytes --]

Greg KH a écrit :
> On Sun, Jan 29, 2006 at 02:58:51PM -0700, Eric W. Biederman wrote:
>> Greg KH <greg@kroah.com> writes:
>>
>>> On Sun, Jan 29, 2006 at 12:22:34AM -0700, Eric W. Biederman wrote:
>>>> +struct task_ref
>>>> +{
>>>> +	atomic_t count;
>>> Please use a struct kref here, instead of your own atomic_t, as that's
>>> why it is in the kernel :)
>>>
>>>> +	enum pid_type type;
>>>> +	struct task_struct *task;
>>>> +};
>>> thanks,
>> I would rather not. Whenever I look at struct kref it seems to be an over
>> abstraction, and as such I find it confusing to work with.  I know
>> whenever I look at the sysfs code I have to actively remind myself
>> that the kref in the structure is not a pointer to a kref.
>>
>> What does the kref abstraction buy?  How does it simplify things?
>> We already have equivalent functions in atomic_t on which it is built.
> 
> It ensures that you get the logic of the reference counting correctly.
> It forces you to do the logic of the get and put and release properly.
> 
> To roughly quote Andrew Morton, "When I see a kref, I know it is used
> properly, otherwise I am forced to read through the code to see if the
> author got the reference counting logic correct."
> 
> It costs _nothing_ to use it, and let's everyone know you got the logic
> correct.
> 
> So don't feel it is a "abstraction", it's a helper for both the author
> (who doesn't have to get the atomic_t calls correct), and for everyone
> else who has to read the code.
> 

kref abstraction is good.

Its current implementation seems straightforward, so I understand some devs 
think they can 'just use an atomic_t' themselves.

But using kref is better because some generic improvement could be done at 
kref level and you dont need to parse all kernel sources to change atomic_t to 
kref...

Example of improvement in kref_put() :

[PATCH] kref : Avoid an atomic operation in kref_put() when the last reference 
is dropped. On most platforms, atomic_read() is a plan read of the counter and 
involves no atomic at all.


Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>

[-- Attachment #2: kref.patch --]
[-- Type: text/plain, Size: 527 bytes --]

--- a/lib/kref.c	2006-01-30 06:11:04.000000000 +0100
+++ b/lib/kref.c	2006-01-30 06:13:32.000000000 +0100
@@ -52,7 +52,12 @@
 	WARN_ON(release == NULL);
 	WARN_ON(release == (void (*)(struct kref *))kfree);
 
-	if (atomic_dec_and_test(&kref->refcount)) {
+	/*
+	 * if current count is one, we are the last user and can release object
+	 * right now, avoiding an atomic operation on 'refcount'
+	 */
+	if ((atomic_read(&kref->refcount) == 1) ||
+	    (atomic_dec_and_test(&kref->refcount))) {
 		release(kref);
 		return 1;
 	}

  reply	other threads:[~2006-01-30  5:19 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-29  7:19 [RFC][PATCH 0/5] Task references Eric W. Biederman
2006-01-29  7:22 ` [PATCH 1/5] pid: Implement task references Eric W. Biederman
2006-01-29  7:24   ` [PATCH 2/5] pid: Add macros for interating through tasks by type Eric W. Biederman
2006-01-29  7:28     ` [PATCH 3/5] pid: Implement kill_tref Eric W. Biederman
2006-01-29  7:33       ` [PATCH 4/5] vt: Update spawnpid to use a task_ref Eric W. Biederman
2006-01-29  7:35         ` [PATCH 5/5] file: Modify struct fown_struct to contain a tref Eric W. Biederman
2006-01-29  8:43           ` Suleiman Souhlal
2006-01-29  9:18             ` Eric W. Biederman
2006-01-30 10:51         ` [PATCH 4/5] vt: Update spawnpid to use a task_ref Pavel Machek
2006-01-30 20:39           ` Eric W. Biederman
2006-01-30 21:05             ` Pavel Machek
2006-01-30 21:15               ` Eric W. Biederman
2006-01-29  8:46   ` [PATCH 1/5] pid: Implement task references Suleiman Souhlal
2006-01-29 19:05   ` Greg KH
2006-01-29 21:58     ` Eric W. Biederman
2006-01-30  4:51       ` Greg KH
2006-01-30  5:19         ` Eric Dumazet [this message]
2006-01-30  5:35           ` Kyle Moffett
2006-01-30  5:46             ` Eric Dumazet
2006-01-30  6:46               ` Kyle Moffett
2006-01-30 18:43           ` Greg KH
2006-01-30 19:58             ` Eric Dumazet
2006-01-30 20:45               ` Eric W. Biederman
2006-01-30 21:32                 ` Eric Dumazet
2006-01-30 21:51                   ` Eric W. Biederman
2006-01-30 20:13         ` Eric W. Biederman
2006-01-31  6:58           ` Greg KH
2006-01-31 16:04             ` Eric W. Biederman
2006-01-29  8:05 ` [RFC][PATCH 0/5] Task references Kyle Moffett
2006-02-06  8:09 ` Eric W. Biederman
2006-02-06 14:36   ` Serge E. Hallyn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=43DDA1E7.5010109@cosmosbay.com \
    --to=dada1@cosmosbay.com \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=arjan@infradead.org \
    --cc=clg@fr.ibm.com \
    --cc=ebiederm@xmission.com \
    --cc=frankeh@watson.ibm.com \
    --cc=greg@kroah.com \
    --cc=haveblue@us.ibm.com \
    --cc=herbert@13thfloor.at \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mrmacman_g4@mac.com \
    --cc=serue@us.ibm.com \
    --cc=ssouhlal@FreeBSD.org \
    --cc=vserver@list.linux-vserver.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox