Skip to contentSkip to conversation

cpumask: Use cpumask_intersects* APIs where applicable

PATCH Applied Shrikanth Hegde 6 participants 20 messages Nov 11 – Nov 18

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

cover cpumask: Use cpumask_intersects* APIs where applicable

It was observed while working on cpumask_intersects_and, check like below could use it instead.

if (cpumask_first_and_and() < nr_cpu_ids)

While there, did check the code for any such similar patterns of cpumask_any_and, cpumask_first_and and explicit loops where cpu found by these APIs are not being used. All those callsites instead can simply use cpumask_intersects.

Note that few more places can use similar change. But, I have left them as is.
drivers/net/ethernet/sfc/efx_channels.c+370 drivers/net/ethernet/sfc/siena/efx_channels.c+371 drivers/platform/x86/intel/uncore-frequency/uncore-frequency.c+153

Also, these patches can combined into three patches if that is easier for review/merge. I have kept it separate since they belong to different subsystems.

Shrikanth Hegde (5):
sched/rt: cpupri_find: Use cpumask_intersects where applicable cpu/hotplug: Use cpumask_intersects where applicable
perf: Use cpumask_intersects where applicable
cpuidle: Use cpumask_intersects_and where applicable
arch_topology: Use cpumask_intersects where applicable

 drivers/base/arch_topology.c | 9 +--------
 drivers/cpuidle/coupled.c    | 4 ++--
 kernel/cpu.c                 | 3 +--
 kernel/events/core.c         | 7 ++-----
 kernel/sched/cpupri.c        | 2 +-
 5 files changed, 7 insertions(+), 18 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/5 sched/rt: cpupri_find: Use cpumask_intersects where applicable

cpumask_any_and returns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it against nr_cpu_ids, that implies whether the two cpumask have a bit in common. I.e cpumask_intersects.

cpumask_any_and uses ffs on (addr1[idx] & addr2[idx]) whereas cpumask_intersects checks only (addr1[idx] & addr2[idx]).

So replace such occurrences.

---
 kernel/sched/cpupri.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/cpupri.c +1 -1
index 8f2237e8b484..44d67fbc4ab2 100644
@@ -97,7 +97,7 @@ static inline int __cpupri_find(struct cpupri *cp, struct task_struct *p,
 	if (skip)
 		return 0;
 
-	if (cpumask_any_and(&p->cpus_mask, vec->mask) >= nr_cpu_ids)
+	if (!cpumask_intersects(&p->cpus_mask, vec->mask))
 		return 0;
 
 	if (lowest_mask) {
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH 1/5] sched/rt: cpupri_find: Use cpumask_intersects where applicable

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

cpumask_any_and returns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it against nr_cpu_ids, that implies whether the two cpumask have a bit in common. I.e cpumask_intersects.

cpumask_any_and uses ffs on (addr1[idx] & addr2[idx]) whereas cpumask_intersects checks only (addr1[idx] & addr2[idx]).

So replace such occurrences.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

[tip: sched/core] sched/rt: cpupri_find: Use cpumask_intersects where applicable

In reply to Shrikanth Hegde
The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     fda7fb5adda6c71be96f5836498d4b207c0811fc
Gitweb:        https://git.kernel.org/tip/fda7fb5adda6c71be96f5836498d4b207c0811fc
Author:        Shrikanth Hegde <[email protected]>
AuthorDate:    Wed, 11 Nov 2026 13:29:41 +05:30
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 18 Nov 2026 14:32:20 +01:00

sched/rt: cpupri_find: Use cpumask_intersects where applicable

cpumask_any_and returns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it against nr_cpu_ids, that implies whether the two cpumask have a bit in common. I.e cpumask_intersects.

cpumask_any_and uses ffs on (addr1[idx] & addr2[idx]) whereas cpumask_intersects checks only (addr1[idx] & addr2[idx]).

So replace such occurrences.

---
 kernel/sched/cpupri.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/cpupri.c +1 -1
index 8f2237e..44d67fb 100644
@@ -97,7 +97,7 @@ static inline int __cpupri_find(struct cpupri *cp, struct task_struct *p,
 	if (skip)
 		return 0;
 
-	if (cpumask_any_and(&p->cpus_mask, vec->mask) >= nr_cpu_ids)
+	if (!cpumask_intersects(&p->cpus_mask, vec->mask))
 		return 0;
 
 	if (lowest_mask) {
#4 lore raw

PATCH 2/5 cpu/hotplug: Use cpumask_intersects where applicable

In reply to Shrikanth Hegde

cpumask_any_and returns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it against nr_cpu_ids, that implies whether the two cpumask have a bit in common. i.e cpumask_intersects.

cpumask_any_and uses ffs on (addr1[idx] & addr2[idx]) whereas cpumask_intersects checks only (addr1[idx] & addr2[idx]).

So replace such occurrences.

---
 kernel/cpu.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/cpu.c +1 -2
index 376d297a6292..e39245d9bffe 100644
@@ -1418,8 +1418,7 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
 	 * Keep at least one housekeeping cpu onlined to avoid generating
 	 * an empty sched_domain span.
 	 */
-	if (cpumask_any_and(cpu_online_mask,
-			    housekeeping_cpumask(HK_TYPE_DOMAIN)) >= nr_cpu_ids) {
+	if (!cpumask_intersects(cpu_online_mask, housekeeping_cpumask(HK_TYPE_DOMAIN))) {
 		ret = -EBUSY;
 		goto out;
 	}
Signature · 1 line
2.52.0
#5 lore raw

Re: [PATCH 2/5] cpu/hotplug: Use cpumask_intersects where applicable

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

cpumask_any_and returns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it against nr_cpu_ids, that implies whether the two cpumask have a bit in common. i.e cpumask_intersects.

cpumask_any_and uses ffs on (addr1[idx] & addr2[idx]) whereas cpumask_intersects checks only (addr1[idx] & addr2[idx]).

So replace such occurrences.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

[tip: sched/core] cpu/hotplug: Use cpumask_intersects where applicable

In reply to Shrikanth Hegde
The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     69465ca8f6b5d332e7996643664442a93be254b6
Gitweb:        https://git.kernel.org/tip/69465ca8f6b5d332e7996643664442a93be254b6
Author:        Shrikanth Hegde <[email protected]>
AuthorDate:    Wed, 11 Nov 2026 13:29:42 +05:30
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 18 Nov 2026 14:32:20 +01:00

cpu/hotplug: Use cpumask_intersects where applicable

cpumask_any_and returns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it against nr_cpu_ids, that implies whether the two cpumask have a bit in common. i.e cpumask_intersects.

cpumask_any_and uses ffs on (addr1[idx] & addr2[idx]) whereas cpumask_intersects checks only (addr1[idx] & addr2[idx]).

So replace such occurrences.

---
 kernel/cpu.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/cpu.c +1 -2
index 376d297..e39245d 100644
@@ -1418,8 +1418,7 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
 	 * Keep at least one housekeeping cpu onlined to avoid generating
 	 * an empty sched_domain span.
 	 */
-	if (cpumask_any_and(cpu_online_mask,
-			    housekeeping_cpumask(HK_TYPE_DOMAIN)) >= nr_cpu_ids) {
+	if (!cpumask_intersects(cpu_online_mask, housekeeping_cpumask(HK_TYPE_DOMAIN))) {
 		ret = -EBUSY;
 		goto out;
 	}
#7 lore raw

PATCH 3/5 perf: Use cpumask_intersects where applicable

In reply to Shrikanth Hegde

cpumask_any_and returns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it against nr_cpu_ids, that implies whether the two cpumask have a bit in common. i.e cpumask_intersects.

cpumask_any_and uses ffs on (addr1[idx] & addr2[idx]) whereas cpumask_intersects checks only (addr1[idx] & addr2[idx]).

So replace such occurrences.

---
 kernel/events/core.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/events/core.c +2 -5
index a6c8e38a3110..f9fa1b47ddf4 100644
@@ -13159,7 +13159,6 @@ static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
 	if (pmu->scope != PERF_PMU_SCOPE_NONE && event->cpu >= 0) {
 		const struct cpumask *cpumask;
 		struct cpumask *pmu_cpumask;
-		int cpu;
 
 		cpumask = perf_scope_cpu_topology_cpumask(pmu->scope, event->cpu);
 		pmu_cpumask = perf_scope_cpumask(pmu->scope);
@@ -13168,8 +13167,7 @@ static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
 		if (!pmu_cpumask || !cpumask)
 			goto err_destroy;
 
-		cpu = cpumask_any_and(pmu_cpumask, cpumask);
-		if (cpu >= nr_cpu_ids)
+		if (!cpumask_intersects(pmu_cpumask, cpumask))
 			goto err_destroy;
 
 		event->event_caps |= PERF_EV_CAP_READ_SCOPE;
@@ -15312,8 +15310,7 @@ static void perf_event_setup_cpumask(unsigned int cpu)
 		if (WARN_ON_ONCE(!pmu_cpumask || !cpumask))
 			continue;
 
-		if (!cpumask_empty(cpumask) &&
-		    cpumask_any_and(pmu_cpumask, cpumask) >= nr_cpu_ids)
+		if (!cpumask_empty(cpumask) && !cpumask_intersects(pmu_cpumask, cpumask))
 			cpumask_set_cpu(cpu, pmu_cpumask);
 	}
 end:
Signature · 1 line
2.52.0
#8 lore raw

Re: [PATCH 3/5] perf: Use cpumask_intersects where applicable

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

cpumask_any_and returns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it against nr_cpu_ids, that implies whether the two cpumask have a bit in common. i.e cpumask_intersects.

cpumask_any_and uses ffs on (addr1[idx] & addr2[idx]) whereas cpumask_intersects checks only (addr1[idx] & addr2[idx]).

So replace such occurrences.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#9 lore raw

[tip: perf/core] perf: Use cpumask_intersects where applicable

In reply to Shrikanth Hegde
The following commit has been merged into the perf/core branch of tip:

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     16bc76f9da9673bde4f8c84087e13573d2baa468
Gitweb:        https://git.kernel.org/tip/16bc76f9da9673bde4f8c84087e13573d2baa468
Author:        Shrikanth Hegde <[email protected]>
AuthorDate:    Wed, 11 Nov 2026 13:29:43 +05:30
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 18 Nov 2026 14:32:21 +01:00

perf: Use cpumask_intersects where applicable

cpumask_any_and returns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it against nr_cpu_ids, that implies whether the two cpumask have a bit in common. i.e cpumask_intersects.

cpumask_any_and uses ffs on (addr1[idx] & addr2[idx]) whereas cpumask_intersects checks only (addr1[idx] & addr2[idx]).

So replace such occurrences.

---
 kernel/events/core.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/events/core.c +2 -5
index e870f12..687bd39 100644
@@ -13274,7 +13274,6 @@ static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
 	if (pmu->scope != PERF_PMU_SCOPE_NONE && event->cpu >= 0) {
 		const struct cpumask *cpumask;
 		struct cpumask *pmu_cpumask;
-		int cpu;
 
 		cpumask = perf_scope_cpu_topology_cpumask(pmu->scope, event->cpu);
 		pmu_cpumask = perf_scope_cpumask(pmu->scope);
@@ -13283,8 +13282,7 @@ static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
 		if (!pmu_cpumask || !cpumask)
 			goto err_destroy;
 
-		cpu = cpumask_any_and(pmu_cpumask, cpumask);
-		if (cpu >= nr_cpu_ids)
+		if (!cpumask_intersects(pmu_cpumask, cpumask))
 			goto err_destroy;
 
 		event->event_caps |= PERF_EV_CAP_READ_SCOPE;
@@ -15442,8 +15440,7 @@ static void perf_event_setup_cpumask(unsigned int cpu)
 		if (WARN_ON_ONCE(!pmu_cpumask || !cpumask))
 			continue;
 
-		if (!cpumask_empty(cpumask) &&
-		    cpumask_any_and(pmu_cpumask, cpumask) >= nr_cpu_ids)
+		if (!cpumask_empty(cpumask) && !cpumask_intersects(pmu_cpumask, cpumask))
 			cpumask_set_cpu(cpu, pmu_cpumask);
 	}
 end:
#10 lore raw

PATCH 4/5 cpuidle: Use cpumask_intersects_and where applicable

In reply to Shrikanth Hegde

cpumask_first_and_and returns, usually the first CPU in the intersection of the three cpumasks. However, if the returned CPU is not used or only to check it against nr_cpu_ids, that implies to check whether the three cpumasks have a bit in common. I.e use cpumask_intersects_and.

cpumask_first_and_and uses ffs on (addr1[idx] & addr2[idx] & addr3[idx]) whereas cpumask_intersects_and checks only
(addr1[idx] & addr2[idx] & addr3[idx]).

So replace such an occurrence.

---
 drivers/cpuidle/coupled.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cpuidle/coupled.c +2 -2
index fb91a9e0e3c2..40db5c86a466 100644
@@ -439,8 +439,8 @@ static int cpuidle_coupled_clear_pokes(int cpu)
 
 static bool cpuidle_coupled_any_pokes_pending(struct cpuidle_coupled *coupled)
 {
-	return cpumask_first_and_and(cpu_online_mask, &coupled->coupled_cpus,
-				     &cpuidle_coupled_poke_pending) < nr_cpu_ids;
+	return cpumask_intersects_and(cpu_online_mask, &coupled->coupled_cpus,
+				      &cpuidle_coupled_poke_pending);
 }
 
 /**
Signature · 1 line
2.52.0
#11 lore raw

Re: [PATCH 4/5] cpuidle: Use cpumask_intersects_and where applicable

Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

cpumask_first_and_and returns, usually the first CPU in the intersection of the three cpumasks. However, if the returned CPU is not used or only to check it against nr_cpu_ids, that implies to check whether the three cpumasks have a bit in common. I.e use cpumask_intersects_and.

Quoted 7 lines

cpumask_first_and_and uses ffs on (addr1[idx] & addr2[idx] & addr3[idx]) whereas cpumask_intersects_and checks only
(addr1[idx] & addr2[idx] & addr3[idx]).

So replace such an occurrence.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#12 lore raw

PATCH 5/5 arch_topology: Use cpumask_intersects where applicable

In reply to Shrikanth Hegde

Looping over one cpumask and checking if any bit is set in another cpumask is same as evaluating if two cpumasks intersects or not.

So use cpumask_intersects instead. It is probably faster too.

---
 drivers/base/arch_topology.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/base/arch_topology.c +1 -8
index 8c5e47c28d9a..aba86e9c2516 100644
@@ -34,14 +34,7 @@ EXPORT_PER_CPU_SYMBOL_GPL(capacity_freq_ref);
 
 static bool supports_scale_freq_counters(const struct cpumask *cpus)
 {
-	int i;
-
-	for_each_cpu(i, cpus) {
-		if (cpumask_test_cpu(i, &scale_freq_counters_mask))
-			return true;
-	}
-
-	return false;
+	return cpumask_intersects(cpus, &scale_freq_counters_mask);
 }
 
 bool topology_scale_freq_invariant(void)
Signature · 1 line
2.52.0
#13 lore raw

Re: [PATCH 5/5] arch_topology: Use cpumask_intersects where applicable

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

Looping over one cpumask and checking if any bit is set in another cpumask is same as evaluating if two cpumasks intersects or not.

So use cpumask_intersects instead. It is probably faster too.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
maintainer Reviewed-by on v1 5/5
#14 lore raw

Re: [PATCH 5/5] arch_topology: Use cpumask_intersects where applicable

In reply to Shrikanth Hegde
Quoted 5 lines · Shrikanth Hegde

Looping over one cpumask and checking if any bit is set in another cpumask is same as evaluating if two cpumasks intersects or not.

So use cpumask_intersects instead. It is probably faster too.

Signature · 2 lines
Regards,
Sudeep
maintainer
#15 lore raw
In reply to Shrikanth Hegde
Quoted 7 lines · Shrikanth Hegde

Shrikanth Hegde (5):

cpuidle: Use cpumask_intersects_and where applicable
arch_topology: Use cpumask_intersects where applicable

 drivers/base/arch_topology.c | 9 +--------
 drivers/cpuidle/coupled.c    | 4 ++--

That's not actually code I maintain. That's rjw stuff.

#16 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Shrikanth Hegde

Shrikanth Hegde (5):

cpuidle: Use cpumask_intersects_and where applicable
arch_topology: Use cpumask_intersects where applicable

  drivers/base/arch_topology.c | 9 +--------
  drivers/cpuidle/coupled.c    | 4 ++--

That's not actually code I maintain. That's rjw stuff.

Yes. Sorry, I should have cc'ed him.

Should I split this series and send it to each subsystem instead?

maintainer
#17 lore raw
Quoted 12 lines · Shrikanth Hegde
Quoted 9 lines · Peter Zijlstra
Quoted 6 lines · Shrikanth Hegde

Shrikanth Hegde (5):

cpuidle: Use cpumask_intersects_and where applicable
arch_topology: Use cpumask_intersects where applicable

  drivers/base/arch_topology.c | 9 +--------
  drivers/cpuidle/coupled.c    | 4 ++--
Quoted 5 lines
Quoted 1 line

That's not actually code I maintain. That's rjw stuff.

Yes. Sorry, I should have cc'ed him.

Should I split this series and send it to each subsystem instead?

I've taken the first 3 patches since that's all tip stuff. Feel free to resend the last two with appropriate Ccs. Its all independent patches anyway.

#18 lore raw
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Shrikanth Hegde
Quoted 9 lines · Peter Zijlstra
Quoted 6 lines · Shrikanth Hegde

Shrikanth Hegde (5):

cpuidle: Use cpumask_intersects_and where applicable
arch_topology: Use cpumask_intersects where applicable

   drivers/base/arch_topology.c | 9 +--------
   drivers/cpuidle/coupled.c    | 4 ++--
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

That's not actually code I maintain. That's rjw stuff.

Yes. Sorry, I should have cc'ed him.

Should I split this series and send it to each subsystem instead?

I've taken the first 3 patches since that's all tip stuff.

Thanks Peter.

Quoted 3 lines

Feel free to
resend the last two with appropriate Ccs. Its all independent patches anyway.

Rafael or greg may not have got a chance to see it.

I will wait for few days if the last two can be picked up from this series. If not, Then I will resend last two as independent patches.

Reviewed-by on the v1 series
#19 lore raw
In reply to Shrikanth Hegde
Quoted 20 lines · Shrikanth Hegde

It was observed while working on cpumask_intersects_and, check like below could use it instead.

if (cpumask_first_and_and() < nr_cpu_ids)

While there, did check the code for any such similar patterns of cpumask_any_and, cpumask_first_and and explicit loops where cpu found by these APIs are not being used. All those callsites instead can simply use cpumask_intersects.

Note that few more places can use similar change. But, I have left them as is.
drivers/net/ethernet/sfc/efx_channels.c+370 drivers/net/ethernet/sfc/siena/efx_channels.c+371 drivers/platform/x86/intel/uncore-frequency/uncore-frequency.c+153

Also, these patches can combined into three patches if that is easier for review/merge. I have kept it separate since they belong to different subsystems.

Quoted 6 lines

Shrikanth Hegde (5):
sched/rt: cpupri_find: Use cpumask_intersects where applicable cpu/hotplug: Use cpumask_intersects where applicable
perf: Use cpumask_intersects where applicable
cpuidle: Use cpumask_intersects_and where applicable
arch_topology: Use cpumask_intersects where applicable

Quoted 10 lines
 drivers/base/arch_topology.c | 9 +--------
 drivers/cpuidle/coupled.c    | 4 ++--
 kernel/cpu.c                 | 3 +--
 kernel/events/core.c         | 7 ++-----
 kernel/sched/cpupri.c        | 2 +-
 5 files changed, 7 insertions(+), 18 deletions(-)
Signature · 1 line
2.52.0