From: Bartosz Golaszewski <redacted>
Andy brought to my attention the fact that users allocating an array of
equally sized elements should check if the size multiplication doesn't
overflow. This is why we have helpers like kmalloc_array().
However we don't have krealloc_array() equivalent and there are many
users who do their own multiplication when calling krealloc() for arrays.
This series provides krealloc_array() and uses it in a couple places.
A separate series will follow adding devm_krealloc_array() which is
needed in the xilinx adc driver.
v1 -> v2:
- added a kernel doc for krealloc_array()
- mentioned krealloc et al in the docs
- collected review tags
Bartosz Golaszewski (8):
mm: slab: provide krealloc_array()
ALSA: pcm: use krealloc_array()
vhost: vringh: use krealloc_array()
pinctrl: use krealloc_array()
edac: ghes: use krealloc_array()
drm: atomic: use krealloc_array()
hwtracing: intel: use krealloc_array()
dma-buf: use krealloc_array()
Documentation/core-api/memory-allocation.rst | 4 ++++
drivers/dma-buf/sync_file.c | 4 ++--
drivers/edac/ghes_edac.c | 4 ++--
drivers/gpu/drm/drm_atomic.c | 3 ++-
drivers/hwtracing/intel_th/msu.c | 2 +-
drivers/pinctrl/pinctrl-utils.c | 2 +-
drivers/vhost/vringh.c | 3 ++-
include/linux/slab.h | 18 ++++++++++++++++++
sound/core/pcm_lib.c | 4 ++--
9 files changed, 34 insertions(+), 10 deletions(-)
--
2.29.1
From: Bartosz Golaszewski <redacted>
Use the helper that checks for overflows internally instead of manually
calculating the size of the new array.
Signed-off-by: Bartosz Golaszewski <redacted>
Reviewed-by: Takashi Iwai <redacted>
---
sound/core/pcm_lib.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
From: Bartosz Golaszewski <redacted>
Use the helper that checks for overflows internally instead of manually
calculating the size of the new array.
Signed-off-by: Bartosz Golaszewski <redacted>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
drivers/pinctrl/pinctrl-utils.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
From: Bartosz Golaszewski <redacted>
Use the helper that checks for overflows internally instead of manually
calculating the size of the new array.
Signed-off-by: Bartosz Golaszewski <redacted>
---
drivers/hwtracing/intel_th/msu.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
From: Bartosz Golaszewski <redacted>
Use the helper that checks for overflows internally instead of manually
calculating the size of the new array.
Signed-off-by: Bartosz Golaszewski <redacted>
Acked-by: Christian König <christian.koenig@amd.com>
---
drivers/dma-buf/sync_file.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
From: Bartosz Golaszewski <redacted>
Use the helper that checks for overflows internally instead of manually
calculating the size of the new array.
Signed-off-by: Bartosz Golaszewski <redacted>
Acked-by: Daniel Vetter <redacted>
---
drivers/gpu/drm/drm_atomic.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
From: Bartosz Golaszewski <redacted>
Use the helper that checks for overflows internally instead of manually
calculating the size of the new array.
Signed-off-by: Bartosz Golaszewski <redacted>
Acked-by: Borislav Petkov <redacted>
---
drivers/edac/ghes_edac.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
From: Bartosz Golaszewski <redacted>
Use the helper that checks for overflows internally instead of manually
calculating the size of the new array.
Signed-off-by: Bartosz Golaszewski <redacted>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
---
drivers/vhost/vringh.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
From: Bartosz Golaszewski <redacted>
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 <redacted>
Acked-by: Vlastimil Babka <redacted>
---
Documentation/core-api/memory-allocation.rst | 4 ++++
include/linux/slab.h | 18 ++++++++++++++++++
2 files changed, 22 insertions(+)
@@ -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.
From: Matthew Wilcox <willy@infradead.org> Date: 2020-11-02 15:41:39
On Mon, Nov 02, 2020 at 04:20:30PM +0100, Bartosz Golaszewski wrote:
+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`.
Is there any reason you chose to `do_this` instead of do_this()? The
automarkup script turns do_this() into a nice link to the documentation
which you're adding below.
Typo 'resising' resizing.
On Mon, Nov 2, 2020 at 4:41 PM Matthew Wilcox [off-list ref] wrote:
On Mon, Nov 02, 2020 at 04:20:30PM +0100, Bartosz Golaszewski wrote:
quoted
+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`.
Is there any reason you chose to `do_this` instead of do_this()? The
automarkup script turns do_this() into a nice link to the documentation
which you're adding below.
No, I just didn't know better. Thanks for bringing this to my attention.
From: Joe Perches <joe@perches.com> Date: 2020-11-03 04:14:27
On Mon, 2020-11-02 at 16:20 +0100, Bartosz Golaszewski wrote:
From: Bartosz Golaszewski <redacted>
Andy brought to my attention the fact that users allocating an array of
equally sized elements should check if the size multiplication doesn't
overflow. This is why we have helpers like kmalloc_array().
However we don't have krealloc_array() equivalent and there are many
users who do their own multiplication when calling krealloc() for arrays.
This series provides krealloc_array() and uses it in a couple places.
My concern about this is a possible assumption that __GFP_ZERO will
work, and as far as I know, it will not.
On Tue, Nov 3, 2020 at 5:14 AM Joe Perches [off-list ref] wrote:
On Mon, 2020-11-02 at 16:20 +0100, Bartosz Golaszewski wrote:
quoted
From: Bartosz Golaszewski <redacted>
Andy brought to my attention the fact that users allocating an array of
equally sized elements should check if the size multiplication doesn't
overflow. This is why we have helpers like kmalloc_array().
However we don't have krealloc_array() equivalent and there are many
users who do their own multiplication when calling krealloc() for arrays.
This series provides krealloc_array() and uses it in a couple places.
My concern about this is a possible assumption that __GFP_ZERO will
work, and as far as I know, it will not.
Yeah so I had this concern for devm_krealloc() and even sent a patch
that extended it to honor __GFP_ZERO before I noticed that regular
krealloc() silently ignores __GFP_ZERO. I'm not sure if this is on
purpose. Maybe we should either make krealloc() honor __GFP_ZERO or
explicitly state in its documentation that it ignores it?
This concern isn't really related to this patch as such - it's more of
a general krealloc() inconsistency.
Bartosz
From: Andy Shevchenko <hidden> Date: 2020-11-03 10:54:28
On Tue, Nov 3, 2020 at 12:13 PM Bartosz Golaszewski [off-list ref] wrote:
On Tue, Nov 3, 2020 at 5:14 AM Joe Perches [off-list ref] wrote:
quoted
On Mon, 2020-11-02 at 16:20 +0100, Bartosz Golaszewski wrote:
quoted
From: Bartosz Golaszewski <redacted>
Yeah so I had this concern for devm_krealloc() and even sent a patch
that extended it to honor __GFP_ZERO before I noticed that regular
krealloc() silently ignores __GFP_ZERO. I'm not sure if this is on
purpose. Maybe we should either make krealloc() honor __GFP_ZERO or
explicitly state in its documentation that it ignores it?
And my voice here is to ignore for the same reasons: respect
realloc(3) and making common sense with the idea of REallocating
(capital letters on purpose).
--
With Best Regards,
Andy Shevchenko