treewide: refresh kmalloc_obj() conversions

This is another run of the Coccinelle script for converting kmalloc()
family of allocations to kmalloc_obj() via the existing rules in
scripts/coccinelle/api/kmalloc_objs.cocci

This catches both the set of kmalloc() uses added since the first
kmalloc_obj() conversions in v7.0 and adds a large group missed in the
first pass due to Coccinelle not interacting well with the cleanup.h
scoped_...() family of macros[1]. I worked around this with spatch's
"--macro-file" argument to a file with all the scoped_...() macros mapped
to Coccinelle's YACFE_ITERATOR[2] as that was the closest viable control
flow indicator I could find.

Build tested allmodconfig on x86, arm64, arm, loongarch, mips, powerpc,
riscv, and s390 with no new warnings.

Link: https://lore.kernel.org/lkml/202609021314.8A9C0B8@keescook/ [1]
Link: https://github.com/coccinelle/coccinelle/blob/master/standard.h [2]
Signed-off-by: Kees Cook <kees+treewide@kernel.org>
This commit is contained in:
Kees Cook
2026-09-02 15:31:14 -07:00
committed by Kees Cook
parent 90feea391c
commit 3a2c4d55e3
304 changed files with 637 additions and 726 deletions

View File

@@ -266,7 +266,7 @@ static char *diag_fmt_alloc(struct bpf_verifier_env *env, size_t size)
}
capacity = max_t(size_t, BPF_DIAG_FMT_CHUNK_SIZE, size);
chunk = kmalloc(struct_size(chunk, data, capacity), GFP_KERNEL_ACCOUNT);
chunk = kmalloc_flex(*chunk, data, capacity, GFP_KERNEL_ACCOUNT);
if (!chunk)
return NULL;

View File

@@ -511,7 +511,7 @@ static int bpf_ma_set_dtor(struct bpf_map *map, struct bpf_mem_alloc *ma,
if (IS_ERR_OR_NULL(map->record))
return 0;
hrec = kzalloc(sizeof(*hrec), GFP_KERNEL);
hrec = kzalloc_obj(*hrec);
if (!hrec)
return -ENOMEM;
hrec->key_size = map->key_size;

View File

@@ -85,7 +85,7 @@ static struct func_instance *call_instance(struct bpf_verifier_env *env,
if (f)
return f;
f = kvzalloc(sizeof(*f), GFP_KERNEL_ACCOUNT);
f = kvzalloc_obj(*f, GFP_KERNEL_ACCOUNT);
if (!f)
return ERR_PTR(-ENOMEM);
f->callsite = lookup_key;

View File

@@ -862,7 +862,7 @@ struct bpf_verifier_log *bpf_log_attr_create_vlog(struct bpf_log_attr *attr_log,
if (!size)
return NULL;
log = kzalloc_obj(*log, GFP_KERNEL);
log = kzalloc_obj(*log);
if (!log)
return ERR_PTR(-ENOMEM);

View File

@@ -5483,7 +5483,7 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
bool priv_stack_supported;
int ret;
dinfo = kvcalloc(env->subprog_cnt, sizeof(*dinfo), GFP_KERNEL_ACCOUNT);
dinfo = kvzalloc_objs(*dinfo, env->subprog_cnt, GFP_KERNEL_ACCOUNT);
if (!dinfo)
return -ENOMEM;
@@ -20536,8 +20536,7 @@ static int process_fd_array_continuous(struct bpf_verifier_env *env,
return -E2BIG;
}
env->fd_array = kvcalloc(cnt, sizeof(*env->fd_array),
GFP_KERNEL_ACCOUNT);
env->fd_array = kvzalloc_objs(*env->fd_array, cnt, GFP_KERNEL_ACCOUNT);
if (!env->fd_array)
return -ENOMEM;
env->fd_array_cnt = cnt;

View File

@@ -51,8 +51,7 @@ struct dma_single_map_param {
static void *dma_single_map_benchmark_prepare(struct map_benchmark_data *map)
{
struct dma_single_map_param *params __free(kfree) = kzalloc(sizeof(*params),
GFP_KERNEL);
struct dma_single_map_param *params __free(kfree) = kzalloc_obj(*params);
if (!params)
return NULL;

View File

@@ -13558,9 +13558,8 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
return ERR_PTR(err);
if (has_addr_filter(event)) {
event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters,
sizeof(struct perf_addr_filter_range),
GFP_KERNEL);
event->addr_filter_ranges = kzalloc_objs(struct perf_addr_filter_range,
pmu->nr_addr_filters);
if (!event->addr_filter_ranges)
return ERR_PTR(-ENOMEM);

View File

@@ -1874,8 +1874,8 @@ static int futex_hash_allocate(unsigned int hash_slots, unsigned int flags)
free_percpu(ref);
}
fph = kvzalloc(struct_size(fph, queues, hash_slots),
GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
fph = kvzalloc_flex(*fph, queues, hash_slots,
GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
if (!fph)
return -ENOMEM;
@@ -2103,7 +2103,7 @@ static int __init futex_init(void)
size = sizeof(struct futex_hash_bucket) * hashsize;
order = get_order(size);
__futex_queues = kcalloc(nr_node_ids, sizeof(*__futex_queues), GFP_KERNEL);
__futex_queues = kzalloc_objs(*__futex_queues, nr_node_ids);
kmemleak_not_leak(__futex_queues);
runtime_const_init(shift, __futex_shift);

View File

@@ -2306,7 +2306,7 @@ int request_nmi(unsigned int irq, irq_handler_t handler,
!irq_supports_nmi(desc))
return -EINVAL;
action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
action = kzalloc_obj(struct irqaction);
if (!action)
return -ENOMEM;

View File

@@ -726,12 +726,11 @@ static int jump_label_add_module(struct module *mod)
if (static_key_sealed(key))
goto do_poke;
jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
jlm = kzalloc_obj(struct static_key_mod);
if (!jlm)
return -ENOMEM;
if (!static_key_linked(key)) {
jlm2 = kzalloc(sizeof(struct static_key_mod),
GFP_KERNEL);
jlm2 = kzalloc_obj(struct static_key_mod);
if (!jlm2) {
kfree(jlm);
return -ENOMEM;

View File

@@ -852,7 +852,7 @@ int kthread_affine_preferred(struct task_struct *p, const struct cpumask *mask)
if (!zalloc_cpumask_var(&affinity, GFP_KERNEL))
return -ENOMEM;
kthread->preferred_affinity = kzalloc(sizeof(struct cpumask), GFP_KERNEL);
kthread->preferred_affinity = kzalloc_obj(struct cpumask);
if (!kthread->preferred_affinity) {
ret = -ENOMEM;
goto out;

View File

@@ -98,16 +98,16 @@ static struct scx_cid_tables *scx_cid_alloc_tables(void)
u32 npossible = num_possible_cpus();
struct scx_cid_tables *tbls;
tbls = kzalloc_obj(*tbls, GFP_KERNEL);
tbls = kzalloc_obj(*tbls);
if (!tbls)
return NULL;
tbls->cid_to_cpu = kvcalloc(npossible, sizeof(*tbls->cid_to_cpu), GFP_KERNEL);
tbls->cpu_to_cid = kvcalloc(nr_cpu_ids, sizeof(*tbls->cpu_to_cid), GFP_KERNEL);
tbls->cid_to_shard = kvcalloc(npossible, sizeof(*tbls->cid_to_shard), GFP_KERNEL);
tbls->shard_node = kvcalloc(npossible, sizeof(*tbls->shard_node), GFP_KERNEL);
tbls->shard_ranges = kvcalloc(npossible, sizeof(*tbls->shard_ranges), GFP_KERNEL);
tbls->topo = kvcalloc(npossible, sizeof(*tbls->topo), GFP_KERNEL);
tbls->cid_to_cpu = kvzalloc_objs(*tbls->cid_to_cpu, npossible);
tbls->cpu_to_cid = kvzalloc_objs(*tbls->cpu_to_cid, nr_cpu_ids);
tbls->cid_to_shard = kvzalloc_objs(*tbls->cid_to_shard, npossible);
tbls->shard_node = kvzalloc_objs(*tbls->shard_node, npossible);
tbls->shard_ranges = kvzalloc_objs(*tbls->shard_ranges, npossible);
tbls->topo = kvzalloc_objs(*tbls->topo, npossible);
if (!tbls->cid_to_cpu || !tbls->cpu_to_cid || !tbls->cid_to_shard ||
!tbls->shard_node || !tbls->shard_ranges || !tbls->topo) {
@@ -490,7 +490,7 @@ __bpf_kfunc void scx_bpf_cid_override(const s32 *cpu_to_cid__arena, u32 cpu_to_c
* region that arena fault recovery covers.
*/
alloced = zalloc_cpumask_var(&seen, GFP_KERNEL);
node_counts = kcalloc(nr_node_ids, sizeof(*node_counts), GFP_KERNEL);
node_counts = kzalloc_objs(*node_counts, nr_node_ids);
if (cpu_to_cid_cnt == nr_cpu_ids)
cpu_to_cid = kmemdup(cpu_to_cid__arena, cpu_to_cid_cnt * sizeof(s32),
GFP_KERNEL);

View File

@@ -5449,7 +5449,7 @@ static ssize_t scx_attr_caps_show(struct kobject *kobj,
struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj);
u32 npossible = num_possible_cpus();
struct scx_cmask *agg __free(kfree) =
kzalloc(struct_size(agg, bits, SCX_CMASK_NR_WORDS(npossible)), GFP_KERNEL);
kzalloc_flex(*agg, bits, SCX_CMASK_NR_WORDS(npossible));
unsigned long *agg_bm __free(bitmap) = bitmap_zalloc(npossible, GFP_KERNEL);
ssize_t count = 0;
s32 cap, si;

View File

@@ -194,7 +194,7 @@ s32 scx_alloc_pshards(struct scx_sched *sch)
shard_node = rcu_dereference_protected(scx_shard_node,
lockdep_is_held(&scx_enable_mutex));
pshard = kzalloc_objs(pshard[0], scx_nr_cid_shards, GFP_KERNEL);
pshard = kzalloc_objs(pshard[0], scx_nr_cid_shards);
if (!pshard)
return -ENOMEM;

View File

@@ -945,7 +945,7 @@ int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter
if (num < 0)
return num;
addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
addrs = kzalloc_objs(*addrs, num);
if (!addrs)
return -ENOMEM;

View File

@@ -2600,8 +2600,8 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
cpu_buffer->remote = buffer->remote;
cpu_buffer->meta_page = (struct trace_buffer_meta *)(void *)desc->meta_va;
cpu_buffer->nr_pages = nr_pages;
cpu_buffer->subbuf_ids = kcalloc(cpu_buffer->nr_pages + 1,
sizeof(*cpu_buffer->subbuf_ids), GFP_KERNEL);
cpu_buffer->subbuf_ids = kzalloc_objs(*cpu_buffer->subbuf_ids,
cpu_buffer->nr_pages + 1);
if (!cpu_buffer->subbuf_ids)
goto fail_free_reader;

View File

@@ -930,7 +930,7 @@ static int __trace_eprobe_create(int argc, const char *argv[])
} else
ep->filter_str = NULL;
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
ctx = kzalloc_obj(*ctx);
if (!ctx)
return -ENOMEM;
ctx->event = ep->event;

View File

@@ -251,8 +251,8 @@ static int trace_remote_get(struct trace_remote *remote, int cpu)
if (cpu != RING_BUFFER_ALL_CPUS && !remote->pcpu_reader_locks) {
int lock_cpu;
remote->pcpu_reader_locks = kcalloc(nr_cpu_ids, sizeof(*remote->pcpu_reader_locks),
GFP_KERNEL);
remote->pcpu_reader_locks = kzalloc_objs(*remote->pcpu_reader_locks,
nr_cpu_ids);
if (!remote->pcpu_reader_locks) {
trace_remote_try_unload(remote);
return -ENOMEM;
@@ -324,7 +324,7 @@ static int __alloc_ring_buffer_iter(struct trace_remote_iterator *iter, int cpu)
return iter->rb_iter ? 0 : -ENOMEM;
}
iter->rb_iters = kcalloc(nr_cpu_ids, sizeof(*iter->rb_iters), GFP_KERNEL);
iter->rb_iters = kzalloc_objs(*iter->rb_iters, nr_cpu_ids);
if (!iter->rb_iters)
return -ENOMEM;
@@ -1204,7 +1204,7 @@ remote_events_dir_header_page_read(struct file *filp, char __user *ubuf, size_t
struct trace_seq *s;
int ret;
s = kmalloc(sizeof(*s), GFP_KERNEL);
s = kmalloc_obj(*s);
if (!s)
return -ENOMEM;
@@ -1227,7 +1227,7 @@ remote_events_dir_header_event_read(struct file *filp, char __user *ubuf, size_t
struct trace_seq *s;
int ret;
s = kmalloc(sizeof(*s), GFP_KERNEL);
s = kmalloc_obj(*s);
if (!s)
return -ENOMEM;