Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'use-vmalloc_array-and-vcalloc'

Julia Lawall says:

====================
use vmalloc_array and vcalloc

The functions vmalloc_array and vcalloc were introduced in

commit a8749a35c399 ("mm: vmalloc: introduce array allocation functions")

but are not used much yet. This series introduces uses of
these functions, to protect against multiplication overflows.

The changes were done using the following Coccinelle semantic
patch.

@initialize:ocaml@
@@

let rename alloc =
match alloc with
"vmalloc" -> "vmalloc_array"
| "vzalloc" -> "vcalloc"
| _ -> failwith "unknown"

@@
size_t e1,e2;
constant C1, C2;
expression E1, E2, COUNT, x1, x2, x3;
typedef u8;
typedef __u8;
type t = {u8,__u8,char,unsigned char};
identifier alloc = {vmalloc,vzalloc};
fresh identifier realloc = script:ocaml(alloc) { rename alloc };
@@

(
alloc(x1*x2*x3)
|
alloc(C1 * C2)
|
alloc((sizeof(t)) * (COUNT), ...)
|
- alloc((e1) * (e2))
+ realloc(e1, e2)
|
- alloc((e1) * (COUNT))
+ realloc(COUNT, e1)
|
- alloc((E1) * (E2))
+ realloc(E1, E2)
)

v2: This series uses vmalloc_array and vcalloc instead of
array_size. It also leaves a multiplication of a constant by a
sizeof as is. Two patches are thus dropped from the series.
====================

Link: https://lore.kernel.org/r/20230627144339.144478-1-Julia.Lawall@inria.fr
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+9 -9
+2 -2
drivers/net/ethernet/amd/pds_core/core.c
··· 196 196 dma_addr_t q_base_pa; 197 197 int err; 198 198 199 - qcq->q.info = vzalloc(num_descs * sizeof(*qcq->q.info)); 199 + qcq->q.info = vcalloc(num_descs, sizeof(*qcq->q.info)); 200 200 if (!qcq->q.info) { 201 201 err = -ENOMEM; 202 202 goto err_out; ··· 219 219 if (err) 220 220 goto err_out_free_q_info; 221 221 222 - qcq->cq.info = vzalloc(num_descs * sizeof(*qcq->cq.info)); 222 + qcq->cq.info = vcalloc(num_descs, sizeof(*qcq->cq.info)); 223 223 if (!qcq->cq.info) { 224 224 err = -ENOMEM; 225 225 goto err_out_free_irq;
+2 -2
drivers/net/ethernet/freescale/enetc/enetc.c
··· 1789 1789 res->bd_count = bd_count; 1790 1790 res->bd_size = sizeof(union enetc_tx_bd); 1791 1791 1792 - res->tx_swbd = vzalloc(bd_count * sizeof(*res->tx_swbd)); 1792 + res->tx_swbd = vcalloc(bd_count, sizeof(*res->tx_swbd)); 1793 1793 if (!res->tx_swbd) 1794 1794 return -ENOMEM; 1795 1795 ··· 1877 1877 if (extended) 1878 1878 res->bd_size *= 2; 1879 1879 1880 - res->rx_swbd = vzalloc(bd_count * sizeof(struct enetc_rx_swbd)); 1880 + res->rx_swbd = vcalloc(bd_count, sizeof(struct enetc_rx_swbd)); 1881 1881 if (!res->rx_swbd) 1882 1882 return -ENOMEM; 1883 1883
+1 -1
drivers/net/ethernet/google/gve/gve_tx.c
··· 248 248 tx->mask = slots - 1; 249 249 250 250 /* alloc metadata */ 251 - tx->info = vzalloc(sizeof(*tx->info) * slots); 251 + tx->info = vcalloc(slots, sizeof(*tx->info)); 252 252 if (!tx->info) 253 253 return -ENOMEM; 254 254
+1 -1
drivers/net/ethernet/marvell/octeon_ep/octep_rx.c
··· 158 158 goto desc_dma_alloc_err; 159 159 } 160 160 161 - oq->buff_info = vzalloc(oq->max_count * OCTEP_OQ_RECVBUF_SIZE); 161 + oq->buff_info = vcalloc(oq->max_count, OCTEP_OQ_RECVBUF_SIZE); 162 162 if (unlikely(!oq->buff_info)) { 163 163 dev_err(&oct->pdev->dev, 164 164 "Failed to allocate buffer info for OQ-%d\n", q_no);
+1 -1
drivers/net/ethernet/microsoft/mana/hw_channel.c
··· 627 627 if (WARN_ON(cq->id >= gc->max_num_cqs)) 628 628 return -EPROTO; 629 629 630 - gc->cq_table = vzalloc(gc->max_num_cqs * sizeof(struct gdma_queue *)); 630 + gc->cq_table = vcalloc(gc->max_num_cqs, sizeof(struct gdma_queue *)); 631 631 if (!gc->cq_table) 632 632 return -ENOMEM; 633 633
+2 -2
drivers/net/ethernet/pensando/ionic/ionic_lif.c
··· 561 561 new->q.dev = dev; 562 562 new->flags = flags; 563 563 564 - new->q.info = vzalloc(num_descs * sizeof(*new->q.info)); 564 + new->q.info = vcalloc(num_descs, sizeof(*new->q.info)); 565 565 if (!new->q.info) { 566 566 netdev_err(lif->netdev, "Cannot allocate queue info\n"); 567 567 err = -ENOMEM; ··· 582 582 if (err) 583 583 goto err_out; 584 584 585 - new->cq.info = vzalloc(num_descs * sizeof(*new->cq.info)); 585 + new->cq.info = vcalloc(num_descs, sizeof(*new->cq.info)); 586 586 if (!new->cq.info) { 587 587 netdev_err(lif->netdev, "Cannot allocate completion queue info\n"); 588 588 err = -ENOMEM;