[PATCH 3/3] driver core: class: Delete a redundant check in APIs class_(for_each|find)_device()

Zijun Hu posted 3 patches 2 weeks, 5 days ago
There is a newer version of this series
[PATCH 3/3] driver core: class: Delete a redundant check in APIs class_(for_each|find)_device()
Posted by Zijun Hu 2 weeks, 5 days ago
From: Zijun Hu <quic_zijuhu@quicinc.com>

Delete redundant check (!@class) in both API class_for_each_device() and
class_find_device() with below reasons:

- The check is covered by later check (!@sp).
- Callers are unlikely to call both APIs with NULL class argument.
- Make parameter check consistent with all of other class APIs.

Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
---
 drivers/base/class.c | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/drivers/base/class.c b/drivers/base/class.c
index e81da280af74..120d3aeb52fe 100644
--- a/drivers/base/class.c
+++ b/drivers/base/class.c
@@ -408,8 +408,6 @@ int class_for_each_device(const struct class *class, const struct device *start,
 	struct device *dev;
 	int error = 0;
 
-	if (!class)
-		return -EINVAL;
 	if (!sp) {
 		WARN(1, "%s called for class '%s' before it was registered",
 		     __func__, class->name);
@@ -456,8 +454,6 @@ struct device *class_find_device(const struct class *class, const struct device
 	struct class_dev_iter iter;
 	struct device *dev;
 
-	if (!class)
-		return NULL;
 	if (!sp) {
 		WARN(1, "%s called for class '%s' before it was registered",
 		     __func__, class->name);

-- 
2.34.1
Re: [PATCH 3/3] driver core: class: Delete a redundant check in APIs class_(for_each|find)_device()
Posted by Greg Kroah-Hartman 1 week, 5 days ago
On Tue, Nov 05, 2024 at 08:20:24AM +0800, Zijun Hu wrote:
> From: Zijun Hu <quic_zijuhu@quicinc.com>
> 
> Delete redundant check (!@class) in both API class_for_each_device() and
> class_find_device() with below reasons:
> 
> - The check is covered by later check (!@sp).
> - Callers are unlikely to call both APIs with NULL class argument.
> - Make parameter check consistent with all of other class APIs.
> 
> Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
> ---
>  drivers/base/class.c | 4 ----
>  1 file changed, 4 deletions(-)
> 
> diff --git a/drivers/base/class.c b/drivers/base/class.c
> index e81da280af74..120d3aeb52fe 100644
> --- a/drivers/base/class.c
> +++ b/drivers/base/class.c
> @@ -408,8 +408,6 @@ int class_for_each_device(const struct class *class, const struct device *start,
>  	struct device *dev;
>  	int error = 0;
>  
> -	if (!class)
> -		return -EINVAL;
>  	if (!sp) {
>  		WARN(1, "%s called for class '%s' before it was registered",
>  		     __func__, class->name);

Now, if I pass in NULL for class, I get an odd warning, AND the kernel
crashes with the dereference of class->name.

So this is not ok :(

> @@ -456,8 +454,6 @@ struct device *class_find_device(const struct class *class, const struct device
>  	struct class_dev_iter iter;
>  	struct device *dev;
>  
> -	if (!class)
> -		return NULL;
>  	if (!sp) {
>  		WARN(1, "%s called for class '%s' before it was registered",
>  		     __func__, class->name);

Same here, this change is going to break things if people get it wrong,
please leave both of these as-is.

thanks,

greg k-h
Re: [PATCH 3/3] driver core: class: Delete a redundant check in APIs class_(for_each|find)_device()
Posted by Zijun Hu 1 week, 4 days ago
On 2024/11/12 19:45, Greg Kroah-Hartman wrote:
> On Tue, Nov 05, 2024 at 08:20:24AM +0800, Zijun Hu wrote:
>> From: Zijun Hu <quic_zijuhu@quicinc.com>
>>
>> Delete redundant check (!@class) in both API class_for_each_device() and
>> class_find_device() with below reasons:
>>
>> - The check is covered by later check (!@sp).
>> - Callers are unlikely to call both APIs with NULL class argument.
>> - Make parameter check consistent with all of other class APIs.
>>
>> Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
>> ---
>>  drivers/base/class.c | 4 ----
>>  1 file changed, 4 deletions(-)
>>
>> diff --git a/drivers/base/class.c b/drivers/base/class.c
>> index e81da280af74..120d3aeb52fe 100644
>> --- a/drivers/base/class.c
>> +++ b/drivers/base/class.c
>> @@ -408,8 +408,6 @@ int class_for_each_device(const struct class *class, const struct device *start,
>>  	struct device *dev;
>>  	int error = 0;
>>  
>> -	if (!class)
>> -		return -EINVAL;
>>  	if (!sp) {
>>  		WARN(1, "%s called for class '%s' before it was registered",
>>  		     __func__, class->name);
> 
> Now, if I pass in NULL for class, I get an odd warning, AND the kernel
> crashes with the dereference of class->name.
> 

yes. you are right.

i did not notice "class->name" in warning message.

> So this is not ok :(
> 
>> @@ -456,8 +454,6 @@ struct device *class_find_device(const struct class *class, const struct device
>>  	struct class_dev_iter iter;
>>  	struct device *dev;
>>  
>> -	if (!class)
>> -		return NULL;
>>  	if (!sp) {
>>  		WARN(1, "%s called for class '%s' before it was registered",
>>  		     __func__, class->name);
> 
> Same here, this change is going to break things if people get it wrong,
> please leave both of these as-is.
> 

okay. agree with you.

> thanks,
> 
> greg k-h