memory-failure.c 48 KB

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