All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] dma-buf: Require VM_PFNMAP vma for mmap
@ 2021-02-23 10:59 ` Daniel Vetter
  0 siblings, 0 replies; 110+ messages in thread
From: Daniel Vetter @ 2021-02-23 10:59 UTC (permalink / raw)
  To: DRI Development
  Cc: Intel Graphics Development, Daniel Vetter, Christian König,
	Jason Gunthorpe, Suren Baghdasaryan, Matthew Wilcox, John Stultz,
	Daniel Vetter, Sumit Semwal, linux-media, linaro-mm-sig

tldr; DMA buffers aren't normal memory, expecting that you can use
them like that (like calling get_user_pages works, or that they're
accounting like any other normal memory) cannot be guaranteed.

Since some userspace only runs on integrated devices, where all
buffers are actually all resident system memory, there's a huge
temptation to assume that a struct page is always present and useable
like for any more pagecache backed mmap. This has the potential to
result in a uapi nightmare.

To stop this gap require that DMA buffer mmaps are VM_PFNMAP, which
blocks get_user_pages and all the other struct page based
infrastructure for everyone. In spirit this is the uapi counterpart to
the kernel-internal CONFIG_DMABUF_DEBUG.

Motivated by a recent patch which wanted to swich the system dma-buf
heap to vm_insert_page instead of vm_insert_pfn.

v2:

Jason brought up that we also want to guarantee that all ptes have the
pte_special flag set, to catch fast get_user_pages (on architectures
that support this). Allowing VM_MIXEDMAP (like VM_SPECIAL does) would
still allow vm_insert_page, but limiting to VM_PFNMAP will catch that.

From auditing the various functions to insert pfn pte entires
(vm_insert_pfn_prot, remap_pfn_range and all it's callers like
dma_mmap_wc) it looks like VM_PFNMAP is already required anyway, so
this should be the correct flag to check for.

References: https://lore.kernel.org/lkml/CAKMK7uHi+mG0z0HUmNt13QCCvutuRVjpcR0NjRL12k-WbWzkRg@mail.gmail.com/
Acked-by: Christian König <christian.koenig@amd.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: John Stultz <john.stultz@linaro.org>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: "Christian König" <christian.koenig@amd.com>
Cc: linux-media@vger.kernel.org
Cc: linaro-mm-sig@lists.linaro.org
---
 drivers/dma-buf/dma-buf.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index f264b70c383e..06cb1d2e9fdc 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -127,6 +127,7 @@ static struct file_system_type dma_buf_fs_type = {
 static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
 {
 	struct dma_buf *dmabuf;
+	int ret;
 
 	if (!is_dma_buf_file(file))
 		return -EINVAL;
@@ -142,7 +143,11 @@ static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
 	    dmabuf->size >> PAGE_SHIFT)
 		return -EINVAL;
 
-	return dmabuf->ops->mmap(dmabuf, vma);
+	ret = dmabuf->ops->mmap(dmabuf, vma);
+
+	WARN_ON(!(vma->vm_flags & VM_PFNMAP));
+
+	return ret;
 }
 
 static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence)
@@ -1244,6 +1249,8 @@ EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
 int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
 		 unsigned long pgoff)
 {
+	int ret;
+
 	if (WARN_ON(!dmabuf || !vma))
 		return -EINVAL;
 
@@ -1264,7 +1271,11 @@ int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
 	vma_set_file(vma, dmabuf->file);
 	vma->vm_pgoff = pgoff;
 
-	return dmabuf->ops->mmap(dmabuf, vma);
+	ret = dmabuf->ops->mmap(dmabuf, vma);
+
+	WARN_ON(!(vma->vm_flags & VM_PFNMAP));
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(dma_buf_mmap);
 
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 110+ messages in thread

end of thread, other threads:[~2021-03-12  7:56 UTC | newest]

Thread overview: 110+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-23 10:59 [PATCH 1/2] dma-buf: Require VM_PFNMAP vma for mmap Daniel Vetter
2021-02-23 10:59 ` [Intel-gfx] " Daniel Vetter
2021-02-23 10:59 ` Daniel Vetter
2021-02-23 10:59 ` [PATCH 2/2] drm/vgem: use shmem helpers Daniel Vetter
2021-02-23 10:59   ` [Intel-gfx] " Daniel Vetter
2021-02-23 11:19   ` Thomas Zimmermann
2021-02-23 11:19     ` [Intel-gfx] " Thomas Zimmermann
2021-02-23 11:51   ` [PATCH] " Daniel Vetter
2021-02-23 11:51     ` [Intel-gfx] " Daniel Vetter
2021-02-23 14:21   ` [PATCH 2/2] " kernel test robot
2021-02-23 14:21     ` kernel test robot
2021-02-23 14:21     ` [Intel-gfx] " kernel test robot
2021-02-23 15:07   ` kernel test robot
2021-02-23 15:07     ` kernel test robot
2021-02-23 15:07     ` [Intel-gfx] " kernel test robot
2021-02-25 10:23   ` [PATCH] " Daniel Vetter
2021-02-25 10:23     ` [Intel-gfx] " Daniel Vetter
2021-02-26  9:19     ` Thomas Zimmermann
2021-02-26  9:19       ` [Intel-gfx] " Thomas Zimmermann
2021-02-26 13:30       ` Daniel Vetter
2021-02-26 13:30         ` [Intel-gfx] " Daniel Vetter
2021-02-26 13:51         ` Thomas Zimmermann
2021-02-26 13:51           ` [Intel-gfx] " Thomas Zimmermann
2021-02-26 14:04           ` Daniel Vetter
2021-02-26 14:04             ` [Intel-gfx] " Daniel Vetter
2021-02-23 11:19 ` [Intel-gfx] ✗ Fi.CI.BUILD: failure for series starting with [1/2] dma-buf: Require VM_PFNMAP vma for mmap Patchwork
2021-02-23 13:11 ` [Intel-gfx] ✗ Fi.CI.BUILD: failure for series starting with [1/2] dma-buf: Require VM_PFNMAP vma for mmap (rev2) Patchwork
2021-02-24  7:46 ` [Linaro-mm-sig] [PATCH 1/2] dma-buf: Require VM_PFNMAP vma for mmap Thomas Hellström (Intel)
2021-02-24  7:46   ` [Intel-gfx] " Thomas Hellström (Intel)
2021-02-24  7:46   ` Thomas Hellström (Intel)
2021-02-24  8:45   ` Daniel Vetter
2021-02-24  8:45     ` [Intel-gfx] " Daniel Vetter
2021-02-24  8:45     ` Daniel Vetter
2021-02-24  9:15     ` Thomas Hellström (Intel)
2021-02-24  9:15       ` [Intel-gfx] " Thomas Hellström (Intel)
2021-02-24  9:15       ` Thomas Hellström (Intel)
2021-02-24  9:31       ` Daniel Vetter
2021-02-24  9:31         ` [Intel-gfx] " Daniel Vetter
2021-02-24  9:31         ` Daniel Vetter
2021-02-25 10:28         ` Christian König
2021-02-25 10:28           ` [Intel-gfx] " Christian König
2021-02-25 10:28           ` Christian König
2021-02-25 10:44           ` Daniel Vetter
2021-02-25 10:44             ` [Intel-gfx] " Daniel Vetter
2021-02-25 10:44             ` Daniel Vetter
2021-02-25 15:49             ` Daniel Vetter
2021-02-25 15:49               ` [Intel-gfx] " Daniel Vetter
2021-02-25 15:49               ` Daniel Vetter
2021-02-25 16:53               ` Christian König
2021-02-25 16:53                 ` [Intel-gfx] " Christian König
2021-02-25 16:53                 ` Christian König
2021-02-26  9:41               ` Thomas Hellström (Intel)
2021-02-26  9:41                 ` [Intel-gfx] " Thomas Hellström (Intel)
2021-02-26  9:41                 ` Thomas Hellström (Intel)
2021-02-26 13:28                 ` Daniel Vetter
2021-02-26 13:28                   ` [Intel-gfx] " Daniel Vetter
2021-02-26 13:28                   ` Daniel Vetter
2021-02-27  8:06                   ` Thomas Hellström (Intel)
2021-02-27  8:06                     ` [Intel-gfx] " Thomas Hellström (Intel)
2021-02-27  8:06                     ` Thomas Hellström (Intel)
2021-03-01  8:28                     ` Daniel Vetter
2021-03-01  8:28                       ` [Intel-gfx] " Daniel Vetter
2021-03-01  8:28                       ` Daniel Vetter
2021-03-01  8:39                       ` Thomas Hellström (Intel)
2021-03-01  8:39                         ` [Intel-gfx] " Thomas Hellström (Intel)
2021-03-01  8:39                         ` Thomas Hellström (Intel)
2021-03-01  9:05                         ` Daniel Vetter
2021-03-01  9:05                           ` [Intel-gfx] " Daniel Vetter
2021-03-01  9:05                           ` Daniel Vetter
2021-03-01  9:21                           ` Thomas Hellström (Intel)
2021-03-01  9:21                             ` [Intel-gfx] " Thomas Hellström (Intel)
2021-03-01  9:21                             ` Thomas Hellström (Intel)
2021-03-01 10:17                             ` Christian König
2021-03-01 10:17                               ` [Intel-gfx] " Christian König
2021-03-01 10:17                               ` Christian König
2021-03-01 14:09                               ` Daniel Vetter
2021-03-01 14:09                                 ` [Intel-gfx] " Daniel Vetter
2021-03-01 14:09                                 ` Daniel Vetter
2021-03-11 10:22                                 ` Thomas Hellström (Intel)
2021-03-11 10:22                                   ` [Intel-gfx] " Thomas Hellström (Intel)
2021-03-11 10:22                                   ` Thomas Hellström (Intel)
2021-03-11 13:00                                   ` Daniel Vetter
2021-03-11 13:00                                     ` [Intel-gfx] " Daniel Vetter
2021-03-11 13:00                                     ` Daniel Vetter
2021-03-11 13:12                                     ` Thomas Hellström (Intel)
2021-03-11 13:12                                       ` [Intel-gfx] " Thomas Hellström (Intel)
2021-03-11 13:12                                       ` Thomas Hellström (Intel)
2021-03-11 13:17                                       ` Daniel Vetter
2021-03-11 13:17                                         ` [Intel-gfx] " Daniel Vetter
2021-03-11 13:17                                         ` Daniel Vetter
2021-03-11 15:37                                         ` Thomas Hellström (Intel)
2021-03-11 15:37                                           ` [Intel-gfx] " Thomas Hellström (Intel)
2021-03-11 15:37                                           ` Thomas Hellström (Intel)
2021-03-12  7:51                                         ` Christian König
2021-03-12  7:51                                           ` [Intel-gfx] " Christian König
2021-03-12  7:51                                           ` Christian König
2021-02-24 18:46     ` Jason Gunthorpe
2021-02-24 18:46       ` Jason Gunthorpe
2021-02-25 10:30       ` Christian König
2021-02-25 10:30         ` [Intel-gfx] " Christian König
2021-02-25 10:30         ` Christian König
2021-02-25 10:45         ` Daniel Vetter
2021-02-25 10:45           ` [Intel-gfx] " Daniel Vetter
2021-02-25 10:45           ` Daniel Vetter
2021-02-25 10:38 ` [Intel-gfx] ✗ Fi.CI.CHECKPATCH: warning for series starting with [1/2] dma-buf: Require VM_PFNMAP vma for mmap (rev3) Patchwork
2021-02-25 11:19 ` [Intel-gfx] ✗ Fi.CI.BAT: failure " Patchwork
2021-02-26  3:57 ` [PATCH 1/2] dma-buf: Require VM_PFNMAP vma for mmap John Stultz
2021-02-26  3:57   ` [Intel-gfx] " John Stultz
2021-02-26  3:57   ` John Stultz
2021-03-11 10:58 ` [Intel-gfx] ✗ Fi.CI.BUILD: failure for series starting with [Linaro-mm-sig,1/2] dma-buf: Require VM_PFNMAP vma for mmap (rev4) Patchwork

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.