memory-failure.c 48 KB

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