12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787 |
- #include <linux/kernel.h>
- #include <linux/mm.h>
- #include <linux/page-flags.h>
- #include <linux/kernel-page-flags.h>
- #include <linux/sched.h>
- #include <linux/ksm.h>
- #include <linux/rmap.h>
- #include <linux/export.h>
- #include <linux/pagemap.h>
- #include <linux/swap.h>
- #include <linux/backing-dev.h>
- #include <linux/migrate.h>
- #include <linux/page-isolation.h>
- #include <linux/suspend.h>
- #include <linux/slab.h>
- #include <linux/swapops.h>
- #include <linux/hugetlb.h>
- #include <linux/memory_hotplug.h>
- #include <linux/mm_inline.h>
- #include <linux/kfifo.h>
- #include <linux/ratelimit.h>
- #include "internal.h"
- #include "ras/ras_event.h"
- int sysctl_memory_failure_early_kill __read_mostly = 0;
- int sysctl_memory_failure_recovery __read_mostly = 1;
- atomic_long_t num_poisoned_pages __read_mostly = ATOMIC_LONG_INIT(0);
- #if defined(CONFIG_HWPOISON_INJECT) || defined(CONFIG_HWPOISON_INJECT_MODULE)
- u32 hwpoison_filter_enable = 0;
- u32 hwpoison_filter_dev_major = ~0U;
- u32 hwpoison_filter_dev_minor = ~0U;
- u64 hwpoison_filter_flags_mask;
- u64 hwpoison_filter_flags_value;
- EXPORT_SYMBOL_GPL(hwpoison_filter_enable);
- EXPORT_SYMBOL_GPL(hwpoison_filter_dev_major);
- EXPORT_SYMBOL_GPL(hwpoison_filter_dev_minor);
- EXPORT_SYMBOL_GPL(hwpoison_filter_flags_mask);
- EXPORT_SYMBOL_GPL(hwpoison_filter_flags_value);
- static int hwpoison_filter_dev(struct page *p)
- {
- struct address_space *mapping;
- dev_t dev;
- if (hwpoison_filter_dev_major == ~0U &&
- hwpoison_filter_dev_minor == ~0U)
- return 0;
-
- if (PageSlab(p))
- return -EINVAL;
- mapping = page_mapping(p);
- if (mapping == NULL || mapping->host == NULL)
- return -EINVAL;
- dev = mapping->host->i_sb->s_dev;
- if (hwpoison_filter_dev_major != ~0U &&
- hwpoison_filter_dev_major != MAJOR(dev))
- return -EINVAL;
- if (hwpoison_filter_dev_minor != ~0U &&
- hwpoison_filter_dev_minor != MINOR(dev))
- return -EINVAL;
- return 0;
- }
- static int hwpoison_filter_flags(struct page *p)
- {
- if (!hwpoison_filter_flags_mask)
- return 0;
- if ((stable_page_flags(p) & hwpoison_filter_flags_mask) ==
- hwpoison_filter_flags_value)
- return 0;
- else
- return -EINVAL;
- }
- #ifdef CONFIG_MEMCG
- u64 hwpoison_filter_memcg;
- EXPORT_SYMBOL_GPL(hwpoison_filter_memcg);
- static int hwpoison_filter_task(struct page *p)
- {
- if (!hwpoison_filter_memcg)
- return 0;
- if (page_cgroup_ino(p) != hwpoison_filter_memcg)
- return -EINVAL;
- return 0;
- }
- #else
- static int hwpoison_filter_task(struct page *p) { return 0; }
- #endif
- int hwpoison_filter(struct page *p)
- {
- if (!hwpoison_filter_enable)
- return 0;
- if (hwpoison_filter_dev(p))
- return -EINVAL;
- if (hwpoison_filter_flags(p))
- return -EINVAL;
- if (hwpoison_filter_task(p))
- return -EINVAL;
- return 0;
- }
- #else
- int hwpoison_filter(struct page *p)
- {
- return 0;
- }
- #endif
- EXPORT_SYMBOL_GPL(hwpoison_filter);
- static int kill_proc(struct task_struct *t, unsigned long addr, int trapno,
- unsigned long pfn, struct page *page, int flags)
- {
- struct siginfo si;
- int ret;
- pr_err("Memory failure: %#lx: Killing %s:%d due to hardware memory corruption\n",
- pfn, t->comm, t->pid);
- si.si_signo = SIGBUS;
- si.si_errno = 0;
- si.si_addr = (void *)addr;
- #ifdef __ARCH_SI_TRAPNO
- si.si_trapno = trapno;
- #endif
- si.si_addr_lsb = compound_order(compound_head(page)) + PAGE_SHIFT;
- if ((flags & MF_ACTION_REQUIRED) && t->mm == current->mm) {
- si.si_code = BUS_MCEERR_AR;
- ret = force_sig_info(SIGBUS, &si, current);
- } else {
-
- si.si_code = BUS_MCEERR_AO;
- ret = send_sig_info(SIGBUS, &si, t);
- }
- if (ret < 0)
- pr_info("Memory failure: Error sending signal to %s:%d: %d\n",
- t->comm, t->pid, ret);
- return ret;
- }
- void shake_page(struct page *p, int access)
- {
- if (!PageSlab(p)) {
- lru_add_drain_all();
- if (PageLRU(p))
- return;
- drain_all_pages(page_zone(p));
- if (PageLRU(p) || is_free_buddy_page(p))
- return;
- }
-
- if (access)
- drop_slab_node(page_to_nid(p));
- }
- EXPORT_SYMBOL_GPL(shake_page);
- struct to_kill {
- struct list_head nd;
- struct task_struct *tsk;
- unsigned long addr;
- char addr_valid;
- };
- static void add_to_kill(struct task_struct *tsk, struct page *p,
- struct vm_area_struct *vma,
- struct list_head *to_kill,
- struct to_kill **tkc)
- {
- struct to_kill *tk;
- if (*tkc) {
- tk = *tkc;
- *tkc = NULL;
- } else {
- tk = kmalloc(sizeof(struct to_kill), GFP_ATOMIC);
- if (!tk) {
- pr_err("Memory failure: Out of memory while machine check handling\n");
- return;
- }
- }
- tk->addr = page_address_in_vma(p, vma);
- tk->addr_valid = 1;
-
- if (tk->addr == -EFAULT) {
- pr_info("Memory failure: Unable to find user space address %lx in %s\n",
- page_to_pfn(p), tsk->comm);
- tk->addr_valid = 0;
- }
- get_task_struct(tsk);
- tk->tsk = tsk;
- list_add_tail(&tk->nd, to_kill);
- }
- static void kill_procs(struct list_head *to_kill, int forcekill, int trapno,
- int fail, struct page *page, unsigned long pfn,
- int flags)
- {
- struct to_kill *tk, *next;
- list_for_each_entry_safe (tk, next, to_kill, nd) {
- if (forcekill) {
-
- if (fail || tk->addr_valid == 0) {
- pr_err("Memory failure: %#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
- pfn, tk->tsk->comm, tk->tsk->pid);
- do_send_sig_info(SIGKILL, SEND_SIG_PRIV,
- tk->tsk, PIDTYPE_PID);
- }
-
- else if (kill_proc(tk->tsk, tk->addr, trapno,
- pfn, page, flags) < 0)
- pr_err("Memory failure: %#lx: Cannot send advisory machine check signal to %s:%d\n",
- pfn, tk->tsk->comm, tk->tsk->pid);
- }
- put_task_struct(tk->tsk);
- kfree(tk);
- }
- }
- static struct task_struct *find_early_kill_thread(struct task_struct *tsk)
- {
- struct task_struct *t;
- for_each_thread(tsk, t)
- if ((t->flags & PF_MCE_PROCESS) && (t->flags & PF_MCE_EARLY))
- return t;
- return NULL;
- }
- static struct task_struct *task_early_kill(struct task_struct *tsk,
- int force_early)
- {
- struct task_struct *t;
- if (!tsk->mm)
- return NULL;
- if (force_early)
- return tsk;
- t = find_early_kill_thread(tsk);
- if (t)
- return t;
- if (sysctl_memory_failure_early_kill)
- return tsk;
- return NULL;
- }
- static void collect_procs_anon(struct page *page, struct list_head *to_kill,
- struct to_kill **tkc, int force_early)
- {
- struct vm_area_struct *vma;
- struct task_struct *tsk;
- struct anon_vma *av;
- pgoff_t pgoff;
- av = page_lock_anon_vma_read(page);
- if (av == NULL)
- return;
- pgoff = page_to_pgoff(page);
- read_lock(&tasklist_lock);
- for_each_process (tsk) {
- struct anon_vma_chain *vmac;
- struct task_struct *t = task_early_kill(tsk, force_early);
- if (!t)
- continue;
- anon_vma_interval_tree_foreach(vmac, &av->rb_root,
- pgoff, pgoff) {
- vma = vmac->vma;
- if (!page_mapped_in_vma(page, vma))
- continue;
- if (vma->vm_mm == t->mm)
- add_to_kill(t, page, vma, to_kill, tkc);
- }
- }
- read_unlock(&tasklist_lock);
- page_unlock_anon_vma_read(av);
- }
- static void collect_procs_file(struct page *page, struct list_head *to_kill,
- struct to_kill **tkc, int force_early)
- {
- struct vm_area_struct *vma;
- struct task_struct *tsk;
- struct address_space *mapping = page->mapping;
- i_mmap_lock_read(mapping);
- read_lock(&tasklist_lock);
- for_each_process(tsk) {
- pgoff_t pgoff = page_to_pgoff(page);
- struct task_struct *t = task_early_kill(tsk, force_early);
- if (!t)
- continue;
- vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff,
- pgoff) {
-
- if (vma->vm_mm == t->mm)
- add_to_kill(t, page, vma, to_kill, tkc);
- }
- }
- read_unlock(&tasklist_lock);
- i_mmap_unlock_read(mapping);
- }
- static void collect_procs(struct page *page, struct list_head *tokill,
- int force_early)
- {
- struct to_kill *tk;
- if (!page->mapping)
- return;
- tk = kmalloc(sizeof(struct to_kill), GFP_NOIO);
- if (!tk)
- return;
- if (PageAnon(page))
- collect_procs_anon(page, tokill, &tk, force_early);
- else
- collect_procs_file(page, tokill, &tk, force_early);
- kfree(tk);
- }
- static const char *action_name[] = {
- [MF_IGNORED] = "Ignored",
- [MF_FAILED] = "Failed",
- [MF_DELAYED] = "Delayed",
- [MF_RECOVERED] = "Recovered",
- };
- static const char * const action_page_types[] = {
- [MF_MSG_KERNEL] = "reserved kernel page",
- [MF_MSG_KERNEL_HIGH_ORDER] = "high-order kernel page",
- [MF_MSG_SLAB] = "kernel slab page",
- [MF_MSG_DIFFERENT_COMPOUND] = "different compound page after locking",
- [MF_MSG_POISONED_HUGE] = "huge page already hardware poisoned",
- [MF_MSG_HUGE] = "huge page",
- [MF_MSG_FREE_HUGE] = "free huge page",
- [MF_MSG_UNMAP_FAILED] = "unmapping failed page",
- [MF_MSG_DIRTY_SWAPCACHE] = "dirty swapcache page",
- [MF_MSG_CLEAN_SWAPCACHE] = "clean swapcache page",
- [MF_MSG_DIRTY_MLOCKED_LRU] = "dirty mlocked LRU page",
- [MF_MSG_CLEAN_MLOCKED_LRU] = "clean mlocked LRU page",
- [MF_MSG_DIRTY_UNEVICTABLE_LRU] = "dirty unevictable LRU page",
- [MF_MSG_CLEAN_UNEVICTABLE_LRU] = "clean unevictable LRU page",
- [MF_MSG_DIRTY_LRU] = "dirty LRU page",
- [MF_MSG_CLEAN_LRU] = "clean LRU page",
- [MF_MSG_TRUNCATED_LRU] = "already truncated LRU page",
- [MF_MSG_BUDDY] = "free buddy page",
- [MF_MSG_BUDDY_2ND] = "free buddy page (2nd try)",
- [MF_MSG_UNKNOWN] = "unknown page",
- };
- static int delete_from_lru_cache(struct page *p)
- {
- if (!isolate_lru_page(p)) {
-
- ClearPageActive(p);
- ClearPageUnevictable(p);
-
- mem_cgroup_uncharge(p);
-
- put_page(p);
- return 0;
- }
- return -EIO;
- }
- static int me_kernel(struct page *p, unsigned long pfn)
- {
- return MF_IGNORED;
- }
- static int me_unknown(struct page *p, unsigned long pfn)
- {
- pr_err("Memory failure: %#lx: Unknown page state\n", pfn);
- return MF_FAILED;
- }
- static int me_pagecache_clean(struct page *p, unsigned long pfn)
- {
- int err;
- int ret = MF_FAILED;
- struct address_space *mapping;
- delete_from_lru_cache(p);
-
- if (PageAnon(p))
- return MF_RECOVERED;
-
- mapping = page_mapping(p);
- if (!mapping) {
-
- return MF_FAILED;
- }
-
- if (mapping->a_ops->error_remove_page) {
- err = mapping->a_ops->error_remove_page(mapping, p);
- if (err != 0) {
- pr_info("Memory failure: %#lx: Failed to punch page: %d\n",
- pfn, err);
- } else if (page_has_private(p) &&
- !try_to_release_page(p, GFP_NOIO)) {
- pr_info("Memory failure: %#lx: failed to release buffers\n",
- pfn);
- } else {
- ret = MF_RECOVERED;
- }
- } else {
-
- if (invalidate_inode_page(p))
- ret = MF_RECOVERED;
- else
- pr_info("Memory failure: %#lx: Failed to invalidate\n",
- pfn);
- }
- return ret;
- }
- static int me_pagecache_dirty(struct page *p, unsigned long pfn)
- {
- struct address_space *mapping = page_mapping(p);
- SetPageError(p);
-
- if (mapping) {
-
- mapping_set_error(mapping, EIO);
- }
- return me_pagecache_clean(p, pfn);
- }
- static int me_swapcache_dirty(struct page *p, unsigned long pfn)
- {
- ClearPageDirty(p);
-
- ClearPageUptodate(p);
- if (!delete_from_lru_cache(p))
- return MF_DELAYED;
- else
- return MF_FAILED;
- }
- static int me_swapcache_clean(struct page *p, unsigned long pfn)
- {
- delete_from_swap_cache(p);
- if (!delete_from_lru_cache(p))
- return MF_RECOVERED;
- else
- return MF_FAILED;
- }
- static int me_huge_page(struct page *p, unsigned long pfn)
- {
- int res = 0;
- struct page *hpage = compound_head(p);
- if (!PageHuge(hpage))
- return MF_DELAYED;
-
- if (!(page_mapping(hpage) || PageAnon(hpage))) {
- res = dequeue_hwpoisoned_huge_page(hpage);
- if (!res)
- return MF_RECOVERED;
- }
- return MF_DELAYED;
- }
- #define dirty (1UL << PG_dirty)
- #define sc (1UL << PG_swapcache)
- #define unevict (1UL << PG_unevictable)
- #define mlock (1UL << PG_mlocked)
- #define writeback (1UL << PG_writeback)
- #define lru (1UL << PG_lru)
- #define swapbacked (1UL << PG_swapbacked)
- #define head (1UL << PG_head)
- #define slab (1UL << PG_slab)
- #define reserved (1UL << PG_reserved)
- static struct page_state {
- unsigned long mask;
- unsigned long res;
- enum mf_action_page_type type;
- int (*action)(struct page *p, unsigned long pfn);
- } error_states[] = {
- { reserved, reserved, MF_MSG_KERNEL, me_kernel },
-
-
- { slab, slab, MF_MSG_SLAB, me_kernel },
- { head, head, MF_MSG_HUGE, me_huge_page },
- { sc|dirty, sc|dirty, MF_MSG_DIRTY_SWAPCACHE, me_swapcache_dirty },
- { sc|dirty, sc, MF_MSG_CLEAN_SWAPCACHE, me_swapcache_clean },
- { mlock|dirty, mlock|dirty, MF_MSG_DIRTY_MLOCKED_LRU, me_pagecache_dirty },
- { mlock|dirty, mlock, MF_MSG_CLEAN_MLOCKED_LRU, me_pagecache_clean },
- { unevict|dirty, unevict|dirty, MF_MSG_DIRTY_UNEVICTABLE_LRU, me_pagecache_dirty },
- { unevict|dirty, unevict, MF_MSG_CLEAN_UNEVICTABLE_LRU, me_pagecache_clean },
- { lru|dirty, lru|dirty, MF_MSG_DIRTY_LRU, me_pagecache_dirty },
- { lru|dirty, lru, MF_MSG_CLEAN_LRU, me_pagecache_clean },
-
- { 0, 0, MF_MSG_UNKNOWN, me_unknown },
- };
- #undef dirty
- #undef sc
- #undef unevict
- #undef mlock
- #undef writeback
- #undef lru
- #undef swapbacked
- #undef head
- #undef slab
- #undef reserved
- static void action_result(unsigned long pfn, enum mf_action_page_type type,
- enum mf_result result)
- {
- trace_memory_failure_event(pfn, type, result);
- pr_err("Memory failure: %#lx: recovery action for %s: %s\n",
- pfn, action_page_types[type], action_name[result]);
- }
- static int page_action(struct page_state *ps, struct page *p,
- unsigned long pfn)
- {
- int result;
- int count;
- result = ps->action(p, pfn);
- count = page_count(p) - 1;
- if (ps->action == me_swapcache_dirty && result == MF_DELAYED)
- count--;
- if (count != 0) {
- pr_err("Memory failure: %#lx: %s still referenced by %d users\n",
- pfn, action_page_types[ps->type], count);
- result = MF_FAILED;
- }
- action_result(pfn, ps->type, result);
-
-
- return (result == MF_RECOVERED || result == MF_DELAYED) ? 0 : -EBUSY;
- }
- int get_hwpoison_page(struct page *page)
- {
- struct page *head = compound_head(page);
- if (!PageHuge(head) && PageTransHuge(head)) {
-
- if (!PageAnon(head)) {
- pr_err("Memory failure: %#lx: non anonymous thp\n",
- page_to_pfn(page));
- return 0;
- }
- }
- if (get_page_unless_zero(head)) {
- if (head == compound_head(page))
- return 1;
- pr_info("Memory failure: %#lx cannot catch tail\n",
- page_to_pfn(page));
- put_page(head);
- }
- return 0;
- }
- EXPORT_SYMBOL_GPL(get_hwpoison_page);
- static int hwpoison_user_mappings(struct page *p, unsigned long pfn,
- int trapno, int flags, struct page **hpagep)
- {
- enum ttu_flags ttu = TTU_UNMAP | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
- struct address_space *mapping;
- LIST_HEAD(tokill);
- int ret;
- int kill = 1, forcekill;
- struct page *hpage = *hpagep;
- bool mlocked = PageMlocked(hpage);
-
- if (PageReserved(p) || PageSlab(p))
- return SWAP_SUCCESS;
- if (!(PageLRU(hpage) || PageHuge(p)))
- return SWAP_SUCCESS;
-
- if (!page_mapped(hpage))
- return SWAP_SUCCESS;
- if (PageKsm(p)) {
- pr_err("Memory failure: %#lx: can't handle KSM pages.\n", pfn);
- return SWAP_FAIL;
- }
- if (PageSwapCache(p)) {
- pr_err("Memory failure: %#lx: keeping poisoned page in swap cache\n",
- pfn);
- ttu |= TTU_IGNORE_HWPOISON;
- }
-
- mapping = page_mapping(hpage);
- if (!(flags & MF_MUST_KILL) && !PageDirty(hpage) && mapping &&
- mapping_cap_writeback_dirty(mapping)) {
- if (page_mkclean(hpage)) {
- SetPageDirty(hpage);
- } else {
- kill = 0;
- ttu |= TTU_IGNORE_HWPOISON;
- pr_info("Memory failure: %#lx: corrupted page was clean: dropped without side effects\n",
- pfn);
- }
- }
-
- if (kill)
- collect_procs(hpage, &tokill, flags & MF_ACTION_REQUIRED);
- ret = try_to_unmap(hpage, ttu);
- if (ret != SWAP_SUCCESS)
- pr_err("Memory failure: %#lx: failed to unmap page (mapcount=%d)\n",
- pfn, page_mapcount(hpage));
-
- if (mlocked)
- shake_page(hpage, 0);
-
- forcekill = PageDirty(hpage) || (flags & MF_MUST_KILL);
- kill_procs(&tokill, forcekill, trapno,
- ret != SWAP_SUCCESS, p, pfn, flags);
- return ret;
- }
- static void set_page_hwpoison_huge_page(struct page *hpage)
- {
- int i;
- int nr_pages = 1 << compound_order(hpage);
- for (i = 0; i < nr_pages; i++)
- SetPageHWPoison(hpage + i);
- }
- static void clear_page_hwpoison_huge_page(struct page *hpage)
- {
- int i;
- int nr_pages = 1 << compound_order(hpage);
- for (i = 0; i < nr_pages; i++)
- ClearPageHWPoison(hpage + i);
- }
- int memory_failure(unsigned long pfn, int trapno, int flags)
- {
- struct page_state *ps;
- struct page *p;
- struct page *hpage;
- struct page *orig_head;
- int res;
- unsigned int nr_pages;
- unsigned long page_flags;
- if (!sysctl_memory_failure_recovery)
- panic("Memory failure from trap %d on page %lx", trapno, pfn);
- if (!pfn_valid(pfn)) {
- pr_err("Memory failure: %#lx: memory outside kernel control\n",
- pfn);
- return -ENXIO;
- }
- p = pfn_to_page(pfn);
- orig_head = hpage = compound_head(p);
- if (TestSetPageHWPoison(p)) {
- pr_err("Memory failure: %#lx: already hardware poisoned\n",
- pfn);
- return 0;
- }
-
- if (PageHuge(p))
- nr_pages = 1 << compound_order(hpage);
- else
- nr_pages = 1;
- num_poisoned_pages_add(nr_pages);
-
- if (!(flags & MF_COUNT_INCREASED) && !get_hwpoison_page(p)) {
- if (is_free_buddy_page(p)) {
- action_result(pfn, MF_MSG_BUDDY, MF_DELAYED);
- return 0;
- } else if (PageHuge(hpage)) {
-
- lock_page(hpage);
- if (PageHWPoison(hpage)) {
- if ((hwpoison_filter(p) && TestClearPageHWPoison(p))
- || (p != hpage && TestSetPageHWPoison(hpage))) {
- num_poisoned_pages_sub(nr_pages);
- unlock_page(hpage);
- return 0;
- }
- }
- set_page_hwpoison_huge_page(hpage);
- res = dequeue_hwpoisoned_huge_page(hpage);
- action_result(pfn, MF_MSG_FREE_HUGE,
- res ? MF_IGNORED : MF_DELAYED);
- unlock_page(hpage);
- return res;
- } else {
- action_result(pfn, MF_MSG_KERNEL_HIGH_ORDER, MF_IGNORED);
- return -EBUSY;
- }
- }
- if (!PageHuge(p) && PageTransHuge(hpage)) {
- lock_page(p);
- if (!PageAnon(p) || unlikely(split_huge_page(p))) {
- unlock_page(p);
- if (!PageAnon(p))
- pr_err("Memory failure: %#lx: non anonymous thp\n",
- pfn);
- else
- pr_err("Memory failure: %#lx: thp split failed\n",
- pfn);
- if (TestClearPageHWPoison(p))
- num_poisoned_pages_sub(nr_pages);
- put_hwpoison_page(p);
- return -EBUSY;
- }
- unlock_page(p);
- VM_BUG_ON_PAGE(!page_count(p), p);
- hpage = compound_head(p);
- }
-
- if (!PageHuge(p)) {
- if (!PageLRU(p))
- shake_page(p, 0);
- if (!PageLRU(p)) {
-
- if (is_free_buddy_page(p)) {
- if (flags & MF_COUNT_INCREASED)
- action_result(pfn, MF_MSG_BUDDY, MF_DELAYED);
- else
- action_result(pfn, MF_MSG_BUDDY_2ND,
- MF_DELAYED);
- return 0;
- }
- }
- }
- lock_page(hpage);
-
- if (PageCompound(p) && compound_head(p) != orig_head) {
- action_result(pfn, MF_MSG_DIFFERENT_COMPOUND, MF_IGNORED);
- res = -EBUSY;
- goto out;
- }
-
- if (PageHuge(p))
- page_flags = hpage->flags;
- else
- page_flags = p->flags;
-
- if (!PageHWPoison(p)) {
- pr_err("Memory failure: %#lx: just unpoisoned\n", pfn);
- num_poisoned_pages_sub(nr_pages);
- unlock_page(hpage);
- put_hwpoison_page(hpage);
- return 0;
- }
- if (hwpoison_filter(p)) {
- if (TestClearPageHWPoison(p))
- num_poisoned_pages_sub(nr_pages);
- unlock_page(hpage);
- put_hwpoison_page(hpage);
- return 0;
- }
- if (!PageHuge(p) && !PageTransTail(p) && !PageLRU(p))
- goto identify_page_state;
-
- if (PageHuge(p) && PageTail(p) && TestSetPageHWPoison(hpage)) {
- action_result(pfn, MF_MSG_POISONED_HUGE, MF_IGNORED);
- unlock_page(hpage);
- put_hwpoison_page(hpage);
- return 0;
- }
-
- if (PageHuge(p))
- set_page_hwpoison_huge_page(hpage);
-
- wait_on_page_writeback(p);
-
- if (hwpoison_user_mappings(p, pfn, trapno, flags, &hpage)
- != SWAP_SUCCESS) {
- action_result(pfn, MF_MSG_UNMAP_FAILED, MF_IGNORED);
- res = -EBUSY;
- goto out;
- }
-
- if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
- action_result(pfn, MF_MSG_TRUNCATED_LRU, MF_IGNORED);
- res = -EBUSY;
- goto out;
- }
- identify_page_state:
- res = -EBUSY;
-
- for (ps = error_states;; ps++)
- if ((p->flags & ps->mask) == ps->res)
- break;
- page_flags |= (p->flags & (1UL << PG_dirty));
- if (!ps->mask)
- for (ps = error_states;; ps++)
- if ((page_flags & ps->mask) == ps->res)
- break;
- res = page_action(ps, p, pfn);
- out:
- unlock_page(hpage);
- return res;
- }
- EXPORT_SYMBOL_GPL(memory_failure);
- #define MEMORY_FAILURE_FIFO_ORDER 4
- #define MEMORY_FAILURE_FIFO_SIZE (1 << MEMORY_FAILURE_FIFO_ORDER)
- struct memory_failure_entry {
- unsigned long pfn;
- int trapno;
- int flags;
- };
- struct memory_failure_cpu {
- DECLARE_KFIFO(fifo, struct memory_failure_entry,
- MEMORY_FAILURE_FIFO_SIZE);
- spinlock_t lock;
- struct work_struct work;
- };
- static DEFINE_PER_CPU(struct memory_failure_cpu, memory_failure_cpu);
- void memory_failure_queue(unsigned long pfn, int trapno, int flags)
- {
- struct memory_failure_cpu *mf_cpu;
- unsigned long proc_flags;
- struct memory_failure_entry entry = {
- .pfn = pfn,
- .trapno = trapno,
- .flags = flags,
- };
- mf_cpu = &get_cpu_var(memory_failure_cpu);
- spin_lock_irqsave(&mf_cpu->lock, proc_flags);
- if (kfifo_put(&mf_cpu->fifo, entry))
- schedule_work_on(smp_processor_id(), &mf_cpu->work);
- else
- pr_err("Memory failure: buffer overflow when queuing memory failure at %#lx\n",
- pfn);
- spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
- put_cpu_var(memory_failure_cpu);
- }
- EXPORT_SYMBOL_GPL(memory_failure_queue);
- static void memory_failure_work_func(struct work_struct *work)
- {
- struct memory_failure_cpu *mf_cpu;
- struct memory_failure_entry entry = { 0, };
- unsigned long proc_flags;
- int gotten;
- mf_cpu = this_cpu_ptr(&memory_failure_cpu);
- for (;;) {
- spin_lock_irqsave(&mf_cpu->lock, proc_flags);
- gotten = kfifo_get(&mf_cpu->fifo, &entry);
- spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
- if (!gotten)
- break;
- if (entry.flags & MF_SOFT_OFFLINE)
- soft_offline_page(pfn_to_page(entry.pfn), entry.flags);
- else
- memory_failure(entry.pfn, entry.trapno, entry.flags);
- }
- }
- static int __init memory_failure_init(void)
- {
- struct memory_failure_cpu *mf_cpu;
- int cpu;
- for_each_possible_cpu(cpu) {
- mf_cpu = &per_cpu(memory_failure_cpu, cpu);
- spin_lock_init(&mf_cpu->lock);
- INIT_KFIFO(mf_cpu->fifo);
- INIT_WORK(&mf_cpu->work, memory_failure_work_func);
- }
- return 0;
- }
- core_initcall(memory_failure_init);
- #define unpoison_pr_info(fmt, pfn, rs) \
- ({ \
- if (__ratelimit(rs)) \
- pr_info(fmt, pfn); \
- })
- int unpoison_memory(unsigned long pfn)
- {
- struct page *page;
- struct page *p;
- int freeit = 0;
- unsigned int nr_pages;
- static DEFINE_RATELIMIT_STATE(unpoison_rs, DEFAULT_RATELIMIT_INTERVAL,
- DEFAULT_RATELIMIT_BURST);
- if (!pfn_valid(pfn))
- return -ENXIO;
- p = pfn_to_page(pfn);
- page = compound_head(p);
- if (!PageHWPoison(p)) {
- unpoison_pr_info("Unpoison: Page was already unpoisoned %#lx\n",
- pfn, &unpoison_rs);
- return 0;
- }
- if (page_count(page) > 1) {
- unpoison_pr_info("Unpoison: Someone grabs the hwpoison page %#lx\n",
- pfn, &unpoison_rs);
- return 0;
- }
- if (page_mapped(page)) {
- unpoison_pr_info("Unpoison: Someone maps the hwpoison page %#lx\n",
- pfn, &unpoison_rs);
- return 0;
- }
- if (page_mapping(page)) {
- unpoison_pr_info("Unpoison: the hwpoison page has non-NULL mapping %#lx\n",
- pfn, &unpoison_rs);
- return 0;
- }
-
- if (!PageHuge(page) && PageTransHuge(page)) {
- unpoison_pr_info("Unpoison: Memory failure is now running on %#lx\n",
- pfn, &unpoison_rs);
- return 0;
- }
- nr_pages = 1 << compound_order(page);
- if (!get_hwpoison_page(p)) {
-
- if (PageHuge(page)) {
- unpoison_pr_info("Unpoison: Memory failure is now running on free hugepage %#lx\n",
- pfn, &unpoison_rs);
- return 0;
- }
- if (TestClearPageHWPoison(p))
- num_poisoned_pages_dec();
- unpoison_pr_info("Unpoison: Software-unpoisoned free page %#lx\n",
- pfn, &unpoison_rs);
- return 0;
- }
- lock_page(page);
-
- if (TestClearPageHWPoison(page)) {
- unpoison_pr_info("Unpoison: Software-unpoisoned page %#lx\n",
- pfn, &unpoison_rs);
- num_poisoned_pages_sub(nr_pages);
- freeit = 1;
- if (PageHuge(page))
- clear_page_hwpoison_huge_page(page);
- }
- unlock_page(page);
- put_hwpoison_page(page);
- if (freeit && !(pfn == my_zero_pfn(0) && page_count(p) == 1))
- put_hwpoison_page(page);
- return 0;
- }
- EXPORT_SYMBOL(unpoison_memory);
- static struct page *new_page(struct page *p, unsigned long private, int **x)
- {
- int nid = page_to_nid(p);
- if (PageHuge(p))
- return alloc_huge_page_node(page_hstate(compound_head(p)),
- nid);
- else
- return __alloc_pages_node(nid, GFP_HIGHUSER_MOVABLE, 0);
- }
- static int __get_any_page(struct page *p, unsigned long pfn, int flags)
- {
- int ret;
- if (flags & MF_COUNT_INCREASED)
- return 1;
-
- if (!get_hwpoison_page(p)) {
- if (PageHuge(p)) {
- pr_info("%s: %#lx free huge page\n", __func__, pfn);
- ret = 0;
- } else if (is_free_buddy_page(p)) {
- pr_info("%s: %#lx free buddy page\n", __func__, pfn);
- ret = 0;
- } else {
- pr_info("%s: %#lx: unknown zero refcount page type %lx\n",
- __func__, pfn, p->flags);
- ret = -EIO;
- }
- } else {
-
- ret = 1;
- }
- return ret;
- }
- static int get_any_page(struct page *page, unsigned long pfn, int flags)
- {
- int ret = __get_any_page(page, pfn, flags);
- if (ret == 1 && !PageHuge(page) && !PageLRU(page)) {
-
- put_hwpoison_page(page);
- shake_page(page, 1);
-
- ret = __get_any_page(page, pfn, 0);
- if (ret == 1 && !PageLRU(page)) {
-
- put_hwpoison_page(page);
- pr_info("soft_offline: %#lx: unknown non LRU page type %lx\n",
- pfn, page->flags);
- return -EIO;
- }
- }
- return ret;
- }
- static int soft_offline_huge_page(struct page *page, int flags)
- {
- int ret;
- unsigned long pfn = page_to_pfn(page);
- struct page *hpage = compound_head(page);
- LIST_HEAD(pagelist);
-
- lock_page(hpage);
- if (PageHWPoison(hpage)) {
- unlock_page(hpage);
- put_hwpoison_page(hpage);
- pr_info("soft offline: %#lx hugepage already poisoned\n", pfn);
- return -EBUSY;
- }
- unlock_page(hpage);
- ret = isolate_huge_page(hpage, &pagelist);
-
- put_hwpoison_page(hpage);
- if (!ret) {
- pr_info("soft offline: %#lx hugepage failed to isolate\n", pfn);
- return -EBUSY;
- }
- ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
- MIGRATE_SYNC, MR_MEMORY_FAILURE);
- if (ret) {
- pr_info("soft offline: %#lx: migration failed %d, type %lx\n",
- pfn, ret, page->flags);
- if (!list_empty(&pagelist))
- putback_movable_pages(&pagelist);
- if (ret > 0)
- ret = -EIO;
- } else {
-
- if (PageHuge(page)) {
- set_page_hwpoison_huge_page(hpage);
- dequeue_hwpoisoned_huge_page(hpage);
- num_poisoned_pages_add(1 << compound_order(hpage));
- } else {
- SetPageHWPoison(page);
- num_poisoned_pages_inc();
- }
- }
- return ret;
- }
- static int __soft_offline_page(struct page *page, int flags)
- {
- int ret;
- unsigned long pfn = page_to_pfn(page);
-
- lock_page(page);
- wait_on_page_writeback(page);
- if (PageHWPoison(page)) {
- unlock_page(page);
- put_hwpoison_page(page);
- pr_info("soft offline: %#lx page already poisoned\n", pfn);
- return -EBUSY;
- }
-
- ret = invalidate_inode_page(page);
- unlock_page(page);
-
- if (ret == 1) {
- put_hwpoison_page(page);
- pr_info("soft_offline: %#lx: invalidated\n", pfn);
- SetPageHWPoison(page);
- num_poisoned_pages_inc();
- return 0;
- }
-
- ret = isolate_lru_page(page);
-
- put_hwpoison_page(page);
- if (!ret) {
- LIST_HEAD(pagelist);
- inc_node_page_state(page, NR_ISOLATED_ANON +
- page_is_file_cache(page));
- list_add(&page->lru, &pagelist);
- ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
- MIGRATE_SYNC, MR_MEMORY_FAILURE);
- if (ret) {
- if (!list_empty(&pagelist)) {
- list_del(&page->lru);
- dec_node_page_state(page, NR_ISOLATED_ANON +
- page_is_file_cache(page));
- putback_lru_page(page);
- }
- pr_info("soft offline: %#lx: migration failed %d, type %lx\n",
- pfn, ret, page->flags);
- if (ret > 0)
- ret = -EIO;
- }
- } else {
- pr_info("soft offline: %#lx: isolation failed: %d, page count %d, type %lx\n",
- pfn, ret, page_count(page), page->flags);
- }
- return ret;
- }
- static int soft_offline_in_use_page(struct page *page, int flags)
- {
- int ret;
- struct page *hpage = compound_head(page);
- if (!PageHuge(page) && PageTransHuge(hpage)) {
- lock_page(page);
- if (!PageAnon(page) || unlikely(split_huge_page(page))) {
- unlock_page(page);
- if (!PageAnon(page))
- pr_info("soft offline: %#lx: non anonymous thp\n", page_to_pfn(page));
- else
- pr_info("soft offline: %#lx: thp split failed\n", page_to_pfn(page));
- put_hwpoison_page(page);
- return -EBUSY;
- }
- unlock_page(page);
- }
- if (PageHuge(page))
- ret = soft_offline_huge_page(page, flags);
- else
- ret = __soft_offline_page(page, flags);
- return ret;
- }
- static void soft_offline_free_page(struct page *page)
- {
- if (PageHuge(page)) {
- struct page *hpage = compound_head(page);
- set_page_hwpoison_huge_page(hpage);
- if (!dequeue_hwpoisoned_huge_page(hpage))
- num_poisoned_pages_add(1 << compound_order(hpage));
- } else {
- if (!TestSetPageHWPoison(page))
- num_poisoned_pages_inc();
- }
- }
- int soft_offline_page(struct page *page, int flags)
- {
- int ret;
- unsigned long pfn = page_to_pfn(page);
- if (PageHWPoison(page)) {
- pr_info("soft offline: %#lx page already poisoned\n", pfn);
- if (flags & MF_COUNT_INCREASED)
- put_hwpoison_page(page);
- return -EBUSY;
- }
- get_online_mems();
- ret = get_any_page(page, pfn, flags);
- put_online_mems();
- if (ret > 0)
- ret = soft_offline_in_use_page(page, flags);
- else if (ret == 0)
- soft_offline_free_page(page);
- return ret;
- }
|