Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753396AbdFMOjw (ORCPT ); Tue, 13 Jun 2017 10:39:52 -0400 Received: from iolanthe.rowland.org ([192.131.102.54]:47442 "HELO iolanthe.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1752078AbdFMOjv (ORCPT ); Tue, 13 Jun 2017 10:39:51 -0400 Date: Tue, 13 Jun 2017 10:39:49 -0400 (EDT) From: Alan Stern X-X-Sender: stern@iolanthe.rowland.org To: Andrey Konovalov cc: Felipe Balbi , Greg Kroah-Hartman , Kees Cook , Andrew Morton , Elena Reshetova , USB list , LKML , syzkaller , Dmitry Vyukov , Kostya Serebryany Subject: Re: gadgetfs: how to wait for USB device initialization? In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1470 Lines: 41 On Tue, 13 Jun 2017, Andrey Konovalov wrote: > Hi! > > I'm trying to use gadgetfs to fuzz USB device drivers by simply > connecting random devices for now. > > What I want to achieve right now is the following: > > 1. mount gadgetfs > 2. emulate connection of a new USB device > 3. wait for the device to finish initializing > 4. unmount gadgetfs > 5. goto 1 > > The question is how do I wait for the device to finish initializing > (the corresponding USB driver to finish probing?) before unmounting > gadgetfs? As I understand, when I write device description to > "/dev/gadget/dummy_udc" the initialization happens asynchronously > after writing is done. The most generic approach is to monitor the system log and wait for the appropriate messages to show up. If you know a little more about the device (such as which driver it will bind to), you might be able to use a udev library to do the monitoring. Or since the gadget driver in this case is part of your program, you could wait until your program sees all the USB requests that get sent during the initialization and probing procedures. For a really simple approach, just wait a fixed amount of time, like 10 seconds. Unless the system is highly loaded or probing takes a lot longer than usual, that should be enough. Alan Stern > To mount and unmount gadgetfs right now I do: > mount("none", "/dev/gadget", "gadgetfs", 0, NULL) > umount2("/dev/gadget", MNT_FORCE | MNT_DETACH) > > Thanks!