From: Xu Yang <xu.yang_2@nxp.com>
To: gregkh@linuxfoundation.org, stern@rowland.harvard.edu,
laurent.pinchart@ideasonboard.com, hdegoede@redhat.com,
mchehab@kernel.org, jeff.johnson@oss.qualcomm.com
Cc: linux-media@vger.kernel.org, linux-usb@vger.kernel.org,
jun.li@nxp.com, imx@lists.linux.dev
Subject: [PATCH 1/2] usbmon: do dma sync for cpu read if the buffer is not dma coherent
Date: Sat, 14 Jun 2025 21:24:45 +0800 [thread overview]
Message-ID: <20250614132446.251218-1-xu.yang_2@nxp.com> (raw)
The urb->transfer_dma may not be dma coherent, in this case usb monitor
may get old data. For example, commit "20e1dbf2bbe2 media: uvcvideo:
Use dma_alloc_noncontiguous API" is allocating non-coherent buffer.
To make usbmon result more reliable, this will add a flag
URB_USBMON_NEED_SYNC to indicate that usb monitor need do dma sync
before reading buffer data.
Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
---
drivers/usb/mon/mon_main.c | 7 +++++++
include/linux/usb.h | 1 +
2 files changed, 8 insertions(+)
diff --git a/drivers/usb/mon/mon_main.c b/drivers/usb/mon/mon_main.c
index af852d53aac6..b9d5c1b46b85 100644
--- a/drivers/usb/mon/mon_main.c
+++ b/drivers/usb/mon/mon_main.c
@@ -14,6 +14,7 @@
#include <linux/slab.h>
#include <linux/notifier.h>
#include <linux/mutex.h>
+#include <linux/dma-mapping.h>
#include "usb_mon.h"
@@ -142,6 +143,12 @@ static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
{
struct mon_bus *mbus;
+ if (urb->transfer_flags & URB_USBMON_NEED_SYNC)
+ dma_sync_single_for_cpu(ubus->sysdev,
+ urb->transfer_dma,
+ urb->transfer_buffer_length,
+ DMA_FROM_DEVICE);
+
mbus = ubus->mon_bus;
if (mbus != NULL)
mon_bus_complete(mbus, urb, status);
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 1b2545b4363b..d31baee4ffa6 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -1368,6 +1368,7 @@ extern int usb_disabled(void);
#define URB_ISO_ASAP 0x0002 /* iso-only; use the first unexpired
* slot in the schedule */
#define URB_NO_TRANSFER_DMA_MAP 0x0004 /* urb->transfer_dma valid on submit */
+#define URB_USBMON_NEED_SYNC 0x0008 /* usb monitor need do dma sync for cpu read */
#define URB_ZERO_PACKET 0x0040 /* Finish bulk OUT with short packet */
#define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt
* needed */
--
2.34.1
next reply other threads:[~2025-06-14 13:22 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-14 13:24 Xu Yang [this message]
2025-06-14 13:24 ` [PATCH 2/2] media: uvcvideo: add URB_USBMON_NEED_SYNC urb flag Xu Yang
2025-06-16 13:43 ` Hans de Goede
2025-06-14 14:16 ` [PATCH 1/2] usbmon: do dma sync for cpu read if the buffer is not dma coherent Laurent Pinchart
2025-06-16 14:20 ` Alan Stern
2025-06-27 3:13 ` Xu Yang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250614132446.251218-1-xu.yang_2@nxp.com \
--to=xu.yang_2@nxp.com \
--cc=gregkh@linuxfoundation.org \
--cc=hdegoede@redhat.com \
--cc=imx@lists.linux.dev \
--cc=jeff.johnson@oss.qualcomm.com \
--cc=jun.li@nxp.com \
--cc=laurent.pinchart@ideasonboard.com \
--cc=linux-media@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=stern@rowland.harvard.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox