Received: by 2002:a05:7412:bc1a:b0:d7:7d3a:4fe2 with SMTP id ki26csp176654rdb; Fri, 18 Aug 2023 19:46:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHiSvjNXjkh92IDn4RkGRmOqHcVOz/Myascldg3+8TwT1FB21RX073IwOp5e7yTfGnGka/k X-Received: by 2002:aa7:db47:0:b0:525:69c8:6f51 with SMTP id n7-20020aa7db47000000b0052569c86f51mr627986edt.35.1692413207694; Fri, 18 Aug 2023 19:46:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692413207; cv=none; d=google.com; s=arc-20160816; b=l4e4VO5v5Rqz1WxrssEa79ZzIXASFQLz4shZ3l4ibv/1KuJeWWfTDLsAPec3Fn5xoo qnod3Mf/hfC9TtFyKTSWlobePFv/j+R2A5JWzhnse+ajgLduLk5XgkUJO+TELKAq1BAG z6ThiB0wQhDPxwgXC93TN7EXOR243EUTLfV44Ecs2u4cf5PTZv2ZGnDNFNz2kf52jfst dAqnDR/P8AcjlsSpwue+SgJSOD5zI8aVg0ksxrnrcwIOSMLGnJQiEIpVR2T+wopJoX4u 47HnogaYKTYuup9ZU8mknSN75K8GtojUapZ/LAElIFh1HI6dv1qDSvGb1mlq31dWN7JQ i+ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=XqxIX25pfWNMBR8oKRJJHtax3nu7NhOK2lG4r3i5+x8=; fh=CZ/kJV5Z2+9aeNn4fizkMkwdBslDsd2n+md1Qg3ZReI=; b=mAXq78L8nXlPySAsv93ep+E2w37K240dEo3q4TFFSDNEfyNavB8c5+imXahSNz0bgQ YUHwt6PNM3m4Yrd7QExHmU77jslwCjbOLeuiqmjuoLLFrit/f4hL9W52LHONcENPbaAj 93pULUtkxAJ2nOghcfEkk7XGf3/JCUNpn4T+dJdlwAHz9/PzQzMWb2157Vlcw7jF79D6 US8Da5eYO3e1otPDHo4fzBgros+Gor1MiAD0Z7mQgTEjH++QQgHKUw28OIB1MTbBQCdv vFt5iaIV1iG6TGlY133XxlPOY+p64YUmhxFe/i9k/mcxoz96JsKWOj2K2PnLtbRzoDgg KDJA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x4-20020aa7dac4000000b00527ccde487csi2278353eds.274.2023.08.18.19.46.23; Fri, 18 Aug 2023 19:46:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242117AbjHRXHR (ORCPT + 99 others); Fri, 18 Aug 2023 19:07:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242078AbjHRXHB (ORCPT ); Fri, 18 Aug 2023 19:07:01 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 736A212B for ; Fri, 18 Aug 2023 16:06:59 -0700 (PDT) Received: (qmail 44209 invoked by uid 1000); 18 Aug 2023 19:06:58 -0400 Date: Fri, 18 Aug 2023 19:06:58 -0400 From: Alan Stern To: Thinh Nguyen Cc: Andrey Konovalov , Felipe Balbi , Greg Kroah-Hartman , USB list , LKML Subject: Re: dwc3: unusual handling of setup requests with wLength == 0 Message-ID: <45d9ef53-e2be-4740-a93a-d36f18a49b39@rowland.harvard.edu> References: <20230818010815.4kcue67idma5yguf@synopsys.com> <20230818031045.wovf5tj2un7nwf72@synopsys.com> <20230818194922.ys26zrqc4pocqq7q@synopsys.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230818194922.ys26zrqc4pocqq7q@synopsys.com> X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Aug 18, 2023 at 07:49:27PM +0000, Thinh Nguyen wrote: > On Thu, Aug 17, 2023, Alan Stern wrote: > > On Fri, Aug 18, 2023 at 03:10:48AM +0000, Thinh Nguyen wrote: > > > I was referring to the next request. It should not be processed until > > > the first request is completed. Depending on the type of request, if > > > there's a delayed_status, the dwc3 driver will not prepare for the > > > Status stage and Setup stage (after status completion) to proceed to the > > > _next_ ->setup callback. > > > > > > My understanding from the described problem is that somehow dwc3 > > > processes the next request immediately without waiting for the raw > > > gadget preparing the data stage. > > > > Um. This is one of the design flaws I mentioned: a new SETUP packet > > arriving before the old control transfer is finished. The USB spec > > requires devices to accept the new SETUP packet and abort the old > > transfer. So in this case, processing the next request immediately is > > the right thing to do. > > No, as I've mentioned, from the gadget driver, it should not see the > next ->setup until the first control transfer completion, regardless > whether the transfer completed with error code due to abort or not. > Everything should happen sequentially from the gadget driver. This > should be handled in the dwc3 driver (though we missed a setup_pending > status check in the data phase that needs to be fixed now that I look at > it again). Actually I agree with you. When a new SETUP packet arrives before the old control transfer has finished, the UDC driver should cancel all pending requests and then invoke the ->setup() callback. (I don't think there is a standard error code for the cancelled requests; net2280 seems to use -EPROTO whereas dummy-hcd seems to use -EOVERFLOW.) > Perhaps the core design should be so that this synchronization does not > depend on the udc driver implementation. Do you mean the UDC core? I don't see how it could get involved in managing control transfers. > > One question is why Andrey is observing a new ->setup() callback > > happening so soon? The host is supposed to allow a fairly long time for > > standard control requests to complete. If the userspace component of > > the Raw Gadget takes too long to act, the transfer could time out and be > > cancelled on the host. But "too long" means several seconds -- is that > > really what's going on here? > > As I noted initially, it should not happen so I'm not sure what's really > the problem here. Granted that the setup_pending check for data phase is > missing in dwc3 driver, there should not be a problem unless the host > actually aborted a control transfer. Also, there should be no data phase > for wlength=0 even for IN direction if we go through the composite > layer. So, I doubt that's what happening here. > > Perhaps Andrey can clarify. My impression from his initial email was that something different was happening. It sounded like his ->setup() callback was invoked with wLength = 0, but before the Raw Gadget driver was ready to queue a response request, the UDC driver sent an automatic status, at which point the host sent another SETUP packet. So the gadget driver got two ->setup() callbacks in a row with no chance to do anything in between. > > > I was talking in context of 0-length transfer (albeit I forgot about the > > > special case of control OUT doesn't have 3-stage). > > > > > > If it's a vendor request 0-length transfer, without responding with > > > USB_GADGET_DELAYED_STATUS, the dwc3 will proceed with preparing the > > > status stage. > > > > This may be a holdover from the early days of the Gadget subsystem. My > > memory from back then isn't very good; I vaguely recall that the first > > UDC drivers would queue their automatic Status-stage requests if wLength > > was 0 and ->setup() returned 0 (which would explain why > > USB_GADGET_DELAYED_STATUS had to be invented). Unless I'm completely > > confused, that's not how UDC drivers are supposed to act now. I did a little checking. The USB_GADGET_DELAYED_STATUS mechanism was introduced for use by the composite framework -- not in order to make a UDC driver work properly. > For dwc3, it's been like this since the beginning that it automatically > queues the status upon host request. USB_GADGET_DELAYED_STATUS was > introduced to support scenarios where the device may need a longer time > to process the specific request (such as SET_CONFIGURATION). It's hard to be sure, but that may be the cause of Andrey's problem. He mentioned something about the Raw Gadget's ->setup() routine returning USB_GADGET_DELAYED_STATUS when the problem occurred, but I think he meant that it did this for the second callback, not the first one. Still, I recommand that dwc3 not automatically queue a Status request when wLength is 0. Gadget drivers don't expect UDC drivers to handle this for them. For example, look at the composite_setup() function in composite.c. It does this: /* respond with data transfer before status phase? */ if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) { req->length = value; req->context = cdev; req->zero = value < w_length; value = composite_ep0_queue(cdev, req, GFP_ATOMIC); Clearly it doesn't want the UDC driver to queue a request for it, no matter what wLength or value is set to. Alan Stern