sched/fair: Don’t free p->numa_faults with concurrent readers [Linux 4.14.136]

This Linux kernel change "sched/fair: Don’t free p->numa_faults with concurrent readers" is included in the Linux 4.14.136 release. This change is authored by Jann Horn <jannh [at]> on Tue Jul 16 17:20:45 2019 +0200. The commit for this change in Linux stable tree is d091921 (patch) which is from upstream commit 16d51a5. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream 16d51a5.

sched/fair: Don't free p->numa_faults with concurrent readers

commit 16d51a590a8ce3befb1308e0e7ab77f3b661af33 upstream.

When going through execve(), zero out the NUMA fault statistics instead of
freeing them.

During execve, the task is reachable through procfs and the scheduler. A
concurrent /proc/*/sched reader can read data from a freed ->numa_faults
allocation (confirmed by KASAN) and write it back to userspace.
I believe that it would also be possible for a use-after-free read to occur
through a race between a NUMA fault and execve(): task_numa_fault() can
lead to task_numa_compare(), which invokes task_weight() on the currently
running task of a different CPU.

Another way to fix this would be to make ->numa_faults RCU-managed or add
extra locking, but it seems easier to wipe the NUMA fault statistics on

Signed-off-by: Jann Horn <>
Signed-off-by: Peter Zijlstra (Intel) <>
Cc: Linus Torvalds <>
Cc: Peter Zijlstra <>
Cc: Petr Mladek <>
Cc: Sergey Senozhatsky <>
Cc: Thomas Gleixner <>
Cc: Will Deacon <>
Fixes: 82727018b0d3 ("sched/numa: Call task_numa_free() from do_execve()")
Signed-off-by: Ingo Molnar <>
Signed-off-by: Greg Kroah-Hartman <>

There are 32 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 fs/exec.c                            |  2 +-
 include/linux/sched/numa_balancing.h |  4 ++--
 kernel/fork.c                        |  2 +-
 kernel/sched/fair.c                  | 24 ++++++++++++++++++++----
 4 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 0936b5a..4623fc3 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1808,7 +1808,7 @@ static int do_execveat_common(int fd, struct filename *filename,
    current->in_execve = 0;
-   task_numa_free(current);
+   task_numa_free(current, false);
diff --git a/include/linux/sched/numa_balancing.h b/include/linux/sched/numa_balancing.h
index e7dd04a..3988762 100644
--- a/include/linux/sched/numa_balancing.h
+++ b/include/linux/sched/numa_balancing.h
@@ -19,7 +19,7 @@
 extern void task_numa_fault(int last_node, int node, int pages, int flags);
 extern pid_t task_numa_group_id(struct task_struct *p);
 extern void set_numabalancing_state(bool enabled);
-extern void task_numa_free(struct task_struct *p);
+extern void task_numa_free(struct task_struct *p, bool final);
 extern bool should_numa_migrate_memory(struct task_struct *p, struct page *page,
                    int src_nid, int dst_cpu);
@@ -34,7 +34,7 @@ static inline pid_t task_numa_group_id(struct task_struct *p)
 static inline void set_numabalancing_state(bool enabled)
-static inline void task_numa_free(struct task_struct *p)
+static inline void task_numa_free(struct task_struct *p, bool final)
 static inline bool should_numa_migrate_memory(struct task_struct *p,
diff --git a/kernel/fork.c b/kernel/fork.c
index a5bb8fa..919e7cd 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -415,7 +415,7 @@ void __put_task_struct(struct task_struct *tsk)
    WARN_ON(tsk == current);

-   task_numa_free(tsk);
+   task_numa_free(tsk, true);
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index af7de1f..0a4e882 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -2358,13 +2358,23 @@ static void task_numa_group(struct task_struct *p, int cpupid, int flags,

-void task_numa_free(struct task_struct *p)
+ * Get rid of NUMA staticstics associated with a task (either current or dead).
+ * If @final is set, the task is dead and has reached refcount zero, so we can
+ * safely free all relevant data structures. Otherwise, there might be
+ * concurrent reads from places like load balancing and procfs, and we should
+ * reset the data back to default state without freeing ->numa_faults.
+ */
+void task_numa_free(struct task_struct *p, bool final)
    struct numa_group *grp = p->numa_group;
-   void *numa_faults = p->numa_faults;
+   unsigned long *numa_faults = p->numa_faults;
    unsigned long flags;
    int i;

+   if (!numa_faults)
+       return;
    if (grp) {
        spin_lock_irqsave(&grp->lock, flags);
        for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
@@ -2377,8 +2387,14 @@ void task_numa_free(struct task_struct *p)

-   p->numa_faults = NULL;
-   kfree(numa_faults);
+   if (final) {
+       p->numa_faults = NULL;
+       kfree(numa_faults);
+   } else {
+       p->total_numa_faults = 0;
+       for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
+           numa_faults[i] = 0;
+   }


Leave a Reply

Your email address will not be published. Required fields are marked *