memory-failure.c 43 KB

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