天天看點

linux記憶體catch很大,【Linux記憶體源碼分析】記憶體溢出保護機制(OOM)

Linux系統記憶體管理中存在着一個稱之為OOM killer(Out-Of-Memory killer)的機制,該機制主要用于記憶體監控,監控程序的記憶體使用量,當系統的記憶體耗盡時,其将根據算法選擇性地kill了部分程序。本文分析的記憶體溢出保護機制,也就是OOM killer機制了。

回到夥伴管理算法中涉及的一函數__alloc_pages_nodemask(),其裡面調用的__alloc_pages_slowpath()并未展開深入,而記憶體溢出保護機制則在此函數中。

先行檢視一下__alloc_pages_slowpath()的實作:

【file:/ mm/page_alloc.h】

static inline struct page *

__alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,

struct zonelist *zonelist, enum zone_type high_zoneidx,

nodemask_t *nodemask, struct zone *preferred_zone,

int migratetype)

{

const gfp_t wait = gfp_mask & __GFP_WAIT;

struct page *page = NULL;

int alloc_flags;

unsigned long pages_reclaimed = 0;

unsigned long did_some_progress;

bool sync_migration = false;

bool deferred_compaction = false;

bool contended_compaction = false;

if (order >= MAX_ORDER) {

WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));

return NULL;

}

if (IS_ENABLED(CONFIG_NUMA) &&

(gfp_mask & GFP_THISNODE) == GFP_THISNODE)

goto nopage;

restart:

if (!(gfp_mask & __GFP_NO_KSWAPD))

wake_all_kswapds(order, zonelist, high_zoneidx, preferred_zone);

alloc_flags = gfp_to_alloc_flags(gfp_mask);

if (!(alloc_flags & ALLOC_CPUSET) && !nodemask)

first_zones_zonelist(zonelist, high_zoneidx, NULL,

&preferred_zone);

rebalance:

page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,

high_zoneidx, alloc_flags & ~ALLOC_NO_WATERMARKS,

preferred_zone, migratetype);

if (page)

goto got_pg;

if (alloc_flags & ALLOC_NO_WATERMARKS) {

zonelist = node_zonelist(numa_node_id(), gfp_mask);

page = __alloc_pages_high_priority(gfp_mask, order,

zonelist, high_zoneidx, nodemask,

preferred_zone, migratetype);

if (page) {

goto got_pg;

}

}

if (!wait) {

WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL);

goto nopage;

}

if (current->flags & PF_MEMALLOC)

goto nopage;

if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))

goto nopage;

page = __alloc_pages_direct_compact(gfp_mask, order,

zonelist, high_zoneidx,

nodemask,

alloc_flags, preferred_zone,

migratetype, sync_migration,

&contended_compaction,

&deferred_compaction,

&did_some_progress);

if (page)

goto got_pg;

sync_migration = true;

if ((deferred_compaction || contended_compaction) &&

(gfp_mask & __GFP_NO_KSWAPD))

goto nopage;

page = __alloc_pages_direct_reclaim(gfp_mask, order,

zonelist, high_zoneidx,

nodemask,

alloc_flags, preferred_zone,

migratetype, &did_some_progress);

if (page)

goto got_pg;

if (!did_some_progress) {

if (oom_gfp_allowed(gfp_mask)) {

if (oom_killer_disabled)

goto nopage;

if ((current->flags & PF_DUMPCORE) &&

!(gfp_mask & __GFP_NOFAIL))

goto nopage;

page = __alloc_pages_may_oom(gfp_mask, order,

zonelist, high_zoneidx,

nodemask, preferred_zone,

migratetype);

if (page)

goto got_pg;

if (!(gfp_mask & __GFP_NOFAIL)) {

if (order > PAGE_ALLOC_COSTLY_ORDER)

goto nopage;

if (high_zoneidx < ZONE_NORMAL)

goto nopage;

}

goto restart;

}

}

pages_reclaimed += did_some_progress;

if (should_alloc_retry(gfp_mask, order, did_some_progress,

pages_reclaimed)) {

wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);

goto rebalance;

} else {

page = __alloc_pages_direct_compact(gfp_mask, order,

zonelist, high_zoneidx,

nodemask,

alloc_flags, preferred_zone,

migratetype, sync_migration,

&contended_compaction,

&deferred_compaction,

&did_some_progress);

if (page)

goto got_pg;

}

nopage:

warn_alloc_failed(gfp_mask, order, NULL);

return page;

got_pg:

if (kmemcheck_enabled)

kmemcheck_pagealloc_alloc(page, order, gfp_mask);

return page;

}

該函數首先判斷調用者是否禁止喚醒kswapd線程,若不做禁止則喚醒線程進行記憶體回收工作,然後通過gfp_to_alloc_flags()對記憶體配置設定辨別進行調整,而後再次調用get_page_from_freelist()嘗試配置設定,如果配置設定到則退出。否則繼續嘗試記憶體配置設定,繼續嘗試配置設定則先行判斷是否設定了ALLOC_NO_WATERMARKS辨別,如果設定了,則将忽略watermark,調用__alloc_pages_high_priority()進行配置設定。

__alloc_pages_high_priority()函數實作:

【file:/ mm/page_alloc.h】

static inline struct page *

__alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,

struct zonelist *zonelist, enum zone_type high_zoneidx,

nodemask_t *nodemask, struct zone *preferred_zone,

int migratetype)

{

struct page *page;

do {

page = get_page_from_freelist(gfp_mask, nodemask, order,

zonelist, high_zoneidx, ALLOC_NO_WATERMARKS,

preferred_zone, migratetype);

if (!page && gfp_mask & __GFP_NOFAIL)

wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);

} while (!page && (gfp_mask & __GFP_NOFAIL));

return page;

}

可以看到該函數根據配置設定辨別__GFP_NOFAIL不斷地調用get_page_from_freelist()循環嘗試去獲得記憶體。

接着回到__alloc_pages_slowpath()中,其從__alloc_pages_high_priority()退出後繼而判斷是否設定了__GFP_WAIT辨別,如果設定則表示記憶體配置設定運作休眠,否則直接以配置設定記憶體失敗而退出。接着将會調用__alloc_pages_direct_compact()和__alloc_pages_direct_reclaim()嘗試回收記憶體并嘗試配置設定。基于上面的多種嘗試記憶體配置設定仍然失敗的情況,将會調用__alloc_pages_may_oom()觸發OOM killer機制。OOM killer将程序kill後會重新再次嘗試記憶體配置設定,最後則是配置設定失敗或配置設定成功的收尾處理。

__alloc_pages_slowpath()暫且分析至此,回到本文重點函數__alloc_pages_may_oom()中進一步進行分析。

【file:/ mm/page_alloc.h】

static inline struct page *

__alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,

struct zonelist *zonelist, enum zone_type high_zoneidx,

nodemask_t *nodemask, struct zone *preferred_zone,

int migratetype)

{

struct page *page;

if (!try_set_zonelist_oom(zonelist, gfp_mask)) {

schedule_timeout_uninterruptible(1);

return NULL;

}

page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,

order, zonelist, high_zoneidx,

ALLOC_WMARK_HIGH|ALLOC_CPUSET,

preferred_zone, migratetype);

if (page)

goto out;

if (!(gfp_mask & __GFP_NOFAIL)) {

if (order > PAGE_ALLOC_COSTLY_ORDER)

goto out;

if (high_zoneidx < ZONE_NORMAL)

goto out;

if (gfp_mask & __GFP_THISNODE)

goto out;

}

out_of_memory(zonelist, gfp_mask, order, nodemask, false);

out:

clear_zonelist_oom(zonelist, gfp_mask);

return page;

}

該函數首先通過try_set_zonelist_oom()判斷OOM killer是否已經在其他核進行killing操作,如果沒有的情況下将會在try_set_zonelist_oom()内部進行鎖操作,確定隻有一個核執行killing的操作。繼而調用get_page_from_freelist()在高watermark的情況下嘗試再次擷取記憶體,不過這裡注定會失敗。接着就是調用到了關鍵函數out_of_memory()。最後函數退出時将會調用clear_zonelist_oom()清除掉try_set_zonelist_oom()裡面的鎖操作。

着重分析一下out_of_memory():

【file:/ mm/oom_kill.c】

void out_of_memory(struct zonelist *zonelist, gfp_t gfp_mask,

int order, nodemask_t *nodemask, bool force_kill)

{

const nodemask_t *mpol_mask;

struct task_struct *p;

unsigned long totalpages;

unsigned long freed = 0;

unsigned int uninitialized_var(points);

enum oom_constraint constraint = CONSTRAINT_NONE;

int killed = 0;

blocking_notifier_call_chain(&oom_notify_list, 0, &freed);

if (freed > 0)

return;

if (fatal_signal_pending(current) || current->flags & PF_EXITING) {

set_thread_flag(TIF_MEMDIE);

return;

}

constraint = constrained_alloc(zonelist, gfp_mask, nodemask,

&totalpages);

mpol_mask = (constraint == CONSTRAINT_MEMORY_POLICY) ? nodemask : NULL;

check_panic_on_oom(constraint, gfp_mask, order, mpol_mask);

if (sysctl_oom_kill_allocating_task && current->mm &&

!oom_unkillable_task(current, NULL, nodemask) &&

current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {

get_task_struct(current);

oom_kill_process(current, gfp_mask, order, 0, totalpages, NULL,

nodemask,

"Out of memory (oom_kill_allocating_task)");

goto out;

}

p = select_bad_process(&points, totalpages, mpol_mask, force_kill);

if (!p) {

dump_header(NULL, gfp_mask, order, NULL, mpol_mask);

panic("Out of memory and no killable processes...\n");

}

if (p != (void *)-1UL) {

oom_kill_process(p, gfp_mask, order, points, totalpages, NULL,

nodemask, "Out of memory");

killed = 1;

}

out:

if (killed)

schedule_timeout_killable(1);

}

該函數首先調用blocking_notifier_call_chain()進行OOM的核心通知鍊回調處理;接着的if (fatal_signal_pending(current) || current->flags &

PF_EXITING)判斷則是用于檢查是否有SIGKILL信号挂起或者正在信号進行中,如果有則退出;再接着通過constrained_alloc()檢查記憶體配置設定限制以及check_panic_on_oom()檢查是否報linux核心panic;繼而判斷sysctl_oom_kill_allocating_task變量及程序檢查,如果符合條件判斷,則将目前配置設定的記憶體kill掉;否則最後,将通過select_bad_process()選出最佳的程序,進而調用oom_kill_process()對其進行kill操作。

最後分析一下select_bad_process()和oom_kill_process(),其中select_bad_process()的實作:

【file:/ mm/oom_kill.c】

static struct task_struct *select_bad_process(unsigned int *ppoints,

unsigned long totalpages, const nodemask_t *nodemask,

bool force_kill)

{

struct task_struct *g, *p;

struct task_struct *chosen = NULL;

unsigned long chosen_points = 0;

rcu_read_lock();

for_each_process_thread(g, p) {

unsigned int points;

switch (oom_scan_process_thread(p, totalpages, nodemask,

force_kill)) {

case OOM_SCAN_SELECT:

chosen = p;

chosen_points = ULONG_MAX;

case OOM_SCAN_CONTINUE:

continue;

case OOM_SCAN_ABORT:

rcu_read_unlock();

return (struct task_struct *)(-1UL);

case OOM_SCAN_OK:

break;

};

points = oom_badness(p, NULL, nodemask, totalpages);

if (!points || points < chosen_points)

continue;

if (points == chosen_points && thread_group_leader(chosen))

continue;

chosen = p;

chosen_points = points;

}

if (chosen)

get_task_struct(chosen);

rcu_read_unlock();

*ppoints = chosen_points * 1000 / totalpages;

return chosen;

}

此函數通過for_each_process_thread()宏周遊所有程序,進而借用oom_scan_process_thread()獲得程序掃描類型然後通過switch-case作特殊化處理,例如存在某程序退出中則中斷掃描、某程序占用記憶體過多且被辨別為優先kill掉則優選等特殊處理。而正常情況則會通過oom_badness()計算出程序的分值,然後根據最高分值将程序控制塊傳回回去。

順便研究一下oom_badness()的實作:

【file:/ mm/oom_kill.c】

unsigned long oom_badness(struct task_struct *p, struct mem_cgroup *memcg,

const nodemask_t *nodemask, unsigned long totalpages)

{

long points;

long adj;

if (oom_unkillable_task(p, memcg, nodemask))

return 0;

p = find_lock_task_mm(p);

if (!p)

return 0;

adj = (long)p->signal->oom_score_adj;

if (adj == OOM_SCORE_ADJ_MIN) {

task_unlock(p);

return 0;

}

points = get_mm_rss(p->mm) + atomic_long_read(&p->mm->nr_ptes) +

get_mm_counter(p->mm, MM_SWAPENTS);

task_unlock(p);

if (has_capability_noaudit(p, CAP_SYS_ADMIN))

points -= (points * 3) / 100;

adj *= totalpages / 1000;

points += adj;

return points > 0 ? points : 1;

}

計算程序分值的函數中,首先排除了不可OOM kill的程序以及oom_score_adj值為OOM_SCORE_ADJ_MIN(即-1000)的程序,其中oom_score_adj取值範圍是-1000到1000;接着就是計算程序的RSS、頁表以及SWAP空間的使用量占RAM的比重,如果該程序是超級程序,則去除3%的權重;最後将oom_score_adj和points歸一後,但凡小于0值的都傳回1,其他的則傳回原值。由此可知,分值越低的則越不會被kill,而且該值可以通過修改oom_score_adj進行調整。

最後分析一下找到了最“bad”的程序後,其享受的“待遇”oom_kill_process():

【file:/ mm/oom_kill.c】

void oom_kill_process(struct task_struct *p, gfp_t gfp_mask, int order,

unsigned int points, unsigned long totalpages,

struct mem_cgroup *memcg, nodemask_t *nodemask,

const char *message)

{

struct task_struct *victim = p;

struct task_struct *child;

struct task_struct *t;

struct mm_struct *mm;

unsigned int victim_points = 0;

static DEFINE_RATELIMIT_STATE(oom_rs, DEFAULT_RATELIMIT_INTERVAL,

DEFAULT_RATELIMIT_BURST);

if (p->flags & PF_EXITING) {

set_tsk_thread_flag(p, TIF_MEMDIE);

put_task_struct(p);

return;

}

if (__ratelimit(&oom_rs))

dump_header(p, gfp_mask, order, memcg, nodemask);

task_lock(p);

pr_err("%s: Kill process %d (%s) score %d or sacrifice child\n",

message, task_pid_nr(p), p->comm, points);

task_unlock(p);

read_lock(&tasklist_lock);

for_each_thread(p, t) {

list_for_each_entry(child, &t->children, sibling) {

unsigned int child_points;

if (child->mm == p->mm)

continue;

child_points = oom_badness(child, memcg, nodemask,

totalpages);

if (child_points > victim_points) {

put_task_struct(victim);

victim = child;

victim_points = child_points;

get_task_struct(victim);

}

}

}

read_unlock(&tasklist_lock);

p = find_lock_task_mm(victim);

if (!p) {

put_task_struct(victim);

return;

} else if (victim != p) {

get_task_struct(p);

put_task_struct(victim);

victim = p;

}

mm = victim->mm;

pr_err("Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB\n",

task_pid_nr(victim), victim->comm, K(victim->mm->total_vm),

K(get_mm_counter(victim->mm, MM_ANONPAGES)),

K(get_mm_counter(victim->mm, MM_FILEPAGES)));

task_unlock(victim);

rcu_read_lock();

for_each_process(p)

if (p->mm == mm && !same_thread_group(p, victim) &&

!(p->flags & PF_KTHREAD)) {

if (p->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)

continue;

task_lock(p);

pr_err("Kill process %d (%s) sharing same memory\n",

task_pid_nr(p), p->comm);

task_unlock(p);

do_send_sig_info(SIGKILL, SEND_SIG_FORCED, p, true);

}

rcu_read_unlock();

set_tsk_thread_flag(victim, TIF_MEMDIE);

do_send_sig_info(SIGKILL, SEND_SIG_FORCED, victim, true);

put_task_struct(victim);

}

該函數将會判斷目前被kill的程序情況,如果該程序處于退出狀态,則設定TIF_MEMDIE标志,不做kill操作;接着會通過list_for_each_entry()周遊該程序的子程序資訊,如果某個子程序擁有不同的mm且合适被kill掉,将會優先考慮将該子程序替代父程序kill掉,這樣可以避免kill掉父程序帶來的接管子程序的工作開銷;再往下通過find_lock_task_mm()找到持有mm鎖的程序,如果程序處于退出狀态,則return,否則繼續處理,若此時的程序與傳入的不是同一個時則更新victim;繼而接着通過for_each_process()查找與目前被kill程序使用到了同樣的共享記憶體的程序進行一起kill掉,kill之前将對應的程序添加辨別TIF_MEMDIE,而kill的動作則是通過發送SICKILL信号給對應程序,由被kill程序從核心态傳回使用者态時進行處理。

至此,OOM kill處理分析完畢。