memory-failure.c 49 KB

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