Skip to contentSkip to conversation

dma-buf: map-info support

PATCH Superseded Rob Clark 6 participants 22 messages Jul 29, 2022 – Aug 8, 2022

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Rob Clark

author
#0 lore raw

cover dma-buf: map-info support

From: Rob Clark <[email protected]>

See 1/3 for motivation.

Rob Clark (3):
dma-buf: Add ioctl to query mmap info
drm/prime: Wire up mmap_info support
drm/msm/prime: Add mmap_info support

 drivers/dma-buf/dma-buf.c           | 26 ++++++++++++++++++++++++++
 drivers/gpu/drm/drm_prime.c         | 12 ++++++++++++
 drivers/gpu/drm/msm/msm_drv.c       |  1 +
 drivers/gpu/drm/msm/msm_drv.h       |  1 +
 drivers/gpu/drm/msm/msm_gem_prime.c | 11 +++++++++++
 include/drm/drm_drv.h               |  7 +++++++
 include/linux/dma-buf.h             |  7 +++++++
 include/uapi/linux/dma-buf.h        | 28 ++++++++++++++++++++++++++++
 8 files changed, 93 insertions(+)
Signature · 1 line
2.36.1
author
#1 lore raw

PATCH 1/3 dma-buf: Add ioctl to query mmap info

From: Rob Clark <[email protected]>

This is a fairly narrowly focused interface, providing a way for a VMM in userspace to tell the guest kernel what pgprot settings to use when mapping a buffer to guest userspace.

For buffers that get mapped into guest userspace, virglrenderer returns a dma-buf fd to the VMM (crosvm or qemu). In addition to mapping the pages into the guest VM, it needs to report to drm/virtio in the guest the cache settings to use for guest userspace. In particular, on some architectures, creating aliased mappings with different cache attributes is frowned upon, so it is important that the guest mappings have the same cache attributes as any potential host mappings.

---
 drivers/dma-buf/dma-buf.c    | 26 ++++++++++++++++++++++++++
 include/linux/dma-buf.h      |  7 +++++++
 include/uapi/linux/dma-buf.h | 28 ++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)
drivers/dma-buf/dma-buf.c +26 -0
index 32f55640890c..d02d6c2a3b49 100644
@@ -326,6 +326,29 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
 	return 0;
 }
 
+static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
+{
+	struct dma_buf_info arg;
+
+	if (copy_from_user(&arg, uarg, sizeof(arg)))
+		return -EFAULT;
+
+	switch (arg.param) {
+	case DMA_BUF_INFO_VM_PROT:
+		if (!dmabuf->ops->mmap_info)
+			return -ENOSYS;
+		arg.value = dmabuf->ops->mmap_info(dmabuf);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (copy_to_user(uarg, &arg, sizeof(arg)))
+		return -EFAULT;
+
+	return 0;
+}
+
 static long dma_buf_ioctl(struct file *file,
 			  unsigned int cmd, unsigned long arg)
 {
@@ -369,6 +392,9 @@ static long dma_buf_ioctl(struct file *file,
 	case DMA_BUF_SET_NAME_B:
 		return dma_buf_set_name(dmabuf, (const char __user *)arg);
 
+	case DMA_BUF_IOCTL_INFO:
+		return dma_buf_info(dmabuf, (const void __user *)arg);
+
 	default:
 		return -ENOTTY;
 	}
include/linux/dma-buf.h +7 -0
index 71731796c8c3..6f4de64a5937 100644
@@ -283,6 +283,13 @@ struct dma_buf_ops {
 	 */
 	int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
 
+	/**
+	 * @mmap_info:
+	 *
+	 * Return mmapping info for the buffer.  See DMA_BUF_INFO_VM_PROT.
+	 */
+	int (*mmap_info)(struct dma_buf *);
+
 	int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
 	void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
 };
include/uapi/linux/dma-buf.h +28 -0
index b1523cb8ab30..a41adac0f46a 100644
@@ -85,6 +85,32 @@ struct dma_buf_sync {
 
 #define DMA_BUF_NAME_LEN	32
 
+
+/**
+ * struct dma_buf_info - Query info about the buffer.
+ */
+struct dma_buf_info {
+
+#define DMA_BUF_INFO_VM_PROT      1
+#  define DMA_BUF_VM_PROT_WC      0
+#  define DMA_BUF_VM_PROT_CACHED  1
+
+	/**
+	 * @param: Which param to query
+	 *
+	 * DMA_BUF_INFO_BM_PROT:
+	 *     Query the access permissions of userspace mmap's of this buffer.
+	 *     Returns one of DMA_BUF_VM_PROT_x
+	 */
+	__u32 param;
+	__u32 pad;
+
+	/**
+	 * @value: Return value of the query.
+	 */
+	__u64 value;
+};
+
 #define DMA_BUF_BASE		'b'
 #define DMA_BUF_IOCTL_SYNC	_IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
 
@@ -95,4 +121,6 @@ struct dma_buf_sync {
 #define DMA_BUF_SET_NAME_A	_IOW(DMA_BUF_BASE, 1, __u32)
 #define DMA_BUF_SET_NAME_B	_IOW(DMA_BUF_BASE, 1, __u64)
 
+#define DMA_BUF_IOCTL_INFO	_IOWR(DMA_BUF_BASE, 2, struct dma_buf_info)
+
 #endif
Signature · 1 line
2.36.1
#2 lore raw

Re: [PATCH 1/3] dma-buf: Add ioctl to query mmap info

I love your patch! Perhaps something to improve:

Hi Rob,

I love your patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.19-rc8]
[cannot apply to drm-misc/drm-misc-next drm/drm-next drm-exynos/exynos-drm-next drm-intel/for-linux-next drm-tip/drm-tip next-20220728] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rob-Clark/dma-buf-map-info-support/20220730-010844
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 6e2c0490769ef8a95b61304389116ccc85c53e12
config: arc-randconfig-r043-20220729 (https://download.01.org/0day-ci/archive/20220730/[email protected]/config)
compiler: arceb-elf-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/203f14a73a179d6c5fbfa4813e45fde2a9ae9860
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Rob-Clark/dma-buf-map-info-support/20220730-010844
git checkout 203f14a73a179d6c5fbfa4813e45fde2a9ae9860
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arc SHELL=/bin/bash drivers/dma-buf/

If you fix the issue, kindly add following tag where applicable

All warnings (new ones prefixed by >>):

drivers/dma-buf/dma-buf.c: In function 'dma_buf_info':

>> drivers/dma-buf/dma-buf.c:346:26: warning: passing argument 1 of 'copy_to_user' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
     346 |         if (copy_to_user(uarg, &arg, sizeof(arg)))
         |                          ^~~~
   In file included from include/linux/sched/task.h:11,
                    from include/linux/sched/signal.h:9,
                    from include/linux/rcuwait.h:6,
                    from include/linux/percpu-rwsem.h:7,
                    from include/linux/fs.h:33,
                    from drivers/dma-buf/dma-buf.c:14:

include/linux/uaccess.h:157:27: note: expected 'void *' but argument is of type 'const void *'
157 | copy_to_user(void __user *to, const void *from, unsigned long n)
| ~~~~~~~~~~~~~^~

vim +346 drivers/dma-buf/dma-buf.c

328
329 static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
330 {
331 struct dma_buf_info arg;
332
333 if (copy_from_user(&arg, uarg, sizeof(arg)))
334 return -EFAULT;
335
336 switch (arg.param) {
337 case DMA_BUF_INFO_VM_PROT:
338 if (!dmabuf->ops->mmap_info)
339 return -ENOSYS;
340 arg.value = dmabuf->ops->mmap_info(dmabuf);
341 break;
342 default:
343 return -EINVAL;
344 }
345

 > 346		if (copy_to_user(uarg, &arg, sizeof(arg)))

347 return -EFAULT;
348
349 return 0;
350 }
351

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#3 lore raw

Re: [PATCH 1/3] dma-buf: Add ioctl to query mmap info

In reply to Rob Clark
I love your patch! Perhaps something to improve:

Hi Rob,

I love your patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.19-rc8]
[cannot apply to drm-misc/drm-misc-next drm/drm-next drm-exynos/exynos-drm-next drm-intel/for-linux-next drm-tip/drm-tip next-20220728] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rob-Clark/dma-buf-map-info-support/20220730-010844
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 6e2c0490769ef8a95b61304389116ccc85c53e12
config: m68k-randconfig-s032-20220729 (https://download.01.org/0day-ci/archive/20220730/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 12.1.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-39-gce1a6720-dirty
# https://github.com/intel-lab-lkp/linux/commit/203f14a73a179d6c5fbfa4813e45fde2a9ae9860
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Rob-Clark/dma-buf-map-info-support/20220730-010844
git checkout 203f14a73a179d6c5fbfa4813e45fde2a9ae9860
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=m68k SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable

sparse warnings: (new ones prefixed by >>)

>> drivers/dma-buf/dma-buf.c:346:26: sparse: sparse: incorrect type in argument 1 (different modifiers) @@     expected void [noderef] __user *to @@     got void const [noderef] __user *uarg @@

drivers/dma-buf/dma-buf.c:346:26: sparse: expected void [noderef] __user *to
drivers/dma-buf/dma-buf.c:346:26: sparse: got void const [noderef] __user *uarg

vim +346 drivers/dma-buf/dma-buf.c

328
329 static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
330 {
331 struct dma_buf_info arg;
332
333 if (copy_from_user(&arg, uarg, sizeof(arg)))
334 return -EFAULT;
335
336 switch (arg.param) {
337 case DMA_BUF_INFO_VM_PROT:
338 if (!dmabuf->ops->mmap_info)
339 return -ENOSYS;
340 arg.value = dmabuf->ops->mmap_info(dmabuf);
341 break;
342 default:
343 return -EINVAL;
344 }
345

 > 346		if (copy_to_user(uarg, &arg, sizeof(arg)))

347 return -EFAULT;
348
349 return 0;
350 }
351

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#4 lore raw

Re: [PATCH 1/3] dma-buf: Add ioctl to query mmap info

In reply to Rob Clark
I love your patch! Yet something to improve:

Hi Rob,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.19-rc8]
[cannot apply to drm-misc/drm-misc-next drm/drm-next drm-exynos/exynos-drm-next drm-intel/for-linux-next drm-tip/drm-tip next-20220728] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rob-Clark/dma-buf-map-info-support/20220730-010844
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 6e2c0490769ef8a95b61304389116ccc85c53e12
config: arm-randconfig-r033-20220729 (https://download.01.org/0day-ci/archive/20220730/[email protected]/config)
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 52cd00cabf479aa7eb6dbb063b7ba41ea57bce9e)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/intel-lab-lkp/linux/commit/203f14a73a179d6c5fbfa4813e45fde2a9ae9860
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Rob-Clark/dma-buf-map-info-support/20220730-010844
git checkout 203f14a73a179d6c5fbfa4813e45fde2a9ae9860
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash drivers/dma-buf/

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

>> drivers/dma-buf/dma-buf.c:346:19: error: passing 'const void *' to parameter of type 'void *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
           if (copy_to_user(uarg, &arg, sizeof(arg)))
                            ^~~~
   include/linux/uaccess.h:157:27: note: passing argument to parameter 'to' here
   copy_to_user(void __user *to, const void *from, unsigned long n)
                             ^

1 error generated.

vim +346 drivers/dma-buf/dma-buf.c

328
329 static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
330 {
331 struct dma_buf_info arg;
332
333 if (copy_from_user(&arg, uarg, sizeof(arg)))
334 return -EFAULT;
335
336 switch (arg.param) {
337 case DMA_BUF_INFO_VM_PROT:
338 if (!dmabuf->ops->mmap_info)
339 return -ENOSYS;
340 arg.value = dmabuf->ops->mmap_info(dmabuf);
341 break;
342 default:
343 return -EINVAL;
344 }
345

 > 346		if (copy_to_user(uarg, &arg, sizeof(arg)))

347 return -EFAULT;
348
349 return 0;
350 }
351

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#5 lore raw

Re: [PATCH 1/3] dma-buf: Add ioctl to query mmap info

In reply to Rob Clark
I love your patch! Yet something to improve:

Hi Rob,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.19-rc8]
[cannot apply to drm-misc/drm-misc-next drm/drm-next drm-exynos/exynos-drm-next drm-intel/for-linux-next drm-tip/drm-tip next-20220728] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rob-Clark/dma-buf-map-info-support/20220730-010844
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 6e2c0490769ef8a95b61304389116ccc85c53e12
config: hexagon-randconfig-r032-20220729 (https://download.01.org/0day-ci/archive/20220731/[email protected]/config)
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 52cd00cabf479aa7eb6dbb063b7ba41ea57bce9e)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/203f14a73a179d6c5fbfa4813e45fde2a9ae9860
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Rob-Clark/dma-buf-map-info-support/20220730-010844
git checkout 203f14a73a179d6c5fbfa4813e45fde2a9ae9860
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash drivers/dma-buf/

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

>> drivers/dma-buf/dma-buf.c:346:19: error: passing 'const void  btf_type_tag(user)*' (aka 'const void *') to parameter of type 'void  btf_type_tag(user)*' (aka 'void *') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
           if (copy_to_user(uarg, &arg, sizeof(arg)))
                            ^~~~
   include/linux/uaccess.h:157:27: note: passing argument to parameter 'to' here
   copy_to_user(void __user *to, const void *from, unsigned long n)
                             ^

1 error generated.

vim +346 drivers/dma-buf/dma-buf.c

328
329 static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
330 {
331 struct dma_buf_info arg;
332
333 if (copy_from_user(&arg, uarg, sizeof(arg)))
334 return -EFAULT;
335
336 switch (arg.param) {
337 case DMA_BUF_INFO_VM_PROT:
338 if (!dmabuf->ops->mmap_info)
339 return -ENOSYS;
340 arg.value = dmabuf->ops->mmap_info(dmabuf);
341 break;
342 default:
343 return -EINVAL;
344 }
345

 > 346		if (copy_to_user(uarg, &arg, sizeof(arg)))

347 return -EFAULT;
348
349 return 0;
350 }
351

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#6 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

In reply to Rob Clark

Am 29.07.22 um 19:07 schrieb Rob Clark:

Quoted 8 lines

From: Rob Clark <[email protected]>

This is a fairly narrowly focused interface, providing a way for a VMM in userspace to tell the guest kernel what pgprot settings to use when mapping a buffer to guest userspace.

For buffers that get mapped into guest userspace, virglrenderer returns a dma-buf fd to the VMM (crosvm or qemu).

Wow, wait a second. Who is giving whom the DMA-buf fd here?

My last status was that this design was illegal and couldn't be implemented because it requires internal knowledge only the exporting driver can have.

@Daniel has anything changed on that is or my status still valid?

Regards,
Christian.

Quoted 120 lines

In addition to mapping the
pages into the guest VM, it needs to report to drm/virtio in the guest the cache settings to use for guest userspace. In particular, on some architectures, creating aliased mappings with different cache attributes is frowned upon, so it is important that the guest mappings have the same cache attributes as any potential host mappings.

---
  drivers/dma-buf/dma-buf.c    | 26 ++++++++++++++++++++++++++
  include/linux/dma-buf.h      |  7 +++++++
  include/uapi/linux/dma-buf.h | 28 ++++++++++++++++++++++++++++
  3 files changed, 61 insertions(+)
drivers/dma-buf/dma-buf.c +26 -0
index 32f55640890c..d02d6c2a3b49 100644
@@ -326,6 +326,29 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
  	return 0;
  }
 
+static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
+{
+	struct dma_buf_info arg;
+
+	if (copy_from_user(&arg, uarg, sizeof(arg)))
+		return -EFAULT;
+
+	switch (arg.param) {
+	case DMA_BUF_INFO_VM_PROT:
+		if (!dmabuf->ops->mmap_info)
+			return -ENOSYS;
+		arg.value = dmabuf->ops->mmap_info(dmabuf);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (copy_to_user(uarg, &arg, sizeof(arg)))
+		return -EFAULT;
+
+	return 0;
+}
+
  static long dma_buf_ioctl(struct file *file,
  			  unsigned int cmd, unsigned long arg)
  {
@@ -369,6 +392,9 @@ static long dma_buf_ioctl(struct file *file,
  	case DMA_BUF_SET_NAME_B:
  		return dma_buf_set_name(dmabuf, (const char __user *)arg);
 
+	case DMA_BUF_IOCTL_INFO:
+		return dma_buf_info(dmabuf, (const void __user *)arg);
+
  	default:
  		return -ENOTTY;
  	}
include/linux/dma-buf.h +7 -0
index 71731796c8c3..6f4de64a5937 100644
@@ -283,6 +283,13 @@ struct dma_buf_ops {
  	 */
  	int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
 
+	/**
+	 * @mmap_info:
+	 *
+	 * Return mmapping info for the buffer.  See DMA_BUF_INFO_VM_PROT.
+	 */
+	int (*mmap_info)(struct dma_buf *);
+
  	int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
  	void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
  };
include/uapi/linux/dma-buf.h +28 -0
index b1523cb8ab30..a41adac0f46a 100644
@@ -85,6 +85,32 @@ struct dma_buf_sync {
 
  #define DMA_BUF_NAME_LEN	32
 
+
+/**
+ * struct dma_buf_info - Query info about the buffer.
+ */
+struct dma_buf_info {
+
+#define DMA_BUF_INFO_VM_PROT      1
+#  define DMA_BUF_VM_PROT_WC      0
+#  define DMA_BUF_VM_PROT_CACHED  1
+
+	/**
+	 * @param: Which param to query
+	 *
+	 * DMA_BUF_INFO_BM_PROT:
+	 *     Query the access permissions of userspace mmap's of this buffer.
+	 *     Returns one of DMA_BUF_VM_PROT_x
+	 */
+	__u32 param;
+	__u32 pad;
+
+	/**
+	 * @value: Return value of the query.
+	 */
+	__u64 value;
+};
+
  #define DMA_BUF_BASE		'b'
  #define DMA_BUF_IOCTL_SYNC	_IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
 
@@ -95,4 +121,6 @@ struct dma_buf_sync {
  #define DMA_BUF_SET_NAME_A	_IOW(DMA_BUF_BASE, 1, __u32)
  #define DMA_BUF_SET_NAME_B	_IOW(DMA_BUF_BASE, 1, __u64)
 
+#define DMA_BUF_IOCTL_INFO	_IOWR(DMA_BUF_BASE, 2, struct dma_buf_info)
+
  #endif
author
#7 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Quoted 4 lines · Christian König

Am 29.07.22 um 19:07 schrieb Rob Clark:

Quoted 2 lines

From: Rob Clark <[email protected]>

Quoted 8 lines
Quoted 6 lines

This is a fairly narrowly focused interface, providing a way for a VMM in userspace to tell the guest kernel what pgprot settings to use when mapping a buffer to guest userspace.

For buffers that get mapped into guest userspace, virglrenderer returns a dma-buf fd to the VMM (crosvm or qemu).

Wow, wait a second. Who is giving whom the DMA-buf fd here?

Not sure I understand the question.. the dma-buf fd could come from EGL_MESA_image_dma_buf_export, gbm, or similar.

Quoted 3 lines

My last status was that this design was illegal and couldn't be implemented because it requires internal knowledge only the exporting driver can have.

This ioctl provides that information from the exporting driver so that a VMM doesn't have to make assumptions ;-)

Currently crosvm assumes if (drivername == "i915") then it is a cached mapping, otherwise it is wc. I'm trying to find a way to fix this. Suggestions welcome, but because of how mapping to a guest VM works, a VMM is a somewhat special case where this information is needed in userspace.

BR,
-R

Quoted 126 lines

@Daniel has anything changed on that is or my status still valid?

Regards,
Christian.

Quoted 120 lines

In addition to mapping the
pages into the guest VM, it needs to report to drm/virtio in the guest the cache settings to use for guest userspace. In particular, on some architectures, creating aliased mappings with different cache attributes is frowned upon, so it is important that the guest mappings have the same cache attributes as any potential host mappings.

---
  drivers/dma-buf/dma-buf.c    | 26 ++++++++++++++++++++++++++
  include/linux/dma-buf.h      |  7 +++++++
  include/uapi/linux/dma-buf.h | 28 ++++++++++++++++++++++++++++
  3 files changed, 61 insertions(+)
drivers/dma-buf/dma-buf.c +26 -0
index 32f55640890c..d02d6c2a3b49 100644
@@ -326,6 +326,29 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
      return 0;
  }
 
+static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
+{
+     struct dma_buf_info arg;
+
+     if (copy_from_user(&arg, uarg, sizeof(arg)))
+             return -EFAULT;
+
+     switch (arg.param) {
+     case DMA_BUF_INFO_VM_PROT:
+             if (!dmabuf->ops->mmap_info)
+                     return -ENOSYS;
+             arg.value = dmabuf->ops->mmap_info(dmabuf);
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     if (copy_to_user(uarg, &arg, sizeof(arg)))
+             return -EFAULT;
+
+     return 0;
+}
+
  static long dma_buf_ioctl(struct file *file,
                        unsigned int cmd, unsigned long arg)
  {
@@ -369,6 +392,9 @@ static long dma_buf_ioctl(struct file *file,
      case DMA_BUF_SET_NAME_B:
              return dma_buf_set_name(dmabuf, (const char __user *)arg);
 
+     case DMA_BUF_IOCTL_INFO:
+             return dma_buf_info(dmabuf, (const void __user *)arg);
+
      default:
              return -ENOTTY;
      }
include/linux/dma-buf.h +7 -0
index 71731796c8c3..6f4de64a5937 100644
@@ -283,6 +283,13 @@ struct dma_buf_ops {
       */
      int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
 
+     /**
+      * @mmap_info:
+      *
+      * Return mmapping info for the buffer.  See DMA_BUF_INFO_VM_PROT.
+      */
+     int (*mmap_info)(struct dma_buf *);
+
      int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
      void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
  };
include/uapi/linux/dma-buf.h +28 -0
index b1523cb8ab30..a41adac0f46a 100644
@@ -85,6 +85,32 @@ struct dma_buf_sync {
 
  #define DMA_BUF_NAME_LEN    32
 
+
+/**
+ * struct dma_buf_info - Query info about the buffer.
+ */
+struct dma_buf_info {
+
+#define DMA_BUF_INFO_VM_PROT      1
+#  define DMA_BUF_VM_PROT_WC      0
+#  define DMA_BUF_VM_PROT_CACHED  1
+
+     /**
+      * @param: Which param to query
+      *
+      * DMA_BUF_INFO_BM_PROT:
+      *     Query the access permissions of userspace mmap's of this buffer.
+      *     Returns one of DMA_BUF_VM_PROT_x
+      */
+     __u32 param;
+     __u32 pad;
+
+     /**
+      * @value: Return value of the query.
+      */
+     __u64 value;
+};
+
  #define DMA_BUF_BASE                'b'
  #define DMA_BUF_IOCTL_SYNC  _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
 
@@ -95,4 +121,6 @@ struct dma_buf_sync {
  #define DMA_BUF_SET_NAME_A  _IOW(DMA_BUF_BASE, 1, __u32)
  #define DMA_BUF_SET_NAME_B  _IOW(DMA_BUF_BASE, 1, __u64)
 
+#define DMA_BUF_IOCTL_INFO   _IOWR(DMA_BUF_BASE, 2, struct dma_buf_info)
+
  #endif
maintainer
#8 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Am 07.08.22 um 19:02 schrieb Rob Clark:

Quoted 12 lines
Quoted 10 lines · Christian König

Am 29.07.22 um 19:07 schrieb Rob Clark:

Quoted 8 lines

From: Rob Clark <[email protected]>

This is a fairly narrowly focused interface, providing a way for a VMM in userspace to tell the guest kernel what pgprot settings to use when mapping a buffer to guest userspace.

For buffers that get mapped into guest userspace, virglrenderer returns a dma-buf fd to the VMM (crosvm or qemu).

Wow, wait a second. Who is giving whom the DMA-buf fd here?

Quoted 8 lines

Not sure I understand the question.. the dma-buf fd could come from EGL_MESA_image_dma_buf_export, gbm, or similar.

Quoted 3 lines

My last status was that this design was illegal and couldn't be implemented because it requires internal knowledge only the exporting driver can have.

This ioctl provides that information from the exporting driver so that a VMM doesn't have to make assumptions ;-)

And exactly that was NAKed the last time it came up. Only the exporting driver is allowed to mmap() the DMA-buf into the guest.

This way you also don't need to transport any caching information anywhere.

Quoted 5 lines

Currently crosvm assumes if (drivername == "i915") then it is a cached mapping, otherwise it is wc. I'm trying to find a way to fix this. Suggestions welcome, but because of how mapping to a guest VM works, a VMM is a somewhat special case where this information is needed in userspace.

Ok that leaves me completely puzzled. How does that work in the first place?

In other words how does the mapping into the guest page tables happen?

Regards,
Christian.

Quoted 129 lines

BR,
-R

Quoted 125 lines

@Daniel has anything changed on that is or my status still valid?

Regards,
Christian.

Quoted 120 lines

In addition to mapping the
pages into the guest VM, it needs to report to drm/virtio in the guest the cache settings to use for guest userspace. In particular, on some architectures, creating aliased mappings with different cache attributes is frowned upon, so it is important that the guest mappings have the same cache attributes as any potential host mappings.

---
   drivers/dma-buf/dma-buf.c    | 26 ++++++++++++++++++++++++++
   include/linux/dma-buf.h      |  7 +++++++
   include/uapi/linux/dma-buf.h | 28 ++++++++++++++++++++++++++++
   3 files changed, 61 insertions(+)
drivers/dma-buf/dma-buf.c +26 -0
index 32f55640890c..d02d6c2a3b49 100644
@@ -326,6 +326,29 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
       return 0;
   }
 
+static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
+{
+     struct dma_buf_info arg;
+
+     if (copy_from_user(&arg, uarg, sizeof(arg)))
+             return -EFAULT;
+
+     switch (arg.param) {
+     case DMA_BUF_INFO_VM_PROT:
+             if (!dmabuf->ops->mmap_info)
+                     return -ENOSYS;
+             arg.value = dmabuf->ops->mmap_info(dmabuf);
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     if (copy_to_user(uarg, &arg, sizeof(arg)))
+             return -EFAULT;
+
+     return 0;
+}
+
   static long dma_buf_ioctl(struct file *file,
                         unsigned int cmd, unsigned long arg)
   {
@@ -369,6 +392,9 @@ static long dma_buf_ioctl(struct file *file,
       case DMA_BUF_SET_NAME_B:
               return dma_buf_set_name(dmabuf, (const char __user *)arg);
 
+     case DMA_BUF_IOCTL_INFO:
+             return dma_buf_info(dmabuf, (const void __user *)arg);
+
       default:
               return -ENOTTY;
       }
include/linux/dma-buf.h +7 -0
index 71731796c8c3..6f4de64a5937 100644
@@ -283,6 +283,13 @@ struct dma_buf_ops {
        */
       int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
 
+     /**
+      * @mmap_info:
+      *
+      * Return mmapping info for the buffer.  See DMA_BUF_INFO_VM_PROT.
+      */
+     int (*mmap_info)(struct dma_buf *);
+
       int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
       void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
   };
include/uapi/linux/dma-buf.h +28 -0
index b1523cb8ab30..a41adac0f46a 100644
@@ -85,6 +85,32 @@ struct dma_buf_sync {
 
   #define DMA_BUF_NAME_LEN    32
 
+
+/**
+ * struct dma_buf_info - Query info about the buffer.
+ */
+struct dma_buf_info {
+
+#define DMA_BUF_INFO_VM_PROT      1
+#  define DMA_BUF_VM_PROT_WC      0
+#  define DMA_BUF_VM_PROT_CACHED  1
+
+     /**
+      * @param: Which param to query
+      *
+      * DMA_BUF_INFO_BM_PROT:
+      *     Query the access permissions of userspace mmap's of this buffer.
+      *     Returns one of DMA_BUF_VM_PROT_x
+      */
+     __u32 param;
+     __u32 pad;
+
+     /**
+      * @value: Return value of the query.
+      */
+     __u64 value;
+};
+
   #define DMA_BUF_BASE                'b'
   #define DMA_BUF_IOCTL_SYNC  _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
 
@@ -95,4 +121,6 @@ struct dma_buf_sync {
   #define DMA_BUF_SET_NAME_A  _IOW(DMA_BUF_BASE, 1, __u32)
   #define DMA_BUF_SET_NAME_B  _IOW(DMA_BUF_BASE, 1, __u64)
 
+#define DMA_BUF_IOCTL_INFO   _IOWR(DMA_BUF_BASE, 2, struct dma_buf_info)
+
   #endif
author
#9 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Quoted 17 lines · Christian König

Am 07.08.22 um 19:02 schrieb Rob Clark:

Quoted 15 lines
Quoted 10 lines · Christian König

Am 29.07.22 um 19:07 schrieb Rob Clark:

Quoted 8 lines

From: Rob Clark <[email protected]>

This is a fairly narrowly focused interface, providing a way for a VMM in userspace to tell the guest kernel what pgprot settings to use when mapping a buffer to guest userspace.

For buffers that get mapped into guest userspace, virglrenderer returns a dma-buf fd to the VMM (crosvm or qemu).

Wow, wait a second. Who is giving whom the DMA-buf fd here?

Not sure I understand the question.. the dma-buf fd could come from EGL_MESA_image_dma_buf_export, gbm, or similar.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

My last status was that this design was illegal and couldn't be implemented because it requires internal knowledge only the exporting driver can have.

This ioctl provides that information from the exporting driver so that a VMM doesn't have to make assumptions ;-)

And exactly that was NAKed the last time it came up. Only the exporting driver is allowed to mmap() the DMA-buf into the guest.

except the exporting driver is in the host ;-)

Quoted 11 lines

This way you also don't need to transport any caching information anywhere.

Quoted 5 lines

Currently crosvm assumes if (drivername == "i915") then it is a cached mapping, otherwise it is wc. I'm trying to find a way to fix this. Suggestions welcome, but because of how mapping to a guest VM works, a VMM is a somewhat special case where this information is needed in userspace.

Ok that leaves me completely puzzled. How does that work in the first place?

In other words how does the mapping into the guest page tables happen?

There are multiple levels to this, but in short mapping to guest userspace happens via drm/virtio (aka "virtio_gpu" or "virtgpu"), the cache attributes are set via "map_info" attribute returned from the host VMM (host userspace, hence the need for this ioctl).

In the host, the host kernel driver mmaps to host userspace (VMM). Here the exporting driver is performing the mmap with correct cache attributes. The VMM uses KVM to map these pages into the guest so they appear as physical pages to the guest kernel. The guest kernel (virtgpu) in turn maps them to guest userspace.

BR,
-R

Quoted 134 lines

Regards,
Christian.

Quoted 129 lines

BR,
-R

Quoted 125 lines

@Daniel has anything changed on that is or my status still valid?

Regards,
Christian.

Quoted 120 lines

In addition to mapping the
pages into the guest VM, it needs to report to drm/virtio in the guest the cache settings to use for guest userspace. In particular, on some architectures, creating aliased mappings with different cache attributes is frowned upon, so it is important that the guest mappings have the same cache attributes as any potential host mappings.

---
   drivers/dma-buf/dma-buf.c    | 26 ++++++++++++++++++++++++++
   include/linux/dma-buf.h      |  7 +++++++
   include/uapi/linux/dma-buf.h | 28 ++++++++++++++++++++++++++++
   3 files changed, 61 insertions(+)
drivers/dma-buf/dma-buf.c +26 -0
index 32f55640890c..d02d6c2a3b49 100644
@@ -326,6 +326,29 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
       return 0;
   }
 
+static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
+{
+     struct dma_buf_info arg;
+
+     if (copy_from_user(&arg, uarg, sizeof(arg)))
+             return -EFAULT;
+
+     switch (arg.param) {
+     case DMA_BUF_INFO_VM_PROT:
+             if (!dmabuf->ops->mmap_info)
+                     return -ENOSYS;
+             arg.value = dmabuf->ops->mmap_info(dmabuf);
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     if (copy_to_user(uarg, &arg, sizeof(arg)))
+             return -EFAULT;
+
+     return 0;
+}
+
   static long dma_buf_ioctl(struct file *file,
                         unsigned int cmd, unsigned long arg)
   {
@@ -369,6 +392,9 @@ static long dma_buf_ioctl(struct file *file,
       case DMA_BUF_SET_NAME_B:
               return dma_buf_set_name(dmabuf, (const char __user *)arg);
 
+     case DMA_BUF_IOCTL_INFO:
+             return dma_buf_info(dmabuf, (const void __user *)arg);
+
       default:
               return -ENOTTY;
       }
include/linux/dma-buf.h +7 -0
index 71731796c8c3..6f4de64a5937 100644
@@ -283,6 +283,13 @@ struct dma_buf_ops {
        */
       int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
 
+     /**
+      * @mmap_info:
+      *
+      * Return mmapping info for the buffer.  See DMA_BUF_INFO_VM_PROT.
+      */
+     int (*mmap_info)(struct dma_buf *);
+
       int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
       void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
   };
include/uapi/linux/dma-buf.h +28 -0
index b1523cb8ab30..a41adac0f46a 100644
@@ -85,6 +85,32 @@ struct dma_buf_sync {
 
   #define DMA_BUF_NAME_LEN    32
 
+
+/**
+ * struct dma_buf_info - Query info about the buffer.
+ */
+struct dma_buf_info {
+
+#define DMA_BUF_INFO_VM_PROT      1
+#  define DMA_BUF_VM_PROT_WC      0
+#  define DMA_BUF_VM_PROT_CACHED  1
+
+     /**
+      * @param: Which param to query
+      *
+      * DMA_BUF_INFO_BM_PROT:
+      *     Query the access permissions of userspace mmap's of this buffer.
+      *     Returns one of DMA_BUF_VM_PROT_x
+      */
+     __u32 param;
+     __u32 pad;
+
+     /**
+      * @value: Return value of the query.
+      */
+     __u64 value;
+};
+
   #define DMA_BUF_BASE                'b'
   #define DMA_BUF_IOCTL_SYNC  _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
 
@@ -95,4 +121,6 @@ struct dma_buf_sync {
   #define DMA_BUF_SET_NAME_A  _IOW(DMA_BUF_BASE, 1, __u32)
   #define DMA_BUF_SET_NAME_B  _IOW(DMA_BUF_BASE, 1, __u64)
 
+#define DMA_BUF_IOCTL_INFO   _IOWR(DMA_BUF_BASE, 2, struct dma_buf_info)
+
   #endif
#10 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Am 07.08.22 um 19:35 schrieb Rob Clark:

Quoted 42 lines
Quoted 16 lines · Christian König

Am 07.08.22 um 19:02 schrieb Rob Clark:

Quoted 15 lines
Quoted 10 lines · Christian König

Am 29.07.22 um 19:07 schrieb Rob Clark:

Quoted 8 lines

From: Rob Clark <[email protected]>

This is a fairly narrowly focused interface, providing a way for a VMM in userspace to tell the guest kernel what pgprot settings to use when mapping a buffer to guest userspace.

For buffers that get mapped into guest userspace, virglrenderer returns a dma-buf fd to the VMM (crosvm or qemu).

Wow, wait a second. Who is giving whom the DMA-buf fd here?

Not sure I understand the question.. the dma-buf fd could come from EGL_MESA_image_dma_buf_export, gbm, or similar.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

My last status was that this design was illegal and couldn't be implemented because it requires internal knowledge only the exporting driver can have.

This ioctl provides that information from the exporting driver so that a VMM doesn't have to make assumptions ;-)

And exactly that was NAKed the last time it came up. Only the exporting driver is allowed to mmap() the DMA-buf into the guest.

except the exporting driver is in the host ;-)

Quoted 10 lines

This way you also don't need to transport any caching information anywhere.

Quoted 5 lines

Currently crosvm assumes if (drivername == "i915") then it is a cached mapping, otherwise it is wc. I'm trying to find a way to fix this. Suggestions welcome, but because of how mapping to a guest VM works, a VMM is a somewhat special case where this information is needed in userspace.

Ok that leaves me completely puzzled. How does that work in the first place?

In other words how does the mapping into the guest page tables happen?

There are multiple levels to this, but in short mapping to guest userspace happens via drm/virtio (aka "virtio_gpu" or "virtgpu"), the cache attributes are set via "map_info" attribute returned from the host VMM (host userspace, hence the need for this ioctl).

Quoted 5 lines

In the host, the host kernel driver mmaps to host userspace (VMM). Here the exporting driver is performing the mmap with correct cache attributes.

The VMM uses KVM to map these pages into the guest so

And exactly that was declared completely illegal the last time it came up on the mailing list.

Daniel implemented a whole bunch of patches into the DMA-buf layer to make it impossible for KVM to do this.

I have absolutely no idea why that is now a topic again and why anybody is still using this approach.

@Daniel can you clarify.

Thanks,
Christian.

Quoted 137 lines

they appear as physical pages to the guest kernel. The guest kernel (virtgpu) in turn maps them to guest userspace.

BR,
-R

Quoted 131 lines

Regards,
Christian.

Quoted 128 lines

BR,
-R

Quoted 125 lines

@Daniel has anything changed on that is or my status still valid?

Regards,
Christian.

Quoted 120 lines

In addition to mapping the
pages into the guest VM, it needs to report to drm/virtio in the guest the cache settings to use for guest userspace. In particular, on some architectures, creating aliased mappings with different cache attributes is frowned upon, so it is important that the guest mappings have the same cache attributes as any potential host mappings.

---
    drivers/dma-buf/dma-buf.c    | 26 ++++++++++++++++++++++++++
    include/linux/dma-buf.h      |  7 +++++++
    include/uapi/linux/dma-buf.h | 28 ++++++++++++++++++++++++++++
    3 files changed, 61 insertions(+)
drivers/dma-buf/dma-buf.c +26 -0
index 32f55640890c..d02d6c2a3b49 100644
@@ -326,6 +326,29 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
        return 0;
    }
 
+static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
+{
+     struct dma_buf_info arg;
+
+     if (copy_from_user(&arg, uarg, sizeof(arg)))
+             return -EFAULT;
+
+     switch (arg.param) {
+     case DMA_BUF_INFO_VM_PROT:
+             if (!dmabuf->ops->mmap_info)
+                     return -ENOSYS;
+             arg.value = dmabuf->ops->mmap_info(dmabuf);
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     if (copy_to_user(uarg, &arg, sizeof(arg)))
+             return -EFAULT;
+
+     return 0;
+}
+
    static long dma_buf_ioctl(struct file *file,
                          unsigned int cmd, unsigned long arg)
    {
@@ -369,6 +392,9 @@ static long dma_buf_ioctl(struct file *file,
        case DMA_BUF_SET_NAME_B:
                return dma_buf_set_name(dmabuf, (const char __user *)arg);
 
+     case DMA_BUF_IOCTL_INFO:
+             return dma_buf_info(dmabuf, (const void __user *)arg);
+
        default:
                return -ENOTTY;
        }
include/linux/dma-buf.h +7 -0
index 71731796c8c3..6f4de64a5937 100644
@@ -283,6 +283,13 @@ struct dma_buf_ops {
         */
        int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
 
+     /**
+      * @mmap_info:
+      *
+      * Return mmapping info for the buffer.  See DMA_BUF_INFO_VM_PROT.
+      */
+     int (*mmap_info)(struct dma_buf *);
+
        int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
        void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
    };
include/uapi/linux/dma-buf.h +28 -0
index b1523cb8ab30..a41adac0f46a 100644
@@ -85,6 +85,32 @@ struct dma_buf_sync {
 
    #define DMA_BUF_NAME_LEN    32
 
+
+/**
+ * struct dma_buf_info - Query info about the buffer.
+ */
+struct dma_buf_info {
+
+#define DMA_BUF_INFO_VM_PROT      1
+#  define DMA_BUF_VM_PROT_WC      0
+#  define DMA_BUF_VM_PROT_CACHED  1
+
+     /**
+      * @param: Which param to query
+      *
+      * DMA_BUF_INFO_BM_PROT:
+      *     Query the access permissions of userspace mmap's of this buffer.
+      *     Returns one of DMA_BUF_VM_PROT_x
+      */
+     __u32 param;
+     __u32 pad;
+
+     /**
+      * @value: Return value of the query.
+      */
+     __u64 value;
+};
+
    #define DMA_BUF_BASE                'b'
    #define DMA_BUF_IOCTL_SYNC  _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
 
@@ -95,4 +121,6 @@ struct dma_buf_sync {
    #define DMA_BUF_SET_NAME_A  _IOW(DMA_BUF_BASE, 1, __u32)
    #define DMA_BUF_SET_NAME_B  _IOW(DMA_BUF_BASE, 1, __u64)
 
+#define DMA_BUF_IOCTL_INFO   _IOWR(DMA_BUF_BASE, 2, struct dma_buf_info)
+
    #endif
author
#11 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Quoted 48 lines · Christian König

Am 07.08.22 um 19:35 schrieb Rob Clark:

Quoted 45 lines
Quoted 16 lines · Christian König

Am 07.08.22 um 19:02 schrieb Rob Clark:

Quoted 15 lines
Quoted 10 lines · Christian König

Am 29.07.22 um 19:07 schrieb Rob Clark:

Quoted 8 lines

From: Rob Clark <[email protected]>

This is a fairly narrowly focused interface, providing a way for a VMM in userspace to tell the guest kernel what pgprot settings to use when mapping a buffer to guest userspace.

For buffers that get mapped into guest userspace, virglrenderer returns a dma-buf fd to the VMM (crosvm or qemu).

Wow, wait a second. Who is giving whom the DMA-buf fd here?

Not sure I understand the question.. the dma-buf fd could come from EGL_MESA_image_dma_buf_export, gbm, or similar.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

My last status was that this design was illegal and couldn't be implemented because it requires internal knowledge only the exporting driver can have.

This ioctl provides that information from the exporting driver so that a VMM doesn't have to make assumptions ;-)

And exactly that was NAKed the last time it came up. Only the exporting driver is allowed to mmap() the DMA-buf into the guest.

except the exporting driver is in the host ;-)

Quoted 10 lines

This way you also don't need to transport any caching information anywhere.

Quoted 5 lines

Currently crosvm assumes if (drivername == "i915") then it is a cached mapping, otherwise it is wc. I'm trying to find a way to fix this. Suggestions welcome, but because of how mapping to a guest VM works, a VMM is a somewhat special case where this information is needed in userspace.

Ok that leaves me completely puzzled. How does that work in the first place?

In other words how does the mapping into the guest page tables happen?

There are multiple levels to this, but in short mapping to guest userspace happens via drm/virtio (aka "virtio_gpu" or "virtgpu"), the cache attributes are set via "map_info" attribute returned from the host VMM (host userspace, hence the need for this ioctl).

In the host, the host kernel driver mmaps to host userspace (VMM). Here the exporting driver is performing the mmap with correct cache attributes.

Quoted 7 lines
Quoted 1 line

The VMM uses KVM to map these pages into the guest so

And exactly that was declared completely illegal the last time it came up on the mailing list.

Daniel implemented a whole bunch of patches into the DMA-buf layer to make it impossible for KVM to do this.

This issue isn't really with KVM, it is not making any CPU mappings itself. KVM is just making the pages available to the guest. Like I said the CPU mapping to the guest userspace is setup by virtgpu. But based on information that the host VMM provides. This patch simply provides a way for the host VMM to provide the correct information.

Quoted 2 lines

I have absolutely no idea why that is now a topic again and why anybody is still using this approach.

Because this is how VMMs work. And it is how the virtgpu device spec[1] is designed.

[1] https://github.com/oasis-tcs/virtio-spec/blob/master/virtio-gpu.tex#L767

Quoted 1 line

@Daniel can you clarify.

Like I've said, I'd be happy to hear alternative suggestions. But the root problem is that it is not possible for the host kernel to directly map into guest userspace. So I don't really see an alternative. Even if it were possible for host kernel to directly map to guest userspace, that ship has already sailed as far as virtio device specification.

BR,
-R

Quoted 141 lines

Thanks,
Christian.

Quoted 137 lines

they appear as physical pages to the guest kernel. The guest kernel (virtgpu) in turn maps them to guest userspace.

BR,
-R

Quoted 131 lines

Regards,
Christian.

Quoted 128 lines

BR,
-R

Quoted 125 lines

@Daniel has anything changed on that is or my status still valid?

Regards,
Christian.

Quoted 120 lines

In addition to mapping the
pages into the guest VM, it needs to report to drm/virtio in the guest the cache settings to use for guest userspace. In particular, on some architectures, creating aliased mappings with different cache attributes is frowned upon, so it is important that the guest mappings have the same cache attributes as any potential host mappings.

---
    drivers/dma-buf/dma-buf.c    | 26 ++++++++++++++++++++++++++
    include/linux/dma-buf.h      |  7 +++++++
    include/uapi/linux/dma-buf.h | 28 ++++++++++++++++++++++++++++
    3 files changed, 61 insertions(+)
drivers/dma-buf/dma-buf.c +26 -0
index 32f55640890c..d02d6c2a3b49 100644
@@ -326,6 +326,29 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
        return 0;
    }
 
+static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
+{
+     struct dma_buf_info arg;
+
+     if (copy_from_user(&arg, uarg, sizeof(arg)))
+             return -EFAULT;
+
+     switch (arg.param) {
+     case DMA_BUF_INFO_VM_PROT:
+             if (!dmabuf->ops->mmap_info)
+                     return -ENOSYS;
+             arg.value = dmabuf->ops->mmap_info(dmabuf);
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     if (copy_to_user(uarg, &arg, sizeof(arg)))
+             return -EFAULT;
+
+     return 0;
+}
+
    static long dma_buf_ioctl(struct file *file,
                          unsigned int cmd, unsigned long arg)
    {
@@ -369,6 +392,9 @@ static long dma_buf_ioctl(struct file *file,
        case DMA_BUF_SET_NAME_B:
                return dma_buf_set_name(dmabuf, (const char __user *)arg);
 
+     case DMA_BUF_IOCTL_INFO:
+             return dma_buf_info(dmabuf, (const void __user *)arg);
+
        default:
                return -ENOTTY;
        }
include/linux/dma-buf.h +7 -0
index 71731796c8c3..6f4de64a5937 100644
@@ -283,6 +283,13 @@ struct dma_buf_ops {
         */
        int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
 
+     /**
+      * @mmap_info:
+      *
+      * Return mmapping info for the buffer.  See DMA_BUF_INFO_VM_PROT.
+      */
+     int (*mmap_info)(struct dma_buf *);
+
        int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
        void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
    };
include/uapi/linux/dma-buf.h +28 -0
index b1523cb8ab30..a41adac0f46a 100644
@@ -85,6 +85,32 @@ struct dma_buf_sync {
 
    #define DMA_BUF_NAME_LEN    32
 
+
+/**
+ * struct dma_buf_info - Query info about the buffer.
+ */
+struct dma_buf_info {
+
+#define DMA_BUF_INFO_VM_PROT      1
+#  define DMA_BUF_VM_PROT_WC      0
+#  define DMA_BUF_VM_PROT_CACHED  1
+
+     /**
+      * @param: Which param to query
+      *
+      * DMA_BUF_INFO_BM_PROT:
+      *     Query the access permissions of userspace mmap's of this buffer.
+      *     Returns one of DMA_BUF_VM_PROT_x
+      */
+     __u32 param;
+     __u32 pad;
+
+     /**
+      * @value: Return value of the query.
+      */
+     __u64 value;
+};
+
    #define DMA_BUF_BASE                'b'
    #define DMA_BUF_IOCTL_SYNC  _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
 
@@ -95,4 +121,6 @@ struct dma_buf_sync {
    #define DMA_BUF_SET_NAME_A  _IOW(DMA_BUF_BASE, 1, __u32)
    #define DMA_BUF_SET_NAME_B  _IOW(DMA_BUF_BASE, 1, __u64)
 
+#define DMA_BUF_IOCTL_INFO   _IOWR(DMA_BUF_BASE, 2, struct dma_buf_info)
+
    #endif
#12 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Am 07.08.22 um 19:56 schrieb Rob Clark:

Quoted 10 lines
Quoted 6 lines · Christian König

[SNIP]
And exactly that was declared completely illegal the last time it came up on the mailing list.

Daniel implemented a whole bunch of patches into the DMA-buf layer to make it impossible for KVM to do this.

This issue isn't really with KVM, it is not making any CPU mappings itself. KVM is just making the pages available to the guest.

Well I can only repeat myself: This is strictly illegal.

Please try this approach with CONFIG_DMABUF_DEBUG set. I'm pretty sure you will immediately run into a crash.

See this here as well https://elixir.bootlin.com/linux/v5.19/source/drivers/dma-buf/dma-buf.c#L653

Daniel intentionally added code to mangle the page pointers to make it impossible for KVM to do this.

If the virtio/virtgpu UAPI was build around the idea that this is possible then it is most likely fundamental broken.

Regards,
Christian.

author
#13 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Quoted 15 lines · Christian König

Am 07.08.22 um 19:56 schrieb Rob Clark:

Quoted 10 lines
Quoted 6 lines · Christian König

[SNIP]
And exactly that was declared completely illegal the last time it came up on the mailing list.

Daniel implemented a whole bunch of patches into the DMA-buf layer to make it impossible for KVM to do this.

This issue isn't really with KVM, it is not making any CPU mappings itself. KVM is just making the pages available to the guest.

Well I can only repeat myself: This is strictly illegal.

Quoted 8 lines

Please try this approach with CONFIG_DMABUF_DEBUG set. I'm pretty sure you will immediately run into a crash.

See this here as well
https://elixir.bootlin.com/linux/v5.19/source/drivers/dma-buf/dma-buf.c#L653

Daniel intentionally added code to mangle the page pointers to make it impossible for KVM to do this.

I don't believe KVM is using the sg table, so this isn't going to stop anything ;-)

Quoted 2 lines

If the virtio/virtgpu UAPI was build around the idea that this is possible then it is most likely fundamental broken.

How else can you envision mmap'ing to guest userspace working? The guest kernel is the one that controls the guest userspace pagetables, not the host kernel. I guess your complaint is about VMs in general, but unfortunately I don't think you'll convince the rest of the industry to abandon VMs ;-)

But more seriously, let's take a step back here.. what scenarios are you seeing this being problematic for? Then we can see how to come up with solutions. The current situation of host userspace VMM just guessing isn't great. And sticking our heads in the sand and pretending VMs don't exist isn't great. So what can we do? I can instead add a msm ioctl to return this info and solve the problem even more narrowly for a single platform. But then the problem still remains on other platforms.

Slightly implicit in this is that mapping dma-bufs to the guest won't work for anything that requires DMA_BUF_IOCTL_SYNC for coherency.. we could add a possible return value for DMA_BUF_INFO_VM_PROT indicating that the buffer does not support mapping to guest or CPU access without DMA_BUF_IOCTL_SYNC. Then at least the VMM can fail gracefully instead of subtly.

BR,
-R

maintainer
#14 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Am 07.08.22 um 21:10 schrieb Rob Clark:

Quoted 18 lines
Quoted 16 lines · Christian König

Am 07.08.22 um 19:56 schrieb Rob Clark:

Quoted 10 lines
Quoted 6 lines · Christian König

[SNIP]
And exactly that was declared completely illegal the last time it came up on the mailing list.

Daniel implemented a whole bunch of patches into the DMA-buf layer to make it impossible for KVM to do this.

This issue isn't really with KVM, it is not making any CPU mappings itself. KVM is just making the pages available to the guest.

Well I can only repeat myself: This is strictly illegal.

Please try this approach with CONFIG_DMABUF_DEBUG set. I'm pretty sure you will immediately run into a crash.

Quoted 7 lines
Quoted 5 lines

See this here as well
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Felixir.bootlin.com%2Flinux%2Fv5.19%2Fsource%2Fdrivers%2Fdma-buf%2Fdma-buf.c%23L653&amp;data=05%7C01%7Cchristian.koenig%40amd.com%7Cc1392f76994f4fef7c7f08da78a86283%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637954961892996770%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=T3g9ICZizCWXkIn5vEnhFYs38Uj37jCwHjMb1s3UtOw%3D&amp;reserved=0

Daniel intentionally added code to mangle the page pointers to make it impossible for KVM to do this.

I don't believe KVM is using the sg table, so this isn't going to stop anything ;-)

Then I have no idea how KVM actually works. Can you please briefly describe that?

Quoted 3 lines
Quoted 2 lines

If the virtio/virtgpu UAPI was build around the idea that this is possible then it is most likely fundamental broken.

How else can you envision mmap'ing to guest userspace working?

Well long story short: You can't.

See userspace mappings are not persistent, but rather faulted in on demand. The exporter is responsible for setting those up to be able to add reverse tracking and so can invalidate those mappings when the backing store changes.

Quoted 4 lines

The guest kernel is the one that controls the guest userspace pagetables, not the host kernel. I guess your complaint is about VMs in general, but unfortunately I don't think you'll convince the rest of the industry to abandon VMs ;-)

I'm not arguing against the usefulness of VM, it's just that what you describe here technically is just utterly nonsense as far as I can tell.

I have to confess that I'm totally lacking how this KVM mapping works, but when the struct pages pointers from the sg_table are not used I see two possibilities what was implemented here:

1. KVM is somehow walking the page tables to figure out what to map into the guest VM.

    This would be *HIGHLY* illegal and not just with DMA-buf, but with pretty much a whole bunch of other drivers/subsystems as well.
    In other words it would be trivial for the guest to take over the host with that because it doesn't take into account that the underlying backing store of DMA-buf and other mmaped() areas can change at any time.

2. The guest VM triggers the fault handler for the mappings to fill in their page tables on demand.

    That would actually work with DMA-buf, but then the guest needs to somehow use the caching attributes from the host side and not use it's own. Because otherwise you can't accommodate that the exporter is changing those caching attributes.

Quoted 4 lines

But more seriously, let's take a step back here.. what scenarios are you seeing this being problematic for? Then we can see how to come up with solutions. The current situation of host userspace VMM just guessing isn't great.

Well "isn't great" is a complete understatement. When KVM/virtio/virtgpu is doing what I guess they are doing here then that is a really major security hole.

Quoted 5 lines

And sticking our heads in the sand and
pretending VMs don't exist isn't great. So what can we do? I can instead add a msm ioctl to return this info and solve the problem even more narrowly for a single platform. But then the problem still remains on other platforms.

Well once more: This is *not* MSM specific, you just absolutely *can't do that* for any driver!

I'm just really wondering what the heck is going on here, because all of this was discussed in lengthy before on the mailing list and very bluntly rejected.

Either I'm missing something (that's certainly possible) or we have a strong case of somebody implementing something without thinking about all the consequences.

Regards,
Christian.

Quoted 10 lines

Slightly implicit in this is that mapping dma-bufs to the guest won't work for anything that requires DMA_BUF_IOCTL_SYNC for coherency.. we could add a possible return value for DMA_BUF_INFO_VM_PROT indicating that the buffer does not support mapping to guest or CPU access without DMA_BUF_IOCTL_SYNC. Then at least the VMM can fail gracefully instead of subtly.

BR,
-R

author
#15 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Quoted 33 lines · Christian König

Am 07.08.22 um 21:10 schrieb Rob Clark:

Quoted 18 lines
Quoted 16 lines · Christian König

Am 07.08.22 um 19:56 schrieb Rob Clark:

Quoted 10 lines
Quoted 6 lines · Christian König

[SNIP]
And exactly that was declared completely illegal the last time it came up on the mailing list.

Daniel implemented a whole bunch of patches into the DMA-buf layer to make it impossible for KVM to do this.

This issue isn't really with KVM, it is not making any CPU mappings itself. KVM is just making the pages available to the guest.

Well I can only repeat myself: This is strictly illegal.

Please try this approach with CONFIG_DMABUF_DEBUG set. I'm pretty sure you will immediately run into a crash.

Quoted 7 lines
Quoted 5 lines

See this here as well
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Felixir.bootlin.com%2Flinux%2Fv5.19%2Fsource%2Fdrivers%2Fdma-buf%2Fdma-buf.c%23L653&amp;data=05%7C01%7Cchristian.koenig%40amd.com%7Cc1392f76994f4fef7c7f08da78a86283%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637954961892996770%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=T3g9ICZizCWXkIn5vEnhFYs38Uj37jCwHjMb1s3UtOw%3D&amp;reserved=0

Daniel intentionally added code to mangle the page pointers to make it impossible for KVM to do this.

I don't believe KVM is using the sg table, so this isn't going to stop anything ;-)

Then I have no idea how KVM actually works. Can you please briefly describe that?

Quoted 2 lines
Quoted 2 lines

If the virtio/virtgpu UAPI was build around the idea that this is possible then it is most likely fundamental broken.

Quoted 8 lines
Quoted 1 line

How else can you envision mmap'ing to guest userspace working?

Well long story short: You can't.

See userspace mappings are not persistent, but rather faulted in on demand. The exporter is responsible for setting those up to be able to add reverse tracking and so can invalidate those mappings when the backing store changes.

I think that is not actually a problem. At least for how it works on arm64 but I'm almost positive x86 is similar.. I'm not sure how else you could virtualize mmu/iommu/etc in a way that didn't have horrible performance.

There are two levels of pagetable translation, the first controlled by the host kernel, the second by the guest. From the PoV of host kernel, it is just memory mapped to userspace, getting faulted in on demand, just as normal. First the guest controlled translation triggers a fault in the guest which sets up guest mapping. And then the second level of translation to translate from what guest sees as PA (but host sees as VA) to actual PA triggers a fault in the host.

Quoted 9 lines
Quoted 5 lines

The guest kernel is the one that controls the guest userspace pagetables, not the host kernel. I guess your complaint is about VMs in general, but unfortunately I don't think you'll convince the rest of the industry to abandon VMs ;-)

I'm not arguing against the usefulness of VM, it's just that what you describe here technically is just utterly nonsense as far as I can tell.

Quoted 6 lines

I have to confess that I'm totally lacking how this KVM mapping works, but when the struct pages pointers from the sg_table are not used I see two possibilities what was implemented here:

1. KVM is somehow walking the page tables to figure out what to map into the guest VM.

it is just mapping host VA to the guest.. the guest kernel sees this as the PA and uses the level of pgtable translation that it controls to map to guest userspace. *All* that is needed (which this patch provides) is the correct cache attributes.

Quoted 11 lines

This would be *HIGHLY* illegal and not just with DMA-buf, but with
pretty much a whole bunch of other drivers/subsystems as well.
In other words it would be trivial for the guest to take over the
host with that because it doesn't take into account that the underlying backing store of DMA-buf and other mmaped() areas can change at any time.

2. The guest VM triggers the fault handler for the mappings to fill in their page tables on demand.

That would actually work with DMA-buf, but then the guest needs to
somehow use the caching attributes from the host side and not use it's own.

This is basically what happens, although via the two levels of pgtable translation. This patch provides the missing piece, the caching attributes.

Quoted 2 lines

Because otherwise you can't accommodate that the exporter is
changing those caching attributes.

Changing the attributes dynamically isn't going to work.. or at least not easily. If you had some sort of synchronous notification to host userspace, it could trigger an irq to the guest, I suppose. But it would mean host kernel has to block waiting for host userspace to interrupt the guest, then wait for guest vgpu process to be scheduled and handle the irq.

At least in the case of msm, the cache attributes are static for the life of the buffer, so this scenario isn't a problem. AFAICT this should work fine for at least all UMA hw.. I'm a bit less sure when it comes to TTM, but shouldn't you at least be able to use worst-cache cache attributes for buffers that are allowed to be mapped to guest?

BR,
-R

Quoted 42 lines
Quoted 5 lines

But more seriously, let's take a step back here.. what scenarios are you seeing this being problematic for? Then we can see how to come up with solutions. The current situation of host userspace VMM just guessing isn't great.

Well "isn't great" is a complete understatement. When KVM/virtio/virtgpu is doing what I guess they are doing here then that is a really major security hole.

Quoted 5 lines

And sticking our heads in the sand and
pretending VMs don't exist isn't great. So what can we do? I can instead add a msm ioctl to return this info and solve the problem even more narrowly for a single platform. But then the problem still remains on other platforms.

Well once more: This is *not* MSM specific, you just absolutely *can't do that* for any driver!

I'm just really wondering what the heck is going on here, because all of this was discussed in lengthy before on the mailing list and very bluntly rejected.

Either I'm missing something (that's certainly possible) or we have a strong case of somebody implementing something without thinking about all the consequences.

Regards,
Christian.

Quoted 10 lines

Slightly implicit in this is that mapping dma-bufs to the guest won't work for anything that requires DMA_BUF_IOCTL_SYNC for coherency.. we could add a possible return value for DMA_BUF_INFO_VM_PROT indicating that the buffer does not support mapping to guest or CPU access without DMA_BUF_IOCTL_SYNC. Then at least the VMM can fail gracefully instead of subtly.

BR,
-R

maintainer
#16 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Am 08.08.22 um 15:26 schrieb Rob Clark:

Quoted 17 lines · Christian König

[SNIP]

Quoted 9 lines
Quoted 3 lines
Quoted 2 lines

If the virtio/virtgpu UAPI was build around the idea that this is possible then it is most likely fundamental broken.

How else can you envision mmap'ing to guest userspace working?

Well long story short: You can't.

See userspace mappings are not persistent, but rather faulted in on demand. The exporter is responsible for setting those up to be able to add reverse tracking and so can invalidate those mappings when the backing store changes.

I think that is not actually a problem. At least for how it works on arm64 but I'm almost positive x86 is similar.. I'm not sure how else you could virtualize mmu/iommu/etc in a way that didn't have horrible performance.

Quoted 7 lines

There are two levels of pagetable translation, the first controlled by the host kernel, the second by the guest. From the PoV of host kernel, it is just memory mapped to userspace, getting faulted in on demand, just as normal. First the guest controlled translation triggers a fault in the guest which sets up guest mapping. And then the second level of translation to translate from what guest sees as PA (but host sees as VA) to actual PA triggers a fault in the host.

Ok, that's calming.

At least that's not the approach talked about the last time this came up and it doesn't rip a massive security hole somewhere.

The question is why is the guest then not using the caching attributes setup by the host page tables when the translation is forwarded anyway?

Quoted 4 lines

[SNIP]
This is basically what happens, although via the two levels of pgtable translation. This patch provides the missing piece, the caching attributes.

Yeah, but that won't work like this. See the backing store migrates all the time and when it is backed by PCIe/VRAM/local memory you need to use write combine while system memory is usually cached.

Quoted 8 lines
Quoted 2 lines

Because otherwise you can't accommodate that the exporter is
changing those caching attributes.

Changing the attributes dynamically isn't going to work.. or at least not easily. If you had some sort of synchronous notification to host userspace, it could trigger an irq to the guest, I suppose. But it would mean host kernel has to block waiting for host userspace to interrupt the guest, then wait for guest vgpu process to be scheduled and handle the irq.

We basically change that on every page flip on APUs and that doesn't sound like something fast.

Thanks for the explanation how this works,
Christian.

Quoted 47 lines

At least in the case of msm, the cache attributes are static for the life of the buffer, so this scenario isn't a problem. AFAICT this should work fine for at least all UMA hw.. I'm a bit less sure when it comes to TTM, but shouldn't you at least be able to use worst-cache cache attributes for buffers that are allowed to be mapped to guest?

BR,
-R

Quoted 37 lines
Quoted 4 lines

But more seriously, let's take a step back here.. what scenarios are you seeing this being problematic for? Then we can see how to come up with solutions. The current situation of host userspace VMM just guessing isn't great.

Well "isn't great" is a complete understatement. When KVM/virtio/virtgpu is doing what I guess they are doing here then that is a really major security hole.

Quoted 5 lines

And sticking our heads in the sand and
pretending VMs don't exist isn't great. So what can we do? I can instead add a msm ioctl to return this info and solve the problem even more narrowly for a single platform. But then the problem still remains on other platforms.

Well once more: This is *not* MSM specific, you just absolutely *can't do that* for any driver!

I'm just really wondering what the heck is going on here, because all of this was discussed in lengthy before on the mailing list and very bluntly rejected.

Either I'm missing something (that's certainly possible) or we have a strong case of somebody implementing something without thinking about all the consequences.

Regards,
Christian.

Quoted 9 lines

Slightly implicit in this is that mapping dma-bufs to the guest won't work for anything that requires DMA_BUF_IOCTL_SYNC for coherency.. we could add a possible return value for DMA_BUF_INFO_VM_PROT indicating that the buffer does not support mapping to guest or CPU access without DMA_BUF_IOCTL_SYNC. Then at least the VMM can fail gracefully instead of subtly.

BR,
-R

#17 lore raw

Re: [Linaro-mm-sig] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Quoted 27 lines · Christian König

Am 08.08.22 um 15:26 schrieb Rob Clark:

Quoted 17 lines · Christian König

[SNIP]

Quoted 9 lines
Quoted 3 lines
Quoted 2 lines

If the virtio/virtgpu UAPI was build around the idea that this is possible then it is most likely fundamental broken.

How else can you envision mmap'ing to guest userspace working?

Well long story short: You can't.

See userspace mappings are not persistent, but rather faulted in on demand. The exporter is responsible for setting those up to be able to add reverse tracking and so can invalidate those mappings when the backing store changes.

I think that is not actually a problem. At least for how it works on arm64 but I'm almost positive x86 is similar.. I'm not sure how else you could virtualize mmu/iommu/etc in a way that didn't have horrible performance.

Quoted 7 lines

There are two levels of pagetable translation, the first controlled by the host kernel, the second by the guest. From the PoV of host kernel, it is just memory mapped to userspace, getting faulted in on demand, just as normal. First the guest controlled translation triggers a fault in the guest which sets up guest mapping. And then the second level of translation to translate from what guest sees as PA (but host sees as VA) to actual PA triggers a fault in the host.

Quoted 4 lines

Ok, that's calming.

At least that's not the approach talked about the last time this came up and it doesn't rip a massive security hole somewhere.

Hmm, tbh I'm not sure which thread/discussion this was.. it could have been before I was paying much attention to the vm use-case

Quoted 2 lines

The question is why is the guest then not using the caching attributes setup by the host page tables when the translation is forwarded anyway?

The guest kernel itself doesn't know. AFAICT, at least on arm, the hw will combine the attributes of the mapping in S1 and S2 pagetables and use the most restrictive. So if S1 (host) is cached but S2 (guest) is WC, you'll end up w/ WC.

That said, at least on aarch64, it seems like we could always tell the guest it is cached, and if mapped WC in S1 you'll end up with WC access. But this seems to depend on an optional feature, FWB, which allows S2 to override S1 attributes, not being enabled. And not entirely sure how it works on x86.

BR,
-R

Quoted 72 lines
Quoted 4 lines

[SNIP]
This is basically what happens, although via the two levels of pgtable translation. This patch provides the missing piece, the caching attributes.

Yeah, but that won't work like this. See the backing store migrates all the time and when it is backed by PCIe/VRAM/local memory you need to use write combine while system memory is usually cached.

Quoted 8 lines
Quoted 2 lines

Because otherwise you can't accommodate that the exporter is
changing those caching attributes.

Changing the attributes dynamically isn't going to work.. or at least not easily. If you had some sort of synchronous notification to host userspace, it could trigger an irq to the guest, I suppose. But it would mean host kernel has to block waiting for host userspace to interrupt the guest, then wait for guest vgpu process to be scheduled and handle the irq.

We basically change that on every page flip on APUs and that doesn't sound like something fast.

Thanks for the explanation how this works,
Christian.

Quoted 47 lines

At least in the case of msm, the cache attributes are static for the life of the buffer, so this scenario isn't a problem. AFAICT this should work fine for at least all UMA hw.. I'm a bit less sure when it comes to TTM, but shouldn't you at least be able to use worst-cache cache attributes for buffers that are allowed to be mapped to guest?

BR,
-R

Quoted 37 lines
Quoted 4 lines

But more seriously, let's take a step back here.. what scenarios are you seeing this being problematic for? Then we can see how to come up with solutions. The current situation of host userspace VMM just guessing isn't great.

Well "isn't great" is a complete understatement. When KVM/virtio/virtgpu is doing what I guess they are doing here then that is a really major security hole.

Quoted 5 lines

And sticking our heads in the sand and
pretending VMs don't exist isn't great. So what can we do? I can instead add a msm ioctl to return this info and solve the problem even more narrowly for a single platform. But then the problem still remains on other platforms.

Well once more: This is *not* MSM specific, you just absolutely *can't do that* for any driver!

I'm just really wondering what the heck is going on here, because all of this was discussed in lengthy before on the mailing list and very bluntly rejected.

Either I'm missing something (that's certainly possible) or we have a strong case of somebody implementing something without thinking about all the consequences.

Regards,
Christian.

Quoted 9 lines

Slightly implicit in this is that mapping dma-bufs to the guest won't work for anything that requires DMA_BUF_IOCTL_SYNC for coherency.. we could add a possible return value for DMA_BUF_INFO_VM_PROT indicating that the buffer does not support mapping to guest or CPU access without DMA_BUF_IOCTL_SYNC. Then at least the VMM can fail gracefully instead of subtly.

BR,
-R

#18 lore raw

Re: [Freedreno] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

In reply to Rob Clark
Quoted 95 lines · Rob Clark

From: Rob Clark <[email protected]>

This is a fairly narrowly focused interface, providing a way for a VMM in userspace to tell the guest kernel what pgprot settings to use when mapping a buffer to guest userspace.

For buffers that get mapped into guest userspace, virglrenderer returns a dma-buf fd to the VMM (crosvm or qemu). In addition to mapping the pages into the guest VM, it needs to report to drm/virtio in the guest the cache settings to use for guest userspace. In particular, on some architectures, creating aliased mappings with different cache attributes is frowned upon, so it is important that the guest mappings have the same cache attributes as any potential host mappings.

---
  drivers/dma-buf/dma-buf.c    | 26 ++++++++++++++++++++++++++
  include/linux/dma-buf.h      |  7 +++++++
  include/uapi/linux/dma-buf.h | 28 ++++++++++++++++++++++++++++
  3 files changed, 61 insertions(+)
drivers/dma-buf/dma-buf.c +26 -0
index 32f55640890c..d02d6c2a3b49 100644
@@ -326,6 +326,29 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
  	return 0;
  }
 
+static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
+{
+	struct dma_buf_info arg;
+
+	if (copy_from_user(&arg, uarg, sizeof(arg)))
+		return -EFAULT;
+
+	switch (arg.param) {
+	case DMA_BUF_INFO_VM_PROT:
+		if (!dmabuf->ops->mmap_info)
+			return -ENOSYS;
+		arg.value = dmabuf->ops->mmap_info(dmabuf);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (copy_to_user(uarg, &arg, sizeof(arg)))
+		return -EFAULT;
+
+	return 0;
+}
+
  static long dma_buf_ioctl(struct file *file,
  			  unsigned int cmd, unsigned long arg)
  {
@@ -369,6 +392,9 @@ static long dma_buf_ioctl(struct file *file,
  	case DMA_BUF_SET_NAME_B:
  		return dma_buf_set_name(dmabuf, (const char __user *)arg);
 
+	case DMA_BUF_IOCTL_INFO:
+		return dma_buf_info(dmabuf, (const void __user *)arg);
+
  	default:
  		return -ENOTTY;
  	}
include/linux/dma-buf.h +7 -0
index 71731796c8c3..6f4de64a5937 100644
@@ -283,6 +283,13 @@ struct dma_buf_ops {
  	 */
  	int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
 
+	/**
+	 * @mmap_info:
+	 *
+	 * Return mmapping info for the buffer.  See DMA_BUF_INFO_VM_PROT.
+	 */
+	int (*mmap_info)(struct dma_buf *);
+
  	int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
  	void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
  };
include/uapi/linux/dma-buf.h +4 -0
index b1523cb8ab30..a41adac0f46a 100644
@@ -85,6 +85,32 @@ struct dma_buf_sync {
 
  #define DMA_BUF_NAME_LEN	32
 
+
+/**
+ * struct dma_buf_info - Query info about the buffer.
+ */

include/uapi/linux/dma-buf.h struct dma_buf_sync { · line 101

Quoted 10 lines
include/uapi/linux/dma-buf.h +10 -0
+struct dma_buf_info {
+
+#define DMA_BUF_INFO_VM_PROT      1
+#  define DMA_BUF_VM_PROT_WC      0
+#  define DMA_BUF_VM_PROT_CACHED  1
+
+	/**
+	 * @param: Which param to query
+	 *
+	 * DMA_BUF_INFO_BM_PROT:

Is there a typo here? BM -> VM ?

-Akhil.

Quoted 22 lines
include/uapi/linux/dma-buf.h +14 -0
+	 *     Query the access permissions of userspace mmap's of this buffer.
+	 *     Returns one of DMA_BUF_VM_PROT_x
+	 */
+	__u32 param;
+	__u32 pad;
+
+	/**
+	 * @value: Return value of the query.
+	 */
+	__u64 value;
+};
+
  #define DMA_BUF_BASE		'b'
  #define DMA_BUF_IOCTL_SYNC	_IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
 
@@ -95,4 +121,6 @@ struct dma_buf_sync {
  #define DMA_BUF_SET_NAME_A	_IOW(DMA_BUF_BASE, 1, __u32)
  #define DMA_BUF_SET_NAME_B	_IOW(DMA_BUF_BASE, 1, __u64)
 
+#define DMA_BUF_IOCTL_INFO	_IOWR(DMA_BUF_BASE, 2, struct dma_buf_info)
+
  #endif
author
#19 lore raw

Re: [Freedreno] [PATCH 1/3] dma-buf: Add ioctl to query mmap info

Quoted 93 lines · Akhil P Oommen
Quoted 91 lines · Rob Clark

From: Rob Clark <[email protected]>

This is a fairly narrowly focused interface, providing a way for a VMM in userspace to tell the guest kernel what pgprot settings to use when mapping a buffer to guest userspace.

For buffers that get mapped into guest userspace, virglrenderer returns a dma-buf fd to the VMM (crosvm or qemu). In addition to mapping the pages into the guest VM, it needs to report to drm/virtio in the guest the cache settings to use for guest userspace. In particular, on some architectures, creating aliased mappings with different cache attributes is frowned upon, so it is important that the guest mappings have the same cache attributes as any potential host mappings.

---
  drivers/dma-buf/dma-buf.c    | 26 ++++++++++++++++++++++++++
  include/linux/dma-buf.h      |  7 +++++++
  include/uapi/linux/dma-buf.h | 28 ++++++++++++++++++++++++++++
  3 files changed, 61 insertions(+)
drivers/dma-buf/dma-buf.c +26 -0
index 32f55640890c..d02d6c2a3b49 100644
@@ -326,6 +326,29 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
      return 0;
  }
 
+static long dma_buf_info(struct dma_buf *dmabuf, const void __user *uarg)
+{
+     struct dma_buf_info arg;
+
+     if (copy_from_user(&arg, uarg, sizeof(arg)))
+             return -EFAULT;
+
+     switch (arg.param) {
+     case DMA_BUF_INFO_VM_PROT:
+             if (!dmabuf->ops->mmap_info)
+                     return -ENOSYS;
+             arg.value = dmabuf->ops->mmap_info(dmabuf);
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     if (copy_to_user(uarg, &arg, sizeof(arg)))
+             return -EFAULT;
+
+     return 0;
+}
+
  static long dma_buf_ioctl(struct file *file,
                        unsigned int cmd, unsigned long arg)
  {
@@ -369,6 +392,9 @@ static long dma_buf_ioctl(struct file *file,
      case DMA_BUF_SET_NAME_B:
              return dma_buf_set_name(dmabuf, (const char __user *)arg);
 
+     case DMA_BUF_IOCTL_INFO:
+             return dma_buf_info(dmabuf, (const void __user *)arg);
+
      default:
              return -ENOTTY;
      }
include/linux/dma-buf.h +7 -0
index 71731796c8c3..6f4de64a5937 100644
@@ -283,6 +283,13 @@ struct dma_buf_ops {
       */
      int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
 
+     /**
+      * @mmap_info:
+      *
+      * Return mmapping info for the buffer.  See DMA_BUF_INFO_VM_PROT.
+      */
+     int (*mmap_info)(struct dma_buf *);
+
      int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
      void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
  };
include/uapi/linux/dma-buf.h +0 -0
index b1523cb8ab30..a41adac0f46a 100644
@@ -85,6 +85,32 @@ struct dma_buf_sync {
 
  #define DMA_BUF_NAME_LEN    32
 
Quoted 15 lines
Quoted 4 lines
include/uapi/linux/dma-buf.h +4 -0
+
+/**
+ * struct dma_buf_info - Query info about the buffer.
+ */

include/uapi/linux/dma-buf.h struct dma_buf_sync { · line 101

Quoted 10 lines
include/uapi/linux/dma-buf.h +10 -0
+struct dma_buf_info {
+
+#define DMA_BUF_INFO_VM_PROT      1
+#  define DMA_BUF_VM_PROT_WC      0
+#  define DMA_BUF_VM_PROT_CACHED  1
+
+     /**
+      * @param: Which param to query
+      *
+      * DMA_BUF_INFO_BM_PROT:

Is there a typo here? BM -> VM ?

yes, fixed locally

Quoted 25 lines

-Akhil.

Quoted 22 lines
include/uapi/linux/dma-buf.h +14 -0
+      *     Query the access permissions of userspace mmap's of this buffer.
+      *     Returns one of DMA_BUF_VM_PROT_x
+      */
+     __u32 param;
+     __u32 pad;
+
+     /**
+      * @value: Return value of the query.
+      */
+     __u64 value;
+};
+
  #define DMA_BUF_BASE                'b'
  #define DMA_BUF_IOCTL_SYNC  _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
 
@@ -95,4 +121,6 @@ struct dma_buf_sync {
  #define DMA_BUF_SET_NAME_A  _IOW(DMA_BUF_BASE, 1, __u32)
  #define DMA_BUF_SET_NAME_B  _IOW(DMA_BUF_BASE, 1, __u64)
 
+#define DMA_BUF_IOCTL_INFO   _IOWR(DMA_BUF_BASE, 2, struct dma_buf_info)
+
  #endif
author
#20 lore raw

PATCH 2/3 drm/prime: Wire up mmap_info support

In reply to Rob Clark

From: Rob Clark <[email protected]>

Just plumbing the thing thru an extra layer.

---
 drivers/gpu/drm/drm_prime.c | 12 ++++++++++++
 include/drm/drm_drv.h       |  7 +++++++
 2 files changed, 19 insertions(+)
drivers/gpu/drm/drm_prime.c +12 -0
index e3f09f18110c..f58586e131c5 100644
@@ -784,6 +784,17 @@ int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
 }
 EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
 
+static int drm_gem_dmabuf_mmap_info(struct dma_buf *dma_buf)
+{
+	struct drm_gem_object *obj = dma_buf->priv;
+	struct drm_device *dev = obj->dev;
+
+	if (!dev->driver->gem_prime_mmap_info)
+		return -ENOSYS;
+
+	return dev->driver->gem_prime_mmap_info(obj);
+}
+
 static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
 	.cache_sgt_mapping = true,
 	.attach = drm_gem_map_attach,
@@ -792,6 +803,7 @@ static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
 	.unmap_dma_buf = drm_gem_unmap_dma_buf,
 	.release = drm_gem_dmabuf_release,
 	.mmap = drm_gem_dmabuf_mmap,
+	.mmap_info = drm_gem_dmabuf_mmap_info,
 	.vmap = drm_gem_dmabuf_vmap,
 	.vunmap = drm_gem_dmabuf_vunmap,
 };
include/drm/drm_drv.h +7 -0
index f6159acb8856..797c0f8c2dd0 100644
@@ -355,6 +355,13 @@ struct drm_driver {
 	 */
 	int (*gem_prime_mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
 
+	/**
+	 * @gem_prim_mmap_info:
+	 *
+	 * Get info about mmap setup by gem_prime_mmap.  See dma_buf_ops:mmap_info.
+	 */
+	int (*gem_prime_mmap_info)(struct drm_gem_object *obj);
+
 	/**
 	 * @dumb_create:
 	 *
Signature · 1 line
2.36.1
author
#21 lore raw

PATCH 3/3 drm/msm/prime: Add mmap_info support

In reply to Rob Clark

From: Rob Clark <[email protected]>

---
 drivers/gpu/drm/msm/msm_drv.c       |  1 +
 drivers/gpu/drm/msm/msm_drv.h       |  1 +
 drivers/gpu/drm/msm/msm_gem_prime.c | 11 +++++++++++
 3 files changed, 13 insertions(+)
drivers/gpu/drm/msm/msm_drv.c +1 -0
index 1ca4a92ba96e..4979aa8187ec 100644
@@ -1044,6 +1044,7 @@ static const struct drm_driver msm_driver = {
 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
 	.gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
 	.gem_prime_mmap     = msm_gem_prime_mmap,
+	.gem_prime_mmap_info= msm_gem_prime_mmap_info,
 #ifdef CONFIG_DEBUG_FS
 	.debugfs_init       = msm_debugfs_init,
 #endif
drivers/gpu/drm/msm/msm_drv.h +1 -0
index 7330d7b5de8e..b4ace34ec889 100644
@@ -271,6 +271,7 @@ void msm_gem_shrinker_init(struct drm_device *dev);
 void msm_gem_shrinker_cleanup(struct drm_device *dev);
 
 int msm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
+int msm_gem_prime_mmap_info(struct drm_gem_object *obj);
 struct sg_table *msm_gem_prime_get_sg_table(struct drm_gem_object *obj);
 int msm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map);
 void msm_gem_prime_vunmap(struct drm_gem_object *obj, struct iosys_map *map);
drivers/gpu/drm/msm/msm_gem_prime.c +11 -0
index c1d91863df05..2bacab7a1921 100644
@@ -5,6 +5,7 @@
  */
 
 #include <linux/dma-buf.h>
+#include <uapi/linux/dma-buf.h>
 
 #include <drm/drm_prime.h>
 
@@ -26,6 +27,16 @@ int msm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
 	return drm_gem_prime_mmap(obj, vma);
 }
 
+int msm_gem_prime_mmap_info(struct drm_gem_object *obj)
+{
+	struct msm_gem_object *msm_obj = to_msm_bo(obj);
+
+	if (msm_obj->flags & MSM_BO_WC)
+		return DMA_BUF_VM_PROT_WC;
+
+	return DMA_BUF_VM_PROT_CACHED;
+}
+
 struct sg_table *msm_gem_prime_get_sg_table(struct drm_gem_object *obj)
 {
 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
Signature · 1 line
2.36.1