linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Nick Bowler <nbowler@elliptictech.com>
To: Christoph Lameter <cl@linux.com>
Cc: Xi Wang <xi.wang@gmail.com>,
	Dan Carpenter <dan.carpenter@oracle.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Jesper Juhl <jj@chaosbits.net>, Jens Axboe <axboe@kernel.dk>,
	Pekka Enberg <penberg@kernel.org>,
	linux-kernel@vger.kernel.org, Matt Mackall <mpm@selenic.com>,
	David Rientjes <rientjes@google.com>
Subject: Re: Uninline kcalloc
Date: Wed, 15 Feb 2012 15:17:57 -0500	[thread overview]
Message-ID: <20120215201757.GA6934@elliptictech.com> (raw)
In-Reply-To: <alpine.DEB.2.00.1202141513210.29019@router.home>

On 2012-02-14 15:24 -0600, Christoph Lameter wrote:
> On Tue, 14 Feb 2012, Nick Bowler wrote:
> 
> > On 2012-02-14 13:37 -0600, Christoph Lameter wrote:
> > > This patch still preserves kcalloc. But note that if kcalloc returns NULL
> > > then multiple conditions may have caused it. One is that the array is
> > > simply too large. The other may be that such an allocation is not possible
> > > due to fragmentation.
> > [...]
> > > +static inline long calculate_array_size(size_t n, size_t size)
> > > +{
> > > +	if (size != 0 && n > ULONG_MAX / size)
> > > +
> > > +		return 0;
> >
> > This isn't right.  The above tests whether or not the result of the
> > multiplication will not be representable in an 'unsigned long'...
> 
> Yes and so does the current kcalloc.

Well, the current kcalloc doesn't assign the result to a signed long.
However, it does assign the result to a size_t, which makes one wonder
why it's not testing against SIZE_MAX.  If size_t has the same range as
unsigned long on all architectures, then this confusion doesn't matter,
but is that actually the case?

> > > +	return n * size;
> >
> > but then the result is assigned to a (signed) long, which may overflow
> > if it's greater than LONG_MAX.
> 
> That can happen?

Yes, because LONG_MAX (the maximum value of your return type) is
strictly less than ULONG_MAX (what you test against).  It's not hard to
pick input numbers that multiply to something between LONG_MAX and
ULONG_MAX, which will cause your function to return a negative value
(standard C leaves the result of such a conversion implementation-
defined, but I'll assume for now that it works this way for everything
that compiles Linux).

Admittedly, your kcalloc change then assigns this negative value to a
size_t, which will result in the correct positive value assuming
SIZE_MAX == ULONG_MAX, but that's gratuitously roundabout.

[...]
> > [...]
> > >  void *kcalloc(size_t n, size_t size, gfp_t flags)
> > >  {
> > > -	if (size != 0 && n > ULONG_MAX / size)
> > > -		return NULL;
> > > -	return __kmalloc(n * size, flags | __GFP_ZERO);
> > > +	size_t s = calculate_array_size(n, size);
> > > +
> > > +	if (s)
> > > +		return kzalloc(s, flags);
> > > +
> > > +	return NULL;
> > >  }
> >
> > This hunk changes the behaviour of kcalloc if either of the two size parameters
> > is 0.
> 
> You want ZERO_PTR returns?
>
> NULL is one permissible return value of calloc() if size == 0. So we are
> now deviating from user space conventions.

Sort of.  While standard C leaves it implementation-defined whether
successful zero-sized allocations are possible, all sane implementations
let them succeed.  Hence, portable C apps need to handle 0 as a special
case, because there are insane implementations out there.  There's no
reason for the kernel to be one of them.

Regardless, this was still a (presumably unintentional) change from
the previous behaviour.

Cheers,
-- 
Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)


  reply	other threads:[~2012-02-15 20:18 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-07 14:11 integer overflows in kernel/relay.c Dan Carpenter
2012-02-08  8:34 ` Jens Axboe
2012-02-08 22:25   ` Andrew Morton
2012-02-09 12:41     ` Jens Axboe
2012-02-09 17:39       ` Andrew Morton
2012-02-09 12:41     ` [PATCH RFC] slab: introduce knalloc/kxnalloc Xi Wang
2012-02-09 13:05       ` Pekka Enberg
2012-02-09 13:19         ` Jens Axboe
2012-02-09 13:26           ` Xi Wang
2012-02-09 13:48             ` [PATCH RFC v2] slab: introduce kmalloc_array Xi Wang
2012-02-09 22:42               ` David Rientjes
2012-02-09 23:08                 ` Andrew Morton
2012-02-09 22:47               ` Jesper Juhl
2012-02-09 23:06                 ` Andrew Morton
2012-02-09 23:43                   ` Joe Perches
2012-02-13 15:08                   ` Xi Wang
2012-02-13 16:01                     ` Christoph Lameter
2012-02-13 19:44                       ` Dan Carpenter
2012-02-13 20:27                         ` Christoph Lameter
2012-02-14  7:20                           ` Dan Carpenter
2012-02-14  7:35                             ` Pekka Enberg
2012-02-14 11:12                             ` Xi Wang
2012-02-14 15:02                             ` Christoph Lameter
2012-02-14 16:30                               ` Xi Wang
2012-02-14 16:34                                 ` Christoph Lameter
2012-02-14 16:43                                   ` Xi Wang
2012-02-14 19:33                                     ` Uninline kcalloc Christoph Lameter
2012-02-14 19:37                                       ` Christoph Lameter
2012-02-14 20:46                                         ` Andrew Morton
2012-02-14 20:50                                         ` Nick Bowler
2012-02-14 21:24                                           ` Christoph Lameter
2012-02-15 20:17                                             ` Nick Bowler [this message]
2012-02-15 20:24                                               ` Nick Bowler
2012-02-14 20:45                                       ` Andrew Morton
2012-02-14 20:58                                         ` Pekka Enberg
2012-02-14 21:09                                           ` Christoph Lameter
2012-02-14 21:31                                             ` Pekka Enberg
2012-02-14 21:38                                               ` Christoph Lameter
2012-02-14 21:46                                             ` Xi Wang
2012-02-14 22:08                                               ` Christoph Lameter
2012-02-15 19:14                                                 ` Xi Wang
2012-02-15 19:34                                                   ` Christoph Lameter
2012-02-16  3:10                                                     ` Xi Wang
2012-02-16 14:51                                                       ` Christoph Lameter
2012-02-16 18:32                                                         ` Xi Wang
2012-02-16 20:47                                                           ` Christoph Lameter
2012-02-10 13:09               ` [PATCH RFC v2] slab: introduce kmalloc_array Alexey Dobriyan
2012-02-10 13:11                 ` Alexey Dobriyan
2012-02-10 13:55                   ` Xi Wang
2012-02-10 13:58                     ` Alexey Dobriyan
2012-02-10 14:09                       ` Xi Wang
2012-02-11 12:19                         ` Alexey Dobriyan
2012-02-12  5:46                           ` Xi Wang
2012-02-09 12:56     ` integer overflows in kernel/relay.c Pekka Enberg
2012-02-09 10:44   ` [patch] relay: prevent integer overflow in relay_open() Dan Carpenter
2012-02-09 11:55     ` walter harms
2012-02-09 12:36       ` Dan Carpenter

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=20120215201757.GA6934@elliptictech.com \
    --to=nbowler@elliptictech.com \
    --cc=akpm@linux-foundation.org \
    --cc=axboe@kernel.dk \
    --cc=cl@linux.com \
    --cc=dan.carpenter@oracle.com \
    --cc=jj@chaosbits.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpm@selenic.com \
    --cc=penberg@kernel.org \
    --cc=rientjes@google.com \
    --cc=xi.wang@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).