dma-buf: warn-only mode for DMABUF_DEBUG
Subsystems (5)
cover dma-buf: warn-only mode for DMABUF_DEBUG
With DMABUF_DEBUG, dma_buf_map_attachment() gives the importer a copy of the exporter's sg_table without the struct pages and offsets, and without the lengths where sg_dma_len() is a separate field. An importer that uses those fields stops working. That is the point of the option, but the failure usually shows up somewhere else and says nothing about the cause.
Commit 143755bdabaa9 ("dma-buf: Make DMABUF_DEBUG default to y on DEBUG_KERNEL kernels"), which I wrote, went into v7.3-rc4. It made the default from commit 646013f513f3 ("dma-buf: enable DMABUF_DEBUG by default on DEBUG kernels") take effect, and distribution configs usually have DEBUG_KERNEL=y. Two reports followed:
- Jianfeng Liu: hardware video decode with drm/msm ends in GPU translation faults [1]. He then sent a revert [2].
- Diederik de Haas: rockchip fails to import buffers when playing video, on a config based on Debian's [3].
In [2] Rob Clark said the problem goes beyond msm and cannot be fixed quickly, and Christian König offered to set the default to N for another few months [4]. I listed more importers that look affected and mentioned this RFC in [5]. As of v7.3-rc6 the default is unchanged.
This series adds DMABUF_DEBUG_WARN as a sub-option of DMABUF_DEBUG. The importer still gets a copy, but one that keeps the CPU side of the exporter's table. The entries are marked with a new bit in dma_flags. sg_page(), sg_nents_for_len() and sg_split() print a rate limited message with a stack trace when they see the bit. These CPU-side accesses can continue after the report instead of failing because the fields were cleared. The report is not a WARN(), so it does not taint and does not trigger panic_on_warn. Strict mode remains the default and continues to remove the CPU-side fields.
Known limits:
- Only access through sg_page(), sg_nents_for_len() and sg_split() is seen. sg_page() covers sg_phys(), sg_virt() and the page iterators. An importer that reads sg->length or sg->offset directly is not noticed.
- When the option is enabled, every sg_page() tests the flag.
- It selects NEED_SG_DMA_FLAGS, which adds dma_flags and may increase the size of struct scatterlist. It also relies on dma_flags being initialised, as the DMA mapping code already does.
- It finds importers. It does not fix them.
Question for the maintainers: could warn mode be what DEBUG_KERNEL kernels get by default, with strict mode kept for CI? Or is an opt-in sub-option all that is wanted, if anything? The series does not change any default.
Testing, on the commits as posted:
- KUnit, the dma-buf suites, under UML and on x86_64 in QEMU, in strict and in warn mode: 56 passed, 1 skipped (it needs 2 CPUs) each time.
- Builds: x86_64, arm64 and ARM926 with DMABUF_DEBUG_WARN=y, ARM926 without DMABUF_DEBUG. Without DMABUF_DEBUG the generated code of dma-buf.o and lib/scatterlist.o is the same as before, except for line numbers.
- IIO DMABUF capture on a Zynq in QEMU, with local device models. The IIO dmaengine buffer calls sg_nents_for_len() on the attachment's table. In strict mode with NEED_SG_DMA_LENGTH the capture fails with -EBUSY. In warn mode it works, the kernel is not tainted, and the log has:
DMA-BUF: importer used the CPU side of an exporter's sg_table
CPU: 0 UID: 0 PID: 48 Comm: iio-dmabuf Not tainted 7.3.0-rc4+ #2 VOLUNTARY Call trace: [...] dump_stack_lvl from sg_nents_for_len+0xd8/0xe4 sg_nents_for_len from iio_dmaengine_buffer_submit_block+0x4c/0x33c iio_dmaengine_buffer_submit_block from iio_dma_buffer_submit_block.part.0+0x5c/0x104 iio_dma_buffer_submit_block.part.0 from iio_dma_buffer_enqueue_dmabuf+0x68/0xa0 iio_dma_buffer_enqueue_dmabuf from iio_buffer_chrdev_ioctl+0x520/0x9a4 iio_buffer_chrdev_ioctl from sys_ioctl+0x460/0x914
- sur40 behind xHCI and intel-iommu on x86_64 in QEMU, with a one-line test-only change in sur40. Here sg_page() is called in iommu_dma_map_sg(), and the trace leads back to sur40_poll(). The capture itself did not finish in that setup.
Details are in the notes on the patches. Not tested on hardware.
Based on v7.3-rc4-70-gfe2ec83746e5.
An LLM agent helped with the code and the testing.
[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]
[3] https://lists.freedesktop.org/archives/dri-devel/2026-September/600904.html
[4] https://lore.kernel.org/r/[email protected]
[5] https://lore.kernel.org/r/[email protected]
Karl Mehltretter (3):
dma-buf: keep the DMA flags in the DMABUF_DEBUG copy
dma-buf: add a warn-only mode to DMABUF_DEBUG
dma-buf: test the debug scatterlist wrapper
drivers/dma-buf/.kunitconfig | 1 + drivers/dma-buf/Kconfig | 23 ++++ drivers/dma-buf/Makefile | 1 + drivers/dma-buf/dma-buf.c | 48 ++++++- drivers/dma-buf/st-dma-buf.c | 253 +++++++++++++++++++++++++++++++++++ include/linux/scatterlist.h | 26 +++- lib/scatterlist.c | 22 +++ lib/sg_split.c | 2 + 8 files changed, 372 insertions(+), 4 deletions(-) create mode 100644 drivers/dma-buf/st-dma-buf.c
base-commit: fe2ec83746e501645709761605c2464a44fd2929
Signature · 1 line
2.53.0
RFC PATCH 1/3 dma-buf: keep the DMA flags in the DMABUF_DEBUG copy
With DMABUF_DEBUG, dma_buf_map_attachment() hands the importer a copy of the sg_table that keeps only sg_dma_address() and sg_dma_len(). The dma_flags (SG_DMA_BUS_ADDRESS, SG_DMA_SWIOTLB) are dropped.
The flags describe the DMA side of an entry, which is the side importers may use. Copy them as well.
This is not a bug fix. No importer reads the flags today. Their only readers are dma-iommu, dma-direct and iommu_map_sg(), which are not supposed to see an importer's copy at all. The warn mode added in the next patch keeps its marker in dma_flags and lets importers that do get there continue. They should then see the same flags as without DMABUF_DEBUG.
---
Notes:
Built alone on x86_64 with NEED_SG_DMA_FLAGS=y (dma-buf.o, W=1). The KUnit test in patch 3 checks the flags in the copy. It passes on x86_64 under QEMU in strict and in warn mode.
drivers/dma-buf/dma-buf.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/dma-buf/dma-buf.c +4 -0
@@ -904,6 +904,10 @@ static int dma_buf_wrap_sg_table(struct sg_table **sg_table)sg_assign_page (to_sg,NULL );sg_dma_address (to_sg) =sg_dma_address (from_sg);sg_dma_len (to_sg) =sg_dma_len (from_sg); +#ifdef CONFIG_NEED_SG_DMA_FLAGS +/* the flags describe the DMA side, e.g. SG_DMA_BUS_ADDRESS */ + to_sg->dma_flags = from_sg->dma_flags; +#endif to_sg =sg_next (to_sg); }
Signature · 1 line
2.53.0
Christian König
maintainerRe: [RFC PATCH 1/3] dma-buf: keep the DMA flags in the DMABUF_DEBUG copy
Quoted 31 lines · Karl Mehltretter
With
DMABUF_DEBUG,dma_buf_map_attachment()hands the importer a copy of thesg_tablethat keeps onlysg_dma_address()andsg_dma_len(). Thedma_flags(SG_DMA_BUS_ADDRESS,SG_DMA_SWIOTLB) are dropped.The flags describe the DMA side of an entry, which is the side importers may use. Copy them as well.
This is not a bug fix. No importer reads the flags today. Their only readers are dma-iommu, dma-direct and
iommu_map_sg(), which are not supposed to see an importer's copy at all. The warn mode added in the next patch keeps its marker indma_flagsand lets importers that do get there continue. They should then see the same flags as withoutDMABUF_DEBUG.Assisted-by LLMSigned-off-by Karl Mehltretter <[email protected]>---
Notes:
Built alone onx86_64with NEED_SG_DMA_FLAGS=y (dma-buf.o, W=1). The KUnit test in patch 3 checks the flags in the copy. It passes onx86_64under QEMU in strict and in warn mode.drivers/dma-buf/dma-buf.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/dma-buf/dma-buf.c +0
-0
drivers/dma-buf/dma-buf.c static int dma_buf_wrap_sg_table(struct sg_table **sg_table) · line 909
Quoted 6 lines
drivers/dma-buf/dma-buf.c +3
-0@@ -904,6 +904,10 @@ static int dma_buf_wrap_sg_table(struct sg_table **sg_table)sg_assign_page (to_sg,NULL );sg_dma_address (to_sg) =sg_dma_address (from_sg);sg_dma_len (to_sg) =sg_dma_len (from_sg); +#ifdef CONFIG_NEED_SG_DMA_FLAGS +/* the flags describe the DMA side, e.g. SG_DMA_BUS_ADDRESS */ + to_sg->dma_flags = from_sg->dma_flags;
We should probably have a WARN_ON_ONCE(sg_dma_is_swiotlb(from_sg)) here as well since some importers/exporters doesn't realize that this combination will usually not work.
Apart from that looks good to me.
Regards,
Christian.
Quoted 4 lines
drivers/dma-buf/dma-buf.c +1
-0+#endif to_sg =sg_next (to_sg); }
Christian König
maintainerRe: [RFC PATCH 1/3] dma-buf: keep the DMA flags in the DMABUF_DEBUG copy
In reply to Karl MehltretterQuoted 14 lines · Karl Mehltretter
With
DMABUF_DEBUG,dma_buf_map_attachment()hands the importer a copy of thesg_tablethat keeps onlysg_dma_address()andsg_dma_len(). Thedma_flags(SG_DMA_BUS_ADDRESS,SG_DMA_SWIOTLB) are dropped.The flags describe the DMA side of an entry, which is the side importers may use. Copy them as well.
This is not a bug fix. No importer reads the flags today. Their only readers are dma-iommu, dma-direct and
iommu_map_sg(), which are not supposed to see an importer's copy at all. The warn mode added in the next patch keeps its marker indma_flagsand lets importers that do get there continue. They should then see the same flags as withoutDMABUF_DEBUG.
Quoted 2 lines
Assisted-by LLMSigned-off-by Karl Mehltretter <[email protected]>
I've reviewed the patch and pushed it to drm-misc-fixes.
Sorry for the delay,
Christian.
Quoted 25 lines
---
Notes:
Built alone onx86_64with NEED_SG_DMA_FLAGS=y (dma-buf.o, W=1). The KUnit test in patch 3 checks the flags in the copy. It passes onx86_64under QEMU in strict and in warn mode.drivers/dma-buf/dma-buf.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/dma-buf/dma-buf.c +4
-0@@ -904,6 +904,10 @@ static int dma_buf_wrap_sg_table(struct sg_table **sg_table)sg_assign_page (to_sg,NULL );sg_dma_address (to_sg) =sg_dma_address (from_sg);sg_dma_len (to_sg) =sg_dma_len (from_sg); +#ifdef CONFIG_NEED_SG_DMA_FLAGS +/* the flags describe the DMA side, e.g. SG_DMA_BUS_ADDRESS */ + to_sg->dma_flags = from_sg->dma_flags; +#endif to_sg =sg_next (to_sg); }
RFC PATCH 2/3 dma-buf: add a warn-only mode to DMABUF_DEBUG
In reply to Karl MehltretterDMABUF_DEBUG hands importers a copy of the exporter's sg_table without the struct pages. An importer that uses them then fails, often far from the cause and without a message that points at it. Where sg_dma_len() is sg->length, the copy cannot hide the length either.
Add DMABUF_DEBUG_WARN. With it the copy keeps the CPU side of the exporter's table: page, offset and length of all orig_nents entries, and the exporter's nents and orig_nents. A table without a CPU side (orig_nents == 0) stays that way. Every entry of the copy is marked with a new dma_flags bit, SG_DMA_DMABUF_DEBUG.
sg_page(), sg_nents_for_len() and sg_split() test the bit and print a rate limited message with a stack trace. sg_page() is mostly reached through DMA and scatterlist helpers, so the trace is what names the importer. These CPU-side accesses can continue after the report instead of failing because the fields were cleared.
The report is not a WARN(). It does not taint the kernel and does not trigger panic_on_warn.
Reads of sg->offset and sg->length that do not go through these helpers are not seen. The option adds a test to every sg_page() and selects NEED_SG_DMA_FLAGS.
Strict mode remains the default and continues to remove the CPU-side fields.
---
Notes:
Tested on v7.3-rc4-70-gfe2ec83746e5 with GCC 15.2.0.
Builds, without warnings:
- x86_64 with DMABUF_DEBUG_WARN=y, full build
- arm64 defconfig with DMABUF_DEBUG_WARN=y: Image, and the msm and rockchip DRM drivers
- ARM926 (SAM9X75) with DMABUF_DEBUG_WARN=y and without NEED_SG_DMA_LENGTH, full build
- ARM926 with DMABUF_DEBUG=n and without NEED_SG_DMA_FLAGS, full build. dma-buf.o and lib/scatterlist.o disassemble as on the base commit, except for __LINE__ constants.
- x86_64 with SG_SPLIT, W=1, the objects touched here: with DMABUF_DEBUG_WARN=y, with DMABUF_DEBUG=y alone, and with DMABUF_DEBUG=n
Runtime, both in QEMU with local device models, not on hardware.
Zynq with an AXI ADC, IIO DMABUF capture into udmabuf buffers, NEED_SG_DMA_LENGTH=y. The IIO dmaengine buffer calls sg_nents_for_len() on the attachment's table. With DMABUF_DEBUG=y alone the capture fails:
DMABUF_ENQUEUE failed: Device or resource busy
With DMABUF_DEBUG_WARN=y all 16 blocks arrive with the right data, the kernel is not tainted, and the log has one report:
DMA-BUF: importer used the CPU side of an exporter's sg_table
CPU: 0 UID: 0 PID: 48 Comm: iio-dmabuf Not tainted 7.3.0-rc4+ #2 VOLUNTARY Hardware name: Xilinx Zynq Platform
Call trace:
unwind_backtrace from show_stack+0x10/0x14
show_stack from dump_stack_lvl+0x54/0x68
dump_stack_lvl from sg_nents_for_len+0xd8/0xe4
sg_nents_for_len from iio_dmaengine_buffer_submit_block+0x4c/0x33c iio_dmaengine_buffer_submit_block from iio_dma_buffer_submit_block.part.0+0x5c/0x104 iio_dma_buffer_submit_block.part.0 from iio_dma_buffer_enqueue_dmabuf+0x68/0xa0 iio_dma_buffer_enqueue_dmabuf from iio_buffer_chrdev_ioctl+0x520/0x9a4 iio_buffer_chrdev_ioctl from sys_ioctl+0x460/0x914
sys_ioctl from ret_fast_syscall+0x0/0x4c
x86_64 with intel-iommu, xHCI and a SUR40, capture into udmabuf buffers. This kernel had a one-line test-only change in sur40 that makes the vb2 queue use the USB controller's DMA device. There was no report during boot. The capture gives one, from sg_page() in iommu_dma_map_sg():
DMA-BUF: importer used the CPU side of an exporter's sg_table
CPU: 0 UID: 0 PID: 9 Comm: kworker/0:0 Not tainted 7.3.0-rc4+ #5 PREEMPT(lazy)
Workqueue: events_freezable input_dev_poller_work
Call Trace:
<TASK>
dump_stack_lvl+0x4d/0x70
iommu_dma_map_sg+0x4c0/0x1010
__dma_map_sg_attrs+0x254/0x3b0
dma_map_sg_attrs+0xe/0x20
usb_hcd_map_urb_for_dma+0x7e0/0x1620 usb_hcd_submit_urb+0x162/0x1af0
usb_sg_wait+0x17c/0x550
sur40_poll+0xb3e/0xff0
input_dev_poller_work+0x54/0x90
process_one_work+0x692/0xf90
[...]
The capture did not finish in this setup. The guest stalled in xhci_queue_bulk_tx() after the report, so this run only shows the report.
The SG_DMA_* defines move up in scatterlist.h because sg_page() needs the new one.
drivers/dma-buf/Kconfig | 23 +++++++++++++++++++ drivers/dma-buf/dma-buf.c | 44 ++++++++++++++++++++++++++++++++++++- include/linux/scatterlist.h | 26 +++++++++++++++++++--- lib/scatterlist.c | 22 +++++++++++++++++++ lib/sg_split.c | 2 ++ 5 files changed, 113 insertions(+), 4 deletions(-)
drivers/dma-buf/Kconfig +23 -0
@@ -49,6 +49,29 @@ config DMABUF_DEBUG exporters. Specifically it validates that importers do not peek at the underlying struct page when they import a buffer. +config DMABUF_DEBUG_WARN + bool "Warn instead of hiding the pages from DMA-BUF importers" + depends on DMABUF_DEBUG + select NEED_SG_DMA_FLAGS + help + DMABUF_DEBUG normally hands importers a copy of the exporter's + sg_table without the struct page pointers, so that an importer which + uses them fails early. + + With this option the copy keeps the pages, offsets and lengths and is + only marked. sg_page() and the helpers built on it, sg_nents_for_len() + and sg_split() then print a rate limited message with a stack trace + when they are used on such a table. The access itself continues + instead of failing because the fields were cleared. The message is + not a WARN(): it does not taint the kernel or trigger panic_on_warn. + + Importers that read sg->offset or sg->length directly are not + noticed. The option adds a test to every sg_page() call. It selects + NEED_SG_DMA_FLAGS, which adds dma_flags and may increase the size of + struct scatterlist. + + If unsure, say N. + config DMABUF_KUNIT_TEST tristate "KUnit tests for DMA-BUF" if !KUNIT_ALL_TESTS depends on KUNIT
drivers/dma-buf/dma-buf.c +43 -1
86 lines not on this page · raw message
include/linux/scatterlist.h +23 -3
@@ -21,6 +21,13 @@ struct scatterlist {#endif }; +/* Bits in dma_flags, see below. sg_page() needs SG_DMA_DMABUF_DEBUG. */ +#ifdef CONFIG_NEED_SG_DMA_FLAGS +#define SG_DMA_BUS_ADDRESS (1 << 0) +#define SG_DMA_SWIOTLB (1 << 1) +#define SG_DMA_DMABUF_DEBUG (1 << 2) +#endif +/* * These macros should be used after a dma_map_sg call has been done * to get bus addresses of each of the SG entries and their lengths.
@@ -188,11 +195,27 @@ static inline void sg_set_folio(struct scatterlist *sg, struct folio *folio, sg->length = len; } +#ifdef CONFIG_DMABUF_DEBUG_WARN +void sg_dmabuf_cpu_access_warn (void ); + +/* Report use of the CPU side of a table that a DMA-BUF importer was given */ +static inline void sg_dmabuf_cpu_access_check (struct scatterlist *sg) +{ +if (unlikely (sg->dma_flags &SG_DMA_DMABUF_DEBUG )) +sg_dmabuf_cpu_access_warn (); +} +#else +static inline void sg_dmabuf_cpu_access_check (struct scatterlist *sg) +{ +} +#endif +static inline struct page *sg_page (struct scatterlist *sg) {#ifdef CONFIG_DEBUG_SG BUG_ON (sg_is_chain (sg));#endif +sg_dmabuf_cpu_access_check (sg);return (struct page *)((sg)->page_link & ~SG_PAGE_LINK_MASK ); }
@@ -303,9 +326,6 @@ static inline void sg_unmark_end(struct scatterlist *sg)*/ #ifdef CONFIG_NEED_SG_DMA_FLAGS -#define SG_DMA_BUS_ADDRESS (1 << 0) -#define SG_DMA_SWIOTLB (1 << 1) -/** * sg_dma_is_bus_address - Return whether a given segment was marked * as a bus address
lib/scatterlist.c +22 -0
@@ -12,6 +12,27 @@#include <linux/bvec.h> #include <linux/uio.h> #include <linux/folio_queue.h> +#include <linux/printk.h> +#include <linux/ratelimit.h> + +#ifdef CONFIG_DMABUF_DEBUG_WARN +/* +* Not a WARN(): a wrong importer must not taint the kernel or trigger +* panic_on_warn. sg_page() is mostly reached through DMA or scatterlist +* helpers, so the stack trace is what identifies the importer. +*/ +void sg_dmabuf_cpu_access_warn (void ) +{ + static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, 1); + +if (!__ratelimit (&rs)) +return ; + +pr_warn ("DMA-BUF: importer used the CPU side of an exporter's sg_table\n" ); +dump_stack_lvl (KERN_WARNING ); +} +EXPORT_SYMBOL (sg_dmabuf_cpu_access_warn); +#endif /** * sg_nents - return total count of entries in scatterlist
@@ -54,6 +75,7 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len)return 0 ;for (nents =0 , total =0 ; sg; sg =sg_next (sg)) { +sg_dmabuf_cpu_access_check (sg); nents++; total += sg->length;if (total >= len)
lib/sg_split.c +2 -0
@@ -33,6 +33,8 @@ static int sg_calculate_split(struct scatterlist *in, int nents, int nb_splits, }for_each_sg (in, sg, nents, i) { +if (!mapped) +sg_dmabuf_cpu_access_check (sg); sglen = mapped ?sg_dma_len (sg) : sg->length;if (skip > sglen) { skip -= sglen;
Signature · 1 line
2.53.0
Re: [RFC PATCH 2/3] dma-buf: add a warn-only mode to DMABUF_DEBUG
Hi,
Quoted 20 lines · Karl Mehltretter
DMABUF_DEBUGhands importers a copy of the exporter'ssg_tablewithout the struct pages. An importer that uses them then fails, often far from the cause and without a message that points at it. Wheresg_dma_len()issg->length, the copy cannot hide the length either.Add
DMABUF_DEBUG_WARN. With it the copy keeps the CPU side of the exporter's table: page, offset and length of allorig_nentsentries, and the exporter's nents andorig_nents. A table without a CPU side (orig_nents== 0) stays that way. Every entry of the copy is marked with a newdma_flagsbit,SG_DMA_DMABUF_DEBUG.
sg_page(),sg_nents_for_len()andsg_split()test the bit and print a rate limited message with a stack trace.sg_page()is mostly reached through DMA and scatterlist helpers, so the trace is what names the importer. These CPU-side accesses can continue after the report instead of failing because the fields were cleared.The report is not a
WARN(). It does not taint the kernel and does not triggerpanic_on_warn.
Quoted 6 lines
Reads of
sg->offsetandsg->lengththat do not go through these helpers are not seen. The option adds a test to everysg_page()and selectsNEED_SG_DMA_FLAGS.Strict mode remains the default and continues to remove the CPU-side fields.
Thanks for this patch set!
I build a 7.3-rc6 based kernel with this patch set included. And I (ofc) enabled DMABUF_DEBUG and DMABUF_DEBUG_WARN.
I then tested it on my NanoPC-T6 Plus (RK3588 SoC) connected to my 1080p monitor and also on my NanoPC-T6 LTS (RK3588) connected to my 4K TV (which is HDMI 2.0, not 2.1, capable).
On both devices I got an identical call trace.
I did get a new (identical) one every time I resized the video window, ie going from windowed to full-screen and back again.
```sh [ 1091.981354] rc rc3: two consecutive events of type space [ 1104.670513] input: EDIFIER e235 (AVRCP) as /devices/virtual/input/input12 [ 1172.090231] devfreq fb000000.gpu: Couldn't update frequency transition information. [ 1189.403278] devfreq fb000000.gpu: Couldn't update frequency transition information. [ 1206.309295] input: EDIFIER e235 (AVRCP) as /devices/virtual/input/input13 [ 1268.968696] DMA-BUF: importer used the CPU side of an exporter's sg_table [ 1268.968710] CPU: 6 UID: 1000 PID: 29025 Comm: sway Not tainted 7.3-rc6+unreleased-arm64-cknow #1 PREEMPTLAZY Debian 7.3~rc6-3 [ 1268.968715] Hardware name: FriendlyElec NanoPC-T6 Plus (DT) [ 1268.968717] Call trace: [ 1268.968719] show_stack+0x20/0x38 (C) [ 1268.968726] dump_stack_lvl+0x60/0x80 [ 1268.968730] sg_dmabuf_cpu_access_warn.part.0+0x24/0x30 [ 1268.968734] sg_dmabuf_cpu_access_warn+0x34/0x38 [ 1268.968739] iommu_map_sg+0xc8/0x1e0 [ 1268.968745] rockchip_gem_iommu_map+0x8c/0x128 [rockchipdrm] [ 1268.968757] rockchip_gem_prime_import_sg_table+0x58/0x160 [rockchipdrm] [ 1268.968761] drm_gem_prime_import_dev+0xa8/0x1d0 [drm] [ 1268.968776] drm_gem_prime_fd_to_handle+0x1a4/0x280 [drm] [ 1268.968786] drm_prime_fd_to_handle_ioctl+0x40/0x58 [drm] [ 1268.968796] drm_ioctl_kernel+0xc8/0x140 [drm] [ 1268.968806] drm_ioctl+0x254/0x558 [drm] [ 1268.968816] __arm64_sys_ioctl+0xac/0x118 [ 1268.968821] invoke_syscall.constprop.0+0xac/0x110 [ 1268.968825] el0_svc_common.constprop.0+0x40/0xf0 [ 1268.968829] do_el0_svc+0x24/0x40 [ 1268.968832] el0_svc+0x40/0x260 [ 1268.968836] el0t_64_sync_handler+0xa0/0xe8 [ 1268.968838] el0t_64_sync+0x198/0x1a0 [ 1290.531300] sg_dmabuf_cpu_access_warn: 36 callbacks suppressed [ 1290.531310] DMA-BUF: importer used the CPU side of an exporter's sg_table [ 1290.531319] CPU: 5 UID: 1000 PID: 29025 Comm: sway Not tainted 7.3-rc6+unreleased-arm64-cknow #1 PREEMPTLAZY Debian 7.3~rc6-3 [ 1290.531323] Hardware name: FriendlyElec NanoPC-T6 Plus (DT) [ 1290.531325] Call trace: [ 1290.531327] show_stack+0x20/0x38 (C) [ 1290.531335] dump_stack_lvl+0x60/0x80 [ 1290.531338] sg_dmabuf_cpu_access_warn.part.0+0x24/0x30 [ 1290.531342] sg_dmabuf_cpu_access_warn+0x34/0x38 [ 1290.531347] iommu_map_sg+0xc8/0x1e0 [ 1290.531352] rockchip_gem_iommu_map+0x8c/0x128 [rockchipdrm] [ 1290.531364] rockchip_gem_prime_import_sg_table+0x58/0x160 [rockchipdrm] [ 1290.531368] drm_gem_prime_import_dev+0xa8/0x1d0 [drm] [ 1290.531384] drm_gem_prime_fd_to_handle+0x1a4/0x280 [drm] [ 1290.531394] drm_prime_fd_to_handle_ioctl+0x40/0x58 [drm] [ 1290.531404] drm_ioctl_kernel+0xc8/0x140 [drm] [ 1290.531414] drm_ioctl+0x254/0x558 [drm] [ 1290.531424] __arm64_sys_ioctl+0xac/0x118 [ 1290.531429] invoke_syscall.constprop.0+0xac/0x110 [ 1290.531433] el0_svc_common.constprop.0+0x40/0xf0 [ 1290.531437] do_el0_svc+0x24/0x40 [ 1290.531439] el0_svc+0x40/0x260 [ 1290.531444] el0t_64_sync_handler+0xa0/0xe8 [ 1290.531446] el0t_64_sync+0x198/0x1a0 [ 1294.197699] devfreq fb000000.gpu: Couldn't update frequency transition information. [ 1313.464098] sg_dmabuf_cpu_access_warn: 149 callbacks suppressed [ 1313.464121] DMA-BUF: importer used the CPU side of an exporter's sg_table [ 1313.464144] CPU: 2 UID: 1000 PID: 29025 Comm: sway Not tainted 7.3-rc6+unreleased-arm64-cknow #1 PREEMPTLAZY Debian 7.3~rc6-3 [ 1313.464156] Hardware name: FriendlyElec NanoPC-T6 Plus (DT) [ 1313.464163] Call trace: [ 1313.464168] show_stack+0x20/0x38 (C) ... ```
Full dmesg output is posted here:
https://paste.sr.ht/~diederik/3d0020b4f3b0e8547397c20d5c10387d88d32341
Hopefully this helps fixing the underlaying issue.
Cheers,
Diederik
Quoted 321 lines
Assisted-by LLMSigned-off-by Karl Mehltretter <[email protected]>---
Notes:
Tested on v7.3-rc4-70-gfe2ec83746e5 with GCC 15.2.0.Builds, without warnings:
-
x86_64with DMABUF_DEBUG_WARN=y, full build
- arm64 defconfig with DMABUF_DEBUG_WARN=y: Image, and the msm and rockchip DRM drivers
- ARM926 (SAM9X75) with DMABUF_DEBUG_WARN=y and withoutNEED_SG_DMA_LENGTH, full build
- ARM926 with DMABUF_DEBUG=n and withoutNEED_SG_DMA_FLAGS, full build. dma-buf.o andlib/scatterlist.odisassemble as on the base commit, except for__LINE__constants.
-x86_64withSG_SPLIT, W=1, the objects touched here: with DMABUF_DEBUG_WARN=y, with DMABUF_DEBUG=y alone, and with DMABUF_DEBUG=nRuntime, both in QEMU with local device models, not on hardware.
Zynq with an AXI ADC, IIO DMABUF capture into udmabuf buffers, NEED_SG_DMA_LENGTH=y. The IIO dmaengine buffer calls
sg_nents_for_len()on the attachment's table. With DMABUF_DEBUG=y alone the capture fails:
DMABUF_ENQUEUEfailed: Device or resource busyWith DMABUF_DEBUG_WARN=y all 16 blocks arrive with the right data, the kernel is not tainted, and the log has one report:
DMA-BUF: importer used the CPU side of an exporter's
sg_table
CPU: 0 UID: 0 PID: 48 Comm: iio-dmabuf Not tainted 7.3.0-rc4+ #2 VOLUNTARY Hardware name: Xilinx Zynq Platform
Call trace:
unwind_backtracefromshow_stack+0x10/0x14
show_stackfromdump_stack_lvl+0x54/0x68
dump_stack_lvlfromsg_nents_for_len+0xd8/0xe4
sg_nents_for_lenfromiio_dmaengine_buffer_submit_block+0x4c/0x33ciio_dmaengine_buffer_submit_blockfrom iio_dma_buffer_submit_block.part.0+0x5c/0x104 iio_dma_buffer_submit_block.part.0 fromiio_dma_buffer_enqueue_dmabuf+0x68/0xa0iio_dma_buffer_enqueue_dmabuffromiio_buffer_chrdev_ioctl+0x520/0x9a4iio_buffer_chrdev_ioctlfromsys_ioctl+0x460/0x914
sys_ioctlfromret_fast_syscall+0x0/0x4c
x86_64with intel-iommu, xHCI and a SUR40, capture into udmabuf buffers. This kernel had a one-line test-only change in sur40 that makes the vb2 queue use the USB controller's DMA device. There was no report during boot. The capture gives one, fromsg_page()iniommu_dma_map_sg():DMA-BUF: importer used the CPU side of an exporter's
sg_table
CPU: 0 UID: 0 PID: 9 Comm: kworker/0:0 Not tainted 7.3.0-rc4+ #5 PREEMPT(lazy)
Workqueue:events_freezableinput_dev_poller_work
Call Trace:
<TASK>
dump_stack_lvl+0x4d/0x70
iommu_dma_map_sg+0x4c0/0x1010
__dma_map_sg_attrs+0x254/0x3b0
dma_map_sg_attrs+0xe/0x20
usb_hcd_map_urb_for_dma+0x7e0/0x1620usb_hcd_submit_urb+0x162/0x1af0
usb_sg_wait+0x17c/0x550
sur40_poll+0xb3e/0xff0
input_dev_poller_work+0x54/0x90
process_one_work+0x692/0xf90
[...]The capture did not finish in this setup. The guest stalled in
xhci_queue_bulk_tx()after the report, so this run only shows the report.The SG_DMA_* defines move up in
scatterlist.hbecausesg_page()needs the new one.drivers/dma-buf/Kconfig | 23 +++++++++++++++++++ drivers/dma-buf/dma-buf.c | 44 ++++++++++++++++++++++++++++++++++++- include/linux/scatterlist.h | 26 +++++++++++++++++++--- lib/scatterlist.c | 22 +++++++++++++++++++ lib/sg_split.c | 2 ++ 5 files changed, 113 insertions(+), 4 deletions(-)drivers/dma-buf/Kconfig +23
-0@@ -49,6 +49,29 @@ config DMABUF_DEBUG exporters. Specifically it validates that importers do not peek at the underlying struct page when they import a buffer. +config DMABUF_DEBUG_WARN + bool "Warn instead of hiding the pages from DMA-BUF importers" + depends on DMABUF_DEBUG + select NEED_SG_DMA_FLAGS + help + DMABUF_DEBUG normally hands importers a copy of the exporter's + sg_table without the struct page pointers, so that an importer which + uses them fails early. + + With this option the copy keeps the pages, offsets and lengths and is + only marked. sg_page() and the helpers built on it, sg_nents_for_len() + and sg_split() then print a rate limited message with a stack trace + when they are used on such a table. The access itself continues + instead of failing because the fields were cleared. The message is + not a WARN(): it does not taint the kernel or trigger panic_on_warn. + + Importers that read sg->offset or sg->length directly are not + noticed. The option adds a test to every sg_page() call. It selects + NEED_SG_DMA_FLAGS, which adds dma_flags and may increase the size of + struct scatterlist. + + If unsure, say N. + config DMABUF_KUNIT_TEST tristate "KUnit tests for DMA-BUF" if !KUNIT_ALL_TESTS depends on KUNITdrivers/dma-buf/dma-buf.c +43
-1@@ -57,6 +57,7 @@struct dma_buf_sg_table_wrapper {struct sg_table *original;struct sg_table wrapper; +unsigned int alloc_nents; };static inline int is_dma_buf_file (struct file *);@@ -874,11 +875,42 @@ void dma_buf_put(struct dma_buf *dmabuf) }EXPORT_SYMBOL_NS_GPL (dma_buf_put,"DMA_BUF" ); +#ifdef CONFIG_DMABUF_DEBUG_WARN +/* +* Warn mode: the importer also gets the CPU side of the exporter's table, +* marked so that sg_page() and friends can report who uses it. +*/ +static void dma_buf_wrap_cpu_side (struct sg_table *to,struct sg_table *from) +{ +struct scatterlist *to_sg, *from_sg; +int i; + +for_each_sgtable_sg (to, to_sg, i) + to_sg->dma_flags |=SG_DMA_DMABUF_DEBUG ; + + to_sg = to->sgl; +for_each_sgtable_sg (from, from_sg, i) { +sg_assign_page (to_sg,sg_page (from_sg)); + to_sg->offset = from_sg->offset; + to_sg->length = from_sg->length; + to_sg =sg_next (to_sg); + } + + to->nents = from->nents; + to->orig_nents = from->orig_nents; +} +#else +static void dma_buf_wrap_cpu_side (struct sg_table *to,struct sg_table *from) +{ +} +#endif +static int dma_buf_wrap_sg_table (struct sg_table **sg_table) {struct scatterlist *to_sg, *from_sg;struct sg_table *from = *sg_table;struct dma_buf_sg_table_wrapper *to; +unsigned int nents = from->nents;int i, ret;if (!IS_ENABLED (CONFIG_DMABUF_DEBUG ))@@ -888,14 +920,21 @@ static int dma_buf_wrap_sg_table(struct sg_table **sg_table)* To catch abuse of the underlying struct page by importers copy the * sg_table without copying the page_link and give only the copy back to * the importer. +* +* With DMABUF_DEBUG_WARN the copy keeps the CPU side, which can have +* more entries than the DMA side. */ to =kzalloc_obj (*to);if (!to)return -ENOMEM ; - ret =sg_alloc_table (&to->wrapper, from->nents,GFP_KERNEL ); +if (IS_ENABLED (CONFIG_DMABUF_DEBUG_WARN )) + nents =max (nents, from->orig_nents); + + ret =sg_alloc_table (&to->wrapper, nents,GFP_KERNEL );if (ret) goto free_to; + to->alloc_nents = nents; to_sg = to->wrapper.sgl;for_each_sgtable_dma_sg (from, from_sg, i) {@@ -910,6 +949,7 @@ static int dma_buf_wrap_sg_table(struct sg_table **sg_table)#endif to_sg =sg_next (to_sg); } +dma_buf_wrap_cpu_side (&to->wrapper, from); to->original = from; *sg_table = &to->wrapper;@@ -929,6 +969,8 @@ static void dma_buf_unwrap_sg_table(struct sg_table **sg_table) copy =container_of (*sg_table,typeof (*copy), wrapper); *sg_table = copy->original; +/* sg_free_table() needs the number of allocated entries */ + copy->wrapper.orig_nents = copy->alloc_nents;sg_free_table (©->wrapper);kfree (copy); }include/linux/scatterlist.h +23
-3@@ -21,6 +21,13 @@ struct scatterlist {#endif }; +/* Bits in dma_flags, see below. sg_page() needs SG_DMA_DMABUF_DEBUG. */ +#ifdef CONFIG_NEED_SG_DMA_FLAGS +#define SG_DMA_BUS_ADDRESS (1 << 0) +#define SG_DMA_SWIOTLB (1 << 1) +#define SG_DMA_DMABUF_DEBUG (1 << 2) +#endif +/* * These macros should be used after a dma_map_sg call has been done * to get bus addresses of each of the SG entries and their lengths. @@ -188,11 +195,27 @@ static inline void sg_set_folio(struct scatterlist *sg, struct folio *folio, sg->length = len; } +#ifdef CONFIG_DMABUF_DEBUG_WARN +void sg_dmabuf_cpu_access_warn (void ); + +/* Report use of the CPU side of a table that a DMA-BUF importer was given */ +static inline void sg_dmabuf_cpu_access_check (struct scatterlist *sg) +{ +if (unlikely (sg->dma_flags &SG_DMA_DMABUF_DEBUG )) +sg_dmabuf_cpu_access_warn (); +} +#else +static inline void sg_dmabuf_cpu_access_check (struct scatterlist *sg) +{ +} +#endif +static inline struct page *sg_page (struct scatterlist *sg) {#ifdef CONFIG_DEBUG_SG BUG_ON (sg_is_chain (sg));#endif +sg_dmabuf_cpu_access_check (sg);return (struct page *)((sg)->page_link & ~SG_PAGE_LINK_MASK ); }@@ -303,9 +326,6 @@ static inline void sg_unmark_end(struct scatterlist *sg)*/ #ifdef CONFIG_NEED_SG_DMA_FLAGS -#define SG_DMA_BUS_ADDRESS (1 << 0) -#define SG_DMA_SWIOTLB (1 << 1) -/** * sg_dma_is_bus_address - Return whether a given segment was marked * as a bus address lib/scatterlist.c +22
-0@@ -12,6 +12,27 @@#include <linux/bvec.h> #include <linux/uio.h> #include <linux/folio_queue.h> +#include <linux/printk.h> +#include <linux/ratelimit.h> + +#ifdef CONFIG_DMABUF_DEBUG_WARN +/* +* Not a WARN(): a wrong importer must not taint the kernel or trigger +* panic_on_warn. sg_page() is mostly reached through DMA or scatterlist +* helpers, so the stack trace is what identifies the importer. +*/ +void sg_dmabuf_cpu_access_warn (void ) +{ + static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, 1); + +if (!__ratelimit (&rs)) +return ; + +pr_warn ("DMA-BUF: importer used the CPU side of an exporter's sg_table\n" ); +dump_stack_lvl (KERN_WARNING ); +} +EXPORT_SYMBOL (sg_dmabuf_cpu_access_warn); +#endif /** * sg_nents - return total count of entries in scatterlist @@ -54,6 +75,7 @@ int sg_nents_for_len(struct scatterlist *sg, u64 len)return 0 ;for (nents =0 , total =0 ; sg; sg =sg_next (sg)) { +sg_dmabuf_cpu_access_check (sg); nents++; total += sg->length;if (total >= len)lib/sg_split.c +2
-0@@ -33,6 +33,8 @@ static int sg_calculate_split(struct scatterlist *in, int nents, int nb_splits, }for_each_sg (in, sg, nents, i) { +if (!mapped) +sg_dmabuf_cpu_access_check (sg); sglen = mapped ?sg_dma_len (sg) : sg->length;if (skip > sglen) { skip -= sglen;
Re: [RFC PATCH 2/3] dma-buf: add a warn-only mode to DMABUF_DEBUG
Quoted 6 lines · Diederik de Haas
[ 1091.981354] rc rc3: two consecutive events of type space [ 1104.670513] input: EDIFIER e235 (AVRCP) as /devices/virtual/input/input12 [ 1172.090231] devfreq fb000000.gpu: Couldn't update frequency transition information. [ 1189.403278] devfreq fb000000.gpu: Couldn't update frequency transition information. [ 1206.309295] input: EDIFIER e235 (AVRCP) as /devices/virtual/input/input13 [ 1268.968696] DMA-BUF: importer used the CPU side of an exporter's sg_table
This is a nice stack trace, is that the point of this series?
Quoted 4 lines
[ 1268.968710] CPU: 6 UID: 1000 PID: 29025 Comm: sway Not tainted 7.3-rc6+unreleased-arm64-cknow #1 PREEMPTLAZY Debian 7.3~rc6-3 [ 1268.968715] Hardware name: FriendlyElec NanoPC-T6 Plus (DT) [ 1268.968717] Call trace: [ 1268.968719] show_stack+0x20/0x38 (C)
Quoted 8 lines
[ 1268.968726] dump_stack_lvl+0x60/0x80 [ 1268.968730] sg_dmabuf_cpu_access_warn.part.0+0x24/0x30 [ 1268.968734] sg_dmabuf_cpu_access_warn+0x34/0x38 [ 1268.968739] iommu_map_sg+0xc8/0x1e0 [ 1268.968745] rockchip_gem_iommu_map+0x8c/0x128 [rockchipdrm] [ 1268.968757] rockchip_gem_prime_import_sg_table+0x58/0x160 [rockchipdrm] [ 1268.968761] drm_gem_prime_import_dev+0xa8/0x1d0 [drm] [ 1268.968776] drm_gem_prime_fd_to_handle+0x1a4/0x280 [drm]
So.. This is the exact same thing I need for iommufd.
rockchip is managing its own iommu domain and you cannot map to an iommu domain without using a physical address.
Of course it is *completely* illegal to call iommu_map_sgtable() in the importer side of a dmabuf.
static int rockchip_gem_iommu_map(struct rockchip_gem_object *rk_obj) {
[..]
ret = iommu_map_sgtable(private->domain, rk_obj->dma_addr, rk_obj->sgt, prot);
Jason
Christian König
maintainerRe: [RFC PATCH 2/3] dma-buf: add a warn-only mode to DMABUF_DEBUG
Quoted 23 lines · Jason Gunthorpe
Quoted 6 lines · Diederik de Haas
[ 1091.981354] rc rc3: two consecutive events of type space [ 1104.670513] input: EDIFIER e235 (AVRCP) as /devices/virtual/input/input12 [ 1172.090231] devfreq fb000000.gpu: Couldn't update frequency transition information. [ 1189.403278] devfreq fb000000.gpu: Couldn't update frequency transition information. [ 1206.309295] input: EDIFIER e235 (AVRCP) as /devices/virtual/input/input13 [ 1268.968696] DMA-BUF: importer used the CPU side of an exporter's sg_tableThis is a nice stack trace, is that the point of this series?
Quoted 4 lines
[ 1268.968710] CPU: 6 UID: 1000 PID: 29025 Comm: sway Not tainted 7.3-rc6+unreleased-arm64-cknow #1 PREEMPTLAZY Debian 7.3~rc6-3 [ 1268.968715] Hardware name: FriendlyElec NanoPC-T6 Plus (DT) [ 1268.968717] Call trace: [ 1268.968719] show_stack+0x20/0x38 (C)Quoted 8 lines
[ 1268.968726] dump_stack_lvl+0x60/0x80 [ 1268.968730] sg_dmabuf_cpu_access_warn.part.0+0x24/0x30 [ 1268.968734] sg_dmabuf_cpu_access_warn+0x34/0x38 [ 1268.968739] iommu_map_sg+0xc8/0x1e0 [ 1268.968745] rockchip_gem_iommu_map+0x8c/0x128 [rockchipdrm] [ 1268.968757] rockchip_gem_prime_import_sg_table+0x58/0x160 [rockchipdrm] [ 1268.968761] drm_gem_prime_import_dev+0xa8/0x1d0 [drm] [ 1268.968776] drm_gem_prime_fd_to_handle+0x1a4/0x280 [drm]
Quoted 7 lines
So.. This is the exact same thing I need for iommufd.
rockchip is managing its own iommu domain and you cannot map to an iommu domain without using a physical address.
Of course it is *completely* illegal to call
iommu_map_sgtable()in the importer side of a dmabuf.
Yeah we are deprecating this for the last ~5 years now. This bubbled up because I change to DMA-buf debug code to be enabled by default on DEBUG_KERNEL.
Rob is now working on a solution right now for MSM which should be used for rockchip as well.
Regards,
Christian.
Quoted 8 lines
static int
rockchip_gem_iommu_map(struct rockchip_gem_object *rk_obj){
[..]ret = iommu_map_sgtable(private->domain, rk_obj->dma_addr, rk_obj->sgt, prot);Jason
Re: [RFC PATCH 2/3] dma-buf: add a warn-only mode to DMABUF_DEBUG
Quoted 21 lines · Christian König
Quoted 13 lines
Quoted 4 lines
[ 1268.968710] CPU: 6 UID: 1000 PID: 29025 Comm: sway Not tainted 7.3-rc6+unreleased-arm64-cknow #1 PREEMPTLAZY Debian 7.3~rc6-3 [ 1268.968715] Hardware name: FriendlyElec NanoPC-T6 Plus (DT) [ 1268.968717] Call trace: [ 1268.968719] show_stack+0x20/0x38 (C)Quoted 8 lines
[ 1268.968726] dump_stack_lvl+0x60/0x80 [ 1268.968730] sg_dmabuf_cpu_access_warn.part.0+0x24/0x30 [ 1268.968734] sg_dmabuf_cpu_access_warn+0x34/0x38 [ 1268.968739] iommu_map_sg+0xc8/0x1e0 [ 1268.968745] rockchip_gem_iommu_map+0x8c/0x128 [rockchipdrm] [ 1268.968757] rockchip_gem_prime_import_sg_table+0x58/0x160 [rockchipdrm] [ 1268.968761] drm_gem_prime_import_dev+0xa8/0x1d0 [drm] [ 1268.968776] drm_gem_prime_fd_to_handle+0x1a4/0x280 [drm]Quoted 7 lines
So.. This is the exact same thing I need for iommufd.
rockchip is managing its own iommu domain and you cannot map to an iommu domain without using a physical address.
Of course it is *completely* illegal to call
iommu_map_sgtable()in the importer side of a dmabuf.
Quoted 6 lines
Yeah we are deprecating this for the last ~5 years now. This bubbled up because I change to DMA-buf debug code to be enabled by default on
DEBUG_KERNEL.Rob is now working on a solution right now for MSM which should be used for rockchip as well.
So is Rob's plan is to remove the iommu_domains, somehow? Lately we've been NAK'ing alot of the DT hacking that people have been doing in other devices along those lines, so I wonder what that will look like.
It is probably easier to just fix things so the importer can get things into an iommu_domain..
Jason
Re: [RFC PATCH 2/3] dma-buf: add a warn-only mode to DMABUF_DEBUG
In reply to Jason GunthorpeQuoted 9 lines · Jason Gunthorpe
Quoted 6 lines · Diederik de Haas
[ 1091.981354] rc rc3: two consecutive events of type space [ 1104.670513] input: EDIFIER e235 (AVRCP) as /devices/virtual/input/input12 [ 1172.090231] devfreq fb000000.gpu: Couldn't update frequency transition information. [ 1189.403278] devfreq fb000000.gpu: Couldn't update frequency transition information. [ 1206.309295] input: EDIFIER e235 (AVRCP) as /devices/virtual/input/input13 [ 1268.968696] DMA-BUF: importer used the CPU side of an exporter's sg_tableThis is a nice stack trace, is that the point of this series?
Actually only the last line, I just "couldn't help myself" to also include the "Couldn't update frequency transition information" part.
It can be unrelated. Or not. I lack the knowledge to make that determination.
Quoted 4 lines
Quoted 4 lines
[ 1268.968710] CPU: 6 UID: 1000 PID: 29025 Comm: sway Not tainted 7.3-rc6+unreleased-arm64-cknow #1 PREEMPTLAZY Debian 7.3~rc6-3 [ 1268.968715] Hardware name: FriendlyElec NanoPC-T6 Plus (DT) [ 1268.968717] Call trace: [ 1268.968719] show_stack+0x20/0x38 (C)
Quoted 8 lines
Quoted 8 lines
[ 1268.968726] dump_stack_lvl+0x60/0x80 [ 1268.968730] sg_dmabuf_cpu_access_warn.part.0+0x24/0x30 [ 1268.968734] sg_dmabuf_cpu_access_warn+0x34/0x38 [ 1268.968739] iommu_map_sg+0xc8/0x1e0 [ 1268.968745] rockchip_gem_iommu_map+0x8c/0x128 [rockchipdrm] [ 1268.968757] rockchip_gem_prime_import_sg_table+0x58/0x160 [rockchipdrm] [ 1268.968761] drm_gem_prime_import_dev+0xa8/0x1d0 [drm] [ 1268.968776] drm_gem_prime_fd_to_handle+0x1a4/0x280 [drm]
IIUC this was meant to show the actual problem.
Quoted 7 lines
So.. This is the exact same thing I need for iommufd.
rockchip is managing its own iommu domain and you cannot map to an iommu domain without using a physical address.
Of course it is *completely* illegal to call
iommu_map_sgtable()in the importer side of a dmabuf.
I can't say anything useful wrt this, so I added the maintainers of ``drivers/gpu/drm/rockchip/rockchip_drm_gem.c`` to the recipient list.
Quoted 5 lines
static int
rockchip_gem_iommu_map(struct rockchip_gem_object *rk_obj){
[..]ret = iommu_map_sgtable(private->domain, rk_obj->dma_addr, rk_obj->sgt, prot);
It may not matter, but I noticed the majority of that function is from 2016:38f993b7c59e ("drm/rockchip: Do not use DMA mapping API if attached to IOMMU domain") 1aa5ca6e3ec6 ("drm/rockchip: Use common IOMMU API to attach devices")
From the first commit (38f993b7c59e):
The API is not suitable for subsystems consisting of multiple devices and requires severe hacks to use it. To mitigate this, this patch implements allocation and address space management locally by using helpers provided by DRM framework, like other DRM drivers do, e.g. Tegra.
This patch should not introduce any functional changes until the driver is made to attach subdevices into an IOMMU domain with the generic IOMMU API, which will happen in following patch. Based heavily on GEM implementation of Tegra DRM driver.
And I assume "following patch" was 1aa5ca6e3ec6.
Cheers,
Diederik
Re: [RFC PATCH 2/3] dma-buf: add a warn-only mode to DMABUF_DEBUG
In reply to Karl Mehltretterinclude/linux/scatterlist.h static inline void sg_set_folio(struct scatterlist *sg, struct folio *folio, · line 220
Quoted 8 lines · Karl Mehltretter
static inline struct page *sg_page(struct scatterlist *sg) {
#ifdefCONFIG_DEBUG_SGinclude/linux/scatterlist.h +1
-0BUG_ON (sg_is_chain (sg));#endif +sg_dmabuf_cpu_access_check (sg);return (struct page *)((sg)->page_link & ~SG_PAGE_LINK_MASK ); }
I'm not sure I understand the overall intention here, I get what this patch does, but no distro could turn this on by default when it touches *everyone* using scatterlist in a performance sensitive spot, and anyone doing testing can use the existing option - so what is the point?
I certainly don't like this patch, and I don't like the word "DMABUF" in the scatterlist at all. If we want to add something it should be a general mechanism under DEBUG_SG that allows anyone to 'hide' the CPU list from any future access.
Jason
Christian König
maintainerRe: [RFC PATCH 2/3] dma-buf: add a warn-only mode to DMABUF_DEBUG
Quoted 15 lines · Jason Gunthorpe
include/linux/scatterlist.h static inline void sg_set_folio(struct scatterlist *sg, struct folio *folio, · line 220
Quoted 8 lines · Karl Mehltretter
static inline struct page *sg_page(struct scatterlist *sg) {
#ifdefCONFIG_DEBUG_SGinclude/linux/scatterlist.h +1
-0BUG_ON (sg_is_chain (sg));#endif +sg_dmabuf_cpu_access_check (sg);return (struct page *)((sg)->page_link & ~SG_PAGE_LINK_MASK ); }I'm not sure I understand the overall intention here, I get what this patch does, but no distro could turn this on by default when it touches *everyone* using scatterlist in a performance sensitive spot, and anyone doing testing can use the existing option - so what is the point?
Yeah, I was just about to write something similar.
Patch #1 in the series is correct, but I would add a warning if SWIOTLB is used since that usually doesn't work either.
Quoted 4 lines
I certainly don't like this patch, and I don't like the word "DMABUF" in the scatterlist at all. If we want to add something it should be a general mechanism under
DEBUG_SGthat allows anyone to 'hide' the CPU list from any future access.
As Christoph also noted the fundamental problem with sg_tables is that we stuffed the input and output parameters to the DMA mapping function into one data structure.
So by using a sg_table DMA-buf accidentally gave importers access to the physical address as well with all the undesired consequences.
I think when anybody wants to address this mess the first thing which needs to be done is to have a container for DMA addresses which is independent of sg_tables.
Regards,
Christian.
Quoted 2 lines
Jason
RFC PATCH 3/3 dma-buf: test the debug scatterlist wrapper
In reply to Karl MehltretterMap a dma-buf from a mock exporter and check the sg_table the importer gets. The exporter's table has one DMA entry and one, two or no CPU entries. That covers a mapping that merged entries and a table without a CPU side.
In all modes the DMA address, length and existing DMA flags must be preserved, and unmap must give the exporter its own table back. In strict mode the CPU fields must be cleared. In warn mode they must be there for all CPU entries and every entry must be marked.
The warn mode check calls sg_page() and sg_nents_for_len() on the copy on purpose, so it prints one report.
The .kunitconfig now sets DMABUF_DEBUG. For the warn mode add --kconfig_add CONFIG_DMABUF_DEBUG_WARN=y.
---
Notes:
KUnit, the dma-buf suites, with
tools/testing/kunit/kunit.py run --kunitconfig drivers/dma-buf/.kunitconfig:
- UML, strict mode (the .kunitconfig as it is): 56 passed, 1 skipped
- UML, --kconfig_add CONFIG_DMABUF_DEBUG_WARN=y: 56 passed, 1 skipped
- UML, --kconfig_add CONFIG_DMABUF_DEBUG=n: 56 passed, 1 skipped
- x86_64 in QEMU (--arch x86_64), strict mode: 56 passed, 1 skipped
- x86_64 in QEMU, DMABUF_DEBUG_WARN=y: 56 passed, 1 skipped
- x86_64 in QEMU with KASAN, both modes: 56 passed, 1 skipped, no KASAN report
The skipped test is test_race_signal_callback, which needs 2 CPUs. UML has no NEED_SG_DMA_LENGTH, x86_64 has it.
In warn mode the test prints one report. On x86_64:
DMA-BUF: importer used the CPU side of an exporter's sg_table [...]
Call Trace:
<TASK>
dump_stack_lvl+0x2f/0x50
check_warn+0x113/0x4b0 test_debug_sg_table+0x2c4/0x740 kunit_try_run_case+0x8e/0x120 kunit_generic_run_threadfn_adapter+0x1c/0x40 kthread+0xc5/0x100
The test also builds as a module on x86_64 (DMABUF_KUNIT_TEST=m).
drivers/dma-buf/.kunitconfig | 1 + drivers/dma-buf/Makefile | 1 + drivers/dma-buf/st-dma-buf.c | 253 +++++++++++++++++++++++++++++++++++ 3 files changed, 255 insertions(+) create mode 100644 drivers/dma-buf/st-dma-buf.c
drivers/dma-buf/.kunitconfig +1 -0
@@ -1,2 +1,3 @@ CONFIG_KUNIT=y CONFIG_DMABUF_KUNIT_TEST=y +CONFIG_DMABUF_DEBUG=y
drivers/dma-buf/Makefile +1 -0
@@ -8,6 +8,7 @@ obj-$(CONFIG_SW_SYNC) += sw_sync.o sync_debug.o obj-$(CONFIG_UDMABUF) += udmabuf.o dmabuf_kunit-y := \ + st-dma-buf.o \ st-dma-fence.o \ st-dma-fence-chain.o \ st-dma-fence-unwrap.o \
drivers/dma-buf/st-dma-buf.c new file +253 -0
253 lines not on this page · raw message
Signature · 1 line
2.53.0
Quoted 13 lines · Karl Mehltretter
With
DMABUF_DEBUG,dma_buf_map_attachment()gives the importer a copy of the exporter'ssg_tablewithout the struct pages and offsets, and without the lengths wheresg_dma_len()is a separate field. An importer that uses those fields stops working. That is the point of the option, but the failure usually shows up somewhere else and says nothing about the cause.Commit
143755bdabaa9("dma-buf: MakeDMABUF_DEBUGdefault to y onDEBUG_KERNELkernels"), which I wrote, went into v7.3-rc4. It made the default from commit646013f513f3("dma-buf: enableDMABUF_DEBUGby default on DEBUG kernels") take effect, and distribution configs usually have DEBUG_KERNEL=y. Two reports followed:
Quoted 8 lines
- Jianfeng Liu: hardware video decode with drm/msm ends in GPU translation faults [1]. He then sent a revert [2].
- Diederik de Haas: rockchip fails to import buffers when playing video, on a config based on Debian's [3].
In [2] Rob Clark said the problem goes beyond msm and cannot be fixed quickly,
What's the status here given that 7.3 will likely be released in a few days? Note, I can see from the discussion that this is a complex problem that can't be quickly solved. That's how it is sometimes. But I wonder if the current state of things is the best compromise for now or if Rob's revert or some other option would be better.
Ciao, Thorsten
Quoted 96 lines
and Christian König offered to set the default to N for another few months [4]. I listed more importers that look affected and mentioned this RFC in [5]. As of v7.3-rc6 the default is unchanged.
This series adds
DMABUF_DEBUG_WARNas a sub-option ofDMABUF_DEBUG. The importer still gets a copy, but one that keeps the CPU side of the exporter's table. The entries are marked with a new bit indma_flags.sg_page(),sg_nents_for_len()andsg_split()print a rate limited message with a stack trace when they see the bit. These CPU-side accesses can continue after the report instead of failing because the fields were cleared. The report is not aWARN(), so it does not taint and does not triggerpanic_on_warn. Strict mode remains the default and continues to remove the CPU-side fields.Known limits:
- Only access through
sg_page(),sg_nents_for_len()andsg_split()is seen.sg_page()coverssg_phys(),sg_virt()and the page iterators. An importer that readssg->lengthorsg->offsetdirectly is not noticed.- When the option is enabled, every
sg_page()tests the flag.- It selects
NEED_SG_DMA_FLAGS, which addsdma_flagsand may increase the size of struct scatterlist. It also relies ondma_flagsbeing initialised, as the DMA mapping code already does.- It finds importers. It does not fix them.
Question for the maintainers: could warn mode be what
DEBUG_KERNELkernels get by default, with strict mode kept for CI? Or is an opt-in sub-option all that is wanted, if anything? The series does not change any default.Testing, on the commits as posted:
- KUnit, the dma-buf suites, under UML and on
x86_64in QEMU, in strict and in warn mode: 56 passed, 1 skipped (it needs 2 CPUs) each time.- Builds:
x86_64, arm64 and ARM926 with DMABUF_DEBUG_WARN=y, ARM926 withoutDMABUF_DEBUG. WithoutDMABUF_DEBUGthe generated code of dma-buf.o andlib/scatterlist.ois the same as before, except for line numbers.- IIO DMABUF capture on a Zynq in QEMU, with local device models. The IIO dmaengine buffer calls
sg_nents_for_len()on the attachment's table. In strict mode withNEED_SG_DMA_LENGTHthe capture fails with-EBUSY. In warn mode it works, the kernel is not tainted, and the log has:DMA-BUF: importer used the CPU side of an exporter's
sg_tableCPU: 0 UID: 0 PID: 48 Comm: iio-dmabuf Not tainted 7.3.0-rc4+ #2 VOLUNTARY Call trace: [...] dump_stack_lvl from sg_nents_for_len+0xd8/0xe4 sg_nents_for_len from iio_dmaengine_buffer_submit_block+0x4c/0x33c iio_dmaengine_buffer_submit_block from iio_dma_buffer_submit_block.part.0+0x5c/0x104 iio_dma_buffer_submit_block.part.0 from iio_dma_buffer_enqueue_dmabuf+0x68/0xa0 iio_dma_buffer_enqueue_dmabuf from iio_buffer_chrdev_ioctl+0x520/0x9a4 iio_buffer_chrdev_ioctl from sys_ioctl+0x460/0x914- sur40 behind xHCI and intel-iommu on
x86_64in QEMU, with a one-line test-only change in sur40. Heresg_page()is called iniommu_dma_map_sg(), and the trace leads back tosur40_poll(). The capture itself did not finish in that setup.Details are in the notes on the patches. Not tested on hardware.
Based on v7.3-rc4-70-gfe2ec83746e5.
An LLM agent helped with the code and the testing.
[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]
[3] https://lists.freedesktop.org/archives/dri-devel/2026-September/600904.html
[4] https://lore.kernel.org/r/[email protected]
[5] https://lore.kernel.org/r/[email protected]Karl Mehltretter (3):
dma-buf: keep the DMA flags in theDMABUF_DEBUGcopy
dma-buf: add a warn-only mode toDMABUF_DEBUG
dma-buf: test the debug scatterlist wrapperdrivers/dma-buf/.kunitconfig | 1 + drivers/dma-buf/Kconfig | 23 ++++ drivers/dma-buf/Makefile | 1 + drivers/dma-buf/dma-buf.c | 48 ++++++- drivers/dma-buf/st-dma-buf.c | 253 +++++++++++++++++++++++++++++++++++ include/linux/scatterlist.h | 26 +++- lib/scatterlist.c | 22 +++ lib/sg_split.c | 2 + 8 files changed, 372 insertions(+), 4 deletions(-) create mode 100644 drivers/dma-buf/st-dma-buf.cbase-commit:
fe2ec83746e501645709761605c2464a44fd2929
Quoted 7 lines · Thorsten Leemhuis
What's the status here given that 7.3 will likely be released in a few days? Note, I can see from the discussion that this is a complex problem that can't be quickly solved. That's how it is sometimes. But I wonder if the current state of things is the best compromise for now or if Rob's revert or some other option would be better.
I think reverting my commit 143755bdabaa ("dma-buf: Make DMABUF_DEBUG default to y on DEBUG_KERNEL kernels") is the best interim solution for v7.3.
Christian, Sumit, could one of you apply Jianfeng's revert before the final release?
https://lore.kernel.org/r/[email protected]/
Thanks,
Karl
Sumit Semwal
maintainerHi Karl,
Quoted 9 lines · Karl Mehltretter,
Quoted 7 lines · Thorsten Leemhuis
What's the status here given that 7.3 will likely be released in a few days? Note, I can see from the discussion that this is a complex problem that can't be quickly solved. That's how it is sometimes. But I wonder if the current state of things is the best compromise for now or if Rob's revert or some other option would be better.
Quoted 10 lines
I think reverting my commit
143755bdabaa("dma-buf: MakeDMABUF_DEBUGdefault to y onDEBUG_KERNELkernels") is the best interim solution for v7.3.Christian, Sumit, could one of you apply Jianfeng's revert before the final release?
Ok, I'll try and revert it for 7.3. We may need to reapply it for the next cycle along with the solution Rob and folks are working on.
Thanks,
Quoted 2 lines
Karl
Best,
Sumit.
Quoted 1 line
Sumit Semwal
maintainerHi Karl,
Quoted 14 lines · Karl Mehltretter
Quoted 7 lines · Thorsten Leemhuis
What's the status here given that 7.3 will likely be released in a few days? Note, I can see from the discussion that this is a complex problem that can't be quickly solved. That's how it is sometimes. But I wonder if the current state of things is the best compromise for now or if Rob's revert or some other option would be better.
I think reverting my commit
143755bdabaa("dma-buf: MakeDMABUF_DEBUGdefault to y onDEBUG_KERNELkernels") is the best interim solution for v7.3.
Quoted 7 lines
Christian, Sumit, could one of you apply Jianfeng's revert before the final release?
https://lore.kernel.org/r/[email protected]/
Thanks,
Karl
I have pushed the revert to drm-misc-fixes; I'll request Dave to push it out to Linus along with drm-fixes.
We should revisit this for the next cycle in a couple of weeks for sure.
Best,
Sumit.