From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.4 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2253C4363A for ; Tue, 27 Oct 2020 18:22:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A521621556 for ; Tue, 27 Oct 2020 18:22:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1827213AbgJ0SWM (ORCPT ); Tue, 27 Oct 2020 14:22:12 -0400 Received: from mx2.suse.de ([195.135.220.15]:49322 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1826497AbgJ0SUK (ORCPT ); Tue, 27 Oct 2020 14:20:10 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id E2118ADCA; Tue, 27 Oct 2020 18:20:07 +0000 (UTC) Subject: Re: [PATCH 1/8] mm: slab: provide krealloc_array() To: Bartosz Golaszewski , Andy Shevchenko , Sumit Semwal , Gustavo Padovan , =?UTF-8?Q?Christian_K=c3=b6nig?= , Mauro Carvalho Chehab , Borislav Petkov , Tony Luck , James Morse , Robert Richter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Alexander Shishkin , Linus Walleij , "Michael S . Tsirkin" , Jason Wang , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Jaroslav Kysela , Takashi Iwai Cc: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, linux-edac@vger.kernel.org, linux-gpio@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-mm@kvack.org, alsa-devel@alsa-project.org, Bartosz Golaszewski References: <20201027121725.24660-1-brgl@bgdev.pl> <20201027121725.24660-2-brgl@bgdev.pl> From: Vlastimil Babka Message-ID: <21ae795b-c0f4-bbf3-20f6-830d0980a673@suse.cz> Date: Tue, 27 Oct 2020 19:20:06 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.3.3 MIME-Version: 1.0 In-Reply-To: <20201027121725.24660-2-brgl@bgdev.pl> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/27/20 1:17 PM, Bartosz Golaszewski wrote: > From: Bartosz Golaszewski > > When allocating an array of elements, users should check for > multiplication overflow or preferably use one of the provided helpers > like: kmalloc_array(). > > There's no krealloc_array() counterpart but there are many users who use > regular krealloc() to reallocate arrays. Let's provide an actual > krealloc_array() implementation. > > Signed-off-by: Bartosz Golaszewski Makes sense. Acked-by: Vlastimil Babka > --- > include/linux/slab.h | 11 +++++++++++ > 1 file changed, 11 insertions(+) > > diff --git a/include/linux/slab.h b/include/linux/slab.h > index dd6897f62010..0e6683affee7 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -592,6 +592,17 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) > return __kmalloc(bytes, flags); > } > > +static __must_check inline void * > +krealloc_array(void *p, size_t new_n, size_t new_size, gfp_t flags) > +{ > + size_t bytes; > + > + if (unlikely(check_mul_overflow(new_n, new_size, &bytes))) > + return NULL; > + > + return krealloc(p, bytes, flags); > +} > + > /** > * kcalloc - allocate memory for an array. The memory is set to zero. > * @n: number of elements. >