Skip to content

Commit

Permalink
treewide: Use array_size() in vzalloc()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kees committed Jun 12, 2018
1 parent 42bc47b commit fad953c
Show file tree
Hide file tree
Showing 64 changed files with 164 additions and 118 deletions.
2 changes: 1 addition & 1 deletion arch/powerpc/kvm/book3s_hv.c
Expand Up @@ -3548,7 +3548,7 @@ static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
unsigned long npages)
{
slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
slot->arch.rmap = vzalloc(array_size(npages, sizeof(*slot->arch.rmap)));
if (!slot->arch.rmap)
return -ENOMEM;

Expand Down
2 changes: 1 addition & 1 deletion arch/powerpc/mm/mmu_context_iommu.c
Expand Up @@ -159,7 +159,7 @@ long mm_iommu_get(struct mm_struct *mm, unsigned long ua, unsigned long entries,
goto unlock_exit;
}

mem->hpas = vzalloc(entries * sizeof(mem->hpas[0]));
mem->hpas = vzalloc(array_size(entries, sizeof(mem->hpas[0])));
if (!mem->hpas) {
kfree(mem);
ret = -ENOMEM;
Expand Down
3 changes: 2 additions & 1 deletion arch/x86/kvm/cpuid.c
Expand Up @@ -785,7 +785,8 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
return -EINVAL;

r = -ENOMEM;
cpuid_entries = vzalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
cpuid_entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
cpuid->nent));
if (!cpuid_entries)
goto out;

Expand Down
2 changes: 1 addition & 1 deletion block/partitions/check.c
Expand Up @@ -122,7 +122,7 @@ static struct parsed_partitions *allocate_partitions(struct gendisk *hd)
return NULL;

nr = disk_max_parts(hd);
state->parts = vzalloc(nr * sizeof(state->parts[0]));
state->parts = vzalloc(array_size(nr, sizeof(state->parts[0])));
if (!state->parts) {
kfree(state);
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion drivers/block/zram/zram_drv.c
Expand Up @@ -898,7 +898,7 @@ static bool zram_meta_alloc(struct zram *zram, u64 disksize)
size_t num_pages;

num_pages = disksize >> PAGE_SHIFT;
zram->table = vzalloc(num_pages * sizeof(*zram->table));
zram->table = vzalloc(array_size(num_pages, sizeof(*zram->table)));
if (!zram->table)
return false;

Expand Down
3 changes: 2 additions & 1 deletion drivers/char/raw.c
Expand Up @@ -321,7 +321,8 @@ static int __init raw_init(void)
max_raw_minors = MAX_RAW_MINORS;
}

raw_devices = vzalloc(sizeof(struct raw_device_data) * max_raw_minors);
raw_devices = vzalloc(array_size(max_raw_minors,
sizeof(struct raw_device_data)));
if (!raw_devices) {
printk(KERN_ERR "Not enough memory for raw device structures\n");
ret = -ENOMEM;
Expand Down
2 changes: 1 addition & 1 deletion drivers/cpufreq/intel_pstate.c
Expand Up @@ -2339,7 +2339,7 @@ static int __init intel_pstate_init(void)

pr_info("Intel P-state driver initializing\n");

all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
all_cpu_data = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
if (!all_cpu_data)
return -ENOMEM;

Expand Down
3 changes: 2 additions & 1 deletion drivers/dma/mic_x100_dma.c
Expand Up @@ -385,7 +385,8 @@ static int mic_dma_alloc_desc_ring(struct mic_dma_chan *ch)
if (dma_mapping_error(dev, ch->desc_ring_micpa))
goto map_error;

ch->tx_array = vzalloc(MIC_DMA_DESC_RX_SIZE * sizeof(*ch->tx_array));
ch->tx_array = vzalloc(array_size(MIC_DMA_DESC_RX_SIZE,
sizeof(*ch->tx_array)));
if (!ch->tx_array)
goto tx_error;
return 0;
Expand Down
3 changes: 2 additions & 1 deletion drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c
Expand Up @@ -369,7 +369,8 @@ int amdgpu_gart_init(struct amdgpu_device *adev)

#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
/* Allocate pages table */
adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
adev->gart.pages = vzalloc(array_size(sizeof(void *),
adev->gart.num_cpu_pages));
if (adev->gart.pages == NULL)
return -ENOMEM;
#endif
Expand Down
2 changes: 1 addition & 1 deletion drivers/gpu/drm/drm_hashtab.c
Expand Up @@ -47,7 +47,7 @@ int drm_ht_create(struct drm_open_hash *ht, unsigned int order)
if (size <= PAGE_SIZE / sizeof(*ht->table))
ht->table = kcalloc(size, sizeof(*ht->table), GFP_KERNEL);
else
ht->table = vzalloc(size*sizeof(*ht->table));
ht->table = vzalloc(array_size(size, sizeof(*ht->table)));
if (!ht->table) {
DRM_ERROR("Out of memory for hash table\n");
return -ENOMEM;
Expand Down
5 changes: 3 additions & 2 deletions drivers/gpu/drm/i915/gvt/gtt.c
Expand Up @@ -1585,8 +1585,9 @@ static struct intel_vgpu_mm *intel_vgpu_create_ggtt_mm(struct intel_vgpu *vgpu)
mm->type = INTEL_GVT_MM_GGTT;

nr_entries = gvt_ggtt_gm_sz(vgpu->gvt) >> I915_GTT_PAGE_SHIFT;
mm->ggtt_mm.virtual_ggtt = vzalloc(nr_entries *
vgpu->gvt->device_info.gtt_entry_size);
mm->ggtt_mm.virtual_ggtt =
vzalloc(array_size(nr_entries,
vgpu->gvt->device_info.gtt_entry_size));
if (!mm->ggtt_mm.virtual_ggtt) {
vgpu_free_mm(mm);
return ERR_PTR(-ENOMEM);
Expand Down
2 changes: 1 addition & 1 deletion drivers/gpu/drm/i915/gvt/mmio.c
Expand Up @@ -267,7 +267,7 @@ int intel_vgpu_init_mmio(struct intel_vgpu *vgpu)
{
const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;

vgpu->mmio.vreg = vzalloc(info->mmio_size * 2);
vgpu->mmio.vreg = vzalloc(array_size(info->mmio_size, 2));
if (!vgpu->mmio.vreg)
return -ENOMEM;

Expand Down
3 changes: 2 additions & 1 deletion drivers/gpu/drm/radeon/radeon_gart.c
Expand Up @@ -347,7 +347,8 @@ int radeon_gart_init(struct radeon_device *rdev)
DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
rdev->gart.num_cpu_pages, rdev->gart.num_gpu_pages);
/* Allocate pages table */
rdev->gart.pages = vzalloc(sizeof(void *) * rdev->gart.num_cpu_pages);
rdev->gart.pages = vzalloc(array_size(sizeof(void *),
rdev->gart.num_cpu_pages));
if (rdev->gart.pages == NULL) {
radeon_gart_fini(rdev);
return -ENOMEM;
Expand Down
18 changes: 9 additions & 9 deletions drivers/gpu/drm/selftests/test-drm_mm.c
Expand Up @@ -389,7 +389,7 @@ static int __igt_reserve(unsigned int count, u64 size)
if (!order)
goto err;

nodes = vzalloc(sizeof(*nodes) * count);
nodes = vzalloc(array_size(count, sizeof(*nodes)));
if (!nodes)
goto err_order;

Expand Down Expand Up @@ -889,7 +889,7 @@ static int __igt_insert_range(unsigned int count, u64 size, u64 start, u64 end)
*/

ret = -ENOMEM;
nodes = vzalloc(count * sizeof(*nodes));
nodes = vzalloc(array_size(count, sizeof(*nodes)));
if (!nodes)
goto err;

Expand Down Expand Up @@ -1046,7 +1046,7 @@ static int igt_align(void *ignored)
* meets our requirements.
*/

nodes = vzalloc(max_count * sizeof(*nodes));
nodes = vzalloc(array_size(max_count, sizeof(*nodes)));
if (!nodes)
goto err;

Expand Down Expand Up @@ -1416,7 +1416,7 @@ static int igt_evict(void *ignored)
*/

ret = -ENOMEM;
nodes = vzalloc(size * sizeof(*nodes));
nodes = vzalloc(array_size(size, sizeof(*nodes)));
if (!nodes)
goto err;

Expand Down Expand Up @@ -1526,7 +1526,7 @@ static int igt_evict_range(void *ignored)
*/

ret = -ENOMEM;
nodes = vzalloc(size * sizeof(*nodes));
nodes = vzalloc(array_size(size, sizeof(*nodes)));
if (!nodes)
goto err;

Expand Down Expand Up @@ -1627,7 +1627,7 @@ static int igt_topdown(void *ignored)
*/

ret = -ENOMEM;
nodes = vzalloc(count * sizeof(*nodes));
nodes = vzalloc(array_size(count, sizeof(*nodes)));
if (!nodes)
goto err;

Expand Down Expand Up @@ -1741,7 +1741,7 @@ static int igt_bottomup(void *ignored)
*/

ret = -ENOMEM;
nodes = vzalloc(count * sizeof(*nodes));
nodes = vzalloc(array_size(count, sizeof(*nodes)));
if (!nodes)
goto err;

Expand Down Expand Up @@ -2098,7 +2098,7 @@ static int igt_color_evict(void *ignored)
*/

ret = -ENOMEM;
nodes = vzalloc(total_size * sizeof(*nodes));
nodes = vzalloc(array_size(total_size, sizeof(*nodes)));
if (!nodes)
goto err;

Expand Down Expand Up @@ -2199,7 +2199,7 @@ static int igt_color_evict_range(void *ignored)
*/

ret = -ENOMEM;
nodes = vzalloc(total_size * sizeof(*nodes));
nodes = vzalloc(array_size(total_size, sizeof(*nodes)));
if (!nodes)
goto err;

Expand Down
2 changes: 1 addition & 1 deletion drivers/gpu/drm/via/via_dmablit.c
Expand Up @@ -235,7 +235,7 @@ via_lock_all_dma_pages(drm_via_sg_info_t *vsg, drm_via_dmablit_t *xfer)
vsg->num_pages = VIA_PFN(xfer->mem_addr + (xfer->num_lines * xfer->mem_stride - 1)) -
first_pfn + 1;

vsg->pages = vzalloc(sizeof(struct page *) * vsg->num_pages);
vsg->pages = vzalloc(array_size(sizeof(struct page *), vsg->num_pages));
if (NULL == vsg->pages)
return -ENOMEM;
ret = get_user_pages_fast((unsigned long)xfer->mem_addr,
Expand Down
16 changes: 10 additions & 6 deletions drivers/infiniband/core/umem_odp.c
Expand Up @@ -285,13 +285,15 @@ struct ib_umem *ib_alloc_odp_umem(struct ib_ucontext *context,
mutex_init(&odp_data->umem_mutex);
init_completion(&odp_data->notifier_completion);

odp_data->page_list = vzalloc(pages * sizeof(*odp_data->page_list));
odp_data->page_list =
vzalloc(array_size(pages, sizeof(*odp_data->page_list)));
if (!odp_data->page_list) {
ret = -ENOMEM;
goto out_odp_data;
}

odp_data->dma_list = vzalloc(pages * sizeof(*odp_data->dma_list));
odp_data->dma_list =
vzalloc(array_size(pages, sizeof(*odp_data->dma_list)));
if (!odp_data->dma_list) {
ret = -ENOMEM;
goto out_page_list;
Expand Down Expand Up @@ -371,15 +373,17 @@ int ib_umem_odp_get(struct ib_ucontext *context, struct ib_umem *umem,
init_completion(&umem->odp_data->notifier_completion);

if (ib_umem_num_pages(umem)) {
umem->odp_data->page_list = vzalloc(ib_umem_num_pages(umem) *
sizeof(*umem->odp_data->page_list));
umem->odp_data->page_list =
vzalloc(array_size(sizeof(*umem->odp_data->page_list),
ib_umem_num_pages(umem)));
if (!umem->odp_data->page_list) {
ret_val = -ENOMEM;
goto out_odp_data;
}

umem->odp_data->dma_list = vzalloc(ib_umem_num_pages(umem) *
sizeof(*umem->odp_data->dma_list));
umem->odp_data->dma_list =
vzalloc(array_size(sizeof(*umem->odp_data->dma_list),
ib_umem_num_pages(umem)));
if (!umem->odp_data->dma_list) {
ret_val = -ENOMEM;
goto out_page_list;
Expand Down
2 changes: 1 addition & 1 deletion drivers/infiniband/hw/hns/hns_roce_mr.c
Expand Up @@ -144,7 +144,7 @@ static int hns_roce_buddy_init(struct hns_roce_buddy *buddy, int max_order)
buddy->bits[i] = kcalloc(s, sizeof(long), GFP_KERNEL |
__GFP_NOWARN);
if (!buddy->bits[i]) {
buddy->bits[i] = vzalloc(s * sizeof(long));
buddy->bits[i] = vzalloc(array_size(s, sizeof(long)));
if (!buddy->bits[i])
goto err_out_free;
}
Expand Down
6 changes: 4 additions & 2 deletions drivers/infiniband/hw/qib/qib_init.c
Expand Up @@ -369,11 +369,13 @@ static void init_shadow_tids(struct qib_devdata *dd)
struct page **pages;
dma_addr_t *addrs;

pages = vzalloc(dd->cfgctxts * dd->rcvtidcnt * sizeof(struct page *));
pages = vzalloc(array_size(sizeof(struct page *),
dd->cfgctxts * dd->rcvtidcnt));
if (!pages)
goto bail;

addrs = vzalloc(dd->cfgctxts * dd->rcvtidcnt * sizeof(dma_addr_t));
addrs = vzalloc(array_size(sizeof(dma_addr_t),
dd->cfgctxts * dd->rcvtidcnt));
if (!addrs)
goto bail_free;

Expand Down
8 changes: 5 additions & 3 deletions drivers/infiniband/ulp/ipoib/ipoib_cm.c
Expand Up @@ -358,7 +358,8 @@ static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_i
int ret;
int i;

rx->rx_ring = vzalloc(ipoib_recvq_size * sizeof *rx->rx_ring);
rx->rx_ring = vzalloc(array_size(ipoib_recvq_size,
sizeof(*rx->rx_ring)));
if (!rx->rx_ring)
return -ENOMEM;

Expand Down Expand Up @@ -1145,7 +1146,7 @@ static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
int ret;

noio_flag = memalloc_noio_save();
p->tx_ring = vzalloc(ipoib_sendq_size * sizeof(*p->tx_ring));
p->tx_ring = vzalloc(array_size(ipoib_sendq_size, sizeof(*p->tx_ring)));
if (!p->tx_ring) {
memalloc_noio_restore(noio_flag);
ret = -ENOMEM;
Expand Down Expand Up @@ -1570,7 +1571,8 @@ static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
return;
}

priv->cm.srq_ring = vzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring);
priv->cm.srq_ring = vzalloc(array_size(ipoib_recvq_size,
sizeof(*priv->cm.srq_ring)));
if (!priv->cm.srq_ring) {
ib_destroy_srq(priv->cm.srq);
priv->cm.srq = NULL;
Expand Down
3 changes: 2 additions & 1 deletion drivers/infiniband/ulp/ipoib/ipoib_main.c
Expand Up @@ -1710,7 +1710,8 @@ static int ipoib_dev_init_default(struct net_device *dev)
if (!priv->rx_ring)
goto out;

priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
priv->tx_ring = vzalloc(array_size(ipoib_sendq_size,
sizeof(*priv->tx_ring)));
if (!priv->tx_ring) {
pr_warn("%s: failed to allocate TX ring (%d entries)\n",
priv->ca->name, ipoib_sendq_size);
Expand Down
2 changes: 1 addition & 1 deletion drivers/lightnvm/pblk-init.c
Expand Up @@ -187,7 +187,7 @@ static int pblk_rwb_init(struct pblk *pblk)

nr_entries = pblk_rb_calculate_size(buffer_size);

entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry));
entries = vzalloc(array_size(nr_entries, sizeof(struct pblk_rb_entry)));
if (!entries)
return -ENOMEM;

Expand Down
2 changes: 1 addition & 1 deletion drivers/lightnvm/pblk-recovery.c
Expand Up @@ -260,7 +260,7 @@ static int pblk_recov_pad_oob(struct pblk *pblk, struct pblk_line *line,
if (!pad_rq)
return -ENOMEM;

data = vzalloc(pblk->max_write_pgs * geo->csecs);
data = vzalloc(array_size(pblk->max_write_pgs, geo->csecs));
if (!data) {
ret = -ENOMEM;
goto free_rq;
Expand Down
4 changes: 2 additions & 2 deletions drivers/md/bcache/super.c
Expand Up @@ -2041,8 +2041,8 @@ static int cache_alloc(struct cache *ca)
!init_fifo(&ca->free[RESERVE_NONE], free, GFP_KERNEL) ||
!init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
!init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
!(ca->buckets = vzalloc(sizeof(struct bucket) *
ca->sb.nbuckets)) ||
!(ca->buckets = vzalloc(array_size(sizeof(struct bucket),
ca->sb.nbuckets))) ||
!(ca->prio_buckets = kzalloc(array3_size(sizeof(uint64_t),
prio_buckets(ca), 2),
GFP_KERNEL)) ||
Expand Down
2 changes: 1 addition & 1 deletion drivers/md/dm-cache-policy-smq.c
Expand Up @@ -69,7 +69,7 @@ static int space_init(struct entry_space *es, unsigned nr_entries)
return 0;
}

es->begin = vzalloc(sizeof(struct entry) * nr_entries);
es->begin = vzalloc(array_size(nr_entries, sizeof(struct entry)));
if (!es->begin)
return -ENOMEM;

Expand Down
15 changes: 10 additions & 5 deletions drivers/media/common/v4l2-tpg/v4l2-tpg-core.c
Expand Up @@ -119,26 +119,31 @@ int tpg_alloc(struct tpg_data *tpg, unsigned max_w)
for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
unsigned pixelsz = plane ? 2 : 4;

tpg->lines[pat][plane] = vzalloc(max_w * 2 * pixelsz);
tpg->lines[pat][plane] =
vzalloc(array3_size(max_w, 2, pixelsz));
if (!tpg->lines[pat][plane])
return -ENOMEM;
if (plane == 0)
continue;
tpg->downsampled_lines[pat][plane] = vzalloc(max_w * 2 * pixelsz);
tpg->downsampled_lines[pat][plane] =
vzalloc(array3_size(max_w, 2, pixelsz));
if (!tpg->downsampled_lines[pat][plane])
return -ENOMEM;
}
}
for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
unsigned pixelsz = plane ? 2 : 4;

tpg->contrast_line[plane] = vzalloc(max_w * pixelsz);
tpg->contrast_line[plane] =
vzalloc(array_size(pixelsz, max_w));
if (!tpg->contrast_line[plane])
return -ENOMEM;
tpg->black_line[plane] = vzalloc(max_w * pixelsz);
tpg->black_line[plane] =
vzalloc(array_size(pixelsz, max_w));
if (!tpg->black_line[plane])
return -ENOMEM;
tpg->random_line[plane] = vzalloc(max_w * 2 * pixelsz);
tpg->random_line[plane] =
vzalloc(array3_size(max_w, 2, pixelsz));
if (!tpg->random_line[plane])
return -ENOMEM;
}
Expand Down

0 comments on commit fad953c

Please sign in to comment.