* [PATCH 1/5] driver core: Add simple parameter checks for APIs device_(for_each|find)_child()
2024-08-11 0:18 [PATCH 0/5] driver core: Prevent device_find_child() from modifying caller's match data Zijun Hu
@ 2024-08-11 0:18 ` Zijun Hu
2024-08-13 9:44 ` Greg Kroah-Hartman
2024-08-11 0:18 ` [PATCH 2/5] driver core: Introduce an API constify_device_find_child_helper() Zijun Hu
` (3 subsequent siblings)
4 siblings, 1 reply; 15+ messages in thread
From: Zijun Hu @ 2024-08-11 0:18 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Davidlohr Bueso,
Jonathan Cameron, Dave Jiang, Alison Schofield, Vishal Verma,
Ira Weiny, Dan Williams, Takashi Sakamoto, Timur Tabi,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Zijun Hu, linux-kernel, linux-cxl, linux1394-devel, netdev,
Zijun Hu
From: Zijun Hu <quic_zijuhu@quicinc.com>
Add simple parameter checks for APIs device_(for_each|find)_child() and
device_for_each_child_reverse().
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
---
drivers/base/core.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 1688e76cb64b..b1dd8c5590dc 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -4004,7 +4004,7 @@ int device_for_each_child(struct device *parent, void *data,
struct device *child;
int error = 0;
- if (!parent->p)
+ if (!parent || !parent->p)
return 0;
klist_iter_init(&parent->p->klist_children, &i);
@@ -4034,7 +4034,7 @@ int device_for_each_child_reverse(struct device *parent, void *data,
struct device *child;
int error = 0;
- if (!parent->p)
+ if (!parent || !parent->p)
return 0;
klist_iter_init(&parent->p->klist_children, &i);
@@ -4068,7 +4068,7 @@ struct device *device_find_child(struct device *parent, void *data,
struct klist_iter i;
struct device *child;
- if (!parent)
+ if (!parent || !parent->p)
return NULL;
klist_iter_init(&parent->p->klist_children, &i);
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH 1/5] driver core: Add simple parameter checks for APIs device_(for_each|find)_child()
2024-08-11 0:18 ` [PATCH 1/5] driver core: Add simple parameter checks for APIs device_(for_each|find)_child() Zijun Hu
@ 2024-08-13 9:44 ` Greg Kroah-Hartman
2024-08-13 10:00 ` quic_zijuhu
0 siblings, 1 reply; 15+ messages in thread
From: Greg Kroah-Hartman @ 2024-08-13 9:44 UTC (permalink / raw)
To: Zijun Hu
Cc: Rafael J. Wysocki, Davidlohr Bueso, Jonathan Cameron, Dave Jiang,
Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
Takashi Sakamoto, Timur Tabi, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, linux-kernel, linux-cxl,
linux1394-devel, netdev, Zijun Hu
On Sun, Aug 11, 2024 at 08:18:07AM +0800, Zijun Hu wrote:
> From: Zijun Hu <quic_zijuhu@quicinc.com>
>
> Add simple parameter checks for APIs device_(for_each|find)_child() and
> device_for_each_child_reverse().
Ok, but why? Who is calling this with NULL as a parent pointer?
Remember, changelog text describes _why_ not just _what_ you are doing.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 1/5] driver core: Add simple parameter checks for APIs device_(for_each|find)_child()
2024-08-13 9:44 ` Greg Kroah-Hartman
@ 2024-08-13 10:00 ` quic_zijuhu
2024-08-13 10:19 ` Greg Kroah-Hartman
0 siblings, 1 reply; 15+ messages in thread
From: quic_zijuhu @ 2024-08-13 10:00 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Rafael J. Wysocki, Davidlohr Bueso, Jonathan Cameron, Dave Jiang,
Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
Takashi Sakamoto, Timur Tabi, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, linux-kernel, linux-cxl,
linux1394-devel, netdev, Zijun Hu
On 8/13/2024 5:44 PM, Greg Kroah-Hartman wrote:
> On Sun, Aug 11, 2024 at 08:18:07AM +0800, Zijun Hu wrote:
>> From: Zijun Hu <quic_zijuhu@quicinc.com>
>>
>> Add simple parameter checks for APIs device_(for_each|find)_child() and
>> device_for_each_child_reverse().
>
> Ok, but why? Who is calling this with NULL as a parent pointer?
>
> Remember, changelog text describes _why_ not just _what_ you are doing.
>
For question why ?
The main purpose of this change is to make these APIs have *CONSISTENT*
parameter checking (!parent || !parent->p)
currently, 2 of them have checking (!parent->p), the other have checking
(!parent), the are INCONSISTENT.
For question who ?
device_find_child() have had such checking (!parent), that maybe mean
original author has concern that parent may be NULL.
Moreover, these are core driver APIs, it is worthy checking input
parameter strictly.
will correct commit message with v2.
> thanks,
>
> greg k-h
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 1/5] driver core: Add simple parameter checks for APIs device_(for_each|find)_child()
2024-08-13 10:00 ` quic_zijuhu
@ 2024-08-13 10:19 ` Greg Kroah-Hartman
0 siblings, 0 replies; 15+ messages in thread
From: Greg Kroah-Hartman @ 2024-08-13 10:19 UTC (permalink / raw)
To: quic_zijuhu
Cc: Rafael J. Wysocki, Davidlohr Bueso, Jonathan Cameron, Dave Jiang,
Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
Takashi Sakamoto, Timur Tabi, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, linux-kernel, linux-cxl,
linux1394-devel, netdev, Zijun Hu
On Tue, Aug 13, 2024 at 06:00:30PM +0800, quic_zijuhu wrote:
> On 8/13/2024 5:44 PM, Greg Kroah-Hartman wrote:
> > On Sun, Aug 11, 2024 at 08:18:07AM +0800, Zijun Hu wrote:
> >> From: Zijun Hu <quic_zijuhu@quicinc.com>
> >>
> >> Add simple parameter checks for APIs device_(for_each|find)_child() and
> >> device_for_each_child_reverse().
> >
> > Ok, but why? Who is calling this with NULL as a parent pointer?
> >
> > Remember, changelog text describes _why_ not just _what_ you are doing.
> >
>
> For question why ?
>
> The main purpose of this change is to make these APIs have *CONSISTENT*
> parameter checking (!parent || !parent->p)
>
> currently, 2 of them have checking (!parent->p), the other have checking
> (!parent), the are INCONSISTENT.
>
>
> For question who ?
> device_find_child() have had such checking (!parent), that maybe mean
> original author has concern that parent may be NULL.
>
> Moreover, these are core driver APIs, it is worthy checking input
> parameter strictly.
Not always, no, don't check for things that will not happen, otherwise
you are checking for no reason at all.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 2/5] driver core: Introduce an API constify_device_find_child_helper()
2024-08-11 0:18 [PATCH 0/5] driver core: Prevent device_find_child() from modifying caller's match data Zijun Hu
2024-08-11 0:18 ` [PATCH 1/5] driver core: Add simple parameter checks for APIs device_(for_each|find)_child() Zijun Hu
@ 2024-08-11 0:18 ` Zijun Hu
2024-08-13 9:45 ` Greg Kroah-Hartman
2024-08-11 0:18 ` [PATCH 3/5] cxl/region: Prevent device_find_child() from modifying caller's match data Zijun Hu
` (2 subsequent siblings)
4 siblings, 1 reply; 15+ messages in thread
From: Zijun Hu @ 2024-08-11 0:18 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Davidlohr Bueso,
Jonathan Cameron, Dave Jiang, Alison Schofield, Vishal Verma,
Ira Weiny, Dan Williams, Takashi Sakamoto, Timur Tabi,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Zijun Hu, linux-kernel, linux-cxl, linux1394-devel, netdev,
Zijun Hu
From: Zijun Hu <quic_zijuhu@quicinc.com>
Introduce constify_device_find_child_helper() to replace existing
device_find_child()'s usages whose match functions will modify
caller's match data.
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
---
drivers/base/core.c | 35 +++++++++++++++++++++++++++++++++++
include/linux/device.h | 7 +++++++
2 files changed, 42 insertions(+)
diff --git a/drivers/base/core.c b/drivers/base/core.c
index b1dd8c5590dc..3f3ebdb5aa0b 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -4128,6 +4128,41 @@ struct device *device_find_any_child(struct device *parent)
}
EXPORT_SYMBOL_GPL(device_find_any_child);
+struct fn_data_struct {
+ int (*match)(struct device *dev, void *data);
+ void *data;
+ struct device *target_device;
+};
+
+static int constify_device_match_fn(struct device *dev, void *data)
+{
+ struct fn_data_struct *fn_data = data;
+ int res;
+
+ res = fn_data->match(dev, fn_data->data);
+ if (res && get_device(dev)) {
+ fn_data->target_device = dev;
+ return res;
+ }
+
+ return 0;
+}
+
+/*
+ * My mission is to clean up existing match functions which will modify
+ * caller's match data for device_find_child(), so i do not introduce
+ * myself here to prevent that i am used for any other purpose.
+ */
+struct device *constify_device_find_child_helper(struct device *parent, void *data,
+ int (*match)(struct device *dev, void *data))
+{
+ struct fn_data_struct fn_data = {match, data, NULL};
+
+ device_for_each_child(parent, &fn_data, constify_device_match_fn);
+ return fn_data.target_device;
+}
+EXPORT_SYMBOL_GPL(constify_device_find_child_helper);
+
int __init devices_init(void)
{
devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
diff --git a/include/linux/device.h b/include/linux/device.h
index 34eb20f5966f..b2423fca3d45 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -1078,6 +1078,13 @@ struct device *device_find_child(struct device *dev, void *data,
struct device *device_find_child_by_name(struct device *parent,
const char *name);
struct device *device_find_any_child(struct device *parent);
+/*
+ * My mission is to clean up existing match functions which will modify
+ * caller's match data for device_find_child(), so please DO NOT use me
+ * for any other purpose.
+ */
+struct device *constify_device_find_child_helper(struct device *parent, void *data,
+ int (*match)(struct device *dev, void *data));
int device_rename(struct device *dev, const char *new_name);
int device_move(struct device *dev, struct device *new_parent,
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH 2/5] driver core: Introduce an API constify_device_find_child_helper()
2024-08-11 0:18 ` [PATCH 2/5] driver core: Introduce an API constify_device_find_child_helper() Zijun Hu
@ 2024-08-13 9:45 ` Greg Kroah-Hartman
2024-08-13 10:50 ` quic_zijuhu
0 siblings, 1 reply; 15+ messages in thread
From: Greg Kroah-Hartman @ 2024-08-13 9:45 UTC (permalink / raw)
To: Zijun Hu
Cc: Rafael J. Wysocki, Davidlohr Bueso, Jonathan Cameron, Dave Jiang,
Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
Takashi Sakamoto, Timur Tabi, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, linux-kernel, linux-cxl,
linux1394-devel, netdev, Zijun Hu
On Sun, Aug 11, 2024 at 08:18:08AM +0800, Zijun Hu wrote:
> From: Zijun Hu <quic_zijuhu@quicinc.com>
>
> Introduce constify_device_find_child_helper() to replace existing
> device_find_child()'s usages whose match functions will modify
> caller's match data.
Ick, that's not a good name, it should be "noun_verb" with the subsystem being on the prefix always.
But why is this even needed? Device pointers are NOT const for the
obvious reason that they can be changed by loads of different things.
Trying to force them to be const is going to be hard, if not impossible.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 2/5] driver core: Introduce an API constify_device_find_child_helper()
2024-08-13 9:45 ` Greg Kroah-Hartman
@ 2024-08-13 10:50 ` quic_zijuhu
2024-08-13 10:57 ` Greg Kroah-Hartman
0 siblings, 1 reply; 15+ messages in thread
From: quic_zijuhu @ 2024-08-13 10:50 UTC (permalink / raw)
To: Greg Kroah-Hartman, Zijun Hu
Cc: Rafael J. Wysocki, Davidlohr Bueso, Jonathan Cameron, Dave Jiang,
Alison Schofield, Vishal Verma, Ira Weiny, Dan Williams,
Takashi Sakamoto, Timur Tabi, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, linux-kernel, linux-cxl,
linux1394-devel, netdev
On 8/13/2024 5:45 PM, Greg Kroah-Hartman wrote:
> On Sun, Aug 11, 2024 at 08:18:08AM +0800, Zijun Hu wrote:
>> From: Zijun Hu <quic_zijuhu@quicinc.com>
>>
>> Introduce constify_device_find_child_helper() to replace existing
>> device_find_child()'s usages whose match functions will modify
>> caller's match data.
>
> Ick, that's not a good name, it should be "noun_verb" with the subsystem being on the prefix always.
>
okay, got it.
is it okay to use device_find_child_mut() suggested by Przemek Kitszel ?
> But why is this even needed? Device pointers are NOT const for the
> obvious reason that they can be changed by loads of different things.
> Trying to force them to be const is going to be hard, if not impossible.
>
[PATCH 3/5] have more discussion about these questions with below link:
https://lore.kernel.org/all/8b8ce122-f16b-4207-b03b-f74b15756ae7@icloud.com/
The ultimate goal is to make device_find_child() have below prototype:
struct device *device_find_child(struct device *dev, const void *data,
int (*match)(struct device *dev, const void *data));
Why ?
(1) It does not make sense, also does not need to, for such device
finding operation to modify caller's match data which is mainly
used for comparison.
(2) It will make the API's match function parameter have the same
signature as all other APIs (bus|class|driver)_find_device().
My idea is that:
use device_find_child() for READ only accessing caller's match data.
use below API if need to Modify caller's data as
constify_device_find_child_helper() does.
int device_for_each_child(struct device *dev, void *data,
int (*fn)(struct device *dev, void *data));
So the The ultimate goal is to protect caller's *match data* @*data NOT
device @*dev.
> thanks,
>
> greg k-h
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH 2/5] driver core: Introduce an API constify_device_find_child_helper()
2024-08-13 10:50 ` quic_zijuhu
@ 2024-08-13 10:57 ` Greg Kroah-Hartman
2024-08-13 11:15 ` quic_zijuhu
0 siblings, 1 reply; 15+ messages in thread
From: Greg Kroah-Hartman @ 2024-08-13 10:57 UTC (permalink / raw)
To: quic_zijuhu
Cc: Zijun Hu, Rafael J. Wysocki, Davidlohr Bueso, Jonathan Cameron,
Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
Dan Williams, Takashi Sakamoto, Timur Tabi, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-kernel,
linux-cxl, linux1394-devel, netdev
On Tue, Aug 13, 2024 at 06:50:04PM +0800, quic_zijuhu wrote:
> On 8/13/2024 5:45 PM, Greg Kroah-Hartman wrote:
> > On Sun, Aug 11, 2024 at 08:18:08AM +0800, Zijun Hu wrote:
> >> From: Zijun Hu <quic_zijuhu@quicinc.com>
> >>
> >> Introduce constify_device_find_child_helper() to replace existing
> >> device_find_child()'s usages whose match functions will modify
> >> caller's match data.
> >
> > Ick, that's not a good name, it should be "noun_verb" with the subsystem being on the prefix always.
> >
> okay, got it.
>
> is it okay to use device_find_child_mut() suggested by Przemek Kitszel ?
No, just switch all callers over to be const and keep the same name.
> > But why is this even needed? Device pointers are NOT const for the
> > obvious reason that they can be changed by loads of different things.
> > Trying to force them to be const is going to be hard, if not impossible.
> >
>
> [PATCH 3/5] have more discussion about these questions with below link:
> https://lore.kernel.org/all/8b8ce122-f16b-4207-b03b-f74b15756ae7@icloud.com/
>
>
> The ultimate goal is to make device_find_child() have below prototype:
>
> struct device *device_find_child(struct device *dev, const void *data,
> int (*match)(struct device *dev, const void *data));
>
> Why ?
>
> (1) It does not make sense, also does not need to, for such device
> finding operation to modify caller's match data which is mainly
> used for comparison.
>
> (2) It will make the API's match function parameter have the same
> signature as all other APIs (bus|class|driver)_find_device().
>
>
> My idea is that:
> use device_find_child() for READ only accessing caller's match data.
>
> use below API if need to Modify caller's data as
> constify_device_find_child_helper() does.
> int device_for_each_child(struct device *dev, void *data,
> int (*fn)(struct device *dev, void *data));
>
> So the The ultimate goal is to protect caller's *match data* @*data NOT
> device @*dev.
Ok, sorry, I was confused.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 2/5] driver core: Introduce an API constify_device_find_child_helper()
2024-08-13 10:57 ` Greg Kroah-Hartman
@ 2024-08-13 11:15 ` quic_zijuhu
0 siblings, 0 replies; 15+ messages in thread
From: quic_zijuhu @ 2024-08-13 11:15 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Zijun Hu, Rafael J. Wysocki, Davidlohr Bueso, Jonathan Cameron,
Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
Dan Williams, Takashi Sakamoto, Timur Tabi, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-kernel,
linux-cxl, linux1394-devel, netdev
On 8/13/2024 6:57 PM, Greg Kroah-Hartman wrote:
> On Tue, Aug 13, 2024 at 06:50:04PM +0800, quic_zijuhu wrote:
>> On 8/13/2024 5:45 PM, Greg Kroah-Hartman wrote:
>>> On Sun, Aug 11, 2024 at 08:18:08AM +0800, Zijun Hu wrote:
>>>> From: Zijun Hu <quic_zijuhu@quicinc.com>
>>>>
>>>> Introduce constify_device_find_child_helper() to replace existing
>>>> device_find_child()'s usages whose match functions will modify
>>>> caller's match data.
>>>
>>> Ick, that's not a good name, it should be "noun_verb" with the subsystem being on the prefix always.
>>>
>> okay, got it.
>>
>> is it okay to use device_find_child_mut() suggested by Przemek Kitszel ?
>
> No, just switch all callers over to be const and keep the same name.
>
>>> But why is this even needed? Device pointers are NOT const for the
>>> obvious reason that they can be changed by loads of different things.
>>> Trying to force them to be const is going to be hard, if not impossible.
>>>
>>
>> [PATCH 3/5] have more discussion about these questions with below link:
>> https://lore.kernel.org/all/8b8ce122-f16b-4207-b03b-f74b15756ae7@icloud.com/
>>
>>
>> The ultimate goal is to make device_find_child() have below prototype:
>>
>> struct device *device_find_child(struct device *dev, const void *data,
>> int (*match)(struct device *dev, const void *data));
>>
>> Why ?
>>
>> (1) It does not make sense, also does not need to, for such device
>> finding operation to modify caller's match data which is mainly
>> used for comparison.
>>
>> (2) It will make the API's match function parameter have the same
>> signature as all other APIs (bus|class|driver)_find_device().
>>
>>
>> My idea is that:
>> use device_find_child() for READ only accessing caller's match data.
>>
>> use below API if need to Modify caller's data as
>> constify_device_find_child_helper() does.
>> int device_for_each_child(struct device *dev, void *data,
>> int (*fn)(struct device *dev, void *data));
>>
>> So the The ultimate goal is to protect caller's *match data* @*data NOT
>> device @*dev.
>
> Ok, sorry, I was confused.
>
Current prototype of the API:
struct device *device_find_child(struct device *dev, void *data,
int (*match)(struct device *dev, void
*data));
prototype we want:
struct device *device_find_child(struct device *dev, const void *data,
int (*match)(struct device *dev, const
void *data));
The only differences are shown below:
void *data -> const void *data // as argument of paramter @data of
(*match)().
int (*match)(struct device *dev, void *data) -> int (*match)(struct
device *dev, const void *data).
We don't change type of @dev. we just make above two parameters have the
same types as below existing finding APIs.
struct device *bus_find_device(const struct bus_type *bus, struct device
*start,
const void *data,
int (*match)(struct device *dev, const
void *data));
struct device *driver_find_device(const struct device_driver *drv,
struct device *start, const void *data,
int (*match)(struct device *dev, const
void *data));
struct device *class_find_device(const struct class *class, const struct
device *start,
const void *data, int (*match)(struct
device *, const void *));
> thanks,
>
> greg k-h
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 3/5] cxl/region: Prevent device_find_child() from modifying caller's match data
2024-08-11 0:18 [PATCH 0/5] driver core: Prevent device_find_child() from modifying caller's match data Zijun Hu
2024-08-11 0:18 ` [PATCH 1/5] driver core: Add simple parameter checks for APIs device_(for_each|find)_child() Zijun Hu
2024-08-11 0:18 ` [PATCH 2/5] driver core: Introduce an API constify_device_find_child_helper() Zijun Hu
@ 2024-08-11 0:18 ` Zijun Hu
2024-08-12 12:54 ` Przemek Kitszel
2024-08-11 0:18 ` [PATCH 4/5] firewire: core: " Zijun Hu
2024-08-11 0:18 ` [PATCH 5/5] net: qcom/emac: " Zijun Hu
4 siblings, 1 reply; 15+ messages in thread
From: Zijun Hu @ 2024-08-11 0:18 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Davidlohr Bueso,
Jonathan Cameron, Dave Jiang, Alison Schofield, Vishal Verma,
Ira Weiny, Dan Williams, Takashi Sakamoto, Timur Tabi,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Zijun Hu, linux-kernel, linux-cxl, linux1394-devel, netdev,
Zijun Hu
From: Zijun Hu <quic_zijuhu@quicinc.com>
It does not make sense for match_free_decoder() as device_find_child()'s
match function to modify caller's match data, fixed by using
constify_device_find_child_helper() instead of device_find_child().
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
---
drivers/cxl/core/region.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 21ad5f242875..266231d69dff 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -849,7 +849,8 @@ cxl_region_find_decoder(struct cxl_port *port,
dev = device_find_child(&port->dev, &cxlr->params,
match_auto_decoder);
else
- dev = device_find_child(&port->dev, &id, match_free_decoder);
+ dev = constify_device_find_child_helper(&port->dev, &id,
+ match_free_decoder);
if (!dev)
return NULL;
/*
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH 3/5] cxl/region: Prevent device_find_child() from modifying caller's match data
2024-08-11 0:18 ` [PATCH 3/5] cxl/region: Prevent device_find_child() from modifying caller's match data Zijun Hu
@ 2024-08-12 12:54 ` Przemek Kitszel
2024-08-12 15:32 ` Zijun Hu
0 siblings, 1 reply; 15+ messages in thread
From: Przemek Kitszel @ 2024-08-12 12:54 UTC (permalink / raw)
To: Zijun Hu, Dan Williams
Cc: linux-kernel, linux-cxl, linux1394-devel, netdev, Zijun Hu,
Greg Kroah-Hartman, Rafael J. Wysocki, Davidlohr Bueso,
Jonathan Cameron, Dave Jiang, Alison Schofield, Vishal Verma,
Ira Weiny, Takashi Sakamoto, Timur Tabi, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
On 8/11/24 02:18, Zijun Hu wrote:
> From: Zijun Hu <quic_zijuhu@quicinc.com>
>
> It does not make sense for match_free_decoder() as device_find_child()'s
> match function to modify caller's match data,
match_free_decoder() is just doing that, treating caller's match data as
a piece of memory to store their int.
(So it is hard to tell it does not make sense "for [it] ... to").
> fixed by using
> constify_device_find_child_helper() instead of device_find_child().
I don't like the constify... name, I would go with something like
device_find_child_mut() or similar.
>
> Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
> ---
> drivers/cxl/core/region.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 21ad5f242875..266231d69dff 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -849,7 +849,8 @@ cxl_region_find_decoder(struct cxl_port *port,
> dev = device_find_child(&port->dev, &cxlr->params,
> match_auto_decoder);
> else
> - dev = device_find_child(&port->dev, &id, match_free_decoder);
> + dev = constify_device_find_child_helper(&port->dev, &id,
> + match_free_decoder);
> if (!dev)
> return NULL;
> /*
>
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 3/5] cxl/region: Prevent device_find_child() from modifying caller's match data
2024-08-12 12:54 ` Przemek Kitszel
@ 2024-08-12 15:32 ` Zijun Hu
0 siblings, 0 replies; 15+ messages in thread
From: Zijun Hu @ 2024-08-12 15:32 UTC (permalink / raw)
To: Przemek Kitszel, Dan Williams
Cc: linux-kernel, linux-cxl, linux1394-devel, netdev, Zijun Hu,
Greg Kroah-Hartman, Rafael J. Wysocki, Davidlohr Bueso,
Jonathan Cameron, Dave Jiang, Alison Schofield, Vishal Verma,
Ira Weiny, Takashi Sakamoto, Timur Tabi, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
On 2024/8/12 20:54, Przemek Kitszel wrote:
> On 8/11/24 02:18, Zijun Hu wrote:
>> From: Zijun Hu <quic_zijuhu@quicinc.com>
>>
>> It does not make sense for match_free_decoder() as device_find_child()'s
>> match function to modify caller's match data,
>
> match_free_decoder() is just doing that, treating caller's match data as
> a piece of memory to store their int.
> (So it is hard to tell it does not make sense "for [it] ... to").
>
Thanks for reply (^^)
The ultimate goal is to make device_find_child() have below prototype:
struct device *device_find_child(struct device *dev, const void *data,
int (*match)(struct device *dev, const void *data));
Why ?
(1) It does not make sense, also does not need to, for such device
finding operation to modify caller's match data which is mainly
used for comparison.
(2) It will make the API's match function parameter have the same
signature as all other APIs (bus|class|driver)_find_device().
My idea is that:
use device_find_child() for READ only accessing caller's match data.
use below API if need to Modify caller's data as
constify_device_find_child_helper() does.
int device_for_each_child(struct device *dev, void *data,
int (*fn)(struct device *dev, void *data));
So match_free_decoder() is not proper as device_find_child()'s
match function.
>> fixed by using
>> constify_device_find_child_helper() instead of device_find_child().
>
> I don't like the constify... name, I would go with something like
> device_find_child_mut() or similar.
>
What about below alternative option i ever thought about ?
Don't introduce API constify_device_find_child_helper() at all, and
change in involved driver such as cxl/core/region.c directly.
>>
>> Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
>> ---
>> drivers/cxl/core/region.c | 3 ++-
>> 1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
>> index 21ad5f242875..266231d69dff 100644
>> --- a/drivers/cxl/core/region.c
>> +++ b/drivers/cxl/core/region.c
>> @@ -849,7 +849,8 @@ cxl_region_find_decoder(struct cxl_port *port,
>> dev = device_find_child(&port->dev, &cxlr->params,
>> match_auto_decoder);
>> else
>> - dev = device_find_child(&port->dev, &id, match_free_decoder);
>> + dev = constify_device_find_child_helper(&port->dev, &id,
>> + match_free_decoder);
>> if (!dev)
>> return NULL;
>> /*
>>
>
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 4/5] firewire: core: Prevent device_find_child() from modifying caller's match data
2024-08-11 0:18 [PATCH 0/5] driver core: Prevent device_find_child() from modifying caller's match data Zijun Hu
` (2 preceding siblings ...)
2024-08-11 0:18 ` [PATCH 3/5] cxl/region: Prevent device_find_child() from modifying caller's match data Zijun Hu
@ 2024-08-11 0:18 ` Zijun Hu
2024-08-11 0:18 ` [PATCH 5/5] net: qcom/emac: " Zijun Hu
4 siblings, 0 replies; 15+ messages in thread
From: Zijun Hu @ 2024-08-11 0:18 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Davidlohr Bueso,
Jonathan Cameron, Dave Jiang, Alison Schofield, Vishal Verma,
Ira Weiny, Dan Williams, Takashi Sakamoto, Timur Tabi,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Zijun Hu, linux-kernel, linux-cxl, linux1394-devel, netdev,
Zijun Hu
From: Zijun Hu <quic_zijuhu@quicinc.com>
It does not make sense for lookup_existing_device() as match function
of device_find_child() to modify caller's match data, fixed by using
constify_device_find_child_helper() instead of device_find_child().
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
---
drivers/firewire/core-device.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/firewire/core-device.c b/drivers/firewire/core-device.c
index 00e9a13e6c45..04b150bc876d 100644
--- a/drivers/firewire/core-device.c
+++ b/drivers/firewire/core-device.c
@@ -1087,8 +1087,9 @@ static void fw_device_init(struct work_struct *work)
return;
}
- revived_dev = device_find_child(card->device,
- device, lookup_existing_device);
+ revived_dev = constify_device_find_child_helper(card->device,
+ device,
+ lookup_existing_device);
if (revived_dev) {
put_device(revived_dev);
fw_device_release(&device->device);
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread* [PATCH 5/5] net: qcom/emac: Prevent device_find_child() from modifying caller's match data
2024-08-11 0:18 [PATCH 0/5] driver core: Prevent device_find_child() from modifying caller's match data Zijun Hu
` (3 preceding siblings ...)
2024-08-11 0:18 ` [PATCH 4/5] firewire: core: " Zijun Hu
@ 2024-08-11 0:18 ` Zijun Hu
4 siblings, 0 replies; 15+ messages in thread
From: Zijun Hu @ 2024-08-11 0:18 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Davidlohr Bueso,
Jonathan Cameron, Dave Jiang, Alison Schofield, Vishal Verma,
Ira Weiny, Dan Williams, Takashi Sakamoto, Timur Tabi,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Zijun Hu, linux-kernel, linux-cxl, linux1394-devel, netdev,
Zijun Hu
From: Zijun Hu <quic_zijuhu@quicinc.com>
It does not make sense for emac_sgmii_acpi_match() as device_find_child()'s
match function to modify caller's match data, fixed by using
constify_device_find_child_helper() instead of device_find_child().
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
---
drivers/net/ethernet/qualcomm/emac/emac-sgmii.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/net/ethernet/qualcomm/emac/emac-sgmii.c b/drivers/net/ethernet/qualcomm/emac/emac-sgmii.c
index e4bc18009d08..e53065756a1d 100644
--- a/drivers/net/ethernet/qualcomm/emac/emac-sgmii.c
+++ b/drivers/net/ethernet/qualcomm/emac/emac-sgmii.c
@@ -358,8 +358,9 @@ int emac_sgmii_config(struct platform_device *pdev, struct emac_adapter *adpt)
if (has_acpi_companion(&pdev->dev)) {
struct device *dev;
- dev = device_find_child(&pdev->dev, &phy->sgmii_ops,
- emac_sgmii_acpi_match);
+ dev = constify_device_find_child_helper(&pdev->dev,
+ &phy->sgmii_ops,
+ emac_sgmii_acpi_match);
if (!dev) {
dev_warn(&pdev->dev, "cannot find internal phy node\n");
--
2.34.1
^ permalink raw reply related [flat|nested] 15+ messages in thread