memory-failure.c 49 KB

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