Skip to contentSkip to conversation

drm/virtio: delay pinning the pages till first use

PATCH In review Maksym Wezdecki 4 participants 5 messages Nov 2, 2021 – Nov 4, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/virtio: delay pinning the pages till first use

From: mwezdeck <[email protected]>

The idea behind the commit:
1. not pin the pages during resource_create ioctl
2. pin the pages on the first use during:
- transfer_*_host ioctl
- map ioctl
3. introduce new ioctl for pinning pages on demand

This change has no impact on user space.

---
 drivers/gpu/drm/virtio/virtgpu_drv.h    |  6 ++-
 drivers/gpu/drm/virtio/virtgpu_ioctl.c  | 65 +++++++++++++++++++++++++
 drivers/gpu/drm/virtio/virtgpu_object.c | 42 ++++++++++++----
 include/uapi/drm/virtgpu_drm.h          | 10 ++++
 4 files changed, 113 insertions(+), 10 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +5 -1
index e0265fe74aa5..cf2cad663575 100644
@@ -278,7 +278,7 @@ struct virtio_gpu_fpriv {
 };
 
 /* virtgpu_ioctl.c */
-#define DRM_VIRTIO_NUM_IOCTLS 12
+#define DRM_VIRTIO_NUM_IOCTLS 13
 extern struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS];
 void virtio_gpu_create_context(struct drm_device *dev, struct drm_file *file);
 
@@ -455,6 +455,10 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
 			     struct virtio_gpu_object **bo_ptr,
 			     struct virtio_gpu_fence *fence);
 
+int virtio_gpu_object_pin(struct virtio_gpu_device *vgdev,
+			  struct virtio_gpu_object_array *objs,
+			  int num_gem_objects);
+
 bool virtio_gpu_is_shmem(struct virtio_gpu_object *bo);
 
 int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
drivers/gpu/drm/virtio/virtgpu_ioctl.c +65 -0

111 lines not on this page · raw message

drivers/gpu/drm/virtio/virtgpu_object.c +33 -9

66 lines not on this page · raw message

include/uapi/drm/virtgpu_drm.h +10 -0
index a13e20cc66b4..be6e67f1bb7f 100644
@@ -48,6 +48,7 @@ extern "C" {
 #define DRM_VIRTGPU_GET_CAPS  0x09
 #define DRM_VIRTGPU_RESOURCE_CREATE_BLOB 0x0a
 #define DRM_VIRTGPU_CONTEXT_INIT 0x0b
+#define DRM_VIRTGPU_PIN 0x0c
 
 #define VIRTGPU_EXECBUF_FENCE_FD_IN	0x01
 #define VIRTGPU_EXECBUF_FENCE_FD_OUT	0x02
@@ -82,6 +83,7 @@ struct drm_virtgpu_execbuffer {
 #define VIRTGPU_PARAM_CROSS_DEVICE 5 /* Cross virtio-device resource sharing  */
 #define VIRTGPU_PARAM_CONTEXT_INIT 6 /* DRM_VIRTGPU_CONTEXT_INIT */
 #define VIRTGPU_PARAM_SUPPORTED_CAPSET_IDs 7 /* Bitmask of supported capability set ids */
+#define VIRTGPU_PARAM_PIN_ON_DEMAND 8 /* is pinning on demand available? */
 
 struct drm_virtgpu_getparam {
 	__u64 param;
@@ -196,6 +198,10 @@ struct drm_virtgpu_context_init {
 	__u64 ctx_set_params;
 };
 
+struct drm_virtgpu_pin {
+	__u32 handle;
+};
+
 #define DRM_IOCTL_VIRTGPU_MAP \
 	DRM_IOWR(DRM_COMMAND_BASE + DRM_VIRTGPU_MAP, struct drm_virtgpu_map)
 
@@ -239,6 +245,10 @@ struct drm_virtgpu_context_init {
 	DRM_IOWR(DRM_COMMAND_BASE + DRM_VIRTGPU_CONTEXT_INIT,		\
 		struct drm_virtgpu_context_init)
 
+#define DRM_IOCTL_VIRTGPU_PIN					\
+	DRM_IOWR(DRM_COMMAND_BASE + DRM_VIRTGPU_PIN,		\
+		struct drm_virtgpu_pin)
+
 #if defined(__cplusplus)
 }
 #endif
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 7 lines · Maksym Wezdecki

From: mwezdeck <[email protected]>

The idea behind the commit:
1. not pin the pages during resource_create ioctl
2. pin the pages on the first use during:
- transfer_*_host ioctl
- map ioctl

i.e. basically lazy pinning. Approach looks sane to me.

Quoted 1 line

3. introduce new ioctl for pinning pages on demand

What is the use case for this ioctl?
In any case this should be a separate patch.

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +9 -0
+	struct virtio_gpu_object_array *objs;
+	struct virtio_gpu_object *bo;
+	struct virtio_gpu_object_shmem *shmem;
+
+	objs = virtio_gpu_array_from_handles(file, &virtio_gpu_map->handle, 1);
+	if (objs == NULL)
+		return -ENOENT;
+
+	bo = gem_to_virtio_gpu_obj(objs->objs[0]);

drivers/gpu/drm/virtio/virtgpu_ioctl.c static int virtio_gpu_map_ioctl(struct drm_device *dev, void *data, · line 123

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +10 -0
+	if (bo == NULL)
+		return -ENOENT;
+	
+	shmem = to_virtio_gpu_shmem(bo);
+	if (shmem == NULL)
+		return -ENOENT;
+
+	if (!shmem->pages) {
+		virtio_gpu_object_pin(vgdev, objs, 1);
+	}

Move this into virtio_gpu_object_pin(),
or create a helper function for it ...

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +5 -0
+	objs = virtio_gpu_array_from_handles(file, &virtio_gpu_pin->handle, 1);
+	if (objs == NULL)
+		return -ENOENT;
+
+	bo = gem_to_virtio_gpu_obj(objs->objs[0]);

drivers/gpu/drm/virtio/virtgpu_ioctl.c static int virtio_gpu_context_init_ioctl(struct drm_device *dev, · line 896

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +10 -0
+	if (bo == NULL)
+		return -ENOENT;
+	
+	shmem = to_virtio_gpu_shmem(bo);
+	if (shmem == NULL)
+		return -ENOENT;
+
+	if (!shmem->pages) {
+		return virtio_gpu_object_pin(vgdev, objs, 1);
+	}

... to avoid this code duplication?

Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_object.c +4 -0
+int virtio_gpu_object_pin(struct virtio_gpu_device *vgdev,
+			  struct virtio_gpu_object_array *objs,
+			  int num_gem_objects)
+{

drivers/gpu/drm/virtio/virtgpu_object.c int virtio_gpu_object_create(struct virtio_gpu_device *vgdev, · line 303

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_object.c +9 -0
+	int i, ret;
+
+	for (i = 0; i < num_gem_objects; i++) {
 
+		ret = virtio_gpu_object_shmem_init(vgdev, bo, &ents, &nents);
+		if (ret != 0) {
+			return -EFAULT;
+		}
+
+		virtio_gpu_object_attach(vgdev, bo, ents, nents);

I think it is enough to do the virtio_gpu_object_attach() call lazily. virtio_gpu_object_shmem_init() should not actually allocate pages, that only happens when virtio_gpu_object_attach() goes ask for a scatter list.

take care,
Gerd

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

reviewer
#2 lore raw
Quoted 10 lines · Gerd Hoffmann
Quoted 7 lines · Maksym Wezdecki

From: mwezdeck <[email protected]>

The idea behind the commit:
1. not pin the pages during resource_create ioctl
2. pin the pages on the first use during:
- transfer_*_host ioctl
- map ioctl

Quoted 6 lines

i.e. basically lazy pinning. Approach looks sane to me.

Quoted 1 line

3. introduce new ioctl for pinning pages on demand

What is the use case for this ioctl?
In any case this should be a separate patch.

Lazy pinning can be a nice optimization that userspace does not necessarily need to know about. This patch however skips pinning for execbuffer ioctl and introduces a new pin ioctl instead. That is a red flag.

maintainer
#3 lore raw
Quoted 15 lines · Chia-I Wu
Quoted 14 lines · Gerd Hoffmann
Quoted 7 lines · Maksym Wezdecki

From: mwezdeck <[email protected]>

The idea behind the commit:
1. not pin the pages during resource_create ioctl
2. pin the pages on the first use during:
- transfer_*_host ioctl
- map ioctl

i.e. basically lazy pinning. Approach looks sane to me.

Quoted 1 line

3. introduce new ioctl for pinning pages on demand

Quoted 7 lines
Quoted 2 lines

What is the use case for this ioctl?
In any case this should be a separate patch.

Lazy pinning can be a nice optimization that userspace does not necessarily need to know about. This patch however skips pinning for execbuffer ioctl and introduces a new pin ioctl instead. That is a red flag.

Ah, so the pin ioctl is for buffers which need a pin for execbuffer.

Yep, that isn't going to fly that way, it'll break old userspace.

Lazy pinning must be opt-in, so new userspace which knows about the pin ioctl can enable lazy pinning. One possible way would be to add a flag for the VIRTGPU_RESOURCE_CREATE ioctl, so lazy pinning can be enabled per resource.

take care,
Gerd

#4 lore raw
In reply to Maksym Wezdecki
Thank you for the patch! Yet something to improve:

Hi Maksym,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on drm/drm-next]
[also build test ERROR on next-20211104]
[cannot apply to v5.15]
[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]

url:    https://github.com/0day-ci/linux/commits/Maksym-Wezdecki/drm-virtio-delay-pinning-the-pages-till-first-use/20211102-193430
base:   git://anongit.freedesktop.org/drm/drm drm-next
config: i386-buildonly-randconfig-r002-20211101 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 264d3b6d4e08401c5b50a85bd76e80b3461d77e6)

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/0day-ci/linux/commit/1795d2fd78a334a37a02dba76ac1e314cf122467
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Maksym-Wezdecki/drm-virtio-delay-pinning-the-pages-till-first-use/20211102-193430 git checkout 1795d2fd78a334a37a02dba76ac1e314cf122467
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 ARCH=i386

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

>> drivers/gpu/drm/virtio/virtgpu_object.c:254:11: error: variable 'ents' is uninitialized when used here [-Werror,-Wuninitialized]
                                                       ents, nents);
                                                       ^~~~
   drivers/gpu/drm/virtio/virtgpu_object.c:219:35: note: initialize the variable 'ents' to silence this warning
           struct virtio_gpu_mem_entry *ents;
                                            ^
                                             = NULL
>> drivers/gpu/drm/virtio/virtgpu_object.c:254:17: error: variable 'nents' is uninitialized when used here [-Werror,-Wuninitialized]
                                                       ents, nents);
                                                             ^~~~~
   drivers/gpu/drm/virtio/virtgpu_object.c:220:20: note: initialize the variable 'nents' to silence this warning
           unsigned int nents;
                             ^
                              = 0

2 errors generated.

vim +/ents +254 drivers/gpu/drm/virtio/virtgpu_object.c

2f2aa13724d568 Gerd Hoffmann   2020-02-07  210  
dc5698e80cf724 Dave Airlie     2013-09-09  211  int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
4441235f9566e6 Gerd Hoffmann   2019-03-18  212  			     struct virtio_gpu_object_params *params,
530b28426a94b8 Gerd Hoffmann   2019-03-18  213  			     struct virtio_gpu_object **bo_ptr,
530b28426a94b8 Gerd Hoffmann   2019-03-18  214  			     struct virtio_gpu_fence *fence)
dc5698e80cf724 Dave Airlie     2013-09-09  215  {
e2324300f427ff Gerd Hoffmann   2019-08-29  216  	struct virtio_gpu_object_array *objs = NULL;
c66df701e783bc Gerd Hoffmann   2019-08-29  217  	struct drm_gem_shmem_object *shmem_obj;
dc5698e80cf724 Dave Airlie     2013-09-09  218  	struct virtio_gpu_object *bo;
2f2aa13724d568 Gerd Hoffmann   2020-02-07  219  	struct virtio_gpu_mem_entry *ents;
2f2aa13724d568 Gerd Hoffmann   2020-02-07  220  	unsigned int nents;
dc5698e80cf724 Dave Airlie     2013-09-09  221  	int ret;
dc5698e80cf724 Dave Airlie     2013-09-09  222  
dc5698e80cf724 Dave Airlie     2013-09-09  223  	*bo_ptr = NULL;
dc5698e80cf724 Dave Airlie     2013-09-09  224  
c66df701e783bc Gerd Hoffmann   2019-08-29  225  	params->size = roundup(params->size, PAGE_SIZE);
c66df701e783bc Gerd Hoffmann   2019-08-29  226  	shmem_obj = drm_gem_shmem_create(vgdev->ddev, params->size);
c66df701e783bc Gerd Hoffmann   2019-08-29  227  	if (IS_ERR(shmem_obj))
c66df701e783bc Gerd Hoffmann   2019-08-29  228  		return PTR_ERR(shmem_obj);
c66df701e783bc Gerd Hoffmann   2019-08-29  229  	bo = gem_to_virtio_gpu_obj(&shmem_obj->base);
dc5698e80cf724 Dave Airlie     2013-09-09  230  
556c62e85f9b97 Matthew Wilcox  2018-10-30  231  	ret = virtio_gpu_resource_id_get(vgdev, &bo->hw_res_handle);
e2324300f427ff Gerd Hoffmann   2019-08-29  232  	if (ret < 0)
e2324300f427ff Gerd Hoffmann   2019-08-29  233  		goto err_free_gem;
e2324300f427ff Gerd Hoffmann   2019-08-29  234  
530b28426a94b8 Gerd Hoffmann   2019-03-18  235  	bo->dumb = params->dumb;
530b28426a94b8 Gerd Hoffmann   2019-03-18  236  
e2324300f427ff Gerd Hoffmann   2019-08-29  237  	if (fence) {
e2324300f427ff Gerd Hoffmann   2019-08-29  238  		ret = -ENOMEM;
e2324300f427ff Gerd Hoffmann   2019-08-29  239  		objs = virtio_gpu_array_alloc(1);
e2324300f427ff Gerd Hoffmann   2019-08-29  240  		if (!objs)
e2324300f427ff Gerd Hoffmann   2019-08-29  241  			goto err_put_id;
c66df701e783bc Gerd Hoffmann   2019-08-29  242  		virtio_gpu_array_add_obj(objs, &bo->base.base);
e2324300f427ff Gerd Hoffmann   2019-08-29  243  
e2324300f427ff Gerd Hoffmann   2019-08-29  244  		ret = virtio_gpu_array_lock_resv(objs);
e2324300f427ff Gerd Hoffmann   2019-08-29  245  		if (ret != 0)
e2324300f427ff Gerd Hoffmann   2019-08-29  246  			goto err_put_objs;
e2324300f427ff Gerd Hoffmann   2019-08-29  247  	}
e2324300f427ff Gerd Hoffmann   2019-08-29  248  
897b4d1acaf563 Gerd Hoffmann   2020-09-23  249  	if (params->blob) {
3389082bb98296 Vivek Kasireddy 2021-04-12  250  		if (params->blob_mem == VIRTGPU_BLOB_MEM_GUEST)
3389082bb98296 Vivek Kasireddy 2021-04-12  251  			bo->guest_blob = true;
3389082bb98296 Vivek Kasireddy 2021-04-12  252  
897b4d1acaf563 Gerd Hoffmann   2020-09-23  253  		virtio_gpu_cmd_resource_create_blob(vgdev, bo, params,
897b4d1acaf563 Gerd Hoffmann   2020-09-23 @254  						    ents, nents);
897b4d1acaf563 Gerd Hoffmann   2020-09-23  255  	} else if (params->virgl) {
30172efbfb842c Gurchetan Singh 2020-09-23  256  		virtio_gpu_cmd_resource_create_3d(vgdev, bo, params,
30172efbfb842c Gurchetan Singh 2020-09-23  257  						  objs, fence);
30172efbfb842c Gurchetan Singh 2020-09-23  258  	} else {
1795d2fd78a334 mwezdeck        2021-11-02  259  		ret = virtio_gpu_object_shmem_init(vgdev, bo, &ents, &nents);
1795d2fd78a334 mwezdeck        2021-11-02  260  		if (ret != 0) {
1795d2fd78a334 mwezdeck        2021-11-02  261  			virtio_gpu_array_put_free(objs);
1795d2fd78a334 mwezdeck        2021-11-02  262  			virtio_gpu_free_object(&shmem_obj->base);
1795d2fd78a334 mwezdeck        2021-11-02  263  			return ret;
1795d2fd78a334 mwezdeck        2021-11-02  264  		}
30172efbfb842c Gurchetan Singh 2020-09-23  265  		virtio_gpu_cmd_create_resource(vgdev, bo, params,
30172efbfb842c Gurchetan Singh 2020-09-23  266  					       objs, fence);
c76d4ab764adae Gurchetan Singh 2020-04-01  267  		virtio_gpu_object_attach(vgdev, bo, ents, nents);
30172efbfb842c Gurchetan Singh 2020-09-23  268  	}
dc5698e80cf724 Dave Airlie     2013-09-09  269  
dc5698e80cf724 Dave Airlie     2013-09-09  270  	*bo_ptr = bo;
dc5698e80cf724 Dave Airlie     2013-09-09  271  	return 0;
e2324300f427ff Gerd Hoffmann   2019-08-29  272  
e2324300f427ff Gerd Hoffmann   2019-08-29  273  err_put_objs:
e2324300f427ff Gerd Hoffmann   2019-08-29  274  	virtio_gpu_array_put_free(objs);
e2324300f427ff Gerd Hoffmann   2019-08-29  275  err_put_id:
e2324300f427ff Gerd Hoffmann   2019-08-29  276  	virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
e2324300f427ff Gerd Hoffmann   2019-08-29  277  err_free_gem:
c66df701e783bc Gerd Hoffmann   2019-08-29  278  	drm_gem_shmem_free_object(&shmem_obj->base);
e2324300f427ff Gerd Hoffmann   2019-08-29  279  	return ret;
dc5698e80cf724 Dave Airlie     2013-09-09  280  }
1795d2fd78a334 mwezdeck        2021-11-02  281  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org