History

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.

Kees Cook committed 2 months ago

Commit 3a2c4d51 parent 90feea3304 files · +637 −726Browse files

Showing the changes to kernel (18 of 304 files). Show all

Unified · Split

18 files
  1. kernel/bpf/diagnostics.c +1 −1
  2. kernel/bpf/hashtab.c +1 −1
  3. kernel/bpf/liveness.c +1 −1
  4. kernel/bpf/log.c +1 −1
  5. kernel/bpf/verifier.c +2 −3
  6. kernel/dma/map_benchmark.c +1 −2
  7. kernel/events/core.c +2 −3
  8. kernel/futex/core.c +3 −3
  9. kernel/irq/manage.c +1 −1
  10. kernel/jump_label.c +2 −3
  11. kernel/kthread.c +1 −1
  12. kernel/sched/ext/cid.c +8 −8
  13. kernel/sched/ext/ext.c +1 −1
  14. kernel/sched/ext/sub.c +1 −1
  15. kernel/trace/fprobe.c +1 −1
  16. kernel/trace/ring_buffer.c +2 −2
  17. kernel/trace/trace_eprobe.c +1 −1
  18. kernel/trace/trace_remote.c +5 −5
kernel/bpf/diagnostics.c+1−1
@@ -266,7 +266,7 @@ static char *diag_fmt_alloc(struct bpf_verifier_env *env, size_t size) · 265 unmodified lines
266266 }
267267
268268 capacity = max_t(size_t, BPF_DIAG_FMT_CHUNK_SIZE, size);
269− chunk = kmalloc(struct_size(chunk, data, capacity), GFP_KERNEL_ACCOUNT);
269+ chunk = kmalloc_flex(*chunk, data, capacity, GFP_KERNEL_ACCOUNT);
270270 if (!chunk)
271271 return NULL;
272272
kernel/bpf/hashtab.c+1−1
@@ -511,7 +511,7 @@ static int bpf_ma_set_dtor(struct bpf_map *map, struct bpf_mem_alloc *ma, · 510 unmodified lines
511511 if (IS_ERR_OR_NULL(map->record))
512512 return 0;
513513
514− hrec = kzalloc(sizeof(*hrec), GFP_KERNEL);
514+ hrec = kzalloc_obj(*hrec);
515515 if (!hrec)
516516 return -ENOMEM;
517517 hrec->key_size = map->key_size;
kernel/bpf/liveness.c+1−1
@@ -85,7 +85,7 @@ static struct func_instance *call_instance(struct bpf_verifier_env *env, · 84 unmodified lines
8585 if (f)
8686 return f;
8787
88− f = kvzalloc(sizeof(*f), GFP_KERNEL_ACCOUNT);
88+ f = kvzalloc_obj(*f, GFP_KERNEL_ACCOUNT);
8989 if (!f)
9090 return ERR_PTR(-ENOMEM);
9191 f->callsite = lookup_key;
kernel/bpf/log.c+1−1
@@ -862,7 +862,7 @@ struct bpf_verifier_log *bpf_log_attr_create_vlog(struct bpf_log_attr *attr_log, · 861 unmodified lines
862862 if (!size)
863863 return NULL;
864864
865− log = kzalloc_obj(*log, GFP_KERNEL);
865+ log = kzalloc_obj(*log);
866866 if (!log)
867867 return ERR_PTR(-ENOMEM);
868868
kernel/bpf/verifier.c+2−3
@@ -5483,7 +5483,7 @@ static int check_max_stack_depth(struct bpf_verifier_env *env) · 5482 unmodified lines
54835483 bool priv_stack_supported;
54845484 int ret;
54855485
5486− dinfo = kvcalloc(env->subprog_cnt, sizeof(*dinfo), GFP_KERNEL_ACCOUNT);
5486+ dinfo = kvzalloc_objs(*dinfo, env->subprog_cnt, GFP_KERNEL_ACCOUNT);
54875487 if (!dinfo)
54885488 return -ENOMEM;
54895489
@@ -20536,8 +20536,7 @@ static int process_fd_array_continuous(struct bpf_verifier_env *env, · 15046 unmodified lines
2053620536 return -E2BIG;
2053720537 }
2053820538
20539− env->fd_array = kvcalloc(cnt, sizeof(*env->fd_array),
20540− GFP_KERNEL_ACCOUNT);
20539+ env->fd_array = kvzalloc_objs(*env->fd_array, cnt, GFP_KERNEL_ACCOUNT);
2054120540 if (!env->fd_array)
2054220541 return -ENOMEM;
2054320542 env->fd_array_cnt = cnt;
kernel/dma/map_benchmark.c+1−2
@@ -51,8 +51,7 @@ struct dma_single_map_param { · 50 unmodified lines
5151
5252 static void *dma_single_map_benchmark_prepare(struct map_benchmark_data *map)
5353 {
54− struct dma_single_map_param *params __free(kfree) = kzalloc(sizeof(*params),
55− GFP_KERNEL);
54+ struct dma_single_map_param *params __free(kfree) = kzalloc_obj(*params);
5655 if (!params)
5756 return NULL;
5857
kernel/events/core.c+2−3
@@ -13558,9 +13558,8 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, · 13557 unmodified lines
1355813558 return ERR_PTR(err);
1355913559
1356013560 if (has_addr_filter(event)) {
13561− event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters,
13562− sizeof(struct perf_addr_filter_range),
13563− GFP_KERNEL);
13561+ event->addr_filter_ranges = kzalloc_objs(struct perf_addr_filter_range,
13562+ pmu->nr_addr_filters);
1356413563 if (!event->addr_filter_ranges)
1356513564 return ERR_PTR(-ENOMEM);
1356613565
kernel/futex/core.c+3−3
@@ -1874,8 +1874,8 @@ static int futex_hash_allocate(unsigned int hash_slots, unsigned int flags) · 1873 unmodified lines
18741874 free_percpu(ref);
18751875 }
18761876
1877− fph = kvzalloc(struct_size(fph, queues, hash_slots),
1878− GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
1877+ fph = kvzalloc_flex(*fph, queues, hash_slots,
1878+ GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
18791879 if (!fph)
18801880 return -ENOMEM;
18811881
@@ -2103,7 +2103,7 @@ static int __init futex_init(void) · 221 unmodified lines
21032103 size = sizeof(struct futex_hash_bucket) * hashsize;
21042104 order = get_order(size);
21052105
2106− __futex_queues = kcalloc(nr_node_ids, sizeof(*__futex_queues), GFP_KERNEL);
2106+ __futex_queues = kzalloc_objs(*__futex_queues, nr_node_ids);
21072107 kmemleak_not_leak(__futex_queues);
21082108
21092109 runtime_const_init(shift, __futex_shift);
kernel/irq/manage.c+1−1
@@ -2306,7 +2306,7 @@ int request_nmi(unsigned int irq, irq_handler_t handler, · 2305 unmodified lines
23062306 !irq_supports_nmi(desc))
23072307 return -EINVAL;
23082308
2309− action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2309+ action = kzalloc_obj(struct irqaction);
23102310 if (!action)
23112311 return -ENOMEM;
23122312
kernel/jump_label.c+2−3
@@ -726,12 +726,11 @@ static int jump_label_add_module(struct module *mod) · 725 unmodified lines
726726 if (static_key_sealed(key))
727727 goto do_poke;
728728
729− jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
729+ jlm = kzalloc_obj(struct static_key_mod);
730730 if (!jlm)
731731 return -ENOMEM;
732732 if (!static_key_linked(key)) {
733− jlm2 = kzalloc(sizeof(struct static_key_mod),
734− GFP_KERNEL);
733+ jlm2 = kzalloc_obj(struct static_key_mod);
735734 if (!jlm2) {
736735 kfree(jlm);
737736 return -ENOMEM;
kernel/kthread.c+1−1
@@ -852,7 +852,7 @@ int kthread_affine_preferred(struct task_struct *p, const struct cpumask *mask) · 851 unmodified lines
852852 if (!zalloc_cpumask_var(&affinity, GFP_KERNEL))
853853 return -ENOMEM;
854854
855− kthread->preferred_affinity = kzalloc(sizeof(struct cpumask), GFP_KERNEL);
855+ kthread->preferred_affinity = kzalloc_obj(struct cpumask);
856856 if (!kthread->preferred_affinity) {
857857 ret = -ENOMEM;
858858 goto out;
kernel/sched/ext/cid.c+8−8
@@ -98,16 +98,16 @@ static struct scx_cid_tables *scx_cid_alloc_tables(void) · 97 unmodified lines
9898 u32 npossible = num_possible_cpus();
9999 struct scx_cid_tables *tbls;
100100
101− tbls = kzalloc_obj(*tbls, GFP_KERNEL);
101+ tbls = kzalloc_obj(*tbls);
102102 if (!tbls)
103103 return NULL;
104104
105− tbls->cid_to_cpu = kvcalloc(npossible, sizeof(*tbls->cid_to_cpu), GFP_KERNEL);
106− tbls->cpu_to_cid = kvcalloc(nr_cpu_ids, sizeof(*tbls->cpu_to_cid), GFP_KERNEL);
107− tbls->cid_to_shard = kvcalloc(npossible, sizeof(*tbls->cid_to_shard), GFP_KERNEL);
108− tbls->shard_node = kvcalloc(npossible, sizeof(*tbls->shard_node), GFP_KERNEL);
109− tbls->shard_ranges = kvcalloc(npossible, sizeof(*tbls->shard_ranges), GFP_KERNEL);
110− tbls->topo = kvcalloc(npossible, sizeof(*tbls->topo), GFP_KERNEL);
105+ tbls->cid_to_cpu = kvzalloc_objs(*tbls->cid_to_cpu, npossible);
106+ tbls->cpu_to_cid = kvzalloc_objs(*tbls->cpu_to_cid, nr_cpu_ids);
107+ tbls->cid_to_shard = kvzalloc_objs(*tbls->cid_to_shard, npossible);
108+ tbls->shard_node = kvzalloc_objs(*tbls->shard_node, npossible);
109+ tbls->shard_ranges = kvzalloc_objs(*tbls->shard_ranges, npossible);
110+ tbls->topo = kvzalloc_objs(*tbls->topo, npossible);
111111
112112 if (!tbls->cid_to_cpu || !tbls->cpu_to_cid || !tbls->cid_to_shard ||
113113 !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 · 376 unmodified lines
490490 * region that arena fault recovery covers.
491491 */
492492 alloced = zalloc_cpumask_var(&seen, GFP_KERNEL);
493− node_counts = kcalloc(nr_node_ids, sizeof(*node_counts), GFP_KERNEL);
493+ node_counts = kzalloc_objs(*node_counts, nr_node_ids);
494494 if (cpu_to_cid_cnt == nr_cpu_ids)
495495 cpu_to_cid = kmemdup(cpu_to_cid__arena, cpu_to_cid_cnt * sizeof(s32),
496496 GFP_KERNEL);
kernel/sched/ext/ext.c+1−1
@@ -5449,7 +5449,7 @@ static ssize_t scx_attr_caps_show(struct kobject *kobj, · 5448 unmodified lines
54495449 struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj);
54505450 u32 npossible = num_possible_cpus();
54515451 struct scx_cmask *agg __free(kfree) =
5452− kzalloc(struct_size(agg, bits, SCX_CMASK_NR_WORDS(npossible)), GFP_KERNEL);
5452+ kzalloc_flex(*agg, bits, SCX_CMASK_NR_WORDS(npossible));
54535453 unsigned long *agg_bm __free(bitmap) = bitmap_zalloc(npossible, GFP_KERNEL);
54545454 ssize_t count = 0;
54555455 s32 cap, si;
kernel/sched/ext/sub.c+1−1
@@ -194,7 +194,7 @@ s32 scx_alloc_pshards(struct scx_sched *sch) · 193 unmodified lines
194194 shard_node = rcu_dereference_protected(scx_shard_node,
195195 lockdep_is_held(&scx_enable_mutex));
196196
197− pshard = kzalloc_objs(pshard[0], scx_nr_cid_shards, GFP_KERNEL);
197+ pshard = kzalloc_objs(pshard[0], scx_nr_cid_shards);
198198 if (!pshard)
199199 return -ENOMEM;
200200
kernel/trace/fprobe.c+1−1
@@ -945,7 +945,7 @@ int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter · 944 unmodified lines
945945 if (num < 0)
946946 return num;
947947
948− addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
948+ addrs = kzalloc_objs(*addrs, num);
949949 if (!addrs)
950950 return -ENOMEM;
951951
kernel/trace/ring_buffer.c+2−2
@@ -2600,8 +2600,8 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu) · 2599 unmodified lines
26002600 cpu_buffer->remote = buffer->remote;
26012601 cpu_buffer->meta_page = (struct trace_buffer_meta *)(void *)desc->meta_va;
26022602 cpu_buffer->nr_pages = nr_pages;
2603− cpu_buffer->subbuf_ids = kcalloc(cpu_buffer->nr_pages + 1,
2604− sizeof(*cpu_buffer->subbuf_ids), GFP_KERNEL);
2603+ cpu_buffer->subbuf_ids = kzalloc_objs(*cpu_buffer->subbuf_ids,
2604+ cpu_buffer->nr_pages + 1);
26052605 if (!cpu_buffer->subbuf_ids)
26062606 goto fail_free_reader;
26072607
kernel/trace/trace_eprobe.c+1−1
@@ -930,7 +930,7 @@ static int __trace_eprobe_create(int argc, const char *argv[]) · 929 unmodified lines
930930 } else
931931 ep->filter_str = NULL;
932932
933− ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
933+ ctx = kzalloc_obj(*ctx);
934934 if (!ctx)
935935 return -ENOMEM;
936936 ctx->event = ep->event;
kernel/trace/trace_remote.c+5−5
@@ -251,8 +251,8 @@ static int trace_remote_get(struct trace_remote *remote, int cpu) · 250 unmodified lines
251251 if (cpu != RING_BUFFER_ALL_CPUS && !remote->pcpu_reader_locks) {
252252 int lock_cpu;
253253
254− remote->pcpu_reader_locks = kcalloc(nr_cpu_ids, sizeof(*remote->pcpu_reader_locks),
255− GFP_KERNEL);
254+ remote->pcpu_reader_locks = kzalloc_objs(*remote->pcpu_reader_locks,
255+ nr_cpu_ids);
256256 if (!remote->pcpu_reader_locks) {
257257 trace_remote_try_unload(remote);
258258 return -ENOMEM;
@@ -324,7 +324,7 @@ static int __alloc_ring_buffer_iter(struct trace_remote_iterator *iter, int cpu) · 65 unmodified lines
324324 return iter->rb_iter ? 0 : -ENOMEM;
325325 }
326326
327− iter->rb_iters = kcalloc(nr_cpu_ids, sizeof(*iter->rb_iters), GFP_KERNEL);
327+ iter->rb_iters = kzalloc_objs(*iter->rb_iters, nr_cpu_ids);
328328 if (!iter->rb_iters)
329329 return -ENOMEM;
330330
@@ -1204,7 +1204,7 @@ remote_events_dir_header_page_read(struct file *filp, char __user *ubuf, size_t · 873 unmodified lines
12041204 struct trace_seq *s;
12051205 int ret;
12061206
1207− s = kmalloc(sizeof(*s), GFP_KERNEL);
1207+ s = kmalloc_obj(*s);
12081208 if (!s)
12091209 return -ENOMEM;
12101210
@@ -1227,7 +1227,7 @@ remote_events_dir_header_event_read(struct file *filp, char __user *ubuf, size_t · 16 unmodified lines
12271227 struct trace_seq *s;
12281228 int ret;
12291229
1230− s = kmalloc(sizeof(*s), GFP_KERNEL);
1230+ s = kmalloc_obj(*s);
12311231 if (!s)
12321232 return -ENOMEM;
12331233