Skip to content

Commit e4a3840

Browse files
PacheNicotorvalds
authored andcommitted
oom_kill.c: futex: delay the OOM reaper to allow time for proper futex cleanup
The pthread struct is allocated on PRIVATE|ANONYMOUS memory [1] which can be targeted by the oom reaper. This mapping is used to store the futex robust list head; the kernel does not keep a copy of the robust list and instead references a userspace address to maintain the robustness during a process death. A race can occur between exit_mm and the oom reaper that allows the oom reaper to free the memory of the futex robust list before the exit path has handled the futex death: CPU1 CPU2 -------------------------------------------------------------------- page_fault do_exit "signal" wake_oom_reaper oom_reaper oom_reap_task_mm (invalidates mm) exit_mm exit_mm_release futex_exit_release futex_cleanup exit_robust_list get_user (EFAULT- can't access memory) If the get_user EFAULT's, the kernel will be unable to recover the waiters on the robust_list, leaving userspace mutexes hung indefinitely. Delay the OOM reaper, allowing more time for the exit path to perform the futex cleanup. Reproducer: https://gitlab.com/jsavitz/oom_futex_reproducer Based on a patch by Michal Hocko. Link: https://elixir.bootlin.com/glibc/glibc-2.35/source/nptl/allocatestack.c#L370 [1] Link: https://lkml.kernel.org/r/20220414144042.677008-1-npache@redhat.com Fixes: 2129258 ("mm: oom: let oom_reap_task and exit_mmap run concurrently") Signed-off-by: Joel Savitz <jsavitz@redhat.com> Signed-off-by: Nico Pache <npache@redhat.com> Co-developed-by: Joel Savitz <jsavitz@redhat.com> Suggested-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Michal Hocko <mhocko@suse.com> Cc: Rafael Aquini <aquini@redhat.com> Cc: Waiman Long <longman@redhat.com> Cc: Herton R. Krzesinski <herton@redhat.com> Cc: Juri Lelli <juri.lelli@redhat.com> Cc: Vincent Guittot <vincent.guittot@linaro.org> Cc: Dietmar Eggemann <dietmar.eggemann@arm.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Ben Segall <bsegall@google.com> Cc: Mel Gorman <mgorman@suse.de> Cc: Daniel Bristot de Oliveira <bristot@redhat.com> Cc: David Rientjes <rientjes@google.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Joel Savitz <jsavitz@redhat.com> Cc: Darren Hart <dvhart@infradead.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 80df2fb commit e4a3840

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

include/linux/sched.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,7 @@ struct task_struct {
14431443
int pagefault_disabled;
14441444
#ifdef CONFIG_MMU
14451445
struct task_struct *oom_reaper_list;
1446+
struct timer_list oom_reaper_timer;
14461447
#endif
14471448
#ifdef CONFIG_VMAP_STACK
14481449
struct vm_struct *stack_vm_area;

mm/oom_kill.c

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ static void oom_reap_task(struct task_struct *tsk)
632632
*/
633633
set_bit(MMF_OOM_SKIP, &mm->flags);
634634

635-
/* Drop a reference taken by wake_oom_reaper */
635+
/* Drop a reference taken by queue_oom_reaper */
636636
put_task_struct(tsk);
637637
}
638638

@@ -644,12 +644,12 @@ static int oom_reaper(void *unused)
644644
struct task_struct *tsk = NULL;
645645

646646
wait_event_freezable(oom_reaper_wait, oom_reaper_list != NULL);
647-
spin_lock(&oom_reaper_lock);
647+
spin_lock_irq(&oom_reaper_lock);
648648
if (oom_reaper_list != NULL) {
649649
tsk = oom_reaper_list;
650650
oom_reaper_list = tsk->oom_reaper_list;
651651
}
652-
spin_unlock(&oom_reaper_lock);
652+
spin_unlock_irq(&oom_reaper_lock);
653653

654654
if (tsk)
655655
oom_reap_task(tsk);
@@ -658,30 +658,56 @@ static int oom_reaper(void *unused)
658658
return 0;
659659
}
660660

661-
static void wake_oom_reaper(struct task_struct *tsk)
661+
static void wake_oom_reaper(struct timer_list *timer)
662662
{
663-
/* mm is already queued? */
664-
if (test_and_set_bit(MMF_OOM_REAP_QUEUED, &tsk->signal->oom_mm->flags))
665-
return;
663+
struct task_struct *tsk = container_of(timer, struct task_struct,
664+
oom_reaper_timer);
665+
struct mm_struct *mm = tsk->signal->oom_mm;
666+
unsigned long flags;
666667

667-
get_task_struct(tsk);
668+
/* The victim managed to terminate on its own - see exit_mmap */
669+
if (test_bit(MMF_OOM_SKIP, &mm->flags)) {
670+
put_task_struct(tsk);
671+
return;
672+
}
668673

669-
spin_lock(&oom_reaper_lock);
674+
spin_lock_irqsave(&oom_reaper_lock, flags);
670675
tsk->oom_reaper_list = oom_reaper_list;
671676
oom_reaper_list = tsk;
672-
spin_unlock(&oom_reaper_lock);
677+
spin_unlock_irqrestore(&oom_reaper_lock, flags);
673678
trace_wake_reaper(tsk->pid);
674679
wake_up(&oom_reaper_wait);
675680
}
676681

682+
/*
683+
* Give the OOM victim time to exit naturally before invoking the oom_reaping.
684+
* The timers timeout is arbitrary... the longer it is, the longer the worst
685+
* case scenario for the OOM can take. If it is too small, the oom_reaper can
686+
* get in the way and release resources needed by the process exit path.
687+
* e.g. The futex robust list can sit in Anon|Private memory that gets reaped
688+
* before the exit path is able to wake the futex waiters.
689+
*/
690+
#define OOM_REAPER_DELAY (2*HZ)
691+
static void queue_oom_reaper(struct task_struct *tsk)
692+
{
693+
/* mm is already queued? */
694+
if (test_and_set_bit(MMF_OOM_REAP_QUEUED, &tsk->signal->oom_mm->flags))
695+
return;
696+
697+
get_task_struct(tsk);
698+
timer_setup(&tsk->oom_reaper_timer, wake_oom_reaper, 0);
699+
tsk->oom_reaper_timer.expires = jiffies + OOM_REAPER_DELAY;
700+
add_timer(&tsk->oom_reaper_timer);
701+
}
702+
677703
static int __init oom_init(void)
678704
{
679705
oom_reaper_th = kthread_run(oom_reaper, NULL, "oom_reaper");
680706
return 0;
681707
}
682708
subsys_initcall(oom_init)
683709
#else
684-
static inline void wake_oom_reaper(struct task_struct *tsk)
710+
static inline void queue_oom_reaper(struct task_struct *tsk)
685711
{
686712
}
687713
#endif /* CONFIG_MMU */
@@ -932,7 +958,7 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
932958
rcu_read_unlock();
933959

934960
if (can_oom_reap)
935-
wake_oom_reaper(victim);
961+
queue_oom_reaper(victim);
936962

937963
mmdrop(mm);
938964
put_task_struct(victim);
@@ -968,7 +994,7 @@ static void oom_kill_process(struct oom_control *oc, const char *message)
968994
task_lock(victim);
969995
if (task_will_free_mem(victim)) {
970996
mark_oom_victim(victim);
971-
wake_oom_reaper(victim);
997+
queue_oom_reaper(victim);
972998
task_unlock(victim);
973999
put_task_struct(victim);
9741000
return;
@@ -1067,7 +1093,7 @@ bool out_of_memory(struct oom_control *oc)
10671093
*/
10681094
if (task_will_free_mem(current)) {
10691095
mark_oom_victim(current);
1070-
wake_oom_reaper(current);
1096+
queue_oom_reaper(current);
10711097
return true;
10721098
}
10731099

0 commit comments

Comments
 (0)