memory-failure.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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/signal.h>
  44. #include <linux/sched/task.h>
  45. #include <linux/ksm.h>
  46. #include <linux/rmap.h>
  47. #include <linux/export.h>
  48. #include <linux/pagemap.h>
  49. #include <linux/swap.h>
  50. #include <linux/backing-dev.h>
  51. #include <linux/migrate.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("Memory failure: %#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("Memory failure: 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 (PageHuge(p))
  197. return;
  198. if (!PageSlab(p)) {
  199. lru_add_drain_all();
  200. if (PageLRU(p))
  201. return;
  202. drain_all_pages(page_zone(p));
  203. if (PageLRU(p) || is_free_buddy_page(p))
  204. return;
  205. }
  206. /*
  207. * Only call shrink_node_slabs here (which would also shrink
  208. * other caches) if access is not potentially fatal.
  209. */
  210. if (access)
  211. drop_slab_node(page_to_nid(p));
  212. }
  213. EXPORT_SYMBOL_GPL(shake_page);
  214. /*
  215. * Kill all processes that have a poisoned page mapped and then isolate
  216. * the page.
  217. *
  218. * General strategy:
  219. * Find all processes having the page mapped and kill them.
  220. * But we keep a page reference around so that the page is not
  221. * actually freed yet.
  222. * Then stash the page away
  223. *
  224. * There's no convenient way to get back to mapped processes
  225. * from the VMAs. So do a brute-force search over all
  226. * running processes.
  227. *
  228. * Remember that machine checks are not common (or rather
  229. * if they are common you have other problems), so this shouldn't
  230. * be a performance issue.
  231. *
  232. * Also there are some races possible while we get from the
  233. * error detection to actually handle it.
  234. */
  235. struct to_kill {
  236. struct list_head nd;
  237. struct task_struct *tsk;
  238. unsigned long addr;
  239. char addr_valid;
  240. };
  241. /*
  242. * Failure handling: if we can't find or can't kill a process there's
  243. * not much we can do. We just print a message and ignore otherwise.
  244. */
  245. /*
  246. * Schedule a process for later kill.
  247. * Uses GFP_ATOMIC allocations to avoid potential recursions in the VM.
  248. * TBD would GFP_NOIO be enough?
  249. */
  250. static void add_to_kill(struct task_struct *tsk, struct page *p,
  251. struct vm_area_struct *vma,
  252. struct list_head *to_kill,
  253. struct to_kill **tkc)
  254. {
  255. struct to_kill *tk;
  256. if (*tkc) {
  257. tk = *tkc;
  258. *tkc = NULL;
  259. } else {
  260. tk = kmalloc(sizeof(struct to_kill), GFP_ATOMIC);
  261. if (!tk) {
  262. pr_err("Memory failure: Out of memory while machine check handling\n");
  263. return;
  264. }
  265. }
  266. tk->addr = page_address_in_vma(p, vma);
  267. tk->addr_valid = 1;
  268. /*
  269. * In theory we don't have to kill when the page was
  270. * munmaped. But it could be also a mremap. Since that's
  271. * likely very rare kill anyways just out of paranoia, but use
  272. * a SIGKILL because the error is not contained anymore.
  273. */
  274. if (tk->addr == -EFAULT) {
  275. pr_info("Memory failure: Unable to find user space address %lx in %s\n",
  276. page_to_pfn(p), tsk->comm);
  277. tk->addr_valid = 0;
  278. }
  279. get_task_struct(tsk);
  280. tk->tsk = tsk;
  281. list_add_tail(&tk->nd, to_kill);
  282. }
  283. /*
  284. * Kill the processes that have been collected earlier.
  285. *
  286. * Only do anything when DOIT is set, otherwise just free the list
  287. * (this is used for clean pages which do not need killing)
  288. * Also when FAIL is set do a force kill because something went
  289. * wrong earlier.
  290. */
  291. static void kill_procs(struct list_head *to_kill, int forcekill, int trapno,
  292. bool fail, struct page *page, unsigned long pfn,
  293. int flags)
  294. {
  295. struct to_kill *tk, *next;
  296. list_for_each_entry_safe (tk, next, to_kill, nd) {
  297. if (forcekill) {
  298. /*
  299. * In case something went wrong with munmapping
  300. * make sure the process doesn't catch the
  301. * signal and then access the memory. Just kill it.
  302. */
  303. if (fail || tk->addr_valid == 0) {
  304. pr_err("Memory failure: %#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
  305. pfn, tk->tsk->comm, tk->tsk->pid);
  306. do_send_sig_info(SIGKILL, SEND_SIG_PRIV,
  307. tk->tsk, PIDTYPE_PID);
  308. }
  309. /*
  310. * In theory the process could have mapped
  311. * something else on the address in-between. We could
  312. * check for that, but we need to tell the
  313. * process anyways.
  314. */
  315. else if (kill_proc(tk->tsk, tk->addr, trapno,
  316. pfn, page, flags) < 0)
  317. pr_err("Memory failure: %#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_NON_PMD_HUGE] = "non-pmd-sized huge page",
  461. [MF_MSG_UNMAP_FAILED] = "unmapping failed page",
  462. [MF_MSG_DIRTY_SWAPCACHE] = "dirty swapcache page",
  463. [MF_MSG_CLEAN_SWAPCACHE] = "clean swapcache page",
  464. [MF_MSG_DIRTY_MLOCKED_LRU] = "dirty mlocked LRU page",
  465. [MF_MSG_CLEAN_MLOCKED_LRU] = "clean mlocked LRU page",
  466. [MF_MSG_DIRTY_UNEVICTABLE_LRU] = "dirty unevictable LRU page",
  467. [MF_MSG_CLEAN_UNEVICTABLE_LRU] = "clean unevictable LRU page",
  468. [MF_MSG_DIRTY_LRU] = "dirty LRU page",
  469. [MF_MSG_CLEAN_LRU] = "clean LRU page",
  470. [MF_MSG_TRUNCATED_LRU] = "already truncated LRU page",
  471. [MF_MSG_BUDDY] = "free buddy page",
  472. [MF_MSG_BUDDY_2ND] = "free buddy page (2nd try)",
  473. [MF_MSG_UNKNOWN] = "unknown page",
  474. };
  475. /*
  476. * XXX: It is possible that a page is isolated from LRU cache,
  477. * and then kept in swap cache or failed to remove from page cache.
  478. * The page count will stop it from being freed by unpoison.
  479. * Stress tests should be aware of this memory leak problem.
  480. */
  481. static int delete_from_lru_cache(struct page *p)
  482. {
  483. if (!isolate_lru_page(p)) {
  484. /*
  485. * Clear sensible page flags, so that the buddy system won't
  486. * complain when the page is unpoison-and-freed.
  487. */
  488. ClearPageActive(p);
  489. ClearPageUnevictable(p);
  490. /*
  491. * Poisoned page might never drop its ref count to 0 so we have
  492. * to uncharge it manually from its memcg.
  493. */
  494. mem_cgroup_uncharge(p);
  495. /*
  496. * drop the page count elevated by isolate_lru_page()
  497. */
  498. put_page(p);
  499. return 0;
  500. }
  501. return -EIO;
  502. }
  503. static int truncate_error_page(struct page *p, unsigned long pfn,
  504. struct address_space *mapping)
  505. {
  506. int ret = MF_FAILED;
  507. if (mapping->a_ops->error_remove_page) {
  508. int err = mapping->a_ops->error_remove_page(mapping, p);
  509. if (err != 0) {
  510. pr_info("Memory failure: %#lx: Failed to punch page: %d\n",
  511. pfn, err);
  512. } else if (page_has_private(p) &&
  513. !try_to_release_page(p, GFP_NOIO)) {
  514. pr_info("Memory failure: %#lx: failed to release buffers\n",
  515. pfn);
  516. } else {
  517. ret = MF_RECOVERED;
  518. }
  519. } else {
  520. /*
  521. * If the file system doesn't support it just invalidate
  522. * This fails on dirty or anything with private pages
  523. */
  524. if (invalidate_inode_page(p))
  525. ret = MF_RECOVERED;
  526. else
  527. pr_info("Memory failure: %#lx: Failed to invalidate\n",
  528. pfn);
  529. }
  530. return ret;
  531. }
  532. /*
  533. * Error hit kernel page.
  534. * Do nothing, try to be lucky and not touch this instead. For a few cases we
  535. * could be more sophisticated.
  536. */
  537. static int me_kernel(struct page *p, unsigned long pfn)
  538. {
  539. return MF_IGNORED;
  540. }
  541. /*
  542. * Page in unknown state. Do nothing.
  543. */
  544. static int me_unknown(struct page *p, unsigned long pfn)
  545. {
  546. pr_err("Memory failure: %#lx: Unknown page state\n", pfn);
  547. return MF_FAILED;
  548. }
  549. /*
  550. * Clean (or cleaned) page cache page.
  551. */
  552. static int me_pagecache_clean(struct page *p, unsigned long pfn)
  553. {
  554. struct address_space *mapping;
  555. delete_from_lru_cache(p);
  556. /*
  557. * For anonymous pages we're done the only reference left
  558. * should be the one m_f() holds.
  559. */
  560. if (PageAnon(p))
  561. return MF_RECOVERED;
  562. /*
  563. * Now truncate the page in the page cache. This is really
  564. * more like a "temporary hole punch"
  565. * Don't do this for block devices when someone else
  566. * has a reference, because it could be file system metadata
  567. * and that's not safe to truncate.
  568. */
  569. mapping = page_mapping(p);
  570. if (!mapping) {
  571. /*
  572. * Page has been teared down in the meanwhile
  573. */
  574. return MF_FAILED;
  575. }
  576. /*
  577. * Truncation is a bit tricky. Enable it per file system for now.
  578. *
  579. * Open: to take i_mutex or not for this? Right now we don't.
  580. */
  581. return truncate_error_page(p, pfn, mapping);
  582. }
  583. /*
  584. * Dirty pagecache page
  585. * Issues: when the error hit a hole page the error is not properly
  586. * propagated.
  587. */
  588. static int me_pagecache_dirty(struct page *p, unsigned long pfn)
  589. {
  590. struct address_space *mapping = page_mapping(p);
  591. SetPageError(p);
  592. /* TBD: print more information about the file. */
  593. if (mapping) {
  594. /*
  595. * IO error will be reported by write(), fsync(), etc.
  596. * who check the mapping.
  597. * This way the application knows that something went
  598. * wrong with its dirty file data.
  599. *
  600. * There's one open issue:
  601. *
  602. * The EIO will be only reported on the next IO
  603. * operation and then cleared through the IO map.
  604. * Normally Linux has two mechanisms to pass IO error
  605. * first through the AS_EIO flag in the address space
  606. * and then through the PageError flag in the page.
  607. * Since we drop pages on memory failure handling the
  608. * only mechanism open to use is through AS_AIO.
  609. *
  610. * This has the disadvantage that it gets cleared on
  611. * the first operation that returns an error, while
  612. * the PageError bit is more sticky and only cleared
  613. * when the page is reread or dropped. If an
  614. * application assumes it will always get error on
  615. * fsync, but does other operations on the fd before
  616. * and the page is dropped between then the error
  617. * will not be properly reported.
  618. *
  619. * This can already happen even without hwpoisoned
  620. * pages: first on metadata IO errors (which only
  621. * report through AS_EIO) or when the page is dropped
  622. * at the wrong time.
  623. *
  624. * So right now we assume that the application DTRT on
  625. * the first EIO, but we're not worse than other parts
  626. * of the kernel.
  627. */
  628. mapping_set_error(mapping, -EIO);
  629. }
  630. return me_pagecache_clean(p, pfn);
  631. }
  632. /*
  633. * Clean and dirty swap cache.
  634. *
  635. * Dirty swap cache page is tricky to handle. The page could live both in page
  636. * cache and swap cache(ie. page is freshly swapped in). So it could be
  637. * referenced concurrently by 2 types of PTEs:
  638. * normal PTEs and swap PTEs. We try to handle them consistently by calling
  639. * try_to_unmap(TTU_IGNORE_HWPOISON) to convert the normal PTEs to swap PTEs,
  640. * and then
  641. * - clear dirty bit to prevent IO
  642. * - remove from LRU
  643. * - but keep in the swap cache, so that when we return to it on
  644. * a later page fault, we know the application is accessing
  645. * corrupted data and shall be killed (we installed simple
  646. * interception code in do_swap_page to catch it).
  647. *
  648. * Clean swap cache pages can be directly isolated. A later page fault will
  649. * bring in the known good data from disk.
  650. */
  651. static int me_swapcache_dirty(struct page *p, unsigned long pfn)
  652. {
  653. ClearPageDirty(p);
  654. /* Trigger EIO in shmem: */
  655. ClearPageUptodate(p);
  656. if (!delete_from_lru_cache(p))
  657. return MF_DELAYED;
  658. else
  659. return MF_FAILED;
  660. }
  661. static int me_swapcache_clean(struct page *p, unsigned long pfn)
  662. {
  663. delete_from_swap_cache(p);
  664. if (!delete_from_lru_cache(p))
  665. return MF_RECOVERED;
  666. else
  667. return MF_FAILED;
  668. }
  669. /*
  670. * Huge pages. Needs work.
  671. * Issues:
  672. * - Error on hugepage is contained in hugepage unit (not in raw page unit.)
  673. * To narrow down kill region to one page, we need to break up pmd.
  674. */
  675. static int me_huge_page(struct page *p, unsigned long pfn)
  676. {
  677. int res = 0;
  678. struct page *hpage = compound_head(p);
  679. struct address_space *mapping;
  680. if (!PageHuge(hpage))
  681. return MF_DELAYED;
  682. mapping = page_mapping(hpage);
  683. if (mapping) {
  684. res = truncate_error_page(hpage, pfn, mapping);
  685. } else {
  686. unlock_page(hpage);
  687. /*
  688. * migration entry prevents later access on error anonymous
  689. * hugepage, so we can free and dissolve it into buddy to
  690. * save healthy subpages.
  691. */
  692. if (PageAnon(hpage))
  693. put_page(hpage);
  694. dissolve_free_huge_page(p);
  695. res = MF_RECOVERED;
  696. lock_page(hpage);
  697. }
  698. return res;
  699. }
  700. /*
  701. * Various page states we can handle.
  702. *
  703. * A page state is defined by its current page->flags bits.
  704. * The table matches them in order and calls the right handler.
  705. *
  706. * This is quite tricky because we can access page at any time
  707. * in its live cycle, so all accesses have to be extremely careful.
  708. *
  709. * This is not complete. More states could be added.
  710. * For any missing state don't attempt recovery.
  711. */
  712. #define dirty (1UL << PG_dirty)
  713. #define sc ((1UL << PG_swapcache) | (1UL << PG_swapbacked))
  714. #define unevict (1UL << PG_unevictable)
  715. #define mlock (1UL << PG_mlocked)
  716. #define writeback (1UL << PG_writeback)
  717. #define lru (1UL << PG_lru)
  718. #define head (1UL << PG_head)
  719. #define slab (1UL << PG_slab)
  720. #define reserved (1UL << PG_reserved)
  721. static struct page_state {
  722. unsigned long mask;
  723. unsigned long res;
  724. enum mf_action_page_type type;
  725. int (*action)(struct page *p, unsigned long pfn);
  726. } error_states[] = {
  727. { reserved, reserved, MF_MSG_KERNEL, me_kernel },
  728. /*
  729. * free pages are specially detected outside this table:
  730. * PG_buddy pages only make a small fraction of all free pages.
  731. */
  732. /*
  733. * Could in theory check if slab page is free or if we can drop
  734. * currently unused objects without touching them. But just
  735. * treat it as standard kernel for now.
  736. */
  737. { slab, slab, MF_MSG_SLAB, me_kernel },
  738. { head, head, MF_MSG_HUGE, me_huge_page },
  739. { sc|dirty, sc|dirty, MF_MSG_DIRTY_SWAPCACHE, me_swapcache_dirty },
  740. { sc|dirty, sc, MF_MSG_CLEAN_SWAPCACHE, me_swapcache_clean },
  741. { mlock|dirty, mlock|dirty, MF_MSG_DIRTY_MLOCKED_LRU, me_pagecache_dirty },
  742. { mlock|dirty, mlock, MF_MSG_CLEAN_MLOCKED_LRU, me_pagecache_clean },
  743. { unevict|dirty, unevict|dirty, MF_MSG_DIRTY_UNEVICTABLE_LRU, me_pagecache_dirty },
  744. { unevict|dirty, unevict, MF_MSG_CLEAN_UNEVICTABLE_LRU, me_pagecache_clean },
  745. { lru|dirty, lru|dirty, MF_MSG_DIRTY_LRU, me_pagecache_dirty },
  746. { lru|dirty, lru, MF_MSG_CLEAN_LRU, me_pagecache_clean },
  747. /*
  748. * Catchall entry: must be at end.
  749. */
  750. { 0, 0, MF_MSG_UNKNOWN, me_unknown },
  751. };
  752. #undef dirty
  753. #undef sc
  754. #undef unevict
  755. #undef mlock
  756. #undef writeback
  757. #undef lru
  758. #undef head
  759. #undef slab
  760. #undef reserved
  761. /*
  762. * "Dirty/Clean" indication is not 100% accurate due to the possibility of
  763. * setting PG_dirty outside page lock. See also comment above set_page_dirty().
  764. */
  765. static void action_result(unsigned long pfn, enum mf_action_page_type type,
  766. enum mf_result result)
  767. {
  768. trace_memory_failure_event(pfn, type, result);
  769. pr_err("Memory failure: %#lx: recovery action for %s: %s\n",
  770. pfn, action_page_types[type], action_name[result]);
  771. }
  772. static int page_action(struct page_state *ps, struct page *p,
  773. unsigned long pfn)
  774. {
  775. int result;
  776. int count;
  777. result = ps->action(p, pfn);
  778. count = page_count(p) - 1;
  779. if (ps->action == me_swapcache_dirty && result == MF_DELAYED)
  780. count--;
  781. if (count > 0) {
  782. pr_err("Memory failure: %#lx: %s still referenced by %d users\n",
  783. pfn, action_page_types[ps->type], count);
  784. result = MF_FAILED;
  785. }
  786. action_result(pfn, ps->type, result);
  787. /* Could do more checks here if page looks ok */
  788. /*
  789. * Could adjust zone counters here to correct for the missing page.
  790. */
  791. return (result == MF_RECOVERED || result == MF_DELAYED) ? 0 : -EBUSY;
  792. }
  793. /**
  794. * get_hwpoison_page() - Get refcount for memory error handling:
  795. * @page: raw error page (hit by memory error)
  796. *
  797. * Return: return 0 if failed to grab the refcount, otherwise true (some
  798. * non-zero value.)
  799. */
  800. int get_hwpoison_page(struct page *page)
  801. {
  802. struct page *head = compound_head(page);
  803. if (!PageHuge(head) && PageTransHuge(head)) {
  804. /*
  805. * Non anonymous thp exists only in allocation/free time. We
  806. * can't handle such a case correctly, so let's give it up.
  807. * This should be better than triggering BUG_ON when kernel
  808. * tries to touch the "partially handled" page.
  809. */
  810. if (!PageAnon(head)) {
  811. pr_err("Memory failure: %#lx: non anonymous thp\n",
  812. page_to_pfn(page));
  813. return 0;
  814. }
  815. }
  816. if (get_page_unless_zero(head)) {
  817. if (head == compound_head(page))
  818. return 1;
  819. pr_info("Memory failure: %#lx cannot catch tail\n",
  820. page_to_pfn(page));
  821. put_page(head);
  822. }
  823. return 0;
  824. }
  825. EXPORT_SYMBOL_GPL(get_hwpoison_page);
  826. /*
  827. * Do all that is necessary to remove user space mappings. Unmap
  828. * the pages and send SIGBUS to the processes if the data was dirty.
  829. */
  830. static bool hwpoison_user_mappings(struct page *p, unsigned long pfn,
  831. int trapno, int flags, struct page **hpagep)
  832. {
  833. enum ttu_flags ttu = TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
  834. struct address_space *mapping;
  835. LIST_HEAD(tokill);
  836. bool unmap_success;
  837. int kill = 1, forcekill;
  838. struct page *hpage = *hpagep;
  839. bool mlocked = PageMlocked(hpage);
  840. /*
  841. * Here we are interested only in user-mapped pages, so skip any
  842. * other types of pages.
  843. */
  844. if (PageReserved(p) || PageSlab(p))
  845. return true;
  846. if (!(PageLRU(hpage) || PageHuge(p)))
  847. return true;
  848. /*
  849. * This check implies we don't kill processes if their pages
  850. * are in the swap cache early. Those are always late kills.
  851. */
  852. if (!page_mapped(hpage))
  853. return true;
  854. if (PageKsm(p)) {
  855. pr_err("Memory failure: %#lx: can't handle KSM pages.\n", pfn);
  856. return false;
  857. }
  858. if (PageSwapCache(p)) {
  859. pr_err("Memory failure: %#lx: keeping poisoned page in swap cache\n",
  860. pfn);
  861. ttu |= TTU_IGNORE_HWPOISON;
  862. }
  863. /*
  864. * Propagate the dirty bit from PTEs to struct page first, because we
  865. * need this to decide if we should kill or just drop the page.
  866. * XXX: the dirty test could be racy: set_page_dirty() may not always
  867. * be called inside page lock (it's recommended but not enforced).
  868. */
  869. mapping = page_mapping(hpage);
  870. if (!(flags & MF_MUST_KILL) && !PageDirty(hpage) && mapping &&
  871. mapping_cap_writeback_dirty(mapping)) {
  872. if (page_mkclean(hpage)) {
  873. SetPageDirty(hpage);
  874. } else {
  875. kill = 0;
  876. ttu |= TTU_IGNORE_HWPOISON;
  877. pr_info("Memory failure: %#lx: corrupted page was clean: dropped without side effects\n",
  878. pfn);
  879. }
  880. }
  881. /*
  882. * First collect all the processes that have the page
  883. * mapped in dirty form. This has to be done before try_to_unmap,
  884. * because ttu takes the rmap data structures down.
  885. *
  886. * Error handling: We ignore errors here because
  887. * there's nothing that can be done.
  888. */
  889. if (kill)
  890. collect_procs(hpage, &tokill, flags & MF_ACTION_REQUIRED);
  891. unmap_success = try_to_unmap(hpage, ttu, NULL);
  892. if (!unmap_success)
  893. pr_err("Memory failure: %#lx: failed to unmap page (mapcount=%d)\n",
  894. pfn, page_mapcount(hpage));
  895. /*
  896. * try_to_unmap() might put mlocked page in lru cache, so call
  897. * shake_page() again to ensure that it's flushed.
  898. */
  899. if (mlocked)
  900. shake_page(hpage, 0);
  901. /*
  902. * Now that the dirty bit has been propagated to the
  903. * struct page and all unmaps done we can decide if
  904. * killing is needed or not. Only kill when the page
  905. * was dirty or the process is not restartable,
  906. * otherwise the tokill list is merely
  907. * freed. When there was a problem unmapping earlier
  908. * use a more force-full uncatchable kill to prevent
  909. * any accesses to the poisoned memory.
  910. */
  911. forcekill = PageDirty(hpage) || (flags & MF_MUST_KILL);
  912. kill_procs(&tokill, forcekill, trapno, !unmap_success, p, pfn, flags);
  913. return unmap_success;
  914. }
  915. static int identify_page_state(unsigned long pfn, struct page *p,
  916. unsigned long page_flags)
  917. {
  918. struct page_state *ps;
  919. /*
  920. * The first check uses the current page flags which may not have any
  921. * relevant information. The second check with the saved page flags is
  922. * carried out only if the first check can't determine the page status.
  923. */
  924. for (ps = error_states;; ps++)
  925. if ((p->flags & ps->mask) == ps->res)
  926. break;
  927. page_flags |= (p->flags & (1UL << PG_dirty));
  928. if (!ps->mask)
  929. for (ps = error_states;; ps++)
  930. if ((page_flags & ps->mask) == ps->res)
  931. break;
  932. return page_action(ps, p, pfn);
  933. }
  934. static int memory_failure_hugetlb(unsigned long pfn, int trapno, int flags)
  935. {
  936. struct page *p = pfn_to_page(pfn);
  937. struct page *head = compound_head(p);
  938. int res;
  939. unsigned long page_flags;
  940. if (TestSetPageHWPoison(head)) {
  941. pr_err("Memory failure: %#lx: already hardware poisoned\n",
  942. pfn);
  943. return 0;
  944. }
  945. num_poisoned_pages_inc();
  946. if (!(flags & MF_COUNT_INCREASED) && !get_hwpoison_page(p)) {
  947. /*
  948. * Check "filter hit" and "race with other subpage."
  949. */
  950. lock_page(head);
  951. if (PageHWPoison(head)) {
  952. if ((hwpoison_filter(p) && TestClearPageHWPoison(p))
  953. || (p != head && TestSetPageHWPoison(head))) {
  954. num_poisoned_pages_dec();
  955. unlock_page(head);
  956. return 0;
  957. }
  958. }
  959. unlock_page(head);
  960. dissolve_free_huge_page(p);
  961. action_result(pfn, MF_MSG_FREE_HUGE, MF_DELAYED);
  962. return 0;
  963. }
  964. lock_page(head);
  965. page_flags = head->flags;
  966. if (!PageHWPoison(head)) {
  967. pr_err("Memory failure: %#lx: just unpoisoned\n", pfn);
  968. num_poisoned_pages_dec();
  969. unlock_page(head);
  970. put_hwpoison_page(head);
  971. return 0;
  972. }
  973. /*
  974. * TODO: hwpoison for pud-sized hugetlb doesn't work right now, so
  975. * simply disable it. In order to make it work properly, we need
  976. * make sure that:
  977. * - conversion of a pud that maps an error hugetlb into hwpoison
  978. * entry properly works, and
  979. * - other mm code walking over page table is aware of pud-aligned
  980. * hwpoison entries.
  981. */
  982. if (huge_page_size(page_hstate(head)) > PMD_SIZE) {
  983. action_result(pfn, MF_MSG_NON_PMD_HUGE, MF_IGNORED);
  984. res = -EBUSY;
  985. goto out;
  986. }
  987. if (!hwpoison_user_mappings(p, pfn, trapno, flags, &head)) {
  988. action_result(pfn, MF_MSG_UNMAP_FAILED, MF_IGNORED);
  989. res = -EBUSY;
  990. goto out;
  991. }
  992. res = identify_page_state(pfn, p, page_flags);
  993. out:
  994. unlock_page(head);
  995. return res;
  996. }
  997. /**
  998. * memory_failure - Handle memory failure of a page.
  999. * @pfn: Page Number of the corrupted page
  1000. * @trapno: Trap number reported in the signal to user space.
  1001. * @flags: fine tune action taken
  1002. *
  1003. * This function is called by the low level machine check code
  1004. * of an architecture when it detects hardware memory corruption
  1005. * of a page. It tries its best to recover, which includes
  1006. * dropping pages, killing processes etc.
  1007. *
  1008. * The function is primarily of use for corruptions that
  1009. * happen outside the current execution context (e.g. when
  1010. * detected by a background scrubber)
  1011. *
  1012. * Must run in process context (e.g. a work queue) with interrupts
  1013. * enabled and no spinlocks hold.
  1014. */
  1015. int memory_failure(unsigned long pfn, int trapno, int flags)
  1016. {
  1017. struct page *p;
  1018. struct page *hpage;
  1019. struct page *orig_head;
  1020. int res;
  1021. unsigned long page_flags;
  1022. if (!sysctl_memory_failure_recovery)
  1023. panic("Memory failure from trap %d on page %lx", trapno, pfn);
  1024. if (!pfn_valid(pfn)) {
  1025. pr_err("Memory failure: %#lx: memory outside kernel control\n",
  1026. pfn);
  1027. return -ENXIO;
  1028. }
  1029. p = pfn_to_page(pfn);
  1030. if (PageHuge(p))
  1031. return memory_failure_hugetlb(pfn, trapno, flags);
  1032. if (TestSetPageHWPoison(p)) {
  1033. pr_err("Memory failure: %#lx: already hardware poisoned\n",
  1034. pfn);
  1035. return 0;
  1036. }
  1037. orig_head = hpage = compound_head(p);
  1038. num_poisoned_pages_inc();
  1039. /*
  1040. * We need/can do nothing about count=0 pages.
  1041. * 1) it's a free page, and therefore in safe hand:
  1042. * prep_new_page() will be the gate keeper.
  1043. * 2) it's part of a non-compound high order page.
  1044. * Implies some kernel user: cannot stop them from
  1045. * R/W the page; let's pray that the page has been
  1046. * used and will be freed some time later.
  1047. * In fact it's dangerous to directly bump up page count from 0,
  1048. * that may make page_freeze_refs()/page_unfreeze_refs() mismatch.
  1049. */
  1050. if (!(flags & MF_COUNT_INCREASED) && !get_hwpoison_page(p)) {
  1051. if (is_free_buddy_page(p)) {
  1052. action_result(pfn, MF_MSG_BUDDY, MF_DELAYED);
  1053. return 0;
  1054. } else {
  1055. action_result(pfn, MF_MSG_KERNEL_HIGH_ORDER, MF_IGNORED);
  1056. return -EBUSY;
  1057. }
  1058. }
  1059. if (PageTransHuge(hpage)) {
  1060. lock_page(p);
  1061. if (!PageAnon(p) || unlikely(split_huge_page(p))) {
  1062. unlock_page(p);
  1063. if (!PageAnon(p))
  1064. pr_err("Memory failure: %#lx: non anonymous thp\n",
  1065. pfn);
  1066. else
  1067. pr_err("Memory failure: %#lx: thp split failed\n",
  1068. pfn);
  1069. if (TestClearPageHWPoison(p))
  1070. num_poisoned_pages_dec();
  1071. put_hwpoison_page(p);
  1072. return -EBUSY;
  1073. }
  1074. unlock_page(p);
  1075. VM_BUG_ON_PAGE(!page_count(p), p);
  1076. hpage = compound_head(p);
  1077. }
  1078. /*
  1079. * We ignore non-LRU pages for good reasons.
  1080. * - PG_locked is only well defined for LRU pages and a few others
  1081. * - to avoid races with __SetPageLocked()
  1082. * - to avoid races with __SetPageSlab*() (and more non-atomic ops)
  1083. * The check (unnecessarily) ignores LRU pages being isolated and
  1084. * walked by the page reclaim code, however that's not a big loss.
  1085. */
  1086. shake_page(p, 0);
  1087. /* shake_page could have turned it free. */
  1088. if (!PageLRU(p) && is_free_buddy_page(p)) {
  1089. if (flags & MF_COUNT_INCREASED)
  1090. action_result(pfn, MF_MSG_BUDDY, MF_DELAYED);
  1091. else
  1092. action_result(pfn, MF_MSG_BUDDY_2ND, MF_DELAYED);
  1093. return 0;
  1094. }
  1095. lock_page(p);
  1096. /*
  1097. * The page could have changed compound pages during the locking.
  1098. * If this happens just bail out.
  1099. */
  1100. if (PageCompound(p) && compound_head(p) != orig_head) {
  1101. action_result(pfn, MF_MSG_DIFFERENT_COMPOUND, MF_IGNORED);
  1102. res = -EBUSY;
  1103. goto out;
  1104. }
  1105. /*
  1106. * We use page flags to determine what action should be taken, but
  1107. * the flags can be modified by the error containment action. One
  1108. * example is an mlocked page, where PG_mlocked is cleared by
  1109. * page_remove_rmap() in try_to_unmap_one(). So to determine page status
  1110. * correctly, we save a copy of the page flags at this time.
  1111. */
  1112. if (PageHuge(p))
  1113. page_flags = hpage->flags;
  1114. else
  1115. page_flags = p->flags;
  1116. /*
  1117. * unpoison always clear PG_hwpoison inside page lock
  1118. */
  1119. if (!PageHWPoison(p)) {
  1120. pr_err("Memory failure: %#lx: just unpoisoned\n", pfn);
  1121. num_poisoned_pages_dec();
  1122. unlock_page(p);
  1123. put_hwpoison_page(p);
  1124. return 0;
  1125. }
  1126. if (hwpoison_filter(p)) {
  1127. if (TestClearPageHWPoison(p))
  1128. num_poisoned_pages_dec();
  1129. unlock_page(p);
  1130. put_hwpoison_page(p);
  1131. return 0;
  1132. }
  1133. /*
  1134. * __munlock_pagevec may clear a writeback page's LRU flag without
  1135. * page_lock. We need wait writeback completion for this page or it
  1136. * may trigger vfs BUG while evict inode.
  1137. */
  1138. if (!PageTransTail(p) && !PageLRU(p) && !PageWriteback(p))
  1139. goto identify_page_state;
  1140. /*
  1141. * It's very difficult to mess with pages currently under IO
  1142. * and in many cases impossible, so we just avoid it here.
  1143. */
  1144. wait_on_page_writeback(p);
  1145. /*
  1146. * Now take care of user space mappings.
  1147. * Abort on fail: __delete_from_page_cache() assumes unmapped page.
  1148. *
  1149. * When the raw error page is thp tail page, hpage points to the raw
  1150. * page after thp split.
  1151. */
  1152. if (!hwpoison_user_mappings(p, pfn, trapno, flags, &hpage)) {
  1153. action_result(pfn, MF_MSG_UNMAP_FAILED, MF_IGNORED);
  1154. res = -EBUSY;
  1155. goto out;
  1156. }
  1157. /*
  1158. * Torn down by someone else?
  1159. */
  1160. if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
  1161. action_result(pfn, MF_MSG_TRUNCATED_LRU, MF_IGNORED);
  1162. res = -EBUSY;
  1163. goto out;
  1164. }
  1165. identify_page_state:
  1166. res = identify_page_state(pfn, p, page_flags);
  1167. out:
  1168. unlock_page(p);
  1169. return res;
  1170. }
  1171. EXPORT_SYMBOL_GPL(memory_failure);
  1172. #define MEMORY_FAILURE_FIFO_ORDER 4
  1173. #define MEMORY_FAILURE_FIFO_SIZE (1 << MEMORY_FAILURE_FIFO_ORDER)
  1174. struct memory_failure_entry {
  1175. unsigned long pfn;
  1176. int trapno;
  1177. int flags;
  1178. };
  1179. struct memory_failure_cpu {
  1180. DECLARE_KFIFO(fifo, struct memory_failure_entry,
  1181. MEMORY_FAILURE_FIFO_SIZE);
  1182. spinlock_t lock;
  1183. struct work_struct work;
  1184. };
  1185. static DEFINE_PER_CPU(struct memory_failure_cpu, memory_failure_cpu);
  1186. /**
  1187. * memory_failure_queue - Schedule handling memory failure of a page.
  1188. * @pfn: Page Number of the corrupted page
  1189. * @trapno: Trap number reported in the signal to user space.
  1190. * @flags: Flags for memory failure handling
  1191. *
  1192. * This function is called by the low level hardware error handler
  1193. * when it detects hardware memory corruption of a page. It schedules
  1194. * the recovering of error page, including dropping pages, killing
  1195. * processes etc.
  1196. *
  1197. * The function is primarily of use for corruptions that
  1198. * happen outside the current execution context (e.g. when
  1199. * detected by a background scrubber)
  1200. *
  1201. * Can run in IRQ context.
  1202. */
  1203. void memory_failure_queue(unsigned long pfn, int trapno, int flags)
  1204. {
  1205. struct memory_failure_cpu *mf_cpu;
  1206. unsigned long proc_flags;
  1207. struct memory_failure_entry entry = {
  1208. .pfn = pfn,
  1209. .trapno = trapno,
  1210. .flags = flags,
  1211. };
  1212. mf_cpu = &get_cpu_var(memory_failure_cpu);
  1213. spin_lock_irqsave(&mf_cpu->lock, proc_flags);
  1214. if (kfifo_put(&mf_cpu->fifo, entry))
  1215. schedule_work_on(smp_processor_id(), &mf_cpu->work);
  1216. else
  1217. pr_err("Memory failure: buffer overflow when queuing memory failure at %#lx\n",
  1218. pfn);
  1219. spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
  1220. put_cpu_var(memory_failure_cpu);
  1221. }
  1222. EXPORT_SYMBOL_GPL(memory_failure_queue);
  1223. static void memory_failure_work_func(struct work_struct *work)
  1224. {
  1225. struct memory_failure_cpu *mf_cpu;
  1226. struct memory_failure_entry entry = { 0, };
  1227. unsigned long proc_flags;
  1228. int gotten;
  1229. mf_cpu = this_cpu_ptr(&memory_failure_cpu);
  1230. for (;;) {
  1231. spin_lock_irqsave(&mf_cpu->lock, proc_flags);
  1232. gotten = kfifo_get(&mf_cpu->fifo, &entry);
  1233. spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
  1234. if (!gotten)
  1235. break;
  1236. if (entry.flags & MF_SOFT_OFFLINE)
  1237. soft_offline_page(pfn_to_page(entry.pfn), entry.flags);
  1238. else
  1239. memory_failure(entry.pfn, entry.trapno, entry.flags);
  1240. }
  1241. }
  1242. static int __init memory_failure_init(void)
  1243. {
  1244. struct memory_failure_cpu *mf_cpu;
  1245. int cpu;
  1246. for_each_possible_cpu(cpu) {
  1247. mf_cpu = &per_cpu(memory_failure_cpu, cpu);
  1248. spin_lock_init(&mf_cpu->lock);
  1249. INIT_KFIFO(mf_cpu->fifo);
  1250. INIT_WORK(&mf_cpu->work, memory_failure_work_func);
  1251. }
  1252. return 0;
  1253. }
  1254. core_initcall(memory_failure_init);
  1255. #define unpoison_pr_info(fmt, pfn, rs) \
  1256. ({ \
  1257. if (__ratelimit(rs)) \
  1258. pr_info(fmt, pfn); \
  1259. })
  1260. /**
  1261. * unpoison_memory - Unpoison a previously poisoned page
  1262. * @pfn: Page number of the to be unpoisoned page
  1263. *
  1264. * Software-unpoison a page that has been poisoned by
  1265. * memory_failure() earlier.
  1266. *
  1267. * This is only done on the software-level, so it only works
  1268. * for linux injected failures, not real hardware failures
  1269. *
  1270. * Returns 0 for success, otherwise -errno.
  1271. */
  1272. int unpoison_memory(unsigned long pfn)
  1273. {
  1274. struct page *page;
  1275. struct page *p;
  1276. int freeit = 0;
  1277. static DEFINE_RATELIMIT_STATE(unpoison_rs, DEFAULT_RATELIMIT_INTERVAL,
  1278. DEFAULT_RATELIMIT_BURST);
  1279. if (!pfn_valid(pfn))
  1280. return -ENXIO;
  1281. p = pfn_to_page(pfn);
  1282. page = compound_head(p);
  1283. if (!PageHWPoison(p)) {
  1284. unpoison_pr_info("Unpoison: Page was already unpoisoned %#lx\n",
  1285. pfn, &unpoison_rs);
  1286. return 0;
  1287. }
  1288. if (page_count(page) > 1) {
  1289. unpoison_pr_info("Unpoison: Someone grabs the hwpoison page %#lx\n",
  1290. pfn, &unpoison_rs);
  1291. return 0;
  1292. }
  1293. if (page_mapped(page)) {
  1294. unpoison_pr_info("Unpoison: Someone maps the hwpoison page %#lx\n",
  1295. pfn, &unpoison_rs);
  1296. return 0;
  1297. }
  1298. if (page_mapping(page)) {
  1299. unpoison_pr_info("Unpoison: the hwpoison page has non-NULL mapping %#lx\n",
  1300. pfn, &unpoison_rs);
  1301. return 0;
  1302. }
  1303. /*
  1304. * unpoison_memory() can encounter thp only when the thp is being
  1305. * worked by memory_failure() and the page lock is not held yet.
  1306. * In such case, we yield to memory_failure() and make unpoison fail.
  1307. */
  1308. if (!PageHuge(page) && PageTransHuge(page)) {
  1309. unpoison_pr_info("Unpoison: Memory failure is now running on %#lx\n",
  1310. pfn, &unpoison_rs);
  1311. return 0;
  1312. }
  1313. if (!get_hwpoison_page(p)) {
  1314. if (TestClearPageHWPoison(p))
  1315. num_poisoned_pages_dec();
  1316. unpoison_pr_info("Unpoison: Software-unpoisoned free page %#lx\n",
  1317. pfn, &unpoison_rs);
  1318. return 0;
  1319. }
  1320. lock_page(page);
  1321. /*
  1322. * This test is racy because PG_hwpoison is set outside of page lock.
  1323. * That's acceptable because that won't trigger kernel panic. Instead,
  1324. * the PG_hwpoison page will be caught and isolated on the entrance to
  1325. * the free buddy page pool.
  1326. */
  1327. if (TestClearPageHWPoison(page)) {
  1328. unpoison_pr_info("Unpoison: Software-unpoisoned page %#lx\n",
  1329. pfn, &unpoison_rs);
  1330. num_poisoned_pages_dec();
  1331. freeit = 1;
  1332. }
  1333. unlock_page(page);
  1334. put_hwpoison_page(page);
  1335. if (freeit && !(pfn == my_zero_pfn(0) && page_count(p) == 1))
  1336. put_hwpoison_page(page);
  1337. return 0;
  1338. }
  1339. EXPORT_SYMBOL(unpoison_memory);
  1340. static struct page *new_page(struct page *p, unsigned long private, int **x)
  1341. {
  1342. int nid = page_to_nid(p);
  1343. return new_page_nodemask(p, nid, &node_states[N_MEMORY]);
  1344. }
  1345. /*
  1346. * Safely get reference count of an arbitrary page.
  1347. * Returns 0 for a free page, -EIO for a zero refcount page
  1348. * that is not free, and 1 for any other page type.
  1349. * For 1 the page is returned with increased page count, otherwise not.
  1350. */
  1351. static int __get_any_page(struct page *p, unsigned long pfn, int flags)
  1352. {
  1353. int ret;
  1354. if (flags & MF_COUNT_INCREASED)
  1355. return 1;
  1356. /*
  1357. * When the target page is a free hugepage, just remove it
  1358. * from free hugepage list.
  1359. */
  1360. if (!get_hwpoison_page(p)) {
  1361. if (PageHuge(p)) {
  1362. pr_info("%s: %#lx free huge page\n", __func__, pfn);
  1363. ret = 0;
  1364. } else if (is_free_buddy_page(p)) {
  1365. pr_info("%s: %#lx free buddy page\n", __func__, pfn);
  1366. ret = 0;
  1367. } else {
  1368. pr_info("%s: %#lx: unknown zero refcount page type %lx\n",
  1369. __func__, pfn, p->flags);
  1370. ret = -EIO;
  1371. }
  1372. } else {
  1373. /* Not a free page */
  1374. ret = 1;
  1375. }
  1376. return ret;
  1377. }
  1378. static int get_any_page(struct page *page, unsigned long pfn, int flags)
  1379. {
  1380. int ret = __get_any_page(page, pfn, flags);
  1381. if (ret == 1 && !PageHuge(page) &&
  1382. !PageLRU(page) && !__PageMovable(page)) {
  1383. /*
  1384. * Try to free it.
  1385. */
  1386. put_hwpoison_page(page);
  1387. shake_page(page, 1);
  1388. /*
  1389. * Did it turn free?
  1390. */
  1391. ret = __get_any_page(page, pfn, 0);
  1392. if (ret == 1 && !PageLRU(page)) {
  1393. /* Drop page reference which is from __get_any_page() */
  1394. put_hwpoison_page(page);
  1395. pr_info("soft_offline: %#lx: unknown non LRU page type %lx (%pGp)\n",
  1396. pfn, page->flags, &page->flags);
  1397. return -EIO;
  1398. }
  1399. }
  1400. return ret;
  1401. }
  1402. static int soft_offline_huge_page(struct page *page, int flags)
  1403. {
  1404. int ret;
  1405. unsigned long pfn = page_to_pfn(page);
  1406. struct page *hpage = compound_head(page);
  1407. LIST_HEAD(pagelist);
  1408. /*
  1409. * This double-check of PageHWPoison is to avoid the race with
  1410. * memory_failure(). See also comment in __soft_offline_page().
  1411. */
  1412. lock_page(hpage);
  1413. if (PageHWPoison(hpage)) {
  1414. unlock_page(hpage);
  1415. put_hwpoison_page(hpage);
  1416. pr_info("soft offline: %#lx hugepage already poisoned\n", pfn);
  1417. return -EBUSY;
  1418. }
  1419. unlock_page(hpage);
  1420. ret = isolate_huge_page(hpage, &pagelist);
  1421. /*
  1422. * get_any_page() and isolate_huge_page() takes a refcount each,
  1423. * so need to drop one here.
  1424. */
  1425. put_hwpoison_page(hpage);
  1426. if (!ret) {
  1427. pr_info("soft offline: %#lx hugepage failed to isolate\n", pfn);
  1428. return -EBUSY;
  1429. }
  1430. ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
  1431. MIGRATE_SYNC, MR_MEMORY_FAILURE);
  1432. if (ret) {
  1433. pr_info("soft offline: %#lx: migration failed %d, type %lx (%pGp)\n",
  1434. pfn, ret, page->flags, &page->flags);
  1435. if (!list_empty(&pagelist))
  1436. putback_movable_pages(&pagelist);
  1437. if (ret > 0)
  1438. ret = -EIO;
  1439. } else {
  1440. if (PageHuge(page))
  1441. dissolve_free_huge_page(page);
  1442. }
  1443. return ret;
  1444. }
  1445. static int __soft_offline_page(struct page *page, int flags)
  1446. {
  1447. int ret;
  1448. unsigned long pfn = page_to_pfn(page);
  1449. /*
  1450. * Check PageHWPoison again inside page lock because PageHWPoison
  1451. * is set by memory_failure() outside page lock. Note that
  1452. * memory_failure() also double-checks PageHWPoison inside page lock,
  1453. * so there's no race between soft_offline_page() and memory_failure().
  1454. */
  1455. lock_page(page);
  1456. wait_on_page_writeback(page);
  1457. if (PageHWPoison(page)) {
  1458. unlock_page(page);
  1459. put_hwpoison_page(page);
  1460. pr_info("soft offline: %#lx page already poisoned\n", pfn);
  1461. return -EBUSY;
  1462. }
  1463. /*
  1464. * Try to invalidate first. This should work for
  1465. * non dirty unmapped page cache pages.
  1466. */
  1467. ret = invalidate_inode_page(page);
  1468. unlock_page(page);
  1469. /*
  1470. * RED-PEN would be better to keep it isolated here, but we
  1471. * would need to fix isolation locking first.
  1472. */
  1473. if (ret == 1) {
  1474. put_hwpoison_page(page);
  1475. pr_info("soft_offline: %#lx: invalidated\n", pfn);
  1476. SetPageHWPoison(page);
  1477. num_poisoned_pages_inc();
  1478. return 0;
  1479. }
  1480. /*
  1481. * Simple invalidation didn't work.
  1482. * Try to migrate to a new page instead. migrate.c
  1483. * handles a large number of cases for us.
  1484. */
  1485. if (PageLRU(page))
  1486. ret = isolate_lru_page(page);
  1487. else
  1488. ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
  1489. /*
  1490. * Drop page reference which is came from get_any_page()
  1491. * successful isolate_lru_page() already took another one.
  1492. */
  1493. put_hwpoison_page(page);
  1494. if (!ret) {
  1495. LIST_HEAD(pagelist);
  1496. /*
  1497. * After isolated lru page, the PageLRU will be cleared,
  1498. * so use !__PageMovable instead for LRU page's mapping
  1499. * cannot have PAGE_MAPPING_MOVABLE.
  1500. */
  1501. if (!__PageMovable(page))
  1502. inc_node_page_state(page, NR_ISOLATED_ANON +
  1503. page_is_file_cache(page));
  1504. list_add(&page->lru, &pagelist);
  1505. ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
  1506. MIGRATE_SYNC, MR_MEMORY_FAILURE);
  1507. if (ret) {
  1508. if (!list_empty(&pagelist))
  1509. putback_movable_pages(&pagelist);
  1510. pr_info("soft offline: %#lx: migration failed %d, type %lx (%pGp)\n",
  1511. pfn, ret, page->flags, &page->flags);
  1512. if (ret > 0)
  1513. ret = -EIO;
  1514. }
  1515. } else {
  1516. pr_info("soft offline: %#lx: isolation failed: %d, page count %d, type %lx (%pGp)\n",
  1517. pfn, ret, page_count(page), page->flags, &page->flags);
  1518. }
  1519. return ret;
  1520. }
  1521. static int soft_offline_in_use_page(struct page *page, int flags)
  1522. {
  1523. int ret;
  1524. struct page *hpage = compound_head(page);
  1525. if (!PageHuge(page) && PageTransHuge(hpage)) {
  1526. lock_page(page);
  1527. if (!PageAnon(page) || unlikely(split_huge_page(page))) {
  1528. unlock_page(page);
  1529. if (!PageAnon(page))
  1530. pr_info("soft offline: %#lx: non anonymous thp\n", page_to_pfn(page));
  1531. else
  1532. pr_info("soft offline: %#lx: thp split failed\n", page_to_pfn(page));
  1533. put_hwpoison_page(page);
  1534. return -EBUSY;
  1535. }
  1536. unlock_page(page);
  1537. }
  1538. if (PageHuge(page))
  1539. ret = soft_offline_huge_page(page, flags);
  1540. else
  1541. ret = __soft_offline_page(page, flags);
  1542. return ret;
  1543. }
  1544. static void soft_offline_free_page(struct page *page)
  1545. {
  1546. struct page *head = compound_head(page);
  1547. if (!TestSetPageHWPoison(head)) {
  1548. num_poisoned_pages_inc();
  1549. if (PageHuge(head))
  1550. dissolve_free_huge_page(page);
  1551. }
  1552. }
  1553. /**
  1554. * soft_offline_page - Soft offline a page.
  1555. * @page: page to offline
  1556. * @flags: flags. Same as memory_failure().
  1557. *
  1558. * Returns 0 on success, otherwise negated errno.
  1559. *
  1560. * Soft offline a page, by migration or invalidation,
  1561. * without killing anything. This is for the case when
  1562. * a page is not corrupted yet (so it's still valid to access),
  1563. * but has had a number of corrected errors and is better taken
  1564. * out.
  1565. *
  1566. * The actual policy on when to do that is maintained by
  1567. * user space.
  1568. *
  1569. * This should never impact any application or cause data loss,
  1570. * however it might take some time.
  1571. *
  1572. * This is not a 100% solution for all memory, but tries to be
  1573. * ``good enough'' for the majority of memory.
  1574. */
  1575. int soft_offline_page(struct page *page, int flags)
  1576. {
  1577. int ret;
  1578. unsigned long pfn = page_to_pfn(page);
  1579. if (PageHWPoison(page)) {
  1580. pr_info("soft offline: %#lx page already poisoned\n", pfn);
  1581. if (flags & MF_COUNT_INCREASED)
  1582. put_hwpoison_page(page);
  1583. return -EBUSY;
  1584. }
  1585. get_online_mems();
  1586. ret = get_any_page(page, pfn, flags);
  1587. put_online_mems();
  1588. if (ret > 0)
  1589. ret = soft_offline_in_use_page(page, flags);
  1590. else if (ret == 0)
  1591. soft_offline_free_page(page);
  1592. return ret;
  1593. }