memory-failure.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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 mce_bad_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_CGROUP_MEM_RES_CTLR_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. /* root_mem_cgroup has NULL dentries */
  131. if (!css->cgroup->dentry)
  132. return -EINVAL;
  133. ino = css->cgroup->dentry->d_inode->i_ino;
  134. css_put(css);
  135. if (ino != hwpoison_filter_memcg)
  136. return -EINVAL;
  137. return 0;
  138. }
  139. #else
  140. static int hwpoison_filter_task(struct page *p) { return 0; }
  141. #endif
  142. int hwpoison_filter(struct page *p)
  143. {
  144. if (!hwpoison_filter_enable)
  145. return 0;
  146. if (hwpoison_filter_dev(p))
  147. return -EINVAL;
  148. if (hwpoison_filter_flags(p))
  149. return -EINVAL;
  150. if (hwpoison_filter_task(p))
  151. return -EINVAL;
  152. return 0;
  153. }
  154. #else
  155. int hwpoison_filter(struct page *p)
  156. {
  157. return 0;
  158. }
  159. #endif
  160. EXPORT_SYMBOL_GPL(hwpoison_filter);
  161. /*
  162. * Send all the processes who have the page mapped a signal.
  163. * ``action optional'' if they are not immediately affected by the error
  164. * ``action required'' if error happened in current execution context
  165. */
  166. static int kill_proc(struct task_struct *t, unsigned long addr, int trapno,
  167. unsigned long pfn, struct page *page, int flags)
  168. {
  169. struct siginfo si;
  170. int ret;
  171. printk(KERN_ERR
  172. "MCE %#lx: Killing %s:%d due to hardware memory corruption\n",
  173. pfn, t->comm, t->pid);
  174. si.si_signo = SIGBUS;
  175. si.si_errno = 0;
  176. si.si_addr = (void *)addr;
  177. #ifdef __ARCH_SI_TRAPNO
  178. si.si_trapno = trapno;
  179. #endif
  180. si.si_addr_lsb = compound_trans_order(compound_head(page)) + PAGE_SHIFT;
  181. if ((flags & MF_ACTION_REQUIRED) && t == current) {
  182. si.si_code = BUS_MCEERR_AR;
  183. ret = force_sig_info(SIGBUS, &si, t);
  184. } else {
  185. /*
  186. * Don't use force here, it's convenient if the signal
  187. * can be temporarily blocked.
  188. * This could cause a loop when the user sets SIGBUS
  189. * to SIG_IGN, but hopefully no one will do that?
  190. */
  191. si.si_code = BUS_MCEERR_AO;
  192. ret = send_sig_info(SIGBUS, &si, t); /* synchronous? */
  193. }
  194. if (ret < 0)
  195. printk(KERN_INFO "MCE: Error sending signal to %s:%d: %d\n",
  196. t->comm, t->pid, ret);
  197. return ret;
  198. }
  199. /*
  200. * When a unknown page type is encountered drain as many buffers as possible
  201. * in the hope to turn the page into a LRU or free page, which we can handle.
  202. */
  203. void shake_page(struct page *p, int access)
  204. {
  205. if (!PageSlab(p)) {
  206. lru_add_drain_all();
  207. if (PageLRU(p))
  208. return;
  209. drain_all_pages();
  210. if (PageLRU(p) || is_free_buddy_page(p))
  211. return;
  212. }
  213. /*
  214. * Only call shrink_slab here (which would also shrink other caches) if
  215. * access is not potentially fatal.
  216. */
  217. if (access) {
  218. int nr;
  219. do {
  220. struct shrink_control shrink = {
  221. .gfp_mask = GFP_KERNEL,
  222. };
  223. nr = shrink_slab(&shrink, 1000, 1000);
  224. if (page_count(p) == 1)
  225. break;
  226. } while (nr > 10);
  227. }
  228. }
  229. EXPORT_SYMBOL_GPL(shake_page);
  230. /*
  231. * Kill all processes that have a poisoned page mapped and then isolate
  232. * the page.
  233. *
  234. * General strategy:
  235. * Find all processes having the page mapped and kill them.
  236. * But we keep a page reference around so that the page is not
  237. * actually freed yet.
  238. * Then stash the page away
  239. *
  240. * There's no convenient way to get back to mapped processes
  241. * from the VMAs. So do a brute-force search over all
  242. * running processes.
  243. *
  244. * Remember that machine checks are not common (or rather
  245. * if they are common you have other problems), so this shouldn't
  246. * be a performance issue.
  247. *
  248. * Also there are some races possible while we get from the
  249. * error detection to actually handle it.
  250. */
  251. struct to_kill {
  252. struct list_head nd;
  253. struct task_struct *tsk;
  254. unsigned long addr;
  255. char addr_valid;
  256. };
  257. /*
  258. * Failure handling: if we can't find or can't kill a process there's
  259. * not much we can do. We just print a message and ignore otherwise.
  260. */
  261. /*
  262. * Schedule a process for later kill.
  263. * Uses GFP_ATOMIC allocations to avoid potential recursions in the VM.
  264. * TBD would GFP_NOIO be enough?
  265. */
  266. static void add_to_kill(struct task_struct *tsk, struct page *p,
  267. struct vm_area_struct *vma,
  268. struct list_head *to_kill,
  269. struct to_kill **tkc)
  270. {
  271. struct to_kill *tk;
  272. if (*tkc) {
  273. tk = *tkc;
  274. *tkc = NULL;
  275. } else {
  276. tk = kmalloc(sizeof(struct to_kill), GFP_ATOMIC);
  277. if (!tk) {
  278. printk(KERN_ERR
  279. "MCE: Out of memory while machine check handling\n");
  280. return;
  281. }
  282. }
  283. tk->addr = page_address_in_vma(p, vma);
  284. tk->addr_valid = 1;
  285. /*
  286. * In theory we don't have to kill when the page was
  287. * munmaped. But it could be also a mremap. Since that's
  288. * likely very rare kill anyways just out of paranoia, but use
  289. * a SIGKILL because the error is not contained anymore.
  290. */
  291. if (tk->addr == -EFAULT) {
  292. pr_info("MCE: Unable to find user space address %lx in %s\n",
  293. page_to_pfn(p), tsk->comm);
  294. tk->addr_valid = 0;
  295. }
  296. get_task_struct(tsk);
  297. tk->tsk = tsk;
  298. list_add_tail(&tk->nd, to_kill);
  299. }
  300. /*
  301. * Kill the processes that have been collected earlier.
  302. *
  303. * Only do anything when DOIT is set, otherwise just free the list
  304. * (this is used for clean pages which do not need killing)
  305. * Also when FAIL is set do a force kill because something went
  306. * wrong earlier.
  307. */
  308. static void kill_procs(struct list_head *to_kill, int doit, int trapno,
  309. int fail, struct page *page, unsigned long pfn,
  310. int flags)
  311. {
  312. struct to_kill *tk, *next;
  313. list_for_each_entry_safe (tk, next, to_kill, nd) {
  314. if (doit) {
  315. /*
  316. * In case something went wrong with munmapping
  317. * make sure the process doesn't catch the
  318. * signal and then access the memory. Just kill it.
  319. */
  320. if (fail || tk->addr_valid == 0) {
  321. printk(KERN_ERR
  322. "MCE %#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
  323. pfn, tk->tsk->comm, tk->tsk->pid);
  324. force_sig(SIGKILL, tk->tsk);
  325. }
  326. /*
  327. * In theory the process could have mapped
  328. * something else on the address in-between. We could
  329. * check for that, but we need to tell the
  330. * process anyways.
  331. */
  332. else if (kill_proc(tk->tsk, tk->addr, trapno,
  333. pfn, page, flags) < 0)
  334. printk(KERN_ERR
  335. "MCE %#lx: Cannot send advisory machine check signal to %s:%d\n",
  336. pfn, tk->tsk->comm, tk->tsk->pid);
  337. }
  338. put_task_struct(tk->tsk);
  339. kfree(tk);
  340. }
  341. }
  342. static int task_early_kill(struct task_struct *tsk)
  343. {
  344. if (!tsk->mm)
  345. return 0;
  346. if (tsk->flags & PF_MCE_PROCESS)
  347. return !!(tsk->flags & PF_MCE_EARLY);
  348. return sysctl_memory_failure_early_kill;
  349. }
  350. /*
  351. * Collect processes when the error hit an anonymous page.
  352. */
  353. static void collect_procs_anon(struct page *page, struct list_head *to_kill,
  354. struct to_kill **tkc)
  355. {
  356. struct vm_area_struct *vma;
  357. struct task_struct *tsk;
  358. struct anon_vma *av;
  359. av = page_lock_anon_vma(page);
  360. if (av == NULL) /* Not actually mapped anymore */
  361. return;
  362. read_lock(&tasklist_lock);
  363. for_each_process (tsk) {
  364. struct anon_vma_chain *vmac;
  365. if (!task_early_kill(tsk))
  366. continue;
  367. list_for_each_entry(vmac, &av->head, same_anon_vma) {
  368. vma = vmac->vma;
  369. if (!page_mapped_in_vma(page, vma))
  370. continue;
  371. if (vma->vm_mm == tsk->mm)
  372. add_to_kill(tsk, page, vma, to_kill, tkc);
  373. }
  374. }
  375. read_unlock(&tasklist_lock);
  376. page_unlock_anon_vma(av);
  377. }
  378. /*
  379. * Collect processes when the error hit a file mapped page.
  380. */
  381. static void collect_procs_file(struct page *page, struct list_head *to_kill,
  382. struct to_kill **tkc)
  383. {
  384. struct vm_area_struct *vma;
  385. struct task_struct *tsk;
  386. struct prio_tree_iter iter;
  387. struct address_space *mapping = page->mapping;
  388. mutex_lock(&mapping->i_mmap_mutex);
  389. read_lock(&tasklist_lock);
  390. for_each_process(tsk) {
  391. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  392. if (!task_early_kill(tsk))
  393. continue;
  394. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff,
  395. pgoff) {
  396. /*
  397. * Send early kill signal to tasks where a vma covers
  398. * the page but the corrupted page is not necessarily
  399. * mapped it in its pte.
  400. * Assume applications who requested early kill want
  401. * to be informed of all such data corruptions.
  402. */
  403. if (vma->vm_mm == tsk->mm)
  404. add_to_kill(tsk, page, vma, to_kill, tkc);
  405. }
  406. }
  407. read_unlock(&tasklist_lock);
  408. mutex_unlock(&mapping->i_mmap_mutex);
  409. }
  410. /*
  411. * Collect the processes who have the corrupted page mapped to kill.
  412. * This is done in two steps for locking reasons.
  413. * First preallocate one tokill structure outside the spin locks,
  414. * so that we can kill at least one process reasonably reliable.
  415. */
  416. static void collect_procs(struct page *page, struct list_head *tokill)
  417. {
  418. struct to_kill *tk;
  419. if (!page->mapping)
  420. return;
  421. tk = kmalloc(sizeof(struct to_kill), GFP_NOIO);
  422. if (!tk)
  423. return;
  424. if (PageAnon(page))
  425. collect_procs_anon(page, tokill, &tk);
  426. else
  427. collect_procs_file(page, tokill, &tk);
  428. kfree(tk);
  429. }
  430. /*
  431. * Error handlers for various types of pages.
  432. */
  433. enum outcome {
  434. IGNORED, /* Error: cannot be handled */
  435. FAILED, /* Error: handling failed */
  436. DELAYED, /* Will be handled later */
  437. RECOVERED, /* Successfully recovered */
  438. };
  439. static const char *action_name[] = {
  440. [IGNORED] = "Ignored",
  441. [FAILED] = "Failed",
  442. [DELAYED] = "Delayed",
  443. [RECOVERED] = "Recovered",
  444. };
  445. /*
  446. * XXX: It is possible that a page is isolated from LRU cache,
  447. * and then kept in swap cache or failed to remove from page cache.
  448. * The page count will stop it from being freed by unpoison.
  449. * Stress tests should be aware of this memory leak problem.
  450. */
  451. static int delete_from_lru_cache(struct page *p)
  452. {
  453. if (!isolate_lru_page(p)) {
  454. /*
  455. * Clear sensible page flags, so that the buddy system won't
  456. * complain when the page is unpoison-and-freed.
  457. */
  458. ClearPageActive(p);
  459. ClearPageUnevictable(p);
  460. /*
  461. * drop the page count elevated by isolate_lru_page()
  462. */
  463. page_cache_release(p);
  464. return 0;
  465. }
  466. return -EIO;
  467. }
  468. /*
  469. * Error hit kernel page.
  470. * Do nothing, try to be lucky and not touch this instead. For a few cases we
  471. * could be more sophisticated.
  472. */
  473. static int me_kernel(struct page *p, unsigned long pfn)
  474. {
  475. return IGNORED;
  476. }
  477. /*
  478. * Page in unknown state. Do nothing.
  479. */
  480. static int me_unknown(struct page *p, unsigned long pfn)
  481. {
  482. printk(KERN_ERR "MCE %#lx: Unknown page state\n", pfn);
  483. return FAILED;
  484. }
  485. /*
  486. * Clean (or cleaned) page cache page.
  487. */
  488. static int me_pagecache_clean(struct page *p, unsigned long pfn)
  489. {
  490. int err;
  491. int ret = FAILED;
  492. struct address_space *mapping;
  493. delete_from_lru_cache(p);
  494. /*
  495. * For anonymous pages we're done the only reference left
  496. * should be the one m_f() holds.
  497. */
  498. if (PageAnon(p))
  499. return RECOVERED;
  500. /*
  501. * Now truncate the page in the page cache. This is really
  502. * more like a "temporary hole punch"
  503. * Don't do this for block devices when someone else
  504. * has a reference, because it could be file system metadata
  505. * and that's not safe to truncate.
  506. */
  507. mapping = page_mapping(p);
  508. if (!mapping) {
  509. /*
  510. * Page has been teared down in the meanwhile
  511. */
  512. return FAILED;
  513. }
  514. /*
  515. * Truncation is a bit tricky. Enable it per file system for now.
  516. *
  517. * Open: to take i_mutex or not for this? Right now we don't.
  518. */
  519. if (mapping->a_ops->error_remove_page) {
  520. err = mapping->a_ops->error_remove_page(mapping, p);
  521. if (err != 0) {
  522. printk(KERN_INFO "MCE %#lx: Failed to punch page: %d\n",
  523. pfn, err);
  524. } else if (page_has_private(p) &&
  525. !try_to_release_page(p, GFP_NOIO)) {
  526. pr_info("MCE %#lx: failed to release buffers\n", pfn);
  527. } else {
  528. ret = RECOVERED;
  529. }
  530. } else {
  531. /*
  532. * If the file system doesn't support it just invalidate
  533. * This fails on dirty or anything with private pages
  534. */
  535. if (invalidate_inode_page(p))
  536. ret = RECOVERED;
  537. else
  538. printk(KERN_INFO "MCE %#lx: Failed to invalidate\n",
  539. pfn);
  540. }
  541. return ret;
  542. }
  543. /*
  544. * Dirty cache page page
  545. * Issues: when the error hit a hole page the error is not properly
  546. * propagated.
  547. */
  548. static int me_pagecache_dirty(struct page *p, unsigned long pfn)
  549. {
  550. struct address_space *mapping = page_mapping(p);
  551. SetPageError(p);
  552. /* TBD: print more information about the file. */
  553. if (mapping) {
  554. /*
  555. * IO error will be reported by write(), fsync(), etc.
  556. * who check the mapping.
  557. * This way the application knows that something went
  558. * wrong with its dirty file data.
  559. *
  560. * There's one open issue:
  561. *
  562. * The EIO will be only reported on the next IO
  563. * operation and then cleared through the IO map.
  564. * Normally Linux has two mechanisms to pass IO error
  565. * first through the AS_EIO flag in the address space
  566. * and then through the PageError flag in the page.
  567. * Since we drop pages on memory failure handling the
  568. * only mechanism open to use is through AS_AIO.
  569. *
  570. * This has the disadvantage that it gets cleared on
  571. * the first operation that returns an error, while
  572. * the PageError bit is more sticky and only cleared
  573. * when the page is reread or dropped. If an
  574. * application assumes it will always get error on
  575. * fsync, but does other operations on the fd before
  576. * and the page is dropped between then the error
  577. * will not be properly reported.
  578. *
  579. * This can already happen even without hwpoisoned
  580. * pages: first on metadata IO errors (which only
  581. * report through AS_EIO) or when the page is dropped
  582. * at the wrong time.
  583. *
  584. * So right now we assume that the application DTRT on
  585. * the first EIO, but we're not worse than other parts
  586. * of the kernel.
  587. */
  588. mapping_set_error(mapping, EIO);
  589. }
  590. return me_pagecache_clean(p, pfn);
  591. }
  592. /*
  593. * Clean and dirty swap cache.
  594. *
  595. * Dirty swap cache page is tricky to handle. The page could live both in page
  596. * cache and swap cache(ie. page is freshly swapped in). So it could be
  597. * referenced concurrently by 2 types of PTEs:
  598. * normal PTEs and swap PTEs. We try to handle them consistently by calling
  599. * try_to_unmap(TTU_IGNORE_HWPOISON) to convert the normal PTEs to swap PTEs,
  600. * and then
  601. * - clear dirty bit to prevent IO
  602. * - remove from LRU
  603. * - but keep in the swap cache, so that when we return to it on
  604. * a later page fault, we know the application is accessing
  605. * corrupted data and shall be killed (we installed simple
  606. * interception code in do_swap_page to catch it).
  607. *
  608. * Clean swap cache pages can be directly isolated. A later page fault will
  609. * bring in the known good data from disk.
  610. */
  611. static int me_swapcache_dirty(struct page *p, unsigned long pfn)
  612. {
  613. ClearPageDirty(p);
  614. /* Trigger EIO in shmem: */
  615. ClearPageUptodate(p);
  616. if (!delete_from_lru_cache(p))
  617. return DELAYED;
  618. else
  619. return FAILED;
  620. }
  621. static int me_swapcache_clean(struct page *p, unsigned long pfn)
  622. {
  623. delete_from_swap_cache(p);
  624. if (!delete_from_lru_cache(p))
  625. return RECOVERED;
  626. else
  627. return FAILED;
  628. }
  629. /*
  630. * Huge pages. Needs work.
  631. * Issues:
  632. * - Error on hugepage is contained in hugepage unit (not in raw page unit.)
  633. * To narrow down kill region to one page, we need to break up pmd.
  634. */
  635. static int me_huge_page(struct page *p, unsigned long pfn)
  636. {
  637. int res = 0;
  638. struct page *hpage = compound_head(p);
  639. /*
  640. * We can safely recover from error on free or reserved (i.e.
  641. * not in-use) hugepage by dequeuing it from freelist.
  642. * To check whether a hugepage is in-use or not, we can't use
  643. * page->lru because it can be used in other hugepage operations,
  644. * such as __unmap_hugepage_range() and gather_surplus_pages().
  645. * So instead we use page_mapping() and PageAnon().
  646. * We assume that this function is called with page lock held,
  647. * so there is no race between isolation and mapping/unmapping.
  648. */
  649. if (!(page_mapping(hpage) || PageAnon(hpage))) {
  650. res = dequeue_hwpoisoned_huge_page(hpage);
  651. if (!res)
  652. return RECOVERED;
  653. }
  654. return DELAYED;
  655. }
  656. /*
  657. * Various page states we can handle.
  658. *
  659. * A page state is defined by its current page->flags bits.
  660. * The table matches them in order and calls the right handler.
  661. *
  662. * This is quite tricky because we can access page at any time
  663. * in its live cycle, so all accesses have to be extremely careful.
  664. *
  665. * This is not complete. More states could be added.
  666. * For any missing state don't attempt recovery.
  667. */
  668. #define dirty (1UL << PG_dirty)
  669. #define sc (1UL << PG_swapcache)
  670. #define unevict (1UL << PG_unevictable)
  671. #define mlock (1UL << PG_mlocked)
  672. #define writeback (1UL << PG_writeback)
  673. #define lru (1UL << PG_lru)
  674. #define swapbacked (1UL << PG_swapbacked)
  675. #define head (1UL << PG_head)
  676. #define tail (1UL << PG_tail)
  677. #define compound (1UL << PG_compound)
  678. #define slab (1UL << PG_slab)
  679. #define reserved (1UL << PG_reserved)
  680. static struct page_state {
  681. unsigned long mask;
  682. unsigned long res;
  683. char *msg;
  684. int (*action)(struct page *p, unsigned long pfn);
  685. } error_states[] = {
  686. { reserved, reserved, "reserved kernel", me_kernel },
  687. /*
  688. * free pages are specially detected outside this table:
  689. * PG_buddy pages only make a small fraction of all free pages.
  690. */
  691. /*
  692. * Could in theory check if slab page is free or if we can drop
  693. * currently unused objects without touching them. But just
  694. * treat it as standard kernel for now.
  695. */
  696. { slab, slab, "kernel slab", me_kernel },
  697. #ifdef CONFIG_PAGEFLAGS_EXTENDED
  698. { head, head, "huge", me_huge_page },
  699. { tail, tail, "huge", me_huge_page },
  700. #else
  701. { compound, compound, "huge", me_huge_page },
  702. #endif
  703. { sc|dirty, sc|dirty, "swapcache", me_swapcache_dirty },
  704. { sc|dirty, sc, "swapcache", me_swapcache_clean },
  705. { unevict|dirty, unevict|dirty, "unevictable LRU", me_pagecache_dirty},
  706. { unevict, unevict, "unevictable LRU", me_pagecache_clean},
  707. { mlock|dirty, mlock|dirty, "mlocked LRU", me_pagecache_dirty },
  708. { mlock, mlock, "mlocked LRU", me_pagecache_clean },
  709. { lru|dirty, lru|dirty, "LRU", me_pagecache_dirty },
  710. { lru|dirty, lru, "clean LRU", me_pagecache_clean },
  711. /*
  712. * Catchall entry: must be at end.
  713. */
  714. { 0, 0, "unknown page state", me_unknown },
  715. };
  716. #undef dirty
  717. #undef sc
  718. #undef unevict
  719. #undef mlock
  720. #undef writeback
  721. #undef lru
  722. #undef swapbacked
  723. #undef head
  724. #undef tail
  725. #undef compound
  726. #undef slab
  727. #undef reserved
  728. static void action_result(unsigned long pfn, char *msg, int result)
  729. {
  730. struct page *page = pfn_to_page(pfn);
  731. printk(KERN_ERR "MCE %#lx: %s%s page recovery: %s\n",
  732. pfn,
  733. PageDirty(page) ? "dirty " : "",
  734. msg, action_name[result]);
  735. }
  736. static int page_action(struct page_state *ps, struct page *p,
  737. unsigned long pfn)
  738. {
  739. int result;
  740. int count;
  741. result = ps->action(p, pfn);
  742. action_result(pfn, ps->msg, result);
  743. count = page_count(p) - 1;
  744. if (ps->action == me_swapcache_dirty && result == DELAYED)
  745. count--;
  746. if (count != 0) {
  747. printk(KERN_ERR
  748. "MCE %#lx: %s page still referenced by %d users\n",
  749. pfn, ps->msg, count);
  750. result = FAILED;
  751. }
  752. /* Could do more checks here if page looks ok */
  753. /*
  754. * Could adjust zone counters here to correct for the missing page.
  755. */
  756. return (result == RECOVERED || result == DELAYED) ? 0 : -EBUSY;
  757. }
  758. /*
  759. * Do all that is necessary to remove user space mappings. Unmap
  760. * the pages and send SIGBUS to the processes if the data was dirty.
  761. */
  762. static int hwpoison_user_mappings(struct page *p, unsigned long pfn,
  763. int trapno, int flags)
  764. {
  765. enum ttu_flags ttu = TTU_UNMAP | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
  766. struct address_space *mapping;
  767. LIST_HEAD(tokill);
  768. int ret;
  769. int kill = 1;
  770. struct page *hpage = compound_head(p);
  771. struct page *ppage;
  772. if (PageReserved(p) || PageSlab(p))
  773. return SWAP_SUCCESS;
  774. /*
  775. * This check implies we don't kill processes if their pages
  776. * are in the swap cache early. Those are always late kills.
  777. */
  778. if (!page_mapped(hpage))
  779. return SWAP_SUCCESS;
  780. if (PageKsm(p))
  781. return SWAP_FAIL;
  782. if (PageSwapCache(p)) {
  783. printk(KERN_ERR
  784. "MCE %#lx: keeping poisoned page in swap cache\n", pfn);
  785. ttu |= TTU_IGNORE_HWPOISON;
  786. }
  787. /*
  788. * Propagate the dirty bit from PTEs to struct page first, because we
  789. * need this to decide if we should kill or just drop the page.
  790. * XXX: the dirty test could be racy: set_page_dirty() may not always
  791. * be called inside page lock (it's recommended but not enforced).
  792. */
  793. mapping = page_mapping(hpage);
  794. if (!PageDirty(hpage) && mapping &&
  795. mapping_cap_writeback_dirty(mapping)) {
  796. if (page_mkclean(hpage)) {
  797. SetPageDirty(hpage);
  798. } else {
  799. kill = 0;
  800. ttu |= TTU_IGNORE_HWPOISON;
  801. printk(KERN_INFO
  802. "MCE %#lx: corrupted page was clean: dropped without side effects\n",
  803. pfn);
  804. }
  805. }
  806. /*
  807. * ppage: poisoned page
  808. * if p is regular page(4k page)
  809. * ppage == real poisoned page;
  810. * else p is hugetlb or THP, ppage == head page.
  811. */
  812. ppage = hpage;
  813. if (PageTransHuge(hpage)) {
  814. /*
  815. * Verify that this isn't a hugetlbfs head page, the check for
  816. * PageAnon is just for avoid tripping a split_huge_page
  817. * internal debug check, as split_huge_page refuses to deal with
  818. * anything that isn't an anon page. PageAnon can't go away fro
  819. * under us because we hold a refcount on the hpage, without a
  820. * refcount on the hpage. split_huge_page can't be safely called
  821. * in the first place, having a refcount on the tail isn't
  822. * enough * to be safe.
  823. */
  824. if (!PageHuge(hpage) && PageAnon(hpage)) {
  825. if (unlikely(split_huge_page(hpage))) {
  826. /*
  827. * FIXME: if splitting THP is failed, it is
  828. * better to stop the following operation rather
  829. * than causing panic by unmapping. System might
  830. * survive if the page is freed later.
  831. */
  832. printk(KERN_INFO
  833. "MCE %#lx: failed to split THP\n", pfn);
  834. BUG_ON(!PageHWPoison(p));
  835. return SWAP_FAIL;
  836. }
  837. /* THP is split, so ppage should be the real poisoned page. */
  838. ppage = p;
  839. }
  840. }
  841. /*
  842. * First collect all the processes that have the page
  843. * mapped in dirty form. This has to be done before try_to_unmap,
  844. * because ttu takes the rmap data structures down.
  845. *
  846. * Error handling: We ignore errors here because
  847. * there's nothing that can be done.
  848. */
  849. if (kill)
  850. collect_procs(ppage, &tokill);
  851. if (hpage != ppage)
  852. lock_page(ppage);
  853. ret = try_to_unmap(ppage, ttu);
  854. if (ret != SWAP_SUCCESS)
  855. printk(KERN_ERR "MCE %#lx: failed to unmap page (mapcount=%d)\n",
  856. pfn, page_mapcount(ppage));
  857. if (hpage != ppage)
  858. unlock_page(ppage);
  859. /*
  860. * Now that the dirty bit has been propagated to the
  861. * struct page and all unmaps done we can decide if
  862. * killing is needed or not. Only kill when the page
  863. * was dirty, otherwise the tokill list is merely
  864. * freed. When there was a problem unmapping earlier
  865. * use a more force-full uncatchable kill to prevent
  866. * any accesses to the poisoned memory.
  867. */
  868. kill_procs(&tokill, !!PageDirty(ppage), trapno,
  869. ret != SWAP_SUCCESS, p, pfn, flags);
  870. return ret;
  871. }
  872. static void set_page_hwpoison_huge_page(struct page *hpage)
  873. {
  874. int i;
  875. int nr_pages = 1 << compound_trans_order(hpage);
  876. for (i = 0; i < nr_pages; i++)
  877. SetPageHWPoison(hpage + i);
  878. }
  879. static void clear_page_hwpoison_huge_page(struct page *hpage)
  880. {
  881. int i;
  882. int nr_pages = 1 << compound_trans_order(hpage);
  883. for (i = 0; i < nr_pages; i++)
  884. ClearPageHWPoison(hpage + i);
  885. }
  886. /**
  887. * memory_failure - Handle memory failure of a page.
  888. * @pfn: Page Number of the corrupted page
  889. * @trapno: Trap number reported in the signal to user space.
  890. * @flags: fine tune action taken
  891. *
  892. * This function is called by the low level machine check code
  893. * of an architecture when it detects hardware memory corruption
  894. * of a page. It tries its best to recover, which includes
  895. * dropping pages, killing processes etc.
  896. *
  897. * The function is primarily of use for corruptions that
  898. * happen outside the current execution context (e.g. when
  899. * detected by a background scrubber)
  900. *
  901. * Must run in process context (e.g. a work queue) with interrupts
  902. * enabled and no spinlocks hold.
  903. */
  904. int memory_failure(unsigned long pfn, int trapno, int flags)
  905. {
  906. struct page_state *ps;
  907. struct page *p;
  908. struct page *hpage;
  909. int res;
  910. unsigned int nr_pages;
  911. if (!sysctl_memory_failure_recovery)
  912. panic("Memory failure from trap %d on page %lx", trapno, pfn);
  913. if (!pfn_valid(pfn)) {
  914. printk(KERN_ERR
  915. "MCE %#lx: memory outside kernel control\n",
  916. pfn);
  917. return -ENXIO;
  918. }
  919. p = pfn_to_page(pfn);
  920. hpage = compound_head(p);
  921. if (TestSetPageHWPoison(p)) {
  922. printk(KERN_ERR "MCE %#lx: already hardware poisoned\n", pfn);
  923. return 0;
  924. }
  925. nr_pages = 1 << compound_trans_order(hpage);
  926. atomic_long_add(nr_pages, &mce_bad_pages);
  927. /*
  928. * We need/can do nothing about count=0 pages.
  929. * 1) it's a free page, and therefore in safe hand:
  930. * prep_new_page() will be the gate keeper.
  931. * 2) it's a free hugepage, which is also safe:
  932. * an affected hugepage will be dequeued from hugepage freelist,
  933. * so there's no concern about reusing it ever after.
  934. * 3) it's part of a non-compound high order page.
  935. * Implies some kernel user: cannot stop them from
  936. * R/W the page; let's pray that the page has been
  937. * used and will be freed some time later.
  938. * In fact it's dangerous to directly bump up page count from 0,
  939. * that may make page_freeze_refs()/page_unfreeze_refs() mismatch.
  940. */
  941. if (!(flags & MF_COUNT_INCREASED) &&
  942. !get_page_unless_zero(hpage)) {
  943. if (is_free_buddy_page(p)) {
  944. action_result(pfn, "free buddy", DELAYED);
  945. return 0;
  946. } else if (PageHuge(hpage)) {
  947. /*
  948. * Check "just unpoisoned", "filter hit", and
  949. * "race with other subpage."
  950. */
  951. lock_page(hpage);
  952. if (!PageHWPoison(hpage)
  953. || (hwpoison_filter(p) && TestClearPageHWPoison(p))
  954. || (p != hpage && TestSetPageHWPoison(hpage))) {
  955. atomic_long_sub(nr_pages, &mce_bad_pages);
  956. return 0;
  957. }
  958. set_page_hwpoison_huge_page(hpage);
  959. res = dequeue_hwpoisoned_huge_page(hpage);
  960. action_result(pfn, "free huge",
  961. res ? IGNORED : DELAYED);
  962. unlock_page(hpage);
  963. return res;
  964. } else {
  965. action_result(pfn, "high order kernel", IGNORED);
  966. return -EBUSY;
  967. }
  968. }
  969. /*
  970. * We ignore non-LRU pages for good reasons.
  971. * - PG_locked is only well defined for LRU pages and a few others
  972. * - to avoid races with __set_page_locked()
  973. * - to avoid races with __SetPageSlab*() (and more non-atomic ops)
  974. * The check (unnecessarily) ignores LRU pages being isolated and
  975. * walked by the page reclaim code, however that's not a big loss.
  976. */
  977. if (!PageHuge(p) && !PageTransTail(p)) {
  978. if (!PageLRU(p))
  979. shake_page(p, 0);
  980. if (!PageLRU(p)) {
  981. /*
  982. * shake_page could have turned it free.
  983. */
  984. if (is_free_buddy_page(p)) {
  985. action_result(pfn, "free buddy, 2nd try",
  986. DELAYED);
  987. return 0;
  988. }
  989. action_result(pfn, "non LRU", IGNORED);
  990. put_page(p);
  991. return -EBUSY;
  992. }
  993. }
  994. /*
  995. * Lock the page and wait for writeback to finish.
  996. * It's very difficult to mess with pages currently under IO
  997. * and in many cases impossible, so we just avoid it here.
  998. */
  999. lock_page(hpage);
  1000. /*
  1001. * unpoison always clear PG_hwpoison inside page lock
  1002. */
  1003. if (!PageHWPoison(p)) {
  1004. printk(KERN_ERR "MCE %#lx: just unpoisoned\n", pfn);
  1005. res = 0;
  1006. goto out;
  1007. }
  1008. if (hwpoison_filter(p)) {
  1009. if (TestClearPageHWPoison(p))
  1010. atomic_long_sub(nr_pages, &mce_bad_pages);
  1011. unlock_page(hpage);
  1012. put_page(hpage);
  1013. return 0;
  1014. }
  1015. /*
  1016. * For error on the tail page, we should set PG_hwpoison
  1017. * on the head page to show that the hugepage is hwpoisoned
  1018. */
  1019. if (PageHuge(p) && PageTail(p) && TestSetPageHWPoison(hpage)) {
  1020. action_result(pfn, "hugepage already hardware poisoned",
  1021. IGNORED);
  1022. unlock_page(hpage);
  1023. put_page(hpage);
  1024. return 0;
  1025. }
  1026. /*
  1027. * Set PG_hwpoison on all pages in an error hugepage,
  1028. * because containment is done in hugepage unit for now.
  1029. * Since we have done TestSetPageHWPoison() for the head page with
  1030. * page lock held, we can safely set PG_hwpoison bits on tail pages.
  1031. */
  1032. if (PageHuge(p))
  1033. set_page_hwpoison_huge_page(hpage);
  1034. wait_on_page_writeback(p);
  1035. /*
  1036. * Now take care of user space mappings.
  1037. * Abort on fail: __delete_from_page_cache() assumes unmapped page.
  1038. */
  1039. if (hwpoison_user_mappings(p, pfn, trapno, flags) != SWAP_SUCCESS) {
  1040. printk(KERN_ERR "MCE %#lx: cannot unmap page, give up\n", pfn);
  1041. res = -EBUSY;
  1042. goto out;
  1043. }
  1044. /*
  1045. * Torn down by someone else?
  1046. */
  1047. if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
  1048. action_result(pfn, "already truncated LRU", IGNORED);
  1049. res = -EBUSY;
  1050. goto out;
  1051. }
  1052. res = -EBUSY;
  1053. for (ps = error_states;; ps++) {
  1054. if ((p->flags & ps->mask) == ps->res) {
  1055. res = page_action(ps, p, pfn);
  1056. break;
  1057. }
  1058. }
  1059. out:
  1060. unlock_page(hpage);
  1061. return res;
  1062. }
  1063. EXPORT_SYMBOL_GPL(memory_failure);
  1064. #define MEMORY_FAILURE_FIFO_ORDER 4
  1065. #define MEMORY_FAILURE_FIFO_SIZE (1 << MEMORY_FAILURE_FIFO_ORDER)
  1066. struct memory_failure_entry {
  1067. unsigned long pfn;
  1068. int trapno;
  1069. int flags;
  1070. };
  1071. struct memory_failure_cpu {
  1072. DECLARE_KFIFO(fifo, struct memory_failure_entry,
  1073. MEMORY_FAILURE_FIFO_SIZE);
  1074. spinlock_t lock;
  1075. struct work_struct work;
  1076. };
  1077. static DEFINE_PER_CPU(struct memory_failure_cpu, memory_failure_cpu);
  1078. /**
  1079. * memory_failure_queue - Schedule handling memory failure of a page.
  1080. * @pfn: Page Number of the corrupted page
  1081. * @trapno: Trap number reported in the signal to user space.
  1082. * @flags: Flags for memory failure handling
  1083. *
  1084. * This function is called by the low level hardware error handler
  1085. * when it detects hardware memory corruption of a page. It schedules
  1086. * the recovering of error page, including dropping pages, killing
  1087. * processes etc.
  1088. *
  1089. * The function is primarily of use for corruptions that
  1090. * happen outside the current execution context (e.g. when
  1091. * detected by a background scrubber)
  1092. *
  1093. * Can run in IRQ context.
  1094. */
  1095. void memory_failure_queue(unsigned long pfn, int trapno, int flags)
  1096. {
  1097. struct memory_failure_cpu *mf_cpu;
  1098. unsigned long proc_flags;
  1099. struct memory_failure_entry entry = {
  1100. .pfn = pfn,
  1101. .trapno = trapno,
  1102. .flags = flags,
  1103. };
  1104. mf_cpu = &get_cpu_var(memory_failure_cpu);
  1105. spin_lock_irqsave(&mf_cpu->lock, proc_flags);
  1106. if (kfifo_put(&mf_cpu->fifo, &entry))
  1107. schedule_work_on(smp_processor_id(), &mf_cpu->work);
  1108. else
  1109. pr_err("Memory failure: buffer overflow when queuing memory failure at 0x%#lx\n",
  1110. pfn);
  1111. spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
  1112. put_cpu_var(memory_failure_cpu);
  1113. }
  1114. EXPORT_SYMBOL_GPL(memory_failure_queue);
  1115. static void memory_failure_work_func(struct work_struct *work)
  1116. {
  1117. struct memory_failure_cpu *mf_cpu;
  1118. struct memory_failure_entry entry = { 0, };
  1119. unsigned long proc_flags;
  1120. int gotten;
  1121. mf_cpu = &__get_cpu_var(memory_failure_cpu);
  1122. for (;;) {
  1123. spin_lock_irqsave(&mf_cpu->lock, proc_flags);
  1124. gotten = kfifo_get(&mf_cpu->fifo, &entry);
  1125. spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
  1126. if (!gotten)
  1127. break;
  1128. memory_failure(entry.pfn, entry.trapno, entry.flags);
  1129. }
  1130. }
  1131. static int __init memory_failure_init(void)
  1132. {
  1133. struct memory_failure_cpu *mf_cpu;
  1134. int cpu;
  1135. for_each_possible_cpu(cpu) {
  1136. mf_cpu = &per_cpu(memory_failure_cpu, cpu);
  1137. spin_lock_init(&mf_cpu->lock);
  1138. INIT_KFIFO(mf_cpu->fifo);
  1139. INIT_WORK(&mf_cpu->work, memory_failure_work_func);
  1140. }
  1141. return 0;
  1142. }
  1143. core_initcall(memory_failure_init);
  1144. /**
  1145. * unpoison_memory - Unpoison a previously poisoned page
  1146. * @pfn: Page number of the to be unpoisoned page
  1147. *
  1148. * Software-unpoison a page that has been poisoned by
  1149. * memory_failure() earlier.
  1150. *
  1151. * This is only done on the software-level, so it only works
  1152. * for linux injected failures, not real hardware failures
  1153. *
  1154. * Returns 0 for success, otherwise -errno.
  1155. */
  1156. int unpoison_memory(unsigned long pfn)
  1157. {
  1158. struct page *page;
  1159. struct page *p;
  1160. int freeit = 0;
  1161. unsigned int nr_pages;
  1162. if (!pfn_valid(pfn))
  1163. return -ENXIO;
  1164. p = pfn_to_page(pfn);
  1165. page = compound_head(p);
  1166. if (!PageHWPoison(p)) {
  1167. pr_info("MCE: Page was already unpoisoned %#lx\n", pfn);
  1168. return 0;
  1169. }
  1170. nr_pages = 1 << compound_trans_order(page);
  1171. if (!get_page_unless_zero(page)) {
  1172. /*
  1173. * Since HWPoisoned hugepage should have non-zero refcount,
  1174. * race between memory failure and unpoison seems to happen.
  1175. * In such case unpoison fails and memory failure runs
  1176. * to the end.
  1177. */
  1178. if (PageHuge(page)) {
  1179. pr_info("MCE: Memory failure is now running on free hugepage %#lx\n", pfn);
  1180. return 0;
  1181. }
  1182. if (TestClearPageHWPoison(p))
  1183. atomic_long_sub(nr_pages, &mce_bad_pages);
  1184. pr_info("MCE: Software-unpoisoned free page %#lx\n", pfn);
  1185. return 0;
  1186. }
  1187. lock_page(page);
  1188. /*
  1189. * This test is racy because PG_hwpoison is set outside of page lock.
  1190. * That's acceptable because that won't trigger kernel panic. Instead,
  1191. * the PG_hwpoison page will be caught and isolated on the entrance to
  1192. * the free buddy page pool.
  1193. */
  1194. if (TestClearPageHWPoison(page)) {
  1195. pr_info("MCE: Software-unpoisoned page %#lx\n", pfn);
  1196. atomic_long_sub(nr_pages, &mce_bad_pages);
  1197. freeit = 1;
  1198. if (PageHuge(page))
  1199. clear_page_hwpoison_huge_page(page);
  1200. }
  1201. unlock_page(page);
  1202. put_page(page);
  1203. if (freeit)
  1204. put_page(page);
  1205. return 0;
  1206. }
  1207. EXPORT_SYMBOL(unpoison_memory);
  1208. static struct page *new_page(struct page *p, unsigned long private, int **x)
  1209. {
  1210. int nid = page_to_nid(p);
  1211. if (PageHuge(p))
  1212. return alloc_huge_page_node(page_hstate(compound_head(p)),
  1213. nid);
  1214. else
  1215. return alloc_pages_exact_node(nid, GFP_HIGHUSER_MOVABLE, 0);
  1216. }
  1217. /*
  1218. * Safely get reference count of an arbitrary page.
  1219. * Returns 0 for a free page, -EIO for a zero refcount page
  1220. * that is not free, and 1 for any other page type.
  1221. * For 1 the page is returned with increased page count, otherwise not.
  1222. */
  1223. static int get_any_page(struct page *p, unsigned long pfn, int flags)
  1224. {
  1225. int ret;
  1226. if (flags & MF_COUNT_INCREASED)
  1227. return 1;
  1228. /*
  1229. * The lock_memory_hotplug prevents a race with memory hotplug.
  1230. * This is a big hammer, a better would be nicer.
  1231. */
  1232. lock_memory_hotplug();
  1233. /*
  1234. * Isolate the page, so that it doesn't get reallocated if it
  1235. * was free.
  1236. */
  1237. set_migratetype_isolate(p);
  1238. /*
  1239. * When the target page is a free hugepage, just remove it
  1240. * from free hugepage list.
  1241. */
  1242. if (!get_page_unless_zero(compound_head(p))) {
  1243. if (PageHuge(p)) {
  1244. pr_info("get_any_page: %#lx free huge page\n", pfn);
  1245. ret = dequeue_hwpoisoned_huge_page(compound_head(p));
  1246. } else if (is_free_buddy_page(p)) {
  1247. pr_info("get_any_page: %#lx free buddy page\n", pfn);
  1248. /* Set hwpoison bit while page is still isolated */
  1249. SetPageHWPoison(p);
  1250. ret = 0;
  1251. } else {
  1252. pr_info("get_any_page: %#lx: unknown zero refcount page type %lx\n",
  1253. pfn, p->flags);
  1254. ret = -EIO;
  1255. }
  1256. } else {
  1257. /* Not a free page */
  1258. ret = 1;
  1259. }
  1260. unset_migratetype_isolate(p);
  1261. unlock_memory_hotplug();
  1262. return ret;
  1263. }
  1264. static int soft_offline_huge_page(struct page *page, int flags)
  1265. {
  1266. int ret;
  1267. unsigned long pfn = page_to_pfn(page);
  1268. struct page *hpage = compound_head(page);
  1269. LIST_HEAD(pagelist);
  1270. ret = get_any_page(page, pfn, flags);
  1271. if (ret < 0)
  1272. return ret;
  1273. if (ret == 0)
  1274. goto done;
  1275. if (PageHWPoison(hpage)) {
  1276. put_page(hpage);
  1277. pr_info("soft offline: %#lx hugepage already poisoned\n", pfn);
  1278. return -EBUSY;
  1279. }
  1280. /* Keep page count to indicate a given hugepage is isolated. */
  1281. list_add(&hpage->lru, &pagelist);
  1282. ret = migrate_huge_pages(&pagelist, new_page, MPOL_MF_MOVE_ALL, 0,
  1283. true);
  1284. if (ret) {
  1285. struct page *page1, *page2;
  1286. list_for_each_entry_safe(page1, page2, &pagelist, lru)
  1287. put_page(page1);
  1288. pr_info("soft offline: %#lx: migration failed %d, type %lx\n",
  1289. pfn, ret, page->flags);
  1290. if (ret > 0)
  1291. ret = -EIO;
  1292. return ret;
  1293. }
  1294. done:
  1295. if (!PageHWPoison(hpage))
  1296. atomic_long_add(1 << compound_trans_order(hpage), &mce_bad_pages);
  1297. set_page_hwpoison_huge_page(hpage);
  1298. dequeue_hwpoisoned_huge_page(hpage);
  1299. /* keep elevated page count for bad page */
  1300. return ret;
  1301. }
  1302. /**
  1303. * soft_offline_page - Soft offline a page.
  1304. * @page: page to offline
  1305. * @flags: flags. Same as memory_failure().
  1306. *
  1307. * Returns 0 on success, otherwise negated errno.
  1308. *
  1309. * Soft offline a page, by migration or invalidation,
  1310. * without killing anything. This is for the case when
  1311. * a page is not corrupted yet (so it's still valid to access),
  1312. * but has had a number of corrected errors and is better taken
  1313. * out.
  1314. *
  1315. * The actual policy on when to do that is maintained by
  1316. * user space.
  1317. *
  1318. * This should never impact any application or cause data loss,
  1319. * however it might take some time.
  1320. *
  1321. * This is not a 100% solution for all memory, but tries to be
  1322. * ``good enough'' for the majority of memory.
  1323. */
  1324. int soft_offline_page(struct page *page, int flags)
  1325. {
  1326. int ret;
  1327. unsigned long pfn = page_to_pfn(page);
  1328. if (PageHuge(page))
  1329. return soft_offline_huge_page(page, flags);
  1330. ret = get_any_page(page, pfn, flags);
  1331. if (ret < 0)
  1332. return ret;
  1333. if (ret == 0)
  1334. goto done;
  1335. /*
  1336. * Page cache page we can handle?
  1337. */
  1338. if (!PageLRU(page)) {
  1339. /*
  1340. * Try to free it.
  1341. */
  1342. put_page(page);
  1343. shake_page(page, 1);
  1344. /*
  1345. * Did it turn free?
  1346. */
  1347. ret = get_any_page(page, pfn, 0);
  1348. if (ret < 0)
  1349. return ret;
  1350. if (ret == 0)
  1351. goto done;
  1352. }
  1353. if (!PageLRU(page)) {
  1354. pr_info("soft_offline: %#lx: unknown non LRU page type %lx\n",
  1355. pfn, page->flags);
  1356. return -EIO;
  1357. }
  1358. lock_page(page);
  1359. wait_on_page_writeback(page);
  1360. /*
  1361. * Synchronized using the page lock with memory_failure()
  1362. */
  1363. if (PageHWPoison(page)) {
  1364. unlock_page(page);
  1365. put_page(page);
  1366. pr_info("soft offline: %#lx page already poisoned\n", pfn);
  1367. return -EBUSY;
  1368. }
  1369. /*
  1370. * Try to invalidate first. This should work for
  1371. * non dirty unmapped page cache pages.
  1372. */
  1373. ret = invalidate_inode_page(page);
  1374. unlock_page(page);
  1375. /*
  1376. * RED-PEN would be better to keep it isolated here, but we
  1377. * would need to fix isolation locking first.
  1378. */
  1379. if (ret == 1) {
  1380. put_page(page);
  1381. ret = 0;
  1382. pr_info("soft_offline: %#lx: invalidated\n", pfn);
  1383. goto done;
  1384. }
  1385. /*
  1386. * Simple invalidation didn't work.
  1387. * Try to migrate to a new page instead. migrate.c
  1388. * handles a large number of cases for us.
  1389. */
  1390. ret = isolate_lru_page(page);
  1391. /*
  1392. * Drop page reference which is came from get_any_page()
  1393. * successful isolate_lru_page() already took another one.
  1394. */
  1395. put_page(page);
  1396. if (!ret) {
  1397. LIST_HEAD(pagelist);
  1398. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1399. page_is_file_cache(page));
  1400. list_add(&page->lru, &pagelist);
  1401. ret = migrate_pages(&pagelist, new_page, MPOL_MF_MOVE_ALL,
  1402. 0, MIGRATE_SYNC);
  1403. if (ret) {
  1404. putback_lru_pages(&pagelist);
  1405. pr_info("soft offline: %#lx: migration failed %d, type %lx\n",
  1406. pfn, ret, page->flags);
  1407. if (ret > 0)
  1408. ret = -EIO;
  1409. }
  1410. } else {
  1411. pr_info("soft offline: %#lx: isolation failed: %d, page count %d, type %lx\n",
  1412. pfn, ret, page_count(page), page->flags);
  1413. }
  1414. if (ret)
  1415. return ret;
  1416. done:
  1417. atomic_long_add(1, &mce_bad_pages);
  1418. SetPageHWPoison(page);
  1419. /* keep elevated page count for bad page */
  1420. return ret;
  1421. }