[patch v11 09/13] task isolation: add preempt notifier to sync per-CPU vmstat dirty info to thread info

From: Marcelo Tosatti
Date: Fri Feb 04 2022 - 12:37:17 EST


If a thread has task isolation activated, is preempted by thread B,
which marks vmstat information dirty, and is preempted back in,
one might return to userspace with vmstat dirty information on the
CPU in question.

To address this problem, add a preempt notifier that transfers vmstat dirty
information to TIF_TASK_ISOL thread flag.

Signed-off-by: Marcelo Tosatti <mtosatti@xxxxxxxxxx>

Index: linux-2.6/kernel/task_isolation.c
===================================================================
--- linux-2.6.orig/kernel/task_isolation.c
+++ linux-2.6/kernel/task_isolation.c
@@ -19,6 +19,7 @@
#include <linux/sched/task.h>
#include <linux/mm.h>
#include <linux/vmstat.h>
+#include <linux/preempt.h>
#include <linux/task_isolation.h>

void __task_isol_exit(struct task_struct *tsk)
@@ -30,6 +31,9 @@ void __task_isol_exit(struct task_struct
return;

static_key_slow_dec(&vmstat_sync_enabled);
+
+ preempt_notifier_unregister(&i->preempt_notifier);
+ preempt_notifier_dec();
}

void __task_isol_free(struct task_struct *tsk)
@@ -40,6 +44,21 @@ void __task_isol_free(struct task_struct
tsk->task_isol_info = NULL;
}

+static void task_isol_sched_in(struct preempt_notifier *pn, int cpu)
+{
+ vmstat_dirty_to_thread_flag();
+}
+
+static void task_isol_sched_out(struct preempt_notifier *pn,
+ struct task_struct *next)
+{
+}
+
+static __read_mostly struct preempt_ops task_isol_preempt_ops = {
+ .sched_in = task_isol_sched_in,
+ .sched_out = task_isol_sched_out,
+};
+
static struct task_isol_info *task_isol_alloc_context(void)
{
struct task_isol_info *info;
@@ -48,6 +67,10 @@ static struct task_isol_info *task_isol_
if (unlikely(!info))
return ERR_PTR(-ENOMEM);

+ preempt_notifier_inc();
+ preempt_notifier_init(&info->preempt_notifier, &task_isol_preempt_ops);
+ preempt_notifier_register(&info->preempt_notifier);
+
preempt_disable();
init_sync_vmstat();
preempt_enable();
Index: linux-2.6/include/linux/task_isolation.h
===================================================================
--- linux-2.6.orig/include/linux/task_isolation.h
+++ linux-2.6/include/linux/task_isolation.h
@@ -17,6 +17,8 @@ struct task_isol_info {
u64 oneshot_mask;

u8 inherit_mask;
+
+ struct preempt_notifier preempt_notifier;
};

extern void __task_isol_free(struct task_struct *tsk);
Index: linux-2.6/include/linux/vmstat.h
===================================================================
--- linux-2.6.orig/include/linux/vmstat.h
+++ linux-2.6/include/linux/vmstat.h
@@ -34,10 +34,16 @@ static inline void sync_vmstat(void)
}

void init_sync_vmstat(void);
+
+void vmstat_dirty_to_thread_flag(void);
#else
static inline void sync_vmstat(void)
{
}
+
+static inline void vmstat_dirty_to_thread_flag(void)
+{
+}
#endif

struct reclaim_stat {
Index: linux-2.6/mm/vmstat.c
===================================================================
--- linux-2.6.orig/mm/vmstat.c
+++ linux-2.6/mm/vmstat.c
@@ -353,6 +353,13 @@ void init_sync_vmstat(void)
set_thread_flag(TIF_TASK_ISOL);
}
EXPORT_SYMBOL_GPL(vmstat_dirty);
+
+void vmstat_dirty_to_thread_flag(void)
+{
+ if (raw_cpu_read(vmstat_dirty) == true)
+ set_thread_flag(TIF_TASK_ISOL);
+}
+EXPORT_SYMBOL_GPL(vmstat_dirty_to_thread_flag);
#else
static inline void mark_vmstat_dirty(void)
{