memory-failure.c 42 KB

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