From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796354701; b=Cqvz1PR3YdqjJx/12jfoGINopgIQUSshvUgShPc8n3rZRznl5YVaa/5wypnFlZLDXIow zvkYsnWm19h+ic8DnWnaB5vL/pt3Y5E6iNTszkb4cMyxiuzKeacZYkxiEEREikNEfsdSN muyLR7pPEYlFxa+AKU4DRu1GK60IbzIAVVJu96d6I5IpxiQUtnYcz1/k56P2Qn7GBULfH GKoLV/AD7PvkWxWwkSpqKM8i8vMqBfsXvOs6T8PGgZh+LSoNiASGu5n+SLy3Fd/cxxoa7 t/vgXQY/EJdiorhQ+UL0EXXM084AVvywuoyh86PkyCADJfKQjMvLiY5SOY2HrJtZNJA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796354701; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=3ZqN5N1/lBb8B8X+Q3cWf7GJ6ZWsO6pZbcff9acR67U=; b=lfQNpHGXtK9i97ry4q+NMYi5+A2ikdM27paxktnpW101TdZMfCONpwn84/xqSa5GvMS0 oSJx0FTWn7I+4KO0AjYvcYoXOHqqFrY+twwif5JItq2ZtgIMpkAZKd0TJeCOY5OfT6VZJ qWL2B/OHC/Obmz/EAXKiFmjDiY0Effi82WtYFJzIuOt41f83dgKZIllHHj6id8e9UDBd/ 5QM4hAfBb1x2JgLsxd4PgLGYH5wehKQU0v3oXBemafdOzM98MII8JhcoFx93e/952o7Ee gMFKt7ipi7VKsIdoLBRAT4/QczLSC3KDdsA1PWdi4XMtEiT6EsgOFCDPsyHnxtS70FQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=V0jSIXck; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D57D8606A32C; Fri, 4 Dec 2026 03:25:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=V0jSIXck Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54FDA1F000C2; Fri, 4 Dec 2026 03:24:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796354701; bh=3ZqN5N1/lBb8B8X+Q3cWf7GJ6ZWsO6pZbcff9acR67U=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=V0jSIXckiyWc9NVKnNHG6YF8/W+XwAQ4lPU86xLmzeVa2bvlzMYRVuXip0O/nAiNh bJEzmmyHWW5EK0tp4qf3gOO99ED8kS0SoBA+yzlWam29V6p2OTUcYXRZOhZyfEs7rI M/b/8verbeQ1j2gZGPNB+2dZoEo0bw4SqP2Ef3lk= Date: Thu, 3 Dec 2026 20:01:15 +0100 From: Greg Kroah-Hartman To: Yu Zhang Cc: Neill Kapron , Aldo Ariel Panzardo , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] usb: gadget: f_ecm: serialize notification state with a spinlock Message-ID: <2026120306-enviably-corncob-662a@gregkh> References: <20261121114154.30274-1-yuz08559@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261121114154.30274-1-yuz08559@gmail.com> On Sat, Nov 21, 2026 at 09:41:54PM +1000, Yu Zhang wrote: > ecm_do_notify() guards re-entry only with an atomic_read() of > notify_count, then fills ecm->notify_req, does atomic_inc() and > usb_ep_queue(). This check-fill-inc-queue sequence is not atomic and > runs from three contexts that share no lock: ecm_notify_complete(), > ecm_open()/ecm_close() (netdev up/down via u_ether) and ecm_set_alt(). > > When the completion handler decrements notify_count to 0 and re-enters > ecm_do_notify(), a concurrent ecm_close()/ecm_set_alt() can observe > notify_count == 0 in the window between the atomic_dec() and the next > atomic_inc(), and queue the same ecm->notify_req a second time. The > request is then added to the endpoint queue twice (list_add corruption), > and notify_state is updated concurrently. > > struct f_ecm has carried a FIXME about this missing locking. The sibling > f_ncm already serializes the identical state machine with a per-instance > spinlock. Do the same in f_ecm: take ecm->lock across the is_open update > and ecm_notify() in open/close/set_alt and across the state update in > ecm_notify_complete(), dropping it only around usb_ep_queue(). > > Signed-off-by: Yu Zhang > --- > v2: > - Take ecm->lock with guard()/scoped_guard() in ecm_open(), ecm_close(), > ecm_set_alt() and ecm_notify_complete() instead of explicit > spin_lock()/spin_unlock() pairs (Greg Kroah-Hartman). The lock is > still dropped around usb_ep_queue() in ecm_do_notify(), as f_ncm does, > because the completion can run before usb_ep_queue() returns. No > change to what is protected. > - Sent as a new thread. > v1: https://lore.kernel.org/all/20261026060031.60582-1-yuz08559@gmail.com/ > > drivers/usb/gadget/function/f_ecm.c | 20 ++++++++++++++++---- > 1 file changed, 16 insertions(+), 4 deletions(-) > > diff --git a/drivers/usb/gadget/function/f_ecm.c b/drivers/usb/gadget/function/f_ecm.c > --- a/drivers/usb/gadget/function/f_ecm.c > +++ b/drivers/usb/gadget/function/f_ecm.c > @@ -59,9 +59,8 @@ struct f_ecm { > atomic_t notify_count; > bool is_open; > > - /* FIXME is_open needs some irq-ish locking > - * ... possibly the same as port.ioport > - */ > + /* protects the notification state machine */ what state machine? > + spinlock_t lock; > }; > > static inline struct f_ecm *func_to_ecm(struct usb_function *f) > @@ -414,7 +413,11 @@ static void ecm_do_notify(struct f_ecm * > event->wIndex = cpu_to_le16(ecm->ctrl_id); > > atomic_inc(&ecm->notify_count); > + > + /* ecm_notify_complete() can run before usb_ep_queue() returns */ > + spin_unlock(&ecm->lock); What locked this? Must it be grabbed before this function is called? If so, please use __must_hold() > status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC); > + spin_lock(&ecm->lock); And now it's locked again? > if (status < 0) { > atomic_dec(&ecm->notify_count); > DBG(cdev, "notify --> %d\n", status); > @@ -438,6 +441,8 @@ static void ecm_notify_complete(struct u > struct usb_composite_dev *cdev = ecm->port.func.config->cdev; > struct usb_cdc_notification *event = req->buf; > > + guard(spinlock)(&ecm->lock); > + > switch (req->status) { > case 0: > /* no fault */ > @@ -588,7 +593,8 @@ static int ecm_set_alt(struct usb_functi > * follow another (if the first is in flight), and instead > * just guarantee that a speed notification is always sent. > */ > - ecm_notify(ecm); > + scoped_guard(spinlock, &ecm->lock) > + ecm_notify(ecm); > } else > goto fail; > > @@ -653,6 +659,8 @@ static void ecm_open(struct gether *geth > > DBG(ecm->port.func.config->cdev, "%s\n", __func__); > > + guard(spinlock)(&ecm->lock); > + > ecm->is_open = true; > ecm_notify(ecm); so this function also needs __must_hold(), right?