memory-failure.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. /*
  2. * Copyright (C) 2008, 2009 Intel Corporation
  3. * Authors: Andi Kleen, Fengguang Wu
  4. *
  5. * This software may be redistributed and/or modified under the terms of
  6. * the GNU General Public License ("GPL") version 2 only as published by the
  7. * Free Software Foundation.
  8. *
  9. * High level machine check handler. Handles pages reported by the
  10. * hardware as being corrupted usually due to a multi-bit ECC memory or cache
  11. * failure.
  12. *
  13. * In addition there is a "soft offline" entry point that allows stop using
  14. * not-yet-corrupted-by-suspicious pages without killing anything.
  15. *
  16. * Handles page cache pages in various states. The tricky part
  17. * here is that we can access any page asynchronously in respect to
  18. * other VM users, because memory failures could happen anytime and
  19. * anywhere. This could violate some of their assumptions. This is why
  20. * this code has to be extremely careful. Generally it tries to use
  21. * normal locking rules, as in get the standard locks, even if that means
  22. * the error handling takes potentially a long time.
  23. *
  24. * There are several operations here with exponential complexity because
  25. * of unsuitable VM data structures. For example the operation to map back
  26. * from RMAP chains to processes has to walk the complete process list and
  27. * has non linear complexity with the number. But since memory corruptions
  28. * are rare we hope to get away with this. This avoids impacting the core
  29. * VM.
  30. */
  31. /*
  32. * Notebook:
  33. * - hugetlb needs more code
  34. * - kcore/oldmem/vmcore/mem/kmem check for hwpoison pages
  35. * - pass bad pages to kdump next kernel
  36. */
  37. #include <linux/kernel.h>
  38. #include <linux/mm.h>
  39. #include <linux/page-flags.h>
  40. #include <linux/kernel-page-flags.h>
  41. #include <linux/sched.h>
  42. #include <linux/ksm.h>
  43. #include <linux/rmap.h>
  44. #include <linux/export.h>
  45. #include <linux/pagemap.h>
  46. #include <linux/swap.h>
  47. #include <linux/backing-dev.h>
  48. #include <linux/migrate.h>
  49. #include <linux/page-isolation.h>
  50. #include <linux/suspend.h>
  51. #include <linux/slab.h>
  52. #include <linux/swapops.h>
  53. #include <linux/hugetlb.h>
  54. #include <linux/memory_hotplug.h>
  55. #include <linux/mm_inline.h>
  56. #include <linux/kfifo.h>
  57. #include "internal.h"
  58. int sysctl_memory_failure_early_kill __read_mostly = 0;
  59. int sysctl_memory_failure_recovery __read_mostly = 1;
  60. atomic_long_t num_poisoned_pages __read_mostly = ATOMIC_LONG_INIT(0);
  61. #if defined(CONFIG_HWPOISON_INJECT) || defined(CONFIG_HWPOISON_INJECT_MODULE)
  62. u32 hwpoison_filter_enable = 0;
  63. u32 hwpoison_filter_dev_major = ~0U;
  64. u32 hwpoison_filter_dev_minor = ~0U;
  65. u64 hwpoison_filter_flags_mask;
  66. u64 hwpoison_filter_flags_value;
  67. EXPORT_SYMBOL_GPL(hwpoison_filter_enable);
  68. EXPORT_SYMBOL_GPL(hwpoison_filter_dev_major);
  69. EXPORT_SYMBOL_GPL(hwpoison_filter_dev_minor);
  70. EXPORT_SYMBOL_GPL(hwpoison_filter_flags_mask);
  71. EXPORT_SYMBOL_GPL(hwpoison_filter_flags_value);
  72. static int hwpoison_filter_dev(struct page *p)
  73. {
  74. struct address_space *mapping;
  75. dev_t dev;
  76. if (hwpoison_filter_dev_major == ~0U &&
  77. hwpoison_filter_dev_minor == ~0U)
  78. return 0;
  79. /*
  80. * page_mapping() does not accept slab pages.
  81. */
  82. if (PageSlab(p))
  83. return -EINVAL;
  84. mapping = page_mapping(p);
  85. if (mapping == NULL || mapping->host == NULL)
  86. return -EINVAL;
  87. dev = mapping->host->i_sb->s_dev;
  88. if (hwpoison_filter_dev_major != ~0U &&
  89. hwpoison_filter_dev_major != MAJOR(dev))
  90. return -EINVAL;
  91. if (hwpoison_filter_dev_minor != ~0U &&
  92. hwpoison_filter_dev_minor != MINOR(dev))
  93. return -EINVAL;
  94. return 0;
  95. }
  96. static int hwpoison_filter_flags(struct page *p)
  97. {
  98. if (!hwpoison_filter_flags_mask)
  99. return 0;
  100. if ((stable_page_flags(p) & hwpoison_filter_flags_mask) ==
  101. hwpoison_filter_flags_value)
  102. return 0;
  103. else
  104. return -EINVAL;
  105. }
  106. /*
  107. * This allows stress tests to limit test scope to a collection of tasks
  108. * by putting them under some memcg. This prevents killing unrelated/important
  109. * processes such as /sbin/init. Note that the target task may share clean
  110. * pages with init (eg. libc text), which is harmless. If the target task
  111. * share _dirty_ pages with another task B, the test scheme must make sure B
  112. * is also included in the memcg. At last, due to race conditions this filter
  113. * can only guarantee that the page either belongs to the memcg tasks, or is
  114. * a freed page.
  115. */
  116. #ifdef CONFIG_MEMCG_SWAP
  117. u64 hwpoison_filter_memcg;
  118. EXPORT_SYMBOL_GPL(hwpoison_filter_memcg);
  119. static int hwpoison_filter_task(struct page *p)
  120. {
  121. struct mem_cgroup *mem;
  122. struct cgroup_subsys_state *css;
  123. unsigned long ino;
  124. if (!hwpoison_filter_memcg)
  125. return 0;
  126. mem = try_get_mem_cgroup_from_page(p);
  127. if (!mem)
  128. return -EINVAL;
  129. css = mem_cgroup_css(mem);
  130. ino = cgroup_ino(css->cgroup);
  131. css_put(css);
  132. if (ino != hwpoison_filter_memcg)
  133. return -EINVAL;
  134. return 0;
  135. }
  136. #else
  137. static int hwpoison_filter_task(struct page *p) { return 0; }
  138. #endif
  139. int hwpoison_filter(struct page *p)
  140. {
  141. if (!hwpoison_filter_enable)
  142. return 0;
  143. if (hwpoison_filter_dev(p))
  144. return -EINVAL;
  145. if (hwpoison_filter_flags(p))
  146. return -EINVAL;
  147. if (hwpoison_filter_task(p))
  148. return -EINVAL;
  149. return 0;
  150. }
  151. #else
  152. int hwpoison_filter(struct page *p)
  153. {
  154. return 0;
  155. }
  156. #endif
  157. EXPORT_SYMBOL_GPL(hwpoison_filter);
  158. /*
  159. * Send all the processes who have the page mapped a signal.
  160. * ``action optional'' if they are not immediately affected by the error
  161. * ``action required'' if error happened in current execution context
  162. */
  163. static int kill_proc(struct task_struct *t, unsigned long addr, int trapno,
  164. unsigned long pfn, struct page *page, int flags)
  165. {
  166. struct siginfo si;
  167. int ret;
  168. printk(KERN_ERR
  169. "MCE %#lx: Killing %s:%d due to hardware memory corruption\n",
  170. pfn, t->comm, t->pid);
  171. si.si_signo = SIGBUS;
  172. si.si_errno = 0;
  173. si.si_addr = (void *)addr;
  174. #ifdef __ARCH_SI_TRAPNO
  175. si.si_trapno = trapno;
  176. #endif
  177. si.si_addr_lsb = compound_order(compound_head(page)) + PAGE_SHIFT;
  178. if ((flags & MF_ACTION_REQUIRED) && t->mm == current->mm) {
  179. si.si_code = BUS_MCEERR_AR;
  180. ret = force_sig_info(SIGBUS, &si, current);
  181. } else {
  182. /*
  183. * Don't use force here, it's convenient if the signal
  184. * can be temporarily blocked.
  185. * This could cause a loop when the user sets SIGBUS
  186. * to SIG_IGN, but hopefully no one will do that?
  187. */
  188. si.si_code = BUS_MCEERR_AO;
  189. ret = send_sig_info(SIGBUS, &si, t); /* synchronous? */
  190. }
  191. if (ret < 0)
  192. printk(KERN_INFO "MCE: Error sending signal to %s:%d: %d\n",
  193. t->comm, t->pid, ret);
  194. return ret;
  195. }
  196. /*
  197. * When a unknown page type is encountered drain as many buffers as possible
  198. * in the hope to turn the page into a LRU or free page, which we can handle.
  199. */
  200. void shake_page(struct page *p, int access)
  201. {
  202. if (!PageSlab(p)) {
  203. lru_add_drain_all();
  204. if (PageLRU(p))
  205. return;
  206. drain_all_pages(page_zone(p));
  207. if (PageLRU(p) || is_free_buddy_page(p))
  208. return;
  209. }
  210. /*
  211. * Only call shrink_node_slabs here (which would also shrink
  212. * other caches) if access is not potentially fatal.
  213. */
  214. if (access)
  215. drop_slab_node(page_to_nid(p));
  216. }
  217. EXPORT_SYMBOL_GPL(shake_page);
  218. /*
  219. * Kill all processes that have a poisoned page mapped and then isolate
  220. * the page.
  221. *
  222. * General strategy:
  223. * Find all processes having the page mapped and kill them.
  224. * But we keep a page reference around so that the page is not
  225. * actually freed yet.
  226. * Then stash the page away
  227. *
  228. * There's no convenient way to get back to mapped processes
  229. * from the VMAs. So do a brute-force search over all
  230. * running processes.
  231. *
  232. * Remember that machine checks are not common (or rather
  233. * if they are common you have other problems), so this shouldn't
  234. * be a performance issue.
  235. *
  236. * Also there are some races possible while we get from the
  237. * error detection to actually handle it.
  238. */
  239. struct to_kill {
  240. struct list_head nd;
  241. struct task_struct *tsk;
  242. unsigned long addr;
  243. char addr_valid;
  244. };
  245. /*
  246. * Failure handling: if we can't find or can't kill a process there's
  247. * not much we can do. We just print a message and ignore otherwise.
  248. */
  249. /*
  250. * Schedule a process for later kill.
  251. * Uses GFP_ATOMIC allocations to avoid potential recursions in the VM.
  252. * TBD would GFP_NOIO be enough?
  253. */
  254. static void add_to_kill(struct task_struct *tsk, struct page *p,
  255. struct vm_area_struct *vma,
  256. struct list_head *to_kill,
  257. struct to_kill **tkc)
  258. {
  259. struct to_kill *tk;
  260. if (*tkc) {
  261. tk = *tkc;
  262. *tkc = NULL;
  263. } else {
  264. tk = kmalloc(sizeof(struct to_kill), GFP_ATOMIC);
  265. if (!tk) {
  266. printk(KERN_ERR
  267. "MCE: Out of memory while machine check handling\n");
  268. return;
  269. }
  270. }
  271. tk->addr = page_address_in_vma(p, vma);
  272. tk->addr_valid = 1;
  273. /*
  274. * In theory we don't have to kill when the page was
  275. * munmaped. But it could be also a mremap. Since that's
  276. * likely very rare kill anyways just out of paranoia, but use
  277. * a SIGKILL because the error is not contained anymore.
  278. */
  279. if (tk->addr == -EFAULT) {
  280. pr_info("MCE: Unable to find user space address %lx in %s\n",
  281. page_to_pfn(p), tsk->comm);
  282. tk->addr_valid = 0;
  283. }
  284. get_task_struct(tsk);
  285. tk->tsk = tsk;
  286. list_add_tail(&tk->nd, to_kill);
  287. }
  288. /*
  289. * Kill the processes that have been collected earlier.
  290. *
  291. * Only do anything when DOIT is set, otherwise just free the list
  292. * (this is used for clean pages which do not need killing)
  293. * Also when FAIL is set do a force kill because something went
  294. * wrong earlier.
  295. */
  296. static void kill_procs(struct list_head *to_kill, int forcekill, int trapno,
  297. int fail, struct page *page, unsigned long pfn,
  298. int flags)
  299. {
  300. struct to_kill *tk, *next;
  301. list_for_each_entry_safe (tk, next, to_kill, nd) {
  302. if (forcekill) {
  303. /*
  304. * In case something went wrong with munmapping
  305. * make sure the process doesn't catch the
  306. * signal and then access the memory. Just kill it.
  307. */
  308. if (fail || tk->addr_valid == 0) {
  309. printk(KERN_ERR
  310. "MCE %#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
  311. pfn, tk->tsk->comm, tk->tsk->pid);
  312. force_sig(SIGKILL, tk->tsk);
  313. }
  314. /*
  315. * In theory the process could have mapped
  316. * something else on the address in-between. We could
  317. * check for that, but we need to tell the
  318. * process anyways.
  319. */
  320. else if (kill_proc(tk->tsk, tk->addr, trapno,
  321. pfn, page, flags) < 0)
  322. printk(KERN_ERR
  323. "MCE %#lx: Cannot send advisory machine check signal to %s:%d\n",
  324. pfn, tk->tsk->comm, tk->tsk->pid);
  325. }
  326. put_task_struct(tk->tsk);
  327. kfree(tk);
  328. }
  329. }
  330. /*
  331. * Find a dedicated thread which is supposed to handle SIGBUS(BUS_MCEERR_AO)
  332. * on behalf of the thread group. Return task_struct of the (first found)
  333. * dedicated thread if found, and return NULL otherwise.
  334. *
  335. * We already hold read_lock(&tasklist_lock) in the caller, so we don't
  336. * have to call rcu_read_lock/unlock() in this function.
  337. */
  338. static struct task_struct *find_early_kill_thread(struct task_struct *tsk)
  339. {
  340. struct task_struct *t;
  341. for_each_thread(tsk, t)
  342. if ((t->flags & PF_MCE_PROCESS) && (t->flags & PF_MCE_EARLY))
  343. return t;
  344. return NULL;
  345. }
  346. /*
  347. * Determine whether a given process is "early kill" process which expects
  348. * to be signaled when some page under the process is hwpoisoned.
  349. * Return task_struct of the dedicated thread (main thread unless explicitly
  350. * specified) if the process is "early kill," and otherwise returns NULL.
  351. */
  352. static struct task_struct *task_early_kill(struct task_struct *tsk,
  353. int force_early)
  354. {
  355. struct task_struct *t;
  356. if (!tsk->mm)
  357. return NULL;
  358. if (force_early)
  359. return tsk;
  360. t = find_early_kill_thread(tsk);
  361. if (t)
  362. return t;
  363. if (sysctl_memory_failure_early_kill)
  364. return tsk;
  365. return NULL;
  366. }
  367. /*
  368. * Collect processes when the error hit an anonymous page.
  369. */
  370. static void collect_procs_anon(struct page *page, struct list_head *to_kill,
  371. struct to_kill **tkc, int force_early)
  372. {
  373. struct vm_area_struct *vma;
  374. struct task_struct *tsk;
  375. struct anon_vma *av;
  376. pgoff_t pgoff;
  377. av = page_lock_anon_vma_read(page);
  378. if (av == NULL) /* Not actually mapped anymore */
  379. return;
  380. pgoff = page_to_pgoff(page);
  381. read_lock(&tasklist_lock);
  382. for_each_process (tsk) {
  383. struct anon_vma_chain *vmac;
  384. struct task_struct *t = task_early_kill(tsk, force_early);
  385. if (!t)
  386. continue;
  387. anon_vma_interval_tree_foreach(vmac, &av->rb_root,
  388. pgoff, pgoff) {
  389. vma = vmac->vma;
  390. if (!page_mapped_in_vma(page, vma))
  391. continue;
  392. if (vma->vm_mm == t->mm)
  393. add_to_kill(t, page, vma, to_kill, tkc);
  394. }
  395. }
  396. read_unlock(&tasklist_lock);
  397. page_unlock_anon_vma_read(av);
  398. }
  399. /*
  400. * Collect processes when the error hit a file mapped page.
  401. */
  402. static void collect_procs_file(struct page *page, struct list_head *to_kill,
  403. struct to_kill **tkc, int force_early)
  404. {
  405. struct vm_area_struct *vma;
  406. struct task_struct *tsk;
  407. struct address_space *mapping = page->mapping;
  408. i_mmap_lock_read(mapping);
  409. read_lock(&tasklist_lock);
  410. for_each_process(tsk) {
  411. pgoff_t pgoff = page_to_pgoff(page);
  412. struct task_struct *t = task_early_kill(tsk, force_early);
  413. if (!t)
  414. continue;
  415. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff,
  416. pgoff) {
  417. /*
  418. * Send early kill signal to tasks where a vma covers
  419. * the page but the corrupted page is not necessarily
  420. * mapped it in its pte.
  421. * Assume applications who requested early kill want
  422. * to be informed of all such data corruptions.
  423. */
  424. if (vma->vm_mm == t->mm)
  425. add_to_kill(t, page, vma, to_kill, tkc);
  426. }
  427. }
  428. read_unlock(&tasklist_lock);
  429. i_mmap_unlock_read(mapping);
  430. }
  431. /*
  432. * Collect the processes who have the corrupted page mapped to kill.
  433. * This is done in two steps for locking reasons.
  434. * First preallocate one tokill structure outside the spin locks,
  435. * so that we can kill at least one process reasonably reliable.
  436. */
  437. static void collect_procs(struct page *page, struct list_head *tokill,
  438. int force_early)
  439. {
  440. struct to_kill *tk;
  441. if (!page->mapping)
  442. return;
  443. tk = kmalloc(sizeof(struct to_kill), GFP_NOIO);
  444. if (!tk)
  445. return;
  446. if (PageAnon(page))
  447. collect_procs_anon(page, tokill, &tk, force_early);
  448. else
  449. collect_procs_file(page, tokill, &tk, force_early);
  450. kfree(tk);
  451. }
  452. /*
  453. * Error handlers for various types of pages.
  454. */
  455. enum outcome {
  456. IGNORED, /* Error: cannot be handled */
  457. FAILED, /* Error: handling failed */
  458. DELAYED, /* Will be handled later */
  459. RECOVERED, /* Successfully recovered */
  460. };
  461. static const char *action_name[] = {
  462. [IGNORED] = "Ignored",
  463. [FAILED] = "Failed",
  464. [DELAYED] = "Delayed",
  465. [RECOVERED] = "Recovered",
  466. };
  467. enum action_page_type {
  468. MSG_KERNEL,
  469. MSG_KERNEL_HIGH_ORDER,
  470. MSG_SLAB,
  471. MSG_DIFFERENT_COMPOUND,
  472. MSG_POISONED_HUGE,
  473. MSG_HUGE,
  474. MSG_FREE_HUGE,
  475. MSG_UNMAP_FAILED,
  476. MSG_DIRTY_SWAPCACHE,
  477. MSG_CLEAN_SWAPCACHE,
  478. MSG_DIRTY_MLOCKED_LRU,
  479. MSG_CLEAN_MLOCKED_LRU,
  480. MSG_DIRTY_UNEVICTABLE_LRU,
  481. MSG_CLEAN_UNEVICTABLE_LRU,
  482. MSG_DIRTY_LRU,
  483. MSG_CLEAN_LRU,
  484. MSG_TRUNCATED_LRU,
  485. MSG_BUDDY,
  486. MSG_BUDDY_2ND,
  487. MSG_UNKNOWN,
  488. };
  489. static const char * const action_page_types[] = {
  490. [MSG_KERNEL] = "reserved kernel page",
  491. [MSG_KERNEL_HIGH_ORDER] = "high-order kernel page",
  492. [MSG_SLAB] = "kernel slab page",
  493. [MSG_DIFFERENT_COMPOUND] = "different compound page after locking",
  494. [MSG_POISONED_HUGE] = "huge page already hardware poisoned",
  495. [MSG_HUGE] = "huge page",
  496. [MSG_FREE_HUGE] = "free huge page",
  497. [MSG_UNMAP_FAILED] = "unmapping failed page",
  498. [MSG_DIRTY_SWAPCACHE] = "dirty swapcache page",
  499. [MSG_CLEAN_SWAPCACHE] = "clean swapcache page",
  500. [MSG_DIRTY_MLOCKED_LRU] = "dirty mlocked LRU page",
  501. [MSG_CLEAN_MLOCKED_LRU] = "clean mlocked LRU page",
  502. [MSG_DIRTY_UNEVICTABLE_LRU] = "dirty unevictable LRU page",
  503. [MSG_CLEAN_UNEVICTABLE_LRU] = "clean unevictable LRU page",
  504. [MSG_DIRTY_LRU] = "dirty LRU page",
  505. [MSG_CLEAN_LRU] = "clean LRU page",
  506. [MSG_TRUNCATED_LRU] = "already truncated LRU page",
  507. [MSG_BUDDY] = "free buddy page",
  508. [MSG_BUDDY_2ND] = "free buddy page (2nd try)",
  509. [MSG_UNKNOWN] = "unknown page",
  510. };
  511. /*
  512. * XXX: It is possible that a page is isolated from LRU cache,
  513. * and then kept in swap cache or failed to remove from page cache.
  514. * The page count will stop it from being freed by unpoison.
  515. * Stress tests should be aware of this memory leak problem.
  516. */
  517. static int delete_from_lru_cache(struct page *p)
  518. {
  519. if (!isolate_lru_page(p)) {
  520. /*
  521. * Clear sensible page flags, so that the buddy system won't
  522. * complain when the page is unpoison-and-freed.
  523. */
  524. ClearPageActive(p);
  525. ClearPageUnevictable(p);
  526. /*
  527. * drop the page count elevated by isolate_lru_page()
  528. */
  529. page_cache_release(p);
  530. return 0;
  531. }
  532. return -EIO;
  533. }
  534. /*
  535. * Error hit kernel page.
  536. * Do nothing, try to be lucky and not touch this instead. For a few cases we
  537. * could be more sophisticated.
  538. */
  539. static int me_kernel(struct page *p, unsigned long pfn)
  540. {
  541. return IGNORED;
  542. }
  543. /*
  544. * Page in unknown state. Do nothing.
  545. */
  546. static int me_unknown(struct page *p, unsigned long pfn)
  547. {
  548. printk(KERN_ERR "MCE %#lx: Unknown page state\n", pfn);
  549. return FAILED;
  550. }
  551. /*
  552. * Clean (or cleaned) page cache page.
  553. */
  554. static int me_pagecache_clean(struct page *p, unsigned long pfn)
  555. {
  556. int err;
  557. int ret = FAILED;
  558. struct address_space *mapping;
  559. delete_from_lru_cache(p);
  560. /*
  561. * For anonymous pages we're done the only reference left
  562. * should be the one m_f() holds.
  563. */
  564. if (PageAnon(p))
  565. return RECOVERED;
  566. /*
  567. * Now truncate the page in the page cache. This is really
  568. * more like a "temporary hole punch"
  569. * Don't do this for block devices when someone else
  570. * has a reference, because it could be file system metadata
  571. * and that's not safe to truncate.
  572. */
  573. mapping = page_mapping(p);
  574. if (!mapping) {
  575. /*
  576. * Page has been teared down in the meanwhile
  577. */
  578. return FAILED;
  579. }
  580. /*
  581. * Truncation is a bit tricky. Enable it per file system for now.
  582. *
  583. * Open: to take i_mutex or not for this? Right now we don't.
  584. */
  585. if (mapping->a_ops->error_remove_page) {
  586. err = mapping->a_ops->error_remove_page(mapping, p);
  587. if (err != 0) {
  588. printk(KERN_INFO "MCE %#lx: Failed to punch page: %d\n",
  589. pfn, err);
  590. } else if (page_has_private(p) &&
  591. !try_to_release_page(p, GFP_NOIO)) {
  592. pr_info("MCE %#lx: failed to release buffers\n", pfn);
  593. } else {
  594. ret = RECOVERED;
  595. }
  596. } else {
  597. /*
  598. * If the file system doesn't support it just invalidate
  599. * This fails on dirty or anything with private pages
  600. */
  601. if (invalidate_inode_page(p))
  602. ret = RECOVERED;
  603. else
  604. printk(KERN_INFO "MCE %#lx: Failed to invalidate\n",
  605. pfn);
  606. }
  607. return ret;
  608. }
  609. /*
  610. * Dirty pagecache page
  611. * Issues: when the error hit a hole page the error is not properly
  612. * propagated.
  613. */
  614. static int me_pagecache_dirty(struct page *p, unsigned long pfn)
  615. {
  616. struct address_space *mapping = page_mapping(p);
  617. SetPageError(p);
  618. /* TBD: print more information about the file. */
  619. if (mapping) {
  620. /*
  621. * IO error will be reported by write(), fsync(), etc.
  622. * who check the mapping.
  623. * This way the application knows that something went
  624. * wrong with its dirty file data.
  625. *
  626. * There's one open issue:
  627. *
  628. * The EIO will be only reported on the next IO
  629. * operation and then cleared through the IO map.
  630. * Normally Linux has two mechanisms to pass IO error
  631. * first through the AS_EIO flag in the address space
  632. * and then through the PageError flag in the page.
  633. * Since we drop pages on memory failure handling the
  634. * only mechanism open to use is through AS_AIO.
  635. *
  636. * This has the disadvantage that it gets cleared on
  637. * the first operation that returns an error, while
  638. * the PageError bit is more sticky and only cleared
  639. * when the page is reread or dropped. If an
  640. * application assumes it will always get error on
  641. * fsync, but does other operations on the fd before
  642. * and the page is dropped between then the error
  643. * will not be properly reported.
  644. *
  645. * This can already happen even without hwpoisoned
  646. * pages: first on metadata IO errors (which only
  647. * report through AS_EIO) or when the page is dropped
  648. * at the wrong time.
  649. *
  650. * So right now we assume that the application DTRT on
  651. * the first EIO, but we're not worse than other parts
  652. * of the kernel.
  653. */
  654. mapping_set_error(mapping, EIO);
  655. }
  656. return me_pagecache_clean(p, pfn);
  657. }
  658. /*
  659. * Clean and dirty swap cache.
  660. *
  661. * Dirty swap cache page is tricky to handle. The page could live both in page
  662. * cache and swap cache(ie. page is freshly swapped in). So it could be
  663. * referenced concurrently by 2 types of PTEs:
  664. * normal PTEs and swap PTEs. We try to handle them consistently by calling
  665. * try_to_unmap(TTU_IGNORE_HWPOISON) to convert the normal PTEs to swap PTEs,
  666. * and then
  667. * - clear dirty bit to prevent IO
  668. * - remove from LRU
  669. * - but keep in the swap cache, so that when we return to it on
  670. * a later page fault, we know the application is accessing
  671. * corrupted data and shall be killed (we installed simple
  672. * interception code in do_swap_page to catch it).
  673. *
  674. * Clean swap cache pages can be directly isolated. A later page fault will
  675. * bring in the known good data from disk.
  676. */
  677. static int me_swapcache_dirty(struct page *p, unsigned long pfn)
  678. {
  679. ClearPageDirty(p);
  680. /* Trigger EIO in shmem: */
  681. ClearPageUptodate(p);
  682. if (!delete_from_lru_cache(p))
  683. return DELAYED;
  684. else
  685. return FAILED;
  686. }
  687. static int me_swapcache_clean(struct page *p, unsigned long pfn)
  688. {
  689. delete_from_swap_cache(p);
  690. if (!delete_from_lru_cache(p))
  691. return RECOVERED;
  692. else
  693. return FAILED;
  694. }
  695. /*
  696. * Huge pages. Needs work.
  697. * Issues:
  698. * - Error on hugepage is contained in hugepage unit (not in raw page unit.)
  699. * To narrow down kill region to one page, we need to break up pmd.
  700. */
  701. static int me_huge_page(struct page *p, unsigned long pfn)
  702. {
  703. int res = 0;
  704. struct page *hpage = compound_head(p);
  705. /*
  706. * We can safely recover from error on free or reserved (i.e.
  707. * not in-use) hugepage by dequeuing it from freelist.
  708. * To check whether a hugepage is in-use or not, we can't use
  709. * page->lru because it can be used in other hugepage operations,
  710. * such as __unmap_hugepage_range() and gather_surplus_pages().
  711. * So instead we use page_mapping() and PageAnon().
  712. * We assume that this function is called with page lock held,
  713. * so there is no race between isolation and mapping/unmapping.
  714. */
  715. if (!(page_mapping(hpage) || PageAnon(hpage))) {
  716. res = dequeue_hwpoisoned_huge_page(hpage);
  717. if (!res)
  718. return RECOVERED;
  719. }
  720. return DELAYED;
  721. }
  722. /*
  723. * Various page states we can handle.
  724. *
  725. * A page state is defined by its current page->flags bits.
  726. * The table matches them in order and calls the right handler.
  727. *
  728. * This is quite tricky because we can access page at any time
  729. * in its live cycle, so all accesses have to be extremely careful.
  730. *
  731. * This is not complete. More states could be added.
  732. * For any missing state don't attempt recovery.
  733. */
  734. #define dirty (1UL << PG_dirty)
  735. #define sc (1UL << PG_swapcache)
  736. #define unevict (1UL << PG_unevictable)
  737. #define mlock (1UL << PG_mlocked)
  738. #define writeback (1UL << PG_writeback)
  739. #define lru (1UL << PG_lru)
  740. #define swapbacked (1UL << PG_swapbacked)
  741. #define head (1UL << PG_head)
  742. #define tail (1UL << PG_tail)
  743. #define compound (1UL << PG_compound)
  744. #define slab (1UL << PG_slab)
  745. #define reserved (1UL << PG_reserved)
  746. static struct page_state {
  747. unsigned long mask;
  748. unsigned long res;
  749. enum action_page_type type;
  750. int (*action)(struct page *p, unsigned long pfn);
  751. } error_states[] = {
  752. { reserved, reserved, MSG_KERNEL, me_kernel },
  753. /*
  754. * free pages are specially detected outside this table:
  755. * PG_buddy pages only make a small fraction of all free pages.
  756. */
  757. /*
  758. * Could in theory check if slab page is free or if we can drop
  759. * currently unused objects without touching them. But just
  760. * treat it as standard kernel for now.
  761. */
  762. { slab, slab, MSG_SLAB, me_kernel },
  763. #ifdef CONFIG_PAGEFLAGS_EXTENDED
  764. { head, head, MSG_HUGE, me_huge_page },
  765. { tail, tail, MSG_HUGE, me_huge_page },
  766. #else
  767. { compound, compound, MSG_HUGE, me_huge_page },
  768. #endif
  769. { sc|dirty, sc|dirty, MSG_DIRTY_SWAPCACHE, me_swapcache_dirty },
  770. { sc|dirty, sc, MSG_CLEAN_SWAPCACHE, me_swapcache_clean },
  771. { mlock|dirty, mlock|dirty, MSG_DIRTY_MLOCKED_LRU, me_pagecache_dirty },
  772. { mlock|dirty, mlock, MSG_CLEAN_MLOCKED_LRU, me_pagecache_clean },
  773. { unevict|dirty, unevict|dirty, MSG_DIRTY_UNEVICTABLE_LRU, me_pagecache_dirty },
  774. { unevict|dirty, unevict, MSG_CLEAN_UNEVICTABLE_LRU, me_pagecache_clean },
  775. { lru|dirty, lru|dirty, MSG_DIRTY_LRU, me_pagecache_dirty },
  776. { lru|dirty, lru, MSG_CLEAN_LRU, me_pagecache_clean },
  777. /*
  778. * Catchall entry: must be at end.
  779. */
  780. { 0, 0, MSG_UNKNOWN, me_unknown },
  781. };
  782. #undef dirty
  783. #undef sc
  784. #undef unevict
  785. #undef mlock
  786. #undef writeback
  787. #undef lru
  788. #undef swapbacked
  789. #undef head
  790. #undef tail
  791. #undef compound
  792. #undef slab
  793. #undef reserved
  794. /*
  795. * "Dirty/Clean" indication is not 100% accurate due to the possibility of
  796. * setting PG_dirty outside page lock. See also comment above set_page_dirty().
  797. */
  798. static void action_result(unsigned long pfn, enum action_page_type type, int result)
  799. {
  800. pr_err("MCE %#lx: recovery action for %s: %s\n",
  801. pfn, action_page_types[type], action_name[result]);
  802. }
  803. static int page_action(struct page_state *ps, struct page *p,
  804. unsigned long pfn)
  805. {
  806. int result;
  807. int count;
  808. result = ps->action(p, pfn);
  809. count = page_count(p) - 1;
  810. if (ps->action == me_swapcache_dirty && result == DELAYED)
  811. count--;
  812. if (count != 0) {
  813. printk(KERN_ERR
  814. "MCE %#lx: %s still referenced by %d users\n",
  815. pfn, action_page_types[ps->type], count);
  816. result = FAILED;
  817. }
  818. action_result(pfn, ps->type, result);
  819. /* Could do more checks here if page looks ok */
  820. /*
  821. * Could adjust zone counters here to correct for the missing page.
  822. */
  823. return (result == RECOVERED || result == DELAYED) ? 0 : -EBUSY;
  824. }
  825. /*
  826. * Do all that is necessary to remove user space mappings. Unmap
  827. * the pages and send SIGBUS to the processes if the data was dirty.
  828. */
  829. static int hwpoison_user_mappings(struct page *p, unsigned long pfn,
  830. int trapno, int flags, struct page **hpagep)
  831. {
  832. enum ttu_flags ttu = TTU_UNMAP | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
  833. struct address_space *mapping;
  834. LIST_HEAD(tokill);
  835. int ret;
  836. int kill = 1, forcekill;
  837. struct page *hpage = *hpagep;
  838. struct page *ppage;
  839. /*
  840. * Here we are interested only in user-mapped pages, so skip any
  841. * other types of pages.
  842. */
  843. if (PageReserved(p) || PageSlab(p))
  844. return SWAP_SUCCESS;
  845. if (!(PageLRU(hpage) || PageHuge(p)))
  846. return SWAP_SUCCESS;
  847. /*
  848. * This check implies we don't kill processes if their pages
  849. * are in the swap cache early. Those are always late kills.
  850. */
  851. if (!page_mapped(hpage))
  852. return SWAP_SUCCESS;
  853. if (PageKsm(p)) {
  854. pr_err("MCE %#lx: can't handle KSM pages.\n", pfn);
  855. return SWAP_FAIL;
  856. }
  857. if (PageSwapCache(p)) {
  858. printk(KERN_ERR
  859. "MCE %#lx: keeping poisoned page in swap cache\n", pfn);
  860. ttu |= TTU_IGNORE_HWPOISON;
  861. }
  862. /*
  863. * Propagate the dirty bit from PTEs to struct page first, because we
  864. * need this to decide if we should kill or just drop the page.
  865. * XXX: the dirty test could be racy: set_page_dirty() may not always
  866. * be called inside page lock (it's recommended but not enforced).
  867. */
  868. mapping = page_mapping(hpage);
  869. if (!(flags & MF_MUST_KILL) && !PageDirty(hpage) && mapping &&
  870. mapping_cap_writeback_dirty(mapping)) {
  871. if (page_mkclean(hpage)) {
  872. SetPageDirty(hpage);
  873. } else {
  874. kill = 0;
  875. ttu |= TTU_IGNORE_HWPOISON;
  876. printk(KERN_INFO
  877. "MCE %#lx: corrupted page was clean: dropped without side effects\n",
  878. pfn);
  879. }
  880. }
  881. /*
  882. * ppage: poisoned page
  883. * if p is regular page(4k page)
  884. * ppage == real poisoned page;
  885. * else p is hugetlb or THP, ppage == head page.
  886. */
  887. ppage = hpage;
  888. if (PageTransHuge(hpage)) {
  889. /*
  890. * Verify that this isn't a hugetlbfs head page, the check for
  891. * PageAnon is just for avoid tripping a split_huge_page
  892. * internal debug check, as split_huge_page refuses to deal with
  893. * anything that isn't an anon page. PageAnon can't go away fro
  894. * under us because we hold a refcount on the hpage, without a
  895. * refcount on the hpage. split_huge_page can't be safely called
  896. * in the first place, having a refcount on the tail isn't
  897. * enough * to be safe.
  898. */
  899. if (!PageHuge(hpage) && PageAnon(hpage)) {
  900. if (unlikely(split_huge_page(hpage))) {
  901. /*
  902. * FIXME: if splitting THP is failed, it is
  903. * better to stop the following operation rather
  904. * than causing panic by unmapping. System might
  905. * survive if the page is freed later.
  906. */
  907. printk(KERN_INFO
  908. "MCE %#lx: failed to split THP\n", pfn);
  909. BUG_ON(!PageHWPoison(p));
  910. return SWAP_FAIL;
  911. }
  912. /*
  913. * We pinned the head page for hwpoison handling,
  914. * now we split the thp and we are interested in
  915. * the hwpoisoned raw page, so move the refcount
  916. * to it. Similarly, page lock is shifted.
  917. */
  918. if (hpage != p) {
  919. if (!(flags & MF_COUNT_INCREASED)) {
  920. put_page(hpage);
  921. get_page(p);
  922. }
  923. lock_page(p);
  924. unlock_page(hpage);
  925. *hpagep = p;
  926. }
  927. /* THP is split, so ppage should be the real poisoned page. */
  928. ppage = p;
  929. }
  930. }
  931. /*
  932. * First collect all the processes that have the page
  933. * mapped in dirty form. This has to be done before try_to_unmap,
  934. * because ttu takes the rmap data structures down.
  935. *
  936. * Error handling: We ignore errors here because
  937. * there's nothing that can be done.
  938. */
  939. if (kill)
  940. collect_procs(ppage, &tokill, flags & MF_ACTION_REQUIRED);
  941. ret = try_to_unmap(ppage, ttu);
  942. if (ret != SWAP_SUCCESS)
  943. printk(KERN_ERR "MCE %#lx: failed to unmap page (mapcount=%d)\n",
  944. pfn, page_mapcount(ppage));
  945. /*
  946. * Now that the dirty bit has been propagated to the
  947. * struct page and all unmaps done we can decide if
  948. * killing is needed or not. Only kill when the page
  949. * was dirty or the process is not restartable,
  950. * otherwise the tokill list is merely
  951. * freed. When there was a problem unmapping earlier
  952. * use a more force-full uncatchable kill to prevent
  953. * any accesses to the poisoned memory.
  954. */
  955. forcekill = PageDirty(ppage) || (flags & MF_MUST_KILL);
  956. kill_procs(&tokill, forcekill, trapno,
  957. ret != SWAP_SUCCESS, p, pfn, flags);
  958. return ret;
  959. }
  960. static void set_page_hwpoison_huge_page(struct page *hpage)
  961. {
  962. int i;
  963. int nr_pages = 1 << compound_order(hpage);
  964. for (i = 0; i < nr_pages; i++)
  965. SetPageHWPoison(hpage + i);
  966. }
  967. static void clear_page_hwpoison_huge_page(struct page *hpage)
  968. {
  969. int i;
  970. int nr_pages = 1 << compound_order(hpage);
  971. for (i = 0; i < nr_pages; i++)
  972. ClearPageHWPoison(hpage + i);
  973. }
  974. /**
  975. * memory_failure - Handle memory failure of a page.
  976. * @pfn: Page Number of the corrupted page
  977. * @trapno: Trap number reported in the signal to user space.
  978. * @flags: fine tune action taken
  979. *
  980. * This function is called by the low level machine check code
  981. * of an architecture when it detects hardware memory corruption
  982. * of a page. It tries its best to recover, which includes
  983. * dropping pages, killing processes etc.
  984. *
  985. * The function is primarily of use for corruptions that
  986. * happen outside the current execution context (e.g. when
  987. * detected by a background scrubber)
  988. *
  989. * Must run in process context (e.g. a work queue) with interrupts
  990. * enabled and no spinlocks hold.
  991. */
  992. int memory_failure(unsigned long pfn, int trapno, int flags)
  993. {
  994. struct page_state *ps;
  995. struct page *p;
  996. struct page *hpage;
  997. int res;
  998. unsigned int nr_pages;
  999. unsigned long page_flags;
  1000. if (!sysctl_memory_failure_recovery)
  1001. panic("Memory failure from trap %d on page %lx", trapno, pfn);
  1002. if (!pfn_valid(pfn)) {
  1003. printk(KERN_ERR
  1004. "MCE %#lx: memory outside kernel control\n",
  1005. pfn);
  1006. return -ENXIO;
  1007. }
  1008. p = pfn_to_page(pfn);
  1009. hpage = compound_head(p);
  1010. if (TestSetPageHWPoison(p)) {
  1011. printk(KERN_ERR "MCE %#lx: already hardware poisoned\n", pfn);
  1012. return 0;
  1013. }
  1014. /*
  1015. * Currently errors on hugetlbfs pages are measured in hugepage units,
  1016. * so nr_pages should be 1 << compound_order. OTOH when errors are on
  1017. * transparent hugepages, they are supposed to be split and error
  1018. * measurement is done in normal page units. So nr_pages should be one
  1019. * in this case.
  1020. */
  1021. if (PageHuge(p))
  1022. nr_pages = 1 << compound_order(hpage);
  1023. else /* normal page or thp */
  1024. nr_pages = 1;
  1025. atomic_long_add(nr_pages, &num_poisoned_pages);
  1026. /*
  1027. * We need/can do nothing about count=0 pages.
  1028. * 1) it's a free page, and therefore in safe hand:
  1029. * prep_new_page() will be the gate keeper.
  1030. * 2) it's a free hugepage, which is also safe:
  1031. * an affected hugepage will be dequeued from hugepage freelist,
  1032. * so there's no concern about reusing it ever after.
  1033. * 3) it's part of a non-compound high order page.
  1034. * Implies some kernel user: cannot stop them from
  1035. * R/W the page; let's pray that the page has been
  1036. * used and will be freed some time later.
  1037. * In fact it's dangerous to directly bump up page count from 0,
  1038. * that may make page_freeze_refs()/page_unfreeze_refs() mismatch.
  1039. */
  1040. if (!(flags & MF_COUNT_INCREASED) &&
  1041. !get_page_unless_zero(hpage)) {
  1042. if (is_free_buddy_page(p)) {
  1043. action_result(pfn, MSG_BUDDY, DELAYED);
  1044. return 0;
  1045. } else if (PageHuge(hpage)) {
  1046. /*
  1047. * Check "filter hit" and "race with other subpage."
  1048. */
  1049. lock_page(hpage);
  1050. if (PageHWPoison(hpage)) {
  1051. if ((hwpoison_filter(p) && TestClearPageHWPoison(p))
  1052. || (p != hpage && TestSetPageHWPoison(hpage))) {
  1053. atomic_long_sub(nr_pages, &num_poisoned_pages);
  1054. unlock_page(hpage);
  1055. return 0;
  1056. }
  1057. }
  1058. set_page_hwpoison_huge_page(hpage);
  1059. res = dequeue_hwpoisoned_huge_page(hpage);
  1060. action_result(pfn, MSG_FREE_HUGE,
  1061. res ? IGNORED : DELAYED);
  1062. unlock_page(hpage);
  1063. return res;
  1064. } else {
  1065. action_result(pfn, MSG_KERNEL_HIGH_ORDER, IGNORED);
  1066. return -EBUSY;
  1067. }
  1068. }
  1069. /*
  1070. * We ignore non-LRU pages for good reasons.
  1071. * - PG_locked is only well defined for LRU pages and a few others
  1072. * - to avoid races with __set_page_locked()
  1073. * - to avoid races with __SetPageSlab*() (and more non-atomic ops)
  1074. * The check (unnecessarily) ignores LRU pages being isolated and
  1075. * walked by the page reclaim code, however that's not a big loss.
  1076. */
  1077. if (!PageHuge(p)) {
  1078. if (!PageLRU(hpage))
  1079. shake_page(hpage, 0);
  1080. if (!PageLRU(hpage)) {
  1081. /*
  1082. * shake_page could have turned it free.
  1083. */
  1084. if (is_free_buddy_page(p)) {
  1085. if (flags & MF_COUNT_INCREASED)
  1086. action_result(pfn, MSG_BUDDY, DELAYED);
  1087. else
  1088. action_result(pfn, MSG_BUDDY_2ND,
  1089. DELAYED);
  1090. return 0;
  1091. }
  1092. }
  1093. }
  1094. lock_page(hpage);
  1095. /*
  1096. * The page could have changed compound pages during the locking.
  1097. * If this happens just bail out.
  1098. */
  1099. if (compound_head(p) != hpage) {
  1100. action_result(pfn, MSG_DIFFERENT_COMPOUND, IGNORED);
  1101. res = -EBUSY;
  1102. goto out;
  1103. }
  1104. /*
  1105. * We use page flags to determine what action should be taken, but
  1106. * the flags can be modified by the error containment action. One
  1107. * example is an mlocked page, where PG_mlocked is cleared by
  1108. * page_remove_rmap() in try_to_unmap_one(). So to determine page status
  1109. * correctly, we save a copy of the page flags at this time.
  1110. */
  1111. page_flags = p->flags;
  1112. /*
  1113. * unpoison always clear PG_hwpoison inside page lock
  1114. */
  1115. if (!PageHWPoison(p)) {
  1116. printk(KERN_ERR "MCE %#lx: just unpoisoned\n", pfn);
  1117. atomic_long_sub(nr_pages, &num_poisoned_pages);
  1118. put_page(hpage);
  1119. res = 0;
  1120. goto out;
  1121. }
  1122. if (hwpoison_filter(p)) {
  1123. if (TestClearPageHWPoison(p))
  1124. atomic_long_sub(nr_pages, &num_poisoned_pages);
  1125. unlock_page(hpage);
  1126. put_page(hpage);
  1127. return 0;
  1128. }
  1129. if (!PageHuge(p) && !PageTransTail(p) && !PageLRU(p))
  1130. goto identify_page_state;
  1131. /*
  1132. * For error on the tail page, we should set PG_hwpoison
  1133. * on the head page to show that the hugepage is hwpoisoned
  1134. */
  1135. if (PageHuge(p) && PageTail(p) && TestSetPageHWPoison(hpage)) {
  1136. action_result(pfn, MSG_POISONED_HUGE, IGNORED);
  1137. unlock_page(hpage);
  1138. put_page(hpage);
  1139. return 0;
  1140. }
  1141. /*
  1142. * Set PG_hwpoison on all pages in an error hugepage,
  1143. * because containment is done in hugepage unit for now.
  1144. * Since we have done TestSetPageHWPoison() for the head page with
  1145. * page lock held, we can safely set PG_hwpoison bits on tail pages.
  1146. */
  1147. if (PageHuge(p))
  1148. set_page_hwpoison_huge_page(hpage);
  1149. /*
  1150. * It's very difficult to mess with pages currently under IO
  1151. * and in many cases impossible, so we just avoid it here.
  1152. */
  1153. wait_on_page_writeback(p);
  1154. /*
  1155. * Now take care of user space mappings.
  1156. * Abort on fail: __delete_from_page_cache() assumes unmapped page.
  1157. *
  1158. * When the raw error page is thp tail page, hpage points to the raw
  1159. * page after thp split.
  1160. */
  1161. if (hwpoison_user_mappings(p, pfn, trapno, flags, &hpage)
  1162. != SWAP_SUCCESS) {
  1163. action_result(pfn, MSG_UNMAP_FAILED, IGNORED);
  1164. res = -EBUSY;
  1165. goto out;
  1166. }
  1167. /*
  1168. * Torn down by someone else?
  1169. */
  1170. if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
  1171. action_result(pfn, MSG_TRUNCATED_LRU, IGNORED);
  1172. res = -EBUSY;
  1173. goto out;
  1174. }
  1175. identify_page_state:
  1176. res = -EBUSY;
  1177. /*
  1178. * The first check uses the current page flags which may not have any
  1179. * relevant information. The second check with the saved page flagss is
  1180. * carried out only if the first check can't determine the page status.
  1181. */
  1182. for (ps = error_states;; ps++)
  1183. if ((p->flags & ps->mask) == ps->res)
  1184. break;
  1185. page_flags |= (p->flags & (1UL << PG_dirty));
  1186. if (!ps->mask)
  1187. for (ps = error_states;; ps++)
  1188. if ((page_flags & ps->mask) == ps->res)
  1189. break;
  1190. res = page_action(ps, p, pfn);
  1191. out:
  1192. unlock_page(hpage);
  1193. return res;
  1194. }
  1195. EXPORT_SYMBOL_GPL(memory_failure);
  1196. #define MEMORY_FAILURE_FIFO_ORDER 4
  1197. #define MEMORY_FAILURE_FIFO_SIZE (1 << MEMORY_FAILURE_FIFO_ORDER)
  1198. struct memory_failure_entry {
  1199. unsigned long pfn;
  1200. int trapno;
  1201. int flags;
  1202. };
  1203. struct memory_failure_cpu {
  1204. DECLARE_KFIFO(fifo, struct memory_failure_entry,
  1205. MEMORY_FAILURE_FIFO_SIZE);
  1206. spinlock_t lock;
  1207. struct work_struct work;
  1208. };
  1209. static DEFINE_PER_CPU(struct memory_failure_cpu, memory_failure_cpu);
  1210. /**
  1211. * memory_failure_queue - Schedule handling memory failure of a page.
  1212. * @pfn: Page Number of the corrupted page
  1213. * @trapno: Trap number reported in the signal to user space.
  1214. * @flags: Flags for memory failure handling
  1215. *
  1216. * This function is called by the low level hardware error handler
  1217. * when it detects hardware memory corruption of a page. It schedules
  1218. * the recovering of error page, including dropping pages, killing
  1219. * processes etc.
  1220. *
  1221. * The function is primarily of use for corruptions that
  1222. * happen outside the current execution context (e.g. when
  1223. * detected by a background scrubber)
  1224. *
  1225. * Can run in IRQ context.
  1226. */
  1227. void memory_failure_queue(unsigned long pfn, int trapno, int flags)
  1228. {
  1229. struct memory_failure_cpu *mf_cpu;
  1230. unsigned long proc_flags;
  1231. struct memory_failure_entry entry = {
  1232. .pfn = pfn,
  1233. .trapno = trapno,
  1234. .flags = flags,
  1235. };
  1236. mf_cpu = &get_cpu_var(memory_failure_cpu);
  1237. spin_lock_irqsave(&mf_cpu->lock, proc_flags);
  1238. if (kfifo_put(&mf_cpu->fifo, entry))
  1239. schedule_work_on(smp_processor_id(), &mf_cpu->work);
  1240. else
  1241. pr_err("Memory failure: buffer overflow when queuing memory failure at %#lx\n",
  1242. pfn);
  1243. spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
  1244. put_cpu_var(memory_failure_cpu);
  1245. }
  1246. EXPORT_SYMBOL_GPL(memory_failure_queue);
  1247. static void memory_failure_work_func(struct work_struct *work)
  1248. {
  1249. struct memory_failure_cpu *mf_cpu;
  1250. struct memory_failure_entry entry = { 0, };
  1251. unsigned long proc_flags;
  1252. int gotten;
  1253. mf_cpu = this_cpu_ptr(&memory_failure_cpu);
  1254. for (;;) {
  1255. spin_lock_irqsave(&mf_cpu->lock, proc_flags);
  1256. gotten = kfifo_get(&mf_cpu->fifo, &entry);
  1257. spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
  1258. if (!gotten)
  1259. break;
  1260. if (entry.flags & MF_SOFT_OFFLINE)
  1261. soft_offline_page(pfn_to_page(entry.pfn), entry.flags);
  1262. else
  1263. memory_failure(entry.pfn, entry.trapno, entry.flags);
  1264. }
  1265. }
  1266. static int __init memory_failure_init(void)
  1267. {
  1268. struct memory_failure_cpu *mf_cpu;
  1269. int cpu;
  1270. for_each_possible_cpu(cpu) {
  1271. mf_cpu = &per_cpu(memory_failure_cpu, cpu);
  1272. spin_lock_init(&mf_cpu->lock);
  1273. INIT_KFIFO(mf_cpu->fifo);
  1274. INIT_WORK(&mf_cpu->work, memory_failure_work_func);
  1275. }
  1276. return 0;
  1277. }
  1278. core_initcall(memory_failure_init);
  1279. /**
  1280. * unpoison_memory - Unpoison a previously poisoned page
  1281. * @pfn: Page number of the to be unpoisoned page
  1282. *
  1283. * Software-unpoison a page that has been poisoned by
  1284. * memory_failure() earlier.
  1285. *
  1286. * This is only done on the software-level, so it only works
  1287. * for linux injected failures, not real hardware failures
  1288. *
  1289. * Returns 0 for success, otherwise -errno.
  1290. */
  1291. int unpoison_memory(unsigned long pfn)
  1292. {
  1293. struct page *page;
  1294. struct page *p;
  1295. int freeit = 0;
  1296. unsigned int nr_pages;
  1297. if (!pfn_valid(pfn))
  1298. return -ENXIO;
  1299. p = pfn_to_page(pfn);
  1300. page = compound_head(p);
  1301. if (!PageHWPoison(p)) {
  1302. pr_info("MCE: Page was already unpoisoned %#lx\n", pfn);
  1303. return 0;
  1304. }
  1305. /*
  1306. * unpoison_memory() can encounter thp only when the thp is being
  1307. * worked by memory_failure() and the page lock is not held yet.
  1308. * In such case, we yield to memory_failure() and make unpoison fail.
  1309. */
  1310. if (!PageHuge(page) && PageTransHuge(page)) {
  1311. pr_info("MCE: Memory failure is now running on %#lx\n", pfn);
  1312. return 0;
  1313. }
  1314. nr_pages = 1 << compound_order(page);
  1315. if (!get_page_unless_zero(page)) {
  1316. /*
  1317. * Since HWPoisoned hugepage should have non-zero refcount,
  1318. * race between memory failure and unpoison seems to happen.
  1319. * In such case unpoison fails and memory failure runs
  1320. * to the end.
  1321. */
  1322. if (PageHuge(page)) {
  1323. pr_info("MCE: Memory failure is now running on free hugepage %#lx\n", pfn);
  1324. return 0;
  1325. }
  1326. if (TestClearPageHWPoison(p))
  1327. atomic_long_dec(&num_poisoned_pages);
  1328. pr_info("MCE: Software-unpoisoned free page %#lx\n", pfn);
  1329. return 0;
  1330. }
  1331. lock_page(page);
  1332. /*
  1333. * This test is racy because PG_hwpoison is set outside of page lock.
  1334. * That's acceptable because that won't trigger kernel panic. Instead,
  1335. * the PG_hwpoison page will be caught and isolated on the entrance to
  1336. * the free buddy page pool.
  1337. */
  1338. if (TestClearPageHWPoison(page)) {
  1339. pr_info("MCE: Software-unpoisoned page %#lx\n", pfn);
  1340. atomic_long_sub(nr_pages, &num_poisoned_pages);
  1341. freeit = 1;
  1342. if (PageHuge(page))
  1343. clear_page_hwpoison_huge_page(page);
  1344. }
  1345. unlock_page(page);
  1346. put_page(page);
  1347. if (freeit && !(pfn == my_zero_pfn(0) && page_count(p) == 1))
  1348. put_page(page);
  1349. return 0;
  1350. }
  1351. EXPORT_SYMBOL(unpoison_memory);
  1352. static struct page *new_page(struct page *p, unsigned long private, int **x)
  1353. {
  1354. int nid = page_to_nid(p);
  1355. if (PageHuge(p))
  1356. return alloc_huge_page_node(page_hstate(compound_head(p)),
  1357. nid);
  1358. else
  1359. return alloc_pages_exact_node(nid, GFP_HIGHUSER_MOVABLE, 0);
  1360. }
  1361. /*
  1362. * Safely get reference count of an arbitrary page.
  1363. * Returns 0 for a free page, -EIO for a zero refcount page
  1364. * that is not free, and 1 for any other page type.
  1365. * For 1 the page is returned with increased page count, otherwise not.
  1366. */
  1367. static int __get_any_page(struct page *p, unsigned long pfn, int flags)
  1368. {
  1369. int ret;
  1370. if (flags & MF_COUNT_INCREASED)
  1371. return 1;
  1372. /*
  1373. * When the target page is a free hugepage, just remove it
  1374. * from free hugepage list.
  1375. */
  1376. if (!get_page_unless_zero(compound_head(p))) {
  1377. if (PageHuge(p)) {
  1378. pr_info("%s: %#lx free huge page\n", __func__, pfn);
  1379. ret = 0;
  1380. } else if (is_free_buddy_page(p)) {
  1381. pr_info("%s: %#lx free buddy page\n", __func__, pfn);
  1382. ret = 0;
  1383. } else {
  1384. pr_info("%s: %#lx: unknown zero refcount page type %lx\n",
  1385. __func__, pfn, p->flags);
  1386. ret = -EIO;
  1387. }
  1388. } else {
  1389. /* Not a free page */
  1390. ret = 1;
  1391. }
  1392. return ret;
  1393. }
  1394. static int get_any_page(struct page *page, unsigned long pfn, int flags)
  1395. {
  1396. int ret = __get_any_page(page, pfn, flags);
  1397. if (ret == 1 && !PageHuge(page) && !PageLRU(page)) {
  1398. /*
  1399. * Try to free it.
  1400. */
  1401. put_page(page);
  1402. shake_page(page, 1);
  1403. /*
  1404. * Did it turn free?
  1405. */
  1406. ret = __get_any_page(page, pfn, 0);
  1407. if (!PageLRU(page)) {
  1408. pr_info("soft_offline: %#lx: unknown non LRU page type %lx\n",
  1409. pfn, page->flags);
  1410. return -EIO;
  1411. }
  1412. }
  1413. return ret;
  1414. }
  1415. static int soft_offline_huge_page(struct page *page, int flags)
  1416. {
  1417. int ret;
  1418. unsigned long pfn = page_to_pfn(page);
  1419. struct page *hpage = compound_head(page);
  1420. LIST_HEAD(pagelist);
  1421. /*
  1422. * This double-check of PageHWPoison is to avoid the race with
  1423. * memory_failure(). See also comment in __soft_offline_page().
  1424. */
  1425. lock_page(hpage);
  1426. if (PageHWPoison(hpage)) {
  1427. unlock_page(hpage);
  1428. put_page(hpage);
  1429. pr_info("soft offline: %#lx hugepage already poisoned\n", pfn);
  1430. return -EBUSY;
  1431. }
  1432. unlock_page(hpage);
  1433. ret = isolate_huge_page(hpage, &pagelist);
  1434. if (ret) {
  1435. /*
  1436. * get_any_page() and isolate_huge_page() takes a refcount each,
  1437. * so need to drop one here.
  1438. */
  1439. put_page(hpage);
  1440. } else {
  1441. pr_info("soft offline: %#lx hugepage failed to isolate\n", pfn);
  1442. return -EBUSY;
  1443. }
  1444. ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
  1445. MIGRATE_SYNC, MR_MEMORY_FAILURE);
  1446. if (ret) {
  1447. pr_info("soft offline: %#lx: migration failed %d, type %lx\n",
  1448. pfn, ret, page->flags);
  1449. /*
  1450. * We know that soft_offline_huge_page() tries to migrate
  1451. * only one hugepage pointed to by hpage, so we need not
  1452. * run through the pagelist here.
  1453. */
  1454. putback_active_hugepage(hpage);
  1455. if (ret > 0)
  1456. ret = -EIO;
  1457. } else {
  1458. /* overcommit hugetlb page will be freed to buddy */
  1459. if (PageHuge(page)) {
  1460. set_page_hwpoison_huge_page(hpage);
  1461. dequeue_hwpoisoned_huge_page(hpage);
  1462. atomic_long_add(1 << compound_order(hpage),
  1463. &num_poisoned_pages);
  1464. } else {
  1465. SetPageHWPoison(page);
  1466. atomic_long_inc(&num_poisoned_pages);
  1467. }
  1468. }
  1469. return ret;
  1470. }
  1471. static int __soft_offline_page(struct page *page, int flags)
  1472. {
  1473. int ret;
  1474. unsigned long pfn = page_to_pfn(page);
  1475. /*
  1476. * Check PageHWPoison again inside page lock because PageHWPoison
  1477. * is set by memory_failure() outside page lock. Note that
  1478. * memory_failure() also double-checks PageHWPoison inside page lock,
  1479. * so there's no race between soft_offline_page() and memory_failure().
  1480. */
  1481. lock_page(page);
  1482. wait_on_page_writeback(page);
  1483. if (PageHWPoison(page)) {
  1484. unlock_page(page);
  1485. put_page(page);
  1486. pr_info("soft offline: %#lx page already poisoned\n", pfn);
  1487. return -EBUSY;
  1488. }
  1489. /*
  1490. * Try to invalidate first. This should work for
  1491. * non dirty unmapped page cache pages.
  1492. */
  1493. ret = invalidate_inode_page(page);
  1494. unlock_page(page);
  1495. /*
  1496. * RED-PEN would be better to keep it isolated here, but we
  1497. * would need to fix isolation locking first.
  1498. */
  1499. if (ret == 1) {
  1500. put_page(page);
  1501. pr_info("soft_offline: %#lx: invalidated\n", pfn);
  1502. SetPageHWPoison(page);
  1503. atomic_long_inc(&num_poisoned_pages);
  1504. return 0;
  1505. }
  1506. /*
  1507. * Simple invalidation didn't work.
  1508. * Try to migrate to a new page instead. migrate.c
  1509. * handles a large number of cases for us.
  1510. */
  1511. ret = isolate_lru_page(page);
  1512. /*
  1513. * Drop page reference which is came from get_any_page()
  1514. * successful isolate_lru_page() already took another one.
  1515. */
  1516. put_page(page);
  1517. if (!ret) {
  1518. LIST_HEAD(pagelist);
  1519. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1520. page_is_file_cache(page));
  1521. list_add(&page->lru, &pagelist);
  1522. ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
  1523. MIGRATE_SYNC, MR_MEMORY_FAILURE);
  1524. if (ret) {
  1525. if (!list_empty(&pagelist)) {
  1526. list_del(&page->lru);
  1527. dec_zone_page_state(page, NR_ISOLATED_ANON +
  1528. page_is_file_cache(page));
  1529. putback_lru_page(page);
  1530. }
  1531. pr_info("soft offline: %#lx: migration failed %d, type %lx\n",
  1532. pfn, ret, page->flags);
  1533. if (ret > 0)
  1534. ret = -EIO;
  1535. } else {
  1536. /*
  1537. * After page migration succeeds, the source page can
  1538. * be trapped in pagevec and actual freeing is delayed.
  1539. * Freeing code works differently based on PG_hwpoison,
  1540. * so there's a race. We need to make sure that the
  1541. * source page should be freed back to buddy before
  1542. * setting PG_hwpoison.
  1543. */
  1544. if (!is_free_buddy_page(page))
  1545. drain_all_pages(page_zone(page));
  1546. SetPageHWPoison(page);
  1547. if (!is_free_buddy_page(page))
  1548. pr_info("soft offline: %#lx: page leaked\n",
  1549. pfn);
  1550. atomic_long_inc(&num_poisoned_pages);
  1551. }
  1552. } else {
  1553. pr_info("soft offline: %#lx: isolation failed: %d, page count %d, type %lx\n",
  1554. pfn, ret, page_count(page), page->flags);
  1555. }
  1556. return ret;
  1557. }
  1558. /**
  1559. * soft_offline_page - Soft offline a page.
  1560. * @page: page to offline
  1561. * @flags: flags. Same as memory_failure().
  1562. *
  1563. * Returns 0 on success, otherwise negated errno.
  1564. *
  1565. * Soft offline a page, by migration or invalidation,
  1566. * without killing anything. This is for the case when
  1567. * a page is not corrupted yet (so it's still valid to access),
  1568. * but has had a number of corrected errors and is better taken
  1569. * out.
  1570. *
  1571. * The actual policy on when to do that is maintained by
  1572. * user space.
  1573. *
  1574. * This should never impact any application or cause data loss,
  1575. * however it might take some time.
  1576. *
  1577. * This is not a 100% solution for all memory, but tries to be
  1578. * ``good enough'' for the majority of memory.
  1579. */
  1580. int soft_offline_page(struct page *page, int flags)
  1581. {
  1582. int ret;
  1583. unsigned long pfn = page_to_pfn(page);
  1584. struct page *hpage = compound_head(page);
  1585. if (PageHWPoison(page)) {
  1586. pr_info("soft offline: %#lx page already poisoned\n", pfn);
  1587. return -EBUSY;
  1588. }
  1589. if (!PageHuge(page) && PageTransHuge(hpage)) {
  1590. if (PageAnon(hpage) && unlikely(split_huge_page(hpage))) {
  1591. pr_info("soft offline: %#lx: failed to split THP\n",
  1592. pfn);
  1593. return -EBUSY;
  1594. }
  1595. }
  1596. get_online_mems();
  1597. /*
  1598. * Isolate the page, so that it doesn't get reallocated if it
  1599. * was free. This flag should be kept set until the source page
  1600. * is freed and PG_hwpoison on it is set.
  1601. */
  1602. if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  1603. set_migratetype_isolate(page, true);
  1604. ret = get_any_page(page, pfn, flags);
  1605. put_online_mems();
  1606. if (ret > 0) { /* for in-use pages */
  1607. if (PageHuge(page))
  1608. ret = soft_offline_huge_page(page, flags);
  1609. else
  1610. ret = __soft_offline_page(page, flags);
  1611. } else if (ret == 0) { /* for free pages */
  1612. if (PageHuge(page)) {
  1613. set_page_hwpoison_huge_page(hpage);
  1614. if (!dequeue_hwpoisoned_huge_page(hpage))
  1615. atomic_long_add(1 << compound_order(hpage),
  1616. &num_poisoned_pages);
  1617. } else {
  1618. if (!TestSetPageHWPoison(page))
  1619. atomic_long_inc(&num_poisoned_pages);
  1620. }
  1621. }
  1622. unset_migratetype_isolate(page, MIGRATE_MOVABLE);
  1623. return ret;
  1624. }