Skip to content

Commit 513024d

Browse files
mhaimovskiKobyElbaz
authored andcommitted
accel/habanalabs: support mapping cb with vmalloc-backed coherent memory
When IOMMU is enabled, dma_alloc_coherent() with GFP_USER may return addresses from the vmalloc range. If such an address is mapped without VM_MIXEDMAP, vm_insert_page() will trigger a BUG_ON due to the VM_PFNMAP restriction. Fix this by checking for vmalloc addresses and setting VM_MIXEDMAP in the VMA before mapping. This ensures safe mapping and avoids kernel crashes. The memory is still driver-allocated and cannot be accessed directly by userspace. Signed-off-by: Moti Haimovski <[email protected]> Reviewed-by: Koby Elbaz <[email protected]> Signed-off-by: Koby Elbaz <[email protected]>
1 parent 0668db4 commit 513024d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

drivers/accel/habanalabs/gaudi/gaudi.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4168,10 +4168,29 @@ static int gaudi_mmap(struct hl_device *hdev, struct vm_area_struct *vma,
41684168
vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP |
41694169
VM_DONTCOPY | VM_NORESERVE);
41704170

4171+
#ifdef _HAS_DMA_MMAP_COHERENT
4172+
/*
4173+
* If dma_alloc_coherent() returns a vmalloc address, set VM_MIXEDMAP
4174+
* so vm_insert_page() can handle it safely. Without this, the kernel
4175+
* may BUG_ON due to VM_PFNMAP.
4176+
*/
4177+
if (is_vmalloc_addr(cpu_addr))
4178+
vm_flags_set(vma, VM_MIXEDMAP);
4179+
41714180
rc = dma_mmap_coherent(hdev->dev, vma, cpu_addr,
41724181
(dma_addr - HOST_PHYS_BASE), size);
41734182
if (rc)
41744183
dev_err(hdev->dev, "dma_mmap_coherent error %d", rc);
4184+
#else
4185+
4186+
rc = remap_pfn_range(vma, vma->vm_start,
4187+
virt_to_phys(cpu_addr) >> PAGE_SHIFT,
4188+
size, vma->vm_page_prot);
4189+
if (rc)
4190+
dev_err(hdev->dev, "remap_pfn_range error %d", rc);
4191+
4192+
#endif
4193+
41754194

41764195
return rc;
41774196
}

drivers/accel/habanalabs/gaudi2/gaudi2.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6832,6 +6832,13 @@ static int gaudi2_mmap(struct hl_device *hdev, struct vm_area_struct *vma,
68326832
VM_DONTCOPY | VM_NORESERVE);
68336833

68346834
#ifdef _HAS_DMA_MMAP_COHERENT
6835+
/*
6836+
* If dma_alloc_coherent() returns a vmalloc address, set VM_MIXEDMAP
6837+
* so vm_insert_page() can handle it safely. Without this, the kernel
6838+
* may BUG_ON due to VM_PFNMAP.
6839+
*/
6840+
if (is_vmalloc_addr(cpu_addr))
6841+
vm_flags_set(vma, VM_MIXEDMAP);
68356842

68366843
rc = dma_mmap_coherent(hdev->dev, vma, cpu_addr, dma_addr, size);
68376844
if (rc)

0 commit comments

Comments
 (0)