public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cxl: core-pmu: Fix the usage of uninitialized variable
@ 2024-10-23 10:56 Zicheng Qu
  2024-10-23 12:26 ` Robert Richter
  2024-10-24  9:38 ` Jonathan Cameron
  0 siblings, 2 replies; 3+ messages in thread
From: Zicheng Qu @ 2024-10-23 10:56 UTC (permalink / raw)
  To: dave, jonathan.cameron, dave.jiang, alison.schofield,
	vishal.l.verma, ira.weiny, dan.j.williams, linux-cxl,
	linux-kernel
  Cc: tanghui20, zhangqiao22, judy.chenhui, quzicheng

In the devm_cxl_pmu_add() function, the variable rc might be
uninitialized before its first use 'if (rc) goto err', leading
to undefined behavior since its value depends on the compiler.
Currently, the switch statement is limited to the CXL_PMU_MEMDEV type.
If additional types are introduced, it could lead to similar concerns.
If the type range remains unchanged, using a switch case is unnecessary.
To enhance code extensibility and stability, it is recommended to
address this potential aspect.

Cc: stable@vger.kernel.org # v6.6+
Fixes: 1ad3f701c399 ("cxl/pci: Find and register CXL PMU devices")
Signed-off-by: Zicheng Qu <quzicheng@huawei.com>
---
 drivers/cxl/core/pmu.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/cxl/core/pmu.c b/drivers/cxl/core/pmu.c
index 5d8e06b0ba6e..2d12887c9915 100644
--- a/drivers/cxl/core/pmu.c
+++ b/drivers/cxl/core/pmu.c
@@ -51,6 +51,9 @@ int devm_cxl_pmu_add(struct device *parent, struct cxl_pmu_regs *regs,
 	case CXL_PMU_MEMDEV:
 		rc = dev_set_name(dev, "pmu_mem%d.%d", assoc_id, index);
 		break;
+	default:
+		rc = -EINVAL;
+		break;
 	}
 	if (rc)
 		goto err;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] cxl: core-pmu: Fix the usage of uninitialized variable
  2024-10-23 10:56 [PATCH] cxl: core-pmu: Fix the usage of uninitialized variable Zicheng Qu
@ 2024-10-23 12:26 ` Robert Richter
  2024-10-24  9:38 ` Jonathan Cameron
  1 sibling, 0 replies; 3+ messages in thread
From: Robert Richter @ 2024-10-23 12:26 UTC (permalink / raw)
  To: Zicheng Qu
  Cc: dave, jonathan.cameron, dave.jiang, alison.schofield,
	vishal.l.verma, ira.weiny, dan.j.williams, linux-cxl,
	linux-kernel, tanghui20, zhangqiao22, judy.chenhui

On 23.10.24 10:56:10, Zicheng Qu wrote:
> In the devm_cxl_pmu_add() function, the variable rc might be
> uninitialized before its first use 'if (rc) goto err', leading
> to undefined behavior since its value depends on the compiler.
> Currently, the switch statement is limited to the CXL_PMU_MEMDEV type.
> If additional types are introduced, it could lead to similar concerns.
> If the type range remains unchanged, using a switch case is unnecessary.
> To enhance code extensibility and stability, it is recommended to
> address this potential aspect.
> 
> Cc: stable@vger.kernel.org # v6.6+
> Fixes: 1ad3f701c399 ("cxl/pci: Find and register CXL PMU devices")
> Signed-off-by: Zicheng Qu <quzicheng@huawei.com>
> ---
>  drivers/cxl/core/pmu.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/cxl/core/pmu.c b/drivers/cxl/core/pmu.c
> index 5d8e06b0ba6e..2d12887c9915 100644
> --- a/drivers/cxl/core/pmu.c
> +++ b/drivers/cxl/core/pmu.c
> @@ -51,6 +51,9 @@ int devm_cxl_pmu_add(struct device *parent, struct cxl_pmu_regs *regs,
>  	case CXL_PMU_MEMDEV:
>  		rc = dev_set_name(dev, "pmu_mem%d.%d", assoc_id, index);
>  		break;
> +	default:
> +		rc = -EINVAL;
> +		break;

You might consider to use the -ENOENT error code and/or to
preinitialize rc with the error code. The default case can be dropped
then.

However, all those variants look ok:

Reviewed-by: Robert Richter <rrichter@amd.com>

>  	}
>  	if (rc)
>  		goto err;
> -- 
> 2.34.1
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] cxl: core-pmu: Fix the usage of uninitialized variable
  2024-10-23 10:56 [PATCH] cxl: core-pmu: Fix the usage of uninitialized variable Zicheng Qu
  2024-10-23 12:26 ` Robert Richter
@ 2024-10-24  9:38 ` Jonathan Cameron
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2024-10-24  9:38 UTC (permalink / raw)
  To: Zicheng Qu
  Cc: dave, dave.jiang, alison.schofield, vishal.l.verma, ira.weiny,
	dan.j.williams, linux-cxl, linux-kernel, tanghui20, zhangqiao22,
	judy.chenhui

On Wed, 23 Oct 2024 10:56:10 +0000
Zicheng Qu <quzicheng@huawei.com> wrote:

> In the devm_cxl_pmu_add() function, the variable rc might be
> uninitialized before its first use 'if (rc) goto err', leading
> to undefined behavior since its value depends on the compiler.
> Currently, the switch statement is limited to the CXL_PMU_MEMDEV type.
> If additional types are introduced, it could lead to similar concerns.
> If the type range remains unchanged, using a switch case is unnecessary.
> To enhance code extensibility and stability, it is recommended to
> address this potential aspect.
> 
> Cc: stable@vger.kernel.org # v6.6+
> Fixes: 1ad3f701c399 ("cxl/pci: Find and register CXL PMU devices")

Not a fix, because this is only called for CXL_PMU_MEMDEV.
So drop stable and fixes tag as I don't see this as appropriate to
backport (unless I'm missing something!)

Reasonable to harden the code. These might turn up in other
types of device. Ports will be handled differently (part of the
portdrv rework that I need to get back to), but maybe someone
will put a CXL PMU on a type 2 device and call this function.

Jonathan
 

> Signed-off-by: Zicheng Qu <quzicheng@huawei.com>
> ---
>  drivers/cxl/core/pmu.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/cxl/core/pmu.c b/drivers/cxl/core/pmu.c
> index 5d8e06b0ba6e..2d12887c9915 100644
> --- a/drivers/cxl/core/pmu.c
> +++ b/drivers/cxl/core/pmu.c
> @@ -51,6 +51,9 @@ int devm_cxl_pmu_add(struct device *parent, struct cxl_pmu_regs *regs,
>  	case CXL_PMU_MEMDEV:
>  		rc = dev_set_name(dev, "pmu_mem%d.%d", assoc_id, index);
>  		break;
> +	default:
> +		rc = -EINVAL;
> +		break;
>  	}
>  	if (rc)
>  		goto err;


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-24  9:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-23 10:56 [PATCH] cxl: core-pmu: Fix the usage of uninitialized variable Zicheng Qu
2024-10-23 12:26 ` Robert Richter
2024-10-24  9:38 ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox