Skip to contentSkip to conversation

nodemask: align nodes_and{,not} with underlying bitmap ops

PATCH In review Yury Norov 9 participants 15 messages Jan 14 – Jan 16

Subsystems (5)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

cover nodemask: align nodes_and{,not} with underlying bitmap ops

nodes_and{,not} are void despite that underlying bitmap_and(,not) return boolean, true if the result bitmap is non-empty. Align nodemask API, and simplify client code.

Yury Norov (3):
nodemask: propagate boolean for nodes_and{,not}
mm: use nodes_and() return value to simplify client code
cgroup: use nodes_and() output where appropriate

 include/linux/nodemask.h | 8 ++++----
 kernel/cgroup/cpuset.c   | 7 +++----
 mm/memory-tiers.c        | 3 +--
 mm/mempolicy.c           | 3 +--
 4 files changed, 9 insertions(+), 12 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/3 nodemask: propagate boolean for nodes_and{,not}

Bitmap functions bitmap_and{,not} return boolean depending on emptiness of the result bitmap. The corresponding nodemask helpers ignore the returned value.

Propagate the underlying bitmaps result to nodemasks users, as it simplifies user code.

---
 include/linux/nodemask.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/nodemask.h +4 -4
index bd38648c998d..204c92462f3c 100644
@@ -157,10 +157,10 @@ static __always_inline bool __node_test_and_set(int node, nodemask_t *addr)
 
 #define nodes_and(dst, src1, src2) \
 			__nodes_and(&(dst), &(src1), &(src2), MAX_NUMNODES)
-static __always_inline void __nodes_and(nodemask_t *dstp, const nodemask_t *src1p,
+static __always_inline bool __nodes_and(nodemask_t *dstp, const nodemask_t *src1p,
 					const nodemask_t *src2p, unsigned int nbits)
 {
-	bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
+	return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
 }
 
 #define nodes_or(dst, src1, src2) \
@@ -181,10 +181,10 @@ static __always_inline void __nodes_xor(nodemask_t *dstp, const nodemask_t *src1
 
 #define nodes_andnot(dst, src1, src2) \
 			__nodes_andnot(&(dst), &(src1), &(src2), MAX_NUMNODES)
-static __always_inline void __nodes_andnot(nodemask_t *dstp, const nodemask_t *src1p,
+static __always_inline bool __nodes_andnot(nodemask_t *dstp, const nodemask_t *src1p,
 					const nodemask_t *src2p, unsigned int nbits)
 {
-	bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
+	return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
 }
 
 #define nodes_copy(dst, src) __nodes_copy(&(dst), &(src), MAX_NUMNODES)
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] nodemask: propagate boolean for nodes_and{,not}

Quoted 8 lines · Yury Norov

Bitmap functions bitmap_and{,not} return boolean depending on emptiness of the result bitmap. The corresponding nodemask helpers ignore the returned value.

Propagate the underlying bitmaps result to nodemasks users, as it simplifies user code.

maintainer Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] nodemask: propagate boolean for nodes_and{,not}

In reply to Yury Norov
Quoted 9 lines · Yury Norov

Bitmap functions bitmap_and{,not} return boolean depending on emptiness of the result bitmap. The corresponding nodemask helpers ignore the returned value.

Propagate the underlying bitmaps result to nodemasks users, as it simplifies user code.

---

Signature · 3 lines
Cheers

David
author
#4 lore raw

PATCH 2/3 mm: use nodes_and() return value to simplify client code

In reply to Yury Norov

establish_demotion_targets() and kernel_migrate_pages() call node_empty() immediately after calling nodes_and(). Now that nodes_and() return false if nodemask is empty, drop the latter.

---
 mm/memory-tiers.c | 3 +--
 mm/mempolicy.c    | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)
mm/memory-tiers.c +1 -2
index 864811fff409..2cbef49a587d 100644
@@ -475,8 +475,7 @@ static void establish_demotion_targets(void)
 	 */
 	list_for_each_entry_reverse(memtier, &memory_tiers, list) {
 		tier_nodes = get_memtier_nodemask(memtier);
-		nodes_and(tier_nodes, node_states[N_CPU], tier_nodes);
-		if (!nodes_empty(tier_nodes)) {
+		if (nodes_and(tier_nodes, node_states[N_CPU], tier_nodes)) {
 			/*
 			 * abstract distance below the max value of this memtier
 			 * is considered toptier.
mm/mempolicy.c +1 -2
index 68a98ba57882..92a0bf7619a2 100644
@@ -1909,8 +1909,7 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode,
 	}
 
 	task_nodes = cpuset_mems_allowed(current);
-	nodes_and(*new, *new, task_nodes);
-	if (nodes_empty(*new))
+	if (!nodes_and(*new, *new, task_nodes))
 		goto out_put;
 
 	err = security_task_movememory(task);
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] mm: use nodes_and() return value to simplify client code

Quoted 5 lines · Yury Norov

establish_demotion_targets() and kernel_migrate_pages() call node_empty() immediately after calling nodes_and(). Now that nodes_and() return false if nodemask is empty, drop the latter.

Acked-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] mm: use nodes_and() return value to simplify client code

In reply to Yury Norov
Quoted 5 lines · Yury Norov

establish_demotion_targets() and kernel_migrate_pages() call node_empty() immediately after calling nodes_and(). Now that nodes_and() return false if nodemask is empty, drop the latter.

Quoted 33 lines
---
 mm/memory-tiers.c | 3 +--
 mm/mempolicy.c    | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)
mm/memory-tiers.c +1 -2
index 864811fff409..2cbef49a587d 100644
@@ -475,8 +475,7 @@ static void establish_demotion_targets(void)
 	 */
 	list_for_each_entry_reverse(memtier, &memory_tiers, list) {
 		tier_nodes = get_memtier_nodemask(memtier);
-		nodes_and(tier_nodes, node_states[N_CPU], tier_nodes);
-		if (!nodes_empty(tier_nodes)) {
+		if (nodes_and(tier_nodes, node_states[N_CPU], tier_nodes)) {
 			/*
 			 * abstract distance below the max value of this memtier
 			 * is considered toptier.
mm/mempolicy.c +1 -2
index 68a98ba57882..92a0bf7619a2 100644
@@ -1909,8 +1909,7 @@ static int kernel_migrate_pages(pid_t pid, unsigned long maxnode,
 	}
 
 	task_nodes = cpuset_mems_allowed(current);
-	nodes_and(*new, *new, task_nodes);
-	if (nodes_empty(*new))
+	if (!nodes_and(*new, *new, task_nodes))
 		goto out_put;
 
 	err = security_task_movememory(task);
maintainer Acked-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] mm: use nodes_and() return value to simplify client code

In reply to Yury Norov
Quoted 5 lines · Yury Norov

establish_demotion_targets() and kernel_migrate_pages() call node_empty() immediately after calling nodes_and(). Now that nodes_and() return false if nodemask is empty, drop the latter.

Signature · 3 lines
Cheers

David
author
#8 lore raw

PATCH 3/3 cgroup: use nodes_and() output where appropriate

In reply to Yury Norov

Now that nodes_and() returns true if the result nodemask is not empty, drop useless nodes_intersects() in guarantee_online_mems() and nodes_empty() in update_nodemasks_hier(), which both are O(N).

---
 kernel/cgroup/cpuset.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/cgroup/cpuset.c +3 -4
index 3e8cc34d8d50..e962efbb300d 100644
@@ -456,9 +456,8 @@ static void guarantee_active_cpus(struct task_struct *tsk,
  */
 static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask)
 {
-	while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY]))
+	while (!nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]))
 		cs = parent_cs(cs);
-	nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]);
 }
 
 /**
@@ -2862,13 +2861,13 @@ static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
 	cpuset_for_each_descendant_pre(cp, pos_css, cs) {
 		struct cpuset *parent = parent_cs(cp);
 
-		nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
+		bool has_mems = nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
 
 		/*
 		 * If it becomes empty, inherit the effective mask of the
 		 * parent, which is guaranteed to have some MEMs.
 		 */
-		if (is_in_v2_mode() && nodes_empty(*new_mems))
+		if (is_in_v2_mode() && !has_mems)
 			*new_mems = parent->effective_mems;
 
 		/* Skip the whole subtree if the nodemask remains the same. */
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH 3/3] cgroup: use nodes_and() output where appropriate

Quoted 5 lines · Yury Norov

Now that nodes_and() returns true if the result nodemask is not empty, drop useless nodes_intersects() in guarantee_online_mems() and nodes_empty() in update_nodemasks_hier(), which both are O(N).

This is a nice simplification, thank you!

maintainer Reviewed-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] cgroup: use nodes_and() output where appropriate

In reply to Yury Norov
Quoted 8 lines · Yury Norov

Now that nodes_and() returns true if the result nodemask is not empty, drop useless nodes_intersects() in guarantee_online_mems() and nodes_empty() in update_nodemasks_hier(), which both are O(N).

---
 kernel/cgroup/cpuset.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

Nice improvement.

Reviewed-by on v1 3/3
#11 lore raw

Re: [PATCH 3/3] cgroup: use nodes_and() output where appropriate

In reply to Yury Norov
Quoted 30 lines · Yury Norov

Now that nodes_and() returns true if the result nodemask is not empty, drop useless nodes_intersects() in guarantee_online_mems() and nodes_empty() in update_nodemasks_hier(), which both are O(N).

---
  kernel/cgroup/cpuset.c | 7 +++----
  1 file changed, 3 insertions(+), 4 deletions(-)
kernel/cgroup/cpuset.c +2 -3
index 3e8cc34d8d50..e962efbb300d 100644
@@ -456,9 +456,8 @@ static void guarantee_active_cpus(struct task_struct *tsk,
   */
  static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask)
  {
-	while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY]))
+	while (!nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]))
  		cs = parent_cs(cs);
-	nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]);
  }
 
  /**
@@ -2862,13 +2861,13 @@ static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
  	cpuset_for_each_descendant_pre(cp, pos_css, cs) {
  		struct cpuset *parent = parent_cs(cp);
 
-		nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
+		bool has_mems = nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);

kernel/cgroup/cpuset.c static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems) · line 2873

Quoted 10 lines
kernel/cgroup/cpuset.c +1 -1
 
  		/*
  		 * If it becomes empty, inherit the effective mask of the
  		 * parent, which is guaranteed to have some MEMs.
  		 */
-		if (is_in_v2_mode() && nodes_empty(*new_mems))
+		if (is_in_v2_mode() && !has_mems)
  			*new_mems = parent->effective_mems;
 
  		/* Skip the whole subtree if the nodemask remains the same. */
Reviewed-by
#12 lore raw

Re: [PATCH 3/3] cgroup: use nodes_and() output where appropriate

Quoted 30 lines · Waiman Long
Quoted 29 lines · Yury Norov

Now that nodes_and() returns true if the result nodemask is not empty, drop useless nodes_intersects() in guarantee_online_mems() and nodes_empty() in update_nodemasks_hier(), which both are O(N).

---
  kernel/cgroup/cpuset.c | 7 +++----

  1 file changed, 3 insertions(+), 4 deletions(-)
kernel/cgroup/cpuset.c +2 -2
index 3e8cc34d8d50..e962efbb300d 100644
@@ -456,9 +456,8 @@ static void guarantee_active_cpus(struct task_struct *tsk,
    */
   static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask)
   {
-    while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY]))
+    while (!nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]))
           cs = parent_cs(cs);
-    nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]);
   }
     /**
@@ -2862,13 +2861,13 @@ static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
       cpuset_for_each_descendant_pre(cp, pos_css, cs) {
           struct cpuset *parent = parent_cs(cp);
   -        nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
+        bool has_mems = nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
             /*
Quoted 9 lines

kernel/cgroup/cpuset.c static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems) · line 2873

Quoted 7 lines
kernel/cgroup/cpuset.c +1 -1
            * If it becomes empty, inherit the effective mask of the
            * parent, which is guaranteed to have some MEMs.
            */
-        if (is_in_v2_mode() && nodes_empty(*new_mems))
+        if (is_in_v2_mode() && !has_mems)
               *new_mems = parent->effective_mems;
             /* Skip the whole subtree if the nodemask remains the same. */
Signature · 2 lines
Best regards,
Ridong
maintainer Acked-by on v1 3/3
#13 lore raw

Re: [PATCH 3/3] cgroup: use nodes_and() output where appropriate

In reply to Yury Norov
Quoted 5 lines · Yury Norov

Now that nodes_and() returns true if the result nodemask is not empty, drop useless nodes_intersects() in guarantee_online_mems() and nodes_empty() in update_nodemasks_hier(), which both are O(N).

Please feel free to route together with the prerequisite patches.

Thanks.

Signature · 1 line
tejun
reviewer Reviewed-by on the v1 series
#14 lore raw
In reply to Yury Norov
Quoted 9 lines · On Wed, 14 Jan 2026 12:22:12 -0500 Yury Norov <[email protected]> wrote:

nodes_and{,not} are void despite that underlying bitmap_and(,not) return boolean, true if the result bitmap is non-empty. Align nodemask API, and simplify client code.

Yury Norov (3):
nodemask: propagate boolean for nodes_and{,not}
mm: use nodes_and() return value to simplify client code
cgroup: use nodes_and() output where appropriate

Quoted 8 lines
 include/linux/nodemask.h | 8 ++++----
 kernel/cgroup/cpuset.c   | 7 +++----
 mm/memory-tiers.c        | 3 +--
 mm/mempolicy.c           | 3 +--
 4 files changed, 9 insertions(+), 12 deletions(-)
Signature · 1 line
2.43.0

The entire set LGTM, thank you for this patchset! Feel free to add:

For the entire series. Have a great day!
Joshua