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

dma-buf: heaps: Don't track CMA dma-buf pages under RssFile

DMA buffers allocated from the CMA dma-buf heap get counted under
RssFile for processes that map them and trigger page faults. In
addition to the incorrect accounting reported to userspace, reclaim
behavior was influenced by the MM_FILEPAGES counter until linux 6.8, but
this memory is not reclaimable. [1] Change the CMA dma-buf heap to set
VM_PFNMAP on the VMA so MM does not poke at the memory managed by this
dma-buf heap, and use vmf_insert_pfn to correct the RSS accounting.

The system dma-buf heap does not suffer from this issue since
remap_pfn_range is used during the mmap of the buffer, which also sets
VM_PFNMAP on the VMA.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/mm/vmscan.c?id=fb46e22a9e3863e08aef8815df9f17d0f4b9aede

Fixes: b61614ec318a ("dma-buf: heaps: Add CMA heap to dmabuf heaps")
Signed-off-by: T.J. Mercier <tjmercier@google.com>
Acked-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20240117181141.286383-1-tjmercier@google.com

authored by

T.J. Mercier and committed by
Sumit Semwal
1c1914d6 9c64e749

+3 -4
+3 -4
drivers/dma-buf/heaps/cma_heap.c
··· 168 168 if (vmf->pgoff > buffer->pagecount) 169 169 return VM_FAULT_SIGBUS; 170 170 171 - vmf->page = buffer->pages[vmf->pgoff]; 172 - get_page(vmf->page); 173 - 174 - return 0; 171 + return vmf_insert_pfn(vma, vmf->address, page_to_pfn(buffer->pages[vmf->pgoff])); 175 172 } 176 173 177 174 static const struct vm_operations_struct dma_heap_vm_ops = { ··· 181 184 182 185 if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0) 183 186 return -EINVAL; 187 + 188 + vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); 184 189 185 190 vma->vm_ops = &dma_heap_vm_ops; 186 191 vma->vm_private_data = buffer;