memory-failure.c 48 KB

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