Commit 513024d5 authored by Moti Haimovski's avatar Moti Haimovski Committed by Koby Elbaz
Browse files

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: default avatarMoti Haimovski <moti.haimovski@intel.com>
Reviewed-by: default avatarKoby Elbaz <koby.elbaz@intel.com>
Signed-off-by: default avatarKoby Elbaz <koby.elbaz@intel.com>
parent 0668db41
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -4168,10 +4168,29 @@ static int gaudi_mmap(struct hl_device *hdev, struct vm_area_struct *vma,
	vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP |
			VM_DONTCOPY | VM_NORESERVE);

#ifdef _HAS_DMA_MMAP_COHERENT
	/*
	 * If dma_alloc_coherent() returns a vmalloc address, set VM_MIXEDMAP
	 * so vm_insert_page() can handle it safely. Without this, the kernel
	 * may BUG_ON due to VM_PFNMAP.
	 */
	if (is_vmalloc_addr(cpu_addr))
		vm_flags_set(vma, VM_MIXEDMAP);

	rc = dma_mmap_coherent(hdev->dev, vma, cpu_addr,
				(dma_addr - HOST_PHYS_BASE), size);
	if (rc)
		dev_err(hdev->dev, "dma_mmap_coherent error %d", rc);
#else

	rc = remap_pfn_range(vma, vma->vm_start,
				virt_to_phys(cpu_addr) >> PAGE_SHIFT,
				size, vma->vm_page_prot);
	if (rc)
		dev_err(hdev->dev, "remap_pfn_range error %d", rc);

 #endif


	return rc;
}
+7 −0
Original line number Diff line number Diff line
@@ -6832,6 +6832,13 @@ static int gaudi2_mmap(struct hl_device *hdev, struct vm_area_struct *vma,
			VM_DONTCOPY | VM_NORESERVE);

#ifdef _HAS_DMA_MMAP_COHERENT
	/*
	 * If dma_alloc_coherent() returns a vmalloc address, set VM_MIXEDMAP
	 * so vm_insert_page() can handle it safely. Without this, the kernel
	 * may BUG_ON due to VM_PFNMAP.
	 */
	if (is_vmalloc_addr(cpu_addr))
		vm_flags_set(vma, VM_MIXEDMAP);

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