From: Linus Torvalds <torvalds@linux-foundation.org>
To: Alan Cox <alan@lxorguk.ukuu.org.uk>
Cc: Pekka Enberg <penberg@cs.helsinki.fi>,
Christoph Lameter <clameter@sgi.com>,
Andrew Morton <akpm@linux-foundation.org>,
linux-kernel@vger.kernel.org, jeremy@goop.org
Subject: Re: SLUB: Return ZERO_SIZE_PTR for kmalloc(0)
Date: Mon, 4 Jun 2007 10:50:41 -0700 (PDT) [thread overview]
Message-ID: <alpine.LFD.0.98.0706041043330.23741@woody.linux-foundation.org> (raw)
In-Reply-To: <20070604182025.558a8d10@the-village.bc.nu>
On Mon, 4 Jun 2007, Alan Cox wrote:
> > The thing is, why *should* we care about comparing addresses? We'll give
>
> Because people use it to tell objects apart. All over the kernel we do
> things like
>
> if (inode1 == inode2)
But that only makes sense if your objects have meaning, which is not
possible with a zero-sized object.
Let's take an example: one of the few reasons to check for equality (or
inequality) is for locking order.
So on UP, the lock goes away, and let's say that you (insanely) only have
a spinlock in the structure in question, so you have a zero-sized
structure that you want to test ordering on because you want to avoid
ABBA deadlocks.
So you write your code as
double_lock()
{
if (ptr1 == ptr2) {
lock(ptr1->lock);
return;
}
if (ptr1 < ptr2) {
lock(ptr1->lock);
lock(ptr2->lock);
return;
}
lock(ptr2->lock);
lock(ptr1->lock);
}
and the interesting thing is that this actually *works* even for the case
where the lock has gone away: even though ptr1/ptr2 are always equal.
In other words, the only _valid_ reasons to compare pointers like this end
up degenerating into working cases even for a zero-sized pointer.
The exception is if you use the memory allocator as a "ID allocator", but
quite frankly, if you use a size of zero, it's your own damn problem.
Insane code is not an argument for insane behaviour.
If people can't be bothered to create a "random ID generator" themselves,
they had damn well better use "kmalloc(1)" rather than "kmalloc(0)" to get
a unique cookie. Asking the allocator to do something idiotic because some
idiot thinks a memory allocator is a cookie allocator is just crazy.
I can understand that things like user-level libraries have to take crazy
people into account, but the kernel internal libraries definitely do not.
(Right now we warn once for zero-sized allocations anyway, and all the
cases we've found so far are either bugs that would have been found with
ZERO_ALLOC_PTR or would have been perfectly fine with it, so I don't think
anybody really _is_ that insane in the kernel)
Linus
next prev parent reply other threads:[~2007-06-04 17:51 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-06-02 1:37 Christoph Lameter
2007-06-02 2:09 ` John Anthony Kazos Jr.
2007-06-02 2:15 ` Christoph Lameter
2007-06-02 2:21 ` John Anthony Kazos Jr.
2007-06-02 2:54 ` Linus Torvalds
2007-06-02 3:06 ` Christoph Lameter
2007-06-02 3:41 ` Andrew Morton
2007-06-02 4:01 ` Christoph Lameter
2007-06-02 4:31 ` Andrew Morton
2007-06-02 4:45 ` Christoph Lameter
2007-06-02 4:54 ` Andrew Morton
2007-06-03 16:17 ` Matt Mackall
2007-06-04 17:59 ` William Lee Irwin III
2007-06-03 16:15 ` Matt Mackall
2007-06-04 14:44 ` Alan Cox
2007-06-04 15:08 ` Pekka Enberg
2007-06-04 16:16 ` Christoph Lameter
2007-06-04 16:22 ` Pekka Enberg
2007-06-04 16:26 ` Linus Torvalds
2007-06-04 16:32 ` Jeremy Fitzhardinge
2007-06-04 16:37 ` Pekka Enberg
2007-06-04 16:38 ` Jeremy Fitzhardinge
2007-06-04 16:43 ` Roland Dreier
2007-06-04 17:02 ` Pekka Enberg
2007-06-04 16:48 ` Linus Torvalds
2007-06-04 18:22 ` Cyrill Gorcunov
2007-06-04 17:20 ` Alan Cox
2007-06-04 17:50 ` Linus Torvalds [this message]
2007-06-04 18:05 ` William Lee Irwin III
2007-06-04 18:47 ` Linus Torvalds
2007-06-04 19:01 ` Christoph Lameter
2007-06-04 19:13 ` Pekka Enberg
2007-06-04 19:14 ` Christoph Lameter
2007-06-04 19:25 ` Pekka Enberg
2007-06-04 22:39 ` Christoph Lameter
2007-06-04 22:53 ` Andrew Morton
2007-06-04 23:09 ` Christoph Lameter
2007-06-05 5:27 ` Pekka Enberg
2007-06-05 17:23 ` Christoph Lameter
2007-06-05 8:50 ` Rene Herman
2007-06-05 12:07 ` John Anthony Kazos Jr.
2007-06-05 12:54 ` Rene Herman
2007-06-05 13:58 ` John Anthony Kazos Jr.
2007-06-05 14:32 ` Rene Herman
2007-06-05 19:07 ` Pekka Enberg
2007-06-05 14:40 ` Jeremy Fitzhardinge
2007-06-05 14:56 ` Rene Herman
2007-06-06 21:26 ` Pavel Machek
2007-06-06 21:58 ` Rene Herman
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=alpine.LFD.0.98.0706041043330.23741@woody.linux-foundation.org \
--to=torvalds@linux-foundation.org \
--cc=akpm@linux-foundation.org \
--cc=alan@lxorguk.ukuu.org.uk \
--cc=clameter@sgi.com \
--cc=jeremy@goop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=penberg@cs.helsinki.fi \
/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
all inboxes | Powered by JetHome®