2022-10-03 22:25:17

by Ali Raza

[permalink] [raw]
Subject: [RFC UKL 03/10] sched: Add task_struct tracking of kernel or application execution

Because UKL removes the barrier between kernel and user space, we need to
track if we are executing application code or kernel code to ensure that we
take the appropriate actions on transitions. When we transition to kernel
code, we need to handle RCU and on the way to user code we need to check if
scheduling needs to happen, etc. We cannot use the CS value from the stack
because it will always be set to the kernel value. These functions will be
used in a later change to entry_64.S to identify the execution context for
the current thread.

Cc: Jonathan Corbet <[email protected]>
Cc: Masahiro Yamada <[email protected]>
Cc: Michal Marek <[email protected]>
Cc: Nick Desaulniers <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Eric Biederman <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Juri Lelli <[email protected]>
Cc: Vincent Guittot <[email protected]>
Cc: Dietmar Eggemann <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Ben Segall <[email protected]>
Cc: Mel Gorman <[email protected]>
Cc: Daniel Bristot de Oliveira <[email protected]>
Cc: Valentin Schneider <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Josh Poimboeuf <[email protected]>

Co-developed-by: Daniel Bristot de Oliveira <[email protected]>
Signed-off-by: Daniel Bristot de Oliveira <[email protected]>
Co-developed-by: Ali Raza <[email protected]>
Signed-off-by: Ali Raza <[email protected]>
---
arch/x86/kernel/process_64.c | 22 ++++++++++++++++++++++
include/linux/sched.h | 26 ++++++++++++++++++++++++++
2 files changed, 48 insertions(+)

diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index 1962008fe743..e9e4a2946452 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -501,6 +501,28 @@ void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase)
task->thread.gsbase = gsbase;
}

+#ifdef CONFIG_UNIKERNEL_LINUX
+/*
+ * 0 = Non UKL thread
+ * 1 = UKL thread - in kernel code
+ * 2 = UKL thread - in application code
+ */
+int is_ukl_thread(void)
+{
+ return current->ukl_thread;
+}
+
+void enter_ukl_user(void)
+{
+ current->ukl_thread = UKL_APPLICATION;
+}
+
+void enter_ukl_kernel(void)
+{
+ current->ukl_thread = UKL_KERNEL;
+}
+#endif
+
static void
start_thread_common(struct pt_regs *regs, unsigned long new_ip,
unsigned long new_sp,
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e7b2f8a5c711..b8bf50ae0fda 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -746,6 +746,13 @@ struct task_struct {
randomized_struct_fields_start

void *stack;
+#ifdef CONFIG_UNIKERNEL_LINUX
+ /*
+ * Indicator used for threads in a UKL application, 0 means non-UKL thread, 1 is UKL thread
+ * in kernel text, 2 is UKL thread in application text
+ */
+ int ukl_thread;
+#endif
refcount_t usage;
/* Per task flags (PF_*), defined further below: */
unsigned int flags;
@@ -1529,6 +1536,25 @@ struct task_struct {
*/
};

+/*
+ * 0 = Non UKL thread
+ * 1 = UKL thread - in kernel code
+ * 2 = UKL thread - in application code
+ */
+#define NON_UKL_THREAD 0
+#define UKL_KERNEL 1
+#define UKL_APPLICATION 2
+
+#ifdef CONFIG_UNIKERNEL_LINUX
+int is_ukl_thread(void);
+void enter_ukl_user(void);
+void enter_ukl_kernel(void);
+#else
+static inline int is_ukl_thread(void) { return NON_UKL_THREAD; }
+static inline void enter_ukl_user(void) {}
+static inline void enter_ukl_kernel(void) {}
+#endif
+
static inline struct pid *task_pid(struct task_struct *task)
{
return task->thread_pid;
--
2.21.3