[PATCH -next RFC 08/11] cpuset: refactor acpus_validate_change

Chen Ridong posted 11 patches 1 month ago
There is a newer version of this series
[PATCH -next RFC 08/11] cpuset: refactor acpus_validate_change
Posted by Chen Ridong 1 month ago
From: Chen Ridong <chenridong@huawei.com>

Refactor acpus_validate_change to handle the special case where
cpuset.cpus can be set even when violating partition sibling CPU
exclusivity rules. This differs from the general validation logic in
validate_change. Add a wrapper function to properly handle this
exceptional case.

Since partition invalidation status can be determined by trialcs->prs_err,
the local variable 'bool invalidate' can be removed.

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

diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 71190f142700..75ad18ab40ae 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -2410,43 +2410,11 @@ static bool invalidate_cs_partition(struct cpuset *cs)
 	return false;
 }
 
-/**
- * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
- * @cs: the cpuset to consider
- * @trialcs: trial cpuset
- * @buf: buffer of cpu numbers written to this cpuset
- */
-static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
-			  const char *buf)
+static int acpus_validate_change(struct cpuset *cs, struct cpuset *trialcs,
+					struct tmpmasks *tmp)
 {
 	int retval;
-	struct tmpmasks tmp;
 	struct cpuset *parent = parent_cs(cs);
-	bool invalidate = false;
-	bool force = false;
-	int old_prs = cs->partition_root_state;
-
-	retval = parse_cpulist(buf, trialcs->cpus_allowed);
-	if (retval < 0)
-		return retval;
-
-	/* Nothing to do if the cpus didn't change */
-	if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
-		return 0;
-
-	if (alloc_tmpmasks(&tmp))
-		return -ENOMEM;
-
-	compute_trialcs_excpus(trialcs, cs);
-
-	invalidate = invalidate_cs_partition(trialcs);
-	cs->prs_err = trialcs->prs_err;
-
-	/*
-	 * Check all the descendants in update_cpumasks_hier() if
-	 * effective_xcpus is to be changed.
-	 */
-	force = !cpumask_equal(cs->effective_xcpus, trialcs->effective_xcpus);
 
 	retval = validate_change(cs, trialcs);
 
@@ -2461,7 +2429,7 @@ static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
 		 * partition. However, any conflicting sibling partitions
 		 * have to be marked as invalid too.
 		 */
-		invalidate = true;
+		trialcs->prs_err = PERR_NOTEXCL;
 		rcu_read_lock();
 		cpuset_for_each_child(cp, css, parent) {
 			struct cpumask *xcpus = user_xcpus(trialcs);
@@ -2469,19 +2437,58 @@ static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
 			if (is_partition_valid(cp) &&
 			    cpumask_intersects(xcpus, cp->effective_xcpus)) {
 				rcu_read_unlock();
-				update_parent_effective_cpumask(cp, partcmd_invalidate, NULL, &tmp);
+				update_parent_effective_cpumask(cp, partcmd_invalidate, NULL, tmp);
 				rcu_read_lock();
 			}
 		}
 		rcu_read_unlock();
 		retval = 0;
 	}
+	return retval;
+}
 
+/**
+ * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
+ * @cs: the cpuset to consider
+ * @trialcs: trial cpuset
+ * @buf: buffer of cpu numbers written to this cpuset
+ */
+static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
+			  const char *buf)
+{
+	int retval;
+	struct tmpmasks tmp;
+	bool force = false;
+	int old_prs = cs->partition_root_state;
+
+	retval = parse_cpulist(buf, trialcs->cpus_allowed);
 	if (retval < 0)
+		return retval;
+
+	/* Nothing to do if the cpus didn't change */
+	if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
+		return 0;
+
+	if (alloc_tmpmasks(&tmp))
+		return -ENOMEM;
+
+	compute_trialcs_excpus(trialcs, cs);
+
+	trialcs->prs_err = PERR_NONE;
+	if (acpus_validate_change(cs, trialcs, &tmp) < 0)
 		goto out_free;
+	/*
+	 * Check all the descendants in update_cpumasks_hier() if
+	 * effective_xcpus is to be changed.
+	 */
+	force = !cpumask_equal(cs->effective_xcpus, trialcs->effective_xcpus);
+
+	invalidate_cs_partition(trialcs);
+	if (trialcs->prs_err)
+		cs->prs_err = trialcs->prs_err;
 
 	if (is_partition_valid(cs) ||
-	   (is_partition_invalid(cs) && !invalidate)) {
+	   (is_partition_invalid(cs) && !trialcs->prs_err)) {
 		struct cpumask *xcpus = trialcs->effective_xcpus;
 
 		if (cpumask_empty(xcpus) && is_partition_invalid(cs))
@@ -2492,7 +2499,7 @@ static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
 		 */
 		if (is_remote_partition(cs))
 			remote_cpus_update(cs, NULL, xcpus, &tmp);
-		else if (invalidate)
+		else if (trialcs->prs_err)
 			update_parent_effective_cpumask(cs, partcmd_invalidate,
 							NULL, &tmp);
 		else
-- 
2.34.1
Re: [PATCH -next RFC 08/11] cpuset: refactor acpus_validate_change
Posted by Waiman Long 1 month ago
On 8/28/25 8:56 AM, Chen Ridong wrote:
> From: Chen Ridong <chenridong@huawei.com>
>
> Refactor acpus_validate_change to handle the special case where
> cpuset.cpus can be set even when violating partition sibling CPU
> exclusivity rules. This differs from the general validation logic in
> validate_change. Add a wrapper function to properly handle this
> exceptional case.
>
> Since partition invalidation status can be determined by trialcs->prs_err,
> the local variable 'bool invalidate' can be removed.
>
> Signed-off-by: Chen Ridong <chenridong@huawei.com>
> ---
>   kernel/cgroup/cpuset.c | 83 +++++++++++++++++++++++-------------------
>   1 file changed, 45 insertions(+), 38 deletions(-)
>
> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index 71190f142700..75ad18ab40ae 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
> @@ -2410,43 +2410,11 @@ static bool invalidate_cs_partition(struct cpuset *cs)
>   	return false;
>   }
>   
> -/**
> - * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
> - * @cs: the cpuset to consider
> - * @trialcs: trial cpuset
> - * @buf: buffer of cpu numbers written to this cpuset
> - */
> -static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
> -			  const char *buf)
> +static int acpus_validate_change(struct cpuset *cs, struct cpuset *trialcs,
> +					struct tmpmasks *tmp)

What does "acpu" stand for? I suppose it means cpus_allowed. I will 
suggest to use a more descriptive name even if it is longer. I did use 
xcpus for exclusive_cpus, but 'x' is a seldomly used English alphabet 
that can associate with exclusive_cpus rather easily, but 'a' is not.

Cheers,
Longman
Re: [PATCH -next RFC 08/11] cpuset: refactor acpus_validate_change
Posted by Chen Ridong 1 month ago

On 2025/8/30 4:12, Waiman Long wrote:
> On 8/28/25 8:56 AM, Chen Ridong wrote:
>> From: Chen Ridong <chenridong@huawei.com>
>>
>> Refactor acpus_validate_change to handle the special case where
>> cpuset.cpus can be set even when violating partition sibling CPU
>> exclusivity rules. This differs from the general validation logic in
>> validate_change. Add a wrapper function to properly handle this
>> exceptional case.
>>
>> Since partition invalidation status can be determined by trialcs->prs_err,
>> the local variable 'bool invalidate' can be removed.
>>
>> Signed-off-by: Chen Ridong <chenridong@huawei.com>
>> ---
>>   kernel/cgroup/cpuset.c | 83 +++++++++++++++++++++++-------------------
>>   1 file changed, 45 insertions(+), 38 deletions(-)
>>
>> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
>> index 71190f142700..75ad18ab40ae 100644
>> --- a/kernel/cgroup/cpuset.c
>> +++ b/kernel/cgroup/cpuset.c
>> @@ -2410,43 +2410,11 @@ static bool invalidate_cs_partition(struct cpuset *cs)
>>       return false;
>>   }
>>   -/**
>> - * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
>> - * @cs: the cpuset to consider
>> - * @trialcs: trial cpuset
>> - * @buf: buffer of cpu numbers written to this cpuset
>> - */
>> -static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
>> -              const char *buf)
>> +static int acpus_validate_change(struct cpuset *cs, struct cpuset *trialcs,
>> +                    struct tmpmasks *tmp)
> 
> What does "acpu" stand for? I suppose it means cpus_allowed. I will suggest to use a more
> descriptive name even if it is longer. I did use xcpus for exclusive_cpus, but 'x' is a seldomly
> used English alphabet that can associate with exclusive_cpus rather easily, but 'a' is not.
> 
> Cheers,
> Longman
> 

Thanks Longman,

The term acpus refers to cpus_allowed.

My original naming convention was intended as follows:

acpus  --> cpus_allowed (allowed cpus)
ecpus  --> effective_cpus
xcpus  --> exclusive_cpus
excpus --> effective_xcpus

-- 
Best regards,
Ridong