summaryrefslogtreecommitdiffstats
path: root/drivers/infiniband
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2018-06-12 17:27:37 -0400
committerKees Cook <keescook@chromium.org>2018-06-12 19:19:22 -0400
commitfad953ce0b22cfd352a9a90b070c34b8791e6868 (patch)
treec1806e803783afe2e82cb205925b6725d67fa76a /drivers/infiniband
parent42bc47b35320e0e587a88e437e18f80f9c5bcbb2 (diff)
treewide: Use array_size() in vzalloc()
The vzalloc() function has no 2-factor argument form, so multiplication factors need to be wrapped in array_size(). This patch replaces cases of: vzalloc(a * b) with: vzalloc(array_size(a, b)) as well as handling cases of: vzalloc(a * b * c) with: vzalloc(array3_size(a, b, c)) This does, however, attempt to ignore constant size factors like: vzalloc(4 * 1024) though any constants defined via macros get caught up in the conversion. Any factors with a sizeof() of "unsigned char", "char", and "u8" were dropped, since they're redundant. The Coccinelle script used for this was: // Fix redundant parens around sizeof(). @@ type TYPE; expression THING, E; @@ ( vzalloc( - (sizeof(TYPE)) * E + sizeof(TYPE) * E , ...) | vzalloc( - (sizeof(THING)) * E + sizeof(THING) * E , ...) ) // Drop single-byte sizes and redundant parens. @@ expression COUNT; typedef u8; typedef __u8; @@ ( vzalloc( - sizeof(u8) * (COUNT) + COUNT , ...) | vzalloc( - sizeof(__u8) * (COUNT) + COUNT , ...) | vzalloc( - sizeof(char) * (COUNT) + COUNT , ...) | vzalloc( - sizeof(unsigned char) * (COUNT) + COUNT , ...) | vzalloc( - sizeof(u8) * COUNT + COUNT , ...) | vzalloc( - sizeof(__u8) * COUNT + COUNT , ...) | vzalloc( - sizeof(char) * COUNT + COUNT , ...) | vzalloc( - sizeof(unsigned char) * COUNT + COUNT , ...) ) // 2-factor product with sizeof(type/expression) and identifier or constant. @@ type TYPE; expression THING; identifier COUNT_ID; constant COUNT_CONST; @@ ( vzalloc( - sizeof(TYPE) * (COUNT_ID) + array_size(COUNT_ID, sizeof(TYPE)) , ...) | vzalloc( - sizeof(TYPE) * COUNT_ID + array_size(COUNT_ID, sizeof(TYPE)) , ...) | vzalloc( - sizeof(TYPE) * (COUNT_CONST) + array_size(COUNT_CONST, sizeof(TYPE)) , ...) | vzalloc( - sizeof(TYPE) * COUNT_CONST + array_size(COUNT_CONST, sizeof(TYPE)) , ...) | vzalloc( - sizeof(THING) * (COUNT_ID) + array_size(COUNT_ID, sizeof(THING)) , ...) | vzalloc( - sizeof(THING) * COUNT_ID + array_size(COUNT_ID, sizeof(THING)) , ...) | vzalloc( - sizeof(THING) * (COUNT_CONST) + array_size(COUNT_CONST, sizeof(THING)) , ...) | vzalloc( - sizeof(THING) * COUNT_CONST + array_size(COUNT_CONST, sizeof(THING)) , ...) ) // 2-factor product, only identifiers. @@ identifier SIZE, COUNT; @@ vzalloc( - SIZE * COUNT + array_size(COUNT, SIZE) , ...) // 3-factor product with 1 sizeof(type) or sizeof(expression), with // redundant parens removed. @@ expression THING; identifier STRIDE, COUNT; type TYPE; @@ ( vzalloc( - sizeof(TYPE) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | vzalloc( - sizeof(TYPE) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | vzalloc( - sizeof(TYPE) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | vzalloc( - sizeof(TYPE) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | vzalloc( - sizeof(THING) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | vzalloc( - sizeof(THING) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | vzalloc( - sizeof(THING) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | vzalloc( - sizeof(THING) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) ) // 3-factor product with 2 sizeof(variable), with redundant parens removed. @@ expression THING1, THING2; identifier COUNT; type TYPE1, TYPE2; @@ ( vzalloc( - sizeof(TYPE1) * sizeof(TYPE2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | vzalloc( - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | vzalloc( - sizeof(THING1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | vzalloc( - sizeof(THING1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | vzalloc( - sizeof(TYPE1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) | vzalloc( - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) ) // 3-factor product, only identifiers, with redundant parens removed. @@ identifier STRIDE, SIZE, COUNT; @@ ( vzalloc( - (COUNT) * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | vzalloc( - COUNT * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | vzalloc( - COUNT * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | vzalloc( - (COUNT) * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | vzalloc( - COUNT * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | vzalloc( - (COUNT) * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | vzalloc( - (COUNT) * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | vzalloc( - COUNT * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) ) // Any remaining multi-factor products, first at least 3-factor products // when they're not all constants... @@ expression E1, E2, E3; constant C1, C2, C3; @@ ( vzalloc(C1 * C2 * C3, ...) | vzalloc( - E1 * E2 * E3 + array3_size(E1, E2, E3) , ...) ) // And then all remaining 2 factors products when they're not all constants. @@ expression E1, E2; constant C1, C2; @@ ( vzalloc(C1 * C2, ...) | vzalloc( - E1 * E2 + array_size(E1, E2) , ...) ) Signed-off-by: Kees Cook <keescook@chromium.org>
Diffstat (limited to 'drivers/infiniband')
-rw-r--r--drivers/infiniband/core/umem_odp.c16
-rw-r--r--drivers/infiniband/hw/hns/hns_roce_mr.c2
-rw-r--r--drivers/infiniband/hw/qib/qib_init.c6
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib_cm.c8
-rw-r--r--drivers/infiniband/ulp/ipoib/ipoib_main.c3
5 files changed, 22 insertions, 13 deletions
diff --git a/drivers/infiniband/core/umem_odp.c b/drivers/infiniband/core/umem_odp.c
index 2aadf5813a40..182436b92ba9 100644
--- a/drivers/infiniband/core/umem_odp.c
+++ b/drivers/infiniband/core/umem_odp.c
@@ -285,13 +285,15 @@ struct ib_umem *ib_alloc_odp_umem(struct ib_ucontext *context,
285 mutex_init(&odp_data->umem_mutex); 285 mutex_init(&odp_data->umem_mutex);
286 init_completion(&odp_data->notifier_completion); 286 init_completion(&odp_data->notifier_completion);
287 287
288 odp_data->page_list = vzalloc(pages * sizeof(*odp_data->page_list)); 288 odp_data->page_list =
289 vzalloc(array_size(pages, sizeof(*odp_data->page_list)));
289 if (!odp_data->page_list) { 290 if (!odp_data->page_list) {
290 ret = -ENOMEM; 291 ret = -ENOMEM;
291 goto out_odp_data; 292 goto out_odp_data;
292 } 293 }
293 294
294 odp_data->dma_list = vzalloc(pages * sizeof(*odp_data->dma_list)); 295 odp_data->dma_list =
296 vzalloc(array_size(pages, sizeof(*odp_data->dma_list)));
295 if (!odp_data->dma_list) { 297 if (!odp_data->dma_list) {
296 ret = -ENOMEM; 298 ret = -ENOMEM;
297 goto out_page_list; 299 goto out_page_list;
@@ -371,15 +373,17 @@ int ib_umem_odp_get(struct ib_ucontext *context, struct ib_umem *umem,
371 init_completion(&umem->odp_data->notifier_completion); 373 init_completion(&umem->odp_data->notifier_completion);
372 374
373 if (ib_umem_num_pages(umem)) { 375 if (ib_umem_num_pages(umem)) {
374 umem->odp_data->page_list = vzalloc(ib_umem_num_pages(umem) * 376 umem->odp_data->page_list =
375 sizeof(*umem->odp_data->page_list)); 377 vzalloc(array_size(sizeof(*umem->odp_data->page_list),
378 ib_umem_num_pages(umem)));
376 if (!umem->odp_data->page_list) { 379 if (!umem->odp_data->page_list) {
377 ret_val = -ENOMEM; 380 ret_val = -ENOMEM;
378 goto out_odp_data; 381 goto out_odp_data;
379 } 382 }
380 383
381 umem->odp_data->dma_list = vzalloc(ib_umem_num_pages(umem) * 384 umem->odp_data->dma_list =
382 sizeof(*umem->odp_data->dma_list)); 385 vzalloc(array_size(sizeof(*umem->odp_data->dma_list),
386 ib_umem_num_pages(umem)));
383 if (!umem->odp_data->dma_list) { 387 if (!umem->odp_data->dma_list) {
384 ret_val = -ENOMEM; 388 ret_val = -ENOMEM;
385 goto out_page_list; 389 goto out_page_list;
diff --git a/drivers/infiniband/hw/hns/hns_roce_mr.c b/drivers/infiniband/hw/hns/hns_roce_mr.c
index d1fe0e7957e3..eb26a5f6fc58 100644
--- a/drivers/infiniband/hw/hns/hns_roce_mr.c
+++ b/drivers/infiniband/hw/hns/hns_roce_mr.c
@@ -144,7 +144,7 @@ static int hns_roce_buddy_init(struct hns_roce_buddy *buddy, int max_order)
144 buddy->bits[i] = kcalloc(s, sizeof(long), GFP_KERNEL | 144 buddy->bits[i] = kcalloc(s, sizeof(long), GFP_KERNEL |
145 __GFP_NOWARN); 145 __GFP_NOWARN);
146 if (!buddy->bits[i]) { 146 if (!buddy->bits[i]) {
147 buddy->bits[i] = vzalloc(s * sizeof(long)); 147 buddy->bits[i] = vzalloc(array_size(s, sizeof(long)));
148 if (!buddy->bits[i]) 148 if (!buddy->bits[i])
149 goto err_out_free; 149 goto err_out_free;
150 } 150 }
diff --git a/drivers/infiniband/hw/qib/qib_init.c b/drivers/infiniband/hw/qib/qib_init.c
index 704505618909..d7cdc77d6306 100644
--- a/drivers/infiniband/hw/qib/qib_init.c
+++ b/drivers/infiniband/hw/qib/qib_init.c
@@ -369,11 +369,13 @@ static void init_shadow_tids(struct qib_devdata *dd)
369 struct page **pages; 369 struct page **pages;
370 dma_addr_t *addrs; 370 dma_addr_t *addrs;
371 371
372 pages = vzalloc(dd->cfgctxts * dd->rcvtidcnt * sizeof(struct page *)); 372 pages = vzalloc(array_size(sizeof(struct page *),
373 dd->cfgctxts * dd->rcvtidcnt));
373 if (!pages) 374 if (!pages)
374 goto bail; 375 goto bail;
375 376
376 addrs = vzalloc(dd->cfgctxts * dd->rcvtidcnt * sizeof(dma_addr_t)); 377 addrs = vzalloc(array_size(sizeof(dma_addr_t),
378 dd->cfgctxts * dd->rcvtidcnt));
377 if (!addrs) 379 if (!addrs)
378 goto bail_free; 380 goto bail_free;
379 381
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_cm.c b/drivers/infiniband/ulp/ipoib/ipoib_cm.c
index 962fbcb57dc7..6535d9beb24d 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_cm.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_cm.c
@@ -358,7 +358,8 @@ static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_i
358 int ret; 358 int ret;
359 int i; 359 int i;
360 360
361 rx->rx_ring = vzalloc(ipoib_recvq_size * sizeof *rx->rx_ring); 361 rx->rx_ring = vzalloc(array_size(ipoib_recvq_size,
362 sizeof(*rx->rx_ring)));
362 if (!rx->rx_ring) 363 if (!rx->rx_ring)
363 return -ENOMEM; 364 return -ENOMEM;
364 365
@@ -1145,7 +1146,7 @@ static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
1145 int ret; 1146 int ret;
1146 1147
1147 noio_flag = memalloc_noio_save(); 1148 noio_flag = memalloc_noio_save();
1148 p->tx_ring = vzalloc(ipoib_sendq_size * sizeof(*p->tx_ring)); 1149 p->tx_ring = vzalloc(array_size(ipoib_sendq_size, sizeof(*p->tx_ring)));
1149 if (!p->tx_ring) { 1150 if (!p->tx_ring) {
1150 memalloc_noio_restore(noio_flag); 1151 memalloc_noio_restore(noio_flag);
1151 ret = -ENOMEM; 1152 ret = -ENOMEM;
@@ -1570,7 +1571,8 @@ static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
1570 return; 1571 return;
1571 } 1572 }
1572 1573
1573 priv->cm.srq_ring = vzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring); 1574 priv->cm.srq_ring = vzalloc(array_size(ipoib_recvq_size,
1575 sizeof(*priv->cm.srq_ring)));
1574 if (!priv->cm.srq_ring) { 1576 if (!priv->cm.srq_ring) {
1575 ib_destroy_srq(priv->cm.srq); 1577 ib_destroy_srq(priv->cm.srq);
1576 priv->cm.srq = NULL; 1578 priv->cm.srq = NULL;
diff --git a/drivers/infiniband/ulp/ipoib/ipoib_main.c b/drivers/infiniband/ulp/ipoib/ipoib_main.c
index 0d74c807110e..26cde95bc0f3 100644
--- a/drivers/infiniband/ulp/ipoib/ipoib_main.c
+++ b/drivers/infiniband/ulp/ipoib/ipoib_main.c
@@ -1710,7 +1710,8 @@ static int ipoib_dev_init_default(struct net_device *dev)
1710 if (!priv->rx_ring) 1710 if (!priv->rx_ring)
1711 goto out; 1711 goto out;
1712 1712
1713 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring); 1713 priv->tx_ring = vzalloc(array_size(ipoib_sendq_size,
1714 sizeof(*priv->tx_ring)));
1714 if (!priv->tx_ring) { 1715 if (!priv->tx_ring) {
1715 pr_warn("%s: failed to allocate TX ring (%d entries)\n", 1716 pr_warn("%s: failed to allocate TX ring (%d entries)\n",
1716 priv->ca->name, ipoib_sendq_size); 1717 priv->ca->name, ipoib_sendq_size);