memory-failure.c 49 KB

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