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>
This commit is contained in:
Kees Cook
2026-02-20 23:49:23 -08:00
parent d39a1d7486
commit 69050f8d6d
8016 changed files with 20055 additions and 20913 deletions

View File

@@ -370,7 +370,7 @@ static int scmi_zones_register(struct device *dev,
unsigned int sp = 0, reg_zones = 0;
struct scmi_powercap_zone *spz, **zones_stack;
zones_stack = kcalloc(pr->num_zones, sizeof(spz), GFP_KERNEL);
zones_stack = kzalloc_objs(spz, pr->num_zones, GFP_KERNEL);
if (!zones_stack)
return -ENOMEM;

View File

@@ -418,7 +418,7 @@ static struct dtpm *dtpm_setup_virtual(const struct dtpm_node *hierarchy,
struct dtpm *dtpm;
int ret;
dtpm = kzalloc(sizeof(*dtpm), GFP_KERNEL);
dtpm = kzalloc_obj(*dtpm, GFP_KERNEL);
if (!dtpm)
return ERR_PTR(-ENOMEM);
dtpm_init(dtpm, NULL);

View File

@@ -212,7 +212,7 @@ static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
goto release_policy;
}
dtpm_cpu = kzalloc(sizeof(*dtpm_cpu), GFP_KERNEL);
dtpm_cpu = kzalloc_obj(*dtpm_cpu, GFP_KERNEL);
if (!dtpm_cpu) {
ret = -ENOMEM;
goto release_policy;

View File

@@ -160,7 +160,7 @@ static int __dtpm_devfreq_setup(struct devfreq *devfreq, struct dtpm *parent)
}
}
dtpm_devfreq = kzalloc(sizeof(*dtpm_devfreq), GFP_KERNEL);
dtpm_devfreq = kzalloc_obj(*dtpm_devfreq, GFP_KERNEL);
if (!dtpm_devfreq)
return -ENOMEM;

View File

@@ -1520,8 +1520,8 @@ static int rapl_detect_domains(struct rapl_package *rp)
}
pr_debug("found %d domains on %s\n", rp->nr_domains, rp->name);
rp->domains = kcalloc(rp->nr_domains, sizeof(struct rapl_domain),
GFP_KERNEL);
rp->domains = kzalloc_objs(struct rapl_domain, rp->nr_domains,
GFP_KERNEL);
if (!rp->domains)
return -ENOMEM;
@@ -2216,7 +2216,7 @@ struct rapl_package *rapl_add_package_cpuslocked(int id, struct rapl_if_priv *pr
struct rapl_package *rp;
int ret;
rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
rp = kzalloc_obj(struct rapl_package, GFP_KERNEL);
if (!rp)
return ERR_PTR(-ENOMEM);

View File

@@ -102,7 +102,7 @@ static struct tpmi_rapl_package *trp_alloc(int pkg_id)
}
}
trp = kzalloc(sizeof(*trp), GFP_KERNEL);
trp = kzalloc_obj(*trp, GFP_KERNEL);
if (!trp) {
ret = -ENOMEM;
goto err_del_powercap;

View File

@@ -501,7 +501,7 @@ struct powercap_zone *powercap_register_zone(
return ERR_PTR(-EINVAL);
memset(power_zone, 0, sizeof(*power_zone));
} else {
power_zone = kzalloc(sizeof(*power_zone), GFP_KERNEL);
power_zone = kzalloc_obj(*power_zone, GFP_KERNEL);
if (!power_zone)
return ERR_PTR(-ENOMEM);
power_zone->allocated = true;
@@ -529,9 +529,8 @@ struct powercap_zone *powercap_register_zone(
power_zone->name = kstrdup(name, GFP_KERNEL);
if (!power_zone->name)
goto err_name_alloc;
power_zone->constraints = kcalloc(nr_constraints,
sizeof(*power_zone->constraints),
GFP_KERNEL);
power_zone->constraints = kzalloc_objs(*power_zone->constraints,
nr_constraints, GFP_KERNEL);
if (!power_zone->constraints)
goto err_const_alloc;
@@ -614,7 +613,7 @@ struct powercap_control_type *powercap_register_control_type(
return ERR_PTR(-EINVAL);
memset(control_type, 0, sizeof(*control_type));
} else {
control_type = kzalloc(sizeof(*control_type), GFP_KERNEL);
control_type = kzalloc_obj(*control_type, GFP_KERNEL);
if (!control_type)
return ERR_PTR(-ENOMEM);
control_type->allocated = true;