memory-failure.c 49 KB

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