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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham 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 6115BC2BB40 for ; Tue, 15 Dec 2020 03:03:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 135EB224D2 for ; Tue, 15 Dec 2020 03:03:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 135EB224D2 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A56966B007E; Mon, 14 Dec 2020 22:03:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A2C4E6B0080; Mon, 14 Dec 2020 22:03:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 944956B0081; Mon, 14 Dec 2020 22:03:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0082.hostedemail.com [216.40.44.82]) by kanga.kvack.org (Postfix) with ESMTP id 7ED6E6B007E for ; Mon, 14 Dec 2020 22:03:58 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 4E140363B for ; Tue, 15 Dec 2020 03:03:58 +0000 (UTC) X-FDA: 77594022156.09.wall79_5a078c327420 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 32224180AD830 for ; Tue, 15 Dec 2020 03:03:58 +0000 (UTC) X-HE-Tag: wall79_5a078c327420 X-Filterd-Recvd-Size: 5222 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf15.hostedemail.com (Postfix) with ESMTP for ; Tue, 15 Dec 2020 03:03:57 +0000 (UTC) Date: Mon, 14 Dec 2020 19:03:55 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1608001436; bh=mFfAp7kTSMxpRo15TtzXC4BMilzX3c3CqhVozstlkg4=; h=From:To:Subject:In-Reply-To:From; b=onfIWUrZmwgybZXqQyXD0QoUjuwev9gdlSe+fd6MyNZpAXhFn8MBOf07VJaCWyOct zYAHYY+eEjpcKVQVFv5TB4gUA9RtiT24IlHSAR2aJsB4CLM6QCcyx22FyhV9uuLfsF SgX9ohtbFH4QY9d5jSbT8Nw8okqWJkohu6VE2ANk= From: Andrew Morton To: airlied@linux.ie, akpm@linux-foundation.org, alexander.shishkin@linux.intel.com, andriy.shevchenko@linux.intel.com, bgolaszewski@baylibre.com, bp@alien8.de, bp@suse.de, christian.koenig@amd.com, cl@linux.com, daniel.vetter@ffwll.ch, daniel@ffwll.ch, gustavo@padovan.org, iamjoonsoo.kim@lge.com, james.morse@arm.com, jasowang@redhat.com, linus.walleij@linaro.org, linux-mm@kvack.org, maarten.lankhorst@linux.intel.com, mchehab@kernel.org, mm-commits@vger.kernel.org, mripard@kernel.org, mst@redhat.com, penberg@kernel.org, perex@perex.cz, rientjes@google.com, rric@kernel.org, sumit.semwal@linaro.org, tiwai@suse.com, tiwai@suse.de, tony.luck@intel.com, torvalds@linux-foundation.org, tzimmermann@suse.de, vbabka@suse.cz Subject: [patch 013/200] mm: slab: provide krealloc_array() Message-ID: <20201215030355.O2qo3ozcX%akpm@linux-foundation.org> In-Reply-To: <20201214190237.a17b70ae14f129e2dca3d204@linux-foundation.org> User-Agent: s-nail v14.8.16 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Bartosz Golaszewski Subject: mm: slab: provide krealloc_array() 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. While at it: add some documentation regarding krealloc. Link: https://lkml.kernel.org/r/20201109110654.12547-3-brgl@bgdev.pl Signed-off-by: Bartosz Golaszewski Acked-by: Vlastimil Babka Cc: Alexander Shishkin Cc: Andy Shevchenko Cc: Borislav Petkov Cc: Borislav Petkov Cc: Christian Knig Cc: Christoph Lameter Cc: Daniel Vetter Cc: Daniel Vetter Cc: David Airlie Cc: David Rientjes Cc: Gustavo Padovan Cc: James Morse Cc: Jaroslav Kysela Cc: Jason Wang Cc: Joonsoo Kim Cc: Linus Walleij Cc: Maarten Lankhorst Cc: Mauro Carvalho Chehab Cc: Maxime Ripard Cc: "Michael S . Tsirkin" Cc: Pekka Enberg Cc: Robert Richter Cc: Sumit Semwal Cc: Takashi Iwai Cc: Takashi Iwai Cc: Thomas Zimmermann Cc: Tony Luck Signed-off-by: Andrew Morton --- Documentation/core-api/memory-allocation.rst | 4 +++ include/linux/slab.h | 18 +++++++++++++++++ 2 files changed, 22 insertions(+) --- a/Documentation/core-api/memory-allocation.rst~mm-slab-provide-krealloc_array +++ a/Documentation/core-api/memory-allocation.rst @@ -147,6 +147,10 @@ The address of a chunk allocated with `k ARCH_KMALLOC_MINALIGN bytes. For sizes which are a power of two, the alignment is also guaranteed to be at least the respective size. +Chunks allocated with kmalloc() can be resized with krealloc(). Similarly +to kmalloc_array(): a helper for resizing arrays is provided in the form of +krealloc_array(). + For large allocations you can use vmalloc() and vzalloc(), or directly request pages from the page allocator. The memory allocated by `vmalloc` and related functions is not physically contiguous. --- a/include/linux/slab.h~mm-slab-provide-krealloc_array +++ a/include/linux/slab.h @@ -593,6 +593,24 @@ static inline void *kmalloc_array(size_t } /** + * krealloc_array - reallocate memory for an array. + * @p: pointer to the memory chunk to reallocate + * @new_n: new number of elements to alloc + * @new_size: new size of a single member of the array + * @flags: the type of memory to allocate (see kmalloc) + */ +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. * @size: element size. _