drm/gud: Free buffers on device removal
Subsystems (3)
PATCH v2 1/2 drm/gud: Free buffers on device removal
Free transfer and compression buffers on device removal instead of at DRM device removal time. This ensures that the usual 2x8MB buffers are released when the device is unplugged and not kept around should userspace keep the DRM device fd open.
At least Ubuntu 20.04 doesn't release the DRM device on unplug.
The damage_lock mutex is not destroyed because it is used outside the drm_dev_enter/exit block in gud_pipe_update(). AFAICT it's possible for an open fbdev descriptor to trigger a commit after the USB device is gone.
v2: Don't destroy damage_lock
--- drivers/gpu/drm/gud/gud_drv.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/gpu/drm/gud/gud_drv.c +5 -4
@@ -394,14 +394,15 @@ static const struct drm_driver gud_drm_driver = { .minor = 0, }; -static void gud_free_buffers_and_mutex (struct drm_device *drm,void *unused) +static void gud_free_buffers_and_mutex (void *data) { -struct gud_device *gdrm =to_gud_device (drm); +struct gud_device *gdrm = data;vfree (gdrm->compress_buf); + gdrm->compress_buf =NULL ;kfree (gdrm->bulk_buf); + gdrm->bulk_buf =NULL ;mutex_destroy (&gdrm->ctrl_lock); -mutex_destroy (&gdrm->damage_lock); }static int gud_probe (struct usb_interface *intf,const struct usb_device_id *id)
@@ -455,7 +456,7 @@ static int gud_probe(struct usb_interface *intf, const struct usb_device_id *id)INIT_WORK (&gdrm->work, gud_flush_work);gud_clear_damage (gdrm); - ret =drmm_add_action_or_reset (drm, gud_free_buffers_and_mutex,NULL ); + ret =devm_add_action (dev, gud_free_buffers_and_mutex, gdrm);if (ret)return ret;
Signature · 1 line
2.23.0
PATCH v2 2/2 drm/gud: Use scatter-gather USB bulk transfer
There'a limit to how big a kmalloc buffer can be, and as memory gets fragmented it becomes more difficult to get big buffers. The downside of smaller buffers is that the driver has to split the transfer up which hampers performance. Compression might also take a hit because of the splitting.
Solve this by allocating the transfer buffer using vmalloc and create a SG table to be passed on to the USB subsystem. vmalloc_32() is used to avoid DMA bounce buffers on USB controllers that can only access 32-bit addresses.
This also solves the problem that split transfers can give host side tearing since flushing is decoupled from rendering.
usb_sg_wait() doesn't have timeout handling builtin, so it is wrapped in a timer like 4 out of 6 users in the kernel have done.
v2:
- Use DIV_ROUND_UP (Linus)
- Add timeout note to the commit log (Linus)
- Expand note about upper buffer limit (Linus)
- Change var name s/timer/ctx/ in gud_usb_bulk_timeout()
--- drivers/gpu/drm/gud/gud_drv.c | 50 +++++++++++++++++++++--------- drivers/gpu/drm/gud/gud_internal.h | 2 ++ drivers/gpu/drm/gud/gud_pipe.c | 47 ++++++++++++++++++++++++---- 3 files changed, 78 insertions(+), 21 deletions(-)
drivers/gpu/drm/gud/gud_drv.c +35 -15
@@ -394,13 +394,40 @@ static const struct drm_driver gud_drm_driver = { .minor = 0, }; +static int gud_alloc_bulk_buffer (struct gud_device *gdrm) +{ +unsigned int i, num_pages; +struct page **pages; +void *ptr; +int ret; + + gdrm->bulk_buf =vmalloc_32 (gdrm->bulk_len); +if (!gdrm->bulk_buf) +return -ENOMEM ; + + num_pages =DIV_ROUND_UP (gdrm->bulk_len,PAGE_SIZE ); + pages =kmalloc_array (num_pages,sizeof (struct page *),GFP_KERNEL ); +if (!pages) +return -ENOMEM ; + +for (i =0 , ptr = gdrm->bulk_buf; i < num_pages; i++, ptr +=PAGE_SIZE ) + pages[i] =vmalloc_to_page (ptr); + + ret =sg_alloc_table_from_pages (&gdrm->bulk_sgt, pages, num_pages, +0 , gdrm->bulk_len,GFP_KERNEL ); +kfree (pages); + +return ret; +} +static void gud_free_buffers_and_mutex (void *data) {struct gud_device *gdrm = data;vfree (gdrm->compress_buf); gdrm->compress_buf =NULL ; -kfree (gdrm->bulk_buf); +sg_free_table (&gdrm->bulk_sgt); +vfree (gdrm->bulk_buf); gdrm->bulk_buf =NULL ;mutex_destroy (&gdrm->ctrl_lock); }
@@ -537,24 +564,17 @@ static int gud_probe(struct usb_interface *intf, const struct usb_device_id *id)if (desc.max_buffer_size) max_buffer_size =le32_to_cpu (desc.max_buffer_size); -retry: -/* -* Use plain kmalloc here since devm_kmalloc() places struct devres at the beginning -* of the buffer it allocates. This wastes a lot of memory when allocating big buffers. -* Asking for 2M would actually allocate 4M. This would also prevent getting the biggest -* possible buffer potentially leading to split transfers. -*/ - gdrm->bulk_buf =kmalloc (max_buffer_size,GFP_KERNEL | __GFP_NOWARN); -if (!gdrm->bulk_buf) { - max_buffer_size =roundup_pow_of_two (max_buffer_size) /2 ; -if (max_buffer_size <SZ_512K ) -return -ENOMEM ; - goto retry; - } +/* Prevent a misbehaving device from allocating loads of RAM. 4096x4096@XRGB8888 = 64 MB */ +if (max_buffer_size >SZ_64M ) + max_buffer_size =SZ_64M ; gdrm->bulk_pipe =usb_sndbulkpipe (interface_to_usbdev (intf),usb_endpoint_num (bulk_out)); gdrm->bulk_len = max_buffer_size; + ret =gud_alloc_bulk_buffer (gdrm); +if (ret) +return ret; +if (gdrm->compression &GUD_COMPRESSION_LZ4 ) { gdrm->lz4_comp_mem =devm_kmalloc (dev,LZ4_MEM_COMPRESS ,GFP_KERNEL );if (!gdrm->lz4_comp_mem)
drivers/gpu/drm/gud/gud_internal.h +2 -0
@@ -5,6 +5,7 @@#include <linux/list.h> #include <linux/mutex.h> +#include <linux/scatterlist.h> #include <linux/usb.h> #include <linux/workqueue.h> #include <uapi/drm/drm_fourcc.h>
@@ -26,6 +27,7 @@ struct gud_device {unsigned int bulk_pipe;void *bulk_buf;size_t bulk_len; +struct sg_table bulk_sgt;u8 compression;void *lz4_comp_mem;
drivers/gpu/drm/gud/gud_pipe.c +41 -6
64 lines not on this page · raw message
Signature · 1 line
2.23.0