linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Bruno Prémont" <bonbons-ud5FBsm0p/xEiooADzr8i9i2O/JbrIOy@public.gmane.org>
To: Dmitry Torokhov
	<dmitry.torokhov-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Cc: Jiri Kosina <jkosina-AlSwsSmVLrQ@public.gmane.org>,
	linux-input-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-usb-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-fbdev-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	"Rick L. Vinyard Jr."
	<rvinyard-qcTL/1vZYtiVc3sceRu5cw@public.gmane.org>,
	Nicu Pavel <npavel-VxACSXvuqMTQT0dZR+AlfA@public.gmane.org>,
	Oliver Neukum <oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org>,
	Jaya Kumar
	<jayakumar.lkml-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Subject: Re: [PATCH v2 1/6] hid: new driver for PicoLCD device
Date: Mon, 22 Mar 2010 12:38:40 +0100	[thread overview]
Message-ID: <20100322123840.7351cc82@neptune.home> (raw)
In-Reply-To: <20100322043508.GC31621-WlK9ik9hQGAhIp7JRqBPierSzoNAToWh@public.gmane.org>

On Sun, 21 March 2010 Dmitry Torokhov <dmitry.torokhov-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> On Sun, Mar 21, 2010 at 05:37:37PM +0100, Bruno Prémont wrote:
> > On Sat, 20 March 2010 Dmitry Torokhov <dmitry.torokhov-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > > > +	usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
> > > > +	complete_all(&data->ready);
> > > > +	INIT_COMPLETION(data->ready);
> > > 
> > > Umm, what does this do, exactly?
> > 
> > It wakes up anyone waiting on the completion and then resets the completion
> > as otherwise any future attempt to wait on it would succeed immediately.
> > 
> 
> You realize that if you re-initialize the completion right after
> signalling it there is a big chance the waiters will miss it (they do
> check completion->done flags that you reset right away.
> 
> In general completions are suited for something that happens once (a
> single request - allocated - processed - signalled) but not for
> repeating use.

Would below approach be more correct?

- move the completion to struct picolcd_pending
  so it meets the "happens once" requirement

- protect data->pending with a mutex
  (though also use spinlock to prevent race between event which
   signals the completion and picolcd_send_and_wait() around timeout)

- use the data->lock spinlock to protect multi-report requests from
  interleaving

In speudo-code this would be something like:

picolcd_send_and_wait(...)
{
	struct picolcd_pending pending;
	...
	init_completion(&pending->ready);
	aquire_mutex(data->mutex);
	spin_lock_irqsave(&data->lock, flags);
	... // prepare report
	data->pending = &pending;
	usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
	spin_unlock_irqrestore(&data->lock, flags);
	wait_for_completion_interruptible_timeout(&pending->ready, HZ*2);
	spin_lock_irqsave(&data->lock, flags);
	if (data->pending == &pending)
		data->pending = NULL;
	spin_unlock_irqrestore(&data->lock, flags);
	release_mutext(data->mutex);
	...
}

picolcd_raw_event(...)
{
	...
	spin_lock_irqsave(&data->lock, flags);
	if (data->pending) {
		// copy event data to pending
		...
		complete(&pending->ready);
	}
	spin_unlock_irqrestore(&data->lock, flags);
	...
}

In picolcd_remove() and picolcd_reset() I could then do something
similar to picolcd_raw_event() to trigger completion with no
data/error in order to skip timeout on the requester side.

Though how should I prevent races on hot-unplug?

Thanks,
Bruno
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2010-03-22 11:38 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-20 16:00 [PATCH v2 0/6] hid: new driver for PicoLCD device Bruno Prémont
     [not found] ` <20100320170014.440959a8-hY15tx4IgV39zxVx7UNMDg@public.gmane.org>
2010-03-20 16:02   ` [PATCH v2 1/6] " Bruno Prémont
2010-03-21  3:46     ` Dmitry Torokhov
     [not found]       ` <20100321034600.GE29360-WlK9ik9hQGAhIp7JRqBPierSzoNAToWh@public.gmane.org>
2010-03-21 16:37         ` Bruno Prémont
2010-03-22  4:35           ` Dmitry Torokhov
     [not found]             ` <20100322043508.GC31621-WlK9ik9hQGAhIp7JRqBPierSzoNAToWh@public.gmane.org>
2010-03-22 11:38               ` Bruno Prémont [this message]
2010-03-22  8:54           ` Jiri Kosina
2010-03-20 16:04   ` [PATCH v2 2/6] hid: add framebuffer support to " Bruno Prémont
2010-03-21  3:25     ` Dmitry Torokhov
2010-03-21  7:24     ` Jaya Kumar
2010-03-21 16:11       ` Bruno Prémont
2010-03-22  8:56         ` Jiri Kosina
2010-03-20 16:06 ` [PATCH v2 3/6] hid: add backlight " Bruno Prémont
2010-03-22  8:59   ` Jiri Kosina
2010-03-22 11:01     ` Bruno Prémont
2010-03-20 16:08 ` [PATCH v2 4/6] hid: add lcd " Bruno Prémont
2010-03-20 16:10 ` [PATCH v2 5/6] hid: add GPO (leds) " Bruno Prémont
2010-03-20 16:11 ` [PATCH v2 6/6] hid: add experimental access to PicoLCD device's EEPROM and FLASH Bruno Prémont
2010-03-21  3:08   ` Dmitry Torokhov
     [not found]     ` <20100321030802.GB29360-WlK9ik9hQGAhIp7JRqBPierSzoNAToWh@public.gmane.org>
2010-03-21 10:29       ` Bruno Prémont

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=20100322123840.7351cc82@neptune.home \
    --to=bonbons-ud5fbsm0p/xeiooadzr8i9i2o/jbrioy@public.gmane.org \
    --cc=dmitry.torokhov-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=jayakumar.lkml-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=jkosina-AlSwsSmVLrQ@public.gmane.org \
    --cc=linux-fbdev-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-input-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-usb-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=npavel-VxACSXvuqMTQT0dZR+AlfA@public.gmane.org \
    --cc=oliver-GvhC2dPhHPQdnm+yROfE0A@public.gmane.org \
    --cc=rvinyard-qcTL/1vZYtiVc3sceRu5cw@public.gmane.org \
    /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).