memory-failure.c 49 KB

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