memory-failure.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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 2bit ECC memory or cache
  11. * failure.
  12. *
  13. * Handles page cache pages in various states. The tricky part
  14. * here is that we can access any page asynchronous to other VM
  15. * users, because memory failures could happen anytime and anywhere,
  16. * possibly violating some of their assumptions. This is why this code
  17. * has to be extremely careful. Generally it tries to use normal locking
  18. * rules, as in get the standard locks, even if that means the
  19. * error handling takes potentially a long time.
  20. *
  21. * The operation to map back from RMAP chains to processes has to walk
  22. * the complete process list and has non linear complexity with the number
  23. * mappings. In short it can be quite slow. But since memory corruptions
  24. * are rare we hope to get away with this.
  25. */
  26. /*
  27. * Notebook:
  28. * - hugetlb needs more code
  29. * - kcore/oldmem/vmcore/mem/kmem check for hwpoison pages
  30. * - pass bad pages to kdump next kernel
  31. */
  32. #define DEBUG 1 /* remove me in 2.6.34 */
  33. #include <linux/kernel.h>
  34. #include <linux/mm.h>
  35. #include <linux/page-flags.h>
  36. #include <linux/kernel-page-flags.h>
  37. #include <linux/sched.h>
  38. #include <linux/ksm.h>
  39. #include <linux/rmap.h>
  40. #include <linux/pagemap.h>
  41. #include <linux/swap.h>
  42. #include <linux/backing-dev.h>
  43. #include "internal.h"
  44. int sysctl_memory_failure_early_kill __read_mostly = 0;
  45. int sysctl_memory_failure_recovery __read_mostly = 1;
  46. atomic_long_t mce_bad_pages __read_mostly = ATOMIC_LONG_INIT(0);
  47. u32 hwpoison_filter_dev_major = ~0U;
  48. u32 hwpoison_filter_dev_minor = ~0U;
  49. u64 hwpoison_filter_flags_mask;
  50. u64 hwpoison_filter_flags_value;
  51. EXPORT_SYMBOL_GPL(hwpoison_filter_dev_major);
  52. EXPORT_SYMBOL_GPL(hwpoison_filter_dev_minor);
  53. EXPORT_SYMBOL_GPL(hwpoison_filter_flags_mask);
  54. EXPORT_SYMBOL_GPL(hwpoison_filter_flags_value);
  55. static int hwpoison_filter_dev(struct page *p)
  56. {
  57. struct address_space *mapping;
  58. dev_t dev;
  59. if (hwpoison_filter_dev_major == ~0U &&
  60. hwpoison_filter_dev_minor == ~0U)
  61. return 0;
  62. /*
  63. * page_mapping() does not accept slab page
  64. */
  65. if (PageSlab(p))
  66. return -EINVAL;
  67. mapping = page_mapping(p);
  68. if (mapping == NULL || mapping->host == NULL)
  69. return -EINVAL;
  70. dev = mapping->host->i_sb->s_dev;
  71. if (hwpoison_filter_dev_major != ~0U &&
  72. hwpoison_filter_dev_major != MAJOR(dev))
  73. return -EINVAL;
  74. if (hwpoison_filter_dev_minor != ~0U &&
  75. hwpoison_filter_dev_minor != MINOR(dev))
  76. return -EINVAL;
  77. return 0;
  78. }
  79. static int hwpoison_filter_flags(struct page *p)
  80. {
  81. if (!hwpoison_filter_flags_mask)
  82. return 0;
  83. if ((stable_page_flags(p) & hwpoison_filter_flags_mask) ==
  84. hwpoison_filter_flags_value)
  85. return 0;
  86. else
  87. return -EINVAL;
  88. }
  89. /*
  90. * This allows stress tests to limit test scope to a collection of tasks
  91. * by putting them under some memcg. This prevents killing unrelated/important
  92. * processes such as /sbin/init. Note that the target task may share clean
  93. * pages with init (eg. libc text), which is harmless. If the target task
  94. * share _dirty_ pages with another task B, the test scheme must make sure B
  95. * is also included in the memcg. At last, due to race conditions this filter
  96. * can only guarantee that the page either belongs to the memcg tasks, or is
  97. * a freed page.
  98. */
  99. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  100. u64 hwpoison_filter_memcg;
  101. EXPORT_SYMBOL_GPL(hwpoison_filter_memcg);
  102. static int hwpoison_filter_task(struct page *p)
  103. {
  104. struct mem_cgroup *mem;
  105. struct cgroup_subsys_state *css;
  106. unsigned long ino;
  107. if (!hwpoison_filter_memcg)
  108. return 0;
  109. mem = try_get_mem_cgroup_from_page(p);
  110. if (!mem)
  111. return -EINVAL;
  112. css = mem_cgroup_css(mem);
  113. /* root_mem_cgroup has NULL dentries */
  114. if (!css->cgroup->dentry)
  115. return -EINVAL;
  116. ino = css->cgroup->dentry->d_inode->i_ino;
  117. css_put(css);
  118. if (ino != hwpoison_filter_memcg)
  119. return -EINVAL;
  120. return 0;
  121. }
  122. #else
  123. static int hwpoison_filter_task(struct page *p) { return 0; }
  124. #endif
  125. int hwpoison_filter(struct page *p)
  126. {
  127. if (hwpoison_filter_dev(p))
  128. return -EINVAL;
  129. if (hwpoison_filter_flags(p))
  130. return -EINVAL;
  131. if (hwpoison_filter_task(p))
  132. return -EINVAL;
  133. return 0;
  134. }
  135. EXPORT_SYMBOL_GPL(hwpoison_filter);
  136. /*
  137. * Send all the processes who have the page mapped an ``action optional''
  138. * signal.
  139. */
  140. static int kill_proc_ao(struct task_struct *t, unsigned long addr, int trapno,
  141. unsigned long pfn)
  142. {
  143. struct siginfo si;
  144. int ret;
  145. printk(KERN_ERR
  146. "MCE %#lx: Killing %s:%d early due to hardware memory corruption\n",
  147. pfn, t->comm, t->pid);
  148. si.si_signo = SIGBUS;
  149. si.si_errno = 0;
  150. si.si_code = BUS_MCEERR_AO;
  151. si.si_addr = (void *)addr;
  152. #ifdef __ARCH_SI_TRAPNO
  153. si.si_trapno = trapno;
  154. #endif
  155. si.si_addr_lsb = PAGE_SHIFT;
  156. /*
  157. * Don't use force here, it's convenient if the signal
  158. * can be temporarily blocked.
  159. * This could cause a loop when the user sets SIGBUS
  160. * to SIG_IGN, but hopefully noone will do that?
  161. */
  162. ret = send_sig_info(SIGBUS, &si, t); /* synchronous? */
  163. if (ret < 0)
  164. printk(KERN_INFO "MCE: Error sending signal to %s:%d: %d\n",
  165. t->comm, t->pid, ret);
  166. return ret;
  167. }
  168. /*
  169. * When a unknown page type is encountered drain as many buffers as possible
  170. * in the hope to turn the page into a LRU or free page, which we can handle.
  171. */
  172. void shake_page(struct page *p)
  173. {
  174. if (!PageSlab(p)) {
  175. lru_add_drain_all();
  176. if (PageLRU(p))
  177. return;
  178. drain_all_pages();
  179. if (PageLRU(p) || is_free_buddy_page(p))
  180. return;
  181. }
  182. /*
  183. * Could call shrink_slab here (which would also
  184. * shrink other caches). Unfortunately that might
  185. * also access the corrupted page, which could be fatal.
  186. */
  187. }
  188. EXPORT_SYMBOL_GPL(shake_page);
  189. /*
  190. * Kill all processes that have a poisoned page mapped and then isolate
  191. * the page.
  192. *
  193. * General strategy:
  194. * Find all processes having the page mapped and kill them.
  195. * But we keep a page reference around so that the page is not
  196. * actually freed yet.
  197. * Then stash the page away
  198. *
  199. * There's no convenient way to get back to mapped processes
  200. * from the VMAs. So do a brute-force search over all
  201. * running processes.
  202. *
  203. * Remember that machine checks are not common (or rather
  204. * if they are common you have other problems), so this shouldn't
  205. * be a performance issue.
  206. *
  207. * Also there are some races possible while we get from the
  208. * error detection to actually handle it.
  209. */
  210. struct to_kill {
  211. struct list_head nd;
  212. struct task_struct *tsk;
  213. unsigned long addr;
  214. unsigned addr_valid:1;
  215. };
  216. /*
  217. * Failure handling: if we can't find or can't kill a process there's
  218. * not much we can do. We just print a message and ignore otherwise.
  219. */
  220. /*
  221. * Schedule a process for later kill.
  222. * Uses GFP_ATOMIC allocations to avoid potential recursions in the VM.
  223. * TBD would GFP_NOIO be enough?
  224. */
  225. static void add_to_kill(struct task_struct *tsk, struct page *p,
  226. struct vm_area_struct *vma,
  227. struct list_head *to_kill,
  228. struct to_kill **tkc)
  229. {
  230. struct to_kill *tk;
  231. if (*tkc) {
  232. tk = *tkc;
  233. *tkc = NULL;
  234. } else {
  235. tk = kmalloc(sizeof(struct to_kill), GFP_ATOMIC);
  236. if (!tk) {
  237. printk(KERN_ERR
  238. "MCE: Out of memory while machine check handling\n");
  239. return;
  240. }
  241. }
  242. tk->addr = page_address_in_vma(p, vma);
  243. tk->addr_valid = 1;
  244. /*
  245. * In theory we don't have to kill when the page was
  246. * munmaped. But it could be also a mremap. Since that's
  247. * likely very rare kill anyways just out of paranoia, but use
  248. * a SIGKILL because the error is not contained anymore.
  249. */
  250. if (tk->addr == -EFAULT) {
  251. pr_debug("MCE: Unable to find user space address %lx in %s\n",
  252. page_to_pfn(p), tsk->comm);
  253. tk->addr_valid = 0;
  254. }
  255. get_task_struct(tsk);
  256. tk->tsk = tsk;
  257. list_add_tail(&tk->nd, to_kill);
  258. }
  259. /*
  260. * Kill the processes that have been collected earlier.
  261. *
  262. * Only do anything when DOIT is set, otherwise just free the list
  263. * (this is used for clean pages which do not need killing)
  264. * Also when FAIL is set do a force kill because something went
  265. * wrong earlier.
  266. */
  267. static void kill_procs_ao(struct list_head *to_kill, int doit, int trapno,
  268. int fail, unsigned long pfn)
  269. {
  270. struct to_kill *tk, *next;
  271. list_for_each_entry_safe (tk, next, to_kill, nd) {
  272. if (doit) {
  273. /*
  274. * In case something went wrong with munmapping
  275. * make sure the process doesn't catch the
  276. * signal and then access the memory. Just kill it.
  277. * the signal handlers
  278. */
  279. if (fail || tk->addr_valid == 0) {
  280. printk(KERN_ERR
  281. "MCE %#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
  282. pfn, tk->tsk->comm, tk->tsk->pid);
  283. force_sig(SIGKILL, tk->tsk);
  284. }
  285. /*
  286. * In theory the process could have mapped
  287. * something else on the address in-between. We could
  288. * check for that, but we need to tell the
  289. * process anyways.
  290. */
  291. else if (kill_proc_ao(tk->tsk, tk->addr, trapno,
  292. pfn) < 0)
  293. printk(KERN_ERR
  294. "MCE %#lx: Cannot send advisory machine check signal to %s:%d\n",
  295. pfn, tk->tsk->comm, tk->tsk->pid);
  296. }
  297. put_task_struct(tk->tsk);
  298. kfree(tk);
  299. }
  300. }
  301. static int task_early_kill(struct task_struct *tsk)
  302. {
  303. if (!tsk->mm)
  304. return 0;
  305. if (tsk->flags & PF_MCE_PROCESS)
  306. return !!(tsk->flags & PF_MCE_EARLY);
  307. return sysctl_memory_failure_early_kill;
  308. }
  309. /*
  310. * Collect processes when the error hit an anonymous page.
  311. */
  312. static void collect_procs_anon(struct page *page, struct list_head *to_kill,
  313. struct to_kill **tkc)
  314. {
  315. struct vm_area_struct *vma;
  316. struct task_struct *tsk;
  317. struct anon_vma *av;
  318. read_lock(&tasklist_lock);
  319. av = page_lock_anon_vma(page);
  320. if (av == NULL) /* Not actually mapped anymore */
  321. goto out;
  322. for_each_process (tsk) {
  323. if (!task_early_kill(tsk))
  324. continue;
  325. list_for_each_entry (vma, &av->head, anon_vma_node) {
  326. if (!page_mapped_in_vma(page, vma))
  327. continue;
  328. if (vma->vm_mm == tsk->mm)
  329. add_to_kill(tsk, page, vma, to_kill, tkc);
  330. }
  331. }
  332. page_unlock_anon_vma(av);
  333. out:
  334. read_unlock(&tasklist_lock);
  335. }
  336. /*
  337. * Collect processes when the error hit a file mapped page.
  338. */
  339. static void collect_procs_file(struct page *page, struct list_head *to_kill,
  340. struct to_kill **tkc)
  341. {
  342. struct vm_area_struct *vma;
  343. struct task_struct *tsk;
  344. struct prio_tree_iter iter;
  345. struct address_space *mapping = page->mapping;
  346. /*
  347. * A note on the locking order between the two locks.
  348. * We don't rely on this particular order.
  349. * If you have some other code that needs a different order
  350. * feel free to switch them around. Or add a reverse link
  351. * from mm_struct to task_struct, then this could be all
  352. * done without taking tasklist_lock and looping over all tasks.
  353. */
  354. read_lock(&tasklist_lock);
  355. spin_lock(&mapping->i_mmap_lock);
  356. for_each_process(tsk) {
  357. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  358. if (!task_early_kill(tsk))
  359. continue;
  360. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff,
  361. pgoff) {
  362. /*
  363. * Send early kill signal to tasks where a vma covers
  364. * the page but the corrupted page is not necessarily
  365. * mapped it in its pte.
  366. * Assume applications who requested early kill want
  367. * to be informed of all such data corruptions.
  368. */
  369. if (vma->vm_mm == tsk->mm)
  370. add_to_kill(tsk, page, vma, to_kill, tkc);
  371. }
  372. }
  373. spin_unlock(&mapping->i_mmap_lock);
  374. read_unlock(&tasklist_lock);
  375. }
  376. /*
  377. * Collect the processes who have the corrupted page mapped to kill.
  378. * This is done in two steps for locking reasons.
  379. * First preallocate one tokill structure outside the spin locks,
  380. * so that we can kill at least one process reasonably reliable.
  381. */
  382. static void collect_procs(struct page *page, struct list_head *tokill)
  383. {
  384. struct to_kill *tk;
  385. if (!page->mapping)
  386. return;
  387. tk = kmalloc(sizeof(struct to_kill), GFP_NOIO);
  388. if (!tk)
  389. return;
  390. if (PageAnon(page))
  391. collect_procs_anon(page, tokill, &tk);
  392. else
  393. collect_procs_file(page, tokill, &tk);
  394. kfree(tk);
  395. }
  396. /*
  397. * Error handlers for various types of pages.
  398. */
  399. enum outcome {
  400. IGNORED, /* Error: cannot be handled */
  401. FAILED, /* Error: handling failed */
  402. DELAYED, /* Will be handled later */
  403. RECOVERED, /* Successfully recovered */
  404. };
  405. static const char *action_name[] = {
  406. [IGNORED] = "Ignored",
  407. [FAILED] = "Failed",
  408. [DELAYED] = "Delayed",
  409. [RECOVERED] = "Recovered",
  410. };
  411. /*
  412. * XXX: It is possible that a page is isolated from LRU cache,
  413. * and then kept in swap cache or failed to remove from page cache.
  414. * The page count will stop it from being freed by unpoison.
  415. * Stress tests should be aware of this memory leak problem.
  416. */
  417. static int delete_from_lru_cache(struct page *p)
  418. {
  419. if (!isolate_lru_page(p)) {
  420. /*
  421. * Clear sensible page flags, so that the buddy system won't
  422. * complain when the page is unpoison-and-freed.
  423. */
  424. ClearPageActive(p);
  425. ClearPageUnevictable(p);
  426. /*
  427. * drop the page count elevated by isolate_lru_page()
  428. */
  429. page_cache_release(p);
  430. return 0;
  431. }
  432. return -EIO;
  433. }
  434. /*
  435. * Error hit kernel page.
  436. * Do nothing, try to be lucky and not touch this instead. For a few cases we
  437. * could be more sophisticated.
  438. */
  439. static int me_kernel(struct page *p, unsigned long pfn)
  440. {
  441. return IGNORED;
  442. }
  443. /*
  444. * Page in unknown state. Do nothing.
  445. */
  446. static int me_unknown(struct page *p, unsigned long pfn)
  447. {
  448. printk(KERN_ERR "MCE %#lx: Unknown page state\n", pfn);
  449. return FAILED;
  450. }
  451. /*
  452. * Clean (or cleaned) page cache page.
  453. */
  454. static int me_pagecache_clean(struct page *p, unsigned long pfn)
  455. {
  456. int err;
  457. int ret = FAILED;
  458. struct address_space *mapping;
  459. delete_from_lru_cache(p);
  460. /*
  461. * For anonymous pages we're done the only reference left
  462. * should be the one m_f() holds.
  463. */
  464. if (PageAnon(p))
  465. return RECOVERED;
  466. /*
  467. * Now truncate the page in the page cache. This is really
  468. * more like a "temporary hole punch"
  469. * Don't do this for block devices when someone else
  470. * has a reference, because it could be file system metadata
  471. * and that's not safe to truncate.
  472. */
  473. mapping = page_mapping(p);
  474. if (!mapping) {
  475. /*
  476. * Page has been teared down in the meanwhile
  477. */
  478. return FAILED;
  479. }
  480. /*
  481. * Truncation is a bit tricky. Enable it per file system for now.
  482. *
  483. * Open: to take i_mutex or not for this? Right now we don't.
  484. */
  485. if (mapping->a_ops->error_remove_page) {
  486. err = mapping->a_ops->error_remove_page(mapping, p);
  487. if (err != 0) {
  488. printk(KERN_INFO "MCE %#lx: Failed to punch page: %d\n",
  489. pfn, err);
  490. } else if (page_has_private(p) &&
  491. !try_to_release_page(p, GFP_NOIO)) {
  492. pr_debug("MCE %#lx: failed to release buffers\n", pfn);
  493. } else {
  494. ret = RECOVERED;
  495. }
  496. } else {
  497. /*
  498. * If the file system doesn't support it just invalidate
  499. * This fails on dirty or anything with private pages
  500. */
  501. if (invalidate_inode_page(p))
  502. ret = RECOVERED;
  503. else
  504. printk(KERN_INFO "MCE %#lx: Failed to invalidate\n",
  505. pfn);
  506. }
  507. return ret;
  508. }
  509. /*
  510. * Dirty cache page page
  511. * Issues: when the error hit a hole page the error is not properly
  512. * propagated.
  513. */
  514. static int me_pagecache_dirty(struct page *p, unsigned long pfn)
  515. {
  516. struct address_space *mapping = page_mapping(p);
  517. SetPageError(p);
  518. /* TBD: print more information about the file. */
  519. if (mapping) {
  520. /*
  521. * IO error will be reported by write(), fsync(), etc.
  522. * who check the mapping.
  523. * This way the application knows that something went
  524. * wrong with its dirty file data.
  525. *
  526. * There's one open issue:
  527. *
  528. * The EIO will be only reported on the next IO
  529. * operation and then cleared through the IO map.
  530. * Normally Linux has two mechanisms to pass IO error
  531. * first through the AS_EIO flag in the address space
  532. * and then through the PageError flag in the page.
  533. * Since we drop pages on memory failure handling the
  534. * only mechanism open to use is through AS_AIO.
  535. *
  536. * This has the disadvantage that it gets cleared on
  537. * the first operation that returns an error, while
  538. * the PageError bit is more sticky and only cleared
  539. * when the page is reread or dropped. If an
  540. * application assumes it will always get error on
  541. * fsync, but does other operations on the fd before
  542. * and the page is dropped inbetween then the error
  543. * will not be properly reported.
  544. *
  545. * This can already happen even without hwpoisoned
  546. * pages: first on metadata IO errors (which only
  547. * report through AS_EIO) or when the page is dropped
  548. * at the wrong time.
  549. *
  550. * So right now we assume that the application DTRT on
  551. * the first EIO, but we're not worse than other parts
  552. * of the kernel.
  553. */
  554. mapping_set_error(mapping, EIO);
  555. }
  556. return me_pagecache_clean(p, pfn);
  557. }
  558. /*
  559. * Clean and dirty swap cache.
  560. *
  561. * Dirty swap cache page is tricky to handle. The page could live both in page
  562. * cache and swap cache(ie. page is freshly swapped in). So it could be
  563. * referenced concurrently by 2 types of PTEs:
  564. * normal PTEs and swap PTEs. We try to handle them consistently by calling
  565. * try_to_unmap(TTU_IGNORE_HWPOISON) to convert the normal PTEs to swap PTEs,
  566. * and then
  567. * - clear dirty bit to prevent IO
  568. * - remove from LRU
  569. * - but keep in the swap cache, so that when we return to it on
  570. * a later page fault, we know the application is accessing
  571. * corrupted data and shall be killed (we installed simple
  572. * interception code in do_swap_page to catch it).
  573. *
  574. * Clean swap cache pages can be directly isolated. A later page fault will
  575. * bring in the known good data from disk.
  576. */
  577. static int me_swapcache_dirty(struct page *p, unsigned long pfn)
  578. {
  579. ClearPageDirty(p);
  580. /* Trigger EIO in shmem: */
  581. ClearPageUptodate(p);
  582. if (!delete_from_lru_cache(p))
  583. return DELAYED;
  584. else
  585. return FAILED;
  586. }
  587. static int me_swapcache_clean(struct page *p, unsigned long pfn)
  588. {
  589. delete_from_swap_cache(p);
  590. if (!delete_from_lru_cache(p))
  591. return RECOVERED;
  592. else
  593. return FAILED;
  594. }
  595. /*
  596. * Huge pages. Needs work.
  597. * Issues:
  598. * No rmap support so we cannot find the original mapper. In theory could walk
  599. * all MMs and look for the mappings, but that would be non atomic and racy.
  600. * Need rmap for hugepages for this. Alternatively we could employ a heuristic,
  601. * like just walking the current process and hoping it has it mapped (that
  602. * should be usually true for the common "shared database cache" case)
  603. * Should handle free huge pages and dequeue them too, but this needs to
  604. * handle huge page accounting correctly.
  605. */
  606. static int me_huge_page(struct page *p, unsigned long pfn)
  607. {
  608. return FAILED;
  609. }
  610. /*
  611. * Various page states we can handle.
  612. *
  613. * A page state is defined by its current page->flags bits.
  614. * The table matches them in order and calls the right handler.
  615. *
  616. * This is quite tricky because we can access page at any time
  617. * in its live cycle, so all accesses have to be extremly careful.
  618. *
  619. * This is not complete. More states could be added.
  620. * For any missing state don't attempt recovery.
  621. */
  622. #define dirty (1UL << PG_dirty)
  623. #define sc (1UL << PG_swapcache)
  624. #define unevict (1UL << PG_unevictable)
  625. #define mlock (1UL << PG_mlocked)
  626. #define writeback (1UL << PG_writeback)
  627. #define lru (1UL << PG_lru)
  628. #define swapbacked (1UL << PG_swapbacked)
  629. #define head (1UL << PG_head)
  630. #define tail (1UL << PG_tail)
  631. #define compound (1UL << PG_compound)
  632. #define slab (1UL << PG_slab)
  633. #define reserved (1UL << PG_reserved)
  634. static struct page_state {
  635. unsigned long mask;
  636. unsigned long res;
  637. char *msg;
  638. int (*action)(struct page *p, unsigned long pfn);
  639. } error_states[] = {
  640. { reserved, reserved, "reserved kernel", me_kernel },
  641. /*
  642. * free pages are specially detected outside this table:
  643. * PG_buddy pages only make a small fraction of all free pages.
  644. */
  645. /*
  646. * Could in theory check if slab page is free or if we can drop
  647. * currently unused objects without touching them. But just
  648. * treat it as standard kernel for now.
  649. */
  650. { slab, slab, "kernel slab", me_kernel },
  651. #ifdef CONFIG_PAGEFLAGS_EXTENDED
  652. { head, head, "huge", me_huge_page },
  653. { tail, tail, "huge", me_huge_page },
  654. #else
  655. { compound, compound, "huge", me_huge_page },
  656. #endif
  657. { sc|dirty, sc|dirty, "swapcache", me_swapcache_dirty },
  658. { sc|dirty, sc, "swapcache", me_swapcache_clean },
  659. { unevict|dirty, unevict|dirty, "unevictable LRU", me_pagecache_dirty},
  660. { unevict, unevict, "unevictable LRU", me_pagecache_clean},
  661. { mlock|dirty, mlock|dirty, "mlocked LRU", me_pagecache_dirty },
  662. { mlock, mlock, "mlocked LRU", me_pagecache_clean },
  663. { lru|dirty, lru|dirty, "LRU", me_pagecache_dirty },
  664. { lru|dirty, lru, "clean LRU", me_pagecache_clean },
  665. /*
  666. * Catchall entry: must be at end.
  667. */
  668. { 0, 0, "unknown page state", me_unknown },
  669. };
  670. static void action_result(unsigned long pfn, char *msg, int result)
  671. {
  672. struct page *page = pfn_to_page(pfn);
  673. printk(KERN_ERR "MCE %#lx: %s%s page recovery: %s\n",
  674. pfn,
  675. PageDirty(page) ? "dirty " : "",
  676. msg, action_name[result]);
  677. }
  678. static int page_action(struct page_state *ps, struct page *p,
  679. unsigned long pfn)
  680. {
  681. int result;
  682. int count;
  683. result = ps->action(p, pfn);
  684. action_result(pfn, ps->msg, result);
  685. count = page_count(p) - 1;
  686. if (ps->action == me_swapcache_dirty && result == DELAYED)
  687. count--;
  688. if (count != 0) {
  689. printk(KERN_ERR
  690. "MCE %#lx: %s page still referenced by %d users\n",
  691. pfn, ps->msg, count);
  692. result = FAILED;
  693. }
  694. /* Could do more checks here if page looks ok */
  695. /*
  696. * Could adjust zone counters here to correct for the missing page.
  697. */
  698. return (result == RECOVERED || result == DELAYED) ? 0 : -EBUSY;
  699. }
  700. #define N_UNMAP_TRIES 5
  701. /*
  702. * Do all that is necessary to remove user space mappings. Unmap
  703. * the pages and send SIGBUS to the processes if the data was dirty.
  704. */
  705. static int hwpoison_user_mappings(struct page *p, unsigned long pfn,
  706. int trapno)
  707. {
  708. enum ttu_flags ttu = TTU_UNMAP | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
  709. struct address_space *mapping;
  710. LIST_HEAD(tokill);
  711. int ret;
  712. int i;
  713. int kill = 1;
  714. if (PageReserved(p) || PageSlab(p))
  715. return SWAP_SUCCESS;
  716. /*
  717. * This check implies we don't kill processes if their pages
  718. * are in the swap cache early. Those are always late kills.
  719. */
  720. if (!page_mapped(p))
  721. return SWAP_SUCCESS;
  722. if (PageCompound(p) || PageKsm(p))
  723. return SWAP_FAIL;
  724. if (PageSwapCache(p)) {
  725. printk(KERN_ERR
  726. "MCE %#lx: keeping poisoned page in swap cache\n", pfn);
  727. ttu |= TTU_IGNORE_HWPOISON;
  728. }
  729. /*
  730. * Propagate the dirty bit from PTEs to struct page first, because we
  731. * need this to decide if we should kill or just drop the page.
  732. * XXX: the dirty test could be racy: set_page_dirty() may not always
  733. * be called inside page lock (it's recommended but not enforced).
  734. */
  735. mapping = page_mapping(p);
  736. if (!PageDirty(p) && mapping && mapping_cap_writeback_dirty(mapping)) {
  737. if (page_mkclean(p)) {
  738. SetPageDirty(p);
  739. } else {
  740. kill = 0;
  741. ttu |= TTU_IGNORE_HWPOISON;
  742. printk(KERN_INFO
  743. "MCE %#lx: corrupted page was clean: dropped without side effects\n",
  744. pfn);
  745. }
  746. }
  747. /*
  748. * First collect all the processes that have the page
  749. * mapped in dirty form. This has to be done before try_to_unmap,
  750. * because ttu takes the rmap data structures down.
  751. *
  752. * Error handling: We ignore errors here because
  753. * there's nothing that can be done.
  754. */
  755. if (kill)
  756. collect_procs(p, &tokill);
  757. /*
  758. * try_to_unmap can fail temporarily due to races.
  759. * Try a few times (RED-PEN better strategy?)
  760. */
  761. for (i = 0; i < N_UNMAP_TRIES; i++) {
  762. ret = try_to_unmap(p, ttu);
  763. if (ret == SWAP_SUCCESS)
  764. break;
  765. pr_debug("MCE %#lx: try_to_unmap retry needed %d\n", pfn, ret);
  766. }
  767. if (ret != SWAP_SUCCESS)
  768. printk(KERN_ERR "MCE %#lx: failed to unmap page (mapcount=%d)\n",
  769. pfn, page_mapcount(p));
  770. /*
  771. * Now that the dirty bit has been propagated to the
  772. * struct page and all unmaps done we can decide if
  773. * killing is needed or not. Only kill when the page
  774. * was dirty, otherwise the tokill list is merely
  775. * freed. When there was a problem unmapping earlier
  776. * use a more force-full uncatchable kill to prevent
  777. * any accesses to the poisoned memory.
  778. */
  779. kill_procs_ao(&tokill, !!PageDirty(p), trapno,
  780. ret != SWAP_SUCCESS, pfn);
  781. return ret;
  782. }
  783. int __memory_failure(unsigned long pfn, int trapno, int flags)
  784. {
  785. struct page_state *ps;
  786. struct page *p;
  787. int res;
  788. if (!sysctl_memory_failure_recovery)
  789. panic("Memory failure from trap %d on page %lx", trapno, pfn);
  790. if (!pfn_valid(pfn)) {
  791. printk(KERN_ERR
  792. "MCE %#lx: memory outside kernel control\n",
  793. pfn);
  794. return -ENXIO;
  795. }
  796. p = pfn_to_page(pfn);
  797. if (TestSetPageHWPoison(p)) {
  798. printk(KERN_ERR "MCE %#lx: already hardware poisoned\n", pfn);
  799. return 0;
  800. }
  801. atomic_long_add(1, &mce_bad_pages);
  802. /*
  803. * We need/can do nothing about count=0 pages.
  804. * 1) it's a free page, and therefore in safe hand:
  805. * prep_new_page() will be the gate keeper.
  806. * 2) it's part of a non-compound high order page.
  807. * Implies some kernel user: cannot stop them from
  808. * R/W the page; let's pray that the page has been
  809. * used and will be freed some time later.
  810. * In fact it's dangerous to directly bump up page count from 0,
  811. * that may make page_freeze_refs()/page_unfreeze_refs() mismatch.
  812. */
  813. if (!(flags & MF_COUNT_INCREASED) &&
  814. !get_page_unless_zero(compound_head(p))) {
  815. if (is_free_buddy_page(p)) {
  816. action_result(pfn, "free buddy", DELAYED);
  817. return 0;
  818. } else {
  819. action_result(pfn, "high order kernel", IGNORED);
  820. return -EBUSY;
  821. }
  822. }
  823. /*
  824. * We ignore non-LRU pages for good reasons.
  825. * - PG_locked is only well defined for LRU pages and a few others
  826. * - to avoid races with __set_page_locked()
  827. * - to avoid races with __SetPageSlab*() (and more non-atomic ops)
  828. * The check (unnecessarily) ignores LRU pages being isolated and
  829. * walked by the page reclaim code, however that's not a big loss.
  830. */
  831. if (!PageLRU(p))
  832. lru_add_drain_all();
  833. if (!PageLRU(p)) {
  834. action_result(pfn, "non LRU", IGNORED);
  835. put_page(p);
  836. return -EBUSY;
  837. }
  838. /*
  839. * Lock the page and wait for writeback to finish.
  840. * It's very difficult to mess with pages currently under IO
  841. * and in many cases impossible, so we just avoid it here.
  842. */
  843. lock_page_nosync(p);
  844. /*
  845. * unpoison always clear PG_hwpoison inside page lock
  846. */
  847. if (!PageHWPoison(p)) {
  848. printk(KERN_ERR "MCE %#lx: just unpoisoned\n", pfn);
  849. res = 0;
  850. goto out;
  851. }
  852. if (hwpoison_filter(p)) {
  853. if (TestClearPageHWPoison(p))
  854. atomic_long_dec(&mce_bad_pages);
  855. unlock_page(p);
  856. put_page(p);
  857. return 0;
  858. }
  859. wait_on_page_writeback(p);
  860. /*
  861. * Now take care of user space mappings.
  862. * Abort on fail: __remove_from_page_cache() assumes unmapped page.
  863. */
  864. if (hwpoison_user_mappings(p, pfn, trapno) != SWAP_SUCCESS) {
  865. printk(KERN_ERR "MCE %#lx: cannot unmap page, give up\n", pfn);
  866. res = -EBUSY;
  867. goto out;
  868. }
  869. /*
  870. * Torn down by someone else?
  871. */
  872. if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
  873. action_result(pfn, "already truncated LRU", IGNORED);
  874. res = -EBUSY;
  875. goto out;
  876. }
  877. res = -EBUSY;
  878. for (ps = error_states;; ps++) {
  879. if ((p->flags & ps->mask) == ps->res) {
  880. res = page_action(ps, p, pfn);
  881. break;
  882. }
  883. }
  884. out:
  885. unlock_page(p);
  886. return res;
  887. }
  888. EXPORT_SYMBOL_GPL(__memory_failure);
  889. /**
  890. * memory_failure - Handle memory failure of a page.
  891. * @pfn: Page Number of the corrupted page
  892. * @trapno: Trap number reported in the signal to user space.
  893. *
  894. * This function is called by the low level machine check code
  895. * of an architecture when it detects hardware memory corruption
  896. * of a page. It tries its best to recover, which includes
  897. * dropping pages, killing processes etc.
  898. *
  899. * The function is primarily of use for corruptions that
  900. * happen outside the current execution context (e.g. when
  901. * detected by a background scrubber)
  902. *
  903. * Must run in process context (e.g. a work queue) with interrupts
  904. * enabled and no spinlocks hold.
  905. */
  906. void memory_failure(unsigned long pfn, int trapno)
  907. {
  908. __memory_failure(pfn, trapno, 0);
  909. }
  910. /**
  911. * unpoison_memory - Unpoison a previously poisoned page
  912. * @pfn: Page number of the to be unpoisoned page
  913. *
  914. * Software-unpoison a page that has been poisoned by
  915. * memory_failure() earlier.
  916. *
  917. * This is only done on the software-level, so it only works
  918. * for linux injected failures, not real hardware failures
  919. *
  920. * Returns 0 for success, otherwise -errno.
  921. */
  922. int unpoison_memory(unsigned long pfn)
  923. {
  924. struct page *page;
  925. struct page *p;
  926. int freeit = 0;
  927. if (!pfn_valid(pfn))
  928. return -ENXIO;
  929. p = pfn_to_page(pfn);
  930. page = compound_head(p);
  931. if (!PageHWPoison(p)) {
  932. pr_debug("MCE: Page was already unpoisoned %#lx\n", pfn);
  933. return 0;
  934. }
  935. if (!get_page_unless_zero(page)) {
  936. if (TestClearPageHWPoison(p))
  937. atomic_long_dec(&mce_bad_pages);
  938. pr_debug("MCE: Software-unpoisoned free page %#lx\n", pfn);
  939. return 0;
  940. }
  941. lock_page_nosync(page);
  942. /*
  943. * This test is racy because PG_hwpoison is set outside of page lock.
  944. * That's acceptable because that won't trigger kernel panic. Instead,
  945. * the PG_hwpoison page will be caught and isolated on the entrance to
  946. * the free buddy page pool.
  947. */
  948. if (TestClearPageHWPoison(p)) {
  949. pr_debug("MCE: Software-unpoisoned page %#lx\n", pfn);
  950. atomic_long_dec(&mce_bad_pages);
  951. freeit = 1;
  952. }
  953. unlock_page(page);
  954. put_page(page);
  955. if (freeit)
  956. put_page(page);
  957. return 0;
  958. }
  959. EXPORT_SYMBOL(unpoison_memory);