diff mbox series

usb: gadget: f_fs: Send a create event for endpoint files

Message ID 20230307232636.9369-1-Jerry@skydio.com
State New
Headers show
Series usb: gadget: f_fs: Send a create event for endpoint files | expand

Commit Message

Jerry Zhang March 7, 2023, 11:26 p.m. UTC
From: Jerry Zhang <jerry@skydio.com>

If userspace is creating a composite gadget with one or
more functionfs functions, it generally goes through a process of:

1. mount functionfs devices
2. start up driver processes
3. driver processes open ep0 and write descriptors
4. once all descriptors are written, enable gadget

The user may want the gadget to be enabled as soon as possible.
However there is no race-free way to know when to enable the gadget
since the gadget enable process doesn't know the status of the
other driver processes. Furthermore, other driver processes
may be fixed binaries, so there may not be an opportunity to
modify their source to use a cooperative synchronization method.

This can be resolved by enabling inotify create events for endpoint
files. The gadget enable process can create inotify watches in each
driver's directory, and a create event for "ep1" will indicate that
the driver has finished writing descriptors. Once events for all
drivers have been received, the gadget is ready to be enabled.

Signed-off-by: Jerry Zhang <jerry@skydio.com>
---
 drivers/usb/gadget/function/f_fs.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Greg Kroah-Hartman March 9, 2023, 2:22 p.m. UTC | #1
<resend as your email headers were corrupt so only got sent to 1 person>

On Tue, Mar 07, 2023 at 03:26:36PM -0800, Jerry Zhang wrote:
> From: Jerry Zhang <jerry@skydio.com>
> 
> If userspace is creating a composite gadget with one or
> more functionfs functions, it generally goes through a process of:
> 
> 1. mount functionfs devices
> 2. start up driver processes
> 3. driver processes open ep0 and write descriptors
> 4. once all descriptors are written, enable gadget
> 
> The user may want the gadget to be enabled as soon as possible.
> However there is no race-free way to know when to enable the gadget
> since the gadget enable process doesn't know the status of the
> other driver processes. Furthermore, other driver processes
> may be fixed binaries, so there may not be an opportunity to
> modify their source to use a cooperative synchronization method.
> 
> This can be resolved by enabling inotify create events for endpoint
> files. The gadget enable process can create inotify watches in each
> driver's directory, and a create event for "ep1" will indicate that
> the driver has finished writing descriptors. Once events for all
> drivers have been received, the gadget is ready to be enabled.
> 
> Signed-off-by: Jerry Zhang <jerry@skydio.com>
> ---
>  drivers/usb/gadget/function/f_fs.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
> index ddfc537c7526..fc038db46e14 100644
> --- a/drivers/usb/gadget/function/f_fs.c
> +++ b/drivers/usb/gadget/function/f_fs.c
> @@ -13,10 +13,11 @@
>  
>  /* #define DEBUG */
>  /* #define VERBOSE_DEBUG */
>  
>  #include <linux/blkdev.h>
> +#include <linux/fsnotify.h>
>  #include <linux/pagemap.h>
>  #include <linux/export.h>
>  #include <linux/fs_parser.h>
>  #include <linux/hid.h>
>  #include <linux/mm.h>
> @@ -1443,10 +1444,11 @@ static struct dentry *ffs_sb_create_file(struct super_block *sb,
>  		dput(dentry);
>  		return NULL;
>  	}
>  
>  	d_add(dentry, inode);
> +	fsnotify_create(dentry->d_parent->d_inode, dentry);
>  	return dentry;
>  }
>  
>  /* Super block */
>  static const struct super_operations ffs_sb_operations = {
> -- 
> 2.37.3
> 

This is a good idea, but where is it now documented that this is the
expected functionality and that userspace can rely on this?

thanks,

greg k-h
diff mbox series

Patch

diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index ddfc537c7526..fc038db46e14 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -13,10 +13,11 @@ 
 
 /* #define DEBUG */
 /* #define VERBOSE_DEBUG */
 
 #include <linux/blkdev.h>
+#include <linux/fsnotify.h>
 #include <linux/pagemap.h>
 #include <linux/export.h>
 #include <linux/fs_parser.h>
 #include <linux/hid.h>
 #include <linux/mm.h>
@@ -1443,10 +1444,11 @@  static struct dentry *ffs_sb_create_file(struct super_block *sb,
 		dput(dentry);
 		return NULL;
 	}
 
 	d_add(dentry, inode);
+	fsnotify_create(dentry->d_parent->d_inode, dentry);
 	return dentry;
 }
 
 /* Super block */
 static const struct super_operations ffs_sb_operations = {