dmabuf: always map dmabuf with PROT_WRITE

This is to enable zero copy inference on dmabufs. Gasket kernel driver
requires all user pointers passed to it to be writable in
drivers/staging/gasket/gasket_page_table.c gasket_perform_mapping
even if it's not actually writing to the buffer. If we map a buffer
with GST_MAP_READ it'll be mmapped with PROT_READ and gasket will
error out with -EFAULT.

While a more correct solution is to fix the gasket driver the needed
linux/mm APIs aren't there in the current version of the kernel.
We'd need
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=73b0140bf0fe9df90fb267c00673c4b9bf285430
which is for a much later kernel version.

This change may look dangerous but what it really does is to make
dmabuf protection equal to buffers allocated by the system memory
allocator; i.e. even if you map a buffer with GST_MAP_READ only
you can, but should not, write to it.

BUG: 142164990
BUG: 148226798
BUG: 148221549
Change-Id: Ie7f7cbce212fd6851e9047e05245b9f634c5e5fd
diff --git a/gst-libs/gst/allocators/gstdmabuf.c b/gst-libs/gst/allocators/gstdmabuf.c
index 4721b67..63d17fa 100644
--- a/gst-libs/gst/allocators/gstdmabuf.c
+++ b/gst-libs/gst/allocators/gstdmabuf.c
@@ -60,7 +60,14 @@
     sync.flags |= DMA_BUF_SYNC_WRITE;
 #endif
 
-  ret = allocator->mem_map (gmem, maxsize, info->flags);
+  /* Workaround to enable sending mapped dmabuf straight to TPU.
+   * Its driver requires all memory sent to it to be writable so
+   * for dmabuf we emulate the behavior of system memory which is
+   * also always really writable even if mapped only for reading
+   * with GST_MAP_READ.
+   * b/148226798
+   */
+  ret = allocator->mem_map (gmem, maxsize, info->flags | GST_MAP_WRITE);
 
 #ifdef HAVE_LINUX_DMA_BUF_H
   if (ret) {