public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <brgl@bgdev•pl>
To: "Andy Shevchenko" <andriy.shevchenko@linux•intel.com>,
	"Sumit Semwal" <sumit.semwal@linaro•org>,
	"Gustavo Padovan" <gustavo@padovan•org>,
	"Christian König" <christian.koenig@amd•com>,
	"Mauro Carvalho Chehab" <mchehab@kernel•org>,
	"Borislav Petkov" <bp@alien8•de>,
	"Tony Luck" <tony.luck@intel•com>,
	"James Morse" <james.morse@arm•com>,
	"Robert Richter" <rric@kernel•org>,
	"Maarten Lankhorst" <maarten.lankhorst@linux•intel.com>,
	"Maxime Ripard" <mripard@kernel•org>,
	"Thomas Zimmermann" <tzimmermann@suse•de>,
	"David Airlie" <airlied@linux•ie>,
	"Daniel Vetter" <daniel@ffwll•ch>,
	"Alexander Shishkin" <alexander.shishkin@linux•intel.com>,
	"Linus Walleij" <linus.walleij@linaro•org>,
	"Michael S . Tsirkin" <mst@redhat•com>,
	"Jason Wang" <jasowang@redhat•com>,
	"Christoph Lameter" <cl@linux•com>,
	"Pekka Enberg" <penberg@kernel•org>,
	"David Rientjes" <rientjes@google•com>,
	"Joonsoo Kim" <iamjoonsoo.kim@lge•com>,
	"Andrew Morton" <akpm@linux-foundation•org>,
	"Jaroslav Kysela" <perex@perex•cz>,
	"Takashi Iwai" <tiwai@suse•com>
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 <bgolaszewski@baylibre•com>,
	Vlastimil Babka <vbabka@suse•cz>
Subject: [PATCH v2 1/8] mm: slab: provide krealloc_array()
Date: Mon,  2 Nov 2020 16:20:30 +0100	[thread overview]
Message-ID: <20201102152037.963-2-brgl@bgdev.pl> (raw)
In-Reply-To: <20201102152037.963-1-brgl@bgdev.pl>

From: Bartosz Golaszewski <bgolaszewski@baylibre•com>

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.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre•com>
Acked-by: Vlastimil Babka <vbabka@suse•cz>
---
 Documentation/core-api/memory-allocation.rst |  4 ++++
 include/linux/slab.h                         | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/Documentation/core-api/memory-allocation.rst b/Documentation/core-api/memory-allocation.rst
index 4446a1ac36cc..6dc38b40439a 100644
--- a/Documentation/core-api/memory-allocation.rst
+++ b/Documentation/core-api/memory-allocation.rst
@@ -147,6 +147,10 @@ The address of a chunk allocated with `kmalloc` is aligned to at least
 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 resising 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.
diff --git a/include/linux/slab.h b/include/linux/slab.h
index dd6897f62010..be4ba5867ac5 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -592,6 +592,24 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
 	return __kmalloc(bytes, flags);
 }
 
+/**
+ * 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.
-- 
2.29.1


  reply	other threads:[~2020-11-02 15:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-02 15:20 [PATCH v2 0/8] slab: provide and use krealloc_array() Bartosz Golaszewski
2020-11-02 15:20 ` Bartosz Golaszewski [this message]
2020-11-02 15:41   ` [PATCH v2 1/8] mm: slab: provide krealloc_array() Matthew Wilcox
2020-11-02 15:43     ` Bartosz Golaszewski
2020-11-02 15:20 ` [PATCH v2 2/8] ALSA: pcm: use krealloc_array() Bartosz Golaszewski
2020-11-02 15:20 ` [PATCH v2 3/8] vhost: vringh: " Bartosz Golaszewski
2020-11-02 15:20 ` [PATCH v2 4/8] pinctrl: " Bartosz Golaszewski
2020-11-02 15:20 ` [PATCH v2 5/8] edac: ghes: " Bartosz Golaszewski
2020-11-02 15:20 ` [PATCH v2 6/8] drm: atomic: " Bartosz Golaszewski
2020-11-02 15:20 ` [PATCH v2 7/8] hwtracing: intel: " Bartosz Golaszewski
2020-11-02 15:20 ` [PATCH v2 8/8] dma-buf: " Bartosz Golaszewski
2020-11-02 16:10   ` Andy Shevchenko
2020-11-03  4:14 ` [PATCH v2 0/8] slab: provide and " Joe Perches
2020-11-03 10:12   ` Bartosz Golaszewski
2020-11-03 10:55     ` Andy Shevchenko

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=20201102152037.963-2-brgl@bgdev.pl \
    --to=brgl@bgdev$(echo .)pl \
    --cc=airlied@linux$(echo .)ie \
    --cc=akpm@linux-foundation$(echo .)org \
    --cc=alexander.shishkin@linux$(echo .)intel.com \
    --cc=alsa-devel@alsa-project$(echo .)org \
    --cc=andriy.shevchenko@linux$(echo .)intel.com \
    --cc=bgolaszewski@baylibre$(echo .)com \
    --cc=bp@alien8$(echo .)de \
    --cc=christian.koenig@amd$(echo .)com \
    --cc=cl@linux$(echo .)com \
    --cc=daniel@ffwll$(echo .)ch \
    --cc=dri-devel@lists$(echo .)freedesktop.org \
    --cc=gustavo@padovan$(echo .)org \
    --cc=iamjoonsoo.kim@lge$(echo .)com \
    --cc=james.morse@arm$(echo .)com \
    --cc=jasowang@redhat$(echo .)com \
    --cc=kvm@vger$(echo .)kernel.org \
    --cc=linaro-mm-sig@lists$(echo .)linaro.org \
    --cc=linus.walleij@linaro$(echo .)org \
    --cc=linux-edac@vger$(echo .)kernel.org \
    --cc=linux-gpio@vger$(echo .)kernel.org \
    --cc=linux-kernel@vger$(echo .)kernel.org \
    --cc=linux-media@vger$(echo .)kernel.org \
    --cc=linux-mm@kvack$(echo .)org \
    --cc=maarten.lankhorst@linux$(echo .)intel.com \
    --cc=mchehab@kernel$(echo .)org \
    --cc=mripard@kernel$(echo .)org \
    --cc=mst@redhat$(echo .)com \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=penberg@kernel$(echo .)org \
    --cc=perex@perex$(echo .)cz \
    --cc=rientjes@google$(echo .)com \
    --cc=rric@kernel$(echo .)org \
    --cc=sumit.semwal@linaro$(echo .)org \
    --cc=tiwai@suse$(echo .)com \
    --cc=tony.luck@intel$(echo .)com \
    --cc=tzimmermann@suse$(echo .)de \
    --cc=vbabka@suse$(echo .)cz \
    --cc=virtualization@lists$(echo .)linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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