Input: sur40 - fix DMA handling in video capture
Subsystems (4)
PATCH 1/1 Input: sur40 - fix DMA handling in video capture
Video capture has failed since commit 6eb0233ec2d0 ("usb: don't inherity DMA properties for USB devices"). sur40 gives the USB interface device to vb2_dma_sg, but the interface no longer has a DMA mask. The mapping fails, leaving no entries for usb_sg_init().
Using the host controller device with vb2_dma_sg would give usb_sg_init() an already DMA-mapped scatterlist. The USB core maps that list for the host controller itself. Mapping it twice overwrites the DMA addresses, and vb2 later unmaps addresses it does not own.
Imported dma-bufs cannot supply the CPU-side scatterlist that usb_sg_init() needs. Importers may use only the DMA fields of the attachment table. DMABUF_DEBUG makes this misuse deterministic by clearing its page and length fields.
Use vb2_vmalloc for capture buffers. Receive each frame into a driver-owned, page-backed scatterlist, then copy it through the vb2 mapping. Synchronize CPU writes to imported dma-bufs with dma_buf_begin_cpu_access() and dma_buf_end_cpu_access().
Verified with a custom QEMU model and raw-gadget emulation. Not tested on real hardware.
---
RFT because I do not have SUR40 hardware. Testing on a Microsoft Surface 2.0 / Samsung SUR40 would be appreciated.
The current RFT passed an x86-64 W=1 build of sur40.o with DMABUF_DEBUG=y. Runtime testing used v7.3-rc4-70-gfe2ec83746e5 on QEMU TCG with KASAN, DMA_API_DEBUG and DMABUF_DEBUG:
- a custom SUR40 model behind qemu-xhci and Intel IOMMU strict mode;
- raw-gadget on dummy_hcd.
Each setup captured 300 MMAP frames and 300 udmabuf frames. Every frame contained the expected sequence data. The xHCI/IOMMU run produced no DMA-API report. The dummy_hcd run reported udmabuf's separate maximum segment-size issue, which also reproduces through DMA_BUF_IOCTL_SYNC without sur40.
Nguyen Ngoc Thang's pending disconnect fixes move resource cleanup to a v4l2 release callback:
https://lore.kernel.org/r/[email protected]/
If that series lands first, the sgl_free() added here must move to the release callback as well.
drivers/input/touchscreen/Kconfig | 5 ++- drivers/input/touchscreen/sur40.c | 68 ++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 12 deletions(-)
drivers/input/touchscreen/Kconfig +3 -2
@@ -1276,9 +1276,10 @@ config TOUCHSCREEN_SUN4I config TOUCHSCREEN_SUR40 tristate "Samsung SUR40 (Surface 2.0/PixelSense) touchscreen" - depends on USB && MEDIA_USB_SUPPORT && HAS_DMA + depends on USB && MEDIA_USB_SUPPORT depends on VIDEO_DEV - select VIDEOBUF2_DMA_SG + select SGL_ALLOC + select VIDEOBUF2_VMALLOC help Say Y here if you want support for the Samsung SUR40 touchscreen (also known as Microsoft Surface 2.0 or Microsoft PixelSense).
drivers/input/touchscreen/sur40.c +58 -10
135 lines not on this page · raw message
base-commit: 2c3418fffa9d037b2038a6db48be63f9e2291806
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] media: use-after-free of sur40 state on disconnect when V4L2 fd is open - [Medium] usb: invalid…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] media: use-after-free of sur40 state on disconnect when V4L2 fd is open
- [Medium] usb: invalid GFP flags (0) passed to usb_sg_init in sleepable context --
commit f65bffa4ada0f7963abf3d4813cdc5924218586d
Author: Karl Mehltretter <[email protected]>
Input: sur40 - fix DMA handling in video capture
This commit fixes video capture failures by transitioning from vb2_dma_sg to vb2_vmalloc. It receives each frame into a driver-owned, page-backed scatterlist and copies it through the vb2 mapping, while synchronizing CPU writes to imported dma-bufs.
Quoted 4 lines
drivers/input/touchscreen/sur40.c +0
-0
[ ... ]
Quoted 7 lines
drivers/input/touchscreen/sur40.c +2
-1@@ -579,11 +584,17 @@ static void sur40_process_video(struct sur40_state *sur40)dev_dbg (sur40->dev,"header acquired\n" ); - sgt =vb2_dma_sg_plane_desc (&new_buf->vb.vb2_buf,0 ); + vaddr =vb2_plane_vaddr (&new_buf->vb.vb2_buf,0 ); +if (!vaddr)
drivers/input/touchscreen/sur40.c static void sur40_process_video(struct sur40_state *sur40) · line 597
Quoted 10 lines
drivers/input/touchscreen/sur40.c +6
-1+ goto err_poll; +/* +* vb2_plane_vaddr() may return a vmalloc or vmap address. Receive +* into page-backed memory so the USB core can map it for DMA. +*/ result =usb_sg_init (&sgr, sur40->usbdev,usb_rcvbulkpipe (sur40->usbdev,VIDEO_ENDPOINT ),0 , - sgt->sgl, sgt->nents, sur40->pix_fmt.sizeimage,0 ); + sur40->video_sgl, sur40->video_nents, size,0 );
[Severity: Medium]
Is it correct to pass 0 as the mem_flags argument to usb_sg_init() here?
Passing 0 implies no reclaim and no wait (equivalent to GFP_NOWAIT), which restricts usb_alloc_urb() and internal kmalloc operations. This can increase the probability of spurious -ENOMEM failures under memory pressure.
Since sur40_process_video() executes inside the input poller worker thread (sur40_poll) which runs in process context and is allowed to sleep (it already calls usb_bulk_msg() and usb_sg_wait()), shouldn't this use GFP_KERNEL instead?
[ ... ]
drivers/input/touchscreen/sur40.c static void sur40_disconnect(struct usb_interface *interface) · line 888
Quoted 7 lines
drivers/input/touchscreen/sur40.c +1
-0@@ -831,6 +883,7 @@ static void sur40_disconnect(struct usb_interface *interface)video_unregister_device (&sur40->vdev);v4l2_device_unregister (&sur40->v4l2); +sgl_free (sur40->video_sgl);kfree (sur40->bulk_in_buffer);kfree (sur40);
[Severity: High]
Could freeing the sur40 structure unconditionally here lead to a use-after-free if userspace holds the V4L2 file descriptor open during device unplug?
When the application later closes the file, the V4L2 core invokes vb2_fop_release(), which dereferences vdev->queue and attempts to acquire vdev->queue->lock. Because vdev is embedded in the freed sur40 structure, this results in a direct use-after-free.
While the commit message acknowledges this bug and defers to a pending patch series for the disconnect fixes, the current patch adds new cleanup to this vulnerable path. The referenced series is absent from the provided tree, leaving the vulnerability exposed.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Karl,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 2c3418fffa9d037b2038a6db48be63f9e2291806]
url: https://github.com/intel-lab-lkp/linux/commits/Karl-Mehltretter/Input-sur40-fix-DMA-handling-in-video-capture/20261007-074430 base: 2c3418fffa9d037b2038a6db48be63f9e2291806 patch link: https://lore.kernel.org/r/20261007054430.40155-1-kmehltretter%40gmail.com patch subject: [RFT PATCH] Input: sur40 - fix DMA handling in video capture config: arm64-randconfig-1003-20261007 (https://download.01.org/0day-ci/archive/20261008/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261008/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>, old ones prefixed by <<):
>> WARNING: modpost: drivers/input/touchscreen/sur40.ko: module uses symbol 'dma_buf_begin_cpu_access' from namespace 'DMA_BUF', but does not import it. >> WARNING: modpost: drivers/input/touchscreen/sur40.ko: module uses symbol 'dma_buf_end_cpu_access' from namespace 'DMA_BUF', but does not import it.
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Karl,
kernel test robot noticed the following build errors:
[auto build test ERROR on 2c3418fffa9d037b2038a6db48be63f9e2291806]
url: https://github.com/intel-lab-lkp/linux/commits/Karl-Mehltretter/Input-sur40-fix-DMA-handling-in-video-capture/20261007-074430 base: 2c3418fffa9d037b2038a6db48be63f9e2291806 patch link: https://lore.kernel.org/r/20261007054430.40155-1-kmehltretter%40gmail.com patch subject: [RFT PATCH] Input: sur40 - fix DMA handling in video capture config: arm-randconfig-1003-20261009 (https://download.01.org/0day-ci/archive/20261009/[email protected]/config) compiler: arm-linux-gnueabi-gcc (GCC) 15.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261009/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>, old ones prefixed by <<):
>> ERROR: modpost: drivers/input/touchscreen/sur40.ko: module uses symbol 'dma_buf_begin_cpu_access' from namespace 'DMA_BUF', but does not import it. >> ERROR: modpost: drivers/input/touchscreen/sur40.ko: module uses symbol 'dma_buf_end_cpu_access' from namespace 'DMA_BUF', but does not import it.
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki