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

@@ -148,8 +148,7 @@ static int __init adummy_init(void)
printk(KERN_ERR "adummy: version %s\n", DRV_VERSION);
adummy_dev = kzalloc(sizeof(struct adummy_dev),
GFP_KERNEL);
adummy_dev = kzalloc_obj(struct adummy_dev, GFP_KERNEL);
if (!adummy_dev) {
printk(KERN_ERR DEV_LABEL ": kzalloc() failed\n");
err = -ENOMEM;

View File

@@ -375,7 +375,7 @@ static int atmtcp_create(int itf,int persist,struct atm_dev **result)
struct atmtcp_dev_data *dev_data;
struct atm_dev *dev;
dev_data = kmalloc(sizeof(*dev_data),GFP_KERNEL);
dev_data = kmalloc_obj(*dev_data, GFP_KERNEL);
if (!dev_data)
return -ENOMEM;

View File

@@ -1845,9 +1845,9 @@ static int eni_start(struct atm_dev *dev)
/* initialize memory management */
buffer_mem = eni_dev->mem - (buf - eni_dev->ram);
eni_dev->free_list_size = buffer_mem/MID_MIN_BUF_SIZE/2;
eni_dev->free_list = kmalloc_array(eni_dev->free_list_size + 1,
sizeof(*eni_dev->free_list),
GFP_KERNEL);
eni_dev->free_list = kmalloc_objs(*eni_dev->free_list,
eni_dev->free_list_size + 1,
GFP_KERNEL);
if (!eni_dev->free_list) {
printk(KERN_ERR DEV_LABEL "(itf %d): couldn't get free page\n",
dev->number);
@@ -1925,7 +1925,7 @@ static int eni_open(struct atm_vcc *vcc)
DPRINTK(DEV_LABEL "(itf %d): open %d.%d\n",vcc->dev->number,vcc->vpi,
vcc->vci);
if (!test_bit(ATM_VF_PARTIAL,&vcc->flags)) {
eni_vcc = kmalloc(sizeof(struct eni_vcc),GFP_KERNEL);
eni_vcc = kmalloc_obj(struct eni_vcc, GFP_KERNEL);
if (!eni_vcc) return -ENOMEM;
vcc->dev_data = eni_vcc;
eni_vcc->tx = NULL; /* for eni_close after open_rx */
@@ -2229,7 +2229,7 @@ static int eni_init_one(struct pci_dev *pci_dev,
goto err_disable;
rc = -ENOMEM;
eni_dev = kmalloc(sizeof(struct eni_dev), GFP_KERNEL);
eni_dev = kmalloc_obj(struct eni_dev, GFP_KERNEL);
if (!eni_dev)
goto err_disable;

View File

@@ -1331,7 +1331,7 @@ fore200e_open(struct atm_vcc *vcc)
spin_unlock_irqrestore(&fore200e->q_lock, flags);
fore200e_vcc = kzalloc(sizeof(struct fore200e_vcc), GFP_ATOMIC);
fore200e_vcc = kzalloc_obj(struct fore200e_vcc, GFP_ATOMIC);
if (fore200e_vcc == NULL) {
vc_map->vcc = NULL;
return -ENOMEM;
@@ -1676,7 +1676,7 @@ fore200e_getstats(struct fore200e* fore200e)
u32 stats_dma_addr;
if (fore200e->stats == NULL) {
fore200e->stats = kzalloc(sizeof(struct stats), GFP_KERNEL);
fore200e->stats = kzalloc_obj(struct stats, GFP_KERNEL);
if (fore200e->stats == NULL)
return -ENOMEM;
}
@@ -1955,7 +1955,7 @@ static int fore200e_irq_request(struct fore200e *fore200e)
static int fore200e_get_esi(struct fore200e *fore200e)
{
struct prom_data* prom = kzalloc(sizeof(struct prom_data), GFP_KERNEL);
struct prom_data* prom = kzalloc_obj(struct prom_data, GFP_KERNEL);
int ok, i;
if (!prom)
@@ -2000,8 +2000,7 @@ static int fore200e_alloc_rx_buf(struct fore200e *fore200e)
DPRINTK(2, "rx buffers %d / %d are being allocated\n", scheme, magn);
/* allocate the array of receive buffers */
buffer = bsq->buffer = kcalloc(nbr, sizeof(struct buffer),
GFP_KERNEL);
buffer = bsq->buffer = kzalloc_objs(struct buffer, nbr, GFP_KERNEL);
if (buffer == NULL)
return -ENOMEM;
@@ -2529,7 +2528,7 @@ static int fore200e_sba_probe(struct platform_device *op)
static int index = 0;
int err;
fore200e = kzalloc(sizeof(struct fore200e), GFP_KERNEL);
fore200e = kzalloc_obj(struct fore200e, GFP_KERNEL);
if (!fore200e)
return -ENOMEM;
@@ -2597,7 +2596,7 @@ static int fore200e_pca_detect(struct pci_dev *pci_dev,
goto out;
}
fore200e = kzalloc(sizeof(struct fore200e), GFP_KERNEL);
fore200e = kzalloc_obj(struct fore200e, GFP_KERNEL);
if (fore200e == NULL) {
err = -ENOMEM;
goto out_disable;

View File

@@ -372,8 +372,7 @@ static int he_init_one(struct pci_dev *pci_dev,
}
pci_set_drvdata(pci_dev, atm_dev);
he_dev = kzalloc(sizeof(struct he_dev),
GFP_KERNEL);
he_dev = kzalloc_obj(struct he_dev, GFP_KERNEL);
if (!he_dev) {
err = -ENOMEM;
goto init_one_failure;
@@ -787,9 +786,8 @@ static int he_init_group(struct he_dev *he_dev, int group)
}
/* rbpl_virt 64-bit pointers */
he_dev->rbpl_virt = kmalloc_array(RBPL_TABLE_SIZE,
sizeof(*he_dev->rbpl_virt),
GFP_KERNEL);
he_dev->rbpl_virt = kmalloc_objs(*he_dev->rbpl_virt, RBPL_TABLE_SIZE,
GFP_KERNEL);
if (!he_dev->rbpl_virt) {
hprintk("unable to allocate rbpl virt table\n");
goto out_free_rbpl_table;
@@ -2132,7 +2130,7 @@ he_open(struct atm_vcc *vcc)
cid = he_mkcid(he_dev, vpi, vci);
he_vcc = kmalloc(sizeof(struct he_vcc), GFP_ATOMIC);
he_vcc = kmalloc_obj(struct he_vcc, GFP_ATOMIC);
if (he_vcc == NULL) {
hprintk("unable to allocate he_vcc during open\n");
return -ENOMEM;

View File

@@ -262,7 +262,7 @@ static int idt77105_start(struct atm_dev *dev)
{
unsigned long flags;
if (!(dev->phy_data = kmalloc(sizeof(struct idt77105_priv),GFP_KERNEL)))
if (!(dev->phy_data = kmalloc_obj(struct idt77105_priv, GFP_KERNEL)))
return -ENOMEM;
PRIV(dev)->dev = dev;
spin_lock_irqsave(&idt77105_priv_lock, flags);

View File

@@ -638,7 +638,7 @@ alloc_scq(struct idt77252_dev *card, int class)
{
struct scq_info *scq;
scq = kzalloc(sizeof(struct scq_info), GFP_KERNEL);
scq = kzalloc_obj(struct scq_info, GFP_KERNEL);
if (!scq)
return NULL;
scq->base = dma_alloc_coherent(&card->pcidev->dev, SCQ_SIZE,
@@ -2116,7 +2116,7 @@ idt77252_init_est(struct vc_map *vc, int pcr)
{
struct rate_estimator *est;
est = kzalloc(sizeof(struct rate_estimator), GFP_KERNEL);
est = kzalloc_obj(struct rate_estimator, GFP_KERNEL);
if (!est)
return NULL;
est->maxcps = pcr < 0 ? -pcr : pcr;
@@ -2424,7 +2424,7 @@ idt77252_open(struct atm_vcc *vcc)
index = VPCI2VC(card, vpi, vci);
if (!card->vcs[index]) {
card->vcs[index] = kzalloc(sizeof(struct vc_map), GFP_KERNEL);
card->vcs[index] = kzalloc_obj(struct vc_map, GFP_KERNEL);
if (!card->vcs[index]) {
printk("%s: can't alloc vc in open()\n", card->name);
mutex_unlock(&card->mutex);
@@ -2855,7 +2855,7 @@ open_card_oam(struct idt77252_dev *card)
for (vci = 3; vci < 5; vci++) {
index = VPCI2VC(card, vpi, vci);
vc = kzalloc(sizeof(struct vc_map), GFP_KERNEL);
vc = kzalloc_obj(struct vc_map, GFP_KERNEL);
if (!vc) {
printk("%s: can't alloc vc\n", card->name);
return -ENOMEM;
@@ -2923,7 +2923,7 @@ open_card_ubr0(struct idt77252_dev *card)
{
struct vc_map *vc;
vc = kzalloc(sizeof(struct vc_map), GFP_KERNEL);
vc = kzalloc_obj(struct vc_map, GFP_KERNEL);
if (!vc) {
printk("%s: can't alloc vc\n", card->name);
return -ENOMEM;
@@ -3621,7 +3621,7 @@ static int idt77252_init_one(struct pci_dev *pcidev,
goto err_out_disable_pdev;
}
card = kzalloc(sizeof(struct idt77252_dev), GFP_KERNEL);
card = kzalloc_obj(struct idt77252_dev, GFP_KERNEL);
if (!card) {
printk("idt77252-%d: can't allocate private data\n", index);
err = -ENOMEM;

View File

@@ -114,7 +114,7 @@ static void ia_enque_head_rtn_q (IARTN_Q *que, IARTN_Q * data)
}
static int ia_enque_rtn_q (IARTN_Q *que, struct desc_tbl_t data) {
IARTN_Q *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
IARTN_Q *entry = kmalloc_obj(*entry, GFP_ATOMIC);
if (!entry)
return -ENOMEM;
entry->data = data;
@@ -1978,9 +1978,8 @@ static int tx_init(struct atm_dev *dev)
buf_desc_ptr++;
tx_pkt_start += iadev->tx_buf_sz;
}
iadev->tx_buf = kmalloc_array(iadev->num_tx_desc,
sizeof(*iadev->tx_buf),
GFP_KERNEL);
iadev->tx_buf = kmalloc_objs(*iadev->tx_buf, iadev->num_tx_desc,
GFP_KERNEL);
if (!iadev->tx_buf) {
printk(KERN_ERR DEV_LABEL " couldn't get mem\n");
goto err_free_dle;
@@ -1989,7 +1988,7 @@ static int tx_init(struct atm_dev *dev)
{
struct cpcs_trailer *cpcs;
cpcs = kmalloc(sizeof(*cpcs), GFP_KERNEL|GFP_DMA);
cpcs = kmalloc_obj(*cpcs, GFP_KERNEL | GFP_DMA);
if(!cpcs) {
printk(KERN_ERR DEV_LABEL " couldn't get freepage\n");
goto err_free_tx_bufs;
@@ -2000,9 +1999,8 @@ static int tx_init(struct atm_dev *dev)
sizeof(*cpcs),
DMA_TO_DEVICE);
}
iadev->desc_tbl = kmalloc_array(iadev->num_tx_desc,
sizeof(*iadev->desc_tbl),
GFP_KERNEL);
iadev->desc_tbl = kmalloc_objs(*iadev->desc_tbl, iadev->num_tx_desc,
GFP_KERNEL);
if (!iadev->desc_tbl) {
printk(KERN_ERR DEV_LABEL " couldn't get mem\n");
goto err_free_all_tx_bufs;
@@ -2130,9 +2128,8 @@ static int tx_init(struct atm_dev *dev)
memset((caddr_t)(iadev->seg_ram+i), 0, iadev->num_vc*4);
vc = (struct main_vc *)iadev->MAIN_VC_TABLE_ADDR;
evc = (struct ext_vc *)iadev->EXT_VC_TABLE_ADDR;
iadev->testTable = kmalloc_array(iadev->num_vc,
sizeof(*iadev->testTable),
GFP_KERNEL);
iadev->testTable = kmalloc_objs(*iadev->testTable, iadev->num_vc,
GFP_KERNEL);
if (!iadev->testTable) {
printk("Get freepage failed\n");
goto err_free_desc_tbl;
@@ -2141,8 +2138,8 @@ static int tx_init(struct atm_dev *dev)
{
memset((caddr_t)vc, 0, sizeof(*vc));
memset((caddr_t)evc, 0, sizeof(*evc));
iadev->testTable[i] = kmalloc(sizeof(struct testTable_t),
GFP_KERNEL);
iadev->testTable[i] = kmalloc_obj(struct testTable_t,
GFP_KERNEL);
if (!iadev->testTable[i])
goto err_free_test_tables;
iadev->testTable[i]->lastTime = 0;
@@ -2712,7 +2709,7 @@ static int ia_open(struct atm_vcc *vcc)
vcc->dev->number, vcc->vpi, vcc->vci);)
/* Device dependent initialization */
ia_vcc = kmalloc(sizeof(*ia_vcc), GFP_KERNEL);
ia_vcc = kmalloc_obj(*ia_vcc, GFP_KERNEL);
if (!ia_vcc) return -ENOMEM;
vcc->dev_data = ia_vcc;
@@ -2798,7 +2795,7 @@ static int ia_ioctl(struct atm_dev *dev, unsigned int cmd, void __user *arg)
rfredn_t *rfL;
if (!capable(CAP_NET_ADMIN)) return -EPERM;
regs_local = kmalloc(sizeof(*regs_local), GFP_KERNEL);
regs_local = kmalloc_obj(*regs_local, GFP_KERNEL);
if (!regs_local) return -ENOMEM;
ffL = &regs_local->ffredn;
rfL = &regs_local->rfredn;
@@ -3168,7 +3165,7 @@ static int ia_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
IADEV *iadev;
int ret;
iadev = kzalloc(sizeof(*iadev), GFP_KERNEL);
iadev = kzalloc_obj(*iadev, GFP_KERNEL);
if (!iadev) {
ret = -ENOMEM;
goto err_out;

View File

@@ -1464,7 +1464,7 @@ static inline void vcc_table_deallocate(const struct lanai_dev *lanai)
static inline struct lanai_vcc *new_lanai_vcc(void)
{
struct lanai_vcc *lvcc;
lvcc = kzalloc(sizeof(*lvcc), GFP_KERNEL);
lvcc = kzalloc_obj(*lvcc, GFP_KERNEL);
if (likely(lvcc != NULL)) {
skb_queue_head_init(&lvcc->tx.backlog);
#ifdef DEBUG
@@ -2555,7 +2555,7 @@ static int lanai_init_one(struct pci_dev *pci,
struct atm_dev *atmdev;
int result;
lanai = kzalloc(sizeof(*lanai), GFP_KERNEL);
lanai = kzalloc_obj(*lanai, GFP_KERNEL);
if (lanai == NULL) {
printk(KERN_ERR DEV_LABEL
": couldn't allocate dev_data structure!\n");

View File

@@ -373,7 +373,7 @@ static int ns_init_card(int i, struct pci_dev *pcidev)
return error;
}
card = kmalloc(sizeof(*card), GFP_KERNEL);
card = kmalloc_obj(*card, GFP_KERNEL);
if (!card) {
printk
("nicstar%d: can't allocate memory for device structure.\n",
@@ -867,7 +867,7 @@ static scq_info *get_scq(ns_dev *card, int size, u32 scd)
if (size != VBR_SCQSIZE && size != CBR_SCQSIZE)
return NULL;
scq = kmalloc(sizeof(*scq), GFP_KERNEL);
scq = kmalloc_obj(*scq, GFP_KERNEL);
if (!scq)
return NULL;
scq->org = dma_alloc_coherent(&card->pcidev->dev,
@@ -876,8 +876,7 @@ static scq_info *get_scq(ns_dev *card, int size, u32 scd)
kfree(scq);
return NULL;
}
scq->skb = kcalloc(size / NS_SCQE_SIZE, sizeof(*scq->skb),
GFP_KERNEL);
scq->skb = kzalloc_objs(*scq->skb, size / NS_SCQE_SIZE, GFP_KERNEL);
if (!scq->skb) {
dma_free_coherent(&card->pcidev->dev,
2 * size, scq->org, scq->dma);

View File

@@ -1196,7 +1196,7 @@ static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
uint32_t data32;
struct solos_card *card;
card = kzalloc(sizeof(*card), GFP_KERNEL);
card = kzalloc_obj(*card, GFP_KERNEL);
if (!card)
return -ENOMEM;

View File

@@ -367,7 +367,7 @@ int suni_init(struct atm_dev *dev)
{
unsigned char mri;
if (!(dev->phy_data = kmalloc(sizeof(struct suni_priv),GFP_KERNEL)))
if (!(dev->phy_data = kmalloc_obj(struct suni_priv, GFP_KERNEL)))
return -ENOMEM;
PRIV(dev)->dev = dev;