memory-failure.c 42 KB

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