dma-buf/udmabuf: Introduce CONFIG_UDMABUF_SIZE_LIMIT_MBYTES
RFC PATCH 1/2 dma-buf/udmabuf: Introduce CONFIG_UDMABUF_SIZE_LIMIT_MBYTES
As udmabuf increasingly enjoys popularity - being used in projects like libcamera, Gstreamer, Mesa and KWin - users more frequently encounter cases where the current default size limit of 64MB is too low. Examples include allocating video buffers at a 8K resolution - and even 4K is affected when using non-subsampled video formats or high bit depths.
While the limit can already be changed via the kernel command line, exposing it as a kernel config makes that easier and more discoverable for distros. Thus let's do that.
--- drivers/dma-buf/Kconfig | 6 ++++++ drivers/dma-buf/udmabuf.c | 4 ++++ 2 files changed, 10 insertions(+)
drivers/dma-buf/Kconfig +6 -0
@@ -40,6 +40,12 @@ config UDMABUF A driver to let userspace turn memfd regions into dma-bufs. Qemu can use this to create host dmabufs for guest framebuffers. +config UDMABUF_SIZE_LIMIT_MBYTES + int "Size limit in Mega Bytes" + default 64 + help + Maximum size of a udmabuf, in megabytes. Default is 64. + config DMABUF_DEBUG bool "DMA-BUF debug checks" depends on DMA_SHARED_BUFFER
drivers/dma-buf/udmabuf.c +4 -0
@@ -20,7 +20,11 @@ static int list_limit = 1024;module_param (list_limit, int,0644 );MODULE_PARM_DESC (list_limit,"udmabuf_create_list->count limit. Default is 1024." ); +#ifdef CONFIG_UDMABUF_SIZE_LIMIT_MBYTES +static int size_limit_mb =CONFIG_UDMABUF_SIZE_LIMIT_MBYTES ; +#else static int size_limit_mb =64 ; +#endif module_param (size_limit_mb, int,0644 );MODULE_PARM_DESC (size_limit_mb,"Max size of a dmabuf, in megabytes. Default is 64." );
Signature · 1 line
2.55.0
RFC PATCH 2/2 dma-buf/udmabuf: Increase default size limit to 256MB
While there does not seem to be a particularly strong reason to have a size limit for udmabufs in the first place, raising the limit moderatly to 256MB is enough to avoid hitting the limit in increasingly common use-cases - notably allocating video buffers for 8K resolutions.
--- drivers/dma-buf/Kconfig | 4 ++-- drivers/dma-buf/udmabuf.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/dma-buf/Kconfig +2 -2
@@ -42,9 +42,9 @@ config UDMABUF config UDMABUF_SIZE_LIMIT_MBYTES int "Size limit in Mega Bytes" - default 64 + default 256 help - Maximum size of a udmabuf, in megabytes. Default is 64. + Maximum size of a udmabuf, in megabytes. Default is 256. config DMABUF_DEBUG bool "DMA-BUF debug checks"
drivers/dma-buf/udmabuf.c +2 -2
@@ -23,10 +23,10 @@ MODULE_PARM_DESC(list_limit, "udmabuf_create_list->count limit. Default is 1024.#ifdef CONFIG_UDMABUF_SIZE_LIMIT_MBYTES static int size_limit_mb =CONFIG_UDMABUF_SIZE_LIMIT_MBYTES ;#else -static int size_limit_mb =64 ; +static int size_limit_mb =256 ;#endif module_param (size_limit_mb, int,0644 ); -MODULE_PARM_DESC (size_limit_mb,"Max size of a dmabuf, in megabytes. Default is 64." ); +MODULE_PARM_DESC (size_limit_mb,"Max size of a dmabuf, in megabytes. Default is 256." );struct udmabuf {pgoff_t pagecount;
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Low] CONFIG_UDMABUF_SIZE_LIMIT_MBYTES lacks a dependency on CONFIG_UDMABUF.
- [Low] Missing range constraint on UDMABUF_SIZE_LIMIT_MBYTES allows negative values, bypassing size validation.
Pre-existing issues:
- [High] Concurrent calls to DMA_BUF_IOCTL_SYNC trigger a race condition in begin_cpu_udmabuf, leading to a memory leak. --
--- Patch [1]: [RFC PATCH 1/2] dma-buf/udmabuf: Introduce CONFIG_UDMABUF_SIZE_LIMIT_MBYTES --- commit 258354f822e15700d93d45826a3e51b308dd35b8
Author: Robert Mader <[email protected]>
dma-buf/udmabuf: Introduce CONFIG_UDMABUF_SIZE_LIMIT_MBYTES
This patch introduces a new Kconfig option, CONFIG_UDMABUF_SIZE_LIMIT_MBYTES, to allow distributions to configure the maximum size limit for udmabufs at compile time. The limit defaults to 64MB and was previously only configurable via the kernel command line.
drivers/dma-buf/Kconfig config UDMABUF · line 45
Quoted 11 lines
drivers/dma-buf/Kconfig +3
-0@@ -40,6 +40,12 @@ config UDMABUF A driver to let userspace turn memfd regions into dma-bufs. Qemu can use this to create host dmabufs for guest framebuffers. +config UDMABUF_SIZE_LIMIT_MBYTES + int "Size limit in Mega Bytes" + default 64
[Severity: Low]
Should this config have a 'depends on UDMABUF' directive?
Without it, users might be prompted to configure the size limit even when the UDMABUF driver itself is disabled, polluting the global config.
[Severity: Low]
Could a user configure a negative value here and bypass the size limits?
If this is set to a negative value, it appears it will wrap around to a massive value when cast to u64 in udmabuf_create():
drivers/dma-buf/udmabuf.c:udmabuf_create() {
...
pglimit = ((u64)size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
...
}
Would it be safer to add a range constraint to the Kconfig definition?
Quoted 6 lines
drivers/dma-buf/Kconfig +3
-0+ help + Maximum size of a udmabuf, in megabytes. Default is 64. + config DMABUF_DEBUG bool "DMA-BUF debug checks" depends on DMA_SHARED_BUFFER
[ ... ]
Quoted 6 lines
drivers/dma-buf/udmabuf.c +0
-0@@ -20,7 +20,11 @@ static int list_limit = 1024;module_param (list_limit, int,0644 );
drivers/dma-buf/udmabuf.c static int list_limit = 1024; · line 30
Quoted 10 lines
drivers/dma-buf/udmabuf.c +4
-0MODULE_PARM_DESC (list_limit,"udmabuf_create_list->count limit. Default is 1024." ); +#ifdef CONFIG_UDMABUF_SIZE_LIMIT_MBYTES +static int size_limit_mb =CONFIG_UDMABUF_SIZE_LIMIT_MBYTES ; +#else static int size_limit_mb =64 ; +#endif module_param (size_limit_mb, int,0644 );MODULE_PARM_DESC (size_limit_mb,"Max size of a dmabuf, in megabytes. Default is 64." );
[Severity: High]
This is a pre-existing issue, but can concurrent calls to DMA_BUF_IOCTL_SYNC cause a memory leak?
Looking at begin_cpu_udmabuf(), the scatter-gather table pointer is checked and assigned without locks:
drivers/dma-buf/udmabuf.c:begin_cpu_udmabuf() {
...
if (!ubuf->sg) {
ubuf->sg = get_sg_table(dev, buf, direction);...
}
If multiple threads invoke the ioctl simultaneously, could they bypass the check, allocate multiple tables, and overwrite ubuf->sg, permanently leaking the previously allocated tables and mappings?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Christian König
maintainerQuoted 9 lines · Robert Mader
As udmabuf increasingly enjoys popularity - being used in projects like libcamera, Gstreamer, Mesa and KWin - users more frequently encounter cases where the current default size limit of 64MB is too low. Examples include allocating video buffers at a 8K resolution - and even 4K is affected when using non-subsampled video formats or high bit depths.
While the limit can already be changed via the kernel command line, exposing it as a kernel config makes that easier and more discoverable for distros. Thus let's do that.
Well config options are usually only useful if the value can't be changed on runtime through a module parameter, but that is clearly not the case here.
On the other hand I do see your problem. I would just vote to disable the limit by default, there is nothing preventing userspace from allocating multiple uDMA-bufs so it doesn't seem to prevent any security issue or similar.
Regards,
Christian.
Quoted 43 lines
Signed-off-by Robert Mader <[email protected]>--- drivers/dma-buf/Kconfig | 6 ++++++ drivers/dma-buf/udmabuf.c | 4 ++++ 2 files changed, 10 insertions(+)drivers/dma-buf/Kconfig +6
-0@@ -40,6 +40,12 @@ config UDMABUF A driver to let userspace turn memfd regions into dma-bufs. Qemu can use this to create host dmabufs for guest framebuffers. +config UDMABUF_SIZE_LIMIT_MBYTES + int "Size limit in Mega Bytes" + default 64 + help + Maximum size of a udmabuf, in megabytes. Default is 64. + config DMABUF_DEBUG bool "DMA-BUF debug checks" depends on DMA_SHARED_BUFFERdrivers/dma-buf/udmabuf.c +4
-1@@ -20,7 +20,11 @@ static int list_limit = 1024;module_param (list_limit, int,0644 );MODULE_PARM_DESC (list_limit,"udmabuf_create_list->count limit. Default is 1024." ); +#ifdef CONFIG_UDMABUF_SIZE_LIMIT_MBYTES +static int size_limit_mb =CONFIG_UDMABUF_SIZE_LIMIT_MBYTES ; +#else static int size_limit_mb =64 ; +#endif module_param (size_limit_mb, int,0644 );MODULE_PARM_DESC (size_limit_mb,"Max size of a dmabuf, in megabytes. Default is 64." ); --2.55.0
Hi,
Quoted 7 lines · Christian König
Quoted 6 lines · Robert Mader
As udmabuf increasingly enjoys popularity - being used in projects like libcamera, Gstreamer, Mesa and KWin - users more frequently encounter cases where the current default size limit of 64MB is too low. Examples include allocating video buffers at a 8K resolution - and even 4K is affected when using non-subsampled video formats or high bit depths.
Quoted 6 lines
Quoted 3 lines
While the limit can already be changed via the kernel command line, exposing it as a kernel config makes that easier and more discoverable for distros. Thus let's do that.
Well config options are usually only useful if the value can't be changed on runtime through a module parameter, but that is clearly not the case here.
On the other hand I do see your problem. I would just vote to disable the limit by default, there is nothing preventing userspace from allocating multiple uDMA-bufs so it doesn't seem to prevent any security issue or similar.
I fully agree and would prefer that as well. If there is no resistance / concerns from anybody I'll send a corresponding patch at the end of the week.
Thanks,
Robert
Quoted 47 lines
Regards,
Christian.Quoted 42 lines
Signed-off-by Robert Mader <[email protected]>--- drivers/dma-buf/Kconfig | 6 ++++++ drivers/dma-buf/udmabuf.c | 4 ++++ 2 files changed, 10 insertions(+)drivers/dma-buf/Kconfig +6
-0@@ -40,6 +40,12 @@ config UDMABUF A driver to let userspace turn memfd regions into dma-bufs. Qemu can use this to create host dmabufs for guest framebuffers. +config UDMABUF_SIZE_LIMIT_MBYTES + int "Size limit in Mega Bytes" + default 64 + help + Maximum size of a udmabuf, in megabytes. Default is 64. + config DMABUF_DEBUG bool "DMA-BUF debug checks" depends on DMA_SHARED_BUFFERdrivers/dma-buf/udmabuf.c +4
-1@@ -20,7 +20,11 @@ static int list_limit = 1024;module_param (list_limit, int,0644 );MODULE_PARM_DESC (list_limit,"udmabuf_create_list->count limit. Default is 1024." ); +#ifdef CONFIG_UDMABUF_SIZE_LIMIT_MBYTES +static int size_limit_mb =CONFIG_UDMABUF_SIZE_LIMIT_MBYTES ; +#else static int size_limit_mb =64 ; +#endif module_param (size_limit_mb, int,0644 );MODULE_PARM_DESC (size_limit_mb,"Max size of a dmabuf, in megabytes. Default is 64." ); --2.55.0
Kasireddy, Vivek
maintainerHi Robert,
Quoted 25 lines
Subject: Re: [RFC PATCH 1/2] dma-buf/udmabuf: Introduce
CONFIG_UDMABUF_SIZE_LIMIT_MBYTESHi,
Quoted 2 lines · Christian König
Quoted 1 line · Robert Mader
As udmabuf increasingly enjoys popularity - being used in projects
like
Quoted 1 line
Quoted 1 line
libcamera, Gstreamer, Mesa and KWin - users more frequently
encounter
Quoted 1 line
Quoted 1 line
cases where the current default size limit of 64MB is too low.
Examples
Quoted 2 lines
Quoted 2 lines
include allocating video buffers at a 8K resolution - and even 4K is affected when using non-subsampled video formats or high bit
depths.
Quoted 3 lines
Quoted 3 lines
While the limit can already be changed via the kernel command line, exposing it as a kernel config makes that easier and more
discoverable
Quoted 2 lines
Quoted 1 line
for distros. Thus let's do that.
Well config options are usually only useful if the value can't be
changed on runtime through a module parameter, but that is clearly not the case here.
Quoted 1 line
Quoted 8 lines
Quoted 1 line
On the other hand I do see your problem. I would just vote to disable
the limit by default, there is nothing preventing userspace from allocating multiple uDMA-bufs so it doesn't seem to prevent any security issue or similar.
I fully agree and would prefer that as well. If there is no resistance / concerns from anybody I'll send a corresponding patch at the end of the week.
AFAIU, the main motivation behind the limits was to ensure that there are some kind of guardrails. I am not opposed to their removal but I think it would now allow users to pin large arbitrary amounts of memory and I feel like there needs to be a way to prevent users from doing that.
Early RFC versions of udmabuf had mlock accounting to address this issue but not sure why it was dropped eventually. I am wondering if it makes sense to bring that back.
Thanks,
Vivek
Quoted 55 lines
Thanks,
Robert
Quoted 11 lines
Regards,
Christian.Quoted 6 lines
Signed-off-by Robert Mader <[email protected]>--- drivers/dma-buf/Kconfig | 6 ++++++ drivers/dma-buf/udmabuf.c | 4 ++++ 2 files changed, 10 insertions(+)Quoted 7 lines
drivers/dma-buf/Kconfig config UDMABUF · line 41
Quoted 7 lines
drivers/dma-buf/Kconfig +0
-0@@ -40,6 +40,12 @@ config UDMABUF A driver to let userspace turn memfd regions into dma-bufs. Qemu can use this to create host dmabufs for guestframebuffers.
Quoted 17 lines
Quoted 15 lines
drivers/dma-buf/Kconfig +6
-0+config UDMABUF_SIZE_LIMIT_MBYTES + int "Size limit in Mega Bytes" + default 64 + help + Maximum size of a udmabuf, in megabytes. Default is 64. + config DMABUF_DEBUG bool "DMA-BUF debug checks" depends on DMA_SHARED_BUFFERdrivers/dma-buf/udmabuf.c +0
-0drivers/dma-buf/udmabuf.c static int list_limit = 1024; · line 21
Quoted 2 lines
drivers/dma-buf/udmabuf.c +0
-0@@ -20,7 +20,11 @@ static int list_limit = 1024;module_param (list_limit, int,0644 ); MODULE_PARM_DESC(list_limit, "udmabuf_create_list->count limit.Default is 1024.");
Quoted 8 lines
drivers/dma-buf/udmabuf.c static int list_limit = 1024;
Quoted 8 lines
drivers/dma-buf/udmabuf.c +4
-0+#ifdef CONFIG_UDMABUF_SIZE_LIMIT_MBYTES +static int size_limit_mb =CONFIG_UDMABUF_SIZE_LIMIT_MBYTES ; +#else static int size_limit_mb =64 ; +#endif module_param (size_limit_mb, int,0644 ); MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, inmegabytes. Default is 64.");
Quoted 4 lines
Quoted 4 lines
drivers/dma-buf/udmabuf.c +0
-1--2.55.0
Hi Vivek,
Quoted 33 lines · Vivek
Hi Robert,
Quoted 23 lines
Subject: Re: [RFC PATCH 1/2] dma-buf/udmabuf: Introduce
CONFIG_UDMABUF_SIZE_LIMIT_MBYTESHi,
Quoted 2 lines · Christian König
Quoted 1 line · Robert Mader
As udmabuf increasingly enjoys popularity - being used in projects
like
Quoted 1 line
Quoted 1 line
libcamera, Gstreamer, Mesa and KWin - users more frequently
encounter
Quoted 1 line
Quoted 1 line
cases where the current default size limit of 64MB is too low.
Examples
Quoted 2 lines
Quoted 2 lines
include allocating video buffers at a 8K resolution - and even 4K is affected when using non-subsampled video formats or high bit
depths.
Quoted 2 lines
Quoted 2 lines
While the limit can already be changed via the kernel command line, exposing it as a kernel config makes that easier and more
discoverable
Quoted 2 lines
Quoted 1 line
for distros. Thus let's do that.
Well config options are usually only useful if the value can't be
changed on runtime through a module parameter, but that is clearly not the case here.
Quoted 8 lines
Quoted 1 line
On the other hand I do see your problem. I would just vote to disable
the limit by default, there is nothing preventing userspace from allocating multiple uDMA-bufs so it doesn't seem to prevent any security issue or similar.
I fully agree and would prefer that as well. If there is no resistance / concerns from anybody I'll send a corresponding patch at the end of the week.
Quoted 8 lines
AFAIU, the main motivation behind the limits was to ensure that there are some kind of guardrails. I am not opposed to their removal but I think it would now allow users to pin large arbitrary amounts of memory and I feel like there needs to be a way to prevent users from doing that.
Early RFC versions of udmabuf had mlock accounting to address this issue but not sure why it was dropped eventually. I am wondering if it makes sense to bring that back.
while the point about pinned memory accounting sounds reasonable to me, I don't think that removing the limit for individual uDMA-bufs would change anything about the current situation. As Christian already pointed out there's nothing preventing clients to allocate multiple uDMA-bufs atm. In fact I believe that the scenario you describe currently is more likely to be caused by clients allocating (and e.g. leaking) many small uDMA-bufs.
There are, however, some other existing guardrails that may somewhat help already:
1. systemd limits udmabuf access to uaccess - i.e. local seats / graphical users (https://github.com/systemd/systemd/pull/33738)
2. at least the Flatpak sandbox binds udmabuf access to DRI device access as the later is considered "more powerful"
(https://github.com/flatpak/flatpak/pull/6158)
I'll therefor go forward and a propose a patch to remove the individual limit, assuming the discussion about mlock (or some kind of more global) accounting can be continued independently.
Thanks,
Robert
Quoted 54 lines
Thanks,
VivekQuoted 50 lines
Thanks,
Robert
Quoted 10 lines
Regards,
Christian.Quoted 6 lines
Signed-off-by Robert Mader<[email protected]>--- drivers/dma-buf/Kconfig | 6 ++++++ drivers/dma-buf/udmabuf.c | 4 ++++ 2 files changed, 10 insertions(+)Quoted 7 lines
drivers/dma-buf/Kconfig config UDMABUF · line 41
Quoted 7 lines
drivers/dma-buf/Kconfig +0
-0@@ -40,6 +40,12 @@ config UDMABUF A driver to let userspace turn memfd regions into dma-bufs. Qemu can use this to create host dmabufs for guestframebuffers.
Quoted 8 lines
Quoted 8 lines
drivers/dma-buf/Kconfig +6
-0+config UDMABUF_SIZE_LIMIT_MBYTES + int "Size limit in Mega Bytes" + default 64 + help + Maximum size of a udmabuf, in megabytes. Default is 64. + config DMABUF_DEBUG bool "DMA-BUF debug checks"Quoted 8 lines
drivers/dma-buf/udmabuf.c static int list_limit = 1024; · line 21
Quoted 8 lines
drivers/dma-buf/Kconfig +0
-0depends on DMA_SHARED_BUFFERdrivers/dma-buf/udmabuf.c +0
-0@@ -20,7 +20,11 @@ static int list_limit = 1024;module_param (list_limit, int,0644 ); MODULE_PARM_DESC(list_limit, "udmabuf_create_list->count limit.Default is 1024.");
Quoted 7 lines
drivers/dma-buf/udmabuf.c static int list_limit = 1024;
Quoted 7 lines
drivers/dma-buf/udmabuf.c +4
-0+#ifdef CONFIG_UDMABUF_SIZE_LIMIT_MBYTES +static int size_limit_mb =CONFIG_UDMABUF_SIZE_LIMIT_MBYTES ; +#else static int size_limit_mb =64 ; +#endif module_param (size_limit_mb, int,0644 ); MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, inmegabytes. Default is 64.");
Quoted 3 lines
Quoted 3 lines
drivers/dma-buf/udmabuf.c +0
-1--2.55.0
Signature · 6 lines
Robert Mader Consultant Software Developer Collabora Ltd. Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK Registered in England & Wales, no. 5513718