Skip to contentSkip to conversation

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

PATCH In review Jerry Zhang 2 participants 2 messages Mar 7, 2023 – Mar 9, 2023 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: f_fs: Send a create event for endpoint files

From: Jerry Zhang <[email protected]>

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.

---
 drivers/usb/gadget/function/f_fs.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/gadget/function/f_fs.c +2 -0
index ddfc537c7526..fc038db46e14 100644
@@ -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 = {
Signature · 1 line
2.37.3
maintainer
#1 lore raw

<resend as your email headers were corrupt so only got sent to 1 person>

Quoted 59 lines · Jerry Zhang

From: Jerry Zhang <[email protected]>

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.

---
 drivers/usb/gadget/function/f_fs.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/gadget/function/f_fs.c +2 -0
index ddfc537c7526..fc038db46e14 100644
@@ -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 = {
Signature · 1 line
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