Skip to contentSkip to conversation

drivers/base: fix iterator cleanup in attribute_container

PATCH In review Ayaan Mirza Baig 2 participants 6 messages Feb 26, 2025 – Mar 1, 2025 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/base: fix iterator cleanup in attribute_container

This patch addresses a long-standing FIXME in dirvers/base/attribute_container.c, where the function failed to call klist_iter_exit() before breaking out of the loop when a matching class device was found.

The code is now refactored to use a goto-based cleanup approach that ensure klist_iter_exit() is invoked exactly once regardless of the exit path.

In addition, checkpatch.pl warnings were fixed along with this change.

Tested on a minimal Debian System in QEMU with GDB attached. No regressions or errors were observed.

---
 drivers/base/attribute_container.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/base/attribute_container.c +7 -8
index b6f941a6ab69..1e7af328dd49 100644
@@ -5,7 +5,7 @@
  * Copyright (c) 2005 - James Bottomley <[email protected]>
  *
  * The basic idea here is to enable a device to be attached to an
- * aritrary numer of classes without having to allocate storage for them.
+ * aritrary number of classes without having to allocate storage for them.
  * Instead, the contained classes select the devices they need to attach
  * to via a matching function.
  */
@@ -21,7 +21,8 @@
 #include "base.h"
 
 /* This is a private structure used to tie the classdev and the
- * container .. it should never be visible outside this file */
+ * container .. it should never be visible outside this file
+ */
 struct internal_container {
 	struct klist_node node;
 	struct attribute_container *cont;
@@ -42,7 +43,6 @@ static void internal_container_klist_put(struct klist_node *n)
 	put_device(&ic->classdev);
 }
 
-
 /**
  * attribute_container_classdev_to_container - given a classdev, return the container
  *
@@ -449,7 +449,7 @@ attribute_container_remove_attrs(struct device *classdev)
 
 	if (cont->grp) {
 		sysfs_remove_group(&classdev->kobj, cont->grp);
-		return ;
+		return;
 	}
 
 	for (i = 0; attrs[i]; i++)
@@ -491,12 +491,11 @@ attribute_container_find_class_device(struct attribute_container *cont,
 	klist_for_each_entry(ic, &cont->containers, node, &iter) {
 		if (ic->classdev.parent == dev) {
 			cdev = &ic->classdev;
-			/* FIXME: must exit iterator then break */
-			klist_iter_exit(&iter);
-			break;
+			goto out;
 		}
 	}
-
+out:
+	klist_iter_exit(&iter);
 	return cdev;
 }
 EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
Signature · 1 line
2.47.1
maintainer
#1 lore raw
Quoted 24 lines · Ayaan Mirza Baig

This patch addresses a long-standing FIXME in dirvers/base/attribute_container.c, where the function failed to call klist_iter_exit() before breaking out of the loop when a matching class device was found.

The code is now refactored to use a goto-based cleanup approach that ensure klist_iter_exit() is invoked exactly once regardless of the exit path.

In addition, checkpatch.pl warnings were fixed along with this change.

Tested on a minimal Debian System in QEMU with GDB attached. No regressions or errors were observed.

---
 drivers/base/attribute_container.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/base/attribute_container.c +0 -0
index b6f941a6ab69..1e7af328dd49 100644

drivers/base/attribute_container.c line 8

Quoted 5 lines
drivers/base/attribute_container.c +1 -1
@@ -5,7 +5,7 @@
  * Copyright (c) 2005 - James Bottomley <[email protected]>
  *
  * The basic idea here is to enable a device to be attached to an
- * aritrary numer of classes without having to allocate storage for them.
+ * aritrary number of classes without having to allocate storage for them.

Nit, why change this in this commit?

drivers/base/attribute_container.c line 25

Quoted 10 lines
drivers/base/attribute_container.c +2 -1
  * Instead, the contained classes select the devices they need to attach
  * to via a matching function.
  */
@@ -21,7 +21,8 @@
 #include "base.h"
 
 /* This is a private structure used to tie the classdev and the
- * container .. it should never be visible outside this file */
+ * container .. it should never be visible outside this file
+ */

Same for this one?

drivers/base/attribute_container.c static void internal_container_klist_put(struct klist_node *n) · line 46

Quoted 9 lines
drivers/base/attribute_container.c +0 -1
 struct internal_container {
 	struct klist_node node;
 	struct attribute_container *cont;
@@ -42,7 +43,6 @@ static void internal_container_klist_put(struct klist_node *n)
 	put_device(&ic->classdev);
 }
 
-
 /**

And this?

I stopped reading here, please, if you are going to make logic changes, just do that, don't mix things up in one larger commit, that's not how we handle kernel patches at all.

Also, did you test a system that uses container devices?

thanks,

greg k-h

#2 lore raw

PATCH 1/2 drivers/base: fix checkpatch.pl warnings in attribute_container.c (v2)

This patch applies only cosmetic changes such as
whitespace fixes, formatting and typos. These
changes are flagged by checkpatch.pl

---
 drivers/base/attribute_container.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/base/attribute_container.c +4 -3
index b6f941a6ab69..69637b2ec3bc 100644
@@ -5,7 +5,7 @@
  * Copyright (c) 2005 - James Bottomley <[email protected]>
  *
  * The basic idea here is to enable a device to be attached to an
- * aritrary numer of classes without having to allocate storage for them.
+ * aritrary number of classes without having to allocate storage for them.
  * Instead, the contained classes select the devices they need to attach
  * to via a matching function.
  */
@@ -21,7 +21,8 @@
 #include "base.h"
 
 /* This is a private structure used to tie the classdev and the
- * container .. it should never be visible outside this file */
+ * container .. it should never be visible outside this file
+ */
 struct internal_container {
 	struct klist_node node;
 	struct attribute_container *cont;
@@ -449,7 +450,7 @@ attribute_container_remove_attrs(struct device *classdev)
 
 	if (cont->grp) {
 		sysfs_remove_group(&classdev->kobj, cont->grp);
-		return ;
+		return;
 	}
 
 	for (i = 0; attrs[i]; i++)
Signature · 1 line
2.47.1
maintainer
#3 lore raw

Re: [PATCH 1/2] drivers/base: fix checkpatch.pl warnings in attribute_container.c (v2)

Quoted 44 lines · Ayaan Mirza Baig

This patch applies only cosmetic changes such as
whitespace fixes, formatting and typos. These
changes are flagged by checkpatch.pl

---
 drivers/base/attribute_container.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/base/attribute_container.c +4 -3
index b6f941a6ab69..69637b2ec3bc 100644
@@ -5,7 +5,7 @@
  * Copyright (c) 2005 - James Bottomley <[email protected]>
  *
  * The basic idea here is to enable a device to be attached to an
- * aritrary numer of classes without having to allocate storage for them.
+ * aritrary number of classes without having to allocate storage for them.
  * Instead, the contained classes select the devices they need to attach
  * to via a matching function.
  */
@@ -21,7 +21,8 @@
 #include "base.h"
 
 /* This is a private structure used to tie the classdev and the
- * container .. it should never be visible outside this file */
+ * container .. it should never be visible outside this file
+ */
 struct internal_container {
 	struct klist_node node;
 	struct attribute_container *cont;
@@ -449,7 +450,7 @@ attribute_container_remove_attrs(struct device *classdev)
 
 	if (cont->grp) {
 		sysfs_remove_group(&classdev->kobj, cont->grp);
-		return ;
+		return;
 	}
 
 	for (i = 0; attrs[i]; i++)
Signature · 1 line
2.47.1

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.

You are receiving this message because of the following common error(s) as indicated below:

- This looks like a new version of a previously submitted patch, but you did not list below the --- line any changes from the previous version. Please read the section entitled "The canonical patch format" in the kernel file, Documentation/process/submitting-patches.rst for what needs to be done here to properly describe this.

If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.

thanks,

greg k-h's patch email bot

#4 lore raw

PATCH 2/2 drivers/base: fix iterator cleanup in attribute_container_find_class_device (v2)

In reply to Greg KH

This patch fixes a long-standing FIXME by refactoring the function to ensure klist_iter_exit() is always invoked once via a unified cleanup path

The patch has been tested thoroughly with a minimal Debain System and scsi devices and GDB.

---
 drivers/base/attribute_container.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/base/attribute_container.c +3 -4
index 69637b2ec3bc..fbf7fd45651e 100644
@@ -492,12 +492,11 @@ attribute_container_find_class_device(struct attribute_container *cont,
 	klist_for_each_entry(ic, &cont->containers, node, &iter) {
 		if (ic->classdev.parent == dev) {
 			cdev = &ic->classdev;
-			/* FIXME: must exit iterator then break */
-			klist_iter_exit(&iter);
-			break;
+			goto out;
 		}
 	}
-
+out:
+	klist_iter_exit(&iter);
 	return cdev;
 }
 EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
Signature · 1 line
2.47.1
maintainer
#5 lore raw

Re: [PATCH 2/2] drivers/base: fix iterator cleanup in attribute_container_find_class_device (v2)

Quoted 9 lines · Ayaan Mirza Baig

This patch fixes a long-standing FIXME by refactoring the function to ensure klist_iter_exit() is always invoked once via a unified cleanup path

The patch has been tested thoroughly with a minimal Debain System and scsi devices and GDB.

No need to duplicate this here, if you sign-off it's implicit that you tested it :)

Quoted 11 lines
---
 drivers/base/attribute_container.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/base/attribute_container.c +0 -0
index 69637b2ec3bc..fbf7fd45651e 100644
@@ -492,12 +492,11 @@ attribute_container_find_class_device(struct attribute_container *cont,
 	klist_for_each_entry(ic, &cont->containers, node, &iter) {
 		if (ic->classdev.parent == dev) {

drivers/base/attribute_container.c attribute_container_find_class_device(struct attribute_container *cont, · line 499

Quoted 10 lines
drivers/base/attribute_container.c +3 -4
 			cdev = &ic->classdev;
-			/* FIXME: must exit iterator then break */
-			klist_iter_exit(&iter);
-			break;
+			goto out;
 		}
 	}
-
+out:
+	klist_iter_exit(&iter);

Are you sure this actually does something different? Why is the FIXME there and why does this "simple" change solve the issue? I don't understand how this change resolves the problem.

In other words, you're going to have to document this a lot better in the changelog message please.

thanks,

greg k-h