memory-failure.c 39 KB

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