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 /drivers/net/netdevsim | |
| 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 'drivers/net/netdevsim')
| -rw-r--r-- | drivers/net/netdevsim/bpf.c | 4 | ||||
| -rw-r--r-- | drivers/net/netdevsim/bus.c | 2 | ||||
| -rw-r--r-- | drivers/net/netdevsim/dev.c | 22 | ||||
| -rw-r--r-- | drivers/net/netdevsim/fib.c | 14 | ||||
| -rw-r--r-- | drivers/net/netdevsim/hwstats.c | 2 | ||||
| -rw-r--r-- | drivers/net/netdevsim/netdev.c | 5 | ||||
| -rw-r--r-- | drivers/net/netdevsim/psample.c | 2 |
7 files changed, 25 insertions, 26 deletions
diff --git a/drivers/net/netdevsim/bpf.c b/drivers/net/netdevsim/bpf.c index 5f17f68f3c08..badf08add94f 100644 --- a/drivers/net/netdevsim/bpf.c +++ b/drivers/net/netdevsim/bpf.c @@ -222,7 +222,7 @@ static int nsim_bpf_create_prog(struct nsim_dev *nsim_dev, char name[16]; int ret; - state = kzalloc(sizeof(*state), GFP_KERNEL); + state = kzalloc_obj(*state, GFP_KERNEL); if (!state) return -ENOMEM; @@ -501,7 +501,7 @@ nsim_bpf_map_alloc(struct netdevsim *ns, struct bpf_offloaded_map *offmap) if (offmap->map.map_flags) return -EINVAL; - nmap = kzalloc(sizeof(*nmap), GFP_KERNEL_ACCOUNT); + nmap = kzalloc_obj(*nmap, GFP_KERNEL_ACCOUNT); if (!nmap) return -ENOMEM; diff --git a/drivers/net/netdevsim/bus.c b/drivers/net/netdevsim/bus.c index d16b95304aa7..8de96fe6c02a 100644 --- a/drivers/net/netdevsim/bus.c +++ b/drivers/net/netdevsim/bus.c @@ -451,7 +451,7 @@ nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queu struct nsim_bus_dev *nsim_bus_dev; int err; - nsim_bus_dev = kzalloc(sizeof(*nsim_bus_dev), GFP_KERNEL); + nsim_bus_dev = kzalloc_obj(*nsim_bus_dev, GFP_KERNEL); if (!nsim_bus_dev) return ERR_PTR(-ENOMEM); diff --git a/drivers/net/netdevsim/dev.c b/drivers/net/netdevsim/dev.c index f7b32446d3b8..a227c0b9fe4c 100644 --- a/drivers/net/netdevsim/dev.c +++ b/drivers/net/netdevsim/dev.c @@ -267,8 +267,8 @@ static ssize_t nsim_bus_dev_max_vfs_write(struct file *file, if (val > NSIM_DEV_VF_PORT_INDEX_MAX - NSIM_DEV_VF_PORT_INDEX_BASE) return -ERANGE; - vfconfigs = kcalloc(val, sizeof(struct nsim_vf_config), - GFP_KERNEL | __GFP_NOWARN); + vfconfigs = kzalloc_objs(struct nsim_vf_config, val, + GFP_KERNEL | __GFP_NOWARN); if (!vfconfigs) return -ENOMEM; @@ -935,13 +935,13 @@ static int nsim_dev_traps_init(struct devlink *devlink) struct nsim_trap_data *nsim_trap_data; int err; - nsim_trap_data = kzalloc(sizeof(*nsim_trap_data), GFP_KERNEL); + nsim_trap_data = kzalloc_obj(*nsim_trap_data, GFP_KERNEL); if (!nsim_trap_data) return -ENOMEM; - nsim_trap_data->trap_items_arr = kcalloc(ARRAY_SIZE(nsim_traps_arr), - sizeof(struct nsim_trap_item), - GFP_KERNEL); + nsim_trap_data->trap_items_arr = kzalloc_objs(struct nsim_trap_item, + ARRAY_SIZE(nsim_traps_arr), + GFP_KERNEL); if (!nsim_trap_data->trap_items_arr) { err = -ENOMEM; goto err_trap_data_free; @@ -1348,7 +1348,7 @@ static int nsim_rate_node_new(struct devlink_rate *node, void **priv, return -EOPNOTSUPP; } - nsim_node = kzalloc(sizeof(*nsim_node), GFP_KERNEL); + nsim_node = kzalloc_obj(*nsim_node, GFP_KERNEL); if (!nsim_node) return -ENOMEM; @@ -1464,7 +1464,7 @@ static int __nsim_dev_port_add(struct nsim_dev *nsim_dev, enum nsim_dev_port_typ if (type == NSIM_DEV_PORT_TYPE_VF && !nsim_dev_get_vfs(nsim_dev)) return -EINVAL; - nsim_dev_port = kzalloc(sizeof(*nsim_dev_port), GFP_KERNEL); + nsim_dev_port = kzalloc_obj(*nsim_dev_port, GFP_KERNEL); if (!nsim_dev_port) return -ENOMEM; nsim_dev_port->port_index = nsim_dev_port_index(type, port_index); @@ -1652,9 +1652,9 @@ int nsim_drv_probe(struct nsim_bus_dev *nsim_bus_dev) dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev); - nsim_dev->vfconfigs = kcalloc(nsim_bus_dev->max_vfs, - sizeof(struct nsim_vf_config), - GFP_KERNEL | __GFP_NOWARN); + nsim_dev->vfconfigs = kzalloc_objs(struct nsim_vf_config, + nsim_bus_dev->max_vfs, + GFP_KERNEL | __GFP_NOWARN); if (!nsim_dev->vfconfigs) { err = -ENOMEM; goto err_devlink_unlock; diff --git a/drivers/net/netdevsim/fib.c b/drivers/net/netdevsim/fib.c index 16c382c42227..b1292a8c0ef1 100644 --- a/drivers/net/netdevsim/fib.c +++ b/drivers/net/netdevsim/fib.c @@ -277,7 +277,7 @@ nsim_fib4_rt_create(struct nsim_fib_data *data, { struct nsim_fib4_rt *fib4_rt; - fib4_rt = kzalloc(sizeof(*fib4_rt), GFP_KERNEL); + fib4_rt = kzalloc_obj(*fib4_rt, GFP_KERNEL); if (!fib4_rt) return NULL; @@ -497,7 +497,7 @@ static int nsim_fib6_rt_nh_add(struct nsim_fib6_rt *fib6_rt, { struct nsim_fib6_rt_nh *fib6_rt_nh; - fib6_rt_nh = kzalloc(sizeof(*fib6_rt_nh), GFP_KERNEL); + fib6_rt_nh = kzalloc_obj(*fib6_rt_nh, GFP_KERNEL); if (!fib6_rt_nh) return -ENOMEM; @@ -544,7 +544,7 @@ nsim_fib6_rt_create(struct nsim_fib_data *data, int i = 0; int err; - fib6_rt = kzalloc(sizeof(*fib6_rt), GFP_KERNEL); + fib6_rt = kzalloc_obj(*fib6_rt, GFP_KERNEL); if (!fib6_rt) return ERR_PTR(-ENOMEM); @@ -807,7 +807,7 @@ static int nsim_fib6_event_init(struct nsim_fib6_event *fib6_event, nrt6 = fen6_info->nsiblings + 1; - rt_arr = kcalloc(nrt6, sizeof(struct fib6_info *), GFP_ATOMIC); + rt_arr = kzalloc_objs(struct fib6_info *, nrt6, GFP_ATOMIC); if (!rt_arr) return -ENOMEM; @@ -987,7 +987,7 @@ static int nsim_fib_event_schedule_work(struct nsim_fib_data *data, */ return NOTIFY_DONE; - fib_event = kzalloc(sizeof(*fib_event), GFP_ATOMIC); + fib_event = kzalloc_obj(*fib_event, GFP_ATOMIC); if (!fib_event) goto err_fib_event_alloc; @@ -1116,7 +1116,7 @@ static struct nsim_nexthop *nsim_nexthop_create(struct nsim_fib_data *data, u64 occ = 0; int i; - nexthop = kzalloc(sizeof(*nexthop), GFP_KERNEL); + nexthop = kzalloc_obj(*nexthop, GFP_KERNEL); if (!nexthop) return ERR_PTR(-ENOMEM); @@ -1556,7 +1556,7 @@ struct nsim_fib_data *nsim_fib_create(struct devlink *devlink, struct nsim_dev *nsim_dev; int err; - data = kzalloc(sizeof(*data), GFP_KERNEL); + data = kzalloc_obj(*data, GFP_KERNEL); if (!data) return ERR_PTR(-ENOMEM); data->devlink = devlink; diff --git a/drivers/net/netdevsim/hwstats.c b/drivers/net/netdevsim/hwstats.c index 1abe48e35ca3..57b782aa1046 100644 --- a/drivers/net/netdevsim/hwstats.c +++ b/drivers/net/netdevsim/hwstats.c @@ -238,7 +238,7 @@ nsim_dev_hwstats_enable_ifindex(struct nsim_dev_hwstats *hwstats, goto out_unlock_list; } - hwsdev = kzalloc(sizeof(*hwsdev), GFP_KERNEL); + hwsdev = kzalloc_obj(*hwsdev, GFP_KERNEL); if (!hwsdev) { err = -ENOMEM; goto out_put_netdev; diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index 6285fbefe38a..5ec028a00c62 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -723,7 +723,7 @@ static struct nsim_rq *nsim_queue_alloc(void) { struct nsim_rq *rq; - rq = kzalloc(sizeof(*rq), GFP_KERNEL_ACCOUNT); + rq = kzalloc_obj(*rq, GFP_KERNEL_ACCOUNT); if (!rq) return NULL; @@ -997,8 +997,7 @@ static int nsim_queue_init(struct netdevsim *ns) struct net_device *dev = ns->netdev; int i; - ns->rq = kcalloc(dev->num_rx_queues, sizeof(*ns->rq), - GFP_KERNEL_ACCOUNT); + ns->rq = kzalloc_objs(*ns->rq, dev->num_rx_queues, GFP_KERNEL_ACCOUNT); if (!ns->rq) return -ENOMEM; diff --git a/drivers/net/netdevsim/psample.c b/drivers/net/netdevsim/psample.c index f0c6477dd0ae..39250b9d201f 100644 --- a/drivers/net/netdevsim/psample.c +++ b/drivers/net/netdevsim/psample.c @@ -200,7 +200,7 @@ int nsim_dev_psample_init(struct nsim_dev *nsim_dev) struct nsim_dev_psample *psample; int err; - psample = kzalloc(sizeof(*psample), GFP_KERNEL); + psample = kzalloc_obj(*psample, GFP_KERNEL); if (!psample) return -ENOMEM; nsim_dev->psample = psample; |
