memory-failure.c 48 KB

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