Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2629812rwd; Fri, 19 May 2023 08:10:58 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7mQl+IclD5JjHmI8koeWjwNGr14y4yrhpqNSDW2VMEvP3QEn+bwXsi0+9DgmUz3rYiy/85 X-Received: by 2002:a17:902:e74f:b0:1ae:4f8a:7d4e with SMTP id p15-20020a170902e74f00b001ae4f8a7d4emr3608658plf.24.1684509058374; Fri, 19 May 2023 08:10:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684509058; cv=none; d=google.com; s=arc-20160816; b=Y/KZv/nl7LrqU2znOfhmlBbumUBdHb645lTJZ38yr5SG/y72i2+MAK1o7HQDJiUi0B K/oCV2kDvSv6Gywz+pxGrQoBRX8RhOeaiCN6si0yh3m62+TTeV/+FgrowaLvdEQd6uR8 Gw5V/+4tsIX/p3m0KvJQ1c6u+cHiu0Oaq0rvRv2t5Dtw5ew1LCdGUxitf9VYnW7SZzTb MeqxOvo6mKdblQ6fMynKmEXyYxL/zUb9VzTWluRYRNva96P+KnYCMBwFyFBoh+18doBr GKwBvgFxOPI6pjx9HyGaS13mfklu4Iu5jj/K/Sq+axwGYtTgtFq1kNjoFXYdCBVxkYdV EHdw== 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=2yj0cEqo2t6mjce3cSwVllu9EHFJ111W9+NLEOI7v30=; b=kIInx6m1A9jz9BQFPgAK/Syd1CXPcctWBnAXZGzKiLfzIdcI4AihftZTn8zAsPcZUK 5vOwsQozTelgJeoRwUC6yRpVCZtLcziA1vFJVBCu3jkN1fPESCd2hEczje2m9zdR+egx EJBlENJMwCKkinah2nAZK2C/IwrPI974q6WMkOT0ojuLi+AF5qs82r42OVoHYH/iTKSA 3m28JKfwi5DTxGkSbQdbW4mMqqzbptaBSm8Mey+SOM+gevNp1Azic/CCEb45cwq6mcIk 1v18bNE3U2MAWu42QDuIiwyk5V8ivzN6b9zep1jF2+lXxUDeF94p0tR64LCYBgEGEJK+ ImvQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n13-20020a170902e54d00b001ae44cd96basi3980857plf.312.2023.05.19.08.10.42; Fri, 19 May 2023 08:10:58 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231792AbjESPH3 (ORCPT + 99 others); Fri, 19 May 2023 11:07:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229468AbjESPH1 (ORCPT ); Fri, 19 May 2023 11:07:27 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 5987A109 for ; Fri, 19 May 2023 08:07:26 -0700 (PDT) Received: (qmail 40772 invoked by uid 1000); 19 May 2023 11:07:25 -0400 Date: Fri, 19 May 2023 11:07:25 -0400 From: Alan Stern To: Badhri Jagan Sridharan Cc: gregkh@linuxfoundation.org, colin.i.king@gmail.com, xuetao09@huawei.com, quic_eserrao@quicinc.com, water.zhangjiantao@huawei.com, peter.chen@freescale.com, balbi@ti.com, francesco@dolcini.it, alistair@alistair23.me, stephan@gerhold.net, bagasdotme@gmail.com, luca@z3ntu.xyz, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Francesco Dolcini Subject: Re: [PATCH v2] usb: gadget: udc: core: Offload usb_udc_vbus_handler processing Message-ID: References: <20230519043041.1593578-1-badhri@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE 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, May 19, 2023 at 10:49:49AM -0400, Alan Stern wrote: > On Fri, May 19, 2023 at 04:30:41AM +0000, Badhri Jagan Sridharan wrote: > > chipidea udc calls usb_udc_vbus_handler from udc_start gadget > > ops causing a deadlock. Avoid this by offloading usb_udc_vbus_handler > > processing. > > Look, this is way overkill. > > usb_udc_vbus_handler() has only two jobs to do: set udc->vbus and call > usb_udc_connect_control(). Furthermore, it gets called from only two > drivers: chipidea and max3420. > > Why not have the callers set udc->vbus themselves and then call > usb_gadget_{dis}connect() directly? Then we could eliminate > usb_udc_vbus_handler() entirely. And the unnecessary calls -- the ones > causing deadlocks -- from within udc_start() and udc_stop() handlers can > be removed with no further consequence. > > This approach simplifies and removes code. Whereas your approach > complicates and adds code for no good reason. I changed my mind. After looking more closely, I found the comment in gadget.h about ->disconnect() callbacks happening in interrupt context. This means we cannot use a mutex to protect the associated state, and therefore the connect_lock _must_ be a spinlock, not a mutex. This also probably means that udc_start and udc_stop callbacks should not be invoked with the lock held. In fact, you might want to avoid using the lock at all with gadget_bind_driver() and gadget_unbind_driver() -- use it only in the functions that these routines call. So it appears the whole connect_lock thing needs to be redesigned with these ideas in mind. However, it's still true that the UDC drivers shouldn't try to set the connection state from within their udc_start and udc_stop callbacks, because the core takes care of this automatically. Alan Stern