linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] pseries/energy: Use OF accessor functions to read ibm, drc-indexes
@ 2019-03-08 15:33 Gautham R. Shenoy
  2019-03-08 16:01 ` [PATCH] pseries/energy: Use OF accessor functions to read ibm,drc-indexes Vaidyanathan Srinivasan
  2019-03-31 10:13 ` pseries/energy: Use OF accessor functions to read ibm, drc-indexes Michael Ellerman
  0 siblings, 2 replies; 3+ messages in thread
From: Gautham R. Shenoy @ 2019-03-08 15:33 UTC (permalink / raw)
  To: Michael Bringmann, Benjamin Herrenschmidt, Paul Mackerras,
	Michael Ellerman, Vaidyanathan Srinivasan
  Cc: Pavithra R. Prakash, linuxppc-dev, linux-kernel, stable,
	Gautham R. Shenoy

From: "Gautham R. Shenoy" <ego@linux.vnet.ibm.com>

In cpu_to_drc_index() in the case when FW_FEATURE_DRC_INFO is absent,
we currently use of_read_property() to obtain the pointer to the array
corresponding to the property "ibm,drc-indexes". The elements of this
array are of type __be32, but are accessed without any conversion to
the OS-endianness, which is buggy on a Little Endian OS.

Fix this by using of_property_read_u32_index() accessor function to
safely read the elements of the array.

Fixes: commit e83636ac3334 ("pseries/drc-info: Search DRC properties for CPU indexes")
Cc: <stable@vger.kernel.org> #v4.16+
Reported-by: Pavithra R. Prakash <pavrampu@in.ibm.com>
Signed-off-by: Gautham R. Shenoy <ego@linux.vnet.ibm.com>
---
 arch/powerpc/platforms/pseries/pseries_energy.c | 27 ++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/arch/powerpc/platforms/pseries/pseries_energy.c b/arch/powerpc/platforms/pseries/pseries_energy.c
index 6ed2212..1c4d1ba 100644
--- a/arch/powerpc/platforms/pseries/pseries_energy.c
+++ b/arch/powerpc/platforms/pseries/pseries_energy.c
@@ -77,18 +77,27 @@ static u32 cpu_to_drc_index(int cpu)
 
 		ret = drc.drc_index_start + (thread_index * drc.sequential_inc);
 	} else {
-		const __be32 *indexes;
-
-		indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
-		if (indexes == NULL)
-			goto err_of_node_put;
+		u32 nr_drc_indexes, thread_drc_index;
 
 		/*
-		 * The first element indexes[0] is the number of drc_indexes
-		 * returned in the list.  Hence thread_index+1 will get the
-		 * drc_index corresponding to core number thread_index.
+		 * The first element of ibm,drc-indexes array is the
+		 * number of drc_indexes returned in the list.  Hence
+		 * thread_index+1 will get the drc_index corresponding
+		 * to core number thread_index.
 		 */
-		ret = indexes[thread_index + 1];
+		rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
+						0, &nr_drc_indexes);
+		if (rc)
+			goto err_of_node_put;
+
+		WARN_ON(thread_index > nr_drc_indexes);
+		rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
+						thread_index + 1,
+						&thread_drc_index);
+		if (rc)
+			goto err_of_node_put;
+
+		ret = thread_drc_index;
 	}
 
 	rc = 0;
-- 
1.9.4


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

* Re: [PATCH] pseries/energy: Use OF accessor functions to read ibm,drc-indexes
  2019-03-08 15:33 [PATCH] pseries/energy: Use OF accessor functions to read ibm, drc-indexes Gautham R. Shenoy
@ 2019-03-08 16:01 ` Vaidyanathan Srinivasan
  2019-03-31 10:13 ` pseries/energy: Use OF accessor functions to read ibm, drc-indexes Michael Ellerman
  1 sibling, 0 replies; 3+ messages in thread
From: Vaidyanathan Srinivasan @ 2019-03-08 16:01 UTC (permalink / raw)
  To: Gautham R. Shenoy
  Cc: Pavithra R. Prakash, linux-kernel, stable, Michael Bringmann,
	Paul Mackerras, linuxppc-dev

* Gautham R Shenoy <ego@linux.vnet.ibm.com> [2019-03-08 21:03:24]:

> From: "Gautham R. Shenoy" <ego@linux.vnet.ibm.com>
> 
> In cpu_to_drc_index() in the case when FW_FEATURE_DRC_INFO is absent,
> we currently use of_read_property() to obtain the pointer to the array
> corresponding to the property "ibm,drc-indexes". The elements of this
> array are of type __be32, but are accessed without any conversion to
> the OS-endianness, which is buggy on a Little Endian OS.
> 
> Fix this by using of_property_read_u32_index() accessor function to
> safely read the elements of the array.
> 
> Fixes: commit e83636ac3334 ("pseries/drc-info: Search DRC properties for CPU indexes")
> Cc: <stable@vger.kernel.org> #v4.16+
> Reported-by: Pavithra R. Prakash <pavrampu@in.ibm.com>
> Signed-off-by: Gautham R. Shenoy <ego@linux.vnet.ibm.com>

Reviewed-by: Vaidyanathan Srinivasan <svaidy@linux.vnet.ibm.com>

> ---
>  arch/powerpc/platforms/pseries/pseries_energy.c | 27 ++++++++++++++++---------
>  1 file changed, 18 insertions(+), 9 deletions(-)
> 
> diff --git a/arch/powerpc/platforms/pseries/pseries_energy.c b/arch/powerpc/platforms/pseries/pseries_energy.c
> index 6ed2212..1c4d1ba 100644
> --- a/arch/powerpc/platforms/pseries/pseries_energy.c
> +++ b/arch/powerpc/platforms/pseries/pseries_energy.c
> @@ -77,18 +77,27 @@ static u32 cpu_to_drc_index(int cpu)
> 
>  		ret = drc.drc_index_start + (thread_index * drc.sequential_inc);
>  	} else {
> -		const __be32 *indexes;
> -
> -		indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
> -		if (indexes == NULL)
> -			goto err_of_node_put;
> +		u32 nr_drc_indexes, thread_drc_index;
> 
>  		/*
> -		 * The first element indexes[0] is the number of drc_indexes
> -		 * returned in the list.  Hence thread_index+1 will get the
> -		 * drc_index corresponding to core number thread_index.
> +		 * The first element of ibm,drc-indexes array is the
> +		 * number of drc_indexes returned in the list.  Hence
> +		 * thread_index+1 will get the drc_index corresponding
> +		 * to core number thread_index.
>  		 */
> -		ret = indexes[thread_index + 1];
> +		rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
> +						0, &nr_drc_indexes);
> +		if (rc)
> +			goto err_of_node_put;
> +
> +		WARN_ON(thread_index > nr_drc_indexes);
> +		rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
> +						thread_index + 1,
> +						&thread_drc_index);
> +		if (rc)
> +			goto err_of_node_put;
> +
> +		ret = thread_drc_index;

Oops!  Good bugfix.  We should use device tree accessors like this in
all places for correct and safe code.

Thanks!

--Vaidy


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

* Re: pseries/energy: Use OF accessor functions to read ibm, drc-indexes
  2019-03-08 15:33 [PATCH] pseries/energy: Use OF accessor functions to read ibm, drc-indexes Gautham R. Shenoy
  2019-03-08 16:01 ` [PATCH] pseries/energy: Use OF accessor functions to read ibm,drc-indexes Vaidyanathan Srinivasan
@ 2019-03-31 10:13 ` Michael Ellerman
  1 sibling, 0 replies; 3+ messages in thread
From: Michael Ellerman @ 2019-03-31 10:13 UTC (permalink / raw)
  To: Gautham R. Shenoy, Michael Bringmann, Benjamin Herrenschmidt,
	Paul Mackerras, Vaidyanathan Srinivasan
  Cc: Pavithra R. Prakash, linuxppc-dev, linux-kernel, stable,
	Gautham R. Shenoy

On Fri, 2019-03-08 at 15:33:24 UTC, "Gautham R. Shenoy" wrote:
> From: "Gautham R. Shenoy" <ego@linux.vnet.ibm.com>
> 
> In cpu_to_drc_index() in the case when FW_FEATURE_DRC_INFO is absent,
> we currently use of_read_property() to obtain the pointer to the array
> corresponding to the property "ibm,drc-indexes". The elements of this
> array are of type __be32, but are accessed without any conversion to
> the OS-endianness, which is buggy on a Little Endian OS.
> 
> Fix this by using of_property_read_u32_index() accessor function to
> safely read the elements of the array.
> 
> Fixes: commit e83636ac3334 ("pseries/drc-info: Search DRC properties for CPU indexes")
> Cc: <stable@vger.kernel.org> #v4.16+
> Reported-by: Pavithra R. Prakash <pavrampu@in.ibm.com>
> Signed-off-by: Gautham R. Shenoy <ego@linux.vnet.ibm.com>
> Reviewed-by: Vaidyanathan Srinivasan <svaidy@linux.vnet.ibm.com>

Applied to powerpc fixes, thanks.

https://git.kernel.org/powerpc/c/ce9afe08e71e3f7d64f337a6e932e508

cheers

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

end of thread, other threads:[~2019-03-31 10:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-03-08 15:33 [PATCH] pseries/energy: Use OF accessor functions to read ibm, drc-indexes Gautham R. Shenoy
2019-03-08 16:01 ` [PATCH] pseries/energy: Use OF accessor functions to read ibm,drc-indexes Vaidyanathan Srinivasan
2019-03-31 10:13 ` pseries/energy: Use OF accessor functions to read ibm, drc-indexes Michael Ellerman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).