driver core: class: Fix bug and code improvements for class APIs
Subsystems (6)
v5 cover driver core: class: Fix bug and code improvements for class APIs
This patch series is to fix bugs and improve codes regarding various driver core device iterating APIs
---
Changes in v5:
- Add comments back and correct tile and commit messages for patch 8/8.
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Squich patches 3-5 into one based on Jonathan and Fan comments.
- Add one more patch
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Correct commit message, add fix tag, and correct pr_crit() message for 1st patch
- Add more patches regarding driver core device iterating APIs.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Remove both fix and stable tag for patch 1/3
- drop patch 3/3
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Zijun Hu (8):
driver core: class: Fix wild pointer dereferences in API class_dev_iter_next()
blk-cgroup: Fix class @block_class's subsystem refcount leakage driver core: Move true expression out of if condition in 3 device finding APIs driver core: Rename declaration parameter name for API device_find_child() cluster driver core: Correct parameter check for API device_for_each_child_reverse_from() driver core: Correct API device_for_each_child_reverse_from() prototype driver core: Introduce device_iter_t for device iterating APIs driver core: Move two simple APIs for finding child device to header
block/blk-cgroup.c | 1 + drivers/base/bus.c | 9 +++++--- drivers/base/class.c | 11 ++++++++-- drivers/base/core.c | 49 +++++++++---------------------------------- drivers/base/driver.c | 9 +++++--- drivers/cxl/core/hdm.c | 2 +- drivers/cxl/core/region.c | 2 +- include/linux/device.h | 46 +++++++++++++++++++++++++++++++--------- include/linux/device/bus.h | 7 +++++-- include/linux/device/class.h | 4 ++-- include/linux/device/driver.h | 2 +- 11 files changed, 78 insertions(+), 64 deletions(-)
---
base-commit: 78d4f34e2115b517bcbfe7ec0d018bbbb6f9b0b8
change-id: 20241104-class_fix-f176bd9eba22
prerequisite-change-id: 20241201-const_dfc_done-aaec71e3bbea:v5
prerequisite-patch-id: 536aa56c0d055f644a1f71ab5c88b7cac9510162
prerequisite-patch-id: 39b0cf088c72853d9ce60c9e633ad2070a0278a8
prerequisite-patch-id: 60b22c42b67ad56a3d2a7b80a30ad588cbe740ec
prerequisite-patch-id: 119a167d7248481987b5e015db0e4fdb0d6edab8
prerequisite-patch-id: 133248083f3d3c57beb16473c2a4c62b3abc5fd0
prerequisite-patch-id: 4cda541f55165650bfa69fb19cbe0524eff0cb85
prerequisite-patch-id: 2b4193c6ea6370c07e6b66de04be89fb09448f54
prerequisite-patch-id: 73c675db18330c89fd8ca4790914d1d486ce0db8
prerequisite-patch-id: 88c50fc851fd7077797fd4e63fb12966b1b601bd
prerequisite-patch-id: 47b93916c1b5fb809d7c99aeaa05c729b1af01c5
prerequisite-patch-id: 5b58c0292ea5a5e37b08e2c8287d94df958128db
prerequisite-patch-id: 52ffb42b5aae69cae708332e0ddc7016139999f1
Best regards,
Signature · 1 line
Zijun Hu <[email protected]>
PATCH v5 1/8 driver core: class: Fix wild pointer dereferences in API class_dev_iter_next()
From: Zijun Hu <[email protected]>
There are a potential wild pointer dereferences issue regarding APIs class_dev_iter_(init|next|exit)(), as explained by below typical usage:
// All members of @iter are wild pointers.
struct class_dev_iter iter;
// class_dev_iter_init(@iter, @class, ...) checks parameter @class for // potential class_to_subsys() error, and it returns void type and does // not initialize its output parameter @iter, so caller can not detect // the error and continues to invoke class_dev_iter_next(@iter) even if // @iter still contains wild pointers. class_dev_iter_init(&iter, ...);
// Dereference these wild pointers in @iter here once suffer the error. while (dev = class_dev_iter_next(&iter)) { ... };
// Also dereference these wild pointers here.class_dev_iter_exit(&iter);
Actually, all callers of these APIs have such usage pattern in kernel tree. Fix by:
- Initialize output parameter @iter by memset() in class_dev_iter_init() and give callers prompt by pr_crit() for the error.
- Check if @iter is valid in class_dev_iter_next().
---
Alternative fix solutions ever thought about:
1) Use BUG_ON(!sp) instead of error return in class_dev_iter_init().
2) Change class_dev_iter_init()'s type to int, lots of jobs to do.
This issue is APIs themself issues, and regardless of how various API users use them, and silent wild pointer dereferences are not what API users expect for the error absolutely.
--- drivers/base/class.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/base/class.c +8 -1
@@ -323,8 +323,12 @@ void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,struct subsys_private *sp =class_to_subsys (class);struct klist_node *start_knode =NULL ; -if (!sp) +memset (iter,0 ,sizeof (*iter)); +if (!sp) { +pr_crit ("%s: class %p was not registered yet\n" , + __func__, class);return ; + }if (start) start_knode = &start->p->knode_class;
@@ -351,6 +355,9 @@ struct device *class_dev_iter_next(struct class_dev_iter *iter)struct klist_node *knode;struct device *dev; +if (!iter->sp) +return NULL ; +while (1 ) { knode =klist_next (&iter->ki);if (!knode)
Signature · 1 line
2.34.1
PATCH v5 2/8 blk-cgroup: Fix class @block_class's subsystem refcount leakage
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
blkcg_fill_root_iostats() iterates over @block_class's devices by class_dev_iter_(init|next)(), but does not end iterating with class_dev_iter_exit(), so causes the class's subsystem refcount leakage.
Fix by ending the iterating with class_dev_iter_exit().
--- block/blk-cgroup.c | 1 + 1 file changed, 1 insertion(+)
block/blk-cgroup.c +1 -0
@@ -1138,6 +1138,7 @@ static void blkcg_fill_root_iostats(void)blkg_iostat_set (&blkg->iostat.cur, &tmp);u64_stats_update_end_irqrestore (&blkg->iostat.sync, flags); } +class_dev_iter_exit (&iter); }static void blkcg_print_one_stat (struct blkcg_gq *blkg,struct seq_file *s)
Signature · 1 line
2.34.1
PATCH v5 3/8 driver core: Move true expression out of if condition in 3 device finding APIs
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
For bus_find_device(), driver_find_device(), and device_find_child(), all of their function body have pattern below:
{
struct klist_iter i;
struct device *dev;
...
while ((dev = next_device(&i)))
if (match(dev, data) && get_device(dev))
break;
...
}The expression 'get_device(dev)' in the if condition always returns true since @dev != NULL.
Move the expression to if body to make logic of these APIs more clearer.
--- drivers/base/bus.c | 7 +++++-- drivers/base/core.c | 7 +++++-- drivers/base/driver.c | 7 +++++-- 3 files changed, 15 insertions(+), 6 deletions(-)
drivers/base/bus.c +5 -2
@@ -402,9 +402,12 @@ struct device *bus_find_device(const struct bus_type *bus,klist_iter_init_node (&sp->klist_devices, &i, (start ? &start->p->knode_bus :NULL )); -while ((dev =next_device (&i))) -if (match (dev, data) &&get_device (dev)) +while ((dev =next_device (&i))) { +if (match (dev, data)) { +get_device (dev);break ; + } + }klist_iter_exit (&i);subsys_put (sp);return dev;
drivers/base/core.c +5 -2
@@ -4089,9 +4089,12 @@ struct device *device_find_child(struct device *parent, const void *data,return NULL ;klist_iter_init (&parent->p->klist_children, &i); -while ((child =next_device (&i))) -if (match (child, data) &&get_device (child)) +while ((child =next_device (&i))) { +if (match (child, data)) { +get_device (child);break ; + } + }klist_iter_exit (&i);return child; }
drivers/base/driver.c +5 -2
@@ -160,9 +160,12 @@ struct device *driver_find_device(const struct device_driver *drv,klist_iter_init_node (&drv->p->klist_devices, &i, (start ? &start->p->knode_driver :NULL )); -while ((dev =next_device (&i))) -if (match (dev, data) &&get_device (dev)) +while ((dev =next_device (&i))) { +if (match (dev, data)) { +get_device (dev);break ; + } + }klist_iter_exit (&i);return dev; }
Signature · 1 line
2.34.1
PATCH v5 4/8 driver core: Rename declaration parameter name for API device_find_child() cluster
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
For APIs:device_find_child()device_for_each_child()device_for_each_child_reverse()
Their declaration has parameter name 'dev', but their defination changes the name to 'parent'.
Rename declaration name to defination 'parent' to make both have the same name.
--- include/linux/device.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/device.h +3 -3
@@ -1074,14 +1074,14 @@ void device_del(struct device *dev); DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T)) -int device_for_each_child (struct device *dev,void *data, +int device_for_each_child (struct device *parent,void *data,int (*fn)(struct device *dev,void *data)); -int device_for_each_child_reverse (struct device *dev,void *data, +int device_for_each_child_reverse (struct device *parent,void *data,int (*fn)(struct device *dev,void *data));int device_for_each_child_reverse_from (struct device *parent,struct device *from,const void *data,int (*fn)(struct device *,const void *)); -struct device *device_find_child (struct device *dev,const void *data, +struct device *device_find_child (struct device *parent,const void *data,device_match_t match);struct device *device_find_child_by_name (struct device *parent,const char *name);
Signature · 1 line
2.34.1
PATCH v5 5/8 driver core: Correct parameter check for API device_for_each_child_reverse_from()
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
device_for_each_child_reverse_from() checks (!parent->p) for its parameter @parent, and that is not consistent with other APIs of its cluster as shown below:
device_for_each_child_reverse_from() // check (!parent->p)
device_for_each_child_reverse() // check (!parent || !parent->p) device_for_each_child() // same above device_find_child() // same above
Correct the API's parameter @parent check by (!parent || !parent->p).
--- drivers/base/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/base/core.c +1 -1
@@ -4050,7 +4050,7 @@ int device_for_each_child_reverse_from(struct device *parent,struct device *child;int error =0 ; -if (!parent->p) +if (!parent || !parent->p)return 0 ; klist_iter_init_node(&parent->p->klist_children, &i,
Signature · 1 line
2.34.1
Re: [PATCH v5 5/8] driver core: Correct parameter check for API device_for_each_child_reverse_from()
Quoted 6 lines · On Tue, 24 Dec 2024 21:37:24 +0800 Zijun Hu <[email protected]> wrote:
From: Zijun Hu <[email protected]>
device_for_each_child_reverse_from()checks (!parent->p) for its parameter @parent, and that is not consistent with other APIs of its cluster as shown below:
Quoted 8 lines
device_for_each_child_reverse_from()// check (!parent->p)device_for_each_child_reverse() // check (!parent || !parent->p) device_for_each_child() // same above device_find_child() // same aboveCorrect the API's parameter @parent check by (!parent || !parent->p).
Signed-off-by Zijun Hu <[email protected]>
Cost is minor and consistency is always nice event if it is very unlikely reasonable code would call this without parent being set.
Quoted 18 lines
--- drivers/base/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/base/core.c +1
-1@@ -4050,7 +4050,7 @@ int device_for_each_child_reverse_from(struct device *parent,struct device *child;int error =0 ; -if (!parent->p) +if (!parent || !parent->p)return 0 ; klist_iter_init_node(&parent->p->klist_children, &i,
PATCH v5 6/8 driver core: Correct API device_for_each_child_reverse_from() prototype
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
For API device_for_each_child_reverse_from(..., const void *data,
int (*fn)(struct device *dev, const void *data))
- Type of @data is const pointer, and means caller's data @*data is not allowed to be modified, but that usually is not proper for such non finding device iterating API.
- Types for both @data and @fn are not consistent with all other for_each device iterating APIs device_for_each_child(_reverse)(), bus_for_each_dev() and (driver|class)_for_each_device().
Correct its prototype by removing const from parameter types, then adapt for various existing usages.
An dedicated typedef device_iter_t will be introduced as @fn() type for various for_each device interating APIs later.
--- drivers/base/core.c | 4 ++-- drivers/cxl/core/hdm.c | 2 +- drivers/cxl/core/region.c | 2 +- include/linux/device.h | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-)
drivers/base/core.c +2 -2
@@ -4043,8 +4043,8 @@ EXPORT_SYMBOL_GPL(device_for_each_child_reverse);* device_for_each_child_reverse_from(); */ int device_for_each_child_reverse_from (struct device *parent, -struct device *from,const void *data, -int (*fn)(struct device *,const void *)) +struct device *from,void *data, +int (*fn)(struct device *,void *)) {struct klist_iter i;struct device *child;
drivers/cxl/core/hdm.c +1 -1
@@ -703,7 +703,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld)return 0 ; } -static int commit_reap (struct device *dev,const void *data) +static int commit_reap (struct device *dev,void *data) {struct cxl_port *port =to_cxl_port (dev->parent);struct cxl_decoder *cxld;
drivers/cxl/core/region.c +1 -1
@@ -778,7 +778,7 @@ static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos)return rc; } -static int check_commit_order (struct device *dev,const void *data) +static int check_commit_order (struct device *dev,void *data) {struct cxl_decoder *cxld =to_cxl_decoder (dev);
include/linux/device.h +2 -2
@@ -1079,8 +1079,8 @@ int device_for_each_child(struct device *parent, void *data,int device_for_each_child_reverse (struct device *parent,void *data,int (*fn)(struct device *dev,void *data));int device_for_each_child_reverse_from (struct device *parent, -struct device *from,const void *data, -int (*fn)(struct device *,const void *)); +struct device *from,void *data, +int (*fn)(struct device *,void *));struct device *device_find_child (struct device *parent,const void *data,device_match_t match); struct device *device_find_child_by_name(struct device *parent,
Signature · 1 line
2.34.1
PATCH v5 7/8 driver core: Introduce device_iter_t for device iterating APIs
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
There are several for_each APIs which has parameter with type below:
int (*fn)(struct device *dev, void *data)
They iterate over various device lists and call @fn() for each device with caller provided data @*data, and they usually need to modify @*data.
Give the type an dedicated typedef with advantages shown below:
typedef int (*device_iter_t)(struct device *dev, void *data)
- Shorter API declarations and definitions
- Prevent further for_each APIs from using bad parameter type
So introduce device_iter_t and apply it to various existing APIs below:bus_for_each_dev()
(class|driver)_for_each_device() device_for_each_child(_reverse|_reverse_from)().
--- drivers/base/bus.c | 2 +- drivers/base/class.c | 2 +- drivers/base/core.c | 6 +++--- drivers/base/driver.c | 2 +- include/linux/device.h | 6 +++--- include/linux/device/bus.h | 7 +++++-- include/linux/device/class.h | 4 ++-- include/linux/device/driver.h | 2 +- 8 files changed, 17 insertions(+), 14 deletions(-)
drivers/base/bus.c +1 -1
@@ -354,7 +354,7 @@ static struct device *next_device(struct klist_iter *i)* count in the supplied callback. */ int bus_for_each_dev (const struct bus_type *bus,struct device *start, -void *data,int (*fn)(struct device *,void *)) +void *data,device_iter_t fn) {struct subsys_private *sp =bus_to_subsys (bus);struct klist_iter i;
drivers/base/class.c +1 -1
@@ -402,7 +402,7 @@ EXPORT_SYMBOL_GPL(class_dev_iter_exit);* code. There's no locking restriction. */ int class_for_each_device (const struct class *class,const struct device *start, -void *data,int (*fn)(struct device *,void *)) +void *data,device_iter_t fn) {struct subsys_private *sp =class_to_subsys (class);struct class_dev_iter iter;
drivers/base/core.c +3 -3
@@ -3980,7 +3980,7 @@ const char *device_get_devnode(const struct device *dev,* other than 0, we break out and return that value. */ int device_for_each_child (struct device *parent,void *data, -int (*fn)(struct device *dev,void *data)) +device_iter_t fn) {struct klist_iter i;struct device *child;
@@ -4010,7 +4010,7 @@ EXPORT_SYMBOL_GPL(device_for_each_child);* other than 0, we break out and return that value. */ int device_for_each_child_reverse (struct device *parent,void *data, -int (*fn)(struct device *dev,void *data)) +device_iter_t fn) {struct klist_iter i;struct device *child;
@@ -4044,7 +4044,7 @@ EXPORT_SYMBOL_GPL(device_for_each_child_reverse);*/ int device_for_each_child_reverse_from (struct device *parent,struct device *from,void *data, -int (*fn)(struct device *,void *)) +device_iter_t fn) {struct klist_iter i;struct device *child;
drivers/base/driver.c +1 -1
@@ -115,7 +115,7 @@ EXPORT_SYMBOL_GPL(driver_set_override);* Iterate over the @drv's list of devices calling @fn for each one. */ int driver_for_each_device (struct device_driver *drv,struct device *start, -void *data,int (*fn)(struct device *,void *)) +void *data,device_iter_t fn) {struct klist_iter i;struct device *dev;
include/linux/device.h +3 -3
@@ -1075,12 +1075,12 @@ void device_del(struct device *dev); DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T))int device_for_each_child (struct device *parent,void *data, -int (*fn)(struct device *dev,void *data)); +device_iter_t fn);int device_for_each_child_reverse (struct device *parent,void *data, -int (*fn)(struct device *dev,void *data)); +device_iter_t fn);int device_for_each_child_reverse_from (struct device *parent,struct device *from,void *data, -int (*fn)(struct device *,void *)); +device_iter_t fn);struct device *device_find_child (struct device *parent,const void *data,device_match_t match); struct device *device_find_child_by_name(struct device *parent,
include/linux/device/bus.h +5 -2
@@ -139,9 +139,12 @@ int device_match_acpi_dev(struct device *dev, const void *adev);int device_match_acpi_handle (struct device *dev,const void *handle);int device_match_any (struct device *dev,const void *unused); +/* Device iterating function type for various driver core for_each APIs */ +typedef int (*device_iter_t )(struct device *dev,void *data); +/* iterator helpers for buses */ -int bus_for_each_dev (const struct bus_type *bus,struct device *start,void *data, -int (*fn)(struct device *dev,void *data)); +int bus_for_each_dev (const struct bus_type *bus,struct device *start, +void *data,device_iter_t fn);struct device *bus_find_device (const struct bus_type *bus,struct device *start,const void *data,device_match_t match);/**
include/linux/device/class.h +2 -2
@@ -92,8 +92,8 @@ void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,struct device *class_dev_iter_next (struct class_dev_iter *iter);void class_dev_iter_exit (struct class_dev_iter *iter); -int class_for_each_device (const struct class *class,const struct device *start,void *data, -int (*fn)(struct device *dev,void *data)); +int class_for_each_device (const struct class *class,const struct device *start, +void *data,device_iter_t fn);struct device *class_find_device (const struct class *class,const struct device *start,const void *data,device_match_t match);
include/linux/device/driver.h +1 -1
@@ -154,7 +154,7 @@ void driver_remove_file(const struct device_driver *driver,int driver_set_override (struct device *dev,const char **override,const char *s,size_t len);int __must_check driver_for_each_device (struct device_driver *drv,struct device *start, -void *data,int (*fn)(struct device *dev,void *)); +void *data,device_iter_t fn);struct device *driver_find_device (const struct device_driver *drv,struct device *start,const void *data,device_match_t match);
Signature · 1 line
2.34.1
PATCH v5 8/8 driver core: Move two simple APIs for finding child device to header
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
The following two APIs are for finding child device, and both only have one line code in function body.device_find_child_by_name()device_find_any_child()
Move them to header as static inline function.
--- drivers/base/core.c | 32 -------------------------------- include/linux/device.h | 32 +++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 35 deletions(-)
drivers/base/core.c +0 -32
@@ -4100,38 +4100,6 @@ struct device *device_find_child(struct device *parent, const void *data, }EXPORT_SYMBOL_GPL (device_find_child); -/** -* device_find_child_by_name - device iterator for locating a child device. -* @parent: parent struct device -* @name: name of the child device -* -* This is similar to the device_find_child() function above, but it -* returns a reference to a device that has the name @name. -* -* NOTE: you will need to drop the reference with put_device() after use. -*/ -struct device *device_find_child_by_name (struct device *parent, -const char *name) -{ -return device_find_child (parent, name, device_match_name); -} -EXPORT_SYMBOL_GPL (device_find_child_by_name); - -/** -* device_find_any_child - device iterator for locating a child device, if any. -* @parent: parent struct device -* -* This is similar to the device_find_child() function above, but it -* returns a reference to a child device, if any. -* -* NOTE: you will need to drop the reference with put_device() after use. -*/ -struct device *device_find_any_child (struct device *parent) -{ -return device_find_child (parent,NULL , device_match_any); -} -EXPORT_SYMBOL_GPL (device_find_any_child); -int __init devices_init (void ) { devices_kset =kset_create_and_add ("devices" , &device_uevent_ops,NULL );
include/linux/device.h +29 -3
@@ -1083,9 +1083,35 @@ int device_for_each_child_reverse_from(struct device *parent, device_iter_t fn);struct device *device_find_child (struct device *parent,const void *data,device_match_t match); -struct device *device_find_child_by_name (struct device *parent, -const char *name); -struct device *device_find_any_child (struct device *parent); +/** +* device_find_child_by_name - device iterator for locating a child device. +* @parent: parent struct device +* @name: name of the child device +* +* This is similar to the device_find_child() function above, but it +* returns a reference to a device that has the name @name. +* +* NOTE: you will need to drop the reference with put_device() after use. +*/ +static inline struct device *device_find_child_by_name (struct device *parent, +const char *name) +{ +return device_find_child (parent, name, device_match_name); +} + +/** +* device_find_any_child - device iterator for locating a child device, if any. +* @parent: parent struct device +* +* This is similar to the device_find_child() function above, but it +* returns a reference to a child device, if any. +* +* NOTE: you will need to drop the reference with put_device() after use. +*/ +static inline struct device *device_find_any_child (struct device *parent) +{ +return device_find_child (parent,NULL , device_match_any); +}int device_rename (struct device *dev,const char *new_name); int device_move(struct device *dev, struct device *new_parent,
Signature · 1 line
2.34.1
Re: [PATCH v5 8/8] driver core: Move two simple APIs for finding child device to header
Quoted 10 lines · On Tue, 24 Dec 2024 21:37:27 +0800 Zijun Hu <[email protected]> wrote:
From: Zijun Hu <[email protected]>
The following two APIs are for finding child device, and both only have one line code in function body.
device_find_child_by_name()device_find_any_child()Move them to header as static inline function.
Signed-off-by Zijun Hu <[email protected]>