memory-failure.c 48 KB

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