Blob Blame History Raw
From ce9afe08e71e3f7d64f337a6e932e50849230fc2 Mon Sep 17 00:00:00 2001
From: "Gautham R. Shenoy" <ego@linux.vnet.ibm.com>
Date: Fri, 8 Mar 2019 21:03:24 +0530
Subject: [PATCH] powerpc/pseries/energy: Use OF accessor functions to read
 ibm,drc-indexes

References: bsc#1129080
Patch-mainline: v5.1-rc3
Git-commit: ce9afe08e71e3f7d64f337a6e932e50849230fc2

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: 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>
[mpe: Make the WARN_ON a WARN_ON_ONCE so it's not retriggerable]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Acked-by: Michal Suchanek <msuchanek@suse.de>
---
 .../platforms/pseries/pseries_energy.c        | 28 +++++++++++--------
 1 file changed, 17 insertions(+), 11 deletions(-)

--- a/arch/powerpc/platforms/pseries/pseries_energy.c
+++ b/arch/powerpc/platforms/pseries/pseries_energy.c
@@ -38,27 +38,33 @@ static int sysfs_entries;
 static u32 cpu_to_drc_index(int cpu)
 {
 	struct device_node *dn = NULL;
-	const int *indexes;
+	u32 nr_drc_indexes, i_drc_index;
 	int i;
-	int rc = 1;
+	int rc;
 	u32 ret = 0;
 
 	dn = of_find_node_by_path("/cpus");
 	if (dn == NULL)
 		goto err;
-	indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
-	if (indexes == NULL)
-		goto err_of_node_put;
+
 	/* Convert logical cpu number to core number */
 	i = cpu_core_index_of_thread(cpu);
+
 	/*
-	 * The first element indexes[0] is the number of drc_indexes
-	 * returned in the list.  Hence i+1 will get the drc_index
-	 * corresponding to core number i.
+	 * The first element of "ibm,drc-indexes" is the number of
+	 * drc_indexes returned in the list.  Hence i + 1 will get the
+	 * drc_index corresponding to core number i.
 	 */
-	WARN_ON(i > indexes[0]);
-	ret = indexes[i + 1];
-	rc = 0;
+	rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
+					0, &nr_drc_indexes);
+	if (rc)
+		goto err_of_node_put;
+
+	WARN_ON_ONCE(i > nr_drc_indexes);
+	rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
+					i + 1, &i_drc_index);
+	if (!rc)
+		ret = i_drc_index;
 
 err_of_node_put:
 	of_node_put(dn);
-- 
2.20.1