From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A5DA36827D for ; Sun, 15 Mar 2026 14:44:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773585878; cv=none; b=DdFeirKLODI7EX5bzgThqqRbeRAN0wt8TOKqTc7LHu7nFffRauqFXv01y+QAgh/pA57Wv/cmdYYuLbfll5AjETL55n3rHi+k36iXS7G702323wHPImThhp3samLs25qsyBXijEZU/xN6xhnqeYhOT/5B5eABqaSvQNGm1vDgVeA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773585878; c=relaxed/simple; bh=+15PsdfpJXtE+2GqEcj6TOzPq7c14GA3e2d03olf0Po=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=RRIkMXnZv+izMvsBMv7bz1VE0qWsEBHQyEOoRtos6KBh8fcDjty7DyAJW4/TAT1/i98uaUBjObwkhq6Zj9PPqbaXxnxLfsvtV/v5g9V+zqxkxmvKHixiiDIzdepGtKf27/lQ/E5+0yyK6oJ6SXKZgDiYgmYopRLPtSkNwWC15o4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rowland.harvard.edu; spf=fail smtp.mailfrom=g.harvard.edu; dkim=pass (2048-bit key) header.d=rowland.harvard.edu header.i=@rowland.harvard.edu header.b=slXTfGGW; arc=none smtp.client-ip=209.85.219.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rowland.harvard.edu Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=g.harvard.edu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rowland.harvard.edu header.i=@rowland.harvard.edu header.b="slXTfGGW" Received: by mail-qv1-f52.google.com with SMTP id 6a1803df08f44-89c4feaaeb4so2972486d6.2 for ; Sun, 15 Mar 2026 07:44:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rowland.harvard.edu; s=google; t=1773585875; x=1774190675; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=O8OKAHs7uT+2ajQgggqXPZOP0fLTZcG9HX/RZUryyHY=; b=slXTfGGWFCS1T1pmBbQdb54rNWeW16zniqfn439Fq1P5mSLCBQbH0wya4wQAibwjNH uSQmn3rE3dHAKxl1quJSE2WQOUaLVYw8I7bO23sXSZuhYwRFBnvP80MmBTu5xXhgGM9y yoigU3GECGHEOPwD48qdZzy+xkElrzEfnjh7hI70gbSuY4J8qZIad1dvCwwAIlO5kWNA lCYzitybNnZI0xq6cU2WnIWmdb04L9cRcqbh9L3EWkK+ffkyPR6aeA9Y0J/2GlVm7q26 BycgtmZ+jUkuX+4gvsa5fP1f6/dRsUhfkf8vHWySgZ6fR6JFbh7IE9u1Bqr7MC6NdnXS V38g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773585875; x=1774190675; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O8OKAHs7uT+2ajQgggqXPZOP0fLTZcG9HX/RZUryyHY=; b=Jqu/3lahUzOjrVu1nWl82LmVQpORcv9F/JnHdLdoqBkVLpddzpKGK/svhy1MKkqeIX dHxqEU1XAJ2I6BnkwtWWUgACGW6znM0WoFFc6VaZ+OW9DOnH7fLPqdH+6dgaWiZJRpuW 2RxupfVzxbSZLoyXBqe2yOyKu3pcKPw8Tr82YuA58PY+S+JB4o14p/B0icZH7762IlOd x9nIdcMObP/yD3z2/0SX0C1werE5rxPOqkh60IZPV9uN0yEMkI/lOt2O2HeIJYVRiGiy Kl6QXoUph1zAzEX9zFK4l17NYiimOJGVnbbTOmzvxVTJvcialHwrcQyYJQ58GOeyta7t /Dfw== X-Forwarded-Encrypted: i=1; AJvYcCXvk0deU8prb/csrNO54HtoTDNO08rkY/1HNwC//J86300APtTZKhRjoOyYoq9Pild8TrPmtQykElvER8A=@vger.kernel.org X-Gm-Message-State: AOJu0YxnIBbajnmql5G237X1DhFFaesgDje58dQni96pCV3SAa7eMHUe uK1ZuleCul//JFa/odbLHaC4TzpuEUAjTZhV75F+1lcHj6M77qW9s5gyqgHPO6XksNCWk+BTOQ8 vARM= X-Gm-Gg: ATEYQzwLdgeJVmh2rJYVB/MQmr/aE5y+uGK3hPLZocRcFyM0mLs6bRfapdsDHx4TatW 0rEywgz1KXONrSf5W/DMu+IeTiY4coWEzkOMtlzy534k0fVvsNGtoKf5rfnaMB9eK5Wg9ieCVZE zYig18iJqTyO+cNr6zBDTVbQHGyPksEVqTduQQ0wUwDtTjuNXNrQ/VR8V1bXIgMGeGgkvOc+N5U 8RgarALvto4EBNxauH9N+iykt8UgCa/mcn/TgsSPKXdY+0vR3ONfkwWAH+bvW3drsLYM04GGPbU j5eHtTm7TVDRUXgEhudRvg76tSaToZpITdJMl980g4KJoAVizPp6Ffvkd9PV7GKQ72xEM15yLsB r7OuzEcPHwx2hY3/kSJ38D2Cf/1veREjAeRgt5yWvtW3itczaUNwD+upq+UsRxHiQrbfKCox/Fz lyNWXIl4WO8aa1oIz5viV8ryw8 X-Received: by 2002:a05:6214:76b:b0:89a:6142:377a with SMTP id 6a1803df08f44-89a81c76c42mr139201406d6.2.1773585875264; Sun, 15 Mar 2026 07:44:35 -0700 (PDT) Received: from rowland.harvard.edu ([2601:19b:d01:d210::687c]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-89a6947c4b8sm97506126d6.20.2026.03.15.07.44.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Mar 2026 07:44:34 -0700 (PDT) Date: Sun, 15 Mar 2026 10:44:31 -0400 From: Alan Stern To: Sebastian Urban Cc: gregkh@linuxfoundation.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] usb: gadget: dummy_hcd: fix data corruption with queued requests Message-ID: <9f2c403b-5955-4e6b-a356-330cebead7ed@rowland.harvard.edu> References: <20260315090307.304644-1-surban@surban.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260315090307.304644-1-surban@surban.net> On Sun, Mar 15, 2026 at 10:03:07AM +0100, Sebastian Urban wrote: > The transfer() function in dummy_hcd iterates over all queued gadget > requests for a given endpoint via list_for_each_entry(). When the > per-frame bandwidth budget is exhausted mid-request, leaving a > partially-transferred gadget request, the loop continues to the next > queued request instead of stopping. Why do you say this? Did you not see the code (at line 1445 approximately): if (unlikely(len == 0)) is_short = 1; else { /* not enough bandwidth left? */ if (limit < ep->ep.maxpacket && limit < len) break; It does break out of the loop when there is not enough space remaining in the bandwidth budget for another transaction. But it does so at the start of the iteration following the last allowed transfer, rather than at the end of last transfer's iteration (as your patch does). Furthermore, this test can't be removed, as there might not be enough space remaining for even a single transaction. > This breaks data ordering in the > URB transfer buffer. > > Two consequences: > > 1. Data corruption: bytes from subsequent requests are written into > the URB buffer ahead of the remaining bytes from the incomplete > request. On the next timer tick the incomplete request resumes, > appending its remaining data after the out-of-order bytes. > > 2. Premature URB completion: if the next request is a ZLP or shorter > than the remaining host buffer, it triggers the is_short path and > completes the URB before all data has been transferred. > > Fix this by breaking out of the loop when the current request has > remaining data (req->req.actual < req->req.length). The request > resumes on the next timer tick, preserving data ordering. As far as I can tell, this change is not necessary. While it would avoid executing a few extra statements in the unlikely case where a request spans multiple URBs, in most situations it just adds a redundant test. Alan Stern > Signed-off-by: Sebastian Urban > --- > drivers/usb/gadget/udc/dummy_hcd.c | 6 ++++++ > 1 file changed, 6 insertions(+) > > diff --git a/drivers/usb/gadget/udc/dummy_hcd.c b/drivers/usb/gadget/udc/dummy_hcd.c > index 1cefca660..0eead4a85 100644 > --- a/drivers/usb/gadget/udc/dummy_hcd.c > +++ b/drivers/usb/gadget/udc/dummy_hcd.c > @@ -1534,6 +1534,12 @@ static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb, > /* rescan to continue with any other queued i/o */ > if (rescan) > goto top; > + > + /* request not fully transferred; stop iterating to > + * preserve data ordering across queued requests. > + */ > + if (req->req.actual < req->req.length) > + break; > } > return sent; > } > -- > 2.53.0 > >