diff mbox series

[RFC,1/9] powerpc/pseries: add cpu DLPAR support for drc-info property

Message ID 1569910334-5972-2-git-send-email-tyreld@linux.ibm.com (mailing list archive)
State Superseded, archived
Headers show
Series Fixes and Enablement of ibm,drc-info property | expand

Commit Message

Tyrel Datwyler Oct. 1, 2019, 6:12 a.m. UTC
From: Tyrel Datwyler <tyreld@linux.vnet.ibm.com>

Older firmwares provided information about Dynamic Reconfig
Connectors (DRC) through several device tree properties, namely
ibm,drc-types, ibm,drc-indexes, ibm,drc-names, and
ibm,drc-power-domains. New firmwares have the ability to present this
same information in a much condensed format through a device tree
property called ibm,drc-info.

The existing cpu DLPAR hotplug code only understands the older DRC
property format when validating the drc-index of a cpu during a
hotplug add. This updates those code paths to use the ibm,drc-info
property, when present, instead for validation.

Signed-off-by: Tyrel Datwyler <tyreld@linux.ibm.com>
---
 arch/powerpc/platforms/pseries/hotplug-cpu.c | 117 ++++++++++++++++++++-------
 1 file changed, 89 insertions(+), 28 deletions(-)

Comments

Nathan Lynch Oct. 10, 2019, 6:56 p.m. UTC | #1
Hi Tyrel,

Tyrel Datwyler <tyreld@linux.ibm.com> writes:
> +static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
> +{
> +	const __be32 *indexes;
> +	int i;
> +
> +	if (of_find_property(parent, "ibm,drc-info", NULL))
> +		return drc_info_valid_index(parent, drc_index);
> +
> +	indexes = of_get_property(parent, "ibm,drc-indexes", NULL);
> +	if (!indexes)
> +		return false;
> +
> +	for (i = 0; i < indexes[0]; i++) {

should this be:

        for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
?


> +		if (be32_to_cpu(indexes[i + 1]) == drc_index)
> +			return true;
> +	}
> +
> +	return false;
>  }

It looks like this rewrites valid_cpu_drc_index()'s existing code for
parsing ibm,drc-indexes but I don't see the need for this.

This patch would be easier to review if that were dropped or split out.

>  
>  static ssize_t dlpar_cpu_add(u32 drc_index)
> @@ -720,8 +756,11 @@ static int dlpar_cpu_remove_by_count(u32 cpus_to_remove)
>  static int find_dlpar_cpus_to_add(u32 *cpu_drcs, u32 cpus_to_add)
>  {
>  	struct device_node *parent;
> +	struct property *info;
> +	const __be32 *indexes;
>  	int cpus_found = 0;
> -	int index, rc;
> +	int i, j;
> +	u32 drc_index;
>  
>  	parent = of_find_node_by_path("/cpus");
>  	if (!parent) {
> @@ -730,24 +769,46 @@ static int find_dlpar_cpus_to_add(u32 *cpu_drcs, u32 cpus_to_add)
>  		return -1;
>  	}
>  
> -	/* Search the ibm,drc-indexes array for possible CPU drcs to
> -	 * add. Note that the format of the ibm,drc-indexes array is
> -	 * the number of entries in the array followed by the array
> -	 * of drc values so we start looking at index = 1.
> -	 */
> -	index = 1;
> -	while (cpus_found < cpus_to_add) {
> -		u32 drc;
> +	info = of_find_property(parent, "ibm,drc-info", NULL);
> +	if (info) {
> +		struct of_drc_info drc;
> +		const __be32 *value;
> +		int count;
>  
> -		rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
> -						index++, &drc);
> -		if (rc)
> -			break;
> +		value = of_prop_next_u32(info, NULL, &count);
> +		if (value)
> +			value++;
>  
> -		if (dlpar_cpu_exists(parent, drc))
> -			continue;
> +		for (i = 0; i < count; i++) {
> +			of_read_drc_info_cell(&info, &value, &drc);
> +			if (strncmp(drc.drc_type, "CPU", 3))
> +				break;
> +
> +			for (j = 0; j < drc.num_sequential_elems; j++) {
> +				drc_index = drc.drc_index_start + (drc.sequential_inc * j);
> +
> +				if (dlpar_cpu_exists(parent, drc_index))
> +					continue;
>  
> -		cpu_drcs[cpus_found++] = drc;
> +				cpu_drcs[cpus_found++] = drc_index;

I am failing to see how this loop is limited by the cpus_to_add
parameter as it was before this change. It looks like this will overflow
the cpu_drcs array when cpus_to_add is less than the number of cpus
found.

As an aside I don't understand how the add_by_count()/dlpar_cpu_exists()
algorithm could be correct as it currently stands. It seems to pick the
first X indexes for which a corresponding cpu node is absent, but that
set of indexes does not necessarily match the set that is available to
configure. Something to address separately I suppose.

> +			}
> +		}
> +	} else {
> +		indexes = of_get_property(parent, "ibm,drc-indexes", NULL);
> +
> +		/* Search the ibm,drc-indexes array for possible CPU drcs to
> +	 	* add. Note that the format of the ibm,drc-indexes array is
> +	 	* the number of entries in the array followed by the array
> +	 	* of drc values so we start looking at index = 1.
> +	 	*/
> +		for (i = 1; i < indexes[0]; i++) {
> +			drc_index = be32_to_cpu(indexes[i]);
> +
> +			if (dlpar_cpu_exists(parent, drc_index))
> +				continue;
> +
> +			cpu_drcs[cpus_found++] = drc_index;
> +		}
>  	}

As above, not sure why this was rewritten, and similar comments as
before apply.
Tyrel Datwyler Oct. 30, 2019, 11:35 p.m. UTC | #2
On 10/10/19 11:56 AM, Nathan Lynch wrote:
> Hi Tyrel,
> 
> Tyrel Datwyler <tyreld@linux.ibm.com> writes:
>> +static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
>> +{
>> +	const __be32 *indexes;
>> +	int i;
>> +
>> +	if (of_find_property(parent, "ibm,drc-info", NULL))
>> +		return drc_info_valid_index(parent, drc_index);
>> +
>> +	indexes = of_get_property(parent, "ibm,drc-indexes", NULL);
>> +	if (!indexes)
>> +		return false;
>> +
>> +	for (i = 0; i < indexes[0]; i++) {
> 
> should this be:
> 
>         for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
> ?

Yes!

> 
> 
>> +		if (be32_to_cpu(indexes[i + 1]) == drc_index)
>> +			return true;
>> +	}
>> +
>> +	return false;
>>  }
> 
> It looks like this rewrites valid_cpu_drc_index()'s existing code for
> parsing ibm,drc-indexes but I don't see the need for this.
> 
> This patch would be easier to review if that were dropped or split out.

Yeah, I'll split it out. There are multiple places where we iterate over the
drc_indexes, and it is implemented several different ways. I basically picked an
implementation to use across the board. I think a better way would be just to
implement a for_each_drc_index(dn, drc_index) macro to abstract away iterator
implementation.

> 
>>  
>>  static ssize_t dlpar_cpu_add(u32 drc_index)
>> @@ -720,8 +756,11 @@ static int dlpar_cpu_remove_by_count(u32 cpus_to_remove)
>>  static int find_dlpar_cpus_to_add(u32 *cpu_drcs, u32 cpus_to_add)
>>  {
>>  	struct device_node *parent;
>> +	struct property *info;
>> +	const __be32 *indexes;
>>  	int cpus_found = 0;
>> -	int index, rc;
>> +	int i, j;
>> +	u32 drc_index;
>>  
>>  	parent = of_find_node_by_path("/cpus");
>>  	if (!parent) {
>> @@ -730,24 +769,46 @@ static int find_dlpar_cpus_to_add(u32 *cpu_drcs, u32 cpus_to_add)
>>  		return -1;
>>  	}
>>  
>> -	/* Search the ibm,drc-indexes array for possible CPU drcs to
>> -	 * add. Note that the format of the ibm,drc-indexes array is
>> -	 * the number of entries in the array followed by the array
>> -	 * of drc values so we start looking at index = 1.
>> -	 */
>> -	index = 1;
>> -	while (cpus_found < cpus_to_add) {
>> -		u32 drc;
>> +	info = of_find_property(parent, "ibm,drc-info", NULL);
>> +	if (info) {
>> +		struct of_drc_info drc;
>> +		const __be32 *value;
>> +		int count;
>>  
>> -		rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
>> -						index++, &drc);
>> -		if (rc)
>> -			break;
>> +		value = of_prop_next_u32(info, NULL, &count);
>> +		if (value)
>> +			value++;
>>  
>> -		if (dlpar_cpu_exists(parent, drc))
>> -			continue;
>> +		for (i = 0; i < count; i++) {
>> +			of_read_drc_info_cell(&info, &value, &drc);
>> +			if (strncmp(drc.drc_type, "CPU", 3))
>> +				break;
>> +
>> +			for (j = 0; j < drc.num_sequential_elems; j++) {
>> +				drc_index = drc.drc_index_start + (drc.sequential_inc * j);
>> +
>> +				if (dlpar_cpu_exists(parent, drc_index))
>> +					continue;
>>  
>> -		cpu_drcs[cpus_found++] = drc;
>> +				cpu_drcs[cpus_found++] = drc_index;
> 
> I am failing to see how this loop is limited by the cpus_to_add
> parameter as it was before this change. It looks like this will overflow
> the cpu_drcs array when cpus_to_add is less than the number of cpus
> found.

You are right. The code is picking every non-present drc_index which will
overflow the supplied buffer as you stated when there are more available indexes
than requested cpus. Will fix to bound the search.

> 
> As an aside I don't understand how the add_by_count()/dlpar_cpu_exists()
> algorithm could be correct as it currently stands. It seems to pick the
> first X indexes for which a corresponding cpu node is absent, but that
> set of indexes does not necessarily match the set that is available to
> configure. Something to address separately I suppose.

I'm not sure I follow?

> 
>> +			}
>> +		}
>> +	} else {
>> +		indexes = of_get_property(parent, "ibm,drc-indexes", NULL);
>> +
>> +		/* Search the ibm,drc-indexes array for possible CPU drcs to
>> +	 	* add. Note that the format of the ibm,drc-indexes array is
>> +	 	* the number of entries in the array followed by the array
>> +	 	* of drc values so we start looking at index = 1.
>> +	 	*/
>> +		for (i = 1; i < indexes[0]; i++) {
>> +			drc_index = be32_to_cpu(indexes[i]);
>> +
>> +			if (dlpar_cpu_exists(parent, drc_index))
>> +				continue;
>> +
>> +			cpu_drcs[cpus_found++] = drc_index;
>> +		}
>>  	}
> 
> As above, not sure why this was rewritten, and similar comments as
> before apply.
> 

Again, wanted to use a single implementation everywere. Obviously, as pointed
out in the previous comment missed a byte swap. Will split out into a separate
patch for consideration.

-Tyrel
Nathan Lynch Oct. 31, 2019, 5:14 p.m. UTC | #3
Tyrel Datwyler <tyreld@linux.ibm.com> writes:
> On 10/10/19 11:56 AM, Nathan Lynch wrote:
>> As an aside I don't understand how the add_by_count()/dlpar_cpu_exists()
>> algorithm could be correct as it currently stands. It seems to pick the
>> first X indexes for which a corresponding cpu node is absent, but that
>> set of indexes does not necessarily match the set that is available to
>> configure. Something to address separately I suppose.
>
> I'm not sure I follow?

Don't worry about it for this patchset, it's just something I noticed
when reviewing. I'm wondering why the cpu add algorithm doesn't work
more like the one for memory, which actually queries the platform for
connector status via dlpar_acquire_drc(). It's possible I'm
misunderstanding something though. Like I said, I think it's something
to address separately if there is indeed an issue.
diff mbox series

Patch

diff --git a/arch/powerpc/platforms/pseries/hotplug-cpu.c b/arch/powerpc/platforms/pseries/hotplug-cpu.c
index bbda646..a2b6cd1 100644
--- a/arch/powerpc/platforms/pseries/hotplug-cpu.c
+++ b/arch/powerpc/platforms/pseries/hotplug-cpu.c
@@ -407,25 +407,61 @@  static bool dlpar_cpu_exists(struct device_node *parent, u32 drc_index)
 	return found;
 }
 
-static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
+static bool drc_info_valid_index(struct device_node *parent, u32 drc_index)
 {
-	bool found = false;
-	int rc, index;
+	struct property *info;
+	struct of_drc_info drc;
+	const __be32 *value;
+	int count, i, j;
 
-	index = 0;
-	while (!found) {
-		u32 drc;
+	info = of_find_property(parent, "ibm,drc-info", NULL);
+	if (!info)
+		return false;
 
-		rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
-						index++, &drc);
-		if (rc)
+	value = of_prop_next_u32(info, NULL, &count);
+
+	/* First value of ibm,drc-info is number of drc-info records */
+	if (value)
+		value++;
+	else
+		return false;
+
+	for (i = 0; i < count; i++) {
+		if (of_read_drc_info_cell(&info, &value, &drc))
+			return false;
+
+		if (strncmp(drc.drc_type, "CPU", 3))
 			break;
 
-		if (drc == drc_index)
-			found = true;
+		if (drc_index > drc.last_drc_index)
+			continue;
+
+		for (j = 0; j < drc.num_sequential_elems; j++)
+			if (drc_index == (drc.drc_index_start + (drc.sequential_inc * j)))
+					return true;
 	}
 
-	return found;
+	return false;
+}
+
+static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
+{
+	const __be32 *indexes;
+	int i;
+
+	if (of_find_property(parent, "ibm,drc-info", NULL))
+		return drc_info_valid_index(parent, drc_index);
+
+	indexes = of_get_property(parent, "ibm,drc-indexes", NULL);
+	if (!indexes)
+		return false;
+
+	for (i = 0; i < indexes[0]; i++) {
+		if (be32_to_cpu(indexes[i + 1]) == drc_index)
+			return true;
+	}
+
+	return false;
 }
 
 static ssize_t dlpar_cpu_add(u32 drc_index)
@@ -720,8 +756,11 @@  static int dlpar_cpu_remove_by_count(u32 cpus_to_remove)
 static int find_dlpar_cpus_to_add(u32 *cpu_drcs, u32 cpus_to_add)
 {
 	struct device_node *parent;
+	struct property *info;
+	const __be32 *indexes;
 	int cpus_found = 0;
-	int index, rc;
+	int i, j;
+	u32 drc_index;
 
 	parent = of_find_node_by_path("/cpus");
 	if (!parent) {
@@ -730,24 +769,46 @@  static int find_dlpar_cpus_to_add(u32 *cpu_drcs, u32 cpus_to_add)
 		return -1;
 	}
 
-	/* Search the ibm,drc-indexes array for possible CPU drcs to
-	 * add. Note that the format of the ibm,drc-indexes array is
-	 * the number of entries in the array followed by the array
-	 * of drc values so we start looking at index = 1.
-	 */
-	index = 1;
-	while (cpus_found < cpus_to_add) {
-		u32 drc;
+	info = of_find_property(parent, "ibm,drc-info", NULL);
+	if (info) {
+		struct of_drc_info drc;
+		const __be32 *value;
+		int count;
 
-		rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
-						index++, &drc);
-		if (rc)
-			break;
+		value = of_prop_next_u32(info, NULL, &count);
+		if (value)
+			value++;
 
-		if (dlpar_cpu_exists(parent, drc))
-			continue;
+		for (i = 0; i < count; i++) {
+			of_read_drc_info_cell(&info, &value, &drc);
+			if (strncmp(drc.drc_type, "CPU", 3))
+				break;
+
+			for (j = 0; j < drc.num_sequential_elems; j++) {
+				drc_index = drc.drc_index_start + (drc.sequential_inc * j);
+
+				if (dlpar_cpu_exists(parent, drc_index))
+					continue;
 
-		cpu_drcs[cpus_found++] = drc;
+				cpu_drcs[cpus_found++] = drc_index;
+			}
+		}
+	} else {
+		indexes = of_get_property(parent, "ibm,drc-indexes", NULL);
+
+		/* Search the ibm,drc-indexes array for possible CPU drcs to
+	 	* add. Note that the format of the ibm,drc-indexes array is
+	 	* the number of entries in the array followed by the array
+	 	* of drc values so we start looking at index = 1.
+	 	*/
+		for (i = 1; i < indexes[0]; i++) {
+			drc_index = be32_to_cpu(indexes[i]);
+
+			if (dlpar_cpu_exists(parent, drc_index))
+				continue;
+
+			cpu_drcs[cpus_found++] = drc_index;
+		}
 	}
 
 	of_node_put(parent);