[PATCH -next RFC -v2 06/11] cpuset: introduce cpus_excl_conflict and mems_excl_conflict helpers

Chen Ridong posted 11 patches 3 weeks, 2 days ago
There is a newer version of this series
[PATCH -next RFC -v2 06/11] cpuset: introduce cpus_excl_conflict and mems_excl_conflict helpers
Posted by Chen Ridong 3 weeks, 2 days ago
From: Chen Ridong <chenridong@huawei.com>

This patch adds cpus_excl_conflict() and mems_excl_conflict() helper
functions to improve code readability and maintainability. The exclusive
conflict checking follows these rules:

1. If either cpuset has the 'exclusive' flag set, their user_xcpus must
   not have any overlap.
2. If both cpusets are non-exclusive, their 'cpuset.cpus.exclusive' values
   must not intersect.
3. The 'cpuset.cpus' of one cpuset must not form a subset of another
   cpuset's 'cpuset.cpus.exclusive'.

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

diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 55674a5ad2f9..389dfd5be6c8 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -582,6 +582,47 @@ static inline bool cpusets_are_exclusive(struct cpuset *cs1, struct cpuset *cs2)
 	return true;
 }
 
+/**
+ * cpus_excl_conflict - Check if two cpusets have exclusive CPU conflicts
+ * @cs1: first cpuset to check
+ * @cs2: second cpuset to check
+ *
+ * Returns: true if CPU exclusivity conflict exists, false otherwise
+ *
+ * Conflict detection rules:
+ * 1. If either cpuset is CPU exclusive, they must be mutually exclusive
+ * 2. exclusive_cpus masks cannot intersect between cpusets
+ * 3. The allowed CPUs of one cpuset cannot be a subset of another's exclusive CPUs
+ */
+static inline bool cpus_excl_conflict(struct cpuset *cs1, struct cpuset *cs2)
+{
+	/* If either cpuset is exclusive, check if they are mutually exclusive */
+	if (is_cpu_exclusive(cs1) || is_cpu_exclusive(cs2))
+		return !cpusets_are_exclusive(cs1, cs2);
+
+	/* Exclusive_cpus cannot intersect */
+	if (cpumask_intersects(cs1->exclusive_cpus, cs2->exclusive_cpus))
+		return true;
+
+	/* The cpus_allowed of one cpuset cannot be a subset of another cpuset's exclusive_cpus */
+	if (!cpumask_empty(cs1->cpus_allowed) &&
+	    cpumask_subset(cs1->cpus_allowed, cs2->exclusive_cpus))
+		return true;
+
+	if (!cpumask_empty(cs2->cpus_allowed) &&
+	    cpumask_subset(cs2->cpus_allowed, cs1->exclusive_cpus))
+		return true;
+
+	return false;
+}
+
+static inline bool mems_excl_conflict(struct cpuset *cs1, struct cpuset *cs2)
+{
+	if ((is_mem_exclusive(cs1) || is_mem_exclusive(cs2)))
+		return nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
+	return false;
+}
+
 /*
  * validate_change() - Used to validate that any proposed cpuset change
  *		       follows the structural rules for cpusets.
@@ -663,38 +704,11 @@ static int validate_change(struct cpuset *cur, struct cpuset *trial)
 	 */
 	ret = -EINVAL;
 	cpuset_for_each_child(c, css, par) {
-		bool txset, cxset;	/* Are exclusive_cpus set? */
-
 		if (c == cur)
 			continue;
-
-		txset = !cpumask_empty(trial->exclusive_cpus);
-		cxset = !cpumask_empty(c->exclusive_cpus);
-		if (is_cpu_exclusive(trial) || is_cpu_exclusive(c) ||
-		    (txset && cxset)) {
-			if (!cpusets_are_exclusive(trial, c))
-				goto out;
-		} else if (txset || cxset) {
-			struct cpumask *xcpus, *acpus;
-
-			/*
-			 * When just one of the exclusive_cpus's is set,
-			 * cpus_allowed of the other cpuset, if set, cannot be
-			 * a subset of it or none of those CPUs will be
-			 * available if these exclusive CPUs are activated.
-			 */
-			if (txset) {
-				xcpus = trial->exclusive_cpus;
-				acpus = c->cpus_allowed;
-			} else {
-				xcpus = c->exclusive_cpus;
-				acpus = trial->cpus_allowed;
-			}
-			if (!cpumask_empty(acpus) && cpumask_subset(acpus, xcpus))
-				goto out;
-		}
-		if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
-		    nodes_intersects(trial->mems_allowed, c->mems_allowed))
+		if (cpus_excl_conflict(trial, c))
+			goto out;
+		if (mems_excl_conflict(trial, c))
 			goto out;
 	}
 
-- 
2.34.1
Re: [PATCH -next RFC -v2 06/11] cpuset: introduce cpus_excl_conflict and mems_excl_conflict helpers
Posted by Waiman Long 2 weeks, 3 days ago
On 9/8/25 11:32 PM, Chen Ridong wrote:
> From: Chen Ridong <chenridong@huawei.com>
>
> This patch adds cpus_excl_conflict() and mems_excl_conflict() helper
> functions to improve code readability and maintainability. The exclusive
> conflict checking follows these rules:
>
> 1. If either cpuset has the 'exclusive' flag set, their user_xcpus must
>     not have any overlap.
> 2. If both cpusets are non-exclusive, their 'cpuset.cpus.exclusive' values
>     must not intersect.

The term "non-exclusive" is somewhat confusing. I suppose you mean that 
the exclusive flag isn't set. However, exclusive flag is a cpuset v1 
only feature and cpus.exclusive is a v2 only feature. They will not 
coexist. You may need to update the wording.

After you fix that, you can add

Reveiwed-by: Waiman Long <longman@redhat.com>
Re: [PATCH -next RFC -v2 06/11] cpuset: introduce cpus_excl_conflict and mems_excl_conflict helpers
Posted by Chen Ridong 2 weeks, 2 days ago

On 2025/9/16 2:42, Waiman Long wrote:
> 
> On 9/8/25 11:32 PM, Chen Ridong wrote:
>> From: Chen Ridong <chenridong@huawei.com>
>>
>> This patch adds cpus_excl_conflict() and mems_excl_conflict() helper
>> functions to improve code readability and maintainability. The exclusive
>> conflict checking follows these rules:
>>
>> 1. If either cpuset has the 'exclusive' flag set, their user_xcpus must
>>     not have any overlap.
>> 2. If both cpusets are non-exclusive, their 'cpuset.cpus.exclusive' values
>>     must not intersect.
> 
> The term "non-exclusive" is somewhat confusing. I suppose you mean that the exclusive flag isn't
> set. However, exclusive flag is a cpuset v1 only feature and cpus.exclusive is a v2 only feature.
> They will not coexist. You may need to update the wording.
> 
> After you fix that, you can add
> 
> Reveiwed-by: Waiman Long <longman@redhat.com>

Thank you, Longman, I will update.

-- 
Best regards,
Ridong