Skip to contentSkip to conversation

cgroup/cpuset: use mems_allowed_seq when checking a policy nodemask

PATCH Applied Gregory Price (Meta) 3 participants 4 messages Oct 19

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cgroup/cpuset: use mems_allowed_seq when checking a policy nodemask

A cpuset update changes the task's mems_allowed and rebinds its mempolicy in separate steps under mems_allowed_seq, but the check reads both without it. This can cause a spurious numa placement miss under some cpuset/mempolicy combinations.

A reader that loads one mask before the update and the other after it can see a spurious empty intersection and drop the nodemask.

For example, a task with cpuset.mems={0} and MPOL_BIND={0}. If the cpuset moves to mems={1,2}, the policy is rebound to {1}.

The allocator can race and compare the old mems_allowed {0} against the new policy {1} - which will result in a dropped nodemask and a misplaced allocation on node 2.

Simply retry the intersection until mems_allowed_seq is stable.

This covers the task policy, which is rebound under mems_allowed_seq. VMA policies are rebound afterwards by mpol_rebind_mm(), outside mems_allowed_seq, and are not covered by this change.

Fixes cc9a6c877661 ("cpuset: mm: reduce large amounts of memory barrier related damage v3")

---
v2: there is no real update here, i'm breaking it off of the page_alloc
series so the page_alloc fixes backport collectively cleaner.

 kernel/cgroup/cpuset.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/cgroup/cpuset.c +12 -1
index 3f52717c19654..8ee239d5c6b48 100644
@@ -4354,10 +4354,21 @@ nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  * @nodemask: the nodemask to be checked
  *
  * Are any of the nodes in the nodemask allowed in current->mems_allowed?
+ *
+ * A cpuset update rebinds the task's mempolicy and changes mems_allowed in
+ * separate steps, so retry if an update raced with the check.
  */
 int cpuset_nodemask_valid_mems_allowed(const nodemask_t *nodemask)
 {
-	return nodes_intersects(*nodemask, current->mems_allowed);
+	unsigned int seq;
+	int ret;
+
+	do {
+		seq = read_mems_allowed_begin();
+		ret = nodes_intersects(*nodemask, current->mems_allowed);
+	} while (read_mems_allowed_retry(seq));
+
+	return ret;
 }
 
 /*
Signature · 1 line
2.53.0-Meta
#1 lore raw
Quoted 8 lines · Gregory Price (Meta)

A cpuset update changes the task's mems_allowed and rebinds its mempolicy in separate steps under mems_allowed_seq, but the check reads both without it. This can cause a spurious numa placement miss under some cpuset/mempolicy combinations.

A reader that loads one mask before the update and the other after it can see a spurious empty intersection and drop the nodemask.

Quoted 6 lines

For example, a task with cpuset.mems={0} and MPOL_BIND={0}. If the cpuset moves to mems={1,2}, the policy is rebound to {1}.

The allocator can race and compare the old mems_allowed {0} against the new policy {1} - which will result in a dropped nodemask and a misplaced allocation on node 2.

And since this can have only performance implications and not cause a functional issue, we don't need Cc: stable.

(unlike violating a __GFP_THISNODE allocation which could be quite bad and has happened in the past)

Quoted 44 lines

Simply retry the intersection until mems_allowed_seq is stable.

This covers the task policy, which is rebound under mems_allowed_seq. VMA policies are rebound afterwards by mpol_rebind_mm(), outside mems_allowed_seq, and are not covered by this change.

Fixes cc9a6c877661 ("cpuset: mm: reduce large amounts of memory barrier related damage v3")

---
v2: there is no real update here, i'm breaking it off of the page_alloc
series so the page_alloc fixes backport collectively cleaner.

 kernel/cgroup/cpuset.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/cgroup/cpuset.c +12 -1
index 3f52717c19654..8ee239d5c6b48 100644
@@ -4354,10 +4354,21 @@ nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  * @nodemask: the nodemask to be checked
  *
  * Are any of the nodes in the nodemask allowed in current->mems_allowed?
+ *
+ * A cpuset update rebinds the task's mempolicy and changes mems_allowed in
+ * separate steps, so retry if an update raced with the check.
  */
 int cpuset_nodemask_valid_mems_allowed(const nodemask_t *nodemask)
 {
-	return nodes_intersects(*nodemask, current->mems_allowed);
+	unsigned int seq;
+	int ret;
+
+	do {
+		seq = read_mems_allowed_begin();
+		ret = nodes_intersects(*nodemask, current->mems_allowed);
+	} while (read_mems_allowed_retry(seq));
+
+	return ret;
 }
 
 /*
#2 lore raw
Quoted 16 lines · Vlastimil Babka (SUSE)
Quoted 8 lines · Gregory Price (Meta)

A cpuset update changes the task's mems_allowed and rebinds its mempolicy in separate steps under mems_allowed_seq, but the check reads both without it. This can cause a spurious numa placement miss under some cpuset/mempolicy combinations.

A reader that loads one mask before the update and the other after it can see a spurious empty intersection and drop the nodemask.

Quoted 6 lines

For example, a task with cpuset.mems={0} and MPOL_BIND={0}. If the cpuset moves to mems={1,2}, the policy is rebound to {1}.

The allocator can race and compare the old mems_allowed {0} against the new policy {1} - which will result in a dropped nodemask and a misplaced allocation on node 2.

Quoted 6 lines

And since this can have only performance implications and not cause a functional issue, we don't need Cc: stable.

(unlike violating a __GFP_THISNODE allocation which could be quite bad and has happened in the past)

Agreed.

~Gregory