Skip to content

Commit 0839152

Browse files
ofirbittogabbay
authored andcommitted
habanalabs: check correct vmalloc return code
vmalloc can return different return code than NULL and a valid pointer. We must validate it in order to dereference a non valid pointer. Signed-off-by: Ofir Bitton <[email protected]> Signed-off-by: Oded Gabbay <[email protected]>
1 parent bce382a commit 0839152

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

drivers/misc/habanalabs/common/memory.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ static int alloc_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args,
6666
num_pgs = (args->alloc.mem_size + (page_size - 1)) >> page_shift;
6767
total_size = num_pgs << page_shift;
6868

69+
if (!total_size) {
70+
dev_err(hdev->dev, "Cannot allocate 0 bytes\n");
71+
return -EINVAL;
72+
}
73+
6974
contiguous = args->flags & HL_MEM_CONTIGUOUS;
7075

7176
if (contiguous) {
@@ -93,7 +98,7 @@ static int alloc_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args,
9398
phys_pg_pack->contiguous = contiguous;
9499

95100
phys_pg_pack->pages = kvmalloc_array(num_pgs, sizeof(u64), GFP_KERNEL);
96-
if (!phys_pg_pack->pages) {
101+
if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) {
97102
rc = -ENOMEM;
98103
goto pages_arr_err;
99104
}
@@ -683,7 +688,7 @@ static int init_phys_pg_pack_from_userptr(struct hl_ctx *ctx,
683688

684689
phys_pg_pack->pages = kvmalloc_array(total_npages, sizeof(u64),
685690
GFP_KERNEL);
686-
if (!phys_pg_pack->pages) {
691+
if (ZERO_OR_NULL_PTR(phys_pg_pack->pages)) {
687692
rc = -ENOMEM;
688693
goto page_pack_arr_mem_err;
689694
}

drivers/misc/habanalabs/common/mmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ int hl_mmu_init(struct hl_device *hdev)
450450
hdev->mmu_shadow_hop0 = kvmalloc_array(prop->max_asid,
451451
prop->mmu_hop_table_size,
452452
GFP_KERNEL | __GFP_ZERO);
453-
if (!hdev->mmu_shadow_hop0) {
453+
if (ZERO_OR_NULL_PTR(hdev->mmu_shadow_hop0)) {
454454
rc = -ENOMEM;
455455
goto err_pool_add;
456456
}

0 commit comments

Comments
 (0)