Skip to contentSkip to conversation

sched: Optimize core cookie matching check

PATCH Applied Fernand Sieber 5 participants 5 messages Nov 5, 2025 – Nov 11, 2025 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched: Optimize core cookie matching check

Early return true if the core cookie matches. This avoids the SMT mask loop to check for an idle core, which might be more expensive on wide platforms.

---
 kernel/sched/sched.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/sched.h +4 -1
index adfb6e3409d7..381cd561e99b 100644
@@ -1432,6 +1432,9 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
 	if (!sched_core_enabled(rq))
 		return true;
 
+	if (rq->core->core_cookie == p->core_cookie)
+		return true;
+
 	for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
 		if (!available_idle_cpu(cpu)) {
 			idle_core = false;
@@ -1443,7 +1446,7 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
 	 * A CPU in an idle core is always the best choice for tasks with
 	 * cookies.
 	 */
-	return idle_core || rq->core->core_cookie == p->core_cookie;
+	return idle_core;
 }
 
 static inline bool sched_group_cookie_match(struct rq *rq,
Signature · 8 lines
2.43.0




Amazon Development Centre (South Africa) (Proprietary) Limited
29 Gogosoa Street, Observatory, Cape Town, Western Cape, 7925, South Africa
Registration Number: 2004 / 034463 / 07
reviewer Reviewed-by on v1 1/1
#1 lore raw

Hello Fernand,

Quoted 14 lines · Fernand Sieber

Early return true if the core cookie matches. This avoids the SMT mask loop to check for an idle core, which might be more expensive on wide platforms.

---
 kernel/sched/sched.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/sched.h +0 -0
index adfb6e3409d7..381cd561e99b 100644

kernel/sched/sched.h static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p) · line 1436

Quoted 5 lines
kernel/sched/sched.h +2 -0
@@ -1432,6 +1432,9 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
 	if (!sched_core_enabled(rq))
 		return true;
 
+	if (rq->core->core_cookie == p->core_cookie)
+		return true;

nit. We can use sched_cpu_cookie_match(rq, p) to check for the above two conditions. but even this is good.

Apart from that, I think this optimization makes sense.

kernel/sched/sched.h static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p) · line 1449

Quoted 10 lines
kernel/sched/sched.h +2 -1
+
 	for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
 		if (!available_idle_cpu(cpu)) {
 			idle_core = false;
@@ -1443,7 +1446,7 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
 	 * A CPU in an idle core is always the best choice for tasks with
 	 * cookies.
 	 */
-	return idle_core || rq->core->core_cookie == p->core_cookie;
+	return idle_core;

Peter, do we care about checking the core_cookie again before returning just in case the task cookie was selected between the check above an here?

If not, then this looks good to me. Feel free to include:

Quoted 3 lines
kernel/sched/sched.h +0 -0
 }
 
 static inline bool sched_group_cookie_match(struct rq *rq,
Signature · 2 lines
Thanks and Regards,
Prateek
Reviewed-by on v1 1/1
#3 lore raw
In reply to Fernand Sieber

Hi Fernand,

Quoted 24 lines · Fernand Sieber

Early return true if the core cookie matches. This avoids the SMT mask loop to check for an idle core, which might be more expensive on wide platforms.

---
 kernel/sched/sched.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/sched.h +3 -0
index adfb6e3409d7..381cd561e99b 100644
@@ -1432,6 +1432,9 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
 	if (!sched_core_enabled(rq))
 		return true;
 
+	if (rq->core->core_cookie == p->core_cookie)
+		return true;
+
 	for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
 		if (!available_idle_cpu(cpu)) {
 			idle_core = false;

kernel/sched/sched.h static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p) · line 1450

Quoted 6 lines
kernel/sched/sched.h +1 -1
@@ -1443,7 +1446,7 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
 	 * A CPU in an idle core is always the best choice for tasks with
 	 * cookies.
 	 */
-	return idle_core || rq->core->core_cookie == p->core_cookie;
+	return idle_core;
 }

LGTM.

Thanks,
Madadi Vineeth Reddy

Quoted 2 lines
kernel/sched/sched.h +0 -0
 static inline bool sched_group_cookie_match(struct rq *rq,
maintainer
#2 lore raw
In reply to K Prateek Nayak
Quoted 28 lines · K Prateek Nayak

Hello Fernand,

Quoted 14 lines · Fernand Sieber

Early return true if the core cookie matches. This avoids the SMT mask loop to check for an idle core, which might be more expensive on wide platforms.

---
 kernel/sched/sched.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/sched.h +0 -0
index adfb6e3409d7..381cd561e99b 100644

kernel/sched/sched.h static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p) · line 1436

Quoted 5 lines
kernel/sched/sched.h +2 -0
@@ -1432,6 +1432,9 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
 	if (!sched_core_enabled(rq))
 		return true;
 
+	if (rq->core->core_cookie == p->core_cookie)
+		return true;

nit. We can use sched_cpu_cookie_match(rq, p) to check for the above two conditions. but even this is good.

Apart from that, I think this optimization makes sense.

Quoted 14 lines

kernel/sched/sched.h static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p) · line 1449

Quoted 10 lines
kernel/sched/sched.h +2 -1
+
 	for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
 		if (!available_idle_cpu(cpu)) {
 			idle_core = false;
@@ -1443,7 +1446,7 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
 	 * A CPU in an idle core is always the best choice for tasks with
 	 * cookies.
 	 */
-	return idle_core || rq->core->core_cookie == p->core_cookie;
+	return idle_core;

Peter, do we care about checking the core_cookie again before returning just in case the task cookie was selected between the check above an here?

I don't think it really matters, but someone what runs this stuff would probably know better than me ;-)

Quoted 3 lines

If not, then this looks good to me. Feel free to include:

Thanks all!

merge bot Applied
#4 lore raw

[tip: sched/core] sched/core: Optimize core cookie matching check

In reply to Fernand Sieber
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:     7f829bde94b1c97b1804fa5860e066ea49dbfca3
Gitweb:        https://git.kernel.org/tip/7f829bde94b1c97b1804fa5860e066ea49dbfca3
Author:        Fernand Sieber <[email protected]>
AuthorDate:    Wed, 05 Nov 2025 17:25:37 +02:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 11 Nov 2025 12:33:37 +01:00

sched/core: Optimize core cookie matching check

Early return true if the core cookie matches. This avoids the SMT mask loop to check for an idle core, which might be more expensive on wide platforms.

---
 kernel/sched/sched.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/sched/sched.h +4 -1
index d04e007..82e74e8 100644
@@ -1432,6 +1432,9 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
 	if (!sched_core_enabled(rq))
 		return true;
 
+	if (rq->core->core_cookie == p->core_cookie)
+		return true;
+
 	for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
 		if (!available_idle_cpu(cpu)) {
 			idle_core = false;
@@ -1443,7 +1446,7 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
 	 * A CPU in an idle core is always the best choice for tasks with
 	 * cookies.
 	 */
-	return idle_core || rq->core->core_cookie == p->core_cookie;
+	return idle_core;
 }
 
 static inline bool sched_group_cookie_match(struct rq *rq,