mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Elson Serrao <quic_eserrao@quicinc.com>
To: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Cc: <gregkh@linuxfoundation.org>, <xu.yang_2@nxp.com>,
	<linux-usb@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<stable@vger.kernel.org>
Subject: Re: [PATCH] usb: roles: cache usb roles received during switch registration
Date: Wed, 5 Feb 2025 15:15:43 -0800	[thread overview]
Message-ID: <c3fca109-b9ad-40c8-8f4a-033c48a86850@quicinc.com> (raw)
In-Reply-To: <Z6DVRbmwB859RlCt@kuha.fi.intel.com>



On 2/3/2025 6:40 AM, Heikki Krogerus wrote:
> On Mon, Jan 27, 2025 at 03:07:15PM -0800, Elson Roy Serrao wrote:
>> The role switch registration and set_role() can happen in parallel as they
>> are invoked independent of each other. There is a possibility that a driver
>> might spend significant amount of time in usb_role_switch_register() API
>> due to the presence of time intensive operations like component_add()
>> which operate under common mutex. This leads to a time window after
>> allocating the switch and before setting the registered flag where the set
>> role notifications are dropped. Below timeline summarizes this behavior
>>
>> Thread1				|	Thread2
>> usb_role_switch_register()	|
>> 	|			|
>> 	---> allocate switch	|
>> 	|			|
>> 	---> component_add()	|	usb_role_switch_set_role()
>> 	|			|	|
>> 	|			|	--> Drop role notifications
>> 	|			|	    since sw->registered
>> 	|			|	    flag is not set.
>> 	|			|
>> 	--->Set registered flag.|
>>
>> To avoid this, cache the last role received and set it once the switch
>> registration is complete. Since we are now caching the roles based on
>> registered flag, protect this flag with the switch mutex.
> 
> Instead, why not just mark the switch registered from the get-go?
> 
> diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c
> index c58a12c147f4..cf38be82d397 100644
> --- a/drivers/usb/roles/class.c
> +++ b/drivers/usb/roles/class.c
> @@ -387,6 +387,8 @@ usb_role_switch_register(struct device *parent,
>         dev_set_name(&sw->dev, "%s-role-switch",
>                      desc->name ? desc->name : dev_name(parent));
>  
> +       sw->registered = true;
> +
>         ret = device_register(&sw->dev);
>         if (ret) {
>                 put_device(&sw->dev);
> @@ -399,8 +401,6 @@ usb_role_switch_register(struct device *parent,
>                         dev_warn(&sw->dev, "failed to add component\n");
>         }
>  
> -       sw->registered = true;
> -
>         /* TODO: Symlinks for the host port and the device controller. */
>  
>         return sw;
> 

Thank you for the feedback.

Yes that works as well. Wasn't entirely sure if we can set that flag from the get-go before calling device_register().
But guess we can reset the flag if device_register fails since that is the only failure path in role_switch_register().

I will upload v2 with this modification.

Best Regards,
Elson

      reply	other threads:[~2025-02-05 23:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-27 23:07 Elson Roy Serrao
2025-02-03 14:40 ` Heikki Krogerus
2025-02-05 23:15   ` Elson Serrao [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c3fca109-b9ad-40c8-8f4a-033c48a86850@quicinc.com \
    --to=quic_eserrao@quicinc.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=xu.yang_2@nxp.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®