driver core: Introduce device_find_first_child() helper
Subsystems (2)
PATCH 1/2 driver core: Introduce device_find_first_child() helper
There are several places in the kernel where this kind of functionality is being used. Provide a generic helper for such cases.
--- drivers/base/core.c | 24 ++++++++++++++++++++++++ include/linux/device.h | 1 + 2 files changed, 25 insertions(+)
drivers/base/core.c +24 -0
@@ -3832,6 +3832,30 @@ struct device *device_find_child_by_name(struct device *parent, }EXPORT_SYMBOL_GPL (device_find_child_by_name); +/** +* device_find_first_child - device iterator for locating the fist child device. +* @parent: parent struct device +* +* This is similar to the device_find_child() function above, but it +* returns a reference to the first child device. +* +* NOTE: you will need to drop the reference with put_device() after use. +*/ +struct device *device_find_first_child (struct device *parent) +{ +struct klist_iter i; +struct device *child; + +if (!parent) +return NULL ; + +klist_iter_init (&parent->p->klist_children, &i); + child =get_device (next_device (&i)); +klist_iter_exit (&i); +return child; +} +EXPORT_SYMBOL_GPL (device_find_first_child); +int __init devices_init (void ) { devices_kset =kset_create_and_add ("devices" , &device_uevent_ops,NULL );
include/linux/device.h +1 -0
@@ -905,6 +905,7 @@ struct device *device_find_child(struct device *dev, void *data, int (*match)(struct device *dev, void *data));struct device *device_find_child_by_name (struct device *parent,const char *name); +struct device *device_find_first_child (struct device *parent);int device_rename (struct device *dev,const char *new_name);int device_move (struct device *dev,struct device *new_parent,enum dpm_order dpm_order);
Signature · 1 line
2.35.1
PATCH 2/2 spi: Use device_find_first_child() instead of custom approach
We have already a helper to get the first child device, use it and drop custom approach.
--- drivers/spi/spi.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/spi/spi.c +2 -7
@@ -2613,11 +2613,6 @@ int spi_slave_abort(struct spi_device *spi) }EXPORT_SYMBOL_GPL (spi_slave_abort); -static int match_true (struct device *dev,void *data) -{ -return 1 ; -} -static ssize_t slave_show (struct device *dev,struct device_attribute *attr,char *buf) {
@@ -2625,7 +2620,7 @@ static ssize_t slave_show(struct device *dev, struct device_attribute *attr, dev);struct device *child; - child =device_find_child (&ctlr->dev,NULL , match_true); + child =device_find_first_child (&ctlr->dev);return sprintf (buf,"%s\n" , child ?to_spi_device (child)->modalias :NULL ); }
@@ -2644,7 +2639,7 @@ static ssize_t slave_store(struct device *dev, struct device_attribute *attr,if (rc !=1 || !name[0 ])return -EINVAL ; - child =device_find_child (&ctlr->dev,NULL , match_true); + child =device_find_first_child (&ctlr->dev);if (child) {/* Remove registered slave */ device_unregister (child);
Signature · 1 line
2.35.1
Re: [PATCH v1 2/2] spi: Use device_find_first_child() instead of custom approach
Quoted 5 lines · Andy Shevchenko
We have already a helper to get the first child device, use it and drop custom approach.
Signed-off-by Andy Shevchenko <[email protected]>
Nice cleanup.
Quoted 41 lines
--- drivers/spi/spi.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)drivers/spi/spi.c +2
-8@@ -2613,11 +2613,6 @@ int spi_slave_abort(struct spi_device *spi) }EXPORT_SYMBOL_GPL (spi_slave_abort); -static int match_true (struct device *dev,void *data) -{ -return 1 ; -} -static ssize_t slave_show (struct device *dev,struct device_attribute *attr,char *buf) {@@ -2625,7 +2620,7 @@ static ssize_t slave_show(struct device *dev, struct device_attribute *attr, dev);struct device *child; - child =device_find_child (&ctlr->dev,NULL , match_true); + child =device_find_first_child (&ctlr->dev);return sprintf (buf,"%s\n" , child ?to_spi_device (child)->modalias :NULL ); }@@ -2644,7 +2639,7 @@ static ssize_t slave_store(struct device *dev, struct device_attribute *attr,if (rc !=1 || !name[0 ])return -EINVAL ; - child =device_find_child (&ctlr->dev,NULL , match_true); + child =device_find_first_child (&ctlr->dev);if (child) {/* Remove registered slave */ device_unregister (child); --2.35.1
Greg Kroah-Hartman
maintainerRe: [PATCH v1 2/2] spi: Use device_find_first_child() instead of custom approach
In reply to Andy ShevchenkoQuoted 25 lines · Andy Shevchenko
We have already a helper to get the first child device, use it and drop custom approach.
Signed-off-by Andy Shevchenko <[email protected]>--- drivers/spi/spi.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)drivers/spi/spi.c +0
-5@@ -2613,11 +2613,6 @@ int spi_slave_abort(struct spi_device *spi) }EXPORT_SYMBOL_GPL (spi_slave_abort); -static int match_true (struct device *dev,void *data) -{ -return 1 ; -} -static ssize_t slave_show (struct device *dev,struct device_attribute *attr,char *buf) {
drivers/spi/spi.c static ssize_t slave_show(struct device *dev, struct device_attribute *attr, · line 2626
Quoted 8 lines
drivers/spi/spi.c +1
-1@@ -2625,7 +2620,7 @@ static ssize_t slave_show(struct device *dev, struct device_attribute *attr, dev);struct device *child; - child =device_find_child (&ctlr->dev,NULL , match_true); + child =device_find_first_child (&ctlr->dev);return sprintf (buf,"%s\n" , child ?to_spi_device (child)->modalias :NULL ); }
Horrible naming convention asside, what is this really showing? I do not see this documented in Documentation/ABI/ anywhere, so can it just be dropped entirely?
Ah, it's in Documentation/spi/spi-summary.rst not where it belongs...
Looks like "any" of the child devices could match here, so it's just finding the first one by default. So you aren't explicitly asking for the real first device, you could return the last one as well, and it would still work as there is just "one" device in this list from what I can tell.
So is does this really deserve a new driver core api call?
thanks,
greg k-h
Re: [PATCH v1 2/2] spi: Use device_find_first_child() instead of custom approach
Quoted 41 lines · Greg Kroah-Hartman
Quoted 25 lines · Andy Shevchenko
We have already a helper to get the first child device, use it and drop custom approach.
Signed-off-by Andy Shevchenko <[email protected]>--- drivers/spi/spi.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)drivers/spi/spi.c +0
-5@@ -2613,11 +2613,6 @@ int spi_slave_abort(struct spi_device *spi) }EXPORT_SYMBOL_GPL (spi_slave_abort); -static int match_true (struct device *dev,void *data) -{ -return 1 ; -} -static ssize_t slave_show (struct device *dev,struct device_attribute *attr,char *buf) {drivers/spi/spi.c static ssize_t slave_show(struct device *dev, struct device_attribute *attr, · line 2626
Quoted 8 lines
drivers/spi/spi.c +1
-1@@ -2625,7 +2620,7 @@ static ssize_t slave_show(struct device *dev, struct device_attribute *attr, dev);struct device *child; - child =device_find_child (&ctlr->dev,NULL , match_true); + child =device_find_first_child (&ctlr->dev);return sprintf (buf,"%s\n" , child ?to_spi_device (child)->modalias :NULL ); }Horrible naming convention asside, what is this really showing? I do not see this documented in
Documentation/ABI/anywhere, so can it just be dropped entirely?Ah, it's in
Documentation/spi/spi-summary.rstnot where it belongs...
Quoted 7 lines
Looks like "any" of the child devices could match here, so it's just finding the first one by default. So you aren't explicitly asking for the real first device, you could return the last one as well, and it would still work as there is just "one" device in this list from what I can tell.
So is does this really deserve a new driver core api call?
As I said I noticed more places like this (*) and the problem is that I can't simply use device_match_any() because of the different prototype.
I agree that all thing should be using _any instead of _first.
*) e.g. ptp_ocp.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Greg Kroah-Hartman
maintainerRe: [PATCH v1 2/2] spi: Use device_find_first_child() instead of custom approach
Quoted 48 lines · Andy Shevchenko
Quoted 47 lines · Greg Kroah-Hartman
Quoted 25 lines · Andy Shevchenko
We have already a helper to get the first child device, use it and drop custom approach.
Signed-off-by Andy Shevchenko <[email protected]>--- drivers/spi/spi.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)drivers/spi/spi.c +0
-5@@ -2613,11 +2613,6 @@ int spi_slave_abort(struct spi_device *spi) }EXPORT_SYMBOL_GPL (spi_slave_abort); -static int match_true (struct device *dev,void *data) -{ -return 1 ; -} -static ssize_t slave_show (struct device *dev,struct device_attribute *attr,char *buf) {drivers/spi/spi.c static ssize_t slave_show(struct device *dev, struct device_attribute *attr, · line 2626
Quoted 8 lines
drivers/spi/spi.c +1
-1@@ -2625,7 +2620,7 @@ static ssize_t slave_show(struct device *dev, struct device_attribute *attr, dev);struct device *child; - child =device_find_child (&ctlr->dev,NULL , match_true); + child =device_find_first_child (&ctlr->dev);return sprintf (buf,"%s\n" , child ?to_spi_device (child)->modalias :NULL ); }Horrible naming convention asside, what is this really showing? I do not see this documented in
Documentation/ABI/anywhere, so can it just be dropped entirely?Ah, it's in
Documentation/spi/spi-summary.rstnot where it belongs...Looks like "any" of the child devices could match here, so it's just finding the first one by default. So you aren't explicitly asking for the real first device, you could return the last one as well, and it would still work as there is just "one" device in this list from what I can tell.
Quoted 4 lines
Quoted 1 line
So is does this really deserve a new driver core api call?
As I said I noticed more places like this (*) and the problem is that I can't simply use
device_match_any()because of the different prototype.
Why not exactly? match_true() above and device_match_any() have the same signature from what I can tell:
static int match_true(struct device *dev, void *data) int device_match_any(struct device *dev, const void *unused)
What am I missing, the const?
Quoted 1 line
I agree that all thing should be using
_anyinstead of_first.
Yes, so let's fix it please, don't propagate bad patterns.
thanks,
greg k-h
Re: [PATCH v1 2/2] spi: Use device_find_first_child() instead of custom approach
Quoted 1 line · Greg Kroah-Hartman
On Wed, Jun 08, 2022 at 02:49:14PM +0300, Andy Shevchenko wrote:
...
Quoted 6 lines
Why not exactly?
match_true()above anddevice_match_any()have the same signature from what I can tell:
static intmatch_true(struct device *dev, void *data)intdevice_match_any(struct device *dev, const void *unused)What am I missing, the const?
Yep! Compiler is very unhappy about it.
Quoted 3 lines
Quoted 1 line
I agree that all thing should be using
_anyinstead of_first.Yes, so let's fix it please, don't propagate bad patterns.
Will do, thanks!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Rafael J. Wysocki
maintainerQuoted 31 lines · Andy Shevchenko
There are several places in the kernel where this kind of functionality is being used. Provide a generic helper for such cases.
Signed-off-by Andy Shevchenko <[email protected]>--- drivers/base/core.c | 24 ++++++++++++++++++++++++ include/linux/device.h | 1 + 2 files changed, 25 insertions(+)drivers/base/core.c +13
-0@@ -3832,6 +3832,30 @@ struct device *device_find_child_by_name(struct device *parent, }EXPORT_SYMBOL_GPL (device_find_child_by_name); +/** +* device_find_first_child - device iterator for locating the fist child device. +* @parent: parent struct device +* +* This is similar to the device_find_child() function above, but it +* returns a reference to the first child device. +* +* NOTE: you will need to drop the reference with put_device() after use. +*/ +struct device *device_find_first_child (struct device *parent) +{ +struct klist_iter i; +struct device *child;
drivers/base/core.c struct device *device_find_child_by_name(struct device *parent, · line 3857
Quoted 10 lines
drivers/base/core.c +10
-0+ +if (!parent) +return NULL ; + +klist_iter_init (&parent->p->klist_children, &i); + child =get_device (next_device (&i)); +klist_iter_exit (&i); +return child; +} +EXPORT_SYMBOL_GPL (device_find_first_child);
I would define it as
static int match_first(struct device *dev, void *)
{
return 1;
}struct device *device_find_first_child(struct device *parent)
{
return device_find_first_child(parent, NULL, match_first);
}EXPORT_SYMBOL_GPL(device_find_first_child);
which is not that much more overhead.
Quoted 19 lines
drivers/base/core.c +1
-0+int __init devices_init (void ) { devices_kset =kset_create_and_add ("devices" , &device_uevent_ops,NULL );include/linux/device.h +1
-1@@ -905,6 +905,7 @@ struct device *device_find_child(struct device *dev, void *data, int (*match)(struct device *dev, void *data));struct device *device_find_child_by_name (struct device *parent,const char *name); +struct device *device_find_first_child (struct device *parent);int device_rename (struct device *dev,const char *new_name);int device_move (struct device *dev,struct device *new_parent,enum dpm_order dpm_order); --2.35.1
Quoted 2 lines · Rafael J. Wysocki
On Tue, Jun 7, 2022 at 10:22 PM Andy Shevchenko
<[email protected]> wrote:
...
Quoted 7 lines
I would define it as
static int
match_first(struct device *dev, void *){ return 1; }
Quoted 7 lines
struct device *device_find_first_child(struct device *parent)
{ return device_find_first_child(parent, NULL, match_first); }
EXPORT_SYMBOL_GPL(device_find_first_child);which is not that much more overhead.
With this we actually may simply provide a match function and it will make the clean ups (like patch 2 in the series) almost the same without introducing a device core call.
Something like
int device_match_any_for_find(struct device *dev, void *unused)
{
return 1;
}As I replied to Greg it's pity we can't use device_match_any()...
Signature · 2 lines
With Best Regards, Andy Shevchenko
Greg Kroah-Hartman
maintainerQuoted 25 lines · Andy Shevchenko
Quoted 2 lines · Rafael J. Wysocki
On Tue, Jun 7, 2022 at 10:22 PM Andy Shevchenko
<[email protected]> wrote:...
Quoted 7 lines
I would define it as
static int
match_first(struct device *dev, void *){ return 1; }Quoted 7 lines
struct device *device_find_first_child(struct device *parent)
{ return device_find_first_child(parent, NULL, match_first); }
EXPORT_SYMBOL_GPL(device_find_first_child);which is not that much more overhead.
With this we actually may simply provide a match function and it will make the clean ups (like patch 2 in the series) almost the same without introducing a device core call.
Quoted 8 lines
Something like
int
device_match_any_for_find(struct device *dev, void *unused){ return 1; }As I replied to Greg it's pity we can't use
device_match_any()...
int device_match_any(struct device *dev, const void *unused)
How is that not ok to use here?
thanks,
greg k-h
Rafael J. Wysocki
maintainerQuoted 34 lines · Greg Kroah-Hartman
Quoted 32 lines · Andy Shevchenko
Quoted 2 lines · Rafael J. Wysocki
On Tue, Jun 7, 2022 at 10:22 PM Andy Shevchenko
<[email protected]> wrote:...
Quoted 7 lines
I would define it as
static int
match_first(struct device *dev, void *){ return 1; }Quoted 7 lines
struct device *device_find_first_child(struct device *parent)
{ return device_find_first_child(parent, NULL, match_first); }
EXPORT_SYMBOL_GPL(device_find_first_child);which is not that much more overhead.
With this we actually may simply provide a match function and it will make the clean ups (like patch 2 in the series) almost the same without introducing a device core call.
Something like
int
device_match_any_for_find(struct device *dev, void *unused){ return 1; }
Quoted 5 lines
Quoted 1 line
As I replied to Greg it's pity we can't use
device_match_any()...int
device_match_any(struct device *dev, const void *unused)How is that not ok to use here?
Because of the const that will be frowned upon by the compiler.
We need to define another device_match_any_relaxed() taking (void *) as the second argument for this.
Greg Kroah-Hartman
maintainerQuoted 38 lines · Rafael J. Wysocki
Quoted 36 lines · Greg Kroah-Hartman
Quoted 33 lines · Andy Shevchenko
Quoted 2 lines · Rafael J. Wysocki
On Tue, Jun 7, 2022 at 10:22 PM Andy Shevchenko
<[email protected]> wrote:...
Quoted 7 lines
I would define it as
static int
match_first(struct device *dev, void *){ return 1; }Quoted 7 lines
struct device *device_find_first_child(struct device *parent)
{ return device_find_first_child(parent, NULL, match_first); }
EXPORT_SYMBOL_GPL(device_find_first_child);which is not that much more overhead.
With this we actually may simply provide a match function and it will make the clean ups (like patch 2 in the series) almost the same without introducing a device core call.
Something like
int
device_match_any_for_find(struct device *dev, void *unused){ return 1; }As I replied to Greg it's pity we can't use
device_match_any()...
Quoted 8 lines
Quoted 3 lines
int
device_match_any(struct device *dev, const void *unused)How is that not ok to use here?
Because of the const that will be frowned upon by the compiler.
We need to define another
device_match_any_relaxed()taking (void *) as the second argument for this.
Or we could cast it away :)
Rafael J. Wysocki
maintainerQuoted 42 lines · Greg Kroah-Hartman
Quoted 40 lines · Rafael J. Wysocki
Quoted 38 lines · Greg Kroah-Hartman
Quoted 25 lines · Andy Shevchenko
Quoted 2 lines · Rafael J. Wysocki
On Tue, Jun 7, 2022 at 10:22 PM Andy Shevchenko
<[email protected]> wrote:...
Quoted 7 lines
I would define it as
static int
match_first(struct device *dev, void *){ return 1; }Quoted 7 lines
struct device *device_find_first_child(struct device *parent)
{ return device_find_first_child(parent, NULL, match_first); }
EXPORT_SYMBOL_GPL(device_find_first_child);which is not that much more overhead.
With this we actually may simply provide a match function and it will make the clean ups (like patch 2 in the series) almost the same without introducing a device core call.
Quoted 8 lines
Something like
int
device_match_any_for_find(struct device *dev, void *unused){ return 1; }As I replied to Greg it's pity we can't use
device_match_any()...int
device_match_any(struct device *dev, const void *unused)
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
How is that not ok to use here?
Because of the const that will be frowned upon by the compiler.
We need to define another
device_match_any_relaxed()taking (void *) as the second argument for this.Or we could cast it away :)
I'm not sure what exactly you mean. The function pointer signature doesn't match here.
Rafael J. Wysocki
maintainerQuoted 20 lines · Andy Shevchenko
Quoted 2 lines · Rafael J. Wysocki
On Tue, Jun 7, 2022 at 10:22 PM Andy Shevchenko
<[email protected]> wrote:...
Quoted 13 lines
I would define it as
static int
match_first(struct device *dev, void *){ return 1; }struct device *device_find_first_child(struct device *parent)
{ return device_find_first_child(parent, NULL, match_first); }
EXPORT_SYMBOL_GPL(device_find_first_child);
Quoted 5 lines
Quoted 1 line
which is not that much more overhead.
With this we actually may simply provide a match function and it will make the clean ups (like patch 2 in the series) almost the same without introducing a device core call.
That works too, but IMO it would be a bit cleaner to have the wrapper defined as a proper function.
Quoted 9 lines
Something like
int
device_match_any_for_find(struct device *dev, void *unused){ return 1; }As I replied to Greg it's pity we can't use
device_match_any()...
Well, that only is a matter of adding one more variant of _match_any_ ...
Greg Kroah-Hartman
maintainerQuoted 2 lines · Andy Shevchenko
There are several places in the kernel where this kind of functionality is being used. Provide a generic helper for such cases.
This feels really wrong/broken.
There should not be any specific ordering of children in the tree. What subsystem relies on this such that they require this?
thanks,
greg k-h