diff mbox series

[v2,1/6] sysfs: do not create empty directories if no attributes are present

Message ID 20220824135951.3604059-1-gregkh@linuxfoundation.org (mailing list archive)
State New, archived
Headers show
Series [v2,1/6] sysfs: do not create empty directories if no attributes are present | expand

Commit Message

Greg KH Aug. 24, 2022, 1:59 p.m. UTC
When creating an attribute group, if it is named a subdirectory is
created and the sysfs files are placed into that subdirectory.  If no
files are created, normally the directory would still be present, but it
would be empty.  Clean this up by removing the directory if no files
were successfully created in the group at all.

Cc: Vinod Koul <vkoul@kernel.org>
Cc: Bard Liao <yung-chuan.liao@linux.intel.com>
Cc: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Cc: Sanyog Kale <sanyog.r.kale@intel.com>
Cc: alsa-devel@alsa-project.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
v2: new patch

Note, totally untested!  The following soundwire patches will need this,
if a soundwire developer could test this out, it would be most
apreciated.

fs/sysfs/group.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

Comments

Pierre-Louis Bossart Aug. 24, 2022, 3:17 p.m. UTC | #1
On 8/24/22 15:59, Greg Kroah-Hartman wrote:
> When creating an attribute group, if it is named a subdirectory is
> created and the sysfs files are placed into that subdirectory.  If no
> files are created, normally the directory would still be present, but it
> would be empty.  Clean this up by removing the directory if no files
> were successfully created in the group at all.
> 
> Cc: Vinod Koul <vkoul@kernel.org>
> Cc: Bard Liao <yung-chuan.liao@linux.intel.com>
> Cc: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
> Cc: Sanyog Kale <sanyog.r.kale@intel.com>
> Cc: alsa-devel@alsa-project.org
> Cc: linux-kernel@vger.kernel.org
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> ---
> v2: new patch
> 
> Note, totally untested!  The following soundwire patches will need this,
> if a soundwire developer could test this out, it would be most
> apreciated.

Not able to see the kernel boot with this first patch. The device is
stuck with the cursor not even blinking. It seems our CI test devices
are also stuck.

This is completely beyond my comfort zone but I can run more tests to
root cause this.


> 
> fs/sysfs/group.c | 19 ++++++++++++++++---
>  1 file changed, 16 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c
> index eeb0e3099421..9fe0b47db47f 100644
> --- a/fs/sysfs/group.c
> +++ b/fs/sysfs/group.c
> @@ -31,12 +31,14 @@ static void remove_files(struct kernfs_node *parent,
>  			kernfs_remove_by_name(parent, (*bin_attr)->attr.name);
>  }
>  
> +/* returns -ERROR if error, or >= 0 for number of files actually created */
>  static int create_files(struct kernfs_node *parent, struct kobject *kobj,
>  			kuid_t uid, kgid_t gid,
>  			const struct attribute_group *grp, int update)
>  {
>  	struct attribute *const *attr;
>  	struct bin_attribute *const *bin_attr;
> +	int files_created = 0;
>  	int error = 0, i;
>  
>  	if (grp->attrs) {
> @@ -65,6 +67,8 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
>  						       gid, NULL);
>  			if (unlikely(error))
>  				break;
> +
> +			files_created++;
>  		}
>  		if (error) {
>  			remove_files(parent, grp);
> @@ -95,12 +99,15 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
>  							   NULL);
>  			if (error)
>  				break;
> +			files_created++;
>  		}
>  		if (error)
>  			remove_files(parent, grp);
>  	}
>  exit:
> -	return error;
> +	if (error)
> +		return error;
> +	return files_created;
>  }
>  
>  
> @@ -146,10 +153,16 @@ static int internal_create_group(struct kobject *kobj, int update,
>  		kn = kobj->sd;
>  	kernfs_get(kn);
>  	error = create_files(kn, kobj, uid, gid, grp, update);
> -	if (error) {
> +	if (error <= 0) {
> +		/*
> +		 * If an error happened _OR_ if no files were created in the
> +		 * attribute group, and we have a name for this group, delete
> +		 * the name so there's not an empty directory.
> +		 */
>  		if (grp->name)
>  			kernfs_remove(kn);
> -	}
> +	} else
> +		error = 0;
>  	kernfs_put(kn);
>  
>  	if (grp->name && update)
Greg KH Aug. 24, 2022, 3:22 p.m. UTC | #2
On Wed, Aug 24, 2022 at 05:17:44PM +0200, Pierre-Louis Bossart wrote:
> 
> 
> On 8/24/22 15:59, Greg Kroah-Hartman wrote:
> > When creating an attribute group, if it is named a subdirectory is
> > created and the sysfs files are placed into that subdirectory.  If no
> > files are created, normally the directory would still be present, but it
> > would be empty.  Clean this up by removing the directory if no files
> > were successfully created in the group at all.
> > 
> > Cc: Vinod Koul <vkoul@kernel.org>
> > Cc: Bard Liao <yung-chuan.liao@linux.intel.com>
> > Cc: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
> > Cc: Sanyog Kale <sanyog.r.kale@intel.com>
> > Cc: alsa-devel@alsa-project.org
> > Cc: linux-kernel@vger.kernel.org
> > Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > ---
> > v2: new patch
> > 
> > Note, totally untested!  The following soundwire patches will need this,
> > if a soundwire developer could test this out, it would be most
> > apreciated.
> 
> Not able to see the kernel boot with this first patch. The device is
> stuck with the cursor not even blinking. It seems our CI test devices
> are also stuck.
> 
> This is completely beyond my comfort zone but I can run more tests to
> root cause this.

Ick, ok, so much for sending out untested patches :(

I'll test and debug this tomorrow and resend a correct version, thanks
for helping out here, sorry it didn't work.

greg k-h
Greg KH Aug. 25, 2022, 7:52 a.m. UTC | #3
On Wed, Aug 24, 2022 at 05:22:37PM +0200, Greg Kroah-Hartman wrote:
> On Wed, Aug 24, 2022 at 05:17:44PM +0200, Pierre-Louis Bossart wrote:
> > 
> > 
> > On 8/24/22 15:59, Greg Kroah-Hartman wrote:
> > > When creating an attribute group, if it is named a subdirectory is
> > > created and the sysfs files are placed into that subdirectory.  If no
> > > files are created, normally the directory would still be present, but it
> > > would be empty.  Clean this up by removing the directory if no files
> > > were successfully created in the group at all.
> > > 
> > > Cc: Vinod Koul <vkoul@kernel.org>
> > > Cc: Bard Liao <yung-chuan.liao@linux.intel.com>
> > > Cc: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
> > > Cc: Sanyog Kale <sanyog.r.kale@intel.com>
> > > Cc: alsa-devel@alsa-project.org
> > > Cc: linux-kernel@vger.kernel.org
> > > Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > > ---
> > > v2: new patch
> > > 
> > > Note, totally untested!  The following soundwire patches will need this,
> > > if a soundwire developer could test this out, it would be most
> > > apreciated.
> > 
> > Not able to see the kernel boot with this first patch. The device is
> > stuck with the cursor not even blinking. It seems our CI test devices
> > are also stuck.
> > 
> > This is completely beyond my comfort zone but I can run more tests to
> > root cause this.
> 
> Ick, ok, so much for sending out untested patches :(
> 
> I'll test and debug this tomorrow and resend a correct version, thanks
> for helping out here, sorry it didn't work.

I have run out of time to work on this for this week, I'll try to pick
it up next week.  Don't worry about the soundwire changes for now, I'll
resend them when I get this all working properly.

thanks,

greg k-h
diff mbox series

Patch

diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c
index eeb0e3099421..9fe0b47db47f 100644
--- a/fs/sysfs/group.c
+++ b/fs/sysfs/group.c
@@ -31,12 +31,14 @@  static void remove_files(struct kernfs_node *parent,
 			kernfs_remove_by_name(parent, (*bin_attr)->attr.name);
 }
 
+/* returns -ERROR if error, or >= 0 for number of files actually created */
 static int create_files(struct kernfs_node *parent, struct kobject *kobj,
 			kuid_t uid, kgid_t gid,
 			const struct attribute_group *grp, int update)
 {
 	struct attribute *const *attr;
 	struct bin_attribute *const *bin_attr;
+	int files_created = 0;
 	int error = 0, i;
 
 	if (grp->attrs) {
@@ -65,6 +67,8 @@  static int create_files(struct kernfs_node *parent, struct kobject *kobj,
 						       gid, NULL);
 			if (unlikely(error))
 				break;
+
+			files_created++;
 		}
 		if (error) {
 			remove_files(parent, grp);
@@ -95,12 +99,15 @@  static int create_files(struct kernfs_node *parent, struct kobject *kobj,
 							   NULL);
 			if (error)
 				break;
+			files_created++;
 		}
 		if (error)
 			remove_files(parent, grp);
 	}
 exit:
-	return error;
+	if (error)
+		return error;
+	return files_created;
 }
 
 
@@ -146,10 +153,16 @@  static int internal_create_group(struct kobject *kobj, int update,
 		kn = kobj->sd;
 	kernfs_get(kn);
 	error = create_files(kn, kobj, uid, gid, grp, update);
-	if (error) {
+	if (error <= 0) {
+		/*
+		 * If an error happened _OR_ if no files were created in the
+		 * attribute group, and we have a name for this group, delete
+		 * the name so there's not an empty directory.
+		 */
 		if (grp->name)
 			kernfs_remove(kn);
-	}
+	} else
+		error = 0;
 	kernfs_put(kn);
 
 	if (grp->name && update)