[PATCH -next RFC 11/16] cpuset: simplify partition update logic for hotplug tasks

Chen Ridong posted 16 patches 4 months, 2 weeks ago
There is a newer version of this series
[PATCH -next RFC 11/16] cpuset: simplify partition update logic for hotplug tasks
Posted by Chen Ridong 4 months, 2 weeks ago
From: Chen Ridong <chenridong@huawei.com>

Simplify the partition update logic in cpuset_hotplug_update_tasks() by
calling the unified local_partition_update() interface.

For local partitions, the previous patch introduced local_partition_update
which handles both validation state transitions:
- Invalidates local partitions that fail validation checks
- Transitions invalid partitions to valid state when no errors are detected

This eliminates the need for separate transition logic
in cpuset_hotplug_update_tasks(), which can now simply call
local_partition_update() to handle all local partition changes.

This patch simplifies the logic by always proceeding to update_tasks for
remote partitions, regardless of whether they were disabled or not. Since
the original code didn't perform any meaningful operations for non-disabled
remote partitions, this change should not affect functionality.

The partition_cmd mechanism can now be safely removed as it is no longer
referenced by any code paths after the partition update logic
simplification.

Signed-off-by: Chen Ridong <chenridong@huawei.com>
---
 kernel/cgroup/cpuset.c | 67 ++++++++++++++++--------------------------
 1 file changed, 26 insertions(+), 41 deletions(-)

diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 9e98df542715..a1896a199c8b 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -1211,17 +1211,6 @@ static void compute_effective_cpumask(struct cpumask *new_cpus,
 	cpumask_and(new_cpus, cs->cpus_allowed, parent->effective_cpus);
 }
 
-/*
- * Commands for update_parent_effective_cpumask
- */
-enum partition_cmd {
-	partcmd_enable,		/* Enable partition root	  */
-	partcmd_enablei,	/* Enable isolated partition root */
-	partcmd_disable,	/* Disable partition root	  */
-	partcmd_update,		/* Update parent's effective_cpus */
-	partcmd_invalidate,	/* Make partition invalid	  */
-};
-
 static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs,
 				    struct tmpmasks *tmp);
 
@@ -2062,6 +2051,9 @@ static int __local_partition_update(struct cpuset *cs, struct cpumask *xcpus,
 		update_partition_sd_lb(cs, old_prs);
 		return part_error;
 	}
+	/* Nothing changes, return PERR_NONE */
+	if (new_prs == old_prs && cpumask_equal(excpus, cs->effective_xcpus))
+		return PERR_NONE;
 
 	cpumask_updated = cpumask_andnot(tmp->addmask, excpus, cs->effective_xcpus);
 	cpumask_updated |= cpumask_andnot(tmp->delmask, cs->effective_xcpus, excpus);
@@ -3868,8 +3860,6 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)
 	static nodemask_t new_mems;
 	bool cpus_updated;
 	bool mems_updated;
-	bool remote;
-	int partcmd = -1;
 	struct cpuset *parent;
 retry:
 	wait_event(cpuset_attach_wq, cs->attach_in_progress == 0);
@@ -3896,16 +3886,15 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)
 	 * Compute effective_cpus for valid partition root, may invalidate
 	 * child partition roots if necessary.
 	 */
-	remote = is_remote_partition(cs);
-	if (remote || (is_partition_valid(cs) && is_partition_valid(parent)))
+	if (is_remote_partition(cs)) {
 		compute_partition_effective_cpumask(cs, &new_cpus);
-
-	if (remote && cpumask_empty(&new_cpus) &&
-	    partition_is_populated(cs, NULL)) {
-		cs->prs_err = PERR_HOTPLUG;
-		remote_partition_disable(cs, tmp);
-		compute_effective_cpumask(&new_cpus, cs, parent);
-		remote = false;
+		if (cpumask_empty(&new_cpus) &&
+		    partition_is_populated(cs, NULL)) {
+			cs->prs_err = PERR_HOTPLUG;
+			remote_partition_disable(cs, tmp);
+			compute_effective_cpumask(&new_cpus, cs, parent);
+		}
+		goto update_tasks;
 	}
 
 	/*
@@ -3913,28 +3902,24 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)
 	 * the following conditions hold:
 	 * 1) empty effective cpus but not valid empty partition.
 	 * 2) parent is invalid or doesn't grant any cpus to child
-	 *    partitions.
-	 */
-	if (is_local_partition(cs) && (!is_partition_valid(parent) ||
-				tasks_nocpu_error(parent, cs, &new_cpus))) {
-		partcmd = partcmd_invalidate;
-		local_partition_invalidate(cs, tmp);
-	}
-	/*
+	 *  partitions.
+	 *
 	 * On the other hand, an invalid partition root may be transitioned
 	 * back to a regular one with a non-empty user xcpus.
+	 *
+	 * local_partition_update can handle these cases.
 	 */
-	else if (is_partition_valid(parent) && is_partition_invalid(cs) &&
-		 !cpumask_empty(user_xcpus(cs))) {
-		partcmd = partcmd_update;
-		local_partition_update(cs, tmp);
-	}
-	if (partcmd >= 0) {
-		if ((partcmd == partcmd_invalidate) || is_partition_valid(cs)) {
-			compute_partition_effective_cpumask(cs, &new_cpus);
-			cpuset_force_rebuild();
-		}
-	}
+	local_partition_update(cs, tmp);
+
+	/*
+	 * Recompute effective CPU mask after partition state update:
+	 * - For valid partitions: calculate partition-specific effective CPUs
+	 * - For invalid partitions: compute member effective CPU mask
+	 */
+	if (is_partition_valid(cs))
+		compute_partition_effective_cpumask(cs, &new_cpus);
+	else
+		compute_effective_cpumask(&new_cpus, cs, parent);
 
 update_tasks:
 	cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus);
-- 
2.34.1
Re: [PATCH -next RFC 11/16] cpuset: simplify partition update logic for hotplug tasks
Posted by Waiman Long 3 months, 3 weeks ago
On 9/28/25 3:13 AM, Chen Ridong wrote:
> From: Chen Ridong <chenridong@huawei.com>
>
> Simplify the partition update logic in cpuset_hotplug_update_tasks() by
> calling the unified local_partition_update() interface.
>
> For local partitions, the previous patch introduced local_partition_update
> which handles both validation state transitions:
> - Invalidates local partitions that fail validation checks
> - Transitions invalid partitions to valid state when no errors are detected
>
> This eliminates the need for separate transition logic
> in cpuset_hotplug_update_tasks(), which can now simply call
> local_partition_update() to handle all local partition changes.
>
> This patch simplifies the logic by always proceeding to update_tasks for
> remote partitions, regardless of whether they were disabled or not. Since
> the original code didn't perform any meaningful operations for non-disabled
> remote partitions, this change should not affect functionality.
>
> The partition_cmd mechanism can now be safely removed as it is no longer

It is partition_cmd enum type.

> referenced by any code paths after the partition update logic
> simplification.
>
> Signed-off-by: Chen Ridong <chenridong@huawei.com>
> ---
>   kernel/cgroup/cpuset.c | 67 ++++++++++++++++--------------------------
>   1 file changed, 26 insertions(+), 41 deletions(-)
>
> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index 9e98df542715..a1896a199c8b 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
> @@ -1211,17 +1211,6 @@ static void compute_effective_cpumask(struct cpumask *new_cpus,
>   	cpumask_and(new_cpus, cs->cpus_allowed, parent->effective_cpus);
>   }
>   
> -/*
> - * Commands for update_parent_effective_cpumask
> - */
> -enum partition_cmd {
> -	partcmd_enable,		/* Enable partition root	  */
> -	partcmd_enablei,	/* Enable isolated partition root */
> -	partcmd_disable,	/* Disable partition root	  */
> -	partcmd_update,		/* Update parent's effective_cpus */
> -	partcmd_invalidate,	/* Make partition invalid	  */
> -};
> -
>   static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs,
>   				    struct tmpmasks *tmp);
>   
> @@ -2062,6 +2051,9 @@ static int __local_partition_update(struct cpuset *cs, struct cpumask *xcpus,
>   		update_partition_sd_lb(cs, old_prs);
>   		return part_error;
>   	}
> +	/* Nothing changes, return PERR_NONE */
> +	if (new_prs == old_prs && cpumask_equal(excpus, cs->effective_xcpus))
> +		return PERR_NONE;
I believe you already have this check added when you introduce 
__local_partition_update() in patch 9. It is a duplicate.

Cheers,
Longman
Re: [PATCH -next RFC 11/16] cpuset: simplify partition update logic for hotplug tasks
Posted by Chen Ridong 3 months, 3 weeks ago

On 2025/10/20 11:00, Waiman Long wrote:
> 
> On 9/28/25 3:13 AM, Chen Ridong wrote:
>> From: Chen Ridong <chenridong@huawei.com>
>>
>> Simplify the partition update logic in cpuset_hotplug_update_tasks() by
>> calling the unified local_partition_update() interface.
>>
>> For local partitions, the previous patch introduced local_partition_update
>> which handles both validation state transitions:
>> - Invalidates local partitions that fail validation checks
>> - Transitions invalid partitions to valid state when no errors are detected
>>
>> This eliminates the need for separate transition logic
>> in cpuset_hotplug_update_tasks(), which can now simply call
>> local_partition_update() to handle all local partition changes.
>>
>> This patch simplifies the logic by always proceeding to update_tasks for
>> remote partitions, regardless of whether they were disabled or not. Since
>> the original code didn't perform any meaningful operations for non-disabled
>> remote partitions, this change should not affect functionality.
>>
>> The partition_cmd mechanism can now be safely removed as it is no longer
> 
> It is partition_cmd enum type.
> 

Thank you,

Will update.

>> referenced by any code paths after the partition update logic
>> simplification.
>>
>> Signed-off-by: Chen Ridong <chenridong@huawei.com>
>> ---
>>   kernel/cgroup/cpuset.c | 67 ++++++++++++++++--------------------------
>>   1 file changed, 26 insertions(+), 41 deletions(-)
>>
>> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
>> index 9e98df542715..a1896a199c8b 100644
>> --- a/kernel/cgroup/cpuset.c
>> +++ b/kernel/cgroup/cpuset.c
>> @@ -1211,17 +1211,6 @@ static void compute_effective_cpumask(struct cpumask *new_cpus,
>>       cpumask_and(new_cpus, cs->cpus_allowed, parent->effective_cpus);
>>   }
>>   -/*
>> - * Commands for update_parent_effective_cpumask
>> - */
>> -enum partition_cmd {
>> -    partcmd_enable,        /* Enable partition root      */
>> -    partcmd_enablei,    /* Enable isolated partition root */
>> -    partcmd_disable,    /* Disable partition root      */
>> -    partcmd_update,        /* Update parent's effective_cpus */
>> -    partcmd_invalidate,    /* Make partition invalid      */
>> -};
>> -
>>   static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs,
>>                       struct tmpmasks *tmp);
>>   @@ -2062,6 +2051,9 @@ static int __local_partition_update(struct cpuset *cs, struct cpumask *xcpus,
>>           update_partition_sd_lb(cs, old_prs);
>>           return part_error;
>>       }
>> +    /* Nothing changes, return PERR_NONE */
>> +    if (new_prs == old_prs && cpumask_equal(excpus, cs->effective_xcpus))
>> +        return PERR_NONE;
> I believe you already have this check added when you introduce __local_partition_update() in patch
> 9. It is a duplicate.
> 
> Cheers,
> Longman

Thank you,

Will update.

-- 
Best regards,
Ridong