cpumask: Use cpumask_intersects* APIs where applicable
Subsystems (6)
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
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
@@ -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
[email protected]
AI reviewRe: [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_andreturns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it againstnr_cpu_ids, that implies whether the two cpumask have a bit in common. I.ecpumask_intersects.
cpumask_any_anduses ffs on (addr1[idx] & addr2[idx]) whereascpumask_intersectschecks only (addr1[idx] & addr2[idx]).So replace such occurrences.
Signed-off-by Shrikanth Hegde <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[tip: sched/core] sched/rt: cpupri_find: Use cpumask_intersects where applicable
In reply to Shrikanth HegdeThe 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
@@ -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) {
PATCH 2/5 cpu/hotplug: Use cpumask_intersects where applicable
In reply to Shrikanth Hegdecpumask_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
@@ -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
[email protected]
AI reviewRe: [PATCH 2/5] cpu/hotplug: Use cpumask_intersects where applicable
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines
cpumask_any_andreturns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it againstnr_cpu_ids, that implies whether the two cpumask have a bit in common. i.ecpumask_intersects.
cpumask_any_anduses ffs on (addr1[idx] & addr2[idx]) whereascpumask_intersectschecks only (addr1[idx] & addr2[idx]).So replace such occurrences.
Signed-off-by Shrikanth Hegde <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[tip: sched/core] cpu/hotplug: Use cpumask_intersects where applicable
In reply to Shrikanth HegdeThe 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
@@ -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; }
PATCH 3/5 perf: Use cpumask_intersects where applicable
In reply to Shrikanth Hegdecpumask_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
@@ -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
[email protected]
AI reviewRe: [PATCH 3/5] perf: Use cpumask_intersects where applicable
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines
cpumask_any_andreturns, usually the first cpu in the intersection of the two cpumasks. However, if the returned value is not used or only to check it againstnr_cpu_ids, that implies whether the two cpumask have a bit in common. i.ecpumask_intersects.
cpumask_any_anduses ffs on (addr1[idx] & addr2[idx]) whereascpumask_intersectschecks only (addr1[idx] & addr2[idx]).So replace such occurrences.
Signed-off-by Shrikanth Hegde <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[tip: perf/core] perf: Use cpumask_intersects where applicable
In reply to Shrikanth HegdeThe 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
@@ -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:
PATCH 4/5 cpuidle: Use cpumask_intersects_and where applicable
In reply to Shrikanth Hegdecpumask_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
@@ -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
[email protected]
AI reviewRe: [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_andreturns, usually the first CPU in the intersection of the three cpumasks. However, if the returned CPU is not used or only to check it againstnr_cpu_ids, that implies to check whether the three cpumasks have a bit in common. I.e usecpumask_intersects_and.
Quoted 7 lines
cpumask_first_and_anduses ffs on (addr1[idx] & addr2[idx] & addr3[idx]) whereascpumask_intersects_andchecks only
(addr1[idx] & addr2[idx] & addr3[idx]).So replace such an occurrence.
Signed-off-by Shrikanth Hegde <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH 5/5 arch_topology: Use cpumask_intersects where applicable
In reply to Shrikanth HegdeLooping 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
@@ -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
[email protected]
AI reviewRe: [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_intersectsinstead. It is probably faster too.Signed-off-by Shrikanth Hegde <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH 5/5] arch_topology: Use cpumask_intersects where applicable
In reply to Shrikanth HegdeQuoted 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_intersectsinstead. It is probably faster too.
Signature · 2 lines
Regards, Sudeep
Peter Zijlstra
maintainerQuoted 7 lines · Shrikanth Hegde
Shrikanth Hegde (5):
cpuidle: Use
cpumask_intersects_andwhere applicable
arch_topology: Usecpumask_intersectswhere applicabledrivers/base/arch_topology.c | 9 +-------- drivers/cpuidle/coupled.c | 4 ++--
That's not actually code I maintain. That's rjw stuff.
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Shrikanth Hegde
Shrikanth Hegde (5):
cpuidle: Use
cpumask_intersects_andwhere applicable
arch_topology: Usecpumask_intersectswhere applicabledrivers/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?
Peter Zijlstra
maintainerQuoted 12 lines · Shrikanth Hegde
Quoted 9 lines · Peter Zijlstra
Quoted 6 lines · Shrikanth Hegde
Shrikanth Hegde (5):
cpuidle: Use
cpumask_intersects_andwhere applicable
arch_topology: Usecpumask_intersectswhere applicabledrivers/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.
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_andwhere applicable
arch_topology: Usecpumask_intersectswhere applicabledrivers/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.
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_andand explicit loops where cpu found by these APIs are not being used. All those callsites instead can simply usecpumask_intersects.Note that few more places can use similar change. But, I have left them as is.
drivers/net/ethernet/sfc/efx_channels.c+370drivers/net/ethernet/sfc/siena/efx_channels.c+371drivers/platform/x86/intel/uncore-frequency/uncore-frequency.c+153Also, 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: Usecpumask_intersectswhere applicable cpu/hotplug: Usecpumask_intersectswhere applicable
perf: Usecpumask_intersectswhere applicable
cpuidle: Usecpumask_intersects_andwhere applicable
arch_topology: Usecpumask_intersectswhere 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