History

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 *".

Kees Cook committed 8 months ago

Commit 69050f81 parent d39a1d78016 files · +20055 −20913Browse files

Showing the changes to kernel/workqueue.c (1 of 8016 files). Show all

Unified · Split

kernel/workqueue.c+9−9
@@ -4714,7 +4714,7 @@ struct workqueue_attrs *alloc_workqueue_attrs_noprof(void) · 4713 unmodified lines
47144714 {
47154715 struct workqueue_attrs *attrs;
47164716
4717− attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
4717+ attrs = kzalloc_obj(*attrs, GFP_KERNEL);
47184718 if (!attrs)
47194719 goto fail;
47204720 if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL))
@@ -5370,7 +5370,7 @@ apply_wqattrs_prepare(struct workqueue_struct *wq, · 649 unmodified lines
53705370 attrs->affn_scope >= WQ_AFFN_NR_TYPES))
53715371 return ERR_PTR(-EINVAL);
53725372
5373− ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL);
5373+ ctx = kzalloc_flex(*ctx, pwq_tbl, nr_cpu_ids, GFP_KERNEL);
53745374
53755375 new_attrs = alloc_workqueue_attrs();
53765376 if (!ctx || !new_attrs)
@@ -7486,7 +7486,7 @@ int workqueue_sysfs_register(struct workqueue_struct *wq) · 2109 unmodified lines
74867486 if (WARN_ON(wq->flags & __WQ_ORDERED))
74877487 return -EINVAL;
74887488
7489− wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
7489+ wq->wq_dev = wq_dev = kzalloc_obj(*wq_dev, GFP_KERNEL);
74907490 if (!wq_dev)
74917491 return -ENOMEM;
74927492
@@ -7879,9 +7879,9 @@ void __init workqueue_init_early(void) · 386 unmodified lines
78797879 wq_power_efficient = true;
78807880
78817881 /* initialize WQ_AFFN_SYSTEM pods */
7882− pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL);
7883− pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL);
7884− pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL);
7882+ pt->pod_cpus = kzalloc_objs(pt->pod_cpus[0], 1, GFP_KERNEL);
7883+ pt->pod_node = kzalloc_objs(pt->pod_node[0], 1, GFP_KERNEL);
7884+ pt->cpu_pod = kzalloc_objs(pt->cpu_pod[0], nr_cpu_ids, GFP_KERNEL);
78857885 BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod);
78867886
78877887 BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE));
@@ -8063,7 +8063,7 @@ static void __init init_pod_type(struct wq_pod_type *pt, · 175 unmodified lines
80638063 pt->nr_pods = 0;
80648064
80658065 /* init @pt->cpu_pod[] according to @cpus_share_pod() */
8066− pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL);
8066+ pt->cpu_pod = kzalloc_objs(pt->cpu_pod[0], nr_cpu_ids, GFP_KERNEL);
80678067 BUG_ON(!pt->cpu_pod);
80688068
80698069 for_each_possible_cpu(cur) {
@@ -8080,8 +8080,8 @@ static void __init init_pod_type(struct wq_pod_type *pt, · 10 unmodified lines
80808080 }
80818081
80828082 /* init the rest to match @pt->cpu_pod[] */
8083− pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL);
8084− pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL);
8083+ pt->pod_cpus = kzalloc_objs(pt->pod_cpus[0], pt->nr_pods, GFP_KERNEL);
8084+ pt->pod_node = kzalloc_objs(pt->pod_node[0], pt->nr_pods, GFP_KERNEL);
80858085 BUG_ON(!pt->pod_cpus || !pt->pod_node);
80868086
80878087 for (pod = 0; pod < pt->nr_pods; pod++)