diff options
| author | Kees Cook <kees@kernel.org> | 2026-02-20 23:49:23 -0800 |
|---|---|---|
| committer | Kees Cook <kees@kernel.org> | 2026-02-21 01:02:28 -0800 |
| commit | 69050f8d6d075dc01af7a5f2f550a8067510366f (patch) | |
| tree | bb265f94d9dfa7876c06a5d9f88673d496a15341 /net/netlink | |
| parent | d39a1d7486d98668dd34aaa6732aad7977c45f5a (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 'net/netlink')
| -rw-r--r-- | net/netlink/af_netlink.c | 2 | ||||
| -rw-r--r-- | net/netlink/diag.c | 2 | ||||
| -rw-r--r-- | net/netlink/genetlink.c | 9 | ||||
| -rw-r--r-- | net/netlink/policy.c | 4 |
4 files changed, 8 insertions, 9 deletions
diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c index 8e5151f0c6e4..2d91b8b8ba9a 100644 --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -2927,7 +2927,7 @@ static int __init netlink_proto_init(void) BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof_field(struct sk_buff, cb)); - nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL); + nl_table = kzalloc_objs(*nl_table, MAX_LINKS, GFP_KERNEL); if (!nl_table) goto panic; diff --git a/net/netlink/diag.c b/net/netlink/diag.c index b8e58132e8af..25e930a64d07 100644 --- a/net/netlink/diag.c +++ b/net/netlink/diag.c @@ -107,7 +107,7 @@ static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, num--; if (!hti) { - hti = kmalloc(sizeof(*hti), GFP_KERNEL); + hti = kmalloc_obj(*hti, GFP_KERNEL); if (!hti) return -ENOMEM; diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c index 978c129c6095..ac1fdf7d7327 100644 --- a/net/netlink/genetlink.c +++ b/net/netlink/genetlink.c @@ -659,7 +659,7 @@ static int genl_sk_privs_alloc(struct genl_family *family) if (!family->sock_priv_size) return 0; - family->sock_privs = kzalloc(sizeof(*family->sock_privs), GFP_KERNEL); + family->sock_privs = kzalloc_obj(*family->sock_privs, GFP_KERNEL); if (!family->sock_privs) return -ENOMEM; xa_init(family->sock_privs); @@ -912,7 +912,7 @@ EXPORT_SYMBOL(genlmsg_put); static struct genl_dumpit_info *genl_dumpit_info_alloc(void) { - return kmalloc(sizeof(struct genl_dumpit_info), GFP_KERNEL); + return kmalloc_obj(struct genl_dumpit_info, GFP_KERNEL); } static void genl_dumpit_info_free(const struct genl_dumpit_info *info) @@ -937,8 +937,7 @@ genl_family_rcv_msg_attrs_parse(const struct genl_family *family, if (!ops->maxattr) return NULL; - attrbuf = kmalloc_array(ops->maxattr + 1, - sizeof(struct nlattr *), GFP_KERNEL); + attrbuf = kmalloc_objs(struct nlattr *, ops->maxattr + 1, GFP_KERNEL); if (!attrbuf) return ERR_PTR(-ENOMEM); @@ -1591,7 +1590,7 @@ static int ctrl_dumppolicy_start(struct netlink_callback *cb) return 0; } - ctx->op_iter = kmalloc(sizeof(*ctx->op_iter), GFP_KERNEL); + ctx->op_iter = kmalloc_obj(*ctx->op_iter, GFP_KERNEL); if (!ctx->op_iter) return -ENOMEM; diff --git a/net/netlink/policy.c b/net/netlink/policy.c index 99458da6be32..a3e818c7c0cf 100644 --- a/net/netlink/policy.c +++ b/net/netlink/policy.c @@ -102,8 +102,8 @@ static struct netlink_policy_dump_state *alloc_state(void) { struct netlink_policy_dump_state *state; - state = kzalloc(struct_size(state, policies, INITIAL_POLICIES_ALLOC), - GFP_KERNEL); + state = kzalloc_flex(*state, policies, INITIAL_POLICIES_ALLOC, + GFP_KERNEL); if (!state) return ERR_PTR(-ENOMEM); state->n_alloc = INITIAL_POLICIES_ALLOC; |
