• David Rientjes's avatar
    mm, memcg: introduce own oom handler to iterate only over its own threads · 9cbb78bb
    David Rientjes authored
    The global oom killer is serialized by the per-zonelist
    try_set_zonelist_oom() which is used in the page allocator.  Concurrent
    oom kills are thus a rare event and only occur in systems using
    mempolicies and with a large number of nodes.
    
    Memory controller oom kills, however, can frequently be concurrent since
    there is no serialization once the oom killer is called for oom conditions
    in several different memcgs in parallel.
    
    This creates a massive contention on tasklist_lock since the oom killer
    requires the readside for the tasklist iteration.  If several memcgs are
    calling the oom killer, this lock can be held for a substantial amount of
    time, especially if threads continue to enter it as other threads are
    exiting.
    
    Since the exit path grabs the writeside of the lock with irqs disabled in
    a few different places, this can cause a soft lockup on cpus as a result
    of tasklist_lock starvation.
    
    The kernel lacks unfair writelocks, and successful calls to the oom killer
    usually result in at least one thread entering the exit path, so an
    alternative solution is needed.
    
    This patch introduces a seperate oom handler for memcgs so that they do
    not require tasklist_lock for as much time.  Instead, it iterates only
    over the threads attached to the oom memcg and grabs a reference to the
    selected thread before calling oom_kill_process() to ensure it doesn't
    prematurely exit.
    
    This still requires tasklist_lock for the tasklist dump, iterating
    children of the selected process, and killing all other threads on the
    system sharing the same memory as the selected victim.  So while this
    isn't a complete solution to tasklist_lock starvation, it significantly
    reduces the amount of time that it is held.
    Acked-by: default avatarKAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
    Acked-by: default avatarMichal Hocko <mhocko@suse.cz>
    Signed-off-by: default avatarDavid Rientjes <rientjes@google.com>
    Cc: Oleg Nesterov <oleg@redhat.com>
    Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
    Reviewed-by: default avatarSha Zhengju <handai.szj@taobao.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    9cbb78bb
oom_kill.c 21.9 KB