Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <moti.haimovski@intel.com>
Reviewed-by: Koby Elbaz <koby.elbaz@intel.com>
Signed-off-by: Koby Elbaz <koby.elbaz@intel.com>

authored by

Moti Haimovski and committed by
Koby Elbaz
513024d5 0668db41

+26
+19
drivers/accel/habanalabs/gaudi/gaudi.c
··· 4168 4168 vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | 4169 4169 VM_DONTCOPY | VM_NORESERVE); 4170 4170 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 + 4171 4180 rc = dma_mmap_coherent(hdev->dev, vma, cpu_addr, 4172 4181 (dma_addr - HOST_PHYS_BASE), size); 4173 4182 if (rc) 4174 4183 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 + 4175 4194 4176 4195 return rc; 4177 4196 }
+7
drivers/accel/habanalabs/gaudi2/gaudi2.c
··· 6832 6832 VM_DONTCOPY | VM_NORESERVE); 6833 6833 6834 6834 #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); 6835 6842 6836 6843 rc = dma_mmap_coherent(hdev->dev, vma, cpu_addr, dma_addr, size); 6837 6844 if (rc)