summaryrefslogtreecommitdiff
path: root/arch/x86/kernel
diff options
context:
space:
mode:
authorKees Cook <kees@kernel.org>2026-02-20 23:49:23 -0800
committerKees Cook <kees@kernel.org>2026-02-21 01:02:28 -0800
commit69050f8d6d075dc01af7a5f2f550a8067510366f (patch)
treebb265f94d9dfa7876c06a5d9f88673d496a15341 /arch/x86/kernel
parentd39a1d7486d98668dd34aaa6732aad7977c45f5a (diff)
downloadlinux-next-69050f8d6d075dc01af7a5f2f550a8067510366f.tar.gz
linux-next-69050f8d6d075dc01af7a5f2f550a8067510366f.zip
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>
Diffstat (limited to 'arch/x86/kernel')
-rw-r--r--arch/x86/kernel/alternative.c2
-rw-r--r--arch/x86/kernel/amd_nb.c3
-rw-r--r--arch/x86/kernel/amd_node.c2
-rw-r--r--arch/x86/kernel/apic/io_apic.c2
-rw-r--r--arch/x86/kernel/apm_32.c2
-rw-r--r--arch/x86/kernel/cpu/amd_cache_disable.c2
-rw-r--r--arch/x86/kernel/cpu/mce/amd.c6
-rw-r--r--arch/x86/kernel/cpu/mce/core.c2
-rw-r--r--arch/x86/kernel/cpu/mce/dev-mcelog.c2
-rw-r--r--arch/x86/kernel/cpu/microcode/amd.c2
-rw-r--r--arch/x86/kernel/cpu/mtrr/generic.c2
-rw-r--r--arch/x86/kernel/cpu/mtrr/legacy.c2
-rw-r--r--arch/x86/kernel/cpu/sgx/driver.c2
-rw-r--r--arch/x86/kernel/cpu/sgx/encl.c4
-rw-r--r--arch/x86/kernel/cpu/sgx/ioctl.c2
-rw-r--r--arch/x86/kernel/cpu/sgx/main.c3
-rw-r--r--arch/x86/kernel/cpu/sgx/virt.c2
-rw-r--r--arch/x86/kernel/hpet.c4
-rw-r--r--arch/x86/kernel/ioport.c2
-rw-r--r--arch/x86/kernel/kdebugfs.c2
-rw-r--r--arch/x86/kernel/kexec-bzimage64.c2
-rw-r--r--arch/x86/kernel/ksysfs.c2
-rw-r--r--arch/x86/kernel/kvm.c2
-rw-r--r--arch/x86/kernel/ldt.c2
-rw-r--r--arch/x86/kernel/uprobes.c2
-rw-r--r--arch/x86/kernel/vm86_32.c2
26 files changed, 32 insertions, 30 deletions
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index 693b59b2f7d0..0ffc7de200b1 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -2160,7 +2160,7 @@ void __init_or_module alternatives_smp_module_add(struct module *mod,
/* Don't bother remembering, we'll never have to undo it. */
goto smp_unlock;
- smp = kzalloc(sizeof(*smp), GFP_KERNEL);
+ smp = kzalloc_obj(*smp, GFP_KERNEL);
if (NULL == smp)
/* we'll run the (safe but slow) SMP code then ... */
goto unlock;
diff --git a/arch/x86/kernel/amd_nb.c b/arch/x86/kernel/amd_nb.c
index c1acead6227a..852e8ff5ebd9 100644
--- a/arch/x86/kernel/amd_nb.c
+++ b/arch/x86/kernel/amd_nb.c
@@ -68,7 +68,8 @@ static int amd_cache_northbridges(void)
amd_northbridges.num = amd_num_nodes();
- nb = kcalloc(amd_northbridges.num, sizeof(struct amd_northbridge), GFP_KERNEL);
+ nb = kzalloc_objs(struct amd_northbridge, amd_northbridges.num,
+ GFP_KERNEL);
if (!nb)
return -ENOMEM;
diff --git a/arch/x86/kernel/amd_node.c b/arch/x86/kernel/amd_node.c
index 3d0a4768d603..2091cb1089a2 100644
--- a/arch/x86/kernel/amd_node.c
+++ b/arch/x86/kernel/amd_node.c
@@ -282,7 +282,7 @@ static int __init amd_smn_init(void)
return -ENODEV;
num_nodes = amd_num_nodes();
- amd_roots = kcalloc(num_nodes, sizeof(*amd_roots), GFP_KERNEL);
+ amd_roots = kzalloc_objs(*amd_roots, num_nodes, GFP_KERNEL);
if (!amd_roots)
return -ENOMEM;
diff --git a/arch/x86/kernel/apic/io_apic.c b/arch/x86/kernel/apic/io_apic.c
index 28f934f05a85..aa3675ba08bb 100644
--- a/arch/x86/kernel/apic/io_apic.c
+++ b/arch/x86/kernel/apic/io_apic.c
@@ -2876,7 +2876,7 @@ int mp_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
if (irq_resolve_mapping(domain, (irq_hw_number_t)pin))
return -EEXIST;
- data = kzalloc(sizeof(*data), GFP_KERNEL);
+ data = kzalloc_obj(*data, GFP_KERNEL);
if (!data)
return -ENOMEM;
diff --git a/arch/x86/kernel/apm_32.c b/arch/x86/kernel/apm_32.c
index 3175d7c134e9..13efc166bd3f 100644
--- a/arch/x86/kernel/apm_32.c
+++ b/arch/x86/kernel/apm_32.c
@@ -1576,7 +1576,7 @@ static int do_open(struct inode *inode, struct file *filp)
{
struct apm_user *as;
- as = kmalloc(sizeof(*as), GFP_KERNEL);
+ as = kmalloc_obj(*as, GFP_KERNEL);
if (as == NULL)
return -ENOMEM;
diff --git a/arch/x86/kernel/cpu/amd_cache_disable.c b/arch/x86/kernel/cpu/amd_cache_disable.c
index 8843b9557aea..13985d2f8b1d 100644
--- a/arch/x86/kernel/cpu/amd_cache_disable.c
+++ b/arch/x86/kernel/cpu/amd_cache_disable.c
@@ -255,7 +255,7 @@ static void init_amd_l3_attrs(void)
if (amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
n += 1;
- amd_l3_attrs = kcalloc(n, sizeof(*amd_l3_attrs), GFP_KERNEL);
+ amd_l3_attrs = kzalloc_objs(*amd_l3_attrs, n, GFP_KERNEL);
if (!amd_l3_attrs)
return;
diff --git a/arch/x86/kernel/cpu/mce/amd.c b/arch/x86/kernel/cpu/mce/amd.c
index 3f1dda355307..4e7a6101e7ed 100644
--- a/arch/x86/kernel/cpu/mce/amd.c
+++ b/arch/x86/kernel/cpu/mce/amd.c
@@ -1088,7 +1088,7 @@ static int allocate_threshold_blocks(unsigned int cpu, struct threshold_bank *tb
(high & MASK_LOCKED_HI))
goto recurse;
- b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
+ b = kzalloc_obj(struct threshold_block, GFP_KERNEL);
if (!b)
return -ENOMEM;
@@ -1147,7 +1147,7 @@ static int threshold_create_bank(struct threshold_bank **bp, unsigned int cpu,
if (!dev)
return -ENODEV;
- b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
+ b = kzalloc_obj(struct threshold_bank, GFP_KERNEL);
if (!b) {
err = -ENOMEM;
goto out;
@@ -1250,7 +1250,7 @@ void mce_threshold_create_device(unsigned int cpu)
return;
numbanks = this_cpu_read(mce_num_banks);
- bp = kcalloc(numbanks, sizeof(*bp), GFP_KERNEL);
+ bp = kzalloc_objs(*bp, numbanks, GFP_KERNEL);
if (!bp)
return;
diff --git a/arch/x86/kernel/cpu/mce/core.c b/arch/x86/kernel/cpu/mce/core.c
index 34440021e8cf..dd6a06ab5270 100644
--- a/arch/x86/kernel/cpu/mce/core.c
+++ b/arch/x86/kernel/cpu/mce/core.c
@@ -2692,7 +2692,7 @@ static int mce_device_create(unsigned int cpu)
if (dev)
return 0;
- dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+ dev = kzalloc_obj(*dev, GFP_KERNEL);
if (!dev)
return -ENOMEM;
dev->id = cpu;
diff --git a/arch/x86/kernel/cpu/mce/dev-mcelog.c b/arch/x86/kernel/cpu/mce/dev-mcelog.c
index 8d023239ce18..ec603e2c089a 100644
--- a/arch/x86/kernel/cpu/mce/dev-mcelog.c
+++ b/arch/x86/kernel/cpu/mce/dev-mcelog.c
@@ -338,7 +338,7 @@ static __init int dev_mcelog_init_device(void)
int err;
mce_log_len = max(MCE_LOG_MIN_LEN, num_online_cpus());
- mcelog = kzalloc(struct_size(mcelog, entry, mce_log_len), GFP_KERNEL);
+ mcelog = kzalloc_flex(*mcelog, entry, mce_log_len, GFP_KERNEL);
if (!mcelog)
return -ENOMEM;
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index caa0f595abcf..e58a73ae431b 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -1086,7 +1086,7 @@ static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover,
if (ret)
return ret;
- patch = kzalloc(sizeof(*patch), GFP_KERNEL);
+ patch = kzalloc_obj(*patch, GFP_KERNEL);
if (!patch) {
pr_err("Patch allocation failure.\n");
return -EINVAL;
diff --git a/arch/x86/kernel/cpu/mtrr/generic.c b/arch/x86/kernel/cpu/mtrr/generic.c
index 0863733858dc..d7db12c06950 100644
--- a/arch/x86/kernel/cpu/mtrr/generic.c
+++ b/arch/x86/kernel/cpu/mtrr/generic.c
@@ -410,7 +410,7 @@ void __init mtrr_copy_map(void)
mutex_lock(&mtrr_mutex);
- cache_map = kcalloc(new_size, sizeof(*cache_map), GFP_KERNEL);
+ cache_map = kzalloc_objs(*cache_map, new_size, GFP_KERNEL);
if (cache_map) {
memmove(cache_map, init_cache_map,
cache_map_n * sizeof(*cache_map));
diff --git a/arch/x86/kernel/cpu/mtrr/legacy.c b/arch/x86/kernel/cpu/mtrr/legacy.c
index 2415ffaaf02c..ee7bc7b5ce96 100644
--- a/arch/x86/kernel/cpu/mtrr/legacy.c
+++ b/arch/x86/kernel/cpu/mtrr/legacy.c
@@ -80,7 +80,7 @@ static struct syscore mtrr_syscore = {
void mtrr_register_syscore(void)
{
- mtrr_value = kcalloc(num_var_ranges, sizeof(*mtrr_value), GFP_KERNEL);
+ mtrr_value = kzalloc_objs(*mtrr_value, num_var_ranges, GFP_KERNEL);
/*
* The CPU has no MTRR and seems to not support SMP. They have
diff --git a/arch/x86/kernel/cpu/sgx/driver.c b/arch/x86/kernel/cpu/sgx/driver.c
index a42c7180900b..74fb59d96730 100644
--- a/arch/x86/kernel/cpu/sgx/driver.c
+++ b/arch/x86/kernel/cpu/sgx/driver.c
@@ -19,7 +19,7 @@ static int __sgx_open(struct inode *inode, struct file *file)
struct sgx_encl *encl;
int ret;
- encl = kzalloc(sizeof(*encl), GFP_KERNEL);
+ encl = kzalloc_obj(*encl, GFP_KERNEL);
if (!encl)
return -ENOMEM;
diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c
index cf149b9f4916..8b6c400c4008 100644
--- a/arch/x86/kernel/cpu/sgx/encl.c
+++ b/arch/x86/kernel/cpu/sgx/encl.c
@@ -854,7 +854,7 @@ int sgx_encl_mm_add(struct sgx_encl *encl, struct mm_struct *mm)
if (sgx_encl_find_mm(encl, mm))
return 0;
- encl_mm = kzalloc(sizeof(*encl_mm), GFP_KERNEL);
+ encl_mm = kzalloc_obj(*encl_mm, GFP_KERNEL);
if (!encl_mm)
return -ENOMEM;
@@ -1163,7 +1163,7 @@ struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
struct sgx_encl_page *encl_page;
unsigned long prot;
- encl_page = kzalloc(sizeof(*encl_page), GFP_KERNEL);
+ encl_page = kzalloc_obj(*encl_page, GFP_KERNEL);
if (!encl_page)
return ERR_PTR(-ENOMEM);
diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c
index 0bc36957979d..ef6674067d80 100644
--- a/arch/x86/kernel/cpu/sgx/ioctl.c
+++ b/arch/x86/kernel/cpu/sgx/ioctl.c
@@ -27,7 +27,7 @@ struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl, bool reclaim)
(SGX_ENCL_PAGE_VA_OFFSET_MASK >> 3) + 1);
if (!(encl->page_cnt % SGX_VA_SLOT_COUNT)) {
- va_page = kzalloc(sizeof(*va_page), GFP_KERNEL);
+ va_page = kzalloc_obj(*va_page, GFP_KERNEL);
if (!va_page)
return ERR_PTR(-ENOMEM);
diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index dc73194416ac..1021a4e33ac6 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -798,7 +798,8 @@ static bool __init sgx_page_cache_init(void)
int nid;
int i;
- sgx_numa_nodes = kmalloc_array(num_possible_nodes(), sizeof(*sgx_numa_nodes), GFP_KERNEL);
+ sgx_numa_nodes = kmalloc_objs(*sgx_numa_nodes, num_possible_nodes(),
+ GFP_KERNEL);
if (!sgx_numa_nodes)
return false;
diff --git a/arch/x86/kernel/cpu/sgx/virt.c b/arch/x86/kernel/cpu/sgx/virt.c
index 8de1f1a755f2..c7be8d0ea869 100644
--- a/arch/x86/kernel/cpu/sgx/virt.c
+++ b/arch/x86/kernel/cpu/sgx/virt.c
@@ -264,7 +264,7 @@ static int __sgx_vepc_open(struct inode *inode, struct file *file)
{
struct sgx_vepc *vepc;
- vepc = kzalloc(sizeof(struct sgx_vepc), GFP_KERNEL);
+ vepc = kzalloc_obj(struct sgx_vepc, GFP_KERNEL);
if (!vepc)
return -ENOMEM;
mutex_init(&vepc->lock);
diff --git a/arch/x86/kernel/hpet.c b/arch/x86/kernel/hpet.c
index d6387dde3ff9..43884229c421 100644
--- a/arch/x86/kernel/hpet.c
+++ b/arch/x86/kernel/hpet.c
@@ -544,7 +544,7 @@ static struct irq_domain *hpet_create_irq_domain(int hpet_id)
if (x86_vector_domain == NULL)
return NULL;
- domain_info = kzalloc(sizeof(*domain_info), GFP_KERNEL);
+ domain_info = kzalloc_obj(*domain_info, GFP_KERNEL);
if (!domain_info)
return NULL;
@@ -1038,7 +1038,7 @@ int __init hpet_enable(void)
if (IS_ENABLED(CONFIG_HPET_EMULATE_RTC) && channels < 2)
goto out_nohpet;
- hc = kcalloc(channels, sizeof(*hc), GFP_KERNEL);
+ hc = kzalloc_objs(*hc, channels, GFP_KERNEL);
if (!hc) {
pr_warn("Disabling HPET.\n");
goto out_nohpet;
diff --git a/arch/x86/kernel/ioport.c b/arch/x86/kernel/ioport.c
index ff40f09ad911..30ab130329dc 100644
--- a/arch/x86/kernel/ioport.c
+++ b/arch/x86/kernel/ioport.c
@@ -90,7 +90,7 @@ long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
/* No point to allocate a bitmap just to clear permissions */
if (!turn_on)
return 0;
- iobm = kmalloc(sizeof(*iobm), GFP_KERNEL);
+ iobm = kmalloc_obj(*iobm, GFP_KERNEL);
if (!iobm)
return -ENOMEM;
diff --git a/arch/x86/kernel/kdebugfs.c b/arch/x86/kernel/kdebugfs.c
index e2e89bebcbc3..4d7bf56524d8 100644
--- a/arch/x86/kernel/kdebugfs.c
+++ b/arch/x86/kernel/kdebugfs.c
@@ -102,7 +102,7 @@ static int __init create_setup_data_nodes(struct dentry *parent)
pa_data = boot_params.hdr.setup_data;
while (pa_data) {
- node = kmalloc(sizeof(*node), GFP_KERNEL);
+ node = kmalloc_obj(*node, GFP_KERNEL);
if (!node) {
error = -ENOMEM;
goto err_dir;
diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c
index 251edc5a040f..b7ba1ec486f9 100644
--- a/arch/x86/kernel/kexec-bzimage64.c
+++ b/arch/x86/kernel/kexec-bzimage64.c
@@ -682,7 +682,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
goto out_free_params;
/* Allocate loader specific data */
- ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL);
+ ldata = kzalloc_obj(struct bzimage64_data, GFP_KERNEL);
if (!ldata) {
ret = -ENOMEM;
goto out_free_params;
diff --git a/arch/x86/kernel/ksysfs.c b/arch/x86/kernel/ksysfs.c
index d547de9b3ed8..c5614b59be4a 100644
--- a/arch/x86/kernel/ksysfs.c
+++ b/arch/x86/kernel/ksysfs.c
@@ -344,7 +344,7 @@ static int __init create_setup_data_nodes(struct kobject *parent)
if (ret)
goto out_setup_data_kobj;
- kobjp = kmalloc_array(nr, sizeof(*kobjp), GFP_KERNEL);
+ kobjp = kmalloc_objs(*kobjp, nr, GFP_KERNEL);
if (!kobjp) {
ret = -ENOMEM;
goto out_setup_data_kobj;
diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
index 26ab6f8e36df..3bc062363814 100644
--- a/arch/x86/kernel/kvm.c
+++ b/arch/x86/kernel/kvm.c
@@ -226,7 +226,7 @@ again:
*/
if (!dummy) {
raw_spin_unlock(&b->lock);
- dummy = kzalloc(sizeof(*dummy), GFP_ATOMIC);
+ dummy = kzalloc_obj(*dummy, GFP_ATOMIC);
/*
* Continue looping on allocation failure, eventually
diff --git a/arch/x86/kernel/ldt.c b/arch/x86/kernel/ldt.c
index 0f19ef355f5f..40c5bf97dd5c 100644
--- a/arch/x86/kernel/ldt.c
+++ b/arch/x86/kernel/ldt.c
@@ -154,7 +154,7 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
if (num_entries > LDT_ENTRIES)
return NULL;
- new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL_ACCOUNT);
+ new_ldt = kmalloc_obj(struct ldt_struct, GFP_KERNEL_ACCOUNT);
if (!new_ldt)
return NULL;
diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c
index 619dddf54424..61d37f5a5ff9 100644
--- a/arch/x86/kernel/uprobes.c
+++ b/arch/x86/kernel/uprobes.c
@@ -696,7 +696,7 @@ static struct uprobe_trampoline *create_uprobe_trampoline(unsigned long vaddr)
if (IS_ERR_VALUE(vaddr))
return NULL;
- tramp = kzalloc(sizeof(*tramp), GFP_KERNEL);
+ tramp = kzalloc_obj(*tramp, GFP_KERNEL);
if (unlikely(!tramp))
return NULL;
diff --git a/arch/x86/kernel/vm86_32.c b/arch/x86/kernel/vm86_32.c
index e6cc84143f3e..3bb70763af9c 100644
--- a/arch/x86/kernel/vm86_32.c
+++ b/arch/x86/kernel/vm86_32.c
@@ -232,7 +232,7 @@ static long do_sys_vm86(struct vm86plus_struct __user *user_vm86, bool plus)
}
if (!vm86) {
- if (!(vm86 = kzalloc(sizeof(*vm86), GFP_KERNEL)))
+ if (!(vm86 = kzalloc_obj(*vm86, GFP_KERNEL)))
return -ENOMEM;
tsk->thread.vm86 = vm86;
}