memory-failure.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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/pagemap.h>
  45. #include <linux/swap.h>
  46. #include <linux/backing-dev.h>
  47. #include <linux/migrate.h>
  48. #include <linux/page-isolation.h>
  49. #include <linux/suspend.h>
  50. #include <linux/slab.h>
  51. #include <linux/swapops.h>
  52. #include <linux/hugetlb.h>
  53. #include <linux/memory_hotplug.h>
  54. #include "internal.h"
  55. int sysctl_memory_failure_early_kill __read_mostly = 0;
  56. int sysctl_memory_failure_recovery __read_mostly = 1;
  57. atomic_long_t mce_bad_pages __read_mostly = ATOMIC_LONG_INIT(0);
  58. #if defined(CONFIG_HWPOISON_INJECT) || defined(CONFIG_HWPOISON_INJECT_MODULE)
  59. u32 hwpoison_filter_enable = 0;
  60. u32 hwpoison_filter_dev_major = ~0U;
  61. u32 hwpoison_filter_dev_minor = ~0U;
  62. u64 hwpoison_filter_flags_mask;
  63. u64 hwpoison_filter_flags_value;
  64. EXPORT_SYMBOL_GPL(hwpoison_filter_enable);
  65. EXPORT_SYMBOL_GPL(hwpoison_filter_dev_major);
  66. EXPORT_SYMBOL_GPL(hwpoison_filter_dev_minor);
  67. EXPORT_SYMBOL_GPL(hwpoison_filter_flags_mask);
  68. EXPORT_SYMBOL_GPL(hwpoison_filter_flags_value);
  69. static int hwpoison_filter_dev(struct page *p)
  70. {
  71. struct address_space *mapping;
  72. dev_t dev;
  73. if (hwpoison_filter_dev_major == ~0U &&
  74. hwpoison_filter_dev_minor == ~0U)
  75. return 0;
  76. /*
  77. * page_mapping() does not accept slab pages.
  78. */
  79. if (PageSlab(p))
  80. return -EINVAL;
  81. mapping = page_mapping(p);
  82. if (mapping == NULL || mapping->host == NULL)
  83. return -EINVAL;
  84. dev = mapping->host->i_sb->s_dev;
  85. if (hwpoison_filter_dev_major != ~0U &&
  86. hwpoison_filter_dev_major != MAJOR(dev))
  87. return -EINVAL;
  88. if (hwpoison_filter_dev_minor != ~0U &&
  89. hwpoison_filter_dev_minor != MINOR(dev))
  90. return -EINVAL;
  91. return 0;
  92. }
  93. static int hwpoison_filter_flags(struct page *p)
  94. {
  95. if (!hwpoison_filter_flags_mask)
  96. return 0;
  97. if ((stable_page_flags(p) & hwpoison_filter_flags_mask) ==
  98. hwpoison_filter_flags_value)
  99. return 0;
  100. else
  101. return -EINVAL;
  102. }
  103. /*
  104. * This allows stress tests to limit test scope to a collection of tasks
  105. * by putting them under some memcg. This prevents killing unrelated/important
  106. * processes such as /sbin/init. Note that the target task may share clean
  107. * pages with init (eg. libc text), which is harmless. If the target task
  108. * share _dirty_ pages with another task B, the test scheme must make sure B
  109. * is also included in the memcg. At last, due to race conditions this filter
  110. * can only guarantee that the page either belongs to the memcg tasks, or is
  111. * a freed page.
  112. */
  113. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  114. u64 hwpoison_filter_memcg;
  115. EXPORT_SYMBOL_GPL(hwpoison_filter_memcg);
  116. static int hwpoison_filter_task(struct page *p)
  117. {
  118. struct mem_cgroup *mem;
  119. struct cgroup_subsys_state *css;
  120. unsigned long ino;
  121. if (!hwpoison_filter_memcg)
  122. return 0;
  123. mem = try_get_mem_cgroup_from_page(p);
  124. if (!mem)
  125. return -EINVAL;
  126. css = mem_cgroup_css(mem);
  127. /* root_mem_cgroup has NULL dentries */
  128. if (!css->cgroup->dentry)
  129. return -EINVAL;
  130. ino = css->cgroup->dentry->d_inode->i_ino;
  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 an ``action optional''
  160. * signal.
  161. */
  162. static int kill_proc_ao(struct task_struct *t, unsigned long addr, int trapno,
  163. unsigned long pfn, struct page *page)
  164. {
  165. struct siginfo si;
  166. int ret;
  167. printk(KERN_ERR
  168. "MCE %#lx: Killing %s:%d early due to hardware memory corruption\n",
  169. pfn, t->comm, t->pid);
  170. si.si_signo = SIGBUS;
  171. si.si_errno = 0;
  172. si.si_code = BUS_MCEERR_AO;
  173. si.si_addr = (void *)addr;
  174. #ifdef __ARCH_SI_TRAPNO
  175. si.si_trapno = trapno;
  176. #endif
  177. si.si_addr_lsb = compound_trans_order(compound_head(page)) + PAGE_SHIFT;
  178. /*
  179. * Don't use force here, it's convenient if the signal
  180. * can be temporarily blocked.
  181. * This could cause a loop when the user sets SIGBUS
  182. * to SIG_IGN, but hopefully no one will do that?
  183. */
  184. ret = send_sig_info(SIGBUS, &si, t); /* synchronous? */
  185. if (ret < 0)
  186. printk(KERN_INFO "MCE: Error sending signal to %s:%d: %d\n",
  187. t->comm, t->pid, ret);
  188. return ret;
  189. }
  190. /*
  191. * When a unknown page type is encountered drain as many buffers as possible
  192. * in the hope to turn the page into a LRU or free page, which we can handle.
  193. */
  194. void shake_page(struct page *p, int access)
  195. {
  196. if (!PageSlab(p)) {
  197. lru_add_drain_all();
  198. if (PageLRU(p))
  199. return;
  200. drain_all_pages();
  201. if (PageLRU(p) || is_free_buddy_page(p))
  202. return;
  203. }
  204. /*
  205. * Only call shrink_slab here (which would also shrink other caches) if
  206. * access is not potentially fatal.
  207. */
  208. if (access) {
  209. int nr;
  210. do {
  211. struct shrink_control shrink = {
  212. .gfp_mask = GFP_KERNEL,
  213. };
  214. nr = shrink_slab(&shrink, 1000, 1000);
  215. if (page_count(p) == 1)
  216. break;
  217. } while (nr > 10);
  218. }
  219. }
  220. EXPORT_SYMBOL_GPL(shake_page);
  221. /*
  222. * Kill all processes that have a poisoned page mapped and then isolate
  223. * the page.
  224. *
  225. * General strategy:
  226. * Find all processes having the page mapped and kill them.
  227. * But we keep a page reference around so that the page is not
  228. * actually freed yet.
  229. * Then stash the page away
  230. *
  231. * There's no convenient way to get back to mapped processes
  232. * from the VMAs. So do a brute-force search over all
  233. * running processes.
  234. *
  235. * Remember that machine checks are not common (or rather
  236. * if they are common you have other problems), so this shouldn't
  237. * be a performance issue.
  238. *
  239. * Also there are some races possible while we get from the
  240. * error detection to actually handle it.
  241. */
  242. struct to_kill {
  243. struct list_head nd;
  244. struct task_struct *tsk;
  245. unsigned long addr;
  246. char addr_valid;
  247. };
  248. /*
  249. * Failure handling: if we can't find or can't kill a process there's
  250. * not much we can do. We just print a message and ignore otherwise.
  251. */
  252. /*
  253. * Schedule a process for later kill.
  254. * Uses GFP_ATOMIC allocations to avoid potential recursions in the VM.
  255. * TBD would GFP_NOIO be enough?
  256. */
  257. static void add_to_kill(struct task_struct *tsk, struct page *p,
  258. struct vm_area_struct *vma,
  259. struct list_head *to_kill,
  260. struct to_kill **tkc)
  261. {
  262. struct to_kill *tk;
  263. if (*tkc) {
  264. tk = *tkc;
  265. *tkc = NULL;
  266. } else {
  267. tk = kmalloc(sizeof(struct to_kill), GFP_ATOMIC);
  268. if (!tk) {
  269. printk(KERN_ERR
  270. "MCE: Out of memory while machine check handling\n");
  271. return;
  272. }
  273. }
  274. tk->addr = page_address_in_vma(p, vma);
  275. tk->addr_valid = 1;
  276. /*
  277. * In theory we don't have to kill when the page was
  278. * munmaped. But it could be also a mremap. Since that's
  279. * likely very rare kill anyways just out of paranoia, but use
  280. * a SIGKILL because the error is not contained anymore.
  281. */
  282. if (tk->addr == -EFAULT) {
  283. pr_info("MCE: Unable to find user space address %lx in %s\n",
  284. page_to_pfn(p), tsk->comm);
  285. tk->addr_valid = 0;
  286. }
  287. get_task_struct(tsk);
  288. tk->tsk = tsk;
  289. list_add_tail(&tk->nd, to_kill);
  290. }
  291. /*
  292. * Kill the processes that have been collected earlier.
  293. *
  294. * Only do anything when DOIT is set, otherwise just free the list
  295. * (this is used for clean pages which do not need killing)
  296. * Also when FAIL is set do a force kill because something went
  297. * wrong earlier.
  298. */
  299. static void kill_procs_ao(struct list_head *to_kill, int doit, int trapno,
  300. int fail, struct page *page, unsigned long pfn)
  301. {
  302. struct to_kill *tk, *next;
  303. list_for_each_entry_safe (tk, next, to_kill, nd) {
  304. if (doit) {
  305. /*
  306. * In case something went wrong with munmapping
  307. * make sure the process doesn't catch the
  308. * signal and then access the memory. Just kill it.
  309. */
  310. if (fail || tk->addr_valid == 0) {
  311. printk(KERN_ERR
  312. "MCE %#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
  313. pfn, tk->tsk->comm, tk->tsk->pid);
  314. force_sig(SIGKILL, tk->tsk);
  315. }
  316. /*
  317. * In theory the process could have mapped
  318. * something else on the address in-between. We could
  319. * check for that, but we need to tell the
  320. * process anyways.
  321. */
  322. else if (kill_proc_ao(tk->tsk, tk->addr, trapno,
  323. pfn, page) < 0)
  324. printk(KERN_ERR
  325. "MCE %#lx: Cannot send advisory machine check signal to %s:%d\n",
  326. pfn, tk->tsk->comm, tk->tsk->pid);
  327. }
  328. put_task_struct(tk->tsk);
  329. kfree(tk);
  330. }
  331. }
  332. static int task_early_kill(struct task_struct *tsk)
  333. {
  334. if (!tsk->mm)
  335. return 0;
  336. if (tsk->flags & PF_MCE_PROCESS)
  337. return !!(tsk->flags & PF_MCE_EARLY);
  338. return sysctl_memory_failure_early_kill;
  339. }
  340. /*
  341. * Collect processes when the error hit an anonymous page.
  342. */
  343. static void collect_procs_anon(struct page *page, struct list_head *to_kill,
  344. struct to_kill **tkc)
  345. {
  346. struct vm_area_struct *vma;
  347. struct task_struct *tsk;
  348. struct anon_vma *av;
  349. read_lock(&tasklist_lock);
  350. av = page_lock_anon_vma(page);
  351. if (av == NULL) /* Not actually mapped anymore */
  352. goto out;
  353. for_each_process (tsk) {
  354. struct anon_vma_chain *vmac;
  355. if (!task_early_kill(tsk))
  356. continue;
  357. list_for_each_entry(vmac, &av->head, same_anon_vma) {
  358. vma = vmac->vma;
  359. if (!page_mapped_in_vma(page, vma))
  360. continue;
  361. if (vma->vm_mm == tsk->mm)
  362. add_to_kill(tsk, page, vma, to_kill, tkc);
  363. }
  364. }
  365. page_unlock_anon_vma(av);
  366. out:
  367. read_unlock(&tasklist_lock);
  368. }
  369. /*
  370. * Collect processes when the error hit a file mapped page.
  371. */
  372. static void collect_procs_file(struct page *page, struct list_head *to_kill,
  373. struct to_kill **tkc)
  374. {
  375. struct vm_area_struct *vma;
  376. struct task_struct *tsk;
  377. struct prio_tree_iter iter;
  378. struct address_space *mapping = page->mapping;
  379. /*
  380. * A note on the locking order between the two locks.
  381. * We don't rely on this particular order.
  382. * If you have some other code that needs a different order
  383. * feel free to switch them around. Or add a reverse link
  384. * from mm_struct to task_struct, then this could be all
  385. * done without taking tasklist_lock and looping over all tasks.
  386. */
  387. read_lock(&tasklist_lock);
  388. mutex_lock(&mapping->i_mmap_mutex);
  389. for_each_process(tsk) {
  390. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  391. if (!task_early_kill(tsk))
  392. continue;
  393. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff,
  394. pgoff) {
  395. /*
  396. * Send early kill signal to tasks where a vma covers
  397. * the page but the corrupted page is not necessarily
  398. * mapped it in its pte.
  399. * Assume applications who requested early kill want
  400. * to be informed of all such data corruptions.
  401. */
  402. if (vma->vm_mm == tsk->mm)
  403. add_to_kill(tsk, page, vma, to_kill, tkc);
  404. }
  405. }
  406. mutex_unlock(&mapping->i_mmap_mutex);
  407. read_unlock(&tasklist_lock);
  408. }
  409. /*
  410. * Collect the processes who have the corrupted page mapped to kill.
  411. * This is done in two steps for locking reasons.
  412. * First preallocate one tokill structure outside the spin locks,
  413. * so that we can kill at least one process reasonably reliable.
  414. */
  415. static void collect_procs(struct page *page, struct list_head *tokill)
  416. {
  417. struct to_kill *tk;
  418. if (!page->mapping)
  419. return;
  420. tk = kmalloc(sizeof(struct to_kill), GFP_NOIO);
  421. if (!tk)
  422. return;
  423. if (PageAnon(page))
  424. collect_procs_anon(page, tokill, &tk);
  425. else
  426. collect_procs_file(page, tokill, &tk);
  427. kfree(tk);
  428. }
  429. /*
  430. * Error handlers for various types of pages.
  431. */
  432. enum outcome {
  433. IGNORED, /* Error: cannot be handled */
  434. FAILED, /* Error: handling failed */
  435. DELAYED, /* Will be handled later */
  436. RECOVERED, /* Successfully recovered */
  437. };
  438. static const char *action_name[] = {
  439. [IGNORED] = "Ignored",
  440. [FAILED] = "Failed",
  441. [DELAYED] = "Delayed",
  442. [RECOVERED] = "Recovered",
  443. };
  444. /*
  445. * XXX: It is possible that a page is isolated from LRU cache,
  446. * and then kept in swap cache or failed to remove from page cache.
  447. * The page count will stop it from being freed by unpoison.
  448. * Stress tests should be aware of this memory leak problem.
  449. */
  450. static int delete_from_lru_cache(struct page *p)
  451. {
  452. if (!isolate_lru_page(p)) {
  453. /*
  454. * Clear sensible page flags, so that the buddy system won't
  455. * complain when the page is unpoison-and-freed.
  456. */
  457. ClearPageActive(p);
  458. ClearPageUnevictable(p);
  459. /*
  460. * drop the page count elevated by isolate_lru_page()
  461. */
  462. page_cache_release(p);
  463. return 0;
  464. }
  465. return -EIO;
  466. }
  467. /*
  468. * Error hit kernel page.
  469. * Do nothing, try to be lucky and not touch this instead. For a few cases we
  470. * could be more sophisticated.
  471. */
  472. static int me_kernel(struct page *p, unsigned long pfn)
  473. {
  474. return IGNORED;
  475. }
  476. /*
  477. * Page in unknown state. Do nothing.
  478. */
  479. static int me_unknown(struct page *p, unsigned long pfn)
  480. {
  481. printk(KERN_ERR "MCE %#lx: Unknown page state\n", pfn);
  482. return FAILED;
  483. }
  484. /*
  485. * Clean (or cleaned) page cache page.
  486. */
  487. static int me_pagecache_clean(struct page *p, unsigned long pfn)
  488. {
  489. int err;
  490. int ret = FAILED;
  491. struct address_space *mapping;
  492. delete_from_lru_cache(p);
  493. /*
  494. * For anonymous pages we're done the only reference left
  495. * should be the one m_f() holds.
  496. */
  497. if (PageAnon(p))
  498. return RECOVERED;
  499. /*
  500. * Now truncate the page in the page cache. This is really
  501. * more like a "temporary hole punch"
  502. * Don't do this for block devices when someone else
  503. * has a reference, because it could be file system metadata
  504. * and that's not safe to truncate.
  505. */
  506. mapping = page_mapping(p);
  507. if (!mapping) {
  508. /*
  509. * Page has been teared down in the meanwhile
  510. */
  511. return FAILED;
  512. }
  513. /*
  514. * Truncation is a bit tricky. Enable it per file system for now.
  515. *
  516. * Open: to take i_mutex or not for this? Right now we don't.
  517. */
  518. if (mapping->a_ops->error_remove_page) {
  519. err = mapping->a_ops->error_remove_page(mapping, p);
  520. if (err != 0) {
  521. printk(KERN_INFO "MCE %#lx: Failed to punch page: %d\n",
  522. pfn, err);
  523. } else if (page_has_private(p) &&
  524. !try_to_release_page(p, GFP_NOIO)) {
  525. pr_info("MCE %#lx: failed to release buffers\n", pfn);
  526. } else {
  527. ret = RECOVERED;
  528. }
  529. } else {
  530. /*
  531. * If the file system doesn't support it just invalidate
  532. * This fails on dirty or anything with private pages
  533. */
  534. if (invalidate_inode_page(p))
  535. ret = RECOVERED;
  536. else
  537. printk(KERN_INFO "MCE %#lx: Failed to invalidate\n",
  538. pfn);
  539. }
  540. return ret;
  541. }
  542. /*
  543. * Dirty cache page page
  544. * Issues: when the error hit a hole page the error is not properly
  545. * propagated.
  546. */
  547. static int me_pagecache_dirty(struct page *p, unsigned long pfn)
  548. {
  549. struct address_space *mapping = page_mapping(p);
  550. SetPageError(p);
  551. /* TBD: print more information about the file. */
  552. if (mapping) {
  553. /*
  554. * IO error will be reported by write(), fsync(), etc.
  555. * who check the mapping.
  556. * This way the application knows that something went
  557. * wrong with its dirty file data.
  558. *
  559. * There's one open issue:
  560. *
  561. * The EIO will be only reported on the next IO
  562. * operation and then cleared through the IO map.
  563. * Normally Linux has two mechanisms to pass IO error
  564. * first through the AS_EIO flag in the address space
  565. * and then through the PageError flag in the page.
  566. * Since we drop pages on memory failure handling the
  567. * only mechanism open to use is through AS_AIO.
  568. *
  569. * This has the disadvantage that it gets cleared on
  570. * the first operation that returns an error, while
  571. * the PageError bit is more sticky and only cleared
  572. * when the page is reread or dropped. If an
  573. * application assumes it will always get error on
  574. * fsync, but does other operations on the fd before
  575. * and the page is dropped between then the error
  576. * will not be properly reported.
  577. *
  578. * This can already happen even without hwpoisoned
  579. * pages: first on metadata IO errors (which only
  580. * report through AS_EIO) or when the page is dropped
  581. * at the wrong time.
  582. *
  583. * So right now we assume that the application DTRT on
  584. * the first EIO, but we're not worse than other parts
  585. * of the kernel.
  586. */
  587. mapping_set_error(mapping, EIO);
  588. }
  589. return me_pagecache_clean(p, pfn);
  590. }
  591. /*
  592. * Clean and dirty swap cache.
  593. *
  594. * Dirty swap cache page is tricky to handle. The page could live both in page
  595. * cache and swap cache(ie. page is freshly swapped in). So it could be
  596. * referenced concurrently by 2 types of PTEs:
  597. * normal PTEs and swap PTEs. We try to handle them consistently by calling
  598. * try_to_unmap(TTU_IGNORE_HWPOISON) to convert the normal PTEs to swap PTEs,
  599. * and then
  600. * - clear dirty bit to prevent IO
  601. * - remove from LRU
  602. * - but keep in the swap cache, so that when we return to it on
  603. * a later page fault, we know the application is accessing
  604. * corrupted data and shall be killed (we installed simple
  605. * interception code in do_swap_page to catch it).
  606. *
  607. * Clean swap cache pages can be directly isolated. A later page fault will
  608. * bring in the known good data from disk.
  609. */
  610. static int me_swapcache_dirty(struct page *p, unsigned long pfn)
  611. {
  612. ClearPageDirty(p);
  613. /* Trigger EIO in shmem: */
  614. ClearPageUptodate(p);
  615. if (!delete_from_lru_cache(p))
  616. return DELAYED;
  617. else
  618. return FAILED;
  619. }
  620. static int me_swapcache_clean(struct page *p, unsigned long pfn)
  621. {
  622. delete_from_swap_cache(p);
  623. if (!delete_from_lru_cache(p))
  624. return RECOVERED;
  625. else
  626. return FAILED;
  627. }
  628. /*
  629. * Huge pages. Needs work.
  630. * Issues:
  631. * - Error on hugepage is contained in hugepage unit (not in raw page unit.)
  632. * To narrow down kill region to one page, we need to break up pmd.
  633. */
  634. static int me_huge_page(struct page *p, unsigned long pfn)
  635. {
  636. int res = 0;
  637. struct page *hpage = compound_head(p);
  638. /*
  639. * We can safely recover from error on free or reserved (i.e.
  640. * not in-use) hugepage by dequeuing it from freelist.
  641. * To check whether a hugepage is in-use or not, we can't use
  642. * page->lru because it can be used in other hugepage operations,
  643. * such as __unmap_hugepage_range() and gather_surplus_pages().
  644. * So instead we use page_mapping() and PageAnon().
  645. * We assume that this function is called with page lock held,
  646. * so there is no race between isolation and mapping/unmapping.
  647. */
  648. if (!(page_mapping(hpage) || PageAnon(hpage))) {
  649. res = dequeue_hwpoisoned_huge_page(hpage);
  650. if (!res)
  651. return RECOVERED;
  652. }
  653. return DELAYED;
  654. }
  655. /*
  656. * Various page states we can handle.
  657. *
  658. * A page state is defined by its current page->flags bits.
  659. * The table matches them in order and calls the right handler.
  660. *
  661. * This is quite tricky because we can access page at any time
  662. * in its live cycle, so all accesses have to be extremely careful.
  663. *
  664. * This is not complete. More states could be added.
  665. * For any missing state don't attempt recovery.
  666. */
  667. #define dirty (1UL << PG_dirty)
  668. #define sc (1UL << PG_swapcache)
  669. #define unevict (1UL << PG_unevictable)
  670. #define mlock (1UL << PG_mlocked)
  671. #define writeback (1UL << PG_writeback)
  672. #define lru (1UL << PG_lru)
  673. #define swapbacked (1UL << PG_swapbacked)
  674. #define head (1UL << PG_head)
  675. #define tail (1UL << PG_tail)
  676. #define compound (1UL << PG_compound)
  677. #define slab (1UL << PG_slab)
  678. #define reserved (1UL << PG_reserved)
  679. static struct page_state {
  680. unsigned long mask;
  681. unsigned long res;
  682. char *msg;
  683. int (*action)(struct page *p, unsigned long pfn);
  684. } error_states[] = {
  685. { reserved, reserved, "reserved kernel", me_kernel },
  686. /*
  687. * free pages are specially detected outside this table:
  688. * PG_buddy pages only make a small fraction of all free pages.
  689. */
  690. /*
  691. * Could in theory check if slab page is free or if we can drop
  692. * currently unused objects without touching them. But just
  693. * treat it as standard kernel for now.
  694. */
  695. { slab, slab, "kernel slab", me_kernel },
  696. #ifdef CONFIG_PAGEFLAGS_EXTENDED
  697. { head, head, "huge", me_huge_page },
  698. { tail, tail, "huge", me_huge_page },
  699. #else
  700. { compound, compound, "huge", me_huge_page },
  701. #endif
  702. { sc|dirty, sc|dirty, "swapcache", me_swapcache_dirty },
  703. { sc|dirty, sc, "swapcache", me_swapcache_clean },
  704. { unevict|dirty, unevict|dirty, "unevictable LRU", me_pagecache_dirty},
  705. { unevict, unevict, "unevictable LRU", me_pagecache_clean},
  706. { mlock|dirty, mlock|dirty, "mlocked LRU", me_pagecache_dirty },
  707. { mlock, mlock, "mlocked LRU", me_pagecache_clean },
  708. { lru|dirty, lru|dirty, "LRU", me_pagecache_dirty },
  709. { lru|dirty, lru, "clean LRU", me_pagecache_clean },
  710. /*
  711. * Catchall entry: must be at end.
  712. */
  713. { 0, 0, "unknown page state", me_unknown },
  714. };
  715. #undef dirty
  716. #undef sc
  717. #undef unevict
  718. #undef mlock
  719. #undef writeback
  720. #undef lru
  721. #undef swapbacked
  722. #undef head
  723. #undef tail
  724. #undef compound
  725. #undef slab
  726. #undef reserved
  727. static void action_result(unsigned long pfn, char *msg, int result)
  728. {
  729. struct page *page = pfn_to_page(pfn);
  730. printk(KERN_ERR "MCE %#lx: %s%s page recovery: %s\n",
  731. pfn,
  732. PageDirty(page) ? "dirty " : "",
  733. msg, action_name[result]);
  734. }
  735. static int page_action(struct page_state *ps, struct page *p,
  736. unsigned long pfn)
  737. {
  738. int result;
  739. int count;
  740. result = ps->action(p, pfn);
  741. action_result(pfn, ps->msg, result);
  742. count = page_count(p) - 1;
  743. if (ps->action == me_swapcache_dirty && result == DELAYED)
  744. count--;
  745. if (count != 0) {
  746. printk(KERN_ERR
  747. "MCE %#lx: %s page still referenced by %d users\n",
  748. pfn, ps->msg, count);
  749. result = FAILED;
  750. }
  751. /* Could do more checks here if page looks ok */
  752. /*
  753. * Could adjust zone counters here to correct for the missing page.
  754. */
  755. return (result == RECOVERED || result == DELAYED) ? 0 : -EBUSY;
  756. }
  757. /*
  758. * Do all that is necessary to remove user space mappings. Unmap
  759. * the pages and send SIGBUS to the processes if the data was dirty.
  760. */
  761. static int hwpoison_user_mappings(struct page *p, unsigned long pfn,
  762. int trapno)
  763. {
  764. enum ttu_flags ttu = TTU_UNMAP | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
  765. struct address_space *mapping;
  766. LIST_HEAD(tokill);
  767. int ret;
  768. int kill = 1;
  769. struct page *hpage = compound_head(p);
  770. struct page *ppage;
  771. if (PageReserved(p) || PageSlab(p))
  772. return SWAP_SUCCESS;
  773. /*
  774. * This check implies we don't kill processes if their pages
  775. * are in the swap cache early. Those are always late kills.
  776. */
  777. if (!page_mapped(hpage))
  778. return SWAP_SUCCESS;
  779. if (PageKsm(p))
  780. return SWAP_FAIL;
  781. if (PageSwapCache(p)) {
  782. printk(KERN_ERR
  783. "MCE %#lx: keeping poisoned page in swap cache\n", pfn);
  784. ttu |= TTU_IGNORE_HWPOISON;
  785. }
  786. /*
  787. * Propagate the dirty bit from PTEs to struct page first, because we
  788. * need this to decide if we should kill or just drop the page.
  789. * XXX: the dirty test could be racy: set_page_dirty() may not always
  790. * be called inside page lock (it's recommended but not enforced).
  791. */
  792. mapping = page_mapping(hpage);
  793. if (!PageDirty(hpage) && mapping &&
  794. mapping_cap_writeback_dirty(mapping)) {
  795. if (page_mkclean(hpage)) {
  796. SetPageDirty(hpage);
  797. } else {
  798. kill = 0;
  799. ttu |= TTU_IGNORE_HWPOISON;
  800. printk(KERN_INFO
  801. "MCE %#lx: corrupted page was clean: dropped without side effects\n",
  802. pfn);
  803. }
  804. }
  805. /*
  806. * ppage: poisoned page
  807. * if p is regular page(4k page)
  808. * ppage == real poisoned page;
  809. * else p is hugetlb or THP, ppage == head page.
  810. */
  811. ppage = hpage;
  812. if (PageTransHuge(hpage)) {
  813. /*
  814. * Verify that this isn't a hugetlbfs head page, the check for
  815. * PageAnon is just for avoid tripping a split_huge_page
  816. * internal debug check, as split_huge_page refuses to deal with
  817. * anything that isn't an anon page. PageAnon can't go away fro
  818. * under us because we hold a refcount on the hpage, without a
  819. * refcount on the hpage. split_huge_page can't be safely called
  820. * in the first place, having a refcount on the tail isn't
  821. * enough * to be safe.
  822. */
  823. if (!PageHuge(hpage) && PageAnon(hpage)) {
  824. if (unlikely(split_huge_page(hpage))) {
  825. /*
  826. * FIXME: if splitting THP is failed, it is
  827. * better to stop the following operation rather
  828. * than causing panic by unmapping. System might
  829. * survive if the page is freed later.
  830. */
  831. printk(KERN_INFO
  832. "MCE %#lx: failed to split THP\n", pfn);
  833. BUG_ON(!PageHWPoison(p));
  834. return SWAP_FAIL;
  835. }
  836. /* THP is split, so ppage should be the real poisoned page. */
  837. ppage = p;
  838. }
  839. }
  840. /*
  841. * First collect all the processes that have the page
  842. * mapped in dirty form. This has to be done before try_to_unmap,
  843. * because ttu takes the rmap data structures down.
  844. *
  845. * Error handling: We ignore errors here because
  846. * there's nothing that can be done.
  847. */
  848. if (kill)
  849. collect_procs(ppage, &tokill);
  850. if (hpage != ppage)
  851. lock_page(ppage);
  852. ret = try_to_unmap(ppage, ttu);
  853. if (ret != SWAP_SUCCESS)
  854. printk(KERN_ERR "MCE %#lx: failed to unmap page (mapcount=%d)\n",
  855. pfn, page_mapcount(ppage));
  856. if (hpage != ppage)
  857. unlock_page(ppage);
  858. /*
  859. * Now that the dirty bit has been propagated to the
  860. * struct page and all unmaps done we can decide if
  861. * killing is needed or not. Only kill when the page
  862. * was dirty, otherwise the tokill list is merely
  863. * freed. When there was a problem unmapping earlier
  864. * use a more force-full uncatchable kill to prevent
  865. * any accesses to the poisoned memory.
  866. */
  867. kill_procs_ao(&tokill, !!PageDirty(ppage), trapno,
  868. ret != SWAP_SUCCESS, p, pfn);
  869. return ret;
  870. }
  871. static void set_page_hwpoison_huge_page(struct page *hpage)
  872. {
  873. int i;
  874. int nr_pages = 1 << compound_trans_order(hpage);
  875. for (i = 0; i < nr_pages; i++)
  876. SetPageHWPoison(hpage + i);
  877. }
  878. static void clear_page_hwpoison_huge_page(struct page *hpage)
  879. {
  880. int i;
  881. int nr_pages = 1 << compound_trans_order(hpage);
  882. for (i = 0; i < nr_pages; i++)
  883. ClearPageHWPoison(hpage + i);
  884. }
  885. int __memory_failure(unsigned long pfn, int trapno, int flags)
  886. {
  887. struct page_state *ps;
  888. struct page *p;
  889. struct page *hpage;
  890. int res;
  891. unsigned int nr_pages;
  892. if (!sysctl_memory_failure_recovery)
  893. panic("Memory failure from trap %d on page %lx", trapno, pfn);
  894. if (!pfn_valid(pfn)) {
  895. printk(KERN_ERR
  896. "MCE %#lx: memory outside kernel control\n",
  897. pfn);
  898. return -ENXIO;
  899. }
  900. p = pfn_to_page(pfn);
  901. hpage = compound_head(p);
  902. if (TestSetPageHWPoison(p)) {
  903. printk(KERN_ERR "MCE %#lx: already hardware poisoned\n", pfn);
  904. return 0;
  905. }
  906. nr_pages = 1 << compound_trans_order(hpage);
  907. atomic_long_add(nr_pages, &mce_bad_pages);
  908. /*
  909. * We need/can do nothing about count=0 pages.
  910. * 1) it's a free page, and therefore in safe hand:
  911. * prep_new_page() will be the gate keeper.
  912. * 2) it's a free hugepage, which is also safe:
  913. * an affected hugepage will be dequeued from hugepage freelist,
  914. * so there's no concern about reusing it ever after.
  915. * 3) it's part of a non-compound high order page.
  916. * Implies some kernel user: cannot stop them from
  917. * R/W the page; let's pray that the page has been
  918. * used and will be freed some time later.
  919. * In fact it's dangerous to directly bump up page count from 0,
  920. * that may make page_freeze_refs()/page_unfreeze_refs() mismatch.
  921. */
  922. if (!(flags & MF_COUNT_INCREASED) &&
  923. !get_page_unless_zero(hpage)) {
  924. if (is_free_buddy_page(p)) {
  925. action_result(pfn, "free buddy", DELAYED);
  926. return 0;
  927. } else if (PageHuge(hpage)) {
  928. /*
  929. * Check "just unpoisoned", "filter hit", and
  930. * "race with other subpage."
  931. */
  932. lock_page(hpage);
  933. if (!PageHWPoison(hpage)
  934. || (hwpoison_filter(p) && TestClearPageHWPoison(p))
  935. || (p != hpage && TestSetPageHWPoison(hpage))) {
  936. atomic_long_sub(nr_pages, &mce_bad_pages);
  937. return 0;
  938. }
  939. set_page_hwpoison_huge_page(hpage);
  940. res = dequeue_hwpoisoned_huge_page(hpage);
  941. action_result(pfn, "free huge",
  942. res ? IGNORED : DELAYED);
  943. unlock_page(hpage);
  944. return res;
  945. } else {
  946. action_result(pfn, "high order kernel", IGNORED);
  947. return -EBUSY;
  948. }
  949. }
  950. /*
  951. * We ignore non-LRU pages for good reasons.
  952. * - PG_locked is only well defined for LRU pages and a few others
  953. * - to avoid races with __set_page_locked()
  954. * - to avoid races with __SetPageSlab*() (and more non-atomic ops)
  955. * The check (unnecessarily) ignores LRU pages being isolated and
  956. * walked by the page reclaim code, however that's not a big loss.
  957. */
  958. if (!PageHuge(p) && !PageTransCompound(p)) {
  959. if (!PageLRU(p))
  960. shake_page(p, 0);
  961. if (!PageLRU(p)) {
  962. /*
  963. * shake_page could have turned it free.
  964. */
  965. if (is_free_buddy_page(p)) {
  966. action_result(pfn, "free buddy, 2nd try",
  967. DELAYED);
  968. return 0;
  969. }
  970. action_result(pfn, "non LRU", IGNORED);
  971. put_page(p);
  972. return -EBUSY;
  973. }
  974. }
  975. /*
  976. * Lock the page and wait for writeback to finish.
  977. * It's very difficult to mess with pages currently under IO
  978. * and in many cases impossible, so we just avoid it here.
  979. */
  980. lock_page(hpage);
  981. /*
  982. * unpoison always clear PG_hwpoison inside page lock
  983. */
  984. if (!PageHWPoison(p)) {
  985. printk(KERN_ERR "MCE %#lx: just unpoisoned\n", pfn);
  986. res = 0;
  987. goto out;
  988. }
  989. if (hwpoison_filter(p)) {
  990. if (TestClearPageHWPoison(p))
  991. atomic_long_sub(nr_pages, &mce_bad_pages);
  992. unlock_page(hpage);
  993. put_page(hpage);
  994. return 0;
  995. }
  996. /*
  997. * For error on the tail page, we should set PG_hwpoison
  998. * on the head page to show that the hugepage is hwpoisoned
  999. */
  1000. if (PageHuge(p) && PageTail(p) && TestSetPageHWPoison(hpage)) {
  1001. action_result(pfn, "hugepage already hardware poisoned",
  1002. IGNORED);
  1003. unlock_page(hpage);
  1004. put_page(hpage);
  1005. return 0;
  1006. }
  1007. /*
  1008. * Set PG_hwpoison on all pages in an error hugepage,
  1009. * because containment is done in hugepage unit for now.
  1010. * Since we have done TestSetPageHWPoison() for the head page with
  1011. * page lock held, we can safely set PG_hwpoison bits on tail pages.
  1012. */
  1013. if (PageHuge(p))
  1014. set_page_hwpoison_huge_page(hpage);
  1015. wait_on_page_writeback(p);
  1016. /*
  1017. * Now take care of user space mappings.
  1018. * Abort on fail: __delete_from_page_cache() assumes unmapped page.
  1019. */
  1020. if (hwpoison_user_mappings(p, pfn, trapno) != SWAP_SUCCESS) {
  1021. printk(KERN_ERR "MCE %#lx: cannot unmap page, give up\n", pfn);
  1022. res = -EBUSY;
  1023. goto out;
  1024. }
  1025. /*
  1026. * Torn down by someone else?
  1027. */
  1028. if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
  1029. action_result(pfn, "already truncated LRU", IGNORED);
  1030. res = -EBUSY;
  1031. goto out;
  1032. }
  1033. res = -EBUSY;
  1034. for (ps = error_states;; ps++) {
  1035. if ((p->flags & ps->mask) == ps->res) {
  1036. res = page_action(ps, p, pfn);
  1037. break;
  1038. }
  1039. }
  1040. out:
  1041. unlock_page(hpage);
  1042. return res;
  1043. }
  1044. EXPORT_SYMBOL_GPL(__memory_failure);
  1045. /**
  1046. * memory_failure - Handle memory failure of a page.
  1047. * @pfn: Page Number of the corrupted page
  1048. * @trapno: Trap number reported in the signal to user space.
  1049. *
  1050. * This function is called by the low level machine check code
  1051. * of an architecture when it detects hardware memory corruption
  1052. * of a page. It tries its best to recover, which includes
  1053. * dropping pages, killing processes etc.
  1054. *
  1055. * The function is primarily of use for corruptions that
  1056. * happen outside the current execution context (e.g. when
  1057. * detected by a background scrubber)
  1058. *
  1059. * Must run in process context (e.g. a work queue) with interrupts
  1060. * enabled and no spinlocks hold.
  1061. */
  1062. void memory_failure(unsigned long pfn, int trapno)
  1063. {
  1064. __memory_failure(pfn, trapno, 0);
  1065. }
  1066. /**
  1067. * unpoison_memory - Unpoison a previously poisoned page
  1068. * @pfn: Page number of the to be unpoisoned page
  1069. *
  1070. * Software-unpoison a page that has been poisoned by
  1071. * memory_failure() earlier.
  1072. *
  1073. * This is only done on the software-level, so it only works
  1074. * for linux injected failures, not real hardware failures
  1075. *
  1076. * Returns 0 for success, otherwise -errno.
  1077. */
  1078. int unpoison_memory(unsigned long pfn)
  1079. {
  1080. struct page *page;
  1081. struct page *p;
  1082. int freeit = 0;
  1083. unsigned int nr_pages;
  1084. if (!pfn_valid(pfn))
  1085. return -ENXIO;
  1086. p = pfn_to_page(pfn);
  1087. page = compound_head(p);
  1088. if (!PageHWPoison(p)) {
  1089. pr_info("MCE: Page was already unpoisoned %#lx\n", pfn);
  1090. return 0;
  1091. }
  1092. nr_pages = 1 << compound_trans_order(page);
  1093. if (!get_page_unless_zero(page)) {
  1094. /*
  1095. * Since HWPoisoned hugepage should have non-zero refcount,
  1096. * race between memory failure and unpoison seems to happen.
  1097. * In such case unpoison fails and memory failure runs
  1098. * to the end.
  1099. */
  1100. if (PageHuge(page)) {
  1101. pr_debug("MCE: Memory failure is now running on free hugepage %#lx\n", pfn);
  1102. return 0;
  1103. }
  1104. if (TestClearPageHWPoison(p))
  1105. atomic_long_sub(nr_pages, &mce_bad_pages);
  1106. pr_info("MCE: Software-unpoisoned free page %#lx\n", pfn);
  1107. return 0;
  1108. }
  1109. lock_page(page);
  1110. /*
  1111. * This test is racy because PG_hwpoison is set outside of page lock.
  1112. * That's acceptable because that won't trigger kernel panic. Instead,
  1113. * the PG_hwpoison page will be caught and isolated on the entrance to
  1114. * the free buddy page pool.
  1115. */
  1116. if (TestClearPageHWPoison(page)) {
  1117. pr_info("MCE: Software-unpoisoned page %#lx\n", pfn);
  1118. atomic_long_sub(nr_pages, &mce_bad_pages);
  1119. freeit = 1;
  1120. if (PageHuge(page))
  1121. clear_page_hwpoison_huge_page(page);
  1122. }
  1123. unlock_page(page);
  1124. put_page(page);
  1125. if (freeit)
  1126. put_page(page);
  1127. return 0;
  1128. }
  1129. EXPORT_SYMBOL(unpoison_memory);
  1130. static struct page *new_page(struct page *p, unsigned long private, int **x)
  1131. {
  1132. int nid = page_to_nid(p);
  1133. if (PageHuge(p))
  1134. return alloc_huge_page_node(page_hstate(compound_head(p)),
  1135. nid);
  1136. else
  1137. return alloc_pages_exact_node(nid, GFP_HIGHUSER_MOVABLE, 0);
  1138. }
  1139. /*
  1140. * Safely get reference count of an arbitrary page.
  1141. * Returns 0 for a free page, -EIO for a zero refcount page
  1142. * that is not free, and 1 for any other page type.
  1143. * For 1 the page is returned with increased page count, otherwise not.
  1144. */
  1145. static int get_any_page(struct page *p, unsigned long pfn, int flags)
  1146. {
  1147. int ret;
  1148. if (flags & MF_COUNT_INCREASED)
  1149. return 1;
  1150. /*
  1151. * The lock_memory_hotplug prevents a race with memory hotplug.
  1152. * This is a big hammer, a better would be nicer.
  1153. */
  1154. lock_memory_hotplug();
  1155. /*
  1156. * Isolate the page, so that it doesn't get reallocated if it
  1157. * was free.
  1158. */
  1159. set_migratetype_isolate(p);
  1160. /*
  1161. * When the target page is a free hugepage, just remove it
  1162. * from free hugepage list.
  1163. */
  1164. if (!get_page_unless_zero(compound_head(p))) {
  1165. if (PageHuge(p)) {
  1166. pr_info("get_any_page: %#lx free huge page\n", pfn);
  1167. ret = dequeue_hwpoisoned_huge_page(compound_head(p));
  1168. } else if (is_free_buddy_page(p)) {
  1169. pr_info("get_any_page: %#lx free buddy page\n", pfn);
  1170. /* Set hwpoison bit while page is still isolated */
  1171. SetPageHWPoison(p);
  1172. ret = 0;
  1173. } else {
  1174. pr_info("get_any_page: %#lx: unknown zero refcount page type %lx\n",
  1175. pfn, p->flags);
  1176. ret = -EIO;
  1177. }
  1178. } else {
  1179. /* Not a free page */
  1180. ret = 1;
  1181. }
  1182. unset_migratetype_isolate(p);
  1183. unlock_memory_hotplug();
  1184. return ret;
  1185. }
  1186. static int soft_offline_huge_page(struct page *page, int flags)
  1187. {
  1188. int ret;
  1189. unsigned long pfn = page_to_pfn(page);
  1190. struct page *hpage = compound_head(page);
  1191. LIST_HEAD(pagelist);
  1192. ret = get_any_page(page, pfn, flags);
  1193. if (ret < 0)
  1194. return ret;
  1195. if (ret == 0)
  1196. goto done;
  1197. if (PageHWPoison(hpage)) {
  1198. put_page(hpage);
  1199. pr_debug("soft offline: %#lx hugepage already poisoned\n", pfn);
  1200. return -EBUSY;
  1201. }
  1202. /* Keep page count to indicate a given hugepage is isolated. */
  1203. list_add(&hpage->lru, &pagelist);
  1204. ret = migrate_huge_pages(&pagelist, new_page, MPOL_MF_MOVE_ALL, 0,
  1205. true);
  1206. if (ret) {
  1207. struct page *page1, *page2;
  1208. list_for_each_entry_safe(page1, page2, &pagelist, lru)
  1209. put_page(page1);
  1210. pr_debug("soft offline: %#lx: migration failed %d, type %lx\n",
  1211. pfn, ret, page->flags);
  1212. if (ret > 0)
  1213. ret = -EIO;
  1214. return ret;
  1215. }
  1216. done:
  1217. if (!PageHWPoison(hpage))
  1218. atomic_long_add(1 << compound_trans_order(hpage), &mce_bad_pages);
  1219. set_page_hwpoison_huge_page(hpage);
  1220. dequeue_hwpoisoned_huge_page(hpage);
  1221. /* keep elevated page count for bad page */
  1222. return ret;
  1223. }
  1224. /**
  1225. * soft_offline_page - Soft offline a page.
  1226. * @page: page to offline
  1227. * @flags: flags. Same as memory_failure().
  1228. *
  1229. * Returns 0 on success, otherwise negated errno.
  1230. *
  1231. * Soft offline a page, by migration or invalidation,
  1232. * without killing anything. This is for the case when
  1233. * a page is not corrupted yet (so it's still valid to access),
  1234. * but has had a number of corrected errors and is better taken
  1235. * out.
  1236. *
  1237. * The actual policy on when to do that is maintained by
  1238. * user space.
  1239. *
  1240. * This should never impact any application or cause data loss,
  1241. * however it might take some time.
  1242. *
  1243. * This is not a 100% solution for all memory, but tries to be
  1244. * ``good enough'' for the majority of memory.
  1245. */
  1246. int soft_offline_page(struct page *page, int flags)
  1247. {
  1248. int ret;
  1249. unsigned long pfn = page_to_pfn(page);
  1250. if (PageHuge(page))
  1251. return soft_offline_huge_page(page, flags);
  1252. ret = get_any_page(page, pfn, flags);
  1253. if (ret < 0)
  1254. return ret;
  1255. if (ret == 0)
  1256. goto done;
  1257. /*
  1258. * Page cache page we can handle?
  1259. */
  1260. if (!PageLRU(page)) {
  1261. /*
  1262. * Try to free it.
  1263. */
  1264. put_page(page);
  1265. shake_page(page, 1);
  1266. /*
  1267. * Did it turn free?
  1268. */
  1269. ret = get_any_page(page, pfn, 0);
  1270. if (ret < 0)
  1271. return ret;
  1272. if (ret == 0)
  1273. goto done;
  1274. }
  1275. if (!PageLRU(page)) {
  1276. pr_info("soft_offline: %#lx: unknown non LRU page type %lx\n",
  1277. pfn, page->flags);
  1278. return -EIO;
  1279. }
  1280. lock_page(page);
  1281. wait_on_page_writeback(page);
  1282. /*
  1283. * Synchronized using the page lock with memory_failure()
  1284. */
  1285. if (PageHWPoison(page)) {
  1286. unlock_page(page);
  1287. put_page(page);
  1288. pr_info("soft offline: %#lx page already poisoned\n", pfn);
  1289. return -EBUSY;
  1290. }
  1291. /*
  1292. * Try to invalidate first. This should work for
  1293. * non dirty unmapped page cache pages.
  1294. */
  1295. ret = invalidate_inode_page(page);
  1296. unlock_page(page);
  1297. /*
  1298. * RED-PEN would be better to keep it isolated here, but we
  1299. * would need to fix isolation locking first.
  1300. */
  1301. if (ret == 1) {
  1302. put_page(page);
  1303. ret = 0;
  1304. pr_info("soft_offline: %#lx: invalidated\n", pfn);
  1305. goto done;
  1306. }
  1307. /*
  1308. * Simple invalidation didn't work.
  1309. * Try to migrate to a new page instead. migrate.c
  1310. * handles a large number of cases for us.
  1311. */
  1312. ret = isolate_lru_page(page);
  1313. /*
  1314. * Drop page reference which is came from get_any_page()
  1315. * successful isolate_lru_page() already took another one.
  1316. */
  1317. put_page(page);
  1318. if (!ret) {
  1319. LIST_HEAD(pagelist);
  1320. list_add(&page->lru, &pagelist);
  1321. ret = migrate_pages(&pagelist, new_page, MPOL_MF_MOVE_ALL,
  1322. 0, true);
  1323. if (ret) {
  1324. putback_lru_pages(&pagelist);
  1325. pr_info("soft offline: %#lx: migration failed %d, type %lx\n",
  1326. pfn, ret, page->flags);
  1327. if (ret > 0)
  1328. ret = -EIO;
  1329. }
  1330. } else {
  1331. pr_info("soft offline: %#lx: isolation failed: %d, page count %d, type %lx\n",
  1332. pfn, ret, page_count(page), page->flags);
  1333. }
  1334. if (ret)
  1335. return ret;
  1336. done:
  1337. atomic_long_add(1, &mce_bad_pages);
  1338. SetPageHWPoison(page);
  1339. /* keep elevated page count for bad page */
  1340. return ret;
  1341. }