All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
To: cip-dev@lists.cip-project.org,
	Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp>,
	Pavel Machek <pavel@denx.de>
Cc: Biju Das <biju.das.jz@bp.renesas.com>
Subject: [PATCH 5.10.y-cip 01/22] mm: slab: provide krealloc_array()
Date: Mon, 20 Dec 2021 13:31:18 +0000	[thread overview]
Message-ID: <20211220133139.21624-2-prabhakar.mahadev-lad.rj@bp.renesas.com> (raw)
In-Reply-To: <20211220133139.21624-1-prabhakar.mahadev-lad.rj@bp.renesas.com>

From: Bartosz Golaszewski <bgolaszewski@baylibre.com>

commit f0dbd2bd1c22c6670e83ddcd46a9beb8b575e86d upstream.

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 <bgolaszewski@baylibre.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Christian Knig <christian.koenig@amd.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: David Airlie <airlied@linux.ie>
Cc: David Rientjes <rientjes@google.com>
Cc: Gustavo Padovan <gustavo@padovan.org>
Cc: James Morse <james.morse@arm.com>
Cc: Jaroslav Kysela <perex@perex.cz>
Cc: Jason Wang <jasowang@redhat.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: "Michael S . Tsirkin" <mst@redhat.com>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Robert Richter <rric@kernel.org>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Takashi Iwai <tiwai@suse.com>
Cc: Takashi Iwai <tiwai@suse.de>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: Tony Luck <tony.luck@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
---
 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..5954ddf6ee13 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 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.
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.17.1



  reply	other threads:[~2021-12-20 13:31 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-20 13:31 [PATCH 5.10.y-cip 00/22] RZ/G2L: Add support for pinctrl/dmac/iic Lad Prabhakar
2021-12-20 13:31 ` Lad Prabhakar [this message]
2021-12-20 13:31 ` [PATCH 5.10.y-cip 02/22] clk: renesas: r9a07g044: Add GPIO clock and reset entries Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 03/22] dt-bindings: pinctrl: renesas: Add DT bindings for RZ/G2L pinctrl Lad Prabhakar
2021-12-22  9:45   ` Pavel Machek
2021-12-22 11:05     ` Prabhakar Mahadev Lad
2021-12-20 13:31 ` [PATCH 5.10.y-cip 04/22] pinctrl: renesas: Add RZ/G2L pin and gpio controller driver Lad Prabhakar
2021-12-22  9:48   ` Pavel Machek
2021-12-22 10:54     ` Prabhakar Mahadev Lad
2021-12-20 13:31 ` [PATCH 5.10.y-cip 05/22] pinctrl: renesas: rzg2l: Fix missing port register 21h Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 06/22] arm64: dts: renesas: rzg2l-smarc: Add scif0 pins Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 07/22] arm64: dts: renesas: r9a07g044: Add pinctrl node Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 08/22] clk: renesas: r9a07g044: Add I2C clocks/resets Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 09/22] dt-bindings: i2c: renesas,riic: Convert to json-schema Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 10/22] dt-bindings: i2c: renesas,riic: Document RZ/G2L I2C controller Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 11/22] arm64: dts: renesas: r9a07g044: Add I2C nodes Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 12/22] arm64: dts: renesas: r9a07g044: Add I2C interrupt-names Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 13/22] clk: renesas: r9a07g044: Add DMAC clocks/resets Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 14/22] dt-bindings: dma: Document RZ/G2L bindings Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 15/22] dmaengine: Extend the dma_slave_width for 128 bytes Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 16/22] dmaengine: sh: Add DMAC driver for RZ/G2L SoC Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 17/22] dmaengine: sh: Fix unused initialization of pointer lmdesc Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 18/22] dmaengine: sh: fix some NULL dereferences Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 19/22] dmaengine: sh: rz-dmac: Add DMA clock handling Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 20/22] dmaengine: sh: make array ds_lut static Lad Prabhakar
2021-12-22 10:04   ` Pavel Machek
2021-12-22 10:26     ` Prabhakar Mahadev Lad
2022-01-04 10:39     ` Biju Das
2021-12-20 13:31 ` [PATCH 5.10.y-cip 21/22] arm64: dts: renesas: r9a07g044: Add DMAC support Lad Prabhakar
2021-12-20 13:31 ` [PATCH 5.10.y-cip 22/22] arm64: defconfig: Enable RZ_DMAC Lad Prabhakar
2021-12-21  9:42 ` [PATCH 5.10.y-cip 00/22] RZ/G2L: Add support for pinctrl/dmac/iic Pavel Machek
2021-12-22 10:06 ` Pavel Machek
2021-12-22 10:19   ` Prabhakar Mahadev Lad
2021-12-22 10:23     ` [cip-dev] " Pavel Machek

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=20211220133139.21624-2-prabhakar.mahadev-lad.rj@bp.renesas.com \
    --to=prabhakar.mahadev-lad.rj@bp.renesas.com \
    --cc=biju.das.jz@bp.renesas.com \
    --cc=cip-dev@lists.cip-project.org \
    --cc=nobuhiro1.iwamatsu@toshiba.co.jp \
    --cc=pavel@denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.