All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oliver Neukum <oneukum@suse.de>
To: Sergey Klyaus <Sergey.Klyaus@Tune-IT.Ru>
Cc: linux-kernel@vger.kernel.org, Alan Stern <stern@rowland.harvard.edu>
Subject: Re: Race condition between userland and USB device attachment
Date: Mon, 28 Jul 2014 11:31:21 +0200	[thread overview]
Message-ID: <1406539881.17754.17.camel@linux-fkkt.site> (raw)
In-Reply-To: <53D25EFE.6000601@Tune-IT.Ru>

On Fri, 2014-07-25 at 17:43 +0400, Sergey Klyaus wrote:
> Hello.
> 
> I am currently working on a project with Thin clients with Citrix 
> Receiver 13 for Linux and encountered interesting problem with USB 
> device redirection.
> ctxusb/ctxusbd process from Citrix Receiver are using inotify mechanism 
> to monitor /dev/bus/usb filesystem, and when device arrives, tries to 
> open it, but get ENODEV status:
> Jul 25 11:36:13 myaut-desktop ctxusbd[2664]: Failed to open device: No 
> such device
> Jul 25 11:36:13 myaut-desktop ctxusb[2751]: Failed to open device 
> 001:003 (error 19 - No such device), bad id?
> 
> It is caused by design of device_add() function: it calls 
> devtmpfs_create_node before bus_add_device. Here are sequence of events:
> 1. device_add() calls devtmpfs_create_node(). That leads to inotify 
> event that.
> 2. ctxusb is awoken because inotify event arises, and calls ctxusbd daemon.
> 3. ctxusbd daemon opens /dev/bus/usb/new-device, so usbdev_open() 
> routine is called
> 4. usbdev_open() calls  usbdev_lookup_by_devt(). Because device is not 
> yet attached to "usb bus", it returns NULL, and thus usbdev_open() 
> returns -ENODEV
> 5. Finally, device_add() calls bus_add_device(), and all subsequent 
> calls of usbdev_open() will succeed. However, ctxusb/ctxusbd already 
> reported an error and abandon device. User is unsatisfied.
> 
> I was able to reproduce that issue on Ubuntu 10.04 with 2.6.32 and 3.13 
> kernels.
> However, it only occur on uni-processor systems (!)
> 
> I see three ways to solve that issue:
> 1. Leave it to userland applications (i.e. using loop with retries and 
> timeouts). However, I feel that it is a kernel issue (application is 
> notified before device is ready).
> 2. Call bus_add_device() before devtmpfs_create_node(). Very rough, and 
> probably breaks a lot of other kernel code.
> 3. Wait in usbdev_open() until reconfiguration is finished (i.e. by 
> using some global lock between usb_new_device() and usbdev_open(), or 
> add completion and special state USB_STATE_CONNECTING to a device).

No to your third option. This is no USB problem. The issue is in the
generic code. The only clean fix is your suggestion (2)

	Regards
		Oliver



  reply	other threads:[~2014-07-28  9:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-25 13:43 Race condition between userland and USB device attachment Sergey Klyaus
2014-07-28  9:31 ` Oliver Neukum [this message]
2014-07-28 14:22   ` Alan Stern
2014-08-06 17:38     ` [PATCH] driver core: fix race with userland in device_add() Sergey Klyaus
2014-08-06 20:18       ` Alan Stern
2014-08-09 14:20         ` Greg Kroah-Hartman
2014-09-08 22:53         ` Greg Kroah-Hartman

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=1406539881.17754.17.camel@linux-fkkt.site \
    --to=oneukum@suse.de \
    --cc=Sergey.Klyaus@Tune-IT.Ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.