Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1761058Ab2BNUun (ORCPT ); Tue, 14 Feb 2012 15:50:43 -0500 Received: from mx.scalarmail.ca ([98.158.95.75]:4206 "EHLO ironport-01.sms.scalar.ca" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1760766Ab2BNUul (ORCPT ); Tue, 14 Feb 2012 15:50:41 -0500 Date: Tue, 14 Feb 2012 15:50:32 -0500 From: Nick Bowler To: Christoph Lameter Cc: Xi Wang , Dan Carpenter , Andrew Morton , Jesper Juhl , Jens Axboe , Pekka Enberg , linux-kernel@vger.kernel.org, Matt Mackall , David Rientjes Subject: Re: Uninline kcalloc Message-ID: <20120214205032.GA631@elliptictech.com> References: <20120213194446.GD26353@mwanda> <20120214072017.GF26353@mwanda> <8F83835C-366C-46AC-A50A-3F680B7D2D83@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Organization: Elliptic Technologies Inc. User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1418 Lines: 48 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/) -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/