memory-failure.c 47 KB

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