lkml.org 
[lkml]   [2012]   [Feb]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: Uninline kcalloc
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'...

> +
> + return n * size;

but then the result is assigned to a (signed) long, which may overflow
if it's greater than LONG_MAX.

> +}
[...]
> 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.

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



\
 
 \ /
  Last update: 2012-02-14 21:53    [W:0.182 / U:0.068 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site