summaryrefslogtreecommitdiff
path: root/lib/assoc_array.c
diff options
context:
space:
mode:
authorKees Cook <kees@kernel.org>2026-02-20 23:49:23 -0800
committerKees Cook <kees@kernel.org>2026-02-21 01:02:28 -0800
commit69050f8d6d075dc01af7a5f2f550a8067510366f (patch)
treebb265f94d9dfa7876c06a5d9f88673d496a15341 /lib/assoc_array.c
parentd39a1d7486d98668dd34aaa6732aad7977c45f5a (diff)
treewide: Replace kmalloc with kmalloc_obj for non-scalar types
This is the result of running the Coccinelle script from scripts/coccinelle/api/kmalloc_objs.cocci. The script is designed to avoid scalar types (which need careful case-by-case checking), and instead replace kmalloc-family calls that allocate struct or union object instances: Single allocations: kmalloc(sizeof(TYPE), ...) are replaced with: kmalloc_obj(TYPE, ...) Array allocations: kmalloc_array(COUNT, sizeof(TYPE), ...) are replaced with: kmalloc_objs(TYPE, COUNT, ...) Flex array allocations: kmalloc(struct_size(PTR, FAM, COUNT), ...) are replaced with: kmalloc_flex(*PTR, FAM, COUNT, ...) (where TYPE may also be *VAR) The resulting allocations no longer return "void *", instead returning "TYPE *". Signed-off-by: Kees Cook <kees@kernel.org>
Diffstat (limited to 'lib/assoc_array.c')
-rw-r--r--lib/assoc_array.c32
1 files changed, 15 insertions, 17 deletions
diff --git a/lib/assoc_array.c b/lib/assoc_array.c
index 388e656ac974..6cd376ad5030 100644
--- a/lib/assoc_array.c
+++ b/lib/assoc_array.c
@@ -454,7 +454,7 @@ static bool assoc_array_insert_in_empty_tree(struct assoc_array_edit *edit)
pr_devel("-->%s()\n", __func__);
- new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
+ new_n0 = kzalloc_obj(struct assoc_array_node, GFP_KERNEL);
if (!new_n0)
return false;
@@ -536,11 +536,11 @@ static bool assoc_array_insert_into_terminal_node(struct assoc_array_edit *edit,
* those now. We may also need a new shortcut, but we deal with that
* when we need it.
*/
- new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
+ new_n0 = kzalloc_obj(struct assoc_array_node, GFP_KERNEL);
if (!new_n0)
return false;
edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
- new_n1 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
+ new_n1 = kzalloc_obj(struct assoc_array_node, GFP_KERNEL);
if (!new_n1)
return false;
edit->new_meta[1] = assoc_array_node_to_ptr(new_n1);
@@ -741,7 +741,7 @@ all_leaves_cluster_together:
keylen = round_up(diff, ASSOC_ARRAY_KEY_CHUNK_SIZE);
keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
- new_s0 = kzalloc(struct_size(new_s0, index_key, keylen), GFP_KERNEL);
+ new_s0 = kzalloc_flex(*new_s0, index_key, keylen, GFP_KERNEL);
if (!new_s0)
return false;
edit->new_meta[2] = assoc_array_shortcut_to_ptr(new_s0);
@@ -832,7 +832,7 @@ static bool assoc_array_insert_mid_shortcut(struct assoc_array_edit *edit,
edit->excised_meta[0] = assoc_array_shortcut_to_ptr(shortcut);
/* Create a new node now since we're going to need it anyway */
- new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
+ new_n0 = kzalloc_obj(struct assoc_array_node, GFP_KERNEL);
if (!new_n0)
return false;
edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
@@ -848,8 +848,7 @@ static bool assoc_array_insert_mid_shortcut(struct assoc_array_edit *edit,
keylen = round_up(diff, ASSOC_ARRAY_KEY_CHUNK_SIZE);
keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
- new_s0 = kzalloc(struct_size(new_s0, index_key, keylen),
- GFP_KERNEL);
+ new_s0 = kzalloc_flex(*new_s0, index_key, keylen, GFP_KERNEL);
if (!new_s0)
return false;
edit->new_meta[1] = assoc_array_shortcut_to_ptr(new_s0);
@@ -898,8 +897,7 @@ static bool assoc_array_insert_mid_shortcut(struct assoc_array_edit *edit,
keylen = round_up(shortcut->skip_to_level, ASSOC_ARRAY_KEY_CHUNK_SIZE);
keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
- new_s1 = kzalloc(struct_size(new_s1, index_key, keylen),
- GFP_KERNEL);
+ new_s1 = kzalloc_flex(*new_s1, index_key, keylen, GFP_KERNEL);
if (!new_s1)
return false;
edit->new_meta[2] = assoc_array_shortcut_to_ptr(new_s1);
@@ -977,7 +975,7 @@ struct assoc_array_edit *assoc_array_insert(struct assoc_array *array,
*/
BUG_ON(assoc_array_ptr_is_meta(object));
- edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
+ edit = kzalloc_obj(struct assoc_array_edit, GFP_KERNEL);
if (!edit)
return ERR_PTR(-ENOMEM);
edit->array = array;
@@ -1089,7 +1087,7 @@ struct assoc_array_edit *assoc_array_delete(struct assoc_array *array,
pr_devel("-->%s()\n", __func__);
- edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
+ edit = kzalloc_obj(struct assoc_array_edit, GFP_KERNEL);
if (!edit)
return ERR_PTR(-ENOMEM);
edit->array = array;
@@ -1206,7 +1204,8 @@ found_leaf:
node = parent;
/* Create a new node to collapse into */
- new_n0 = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
+ new_n0 = kzalloc_obj(struct assoc_array_node,
+ GFP_KERNEL);
if (!new_n0)
goto enomem;
edit->new_meta[0] = assoc_array_node_to_ptr(new_n0);
@@ -1281,7 +1280,7 @@ struct assoc_array_edit *assoc_array_clear(struct assoc_array *array,
if (!array->root)
return NULL;
- edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
+ edit = kzalloc_obj(struct assoc_array_edit, GFP_KERNEL);
if (!edit)
return ERR_PTR(-ENOMEM);
edit->array = array;
@@ -1469,7 +1468,7 @@ int assoc_array_gc(struct assoc_array *array,
if (!array->root)
return 0;
- edit = kzalloc(sizeof(struct assoc_array_edit), GFP_KERNEL);
+ edit = kzalloc_obj(struct assoc_array_edit, GFP_KERNEL);
if (!edit)
return -ENOMEM;
edit->array = array;
@@ -1490,8 +1489,7 @@ descend:
shortcut = assoc_array_ptr_to_shortcut(cursor);
keylen = round_up(shortcut->skip_to_level, ASSOC_ARRAY_KEY_CHUNK_SIZE);
keylen >>= ASSOC_ARRAY_KEY_CHUNK_SHIFT;
- new_s = kmalloc(struct_size(new_s, index_key, keylen),
- GFP_KERNEL);
+ new_s = kmalloc_flex(*new_s, index_key, keylen, GFP_KERNEL);
if (!new_s)
goto enomem;
pr_devel("dup shortcut %p -> %p\n", shortcut, new_s);
@@ -1505,7 +1503,7 @@ descend:
/* Duplicate the node at this position */
node = assoc_array_ptr_to_node(cursor);
- new_n = kzalloc(sizeof(struct assoc_array_node), GFP_KERNEL);
+ new_n = kzalloc_obj(struct assoc_array_node, GFP_KERNEL);
if (!new_n)
goto enomem;
pr_devel("dup node %p -> %p\n", node, new_n);