Skip to contentSkip to conversation

module/decompress: Avoid open-coded kvrealloc()

PATCH Applied Kees Cook 3 participants 3 messages Nov 19, 2025 – Dec 22, 2025 Module support

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 module/decompress: Avoid open-coded kvrealloc()

Replace open-coded allocate/copy with kvrealloc().

---
Cc: Petr Pavlu <[email protected]>
Cc: Luis Chamberlain <[email protected]>
Cc: Sami Tolvanen <[email protected]>
Cc: Daniel Gomez <[email protected]>
Cc: <[email protected]>

---
 kernel/module/decompress.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/module/decompress.c +5 -5
index 474e68f0f063..36f52a232a12 100644
@@ -17,16 +17,16 @@
 static int module_extend_max_pages(struct load_info *info, unsigned int extent)
 {
 	struct page **new_pages;
+	unsigned int new_max = info->max_pages + extent;
 
-	new_pages = kvmalloc_array(info->max_pages + extent,
-				   sizeof(info->pages), GFP_KERNEL);
+	new_pages = kvrealloc(info->pages,
+			      size_mul(new_max, sizeof(*info->pages)),
+			      GFP_KERNEL);
 	if (!new_pages)
 		return -ENOMEM;
 
-	memcpy(new_pages, info->pages, info->max_pages * sizeof(info->pages));
-	kvfree(info->pages);
 	info->pages = new_pages;
-	info->max_pages += extent;
+	info->max_pages = new_max;
 
 	return 0;
 }
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Kees Cook

Replace open-coded allocate/copy with kvrealloc().

---
Cc: Petr Pavlu <[email protected]>
Cc: Luis Chamberlain <[email protected]>
Cc: Sami Tolvanen <[email protected]>
Cc: Daniel Gomez <[email protected]>
Cc: <[email protected]>

---
 kernel/module/decompress.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/module/decompress.c +0 -0
index 474e68f0f063..36f52a232a12 100644

kernel/module/decompress.c line 24

Quoted 10 lines
kernel/module/decompress.c +4 -2
@@ -17,16 +17,16 @@
 static int module_extend_max_pages(struct load_info *info, unsigned int extent)
 {
 	struct page **new_pages;
+	unsigned int new_max = info->max_pages + extent;
 
-	new_pages = kvmalloc_array(info->max_pages + extent,
-				   sizeof(info->pages), GFP_KERNEL);
+	new_pages = kvrealloc(info->pages,
+			      size_mul(new_max, sizeof(*info->pages)),
+			      GFP_KERNEL);

Nit: Using the array_size() alias of size_mul() might be clearer in this context.

It looks ok to me in any case.

Signature · 2 lines
Thanks,
Petr
maintainer Applied
#2 lore raw
In reply to Kees Cook
Quoted 3 lines · Kees Cook

Replace open-coded allocate/copy with kvrealloc().

Applied to modules-next, thanks!

[1/1] module/decompress: Avoid open-coded kvrealloc()

      commit: 68e85558587e6bbb5c3ea3c8b4c71ab852e4b53e

Best regards,

Sami