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
266 }266 }
267 267
268 capacity = max_t(size_t, BPF_DIAG_FMT_CHUNK_SIZE, size);268 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);
270 if (!chunk)270 if (!chunk)
271 return NULL;271 return NULL;
272 272
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
511 if (IS_ERR_OR_NULL(map->record))511 if (IS_ERR_OR_NULL(map->record))
512 return 0;512 return 0;
513 513
514 hrec = kzalloc(sizeof(*hrec), GFP_KERNEL);514 hrec = kzalloc_obj(*hrec);
515 if (!hrec)515 if (!hrec)
516 return -ENOMEM;516 return -ENOMEM;
517 hrec->key_size = map->key_size;517 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
85 if (f)85 if (f)
86 return f;86 return f;
87 87
88 f = kvzalloc(sizeof(*f), GFP_KERNEL_ACCOUNT);88 f = kvzalloc_obj(*f, GFP_KERNEL_ACCOUNT);
89 if (!f)89 if (!f)
90 return ERR_PTR(-ENOMEM);90 return ERR_PTR(-ENOMEM);
91 f->callsite = lookup_key;91 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
862 if (!size)862 if (!size)
863 return NULL;863 return NULL;
864 864
865 log = kzalloc_obj(*log, GFP_KERNEL);865 log = kzalloc_obj(*log);
866 if (!log)866 if (!log)
867 return ERR_PTR(-ENOMEM);867 return ERR_PTR(-ENOMEM);
868 868
kernel/bpf/verifier.c+2−3
@@ -5483,7 +5483,7 @@ static int check_max_stack_depth(struct bpf_verifier_env *env) · 5482 unmodified lines
5483 bool priv_stack_supported;5483 bool priv_stack_supported;
5484 int ret;5484 int ret;
5485 5485
5486 dinfo = kvcalloc(env->subprog_cnt, sizeof(*dinfo), GFP_KERNEL_ACCOUNT);5486 dinfo = kvzalloc_objs(*dinfo, env->subprog_cnt, GFP_KERNEL_ACCOUNT);
5487 if (!dinfo)5487 if (!dinfo)
5488 return -ENOMEM;5488 return -ENOMEM;
5489 5489
@@ -20536,8 +20536,7 @@ static int process_fd_array_continuous(struct bpf_verifier_env *env, · 15046 unmodified lines
20536 return -E2BIG;20536 return -E2BIG;
20537 }20537 }
20538 20538
20539 env->fd_array = kvcalloc(cnt, sizeof(*env->fd_array),20539 env->fd_array = kvzalloc_objs(*env->fd_array, cnt, GFP_KERNEL_ACCOUNT);
20540 GFP_KERNEL_ACCOUNT);
20541 if (!env->fd_array)20540 if (!env->fd_array)
20542 return -ENOMEM;20541 return -ENOMEM;
20543 env->fd_array_cnt = cnt;20542 env->fd_array_cnt = cnt;
kernel/dma/map_benchmark.c+1−2
@@ -51,8 +51,7 @@ struct dma_single_map_param { · 50 unmodified lines
51 51
52static void *dma_single_map_benchmark_prepare(struct map_benchmark_data *map)52static void *dma_single_map_benchmark_prepare(struct map_benchmark_data *map)
53{53{
54 struct dma_single_map_param *params __free(kfree) = kzalloc(sizeof(*params),54 struct dma_single_map_param *params __free(kfree) = kzalloc_obj(*params);
55 GFP_KERNEL);
56 if (!params)55 if (!params)
57 return NULL;56 return NULL;
58 57
kernel/events/core.c+2−3
@@ -13558,9 +13558,8 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, · 13557 unmodified lines
13558 return ERR_PTR(err);13558 return ERR_PTR(err);
13559 13559
13560 if (has_addr_filter(event)) {13560 if (has_addr_filter(event)) {
13561 event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters,13561 event->addr_filter_ranges = kzalloc_objs(struct perf_addr_filter_range,
13562 sizeof(struct perf_addr_filter_range),13562 pmu->nr_addr_filters);
13563 GFP_KERNEL);
13564 if (!event->addr_filter_ranges)13563 if (!event->addr_filter_ranges)
13565 return ERR_PTR(-ENOMEM);13564 return ERR_PTR(-ENOMEM);
13566 13565
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
1874 free_percpu(ref);1874 free_percpu(ref);
1875 }1875 }
1876 1876
1877 fph = kvzalloc(struct_size(fph, queues, hash_slots),1877 fph = kvzalloc_flex(*fph, queues, hash_slots,
1878 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);1878 GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
1879 if (!fph)1879 if (!fph)
1880 return -ENOMEM;1880 return -ENOMEM;
1881 1881
@@ -2103,7 +2103,7 @@ static int __init futex_init(void) · 221 unmodified lines
2103 size = sizeof(struct futex_hash_bucket) * hashsize;2103 size = sizeof(struct futex_hash_bucket) * hashsize;
2104 order = get_order(size);2104 order = get_order(size);
2105 2105
2106 __futex_queues = kcalloc(nr_node_ids, sizeof(*__futex_queues), GFP_KERNEL);2106 __futex_queues = kzalloc_objs(*__futex_queues, nr_node_ids);
2107 kmemleak_not_leak(__futex_queues);2107 kmemleak_not_leak(__futex_queues);
2108 2108
2109 runtime_const_init(shift, __futex_shift);2109 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
2306 !irq_supports_nmi(desc))2306 !irq_supports_nmi(desc))
2307 return -EINVAL;2307 return -EINVAL;
2308 2308
2309 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);2309 action = kzalloc_obj(struct irqaction);
2310 if (!action)2310 if (!action)
2311 return -ENOMEM;2311 return -ENOMEM;
2312 2312
kernel/jump_label.c+2−3
@@ -726,12 +726,11 @@ static int jump_label_add_module(struct module *mod) · 725 unmodified lines
726 if (static_key_sealed(key))726 if (static_key_sealed(key))
727 goto do_poke;727 goto do_poke;
728 728
729 jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);729 jlm = kzalloc_obj(struct static_key_mod);
730 if (!jlm)730 if (!jlm)
731 return -ENOMEM;731 return -ENOMEM;
732 if (!static_key_linked(key)) {732 if (!static_key_linked(key)) {
733 jlm2 = kzalloc(sizeof(struct static_key_mod),733 jlm2 = kzalloc_obj(struct static_key_mod);
734 GFP_KERNEL);
735 if (!jlm2) {734 if (!jlm2) {
736 kfree(jlm);735 kfree(jlm);
737 return -ENOMEM;736 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
852 if (!zalloc_cpumask_var(&affinity, GFP_KERNEL))852 if (!zalloc_cpumask_var(&affinity, GFP_KERNEL))
853 return -ENOMEM;853 return -ENOMEM;
854 854
855 kthread->preferred_affinity = kzalloc(sizeof(struct cpumask), GFP_KERNEL);855 kthread->preferred_affinity = kzalloc_obj(struct cpumask);
856 if (!kthread->preferred_affinity) {856 if (!kthread->preferred_affinity) {
857 ret = -ENOMEM;857 ret = -ENOMEM;
858 goto out;858 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
98 u32 npossible = num_possible_cpus();98 u32 npossible = num_possible_cpus();
99 struct scx_cid_tables *tbls;99 struct scx_cid_tables *tbls;
100 100
101 tbls = kzalloc_obj(*tbls, GFP_KERNEL);101 tbls = kzalloc_obj(*tbls);
102 if (!tbls)102 if (!tbls)
103 return NULL;103 return NULL;
104 104
105 tbls->cid_to_cpu = kvcalloc(npossible, sizeof(*tbls->cid_to_cpu), GFP_KERNEL);105 tbls->cid_to_cpu = kvzalloc_objs(*tbls->cid_to_cpu, npossible);
106 tbls->cpu_to_cid = kvcalloc(nr_cpu_ids, sizeof(*tbls->cpu_to_cid), GFP_KERNEL);106 tbls->cpu_to_cid = kvzalloc_objs(*tbls->cpu_to_cid, nr_cpu_ids);
107 tbls->cid_to_shard = kvcalloc(npossible, sizeof(*tbls->cid_to_shard), GFP_KERNEL);107 tbls->cid_to_shard = kvzalloc_objs(*tbls->cid_to_shard, npossible);
108 tbls->shard_node = kvcalloc(npossible, sizeof(*tbls->shard_node), GFP_KERNEL);108 tbls->shard_node = kvzalloc_objs(*tbls->shard_node, npossible);
109 tbls->shard_ranges = kvcalloc(npossible, sizeof(*tbls->shard_ranges), GFP_KERNEL);109 tbls->shard_ranges = kvzalloc_objs(*tbls->shard_ranges, npossible);
110 tbls->topo = kvcalloc(npossible, sizeof(*tbls->topo), GFP_KERNEL);110 tbls->topo = kvzalloc_objs(*tbls->topo, npossible);
111 111
112 if (!tbls->cid_to_cpu || !tbls->cpu_to_cid || !tbls->cid_to_shard ||112 if (!tbls->cid_to_cpu || !tbls->cpu_to_cid || !tbls->cid_to_shard ||
113 !tbls->shard_node || !tbls->shard_ranges || !tbls->topo) {113 !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
490 * region that arena fault recovery covers.490 * region that arena fault recovery covers.
491 */491 */
492 alloced = zalloc_cpumask_var(&seen, GFP_KERNEL);492 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);
494 if (cpu_to_cid_cnt == nr_cpu_ids)494 if (cpu_to_cid_cnt == nr_cpu_ids)
495 cpu_to_cid = kmemdup(cpu_to_cid__arena, cpu_to_cid_cnt * sizeof(s32),495 cpu_to_cid = kmemdup(cpu_to_cid__arena, cpu_to_cid_cnt * sizeof(s32),
496 GFP_KERNEL);496 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
5449 struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj);5449 struct scx_sched *sch = container_of(kobj, struct scx_sched, kobj);
5450 u32 npossible = num_possible_cpus();5450 u32 npossible = num_possible_cpus();
5451 struct scx_cmask *agg __free(kfree) =5451 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));
5453 unsigned long *agg_bm __free(bitmap) = bitmap_zalloc(npossible, GFP_KERNEL);5453 unsigned long *agg_bm __free(bitmap) = bitmap_zalloc(npossible, GFP_KERNEL);
5454 ssize_t count = 0;5454 ssize_t count = 0;
5455 s32 cap, si;5455 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
194 shard_node = rcu_dereference_protected(scx_shard_node,194 shard_node = rcu_dereference_protected(scx_shard_node,
195 lockdep_is_held(&scx_enable_mutex));195 lockdep_is_held(&scx_enable_mutex));
196 196
197 pshard = kzalloc_objs(pshard[0], scx_nr_cid_shards, GFP_KERNEL);197 pshard = kzalloc_objs(pshard[0], scx_nr_cid_shards);
198 if (!pshard)198 if (!pshard)
199 return -ENOMEM;199 return -ENOMEM;
200 200
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
945 if (num < 0)945 if (num < 0)
946 return num;946 return num;
947 947
948 addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);948 addrs = kzalloc_objs(*addrs, num);
949 if (!addrs)949 if (!addrs)
950 return -ENOMEM;950 return -ENOMEM;
951 951
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
2600 cpu_buffer->remote = buffer->remote;2600 cpu_buffer->remote = buffer->remote;
2601 cpu_buffer->meta_page = (struct trace_buffer_meta *)(void *)desc->meta_va;2601 cpu_buffer->meta_page = (struct trace_buffer_meta *)(void *)desc->meta_va;
2602 cpu_buffer->nr_pages = nr_pages;2602 cpu_buffer->nr_pages = nr_pages;
2603 cpu_buffer->subbuf_ids = kcalloc(cpu_buffer->nr_pages + 1,2603 cpu_buffer->subbuf_ids = kzalloc_objs(*cpu_buffer->subbuf_ids,
2604 sizeof(*cpu_buffer->subbuf_ids), GFP_KERNEL);2604 cpu_buffer->nr_pages + 1);
2605 if (!cpu_buffer->subbuf_ids)2605 if (!cpu_buffer->subbuf_ids)
2606 goto fail_free_reader;2606 goto fail_free_reader;
2607 2607
kernel/trace/trace_eprobe.c+1−1
@@ -930,7 +930,7 @@ static int __trace_eprobe_create(int argc, const char *argv[]) · 929 unmodified lines
930 } else930 } else
931 ep->filter_str = NULL;931 ep->filter_str = NULL;
932 932
933 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);933 ctx = kzalloc_obj(*ctx);
934 if (!ctx)934 if (!ctx)
935 return -ENOMEM;935 return -ENOMEM;
936 ctx->event = ep->event;936 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
251 if (cpu != RING_BUFFER_ALL_CPUS && !remote->pcpu_reader_locks) {251 if (cpu != RING_BUFFER_ALL_CPUS && !remote->pcpu_reader_locks) {
252 int lock_cpu;252 int lock_cpu;
253 253
254 remote->pcpu_reader_locks = kcalloc(nr_cpu_ids, sizeof(*remote->pcpu_reader_locks),254 remote->pcpu_reader_locks = kzalloc_objs(*remote->pcpu_reader_locks,
255 GFP_KERNEL);255 nr_cpu_ids);
256 if (!remote->pcpu_reader_locks) {256 if (!remote->pcpu_reader_locks) {
257 trace_remote_try_unload(remote);257 trace_remote_try_unload(remote);
258 return -ENOMEM;258 return -ENOMEM;
@@ -324,7 +324,7 @@ static int __alloc_ring_buffer_iter(struct trace_remote_iterator *iter, int cpu) · 65 unmodified lines
324 return iter->rb_iter ? 0 : -ENOMEM;324 return iter->rb_iter ? 0 : -ENOMEM;
325 }325 }
326 326
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);
328 if (!iter->rb_iters)328 if (!iter->rb_iters)
329 return -ENOMEM;329 return -ENOMEM;
330 330
@@ -1204,7 +1204,7 @@ remote_events_dir_header_page_read(struct file *filp, char __user *ubuf, size_t · 873 unmodified lines
1204 struct trace_seq *s;1204 struct trace_seq *s;
1205 int ret;1205 int ret;
1206 1206
1207 s = kmalloc(sizeof(*s), GFP_KERNEL);1207 s = kmalloc_obj(*s);
1208 if (!s)1208 if (!s)
1209 return -ENOMEM;1209 return -ENOMEM;
1210 1210
@@ -1227,7 +1227,7 @@ remote_events_dir_header_event_read(struct file *filp, char __user *ubuf, size_t · 16 unmodified lines
1227 struct trace_seq *s;1227 struct trace_seq *s;
1228 int ret;1228 int ret;
1229 1229
1230 s = kmalloc(sizeof(*s), GFP_KERNEL);1230 s = kmalloc_obj(*s);
1231 if (!s)1231 if (!s)
1232 return -ENOMEM;1232 return -ENOMEM;
1233 1233