diff mbox series

sched: Add core cookie update tracepoint

Message ID 20250128113410.263994-1-sieberf@amazon.com (mailing list archive)
State New
Headers show
Series sched: Add core cookie update tracepoint | expand

Commit Message

Fernand Sieber Jan. 28, 2025, 11:34 a.m. UTC
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 mbox series

Patch

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;
 
 	/*