Whamcloud - gitweb
LU-9679 libcfs: Add CFS_ALLOC_PTR_ARRAY and free 75/36975/6
authorMr NeilBrown <neilb@suse.de>
Thu, 14 Nov 2019 02:59:39 +0000 (13:59 +1100)
committerOleg Drokin <green@whamcloud.com>
Thu, 5 Mar 2020 22:35:20 +0000 (22:35 +0000)
Following the pattern of CFS_ALLOC_PTR() and the kernel
pattern of kmalloc_array(), add
  CFS_ALLOC_PTR_ARRAY()
and
  CFS_FREE_PTR_ARRAY()

which allocate and free arrays given a pointer to
the array, and a number of elements

This makes code easier to read and could be a step
toward using the kernel's hardened alloc_array interfaces,
which insure the multiplication doesn't overflow.

Signed-off-by: Mr NeilBrown <neilb@suse.de>
Change-Id: I54c919bd9ce22fbc72715c3da16f8c29e7135ccc
Reviewed-on: https://review.whamcloud.com/36975
Tested-by: jenkins <devops@whamcloud.com>
Reviewed-by: Yang Sheng <ys@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
16 files changed:
libcfs/include/libcfs/libcfs_private.h
libcfs/libcfs/libcfs_cpu.c
libcfs/libcfs/libcfs_string.c
lnet/klnds/gnilnd/gnilnd.c
lnet/klnds/gnilnd/gnilnd_conn.c
lnet/klnds/gnilnd/gnilnd_proc.c
lnet/klnds/o2iblnd/o2iblnd.c
lnet/klnds/socklnd/socklnd_cb.c
lnet/lnet/api-ni.c
lnet/lnet/config.c
lnet/lnet/lib-msg.c
lnet/lnet/lib-ptl.c
lnet/lnet/peer.c
lnet/selftest/console.c
lnet/selftest/module.c
lustre/obdclass/lprocfs_status.c

index 27db6b7..28db507 100644 (file)
@@ -343,8 +343,13 @@ do {                                                            \
 #define LASSERT_ATOMIC_ZERO(a)                  LASSERT_ATOMIC_EQ(a, 0)
 #define LASSERT_ATOMIC_POS(a)                   LASSERT_ATOMIC_GT(a, 0)
 
-#define CFS_ALLOC_PTR(ptr)      LIBCFS_ALLOC(ptr, sizeof (*(ptr)));
-#define CFS_FREE_PTR(ptr)       LIBCFS_FREE(ptr, sizeof (*(ptr)));
+#define CFS_ALLOC_PTR(ptr)      LIBCFS_ALLOC(ptr, sizeof(*(ptr)));
+#define CFS_ALLOC_PTR_ARRAY(ptr, count)                        \
+       LIBCFS_ALLOC(ptr, (count) * sizeof(*(ptr)))
+
+#define CFS_FREE_PTR(ptr)       LIBCFS_FREE(ptr, sizeof(*(ptr)));
+#define CFS_FREE_PTR_ARRAY(ptr, count)                 \
+       LIBCFS_FREE(ptr, (count) * sizeof(*(ptr)))
 
 /* implication */
 #define ergo(a, b) (!(a) || (b))
index ea273a9..808169a 100644 (file)
@@ -86,23 +86,21 @@ struct cfs_cpt_table *cfs_cpt_table_alloc(int ncpt)
        if (!cptab->ctb_nodemask)
                goto failed_alloc_nodemask;
 
-       LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
-                    nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
+       CFS_ALLOC_PTR_ARRAY(cptab->ctb_cpu2cpt, nr_cpu_ids);
        if (!cptab->ctb_cpu2cpt)
                goto failed_alloc_cpu2cpt;
 
        memset(cptab->ctb_cpu2cpt, -1,
               nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
 
-       LIBCFS_ALLOC(cptab->ctb_node2cpt,
-                    nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
+       CFS_ALLOC_PTR_ARRAY(cptab->ctb_node2cpt, nr_node_ids);
        if (!cptab->ctb_node2cpt)
                goto failed_alloc_node2cpt;
 
        memset(cptab->ctb_node2cpt, -1,
               nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
 
-       LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
+       CFS_ALLOC_PTR_ARRAY(cptab->ctb_parts, ncpt);
        if (!cptab->ctb_parts)
                goto failed_alloc_ctb_parts;
 
@@ -118,8 +116,7 @@ struct cfs_cpt_table *cfs_cpt_table_alloc(int ncpt)
                if (!part->cpt_nodemask)
                        goto failed_setting_ctb_parts;
 
-               LIBCFS_ALLOC(part->cpt_distance,
-                            cptab->ctb_nparts * sizeof(part->cpt_distance[0]));
+               CFS_ALLOC_PTR_ARRAY(part->cpt_distance, cptab->ctb_nparts);
                if (!part->cpt_distance)
                        goto failed_setting_ctb_parts;
 
@@ -141,26 +138,22 @@ failed_setting_ctb_parts:
                free_cpumask_var(part->cpt_cpumask);
 
                if (part->cpt_distance) {
-                       LIBCFS_FREE(part->cpt_distance,
-                               cptab->ctb_nparts *
-                                       sizeof(part->cpt_distance[0]));
+                       CFS_FREE_PTR_ARRAY(part->cpt_distance,
+                                          cptab->ctb_nparts);
                }
        }
 
-       if (cptab->ctb_parts) {
-               LIBCFS_FREE(cptab->ctb_parts,
-                           cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
-       }
+       if (cptab->ctb_parts)
+               CFS_FREE_PTR_ARRAY(cptab->ctb_parts, cptab->ctb_nparts);
+
 failed_alloc_ctb_parts:
-       if (cptab->ctb_node2cpt) {
-               LIBCFS_FREE(cptab->ctb_node2cpt,
-                           nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
-       }
+       if (cptab->ctb_node2cpt)
+               CFS_FREE_PTR_ARRAY(cptab->ctb_node2cpt, nr_node_ids);
+
 failed_alloc_node2cpt:
-       if (cptab->ctb_cpu2cpt) {
-               LIBCFS_FREE(cptab->ctb_cpu2cpt,
-                           nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
-       }
+       if (cptab->ctb_cpu2cpt)
+               CFS_FREE_PTR_ARRAY(cptab->ctb_cpu2cpt, nr_cpu_ids);
+
 failed_alloc_cpu2cpt:
        if (cptab->ctb_nodemask)
                LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
@@ -176,15 +169,11 @@ void cfs_cpt_table_free(struct cfs_cpt_table *cptab)
 {
        int i;
 
-       if (cptab->ctb_cpu2cpt) {
-               LIBCFS_FREE(cptab->ctb_cpu2cpt,
-                           nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
-       }
+       if (cptab->ctb_cpu2cpt)
+               CFS_FREE_PTR_ARRAY(cptab->ctb_cpu2cpt, nr_cpu_ids);
 
-       if (cptab->ctb_node2cpt) {
-               LIBCFS_FREE(cptab->ctb_node2cpt,
-                           nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
-       }
+       if (cptab->ctb_node2cpt)
+               CFS_FREE_PTR_ARRAY(cptab->ctb_node2cpt, nr_node_ids);
 
        for (i = 0; cptab->ctb_parts && i < cptab->ctb_nparts; i++) {
                struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
@@ -196,17 +185,13 @@ void cfs_cpt_table_free(struct cfs_cpt_table *cptab)
 
                free_cpumask_var(part->cpt_cpumask);
 
-               if (part->cpt_distance) {
-                       LIBCFS_FREE(part->cpt_distance,
-                               cptab->ctb_nparts *
-                                       sizeof(part->cpt_distance[0]));
-               }
+               if (part->cpt_distance)
+                       CFS_FREE_PTR_ARRAY(part->cpt_distance,
+                                          cptab->ctb_nparts);
        }
 
-       if (cptab->ctb_parts) {
-               LIBCFS_FREE(cptab->ctb_parts,
-                           cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
-       }
+       if (cptab->ctb_parts)
+               CFS_FREE_PTR_ARRAY(cptab->ctb_parts, cptab->ctb_nparts);
 
        if (cptab->ctb_nodemask)
                LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
index ee39d23..c6368de 100644 (file)
@@ -475,7 +475,7 @@ cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp)
                return -EINVAL;
        }
 
-       LIBCFS_ALLOC(val, sizeof(val[0]) * count);
+       CFS_ALLOC_PTR_ARRAY(val, count);
        if (val == NULL)
                return -ENOMEM;
 
@@ -498,7 +498,7 @@ cfs_expr_list_values_free(__u32 *values, int num)
        /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
         * by OBD_FREE() if it's called by module other than libcfs & LNet,
         * otherwise we will see fake memory leak */
-       LIBCFS_FREE(values, num * sizeof(values[0]));
+       CFS_FREE_PTR_ARRAY(values, num);
 }
 EXPORT_SYMBOL(cfs_expr_list_values_free);
 
index 14fb5d6..51f98e9 100644 (file)
@@ -576,7 +576,7 @@ kgnilnd_peer_notify(kgn_peer_t *peer, int error, int alive)
                        return;
                }
 
-               LIBCFS_ALLOC(nets, nnets * sizeof(*nets));
+               CFS_ALLOC_PTR_ARRAY(nets, nnets);
 
                if (nets == NULL) {
                        up_read(&kgnilnd_data.kgn_net_rw_sem);
@@ -613,7 +613,7 @@ kgnilnd_peer_notify(kgn_peer_t *peer, int error, int alive)
                        kgnilnd_net_decref(net);
                }
 
-               LIBCFS_FREE(nets, nnets * sizeof(*nets));
+               CFS_FREE_PTR_ARRAY(nets, nnets);
        }
 }
 
@@ -2192,8 +2192,8 @@ int kgnilnd_base_startup(void)
                init_rwsem(&dev->gnd_conn_sem);
 
                /* alloc & setup nid based dgram table */
-               LIBCFS_ALLOC(dev->gnd_dgrams,
-                           sizeof(struct list_head) * *kgnilnd_tunables.kgn_peer_hash_size);
+               CFS_ALLOC_PTR_ARRAY(dev->gnd_dgrams,
+                                   *kgnilnd_tunables.kgn_peer_hash_size);
 
                if (dev->gnd_dgrams == NULL)
                        GOTO(failed, rc = -ENOMEM);
@@ -2236,8 +2236,8 @@ int kgnilnd_base_startup(void)
 
        rwlock_init(&kgnilnd_data.kgn_peer_conn_lock);
 
-       LIBCFS_ALLOC(kgnilnd_data.kgn_peers,
-                   sizeof(struct list_head) * *kgnilnd_tunables.kgn_peer_hash_size);
+       CFS_ALLOC_PTR_ARRAY(kgnilnd_data.kgn_peers,
+                           *kgnilnd_tunables.kgn_peer_hash_size);
 
        if (kgnilnd_data.kgn_peers == NULL)
                GOTO(failed, rc = -ENOMEM);
@@ -2246,8 +2246,8 @@ int kgnilnd_base_startup(void)
                INIT_LIST_HEAD(&kgnilnd_data.kgn_peers[i]);
        }
 
-       LIBCFS_ALLOC(kgnilnd_data.kgn_conns,
-                   sizeof(struct list_head) * *kgnilnd_tunables.kgn_peer_hash_size);
+       CFS_ALLOC_PTR_ARRAY(kgnilnd_data.kgn_conns,
+                           *kgnilnd_tunables.kgn_peer_hash_size);
 
        if (kgnilnd_data.kgn_conns == NULL)
                GOTO(failed, rc = -ENOMEM);
@@ -2256,8 +2256,8 @@ int kgnilnd_base_startup(void)
                INIT_LIST_HEAD(&kgnilnd_data.kgn_conns[i]);
        }
 
-       LIBCFS_ALLOC(kgnilnd_data.kgn_nets,
-                   sizeof(struct list_head) * *kgnilnd_tunables.kgn_net_hash_size);
+       CFS_ALLOC_PTR_ARRAY(kgnilnd_data.kgn_nets,
+                           *kgnilnd_tunables.kgn_net_hash_size);
 
        if (kgnilnd_data.kgn_nets == NULL)
                GOTO(failed, rc = -ENOMEM);
@@ -2524,9 +2524,8 @@ kgnilnd_base_shutdown(void)
                for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
                        LASSERT(list_empty(&kgnilnd_data.kgn_peers[i]));
 
-               LIBCFS_FREE(kgnilnd_data.kgn_peers,
-                           sizeof (struct list_head) *
-                           *kgnilnd_tunables.kgn_peer_hash_size);
+               CFS_FREE_PTRE_ARRAT(kgnilnd_data.kgn_peers,
+                                   *kgnilnd_tunables.kgn_peer_hash_size);
        }
 
        down_write(&kgnilnd_data.kgn_net_rw_sem);
@@ -2534,9 +2533,8 @@ kgnilnd_base_shutdown(void)
                for (i = 0; i < *kgnilnd_tunables.kgn_net_hash_size; i++)
                        LASSERT(list_empty(&kgnilnd_data.kgn_nets[i]));
 
-               LIBCFS_FREE(kgnilnd_data.kgn_nets,
-                           sizeof (struct list_head) *
-                           *kgnilnd_tunables.kgn_net_hash_size);
+               CFS_FREE_PTRE_ARRAY(kgnilnd_data.kgn_nets,
+                                   *kgnilnd_tunables.kgn_net_hash_size);
        }
        up_write(&kgnilnd_data.kgn_net_rw_sem);
 
@@ -2547,9 +2545,8 @@ kgnilnd_base_shutdown(void)
                for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
                        LASSERT(list_empty(&kgnilnd_data.kgn_conns[i]));
 
-               LIBCFS_FREE(kgnilnd_data.kgn_conns,
-                           sizeof (struct list_head) *
-                           *kgnilnd_tunables.kgn_peer_hash_size);
+               CFS_FREE_PTR_ARRAY(kgnilnd_data.kgn_conns,
+                                  *kgnilnd_tunables.kgn_peer_hash_size);
        }
 
        for (i = 0; i < kgnilnd_data.kgn_ndevs; i++) {
@@ -2560,12 +2557,12 @@ kgnilnd_base_shutdown(void)
                        "dgrams left %d\n", atomic_read(&dev->gnd_ndgrams));
 
                if (dev->gnd_dgrams != NULL) {
-                       for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size; i++)
+                       for (i = 0; i < *kgnilnd_tunables.kgn_peer_hash_size;
+                            i++)
                                LASSERT(list_empty(&dev->gnd_dgrams[i]));
 
-                       LIBCFS_FREE(dev->gnd_dgrams,
-                                   sizeof (struct list_head) *
-                                   *kgnilnd_tunables.kgn_peer_hash_size);
+                       CFS_FREE_PTR_ARRAY(dev->gnd_dgrams,
+                                          *kgnilnd_tunables.kgn_peer_hash_size);
                }
 
                kgnilnd_free_phys_fmablk(dev);
index efb6d3d..cdd7310 100644 (file)
@@ -190,7 +190,7 @@ kgnilnd_alloc_fmablk(kgn_device_t *device, int use_phys)
        }
 
        /* allocate just enough space for the bits to track the mailboxes */
-       LIBCFS_ALLOC(fma_blk->gnm_bit_array, BITS_TO_LONGS(num_mbox) * sizeof(unsigned long));
+       CFS_ALLOC_PTR_ARRAY(fma_blk->gnm_bit_array, BITS_TO_LONGS(num_mbox));
        if (fma_blk->gnm_bit_array == NULL) {
                CNETERR("could not allocate mailbox bitmask, %lu bytes for %d mbox\n",
                       sizeof(unsigned long) * BITS_TO_LONGS(num_mbox), num_mbox);
@@ -199,8 +199,10 @@ kgnilnd_alloc_fmablk(kgn_device_t *device, int use_phys)
        }
        bitmap_zero(fma_blk->gnm_bit_array, num_mbox);
 
-       /* now that the num_mbox is set based on allocation type, get debug info setup */
-       LIBCFS_ALLOC(fma_blk->gnm_mbox_info, sizeof(kgn_mbox_info_t) * num_mbox);
+       /* now that the num_mbox is set based on allocation type, get debug
+        * info setup
+        * */
+       CFS_ALLOC_PTR_ARRAY(fma_blk->gnm_mbox_info, num_mbox);
        if (fma_blk->gnm_mbox_info == NULL) {
                CNETERR("could not allocate mailbox debug, %lu bytes for %d mbox\n",
                       sizeof(kgn_mbox_info_t) * num_mbox, num_mbox);
@@ -238,9 +240,9 @@ kgnilnd_alloc_fmablk(kgn_device_t *device, int use_phys)
        return 0;
 
 free_info:
-       LIBCFS_FREE(fma_blk->gnm_mbox_info, sizeof(kgn_mbox_info_t)*num_mbox);
+       CFS_FREE_PTR_ARRAY(fma_blk->gnm_mbox_info, num_mbox);
 free_bit:
-       LIBCFS_FREE(fma_blk->gnm_bit_array, BITS_TO_LONGS(num_mbox) * sizeof (unsigned long));
+       CFS_FREE_PTR_ARRAY(fma_blk->gnm_bit_array, BITS_TO_LONGS(num_mbox));
 free_blk:
        if (fma_blk->gnm_state == GNILND_FMABLK_VIRT) {
                kgnilnd_vfree(fma_blk->gnm_block, fma_blk->gnm_blk_size);
@@ -353,8 +355,9 @@ kgnilnd_free_fmablk_locked(kgn_device_t *dev, kgn_fma_memblock_t *fma_blk)
 
        list_del(&fma_blk->gnm_bufflist);
 
-       LIBCFS_FREE(fma_blk->gnm_mbox_info, sizeof(kgn_mbox_info_t)*fma_blk->gnm_num_mboxs);
-       LIBCFS_FREE(fma_blk->gnm_bit_array, BITS_TO_LONGS(fma_blk->gnm_num_mboxs) * sizeof (unsigned long));
+       CFS_FREE_PTR_ARRAY(fma_blk->gnm_mbox_info, fma_blk->gnm_num_mboxs);
+       CFS_FREE_PTR_ARRAY(fma_blk->gnm_bit_array,
+                          BITS_TO_LONGS(fma_blk->gnm_num_mboxs));
        LIBCFS_FREE(fma_blk, sizeof(kgn_fma_memblock_t));
 }
 
index eb99fd2..3cab86a 100644 (file)
@@ -48,8 +48,8 @@ _kgnilnd_proc_run_cksum_test(int caseno, int nloops, int nob)
        __u16                    cksum, cksum2;
        __u64                    mbytes;
 
-       LIBCFS_ALLOC(src, LNET_MAX_IOV * sizeof(lnet_kiov_t));
-       LIBCFS_ALLOC(dest, LNET_MAX_IOV * sizeof(lnet_kiov_t));
+       CFS_ALLOC_PTR_ARRAY(src, LNET_MAX_IOV);
+       CFS_ALLOC_PTR_ARRAY(dest, LNET_MAX_IOV);
 
        if (src == NULL || dest == NULL) {
                CERROR("couldn't allocate iovs\n");
@@ -150,9 +150,9 @@ unwind:
        }
 
        if (src != NULL)
-               LIBCFS_FREE(src, LNET_MAX_IOV * sizeof(lnet_kiov_t));
+               CFS_FREE_PTR_ARRAY(src, LNET_MAX_IOV);
        if (dest != NULL)
-               LIBCFS_FREE(dest, LNET_MAX_IOV * sizeof(lnet_kiov_t));
+               CFS_FREE_PTR_ARRAY(dest, LNET_MAX_IOV);
        return rc;
 }
 
index dc6d592..dae653b 100644 (file)
@@ -1050,10 +1050,8 @@ kiblnd_destroy_conn(struct kib_conn *conn)
        if (conn->ibc_rx_pages != NULL)
                kiblnd_unmap_rx_descs(conn);
 
-       if (conn->ibc_rxs != NULL) {
-               LIBCFS_FREE(conn->ibc_rxs,
-                           IBLND_RX_MSGS(conn) * sizeof(struct kib_rx));
-       }
+       if (conn->ibc_rxs != NULL)
+               CFS_FREE_PTR_ARRAY(conn->ibc_rxs, IBLND_RX_MSGS(conn));
 
        if (conn->ibc_connvars != NULL)
                LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
@@ -2228,37 +2226,32 @@ kiblnd_destroy_tx_pool(struct kib_pool *pool)
         if (tpo->tpo_tx_descs == NULL)
                 goto out;
 
-        for (i = 0; i < pool->po_size; i++) {
+       for (i = 0; i < pool->po_size; i++) {
                struct kib_tx *tx = &tpo->tpo_tx_descs[i];
                int       wrq_sge = *kiblnd_tunables.kib_wrq_sge;
 
                list_del(&tx->tx_list);
-                if (tx->tx_pages != NULL)
-                        LIBCFS_FREE(tx->tx_pages,
-                                    LNET_MAX_IOV *
-                                    sizeof(*tx->tx_pages));
-                if (tx->tx_frags != NULL)
-                        LIBCFS_FREE(tx->tx_frags,
-                                   (1 + IBLND_MAX_RDMA_FRAGS) *
-                                   sizeof(*tx->tx_frags));
-                if (tx->tx_wrq != NULL)
-                        LIBCFS_FREE(tx->tx_wrq,
-                                    (1 + IBLND_MAX_RDMA_FRAGS) *
-                                    sizeof(*tx->tx_wrq));
+               if (tx->tx_pages != NULL)
+                       CFS_FREE_PTR_ARRAY(tx->tx_pages, LNET_MAX_IOV);
+               if (tx->tx_frags != NULL)
+                       CFS_FREE_PTR_ARRAY(tx->tx_frags,
+                                          (1 + IBLND_MAX_RDMA_FRAGS));
+               if (tx->tx_wrq != NULL)
+                       CFS_FREE_PTR_ARRAY(tx->tx_wrq,
+                                          (1 + IBLND_MAX_RDMA_FRAGS));
                if (tx->tx_sge != NULL)
-                       LIBCFS_FREE(tx->tx_sge,
-                                   (1 + IBLND_MAX_RDMA_FRAGS) * wrq_sge *
-                                   sizeof(*tx->tx_sge));
-                if (tx->tx_rd != NULL)
-                        LIBCFS_FREE(tx->tx_rd,
+                       CFS_FREE_PTR_ARRAY(tx->tx_sge,
+                                          (1 + IBLND_MAX_RDMA_FRAGS) *
+                                          wrq_sge);
+               if (tx->tx_rd != NULL)
+                       LIBCFS_FREE(tx->tx_rd,
                                    offsetof(struct kib_rdma_desc,
-                                             rd_frags[IBLND_MAX_RDMA_FRAGS]));
-        }
+                                            rd_frags[IBLND_MAX_RDMA_FRAGS]));
+       }
 
-        LIBCFS_FREE(tpo->tpo_tx_descs,
-                   pool->po_size * sizeof(struct kib_tx));
+       CFS_FREE_PTR_ARRAY(tpo->tpo_tx_descs, pool->po_size);
 out:
-        kiblnd_fini_pool(pool);
+       kiblnd_fini_pool(pool);
        CFS_FREE_PTR(tpo);
 }
 
@@ -2995,11 +2988,9 @@ kiblnd_base_shutdown(void)
                 break;
         }
 
-       if (kiblnd_data.kib_peers != NULL) {
-               LIBCFS_FREE(kiblnd_data.kib_peers,
-                           sizeof(struct list_head) *
-                           kiblnd_data.kib_peer_hash_size);
-       }
+       if (kiblnd_data.kib_peers)
+               CFS_FREE_PTR_ARRAY(kiblnd_data.kib_peers,
+                                  kiblnd_data.kib_peer_hash_size);
 
        if (kiblnd_data.kib_scheds != NULL)
                cfs_percpt_free(kiblnd_data.kib_scheds);
@@ -3104,9 +3095,8 @@ kiblnd_base_startup(struct net *ns)
        INIT_LIST_HEAD(&kiblnd_data.kib_failed_devs);
 
        kiblnd_data.kib_peer_hash_size = IBLND_PEER_HASH_SIZE;
-       LIBCFS_ALLOC(kiblnd_data.kib_peers,
-                    sizeof(struct list_head) *
-                    kiblnd_data.kib_peer_hash_size);
+       CFS_ALLOC_PTR_ARRAY(kiblnd_data.kib_peers,
+                           kiblnd_data.kib_peer_hash_size);
        if (kiblnd_data.kib_peers == NULL)
                goto failed;
 
index 8839f31..9009af3 100644 (file)
@@ -1627,10 +1627,8 @@ int ksocknal_scheduler(void *arg)
        }
 
        spin_unlock_bh(&sched->kss_lock);
-       LIBCFS_FREE(rx_scratch_pgs, sizeof(*rx_scratch_pgs) *
-                   LNET_MAX_IOV);
-       LIBCFS_FREE(scratch_iov, sizeof(*scratch_iov) *
-                   LNET_MAX_IOV);
+       CFS_FREE_PTR_ARRAY(rx_scratch_pgs, LNET_MAX_IOV);
+       CFS_FREE_PTR_ARRAY(scratch_iov, LNET_MAX_IOV);
        ksocknal_thread_fini();
        return 0;
 }
index 2ee5970..6ced2aa 100644 (file)
@@ -622,7 +622,7 @@ lnet_create_remote_nets_table(void)
 
        LASSERT(the_lnet.ln_remote_nets_hash == NULL);
        LASSERT(the_lnet.ln_remote_nets_hbits > 0);
-       LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
+       CFS_ALLOC_PTR_ARRAY(hash, LNET_REMOTE_NETS_HASH_SIZE);
        if (hash == NULL) {
                CERROR("Failed to create remote nets hash table\n");
                return -ENOMEM;
@@ -645,9 +645,8 @@ lnet_destroy_remote_nets_table(void)
        for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
                LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
 
-       LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
-                   LNET_REMOTE_NETS_HASH_SIZE *
-                   sizeof(the_lnet.ln_remote_nets_hash[0]));
+       CFS_FREE_PTR_ARRAY(the_lnet.ln_remote_nets_hash,
+                          LNET_REMOTE_NETS_HASH_SIZE);
        the_lnet.ln_remote_nets_hash = NULL;
 }
 
@@ -997,8 +996,7 @@ lnet_res_container_cleanup(struct lnet_res_container *rec)
        }
 
        if (rec->rec_lh_hash != NULL) {
-               LIBCFS_FREE(rec->rec_lh_hash,
-                           LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
+               CFS_FREE_PTR_ARRAY(rec->rec_lh_hash, LNET_LH_HASH_SIZE);
                rec->rec_lh_hash = NULL;
        }
 
@@ -4282,7 +4280,6 @@ lnet_discover(struct lnet_process_id id, __u32 force,
        int i;
        int rc;
        int max_intf = lnet_interfaces_max;
-       size_t buf_size;
 
        if (n_ids <= 0 ||
            id.nid == LNET_NID_ANY)
@@ -4298,9 +4295,7 @@ lnet_discover(struct lnet_process_id id, __u32 force,
        if (n_ids > max_intf)
                n_ids = max_intf;
 
-       buf_size = n_ids * sizeof(*buf);
-
-       LIBCFS_ALLOC(buf, buf_size);
+       CFS_ALLOC_PTR_ARRAY(buf, n_ids);
        if (!buf)
                return -ENOMEM;
 
@@ -4353,7 +4348,7 @@ out_decref:
 out:
        lnet_net_unlock(cpt);
 
-       LIBCFS_FREE(buf, buf_size);
+       CFS_FREE_PTR_ARRAY(buf, n_ids);
 
        return rc;
 }
index 7b7d775..7ffa2e1 100644 (file)
@@ -165,15 +165,14 @@ lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
        if (cpts == NULL) {
                /* there is an NI which will exist on all CPTs */
                if (net->net_cpts != NULL)
-                       LIBCFS_FREE(net->net_cpts, sizeof(*net->net_cpts) *
-                                   net->net_ncpts);
+                       CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
                net->net_cpts = NULL;
                net->net_ncpts = LNET_CPT_NUMBER;
                return 0;
        }
 
        if (net->net_cpts == NULL) {
-               LIBCFS_ALLOC(net->net_cpts, sizeof(*net->net_cpts) * ncpts);
+               CFS_ALLOC_PTR_ARRAY(net->net_cpts, ncpts);
                if (net->net_cpts == NULL)
                        return -ENOMEM;
                memcpy(net->net_cpts, cpts, ncpts * sizeof(*net->net_cpts));
@@ -181,7 +180,7 @@ lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
                return 0;
        }
 
-       LIBCFS_ALLOC(added_cpts, sizeof(*added_cpts) * LNET_CPT_NUMBER);
+       CFS_ALLOC_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
        if (added_cpts == NULL)
                return -ENOMEM;
 
@@ -197,7 +196,7 @@ lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
                __u32 *array = NULL, *loc;
                __u32 total_entries = j + net->net_ncpts;
 
-               LIBCFS_ALLOC(array, sizeof(*net->net_cpts) * total_entries);
+               CFS_ALLOC_PTR_ARRAY(array, total_entries);
                if (array == NULL) {
                        rc = -ENOMEM;
                        goto failed;
@@ -208,14 +207,13 @@ lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
                loc = array + net->net_ncpts;
                memcpy(loc, added_cpts, j * sizeof(*net->net_cpts));
 
-               LIBCFS_FREE(net->net_cpts, sizeof(*net->net_cpts) *
-                           net->net_ncpts);
+               CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
                net->net_ncpts = total_entries;
                net->net_cpts = array;
        }
 
 failed:
-       LIBCFS_FREE(added_cpts, sizeof(*added_cpts) * LNET_CPT_NUMBER);
+       CFS_FREE_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
 
        return rc;
 }
@@ -266,8 +264,7 @@ lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
         * CPTs which the remaining NIs are associated with.
         */
        if (net->net_cpts != NULL) {
-               LIBCFS_FREE(net->net_cpts,
-                       sizeof(*net->net_cpts) * net->net_ncpts);
+               CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
                net->net_cpts = NULL;
        }
 
@@ -284,9 +281,8 @@ lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
                         * accross CPT lines.
                         */
                        if (net->net_cpts != NULL) {
-                               LIBCFS_FREE(net->net_cpts,
-                                               sizeof(*net->net_cpts) *
-                                               net->net_ncpts);
+                               CFS_FREE_PTR_ARRAY(net->net_cpts,
+                                                  net->net_ncpts);
                                net->net_cpts = NULL;
                                net->net_ncpts = LNET_CPT_NUMBER;
                        }
@@ -350,8 +346,7 @@ lnet_net_free(struct lnet_net *net)
        }
 
        if (net->net_cpts != NULL)
-               LIBCFS_FREE(net->net_cpts,
-                           sizeof(*net->net_cpts) * net->net_ncpts);
+               CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
 
        LIBCFS_FREE(net, sizeof(*net));
 }
@@ -527,7 +522,7 @@ lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el, char *iface)
 
                LASSERT(rc <= LNET_CPT_NUMBER);
                if (rc == LNET_CPT_NUMBER) {
-                       LIBCFS_FREE(ni->ni_cpts, rc * sizeof(ni->ni_cpts[0]));
+                       CFS_FREE_PTR_ARRAY(ni->ni_cpts, rc);
                        ni->ni_cpts = NULL;
                }
 
@@ -560,7 +555,8 @@ lnet_ni_alloc_w_cpt_array(struct lnet_net *net, __u32 *cpts, __u32 ncpts,
                ni->ni_ncpts = LNET_CPT_NUMBER;
        } else {
                size_t array_size = ncpts * sizeof(ni->ni_cpts[0]);
-               LIBCFS_ALLOC(ni->ni_cpts, array_size);
+
+               CFS_ALLOC_PTR_ARRAY(ni->ni_cpts, ncpts);
                if (ni->ni_cpts == NULL)
                        goto failed;
                memcpy(ni->ni_cpts, cpts, array_size);
@@ -1688,7 +1684,7 @@ lnet_parse_ip2nets (char **networksp, char *ip2nets)
                return nip;
        }
 
-       LIBCFS_ALLOC(ipaddrs, nip * sizeof(*ipaddrs));
+       CFS_ALLOC_PTR_ARRAY(ipaddrs, nip);
        if (!ipaddrs) {
                rc = -ENOMEM;
                CERROR("lnet: Can't allocate ipaddrs[%d], rc = %d\n",
@@ -1707,7 +1703,7 @@ lnet_parse_ip2nets (char **networksp, char *ip2nets)
                                   "any local IP interfaces\n");
                rc = -ENOENT;
        }
-       LIBCFS_FREE(ipaddrs, nip * sizeof(*ipaddrs));
+       CFS_FREE_PTR_ARRAY(ipaddrs, nip);
 out_free_addrs:
        kfree(ifaces);
        return rc > 0 ? 0 : rc;
index a00ba31..d1d3835 100644 (file)
@@ -1162,16 +1162,14 @@ lnet_msg_container_cleanup(struct lnet_msg_container *container)
                CERROR("%d active msg on exit\n", count);
 
        if (container->msc_finalizers != NULL) {
-               LIBCFS_FREE(container->msc_finalizers,
-                           container->msc_nfinalizers *
-                           sizeof(*container->msc_finalizers));
+               CFS_FREE_PTR_ARRAY(container->msc_finalizers,
+                                  container->msc_nfinalizers);
                container->msc_finalizers = NULL;
        }
 
        if (container->msc_resenders != NULL) {
-               LIBCFS_FREE(container->msc_resenders,
-                           container->msc_nfinalizers *
-                           sizeof(*container->msc_resenders));
+               CFS_FREE_PTR_ARRAY(container->msc_resenders,
+                                  container->msc_nfinalizers);
                container->msc_resenders = NULL;
        }
        container->msc_init = 0;
index 560ef06..abdd6ae 100644 (file)
@@ -775,7 +775,7 @@ lnet_ptl_cleanup(struct lnet_portal *ptl)
                        }
                }
                /* the extra entry is for MEs with ignore bits */
-               LIBCFS_FREE(mhash, sizeof(*mhash) * (LNET_MT_HASH_SIZE + 1));
+               CFS_FREE_PTR_ARRAY(mhash, LNET_MT_HASH_SIZE + 1);
        }
 
        cfs_percpt_free(ptl->ptl_mtables);
index 19f256b..e4a6d49 100644 (file)
@@ -104,7 +104,7 @@ lnet_peer_tables_destroy(void)
                for (j = 0; j < LNET_PEER_HASH_SIZE; j++)
                        LASSERT(list_empty(&hash[j]));
 
-               LIBCFS_FREE(hash, LNET_PEER_HASH_SIZE * sizeof(*hash));
+               CFS_FREE_PTR_ARRAY(hash, LNET_PEER_HASH_SIZE);
        }
 
        cfs_percpt_free(the_lnet.ln_peer_tables);
@@ -1049,7 +1049,7 @@ lnet_peer_add_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
 
        if (oldnids) {
                size = sizeof(*nids) * (lpni->lpni_pref_nnids - 1);
-               LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
+               CFS_FREE_PTR_ARRAY(oldnids, size);
        }
 out:
        if (rc == -EEXIST && (lpni->lpni_state & LNET_PEER_NI_NON_MR_PREF)) {
@@ -1129,7 +1129,7 @@ lnet_peer_del_pref_nid(struct lnet_peer_ni *lpni, lnet_nid_t nid)
 
        if (oldnids) {
                size = sizeof(*nids) * (lpni->lpni_pref_nnids + 1);
-               LIBCFS_FREE(oldnids, sizeof(*oldnids) * size);
+               CFS_FREE_PTR_ARRAY(oldnids, size);
        }
 out:
        CDEBUG(D_NET, "peer %s nid %s: %d\n",
@@ -1720,10 +1720,9 @@ lnet_destroy_peer_ni_locked(struct lnet_peer_ni *lpni)
        ptable->pt_zombies--;
        spin_unlock(&ptable->pt_zombie_lock);
 
-       if (lpni->lpni_pref_nnids > 1) {
-               LIBCFS_FREE(lpni->lpni_pref.nids,
-                       sizeof(*lpni->lpni_pref.nids) * lpni->lpni_pref_nnids);
-       }
+       if (lpni->lpni_pref_nnids > 1)
+               CFS_FREE_PTR_ARRAY(lpni->lpni_pref.nids, lpni->lpni_pref_nnids);
+
        LIBCFS_FREE(lpni, sizeof(*lpni));
 
        lnet_peer_net_decref_locked(lpn);
@@ -2584,9 +2583,9 @@ static int lnet_peer_merge_data(struct lnet_peer *lp,
        spin_unlock(&lp->lp_lock);
 
        nnis = max_t(int, lp->lp_nnis, pbuf->pb_info.pi_nnis);
-       LIBCFS_ALLOC(curnis, nnis * sizeof(*curnis));
-       LIBCFS_ALLOC(addnis, nnis * sizeof(*addnis));
-       LIBCFS_ALLOC(delnis, nnis * sizeof(*delnis));
+       CFS_ALLOC_PTR_ARRAY(curnis, nnis);
+       CFS_ALLOC_PTR_ARRAY(addnis, nnis);
+       CFS_ALLOC_PTR_ARRAY(delnis, nnis);
        if (!curnis || !addnis || !delnis) {
                rc = -ENOMEM;
                goto out;
@@ -2690,9 +2689,9 @@ static int lnet_peer_merge_data(struct lnet_peer *lp,
         */
        rc = 0;
 out:
-       LIBCFS_FREE(curnis, nnis * sizeof(*curnis));
-       LIBCFS_FREE(addnis, nnis * sizeof(*addnis));
-       LIBCFS_FREE(delnis, nnis * sizeof(*delnis));
+       CFS_FREE_PTR_ARRAY(curnis, nnis);
+       CFS_FREE_PTR_ARRAY(addnis, nnis);
+       CFS_FREE_PTR_ARRAY(delnis, nnis);
        lnet_ping_buffer_decref(pbuf);
        CDEBUG(D_NET, "peer %s (%p): %d\n", libcfs_nid2str(lp->lp_primary_nid), lp, rc);
 
index ccaced7..266c24e 100644 (file)
@@ -855,24 +855,22 @@ lstcon_batch_add(char *name)
                 return -ENOMEM;
         }
 
-       LIBCFS_ALLOC(bat->bat_cli_hash,
-                    sizeof(struct list_head) * LST_NODE_HASHSIZE);
+       CFS_ALLOC_PTR_ARRAY(bat->bat_cli_hash, LST_NODE_HASHSIZE);
        if (bat->bat_cli_hash == NULL) {
                CERROR("Can't allocate hash for batch %s\n", name);
                LIBCFS_FREE(bat, sizeof(*bat));
 
-                return -ENOMEM;
-        }
+               return -ENOMEM;
+       }
 
-        LIBCFS_ALLOC(bat->bat_srv_hash,
-                    sizeof(struct list_head) * LST_NODE_HASHSIZE);
-        if (bat->bat_srv_hash == NULL) {
-                CERROR("Can't allocate hash for batch %s\n", name);
-                LIBCFS_FREE(bat->bat_cli_hash, LST_NODE_HASHSIZE);
+       CFS_ALLOC_PTR_ARRAY(bat->bat_srv_hash, LST_NODE_HASHSIZE);
+       if (bat->bat_srv_hash == NULL) {
+               CERROR("Can't allocate hash for batch %s\n", name);
+               LIBCFS_FREE(bat->bat_cli_hash, LST_NODE_HASHSIZE);
                LIBCFS_FREE(bat, sizeof(*bat));
 
-                return -ENOMEM;
-        }
+               return -ENOMEM;
+       }
 
        if (strlen(name) > sizeof(bat->bat_name)-1) {
                LIBCFS_FREE(bat->bat_srv_hash, LST_NODE_HASHSIZE);
@@ -2025,8 +2023,8 @@ lstcon_console_init(void)
        INIT_LIST_HEAD(&console_session.ses_bat_list);
        INIT_LIST_HEAD(&console_session.ses_trans_list);
 
-       LIBCFS_ALLOC(console_session.ses_ndl_hash,
-                    sizeof(struct list_head) * LST_GLOBAL_HASHSIZE);
+       CFS_ALLOC_PTR_ARRAY(console_session.ses_ndl_hash,
+                              LST_GLOBAL_HASHSIZE);
        if (console_session.ses_ndl_hash == NULL)
                return -ENOMEM;
 
@@ -2040,8 +2038,8 @@ lstcon_console_init(void)
        rc = srpc_add_service(&lstcon_acceptor_service);
        LASSERT(rc != -EBUSY);
        if (rc != 0) {
-               LIBCFS_FREE(console_session.ses_ndl_hash,
-                           sizeof(struct list_head) * LST_GLOBAL_HASHSIZE);
+               CFS_FREE_PTR_ARRAY(console_session.ses_ndl_hash,
+                                  LST_GLOBAL_HASHSIZE);
                return rc;
        }
 
@@ -2063,8 +2061,7 @@ out:
        srpc_shutdown_service(&lstcon_acceptor_service);
        srpc_remove_service(&lstcon_acceptor_service);
 
-       LIBCFS_FREE(console_session.ses_ndl_hash,
-                   sizeof(struct list_head) * LST_GLOBAL_HASHSIZE);
+       CFS_FREE_PTR_ARRAY(console_session.ses_ndl_hash, LST_GLOBAL_HASHSIZE);
 
        srpc_wait_service_shutdown(&lstcon_acceptor_service);
 
@@ -2099,8 +2096,8 @@ lstcon_console_fini(void)
        for (i = 0; i < LST_NODE_HASHSIZE; i++)
                LASSERT(list_empty(&console_session.ses_ndl_hash[i]));
 
-       LIBCFS_FREE(console_session.ses_ndl_hash,
-                   sizeof(struct list_head) * LST_GLOBAL_HASHSIZE);
+       CFS_FREE_PTR_ARRAY(console_session.ses_ndl_hash,
+                          LST_GLOBAL_HASHSIZE);
 
        srpc_wait_service_shutdown(&lstcon_acceptor_service);
 
index a6a06d1..9d77563 100644 (file)
@@ -71,9 +71,8 @@ lnet_selftest_exit(void)
                                continue;
                        cfs_wi_sched_destroy(lst_sched_test[i]);
                }
-               LIBCFS_FREE(lst_sched_test,
-                           sizeof(lst_sched_test[0]) *
-                           cfs_cpt_number(lnet_cpt_table()));
+               CFS_FREE_PTR_ARRAY(lst_sched_test,
+                                  cfs_cpt_number(lnet_cpt_table()));
                lst_sched_test = NULL;
                /* fallthrough */
        case LST_INIT_WI_SERIAL:
@@ -116,7 +115,7 @@ lnet_selftest_init(void)
        lst_init_step = LST_INIT_WI_SERIAL;
 
        nscheds = cfs_cpt_number(lnet_cpt_table());
-       LIBCFS_ALLOC(lst_sched_test, sizeof(lst_sched_test[0]) * nscheds);
+       CFS_ALLOC_PTR_ARRAY(lst_sched_test, nscheds);
        if (lst_sched_test == NULL)
                goto error;
 
index 2b9214f..32d9d5a 100644 (file)
@@ -1264,8 +1264,7 @@ struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
        spin_lock_init(&stats->ls_lock);
 
        /* alloc num of counter headers */
-       LIBCFS_ALLOC(stats->ls_cnt_header,
-                    stats->ls_num * sizeof(struct lprocfs_counter_header));
+       CFS_ALLOC_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
        if (!stats->ls_cnt_header)
                goto fail;
 
@@ -1312,8 +1311,7 @@ void lprocfs_free_stats(struct lprocfs_stats **statsh)
                if (stats->ls_percpu[i])
                        LIBCFS_FREE(stats->ls_percpu[i], percpusize);
        if (stats->ls_cnt_header)
-               LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
-                                       sizeof(struct lprocfs_counter_header));
+               CFS_FREE_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
        LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
 }
 EXPORT_SYMBOL(lprocfs_free_stats);