diff mbox series

[net,V2,12/12] net/mlx5: SD, Handle possible devcom ERR_PTR

Message ID 20240409190820.227554-13-tariqt@nvidia.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series mlx5 misc fixes | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 942 this patch: 942
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 1 maintainers not CCed: linux-rdma@vger.kernel.org
netdev/build_clang success Errors and warnings before: 953 this patch: 953
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 953 this patch: 953
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 10 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-10--06-00 (tests: 962)

Commit Message

Tariq Toukan April 9, 2024, 7:08 p.m. UTC
Check if devcom holds an error pointer and return immediately.

This fixes Smatch static checker warning:
drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c:221 sd_register()
error: 'devcom' dereferencing possible ERR_PTR()

Fixes: d3d057666090 ("net/mlx5: SD, Implement devcom communication and primary election")
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Link: https://lore.kernel.org/all/f09666c8-e604-41f6-958b-4cc55c73faf9@gmail.com/T/
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
Reviewed-by: Gal Pressman <gal@nvidia.com>
---
 drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Dan Carpenter April 10, 2024, 1:24 p.m. UTC | #1
On Tue, Apr 09, 2024 at 10:08:20PM +0300, Tariq Toukan wrote:
> Check if devcom holds an error pointer and return immediately.
> 
> This fixes Smatch static checker warning:
> drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c:221 sd_register()
> error: 'devcom' dereferencing possible ERR_PTR()
> 
> Fixes: d3d057666090 ("net/mlx5: SD, Implement devcom communication and primary election")
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Link: https://lore.kernel.org/all/f09666c8-e604-41f6-958b-4cc55c73faf9@gmail.com/T/
> Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
> Reviewed-by: Gal Pressman <gal@nvidia.com>
> ---
>  drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c b/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c
> index 5b28084e8a03..adbafed44ce7 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c
> @@ -213,8 +213,8 @@ static int sd_register(struct mlx5_core_dev *dev)
>  	sd = mlx5_get_sd(dev);
>  	devcom = mlx5_devcom_register_component(dev->priv.devc, MLX5_DEVCOM_SD_GROUP,
>  						sd->group_id, NULL, dev);
> -	if (!devcom)
> -		return -ENOMEM;
> +	if (IS_ERR_OR_NULL(devcom))
> +		return devcom ? PTR_ERR(devcom) : -ENOMEM;

Why not just change mlx5_devcom_register_component() to return
ERR_PTR(-EINVAL); instead of NULL?  Then the callers could just do:

	if (IS_ERR(devcom))
		return PTR_ERR(devcom);

We only have a sample size of 4 callers but doing it in this
non-standard way seems to introduce bugs in 25% of the callers.

regards,
dan carpenter
Tariq Toukan April 10, 2024, 6:16 p.m. UTC | #2
On 10/04/2024 16:24, Dan Carpenter wrote:
> On Tue, Apr 09, 2024 at 10:08:20PM +0300, Tariq Toukan wrote:
>> Check if devcom holds an error pointer and return immediately.
>>
>> This fixes Smatch static checker warning:
>> drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c:221 sd_register()
>> error: 'devcom' dereferencing possible ERR_PTR()
>>
>> Fixes: d3d057666090 ("net/mlx5: SD, Implement devcom communication and primary election")
>> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
>> Link: https://lore.kernel.org/all/f09666c8-e604-41f6-958b-4cc55c73faf9@gmail.com/T/
>> Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
>> Reviewed-by: Gal Pressman <gal@nvidia.com>
>> ---
>>   drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c b/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c
>> index 5b28084e8a03..adbafed44ce7 100644
>> --- a/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c
>> +++ b/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c
>> @@ -213,8 +213,8 @@ static int sd_register(struct mlx5_core_dev *dev)
>>   	sd = mlx5_get_sd(dev);
>>   	devcom = mlx5_devcom_register_component(dev->priv.devc, MLX5_DEVCOM_SD_GROUP,
>>   						sd->group_id, NULL, dev);
>> -	if (!devcom)
>> -		return -ENOMEM;
>> +	if (IS_ERR_OR_NULL(devcom))
>> +		return devcom ? PTR_ERR(devcom) : -ENOMEM;
> 
> Why not just change mlx5_devcom_register_component() to return
> ERR_PTR(-EINVAL); instead of NULL?  Then the callers could just do:
> 
> 	if (IS_ERR(devcom))
> 		return PTR_ERR(devcom);
> 
> We only have a sample size of 4 callers but doing it in this
> non-standard way seems to introduce bugs in 25% of the callers.
> 
> regards,
> dan carpenter
> 
> 

Hi Dan,

Touching mlx5_devcom_register_component() as a fix for commit 
d3d057666090 ("net/mlx5: SD, Implement devcom communication and primary 
election") doesn't look right to me.
In addition, I prefer minimal fixes to net.

After this one is accepted to net, we can enhance the code in a followup 
patch to net-next.

Regards,
Tariq
Jakub Kicinski April 11, 2024, 2:52 a.m. UTC | #3
On Wed, 10 Apr 2024 21:16:37 +0300 Tariq Toukan wrote:
> Touching mlx5_devcom_register_component() as a fix for commit 
> d3d057666090 ("net/mlx5: SD, Implement devcom communication and primary 
> election") doesn't look right to me.
> In addition, I prefer minimal fixes to net.
> 
> After this one is accepted to net, we can enhance the code in a followup 
> patch to net-next.

I think the official guidance from Greg is "fix it right, worry about
size of the change when stable backport fails".
mlx5_devcom_register_component() has 4 callers, please follow Dan's advice.
I'll apply other 11 patches.
diff mbox series

Patch

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c b/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c
index 5b28084e8a03..adbafed44ce7 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c
@@ -213,8 +213,8 @@  static int sd_register(struct mlx5_core_dev *dev)
 	sd = mlx5_get_sd(dev);
 	devcom = mlx5_devcom_register_component(dev->priv.devc, MLX5_DEVCOM_SD_GROUP,
 						sd->group_id, NULL, dev);
-	if (!devcom)
-		return -ENOMEM;
+	if (IS_ERR_OR_NULL(devcom))
+		return devcom ? PTR_ERR(devcom) : -ENOMEM;
 
 	sd->devcom = devcom;