Skip to contentSkip to conversation

klp-build: Use kzalloc_objs() for object and function arrays

PATCH Applied Hungyu Lin 2 participants 2 messages Oct 20 – Nov 25 Live patching

lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 klp-build: Use kzalloc_objs() for object and function arrays

Allocate the object and function arrays with kzalloc_objs() so their element sizes are derived from the destination types and multiplication overflow is handled by the allocation helper.

Keep the extra zeroed entry used to terminate each array. The default allocation flags remain GFP_KERNEL.

---
 scripts/livepatch/init.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/livepatch/init.c +2 -2
index f14d8c8fb35f..908676d6d07d 100644
@@ -34,7 +34,7 @@ static int __init livepatch_mod_init(void)
 		goto err;
 	}
 
-	objs = kzalloc(sizeof(struct klp_object) * (nr_objs + 1),  GFP_KERNEL);
+	objs = kzalloc_objs(*objs, nr_objs + 1);
 	if (!objs) {
 		ret = -ENOMEM;
 		goto err_free_patch;
@@ -47,7 +47,7 @@ static int __init livepatch_mod_init(void)
 		struct klp_func *funcs = objs[i].funcs;
 		struct klp_object *obj = objs + i;
 
-		funcs = kzalloc(sizeof(struct klp_func) * (nr_funcs + 1), GFP_KERNEL);
+		funcs = kzalloc_objs(*funcs, nr_funcs + 1);
 		if (!funcs) {
 			ret = -ENOMEM;
 			for (int j = 0; j < i; j++)

base-commit: f663062d717eac2c01f77eaf321fa630528e672d

merge bot Applied
#1 lore raw
The following commit has been merged into the objtool/core branch of tip:

The following commit has been merged into the objtool/core branch of tip:

Commit-ID:     0397eb93362723345eb3a0ffbda0e19e654c0440
Gitweb:        https://git.kernel.org/tip/0397eb93362723345eb3a0ffbda0e19e654c0440
Author:        Hungyu Lin <[email protected]>
AuthorDate:    Tue, 20 Oct 2026 07:27:12 
Committer:     Josh Poimboeuf <[email protected]>
CommitterDate: Sat, 21 Nov 2026 23:38:32 -08:00

klp-build: Use kzalloc_objs() for object and function arrays

Allocate the object and function arrays with kzalloc_objs() so their element sizes are derived from the destination types and multiplication overflow is handled by the allocation helper.

Keep the extra zeroed entry used to terminate each array. The default allocation flags remain GFP_KERNEL.

---
 scripts/livepatch/init.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/livepatch/init.c +2 -2
index 16aff8f..df041fe 100644
@@ -34,7 +34,7 @@ static int __init livepatch_mod_init(void)
 		goto err;
 	}
 
-	objs = kzalloc(sizeof(struct klp_object) * (nr_objs + 1),  GFP_KERNEL);
+	objs = kzalloc_objs(*objs, nr_objs + 1);
 	if (!objs) {
 		ret = -ENOMEM;
 		goto err_free_patch;
@@ -47,7 +47,7 @@ static int __init livepatch_mod_init(void)
 		struct klp_func *funcs = objs[i].funcs;
 		struct klp_object *obj = objs + i;
 
-		funcs = kzalloc(sizeof(struct klp_func) * (nr_funcs + 1), GFP_KERNEL);
+		funcs = kzalloc_objs(*funcs, nr_funcs + 1);
 		if (!funcs) {
 			ret = -ENOMEM;
 			for (int j = 0; j < i; j++)