memory-failure.c 34 KB

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