Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1579255rwd; Wed, 31 May 2023 16:23:26 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7TerfpzLuIdTo1dVFoVUXvp5dBEQcE3c8DueCNDwPan6pe2EIA1ACTdxNH0d/2ci0+9wPz X-Received: by 2002:a05:6a20:9386:b0:105:94e5:f5c2 with SMTP id x6-20020a056a20938600b0010594e5f5c2mr6441258pzh.13.1685575406395; Wed, 31 May 2023 16:23:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685575406; cv=none; d=google.com; s=arc-20160816; b=Ssc9TnvGnZU/RawYPdQys/yqP9kejopbgTczkRUYXIajGbFDV15DjhFahjtPlBFDGV zCeFSIRTU9hCfyBWniR3QGkaT2zm7CCdIcbNa9UbqSTdRH5mbM0r5eAIFtUiNStthv9b jfPHAdV8Zl3V7Wps3xS77XxNz3ZNk9sXrjEYgbTcWSgid/A/iCV8LoOsAPWHDqkpHHfs saTqXwy1rMqOz1c9oUB3oHiJ98Z/bLBHqB74SPFUQ8zz81vXIhI90eWkG/iCDSHf5Jef 6wyMs82h2uvhM/5r2GgV4cRpi8yVUyffRqWoBvlJ6JBm9LwvbnXlEoqt498vs6UKQTS0 uQUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=mtSHrKf9FFhhMQ62tucQUCDkhjQ9Kszms7jWwtFDsZY=; b=WrOeoYAOYOPjwuRM3VM3rsSHZaKjeen8R7qhk/fXo4ESRuerFF0zscS5M6NDkIVsWo JG2qFzfdhjhdG6vVqqGkINW2nDXPNnIrPtj2Xx9HuDIf8vUsdydr2Y8ScfLYEnOAzpka wP3TAiCKJifupR9fAHmNyFpzvWy0pex1fUYntUhYikmay+qBJC46fJasoybwu5rwJeUT XdrDEQYk1YOMKdH5U429Q0IGGDK1VOEC6csY/Of+1UrIIh4+vn+h7lNrS5Egn5WgXJTg dC8ss2qhIty2fB8bFysKB/xTRaSFHfAsScliqu4p3xYI4FwYIlLm3qaJ0FcIWZYs8TZ6 GZwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=SQxesh06; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d6-20020a170902654600b001ac307c4ef4si1679244pln.301.2023.05.31.16.23.10; Wed, 31 May 2023 16:23:26 -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; dkim=pass header.i=@google.com header.s=20221208 header.b=SQxesh06; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231237AbjEaWlK (ORCPT + 99 others); Wed, 31 May 2023 18:41:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231191AbjEaWlI (ORCPT ); Wed, 31 May 2023 18:41:08 -0400 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABAE511F for ; Wed, 31 May 2023 15:41:03 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-64d2f99c8c3so205350b3a.0 for ; Wed, 31 May 2023 15:41:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685572863; x=1688164863; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=mtSHrKf9FFhhMQ62tucQUCDkhjQ9Kszms7jWwtFDsZY=; b=SQxesh06GdJOvXg5faM/LLzHy17vXsEvwVJlsmnZVmT3s4Qn7IjkyprEnp1Y+lBR5l gsvYgnsqnpyyqbXSYMwIUWhYMUxBSVEiytjIk7R1MGQ6xdDUM0O7Kq7cXQ8AUFGqeTcR C5R0FDs4kP1yCNsHMPwn6k5TmT9QGuMbALEXQOlfX83RGa/xu9pXCTcyYJWQd3FZtwmV SMuKD4kf8YGXK02ZCZHUThPzxNNRUnH0bGAX9ED27eykw9Ih6/Db9Dyzkc5WMjRdK4SD UFtY3GIFF5uckr7nIQmZNaEz7e/WlLgAzzxDTtdkqeI4CR5HW2FyVKLYoh8gmM7bEkxi s7lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685572863; x=1688164863; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mtSHrKf9FFhhMQ62tucQUCDkhjQ9Kszms7jWwtFDsZY=; b=MA7VxxgC8c9s7KVYzY5UPvHGhaVIPaM5+g23Q2qMRDqYRyN1YRNQAyAnjhRLv8qfyT X5iCA7aFzyyxX5q4OYbC8t1io7AtEH0E1Qf8TnbnAFlpIkJy9ElgpxxeBIRZSILTY7mu NGvjspYpmfUAcjkPjnnCYProffgDEFegzkN7wdzcPKcvdBDifKTj2S+2zq8fgnbOnlnW LNVEQFAe89HGYiR5MEDmgo9iuBNzi9bNu+96t00kH5qW2Qx6iGAjLB/RQ05Cpv+v4KtU CPbhDnEHzsc4MoR+XQ/y+QiBCrkTLOlHrWf6gUU7ObIf4Gjz5Y4hmVerF72JDlWfJpf/ ByXg== X-Gm-Message-State: AC+VfDxoVMQShypVcC5tdgCSG2vcBfSVhaiM1ZGzmLqst3tsV5YUm6g9 mkTdfjXDZrgdMBdVBa2dxq71UH7AmOFGZP87Q+qdxQ== X-Received: by 2002:a05:6a21:100f:b0:10b:8024:d253 with SMTP id nk15-20020a056a21100f00b0010b8024d253mr4758297pzb.26.1685572862969; Wed, 31 May 2023 15:41:02 -0700 (PDT) MIME-Version: 1.0 References: <20230531040203.19295-1-badhri@google.com> <20230531040203.19295-2-badhri@google.com> <618e4f17-2799-4838-a21c-184c9303bef6@rowland.harvard.edu> In-Reply-To: <618e4f17-2799-4838-a21c-184c9303bef6@rowland.harvard.edu> From: Badhri Jagan Sridharan Date: Wed, 31 May 2023 15:40:26 -0700 Message-ID: Subject: Re: [PATCH v5 2/3] usb: gadget: udc: core: Invoke usb_gadget_connect only when started To: Alan Stern Cc: gregkh@linuxfoundation.org, colin.i.king@gmail.com, xuetao09@huawei.com, quic_eserrao@quicinc.com, water.zhangjiantao@huawei.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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,ENV_AND_HDR_SPF_MATCH, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 Wed, May 31, 2023 at 10:55=E2=80=AFAM Alan Stern wrote: > > On Wed, May 31, 2023 at 04:02:02AM +0000, Badhri Jagan Sridharan wrote: > > usb_udc_connect_control() does not check to see if the udc has already > > been started. This causes gadget->ops->pullup to be called through > > usb_gadget_connect() when invoked from usb_udc_vbus_handler() even > > before usb_gadget_udc_start() is called. Guard this by checking for > > udc->started in usb_udc_connect_control() before invoking > > usb_gadget_connect(). > > After a merged version of patches 1/3 and 3/3 have been applied, it > seems like most of this will not be needed any more. Maybe not any of > it. Without the connect_lock introduced in this patch, wouldn't the usb_gadget_connect()/ usb_gadget_disconnect() through soft_connect_store() race against usb_gadget_connect()/ usb_gadget_disconnect() through usb_udc_connect_control() ? On a side note, I am working on merging patches 1/3 and 3/3. Thanks, Badhri > > usb_udc_connect_control() gets called from only two places. One of them > is in gadget_bind_driver(), where we know that the UDC has been started > and connecting is allowed. The other place is the vbus work routine > queued by usb_udc_vbus_handler(). If that place checks the new > allow_connect flag before calling usb_gadget_connect(), nothing more > will be needed. You just have to make sure that the allow_connect flag > is set in gadget_bind_driver between the start and connect_control > calls, and it is cleared in gadget_unbind_driver before the > cancel_work_sync call. > > It's possible that a new mutex will be needed to synchronize accesses to > the allow_connect flag. That's something you will have to study and > decide on. But if you can avoid adding one, that would be best. > > > Guarding udc->vbus, udc->started, gadget->connect, gadget->deactivate > > related functions with connect_lock. usb_gadget_connect_locked(), > > usb_gadget_disconnect_locked(), usb_udc_connect_control_locked(), > > usb_gadget_udc_start_locked(), usb_gadget_udc_stop_locked() are called > > with this lock held as they can be simulataneously invoked from > > different code paths. > > It's a general principle of kernel programming that locks protect data, > not code. So if this patch were to be accepted, you would have to > change this description to say that connect_lock guards various flags, > not various function calls. > > Alan Stern