linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Xu Yang <xu.yang_2@nxp.com>
To: Guenter Roeck <linux@roeck-us.net>,
	Heikki Krogerus <heikki.krogerus@linux.intel.com>
Cc: "gregkh@linuxfoundation.org" <gregkh@linuxfoundation.org>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
	Jun Li <jun.li@nxp.com>, dl-linux-imx <linux-imx@nxp.com>
Subject: Re: [PATCH] usb: typec: tcpm: fix tcpm unregister port but leave a pending timer
Date: Mon, 22 Nov 2021 05:14:49 +0000	[thread overview]
Message-ID: <DB8PR04MB6843626773FB7975E5947C738C9F9@DB8PR04MB6843.eurprd04.prod.outlook.com> (raw)

Hi,

> -----Original Message-----
> 
> On 11/18/21 5:18 AM, Heikki Krogerus wrote:
> > Hi,
> >
> > On Thu, Nov 18, 2021 at 05:23:52PM +0800, Xu Yang wrote:
> >> @@ -6428,6 +6432,9 @@ void tcpm_unregister_port(struct tcpm_port
> *port)
> >>   {
> >>      int i;
> >
> > You need to take the port lock here, no?
> >
> >          mutex_lock(&port->lock);
> >
> >> +    kthread_destroy_worker(port->wq);
> >> +    port->wq = NULL;
> >
> >          mutex_unlock(&port->lock);
> >
> 
> I don't think the timer code runs under the lock, so that won't help.
> But, yes, the code is inherently racy. At the very least, port->wq
> would have to be set to NULL first, but even that would have to be
> synchronized. I wonder how other drivers handle that situation.
> 
> Guenter
> 

Actually, the race is unlikely to happen if the hrtimer's expire time is a
little bigger. But it's better to avoid this situation from happening. So it
may be necessary to use a flag to indicate to the hrtimer the port is going
to be unregistered and it's no need to queue work anymore.

The patch maybe like following:

struct tcpm_port  *tcpm_register_port(...)
{
	...
	port->registered = true;
	return;
}

static enum hrtimer_restart  state_machine_timer_handler(timer)
{
	if (port->registered)
		kthread_queue_work(port->wq, work);
	return HRTIMER_NORESTART;
}

void tcpm_unregister_port(port)
{
	port->registered = false;
	kthread_destroy_worker(port->wq);

	hrtimer_cancel(&timer);
	...
}

What about this idea? Or I will submit it as patch V2.

Xu Yang

             reply	other threads:[~2021-11-22  5:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-22  5:14 Xu Yang [this message]
  -- strict thread matches above, loose matches on Subject: below --
2021-11-18  9:23 [PATCH] usb: typec: tcpm: fix tcpm unregister port but leave a pending timer Xu Yang
2021-11-18 13:18 ` Heikki Krogerus
2021-11-18 14:38   ` Guenter Roeck

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=DB8PR04MB6843626773FB7975E5947C738C9F9@DB8PR04MB6843.eurprd04.prod.outlook.com \
    --to=xu.yang_2@nxp.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=jun.li@nxp.com \
    --cc=linux-imx@nxp.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /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;
as well as URLs for NNTP newsgroup(s).