From 8a0c17585f9463c20cb975603b7269a13d705904 Mon Sep 17 00:00:00 2001 From: Chris Goldsworthy Date: Wed, 27 Jul 2022 16:58:09 -0700 Subject: [PATCH] ANDROID: dma-buf: Add vendorhook to allow mmaping more memory than a DMA-BUF holds Add vendorhook to allow mmaping more memory than a DMA-BUF holds. The implementor of the vmap callback for the DMA-BUF is responsible for ensuring that all pages are backed by memory. The hook takes as input a DMA-BUF to allow the VMA bounds check to be done on a case-by-case basis for DMA-BUFs. Note that if the override is allowed to go through for a given DMA-BUF, then it can be the case that the size of this "fully" mmaped DMA-BUF is reported incorrectly when looking at /proc/pid/maps for the owning process. [revert when android14-6. gets created] Bug: 234753494 Change-Id: Iba8cc8adfd2290e4dc7ef04fce5d6a80ac92e0b3 Signed-off-by: Chris Goldsworthy --- drivers/android/vendor_hooks.c | 2 ++ drivers/dma-buf/dma-buf.c | 8 ++++++-- include/trace/hooks/dma_buf.h | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 include/trace/hooks/dma_buf.h diff --git a/drivers/android/vendor_hooks.c b/drivers/android/vendor_hooks.c index a34c371550f7..3a2c3a2f3248 100644 --- a/drivers/android/vendor_hooks.c +++ b/drivers/android/vendor_hooks.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -107,6 +108,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_ufs_send_uic_command); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_ufs_send_tm_command); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_ufs_check_int_errors); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_cgroup_attach); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_ignore_dmabuf_vmap_bounds); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_iommu_setup_dma_ops); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_iommu_iovad_alloc_iova); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_iommu_iovad_free_iova); diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index b086a1e2bac2..c4d911fc6d7d 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -128,6 +129,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; + bool ignore_bounds = false; if (!is_dma_buf_file(file)) return -EINVAL; @@ -138,9 +140,11 @@ static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma) if (!dmabuf->ops->mmap) return -EINVAL; + trace_android_vh_ignore_dmabuf_vmap_bounds(dmabuf, &ignore_bounds); + /* check for overflowing the buffer's size */ - if (vma->vm_pgoff + vma_pages(vma) > - dmabuf->size >> PAGE_SHIFT) + if ((vma->vm_pgoff + vma_pages(vma) > + dmabuf->size >> PAGE_SHIFT) && !ignore_bounds) return -EINVAL; return dmabuf->ops->mmap(dmabuf, vma); diff --git a/include/trace/hooks/dma_buf.h b/include/trace/hooks/dma_buf.h new file mode 100644 index 000000000000..7db586c451fb --- /dev/null +++ b/include/trace/hooks/dma_buf.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM dma_buf + +#define TRACE_INCLUDE_PATH trace/hooks + +#if !defined(_TRACE_HOOK_DMA_BUF_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_HOOK_DMA_BUF_H + +#include + +#include + +DECLARE_HOOK(android_vh_ignore_dmabuf_vmap_bounds, + TP_PROTO(struct dma_buf *dma_buf, bool *ignore_bounds), + TP_ARGS(dma_buf, ignore_bounds)); + +#endif /* _TRACE_HOOK_DMA_BUF_H */ + +/* This part must be outside protection */ +#include