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

@@ -124,7 +124,7 @@ static int cp2615_check_status(enum cp2615_i2c_status status)
static int
cp2615_i2c_send(struct usb_interface *usbif, struct cp2615_i2c_transfer *i2c_w)
{
struct cp2615_iop_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
struct cp2615_iop_msg *msg = kzalloc_obj(*msg, GFP_KERNEL);
struct usb_device *usbdev = interface_to_usbdev(usbif);
int res = cp2615_init_i2c_msg(msg, i2c_w);
@@ -143,7 +143,7 @@ cp2615_i2c_recv(struct usb_interface *usbif, unsigned char tag, void *buf)
struct cp2615_i2c_transfer_result *i2c_r;
int res;
msg = kzalloc(sizeof(*msg), GFP_KERNEL);
msg = kzalloc_obj(*msg, GFP_KERNEL);
if (!msg)
return -ENOMEM;
@@ -171,7 +171,7 @@ cp2615_i2c_recv(struct usb_interface *usbif, unsigned char tag, void *buf)
/* Checks if the IOP is functional by querying the part's ID */
static int cp2615_check_iop(struct usb_interface *usbif)
{
struct cp2615_iop_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
struct cp2615_iop_msg *msg = kzalloc_obj(*msg, GFP_KERNEL);
struct cp2615_iop_accessory_info *info = (struct cp2615_iop_accessory_info *)&msg->data;
struct usb_device *usbdev = interface_to_usbdev(usbif);
int res = cp2615_init_iop_msg(msg, iop_GetAccessoryInfo, NULL, 0);

View File

@@ -636,7 +636,7 @@ static int cpm_i2c_probe(struct platform_device *ofdev)
struct cpm_i2c *cpm;
const u32 *data;
cpm = kzalloc(sizeof(struct cpm_i2c), GFP_KERNEL);
cpm = kzalloc_obj(struct cpm_i2c, GFP_KERNEL);
if (!cpm)
return -ENOMEM;

View File

@@ -93,7 +93,7 @@ static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
int status, ret;
/* Allocate command-response buffer */
req = kzalloc(sizeof(*req), GFP_KERNEL);
req = kzalloc_obj(*req, GFP_KERNEL);
if (!req)
return -ENOMEM;

View File

@@ -445,7 +445,7 @@ static int diolan_u2c_probe(struct usb_interface *interface,
return -ENODEV;
/* allocate memory for our device state and initialize it */
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
dev = kzalloc_obj(*dev, GFP_KERNEL);
if (dev == NULL) {
ret = -ENOMEM;
goto error;

View File

@@ -720,7 +720,7 @@ static int pch_i2c_probe(struct pci_dev *pdev,
pch_pci_dbg(pdev, "Entered.\n");
adap_info = kzalloc((sizeof(struct adapter_info)), GFP_KERNEL);
adap_info = kzalloc_obj(struct adapter_info, GFP_KERNEL);
if (adap_info == NULL)
return -ENOMEM;

View File

@@ -707,7 +707,7 @@ static int fsi_i2c_probe(struct fsi_device *fsi_dev)
if (!of_device_is_available(np))
continue;
port = kzalloc(sizeof(*port), GFP_KERNEL);
port = kzalloc_obj(*port, GFP_KERNEL);
if (!port) {
of_node_put(np);
break;

View File

@@ -365,7 +365,7 @@ static int highlander_i2c_probe(struct platform_device *pdev)
return -ENODEV;
}
dev = kzalloc(sizeof(struct highlander_i2c_dev), GFP_KERNEL);
dev = kzalloc_obj(struct highlander_i2c_dev, GFP_KERNEL);
if (unlikely(!dev))
return -ENOMEM;

View File

@@ -687,7 +687,7 @@ static int iic_probe(struct platform_device *ofdev)
const u32 *freq;
int ret;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
dev = kzalloc_obj(*dev, GFP_KERNEL);
if (!dev)
return -ENOMEM;

View File

@@ -415,13 +415,13 @@ iop3xx_i2c_probe(struct platform_device *pdev)
struct i2c_adapter *new_adapter;
struct i2c_algo_iop3xx_data *adapter_data;
new_adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
new_adapter = kzalloc_obj(struct i2c_adapter, GFP_KERNEL);
if (!new_adapter) {
ret = -ENOMEM;
goto out;
}
adapter_data = kzalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL);
adapter_data = kzalloc_obj(struct i2c_algo_iop3xx_data, GFP_KERNEL);
if (!adapter_data) {
ret = -ENOMEM;
goto free_adapter;

View File

@@ -359,7 +359,7 @@ static int nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
int res1, res2;
/* we support 2 SMBus adapters */
smbuses = kcalloc(2, sizeof(struct nforce2_smbus), GFP_KERNEL);
smbuses = kzalloc_objs(struct nforce2_smbus, 2, GFP_KERNEL);
if (!smbuses)
return -ENOMEM;
pci_set_drvdata(dev, smbuses);

View File

@@ -288,7 +288,7 @@ static void i2c_parport_attach(struct parport *port)
return;
}
adapter = kzalloc(sizeof(struct i2c_par), GFP_KERNEL);
adapter = kzalloc_obj(struct i2c_par, GFP_KERNEL);
if (!adapter)
return;
memset(&i2c_parport_cb, 0, sizeof(i2c_parport_cb));

View File

@@ -919,7 +919,7 @@ static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
struct i2c_piix4_adapdata *adapdata;
int retval;
adap = kzalloc(sizeof(*adap), GFP_KERNEL);
adap = kzalloc_obj(*adap, GFP_KERNEL);
if (adap == NULL) {
release_region(smba, SMBIOSIZE);
return -ENOMEM;
@@ -930,7 +930,7 @@ static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
adap->algo = sb800_main ? &piix4_smbus_algorithm_sb800
: &smbus_algorithm;
adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
adapdata = kzalloc_obj(*adapdata, GFP_KERNEL);
if (adapdata == NULL) {
kfree(adap);
release_region(smba, SMBIOSIZE);

View File

@@ -112,7 +112,7 @@ static int ce4100_i2c_probe(struct pci_dev *dev,
dev_err(&dev->dev, "Missing device tree node.\n");
return -EINVAL;
}
sds = kzalloc(sizeof(*sds), GFP_KERNEL);
sds = kzalloc_obj(*sds, GFP_KERNEL);
if (!sds)
return -ENOMEM;

View File

@@ -799,7 +799,8 @@ static int geni_i2c_gpi_xfer(struct geni_i2c_dev *gi2c, struct i2c_msg msgs[], i
if (gi2c->is_tx_multi_desc_xfer) {
tx_multi_xfer->dma_buf = kcalloc(num, sizeof(void *), GFP_KERNEL);
tx_multi_xfer->dma_addr = kcalloc(num, sizeof(dma_addr_t), GFP_KERNEL);
tx_multi_xfer->dma_addr = kzalloc_objs(dma_addr_t, num,
GFP_KERNEL);
if (!tx_multi_xfer->dma_buf || !tx_multi_xfer->dma_addr) {
ret = -ENOMEM;
goto err;

View File

@@ -359,7 +359,7 @@ static int smbus_cmi_probe(struct platform_device *device)
struct acpi_smbus_cmi *smbus_cmi;
int ret;
smbus_cmi = kzalloc(sizeof(struct acpi_smbus_cmi), GFP_KERNEL);
smbus_cmi = kzalloc_obj(struct acpi_smbus_cmi, GFP_KERNEL);
if (!smbus_cmi)
return -ENOMEM;

View File

@@ -443,7 +443,7 @@ static int sh7760_i2c_probe(struct platform_device *pdev)
goto out0;
}
id = kzalloc(sizeof(*id), GFP_KERNEL);
id = kzalloc_obj(*id, GFP_KERNEL);
if (!id) {
ret = -ENOMEM;
goto out0;

View File

@@ -64,7 +64,7 @@ static int simtec_i2c_probe(struct platform_device *dev)
int size;
int ret;
pd = kzalloc(sizeof(struct simtec_i2c_data), GFP_KERNEL);
pd = kzalloc_obj(struct simtec_i2c_data, GFP_KERNEL);
if (pd == NULL)
return -ENOMEM;

View File

@@ -544,7 +544,7 @@ static int stm32f7_i2c_compute_timing(struct stm32f7_i2c_dev *i2c_dev,
if (((sdadel >= sdadel_min) &&
(sdadel <= sdadel_max)) &&
(p != p_prev)) {
v = kmalloc(sizeof(*v), GFP_KERNEL);
v = kmalloc_obj(*v, GFP_KERNEL);
if (!v) {
ret = -ENOMEM;
goto exit;

View File

@@ -203,7 +203,7 @@ static int taos_connect(struct serio *serio, struct serio_driver *drv)
char *name;
int err;
taos = kzalloc(sizeof(struct taos_data), GFP_KERNEL);
taos = kzalloc_obj(struct taos_data, GFP_KERNEL);
if (!taos) {
err = -ENOMEM;
goto exit;

View File

@@ -55,7 +55,7 @@ static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
struct i2c_msg *pmsg;
int i, ret;
pstatus = kmalloc(sizeof(*pstatus), GFP_KERNEL);
pstatus = kmalloc_obj(*pstatus, GFP_KERNEL);
if (!pstatus)
return -ENOMEM;
@@ -123,7 +123,7 @@ static u32 usb_func(struct i2c_adapter *adapter)
__le32 *pfunc;
u32 ret;
pfunc = kmalloc(sizeof(*pfunc), GFP_KERNEL);
pfunc = kmalloc_obj(*pfunc, GFP_KERNEL);
/* get functionality from adapter */
if (!pfunc || usb_read(adapter, CMD_GET_FUNC, 0, 0, pfunc,
@@ -233,7 +233,7 @@ static int i2c_tiny_usb_probe(struct usb_interface *interface,
dev_dbg(&interface->dev, "probing usb device\n");
/* allocate memory for our device state and initialize it */
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
dev = kzalloc_obj(*dev, GFP_KERNEL);
if (!dev)
goto error;

View File

@@ -139,7 +139,7 @@ static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
struct virtio_i2c_req *reqs;
int count;
reqs = kcalloc(num, sizeof(*reqs), GFP_KERNEL);
reqs = kzalloc_objs(*reqs, num, GFP_KERNEL);
if (!reqs)
return -ENOMEM;

View File

@@ -418,7 +418,7 @@ static struct scx200_acb_iface *scx200_create_iface(const char *text,
struct scx200_acb_iface *iface;
struct i2c_adapter *adapter;
iface = kzalloc(sizeof(*iface), GFP_KERNEL);
iface = kzalloc_obj(*iface, GFP_KERNEL);
if (!iface)
return NULL;

View File

@@ -137,7 +137,7 @@ static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_aliases, b
struct i2c_atr_alias_pool *alias_pool;
int ret;
alias_pool = kzalloc(sizeof(*alias_pool), GFP_KERNEL);
alias_pool = kzalloc_obj(*alias_pool, GFP_KERNEL);
if (!alias_pool)
return ERR_PTR(-ENOMEM);
@@ -183,7 +183,7 @@ static struct i2c_atr_alias_pair *i2c_atr_create_c2a(struct i2c_atr_chan *chan,
lockdep_assert_held(&chan->alias_pairs_lock);
c2a = kzalloc(sizeof(*c2a), GFP_KERNEL);
c2a = kzalloc_obj(*c2a, GFP_KERNEL);
if (!c2a)
return NULL;
@@ -724,7 +724,7 @@ struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent, struct device *dev,
if (!ops || !ops->attach_addr || !ops->detach_addr)
return ERR_PTR(-EINVAL);
atr = kzalloc(struct_size(atr, adapter, max_adapters), GFP_KERNEL);
atr = kzalloc_flex(*atr, adapter, max_adapters, GFP_KERNEL);
if (!atr)
return ERR_PTR(-ENOMEM);
@@ -800,7 +800,7 @@ int i2c_atr_add_adapter(struct i2c_atr *atr, struct i2c_atr_adap_desc *desc)
return -EEXIST;
}
chan = kzalloc(sizeof(*chan), GFP_KERNEL);
chan = kzalloc_obj(*chan, GFP_KERNEL);
if (!chan)
return -ENOMEM;

View File

@@ -61,7 +61,7 @@ int i2c_register_board_info(int busnum, struct i2c_board_info const *info, unsig
for (status = 0; len; len--, info++) {
struct i2c_devinfo *devinfo;
devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL);
devinfo = kzalloc_obj(*devinfo, GFP_KERNEL);
if (!devinfo) {
pr_debug("i2c-core: can't register boardinfo!\n");
status = -ENOMEM;

View File

@@ -683,7 +683,7 @@ i2c_acpi_space_handler(u32 function, acpi_physical_address command,
if (ACPI_FAILURE(ret))
return ret;
client = kzalloc(sizeof(*client), GFP_KERNEL);
client = kzalloc_obj(*client, GFP_KERNEL);
if (!client) {
ret = AE_NO_MEMORY;
goto err;
@@ -793,8 +793,7 @@ int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
if (!handle)
return -ENODEV;
data = kzalloc(sizeof(struct i2c_acpi_handler_data),
GFP_KERNEL);
data = kzalloc_obj(struct i2c_acpi_handler_data, GFP_KERNEL);
if (!data)
return -ENOMEM;

View File

@@ -964,7 +964,7 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
bool need_put = false;
int status;
client = kzalloc(sizeof *client, GFP_KERNEL);
client = kzalloc_obj(*client, GFP_KERNEL);
if (!client)
return ERR_PTR(-ENOMEM);
@@ -2529,7 +2529,7 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
return 0;
/* Set up a temporary client to help detect callback */
temp_client = kzalloc(sizeof(*temp_client), GFP_KERNEL);
temp_client = kzalloc_obj(*temp_client, GFP_KERNEL);
if (!temp_client)
return -ENOMEM;

View File

@@ -63,7 +63,7 @@ static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node
dev_dbg(dev, "Enabling %pOF\n", node);
struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL);
struct of_changeset *ocs __free(kfree) = kzalloc_obj(*ocs, GFP_KERNEL);
if (!ocs)
return -ENOMEM;

View File

@@ -74,7 +74,7 @@ static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
return ERR_PTR(-ENODEV);
}
i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
i2c_dev = kzalloc_obj(*i2c_dev, GFP_KERNEL);
if (!i2c_dev)
return ERR_PTR(-ENOMEM);
i2c_dev->adap = adap;
@@ -552,8 +552,8 @@ static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned lo
if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
return -EINVAL;
rdwr_pa = kmalloc_array(rdwr_arg.nmsgs, sizeof(struct i2c_msg),
GFP_KERNEL);
rdwr_pa = kmalloc_objs(struct i2c_msg, rdwr_arg.nmsgs,
GFP_KERNEL);
if (!rdwr_pa)
return -ENOMEM;
@@ -612,7 +612,7 @@ static int i2cdev_open(struct inode *inode, struct file *file)
* or I2C core code!! It just holds private copies of addressing
* information and maybe a PEC flag.
*/
client = kzalloc(sizeof(*client), GFP_KERNEL);
client = kzalloc_obj(*client, GFP_KERNEL);
if (!client) {
i2c_put_adapter(adap);
return -ENOMEM;

View File

@@ -277,7 +277,7 @@ int i2c_mux_add_adapter(struct i2c_mux_core *muxc,
return -EINVAL;
}
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
priv = kzalloc_obj(*priv, GFP_KERNEL);
if (!priv)
return -ENOMEM;

View File

@@ -319,8 +319,7 @@ struct i2c_client *i2c_new_slave_host_notify_device(struct i2c_adapter *adapter)
struct i2c_client *client;
int ret;
status = kzalloc(sizeof(struct i2c_slave_host_notify_status),
GFP_KERNEL);
status = kzalloc_obj(struct i2c_slave_host_notify_status, GFP_KERNEL);
if (!status)
return ERR_PTR(-ENOMEM);

View File

@@ -372,8 +372,7 @@ static int __init i2c_stub_init(void)
/* Allocate memory for all chips at once */
stub_chips_nr = i;
stub_chips = kcalloc(stub_chips_nr, sizeof(struct stub_chip),
GFP_KERNEL);
stub_chips = kzalloc_objs(struct stub_chip, stub_chips_nr, GFP_KERNEL);
if (!stub_chips)
return -ENOMEM;