public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] sched: Add core cookie update tracepoint
@ 2025-01-28 11:34 Fernand Sieber
  0 siblings, 0 replies; only message in thread
From: Fernand Sieber @ 2025-01-28 11:34 UTC (permalink / raw)
  To: linux-kernel, Peter Zijlstra, Ingo Molnar, Vincent Guittot
  Cc: linux-trace-kernel, James Gowans, nh-open-source

ftrace based analysis can be used to qualify correct behavior of cookie based
applications. Security posture can be verified by tracing the amount of time
that tasks with different cookies collocate on hyperthread siblings and
asserting that it is kept to a minimum. Performance posture can be we measured
by minimizing foce idle (when an hyperthread is kept idle because it doesn't
have a runnable task matching the cookie of its sibling). It is necessary
for the application doing such an analyis to know the cookie associated with
each task at any point in time.

While the task to cookie mapping is driven by userspace and thus can
alternatively be supplied through a custom side channel to an application
analysing a trace, it is more convenient and accurate if the mapping is already
part of the trace. Given that these events are infrequent the induced
overhead is negligible.

Signed-off-by: Fernand Sieber <sieberf@amazon.com>
---
 include/trace/events/sched.h | 30 ++++++++++++++++++++++++++++++
 kernel/sched/core_sched.c    |  1 +
 2 files changed, 31 insertions(+)

diff --git a/include/trace/events/sched.h b/include/trace/events/sched.h
index 9ea4c404bd4e..6034acbc1893 100644
--- a/include/trace/events/sched.h
+++ b/include/trace/events/sched.h
@@ -769,6 +769,36 @@ TRACE_EVENT(sched_wake_idle_without_ipi,
 	TP_printk("cpu=%d", __entry->cpu)
 );
 
+#ifdef CONFIG_SCHED_CORE
+/*
+ * Tracepoint for assigning cookies.
+ */
+TRACE_EVENT(sched_setcookie,
+
+	TP_PROTO(struct task_struct *tsk, unsigned long cookie),
+
+	TP_ARGS(tsk, cookie),
+
+	TP_STRUCT__entry(
+		__array(char, comm, TASK_COMM_LEN)
+		__field(pid_t, pid)
+		__field(unsigned long, oldcookie)
+		__field(unsigned long, newcookie)
+	),
+
+	TP_fast_assign(
+		memcpy(__entry->comm, tsk->comm, TASK_COMM_LEN);
+		__entry->pid		= tsk->pid;
+		__entry->oldcookie      = tsk->core_cookie;
+		__entry->newcookie      = cookie;
+	),
+
+	TP_printk("comm=%s pid=%d oldcookie=%lx newcookie=%lx",
+			__entry->comm, __entry->pid,
+			__entry->oldcookie, __entry->newcookie)
+);
+#endif
+
 /*
  * Following tracepoints are not exported in tracefs and provide hooking
  * mechanisms only for testing and debugging purposes.
diff --git a/kernel/sched/core_sched.c b/kernel/sched/core_sched.c
index 1ef98a93eb1d..0321ccd9f944 100644
--- a/kernel/sched/core_sched.c
+++ b/kernel/sched/core_sched.c
@@ -71,6 +71,7 @@ static unsigned long sched_core_update_cookie(struct task_struct *p,
 		sched_core_dequeue(rq, p, DEQUEUE_SAVE);
 
 	old_cookie = p->core_cookie;
+	trace_sched_setcookie(p, cookie);
 	p->core_cookie = cookie;
 
 	/*
-- 
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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2025-01-28 11:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-28 11:34 [PATCH] sched: Add core cookie update tracepoint Fernand Sieber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox