memory-failure.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  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. * Poisoned page might never drop its ref count to 0 so we have
  491. * to uncharge it manually from its memcg.
  492. */
  493. mem_cgroup_uncharge(p);
  494. /*
  495. * drop the page count elevated by isolate_lru_page()
  496. */
  497. put_page(p);
  498. return 0;
  499. }
  500. return -EIO;
  501. }
  502. /*
  503. * Error hit kernel page.
  504. * Do nothing, try to be lucky and not touch this instead. For a few cases we
  505. * could be more sophisticated.
  506. */
  507. static int me_kernel(struct page *p, unsigned long pfn)
  508. {
  509. return MF_IGNORED;
  510. }
  511. /*
  512. * Page in unknown state. Do nothing.
  513. */
  514. static int me_unknown(struct page *p, unsigned long pfn)
  515. {
  516. pr_err("Memory failure: %#lx: Unknown page state\n", pfn);
  517. return MF_FAILED;
  518. }
  519. /*
  520. * Clean (or cleaned) page cache page.
  521. */
  522. static int me_pagecache_clean(struct page *p, unsigned long pfn)
  523. {
  524. int err;
  525. int ret = MF_FAILED;
  526. struct address_space *mapping;
  527. delete_from_lru_cache(p);
  528. /*
  529. * For anonymous pages we're done the only reference left
  530. * should be the one m_f() holds.
  531. */
  532. if (PageAnon(p))
  533. return MF_RECOVERED;
  534. /*
  535. * Now truncate the page in the page cache. This is really
  536. * more like a "temporary hole punch"
  537. * Don't do this for block devices when someone else
  538. * has a reference, because it could be file system metadata
  539. * and that's not safe to truncate.
  540. */
  541. mapping = page_mapping(p);
  542. if (!mapping) {
  543. /*
  544. * Page has been teared down in the meanwhile
  545. */
  546. return MF_FAILED;
  547. }
  548. /*
  549. * Truncation is a bit tricky. Enable it per file system for now.
  550. *
  551. * Open: to take i_mutex or not for this? Right now we don't.
  552. */
  553. if (mapping->a_ops->error_remove_page) {
  554. err = mapping->a_ops->error_remove_page(mapping, p);
  555. if (err != 0) {
  556. pr_info("Memory failure: %#lx: Failed to punch page: %d\n",
  557. pfn, err);
  558. } else if (page_has_private(p) &&
  559. !try_to_release_page(p, GFP_NOIO)) {
  560. pr_info("Memory failure: %#lx: failed to release buffers\n",
  561. pfn);
  562. } else {
  563. ret = MF_RECOVERED;
  564. }
  565. } else {
  566. /*
  567. * If the file system doesn't support it just invalidate
  568. * This fails on dirty or anything with private pages
  569. */
  570. if (invalidate_inode_page(p))
  571. ret = MF_RECOVERED;
  572. else
  573. pr_info("Memory failure: %#lx: Failed to invalidate\n",
  574. pfn);
  575. }
  576. return ret;
  577. }
  578. /*
  579. * Dirty pagecache page
  580. * Issues: when the error hit a hole page the error is not properly
  581. * propagated.
  582. */
  583. static int me_pagecache_dirty(struct page *p, unsigned long pfn)
  584. {
  585. struct address_space *mapping = page_mapping(p);
  586. SetPageError(p);
  587. /* TBD: print more information about the file. */
  588. if (mapping) {
  589. /*
  590. * IO error will be reported by write(), fsync(), etc.
  591. * who check the mapping.
  592. * This way the application knows that something went
  593. * wrong with its dirty file data.
  594. *
  595. * There's one open issue:
  596. *
  597. * The EIO will be only reported on the next IO
  598. * operation and then cleared through the IO map.
  599. * Normally Linux has two mechanisms to pass IO error
  600. * first through the AS_EIO flag in the address space
  601. * and then through the PageError flag in the page.
  602. * Since we drop pages on memory failure handling the
  603. * only mechanism open to use is through AS_AIO.
  604. *
  605. * This has the disadvantage that it gets cleared on
  606. * the first operation that returns an error, while
  607. * the PageError bit is more sticky and only cleared
  608. * when the page is reread or dropped. If an
  609. * application assumes it will always get error on
  610. * fsync, but does other operations on the fd before
  611. * and the page is dropped between then the error
  612. * will not be properly reported.
  613. *
  614. * This can already happen even without hwpoisoned
  615. * pages: first on metadata IO errors (which only
  616. * report through AS_EIO) or when the page is dropped
  617. * at the wrong time.
  618. *
  619. * So right now we assume that the application DTRT on
  620. * the first EIO, but we're not worse than other parts
  621. * of the kernel.
  622. */
  623. mapping_set_error(mapping, EIO);
  624. }
  625. return me_pagecache_clean(p, pfn);
  626. }
  627. /*
  628. * Clean and dirty swap cache.
  629. *
  630. * Dirty swap cache page is tricky to handle. The page could live both in page
  631. * cache and swap cache(ie. page is freshly swapped in). So it could be
  632. * referenced concurrently by 2 types of PTEs:
  633. * normal PTEs and swap PTEs. We try to handle them consistently by calling
  634. * try_to_unmap(TTU_IGNORE_HWPOISON) to convert the normal PTEs to swap PTEs,
  635. * and then
  636. * - clear dirty bit to prevent IO
  637. * - remove from LRU
  638. * - but keep in the swap cache, so that when we return to it on
  639. * a later page fault, we know the application is accessing
  640. * corrupted data and shall be killed (we installed simple
  641. * interception code in do_swap_page to catch it).
  642. *
  643. * Clean swap cache pages can be directly isolated. A later page fault will
  644. * bring in the known good data from disk.
  645. */
  646. static int me_swapcache_dirty(struct page *p, unsigned long pfn)
  647. {
  648. ClearPageDirty(p);
  649. /* Trigger EIO in shmem: */
  650. ClearPageUptodate(p);
  651. if (!delete_from_lru_cache(p))
  652. return MF_DELAYED;
  653. else
  654. return MF_FAILED;
  655. }
  656. static int me_swapcache_clean(struct page *p, unsigned long pfn)
  657. {
  658. delete_from_swap_cache(p);
  659. if (!delete_from_lru_cache(p))
  660. return MF_RECOVERED;
  661. else
  662. return MF_FAILED;
  663. }
  664. /*
  665. * Huge pages. Needs work.
  666. * Issues:
  667. * - Error on hugepage is contained in hugepage unit (not in raw page unit.)
  668. * To narrow down kill region to one page, we need to break up pmd.
  669. */
  670. static int me_huge_page(struct page *p, unsigned long pfn)
  671. {
  672. int res = 0;
  673. struct page *hpage = compound_head(p);
  674. if (!PageHuge(hpage))
  675. return MF_DELAYED;
  676. /*
  677. * We can safely recover from error on free or reserved (i.e.
  678. * not in-use) hugepage by dequeuing it from freelist.
  679. * To check whether a hugepage is in-use or not, we can't use
  680. * page->lru because it can be used in other hugepage operations,
  681. * such as __unmap_hugepage_range() and gather_surplus_pages().
  682. * So instead we use page_mapping() and PageAnon().
  683. */
  684. if (!(page_mapping(hpage) || PageAnon(hpage))) {
  685. res = dequeue_hwpoisoned_huge_page(hpage);
  686. if (!res)
  687. return MF_RECOVERED;
  688. }
  689. return MF_DELAYED;
  690. }
  691. /*
  692. * Various page states we can handle.
  693. *
  694. * A page state is defined by its current page->flags bits.
  695. * The table matches them in order and calls the right handler.
  696. *
  697. * This is quite tricky because we can access page at any time
  698. * in its live cycle, so all accesses have to be extremely careful.
  699. *
  700. * This is not complete. More states could be added.
  701. * For any missing state don't attempt recovery.
  702. */
  703. #define dirty (1UL << PG_dirty)
  704. #define sc ((1UL << PG_swapcache) | (1UL << PG_swapbacked))
  705. #define unevict (1UL << PG_unevictable)
  706. #define mlock (1UL << PG_mlocked)
  707. #define writeback (1UL << PG_writeback)
  708. #define lru (1UL << PG_lru)
  709. #define head (1UL << PG_head)
  710. #define slab (1UL << PG_slab)
  711. #define reserved (1UL << PG_reserved)
  712. static struct page_state {
  713. unsigned long mask;
  714. unsigned long res;
  715. enum mf_action_page_type type;
  716. int (*action)(struct page *p, unsigned long pfn);
  717. } error_states[] = {
  718. { reserved, reserved, MF_MSG_KERNEL, me_kernel },
  719. /*
  720. * free pages are specially detected outside this table:
  721. * PG_buddy pages only make a small fraction of all free pages.
  722. */
  723. /*
  724. * Could in theory check if slab page is free or if we can drop
  725. * currently unused objects without touching them. But just
  726. * treat it as standard kernel for now.
  727. */
  728. { slab, slab, MF_MSG_SLAB, me_kernel },
  729. { head, head, MF_MSG_HUGE, me_huge_page },
  730. { sc|dirty, sc|dirty, MF_MSG_DIRTY_SWAPCACHE, me_swapcache_dirty },
  731. { sc|dirty, sc, MF_MSG_CLEAN_SWAPCACHE, me_swapcache_clean },
  732. { mlock|dirty, mlock|dirty, MF_MSG_DIRTY_MLOCKED_LRU, me_pagecache_dirty },
  733. { mlock|dirty, mlock, MF_MSG_CLEAN_MLOCKED_LRU, me_pagecache_clean },
  734. { unevict|dirty, unevict|dirty, MF_MSG_DIRTY_UNEVICTABLE_LRU, me_pagecache_dirty },
  735. { unevict|dirty, unevict, MF_MSG_CLEAN_UNEVICTABLE_LRU, me_pagecache_clean },
  736. { lru|dirty, lru|dirty, MF_MSG_DIRTY_LRU, me_pagecache_dirty },
  737. { lru|dirty, lru, MF_MSG_CLEAN_LRU, me_pagecache_clean },
  738. /*
  739. * Catchall entry: must be at end.
  740. */
  741. { 0, 0, MF_MSG_UNKNOWN, me_unknown },
  742. };
  743. #undef dirty
  744. #undef sc
  745. #undef unevict
  746. #undef mlock
  747. #undef writeback
  748. #undef lru
  749. #undef head
  750. #undef slab
  751. #undef reserved
  752. /*
  753. * "Dirty/Clean" indication is not 100% accurate due to the possibility of
  754. * setting PG_dirty outside page lock. See also comment above set_page_dirty().
  755. */
  756. static void action_result(unsigned long pfn, enum mf_action_page_type type,
  757. enum mf_result result)
  758. {
  759. trace_memory_failure_event(pfn, type, result);
  760. pr_err("Memory failure: %#lx: recovery action for %s: %s\n",
  761. pfn, action_page_types[type], action_name[result]);
  762. }
  763. static int page_action(struct page_state *ps, struct page *p,
  764. unsigned long pfn)
  765. {
  766. int result;
  767. int count;
  768. result = ps->action(p, pfn);
  769. count = page_count(p) - 1;
  770. if (ps->action == me_swapcache_dirty && result == MF_DELAYED)
  771. count--;
  772. if (count != 0) {
  773. pr_err("Memory failure: %#lx: %s still referenced by %d users\n",
  774. pfn, action_page_types[ps->type], count);
  775. result = MF_FAILED;
  776. }
  777. action_result(pfn, ps->type, result);
  778. /* Could do more checks here if page looks ok */
  779. /*
  780. * Could adjust zone counters here to correct for the missing page.
  781. */
  782. return (result == MF_RECOVERED || result == MF_DELAYED) ? 0 : -EBUSY;
  783. }
  784. /**
  785. * get_hwpoison_page() - Get refcount for memory error handling:
  786. * @page: raw error page (hit by memory error)
  787. *
  788. * Return: return 0 if failed to grab the refcount, otherwise true (some
  789. * non-zero value.)
  790. */
  791. int get_hwpoison_page(struct page *page)
  792. {
  793. struct page *head = compound_head(page);
  794. if (!PageHuge(head) && PageTransHuge(head)) {
  795. /*
  796. * Non anonymous thp exists only in allocation/free time. We
  797. * can't handle such a case correctly, so let's give it up.
  798. * This should be better than triggering BUG_ON when kernel
  799. * tries to touch the "partially handled" page.
  800. */
  801. if (!PageAnon(head)) {
  802. pr_err("Memory failure: %#lx: non anonymous thp\n",
  803. page_to_pfn(page));
  804. return 0;
  805. }
  806. }
  807. if (get_page_unless_zero(head)) {
  808. if (head == compound_head(page))
  809. return 1;
  810. pr_info("Memory failure: %#lx cannot catch tail\n",
  811. page_to_pfn(page));
  812. put_page(head);
  813. }
  814. return 0;
  815. }
  816. EXPORT_SYMBOL_GPL(get_hwpoison_page);
  817. /*
  818. * Do all that is necessary to remove user space mappings. Unmap
  819. * the pages and send SIGBUS to the processes if the data was dirty.
  820. */
  821. static bool hwpoison_user_mappings(struct page *p, unsigned long pfn,
  822. int trapno, int flags, struct page **hpagep)
  823. {
  824. enum ttu_flags ttu = TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
  825. struct address_space *mapping;
  826. LIST_HEAD(tokill);
  827. bool unmap_success;
  828. int kill = 1, forcekill;
  829. struct page *hpage = *hpagep;
  830. bool mlocked = PageMlocked(hpage);
  831. /*
  832. * Here we are interested only in user-mapped pages, so skip any
  833. * other types of pages.
  834. */
  835. if (PageReserved(p) || PageSlab(p))
  836. return true;
  837. if (!(PageLRU(hpage) || PageHuge(p)))
  838. return true;
  839. /*
  840. * This check implies we don't kill processes if their pages
  841. * are in the swap cache early. Those are always late kills.
  842. */
  843. if (!page_mapped(hpage))
  844. return true;
  845. if (PageKsm(p)) {
  846. pr_err("Memory failure: %#lx: can't handle KSM pages.\n", pfn);
  847. return false;
  848. }
  849. if (PageSwapCache(p)) {
  850. pr_err("Memory failure: %#lx: keeping poisoned page in swap cache\n",
  851. pfn);
  852. ttu |= TTU_IGNORE_HWPOISON;
  853. }
  854. /*
  855. * Propagate the dirty bit from PTEs to struct page first, because we
  856. * need this to decide if we should kill or just drop the page.
  857. * XXX: the dirty test could be racy: set_page_dirty() may not always
  858. * be called inside page lock (it's recommended but not enforced).
  859. */
  860. mapping = page_mapping(hpage);
  861. if (!(flags & MF_MUST_KILL) && !PageDirty(hpage) && mapping &&
  862. mapping_cap_writeback_dirty(mapping)) {
  863. if (page_mkclean(hpage)) {
  864. SetPageDirty(hpage);
  865. } else {
  866. kill = 0;
  867. ttu |= TTU_IGNORE_HWPOISON;
  868. pr_info("Memory failure: %#lx: corrupted page was clean: dropped without side effects\n",
  869. pfn);
  870. }
  871. }
  872. /*
  873. * First collect all the processes that have the page
  874. * mapped in dirty form. This has to be done before try_to_unmap,
  875. * because ttu takes the rmap data structures down.
  876. *
  877. * Error handling: We ignore errors here because
  878. * there's nothing that can be done.
  879. */
  880. if (kill)
  881. collect_procs(hpage, &tokill, flags & MF_ACTION_REQUIRED);
  882. unmap_success = try_to_unmap(hpage, ttu);
  883. if (!unmap_success)
  884. pr_err("Memory failure: %#lx: failed to unmap page (mapcount=%d)\n",
  885. pfn, page_mapcount(hpage));
  886. /*
  887. * try_to_unmap() might put mlocked page in lru cache, so call
  888. * shake_page() again to ensure that it's flushed.
  889. */
  890. if (mlocked)
  891. shake_page(hpage, 0);
  892. /*
  893. * Now that the dirty bit has been propagated to the
  894. * struct page and all unmaps done we can decide if
  895. * killing is needed or not. Only kill when the page
  896. * was dirty or the process is not restartable,
  897. * otherwise the tokill list is merely
  898. * freed. When there was a problem unmapping earlier
  899. * use a more force-full uncatchable kill to prevent
  900. * any accesses to the poisoned memory.
  901. */
  902. forcekill = PageDirty(hpage) || (flags & MF_MUST_KILL);
  903. kill_procs(&tokill, forcekill, trapno, !unmap_success, p, pfn, flags);
  904. return unmap_success;
  905. }
  906. static void set_page_hwpoison_huge_page(struct page *hpage)
  907. {
  908. int i;
  909. int nr_pages = 1 << compound_order(hpage);
  910. for (i = 0; i < nr_pages; i++)
  911. SetPageHWPoison(hpage + i);
  912. }
  913. static void clear_page_hwpoison_huge_page(struct page *hpage)
  914. {
  915. int i;
  916. int nr_pages = 1 << compound_order(hpage);
  917. for (i = 0; i < nr_pages; i++)
  918. ClearPageHWPoison(hpage + i);
  919. }
  920. /**
  921. * memory_failure - Handle memory failure of a page.
  922. * @pfn: Page Number of the corrupted page
  923. * @trapno: Trap number reported in the signal to user space.
  924. * @flags: fine tune action taken
  925. *
  926. * This function is called by the low level machine check code
  927. * of an architecture when it detects hardware memory corruption
  928. * of a page. It tries its best to recover, which includes
  929. * dropping pages, killing processes etc.
  930. *
  931. * The function is primarily of use for corruptions that
  932. * happen outside the current execution context (e.g. when
  933. * detected by a background scrubber)
  934. *
  935. * Must run in process context (e.g. a work queue) with interrupts
  936. * enabled and no spinlocks hold.
  937. */
  938. int memory_failure(unsigned long pfn, int trapno, int flags)
  939. {
  940. struct page_state *ps;
  941. struct page *p;
  942. struct page *hpage;
  943. struct page *orig_head;
  944. int res;
  945. unsigned int nr_pages;
  946. unsigned long page_flags;
  947. if (!sysctl_memory_failure_recovery)
  948. panic("Memory failure from trap %d on page %lx", trapno, pfn);
  949. if (!pfn_valid(pfn)) {
  950. pr_err("Memory failure: %#lx: memory outside kernel control\n",
  951. pfn);
  952. return -ENXIO;
  953. }
  954. p = pfn_to_page(pfn);
  955. orig_head = hpage = compound_head(p);
  956. if (TestSetPageHWPoison(p)) {
  957. pr_err("Memory failure: %#lx: already hardware poisoned\n",
  958. pfn);
  959. return 0;
  960. }
  961. /*
  962. * Currently errors on hugetlbfs pages are measured in hugepage units,
  963. * so nr_pages should be 1 << compound_order. OTOH when errors are on
  964. * transparent hugepages, they are supposed to be split and error
  965. * measurement is done in normal page units. So nr_pages should be one
  966. * in this case.
  967. */
  968. if (PageHuge(p))
  969. nr_pages = 1 << compound_order(hpage);
  970. else /* normal page or thp */
  971. nr_pages = 1;
  972. num_poisoned_pages_add(nr_pages);
  973. /*
  974. * We need/can do nothing about count=0 pages.
  975. * 1) it's a free page, and therefore in safe hand:
  976. * prep_new_page() will be the gate keeper.
  977. * 2) it's a free hugepage, which is also safe:
  978. * an affected hugepage will be dequeued from hugepage freelist,
  979. * so there's no concern about reusing it ever after.
  980. * 3) it's part of a non-compound high order page.
  981. * Implies some kernel user: cannot stop them from
  982. * R/W the page; let's pray that the page has been
  983. * used and will be freed some time later.
  984. * In fact it's dangerous to directly bump up page count from 0,
  985. * that may make page_freeze_refs()/page_unfreeze_refs() mismatch.
  986. */
  987. if (!(flags & MF_COUNT_INCREASED) && !get_hwpoison_page(p)) {
  988. if (is_free_buddy_page(p)) {
  989. action_result(pfn, MF_MSG_BUDDY, MF_DELAYED);
  990. return 0;
  991. } else if (PageHuge(hpage)) {
  992. /*
  993. * Check "filter hit" and "race with other subpage."
  994. */
  995. lock_page(hpage);
  996. if (PageHWPoison(hpage)) {
  997. if ((hwpoison_filter(p) && TestClearPageHWPoison(p))
  998. || (p != hpage && TestSetPageHWPoison(hpage))) {
  999. num_poisoned_pages_sub(nr_pages);
  1000. unlock_page(hpage);
  1001. return 0;
  1002. }
  1003. }
  1004. set_page_hwpoison_huge_page(hpage);
  1005. res = dequeue_hwpoisoned_huge_page(hpage);
  1006. action_result(pfn, MF_MSG_FREE_HUGE,
  1007. res ? MF_IGNORED : MF_DELAYED);
  1008. unlock_page(hpage);
  1009. return res;
  1010. } else {
  1011. action_result(pfn, MF_MSG_KERNEL_HIGH_ORDER, MF_IGNORED);
  1012. return -EBUSY;
  1013. }
  1014. }
  1015. if (!PageHuge(p) && PageTransHuge(hpage)) {
  1016. lock_page(p);
  1017. if (!PageAnon(p) || unlikely(split_huge_page(p))) {
  1018. unlock_page(p);
  1019. if (!PageAnon(p))
  1020. pr_err("Memory failure: %#lx: non anonymous thp\n",
  1021. pfn);
  1022. else
  1023. pr_err("Memory failure: %#lx: thp split failed\n",
  1024. pfn);
  1025. if (TestClearPageHWPoison(p))
  1026. num_poisoned_pages_sub(nr_pages);
  1027. put_hwpoison_page(p);
  1028. return -EBUSY;
  1029. }
  1030. unlock_page(p);
  1031. VM_BUG_ON_PAGE(!page_count(p), p);
  1032. hpage = compound_head(p);
  1033. }
  1034. /*
  1035. * We ignore non-LRU pages for good reasons.
  1036. * - PG_locked is only well defined for LRU pages and a few others
  1037. * - to avoid races with __SetPageLocked()
  1038. * - to avoid races with __SetPageSlab*() (and more non-atomic ops)
  1039. * The check (unnecessarily) ignores LRU pages being isolated and
  1040. * walked by the page reclaim code, however that's not a big loss.
  1041. */
  1042. shake_page(p, 0);
  1043. /* shake_page could have turned it free. */
  1044. if (!PageLRU(p) && is_free_buddy_page(p)) {
  1045. if (flags & MF_COUNT_INCREASED)
  1046. action_result(pfn, MF_MSG_BUDDY, MF_DELAYED);
  1047. else
  1048. action_result(pfn, MF_MSG_BUDDY_2ND, MF_DELAYED);
  1049. return 0;
  1050. }
  1051. lock_page(hpage);
  1052. /*
  1053. * The page could have changed compound pages during the locking.
  1054. * If this happens just bail out.
  1055. */
  1056. if (PageCompound(p) && compound_head(p) != orig_head) {
  1057. action_result(pfn, MF_MSG_DIFFERENT_COMPOUND, MF_IGNORED);
  1058. res = -EBUSY;
  1059. goto out;
  1060. }
  1061. /*
  1062. * We use page flags to determine what action should be taken, but
  1063. * the flags can be modified by the error containment action. One
  1064. * example is an mlocked page, where PG_mlocked is cleared by
  1065. * page_remove_rmap() in try_to_unmap_one(). So to determine page status
  1066. * correctly, we save a copy of the page flags at this time.
  1067. */
  1068. if (PageHuge(p))
  1069. page_flags = hpage->flags;
  1070. else
  1071. page_flags = p->flags;
  1072. /*
  1073. * unpoison always clear PG_hwpoison inside page lock
  1074. */
  1075. if (!PageHWPoison(p)) {
  1076. pr_err("Memory failure: %#lx: just unpoisoned\n", pfn);
  1077. num_poisoned_pages_sub(nr_pages);
  1078. unlock_page(hpage);
  1079. put_hwpoison_page(hpage);
  1080. return 0;
  1081. }
  1082. if (hwpoison_filter(p)) {
  1083. if (TestClearPageHWPoison(p))
  1084. num_poisoned_pages_sub(nr_pages);
  1085. unlock_page(hpage);
  1086. put_hwpoison_page(hpage);
  1087. return 0;
  1088. }
  1089. if (!PageHuge(p) && !PageTransTail(p) && !PageLRU(p))
  1090. goto identify_page_state;
  1091. /*
  1092. * For error on the tail page, we should set PG_hwpoison
  1093. * on the head page to show that the hugepage is hwpoisoned
  1094. */
  1095. if (PageHuge(p) && PageTail(p) && TestSetPageHWPoison(hpage)) {
  1096. action_result(pfn, MF_MSG_POISONED_HUGE, MF_IGNORED);
  1097. unlock_page(hpage);
  1098. put_hwpoison_page(hpage);
  1099. return 0;
  1100. }
  1101. /*
  1102. * Set PG_hwpoison on all pages in an error hugepage,
  1103. * because containment is done in hugepage unit for now.
  1104. * Since we have done TestSetPageHWPoison() for the head page with
  1105. * page lock held, we can safely set PG_hwpoison bits on tail pages.
  1106. */
  1107. if (PageHuge(p))
  1108. set_page_hwpoison_huge_page(hpage);
  1109. /*
  1110. * It's very difficult to mess with pages currently under IO
  1111. * and in many cases impossible, so we just avoid it here.
  1112. */
  1113. wait_on_page_writeback(p);
  1114. /*
  1115. * Now take care of user space mappings.
  1116. * Abort on fail: __delete_from_page_cache() assumes unmapped page.
  1117. *
  1118. * When the raw error page is thp tail page, hpage points to the raw
  1119. * page after thp split.
  1120. */
  1121. if (!hwpoison_user_mappings(p, pfn, trapno, flags, &hpage)) {
  1122. action_result(pfn, MF_MSG_UNMAP_FAILED, MF_IGNORED);
  1123. res = -EBUSY;
  1124. goto out;
  1125. }
  1126. /*
  1127. * Torn down by someone else?
  1128. */
  1129. if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
  1130. action_result(pfn, MF_MSG_TRUNCATED_LRU, MF_IGNORED);
  1131. res = -EBUSY;
  1132. goto out;
  1133. }
  1134. identify_page_state:
  1135. res = -EBUSY;
  1136. /*
  1137. * The first check uses the current page flags which may not have any
  1138. * relevant information. The second check with the saved page flagss is
  1139. * carried out only if the first check can't determine the page status.
  1140. */
  1141. for (ps = error_states;; ps++)
  1142. if ((p->flags & ps->mask) == ps->res)
  1143. break;
  1144. page_flags |= (p->flags & (1UL << PG_dirty));
  1145. if (!ps->mask)
  1146. for (ps = error_states;; ps++)
  1147. if ((page_flags & ps->mask) == ps->res)
  1148. break;
  1149. res = page_action(ps, p, pfn);
  1150. out:
  1151. unlock_page(hpage);
  1152. return res;
  1153. }
  1154. EXPORT_SYMBOL_GPL(memory_failure);
  1155. #define MEMORY_FAILURE_FIFO_ORDER 4
  1156. #define MEMORY_FAILURE_FIFO_SIZE (1 << MEMORY_FAILURE_FIFO_ORDER)
  1157. struct memory_failure_entry {
  1158. unsigned long pfn;
  1159. int trapno;
  1160. int flags;
  1161. };
  1162. struct memory_failure_cpu {
  1163. DECLARE_KFIFO(fifo, struct memory_failure_entry,
  1164. MEMORY_FAILURE_FIFO_SIZE);
  1165. spinlock_t lock;
  1166. struct work_struct work;
  1167. };
  1168. static DEFINE_PER_CPU(struct memory_failure_cpu, memory_failure_cpu);
  1169. /**
  1170. * memory_failure_queue - Schedule handling memory failure of a page.
  1171. * @pfn: Page Number of the corrupted page
  1172. * @trapno: Trap number reported in the signal to user space.
  1173. * @flags: Flags for memory failure handling
  1174. *
  1175. * This function is called by the low level hardware error handler
  1176. * when it detects hardware memory corruption of a page. It schedules
  1177. * the recovering of error page, including dropping pages, killing
  1178. * processes etc.
  1179. *
  1180. * The function is primarily of use for corruptions that
  1181. * happen outside the current execution context (e.g. when
  1182. * detected by a background scrubber)
  1183. *
  1184. * Can run in IRQ context.
  1185. */
  1186. void memory_failure_queue(unsigned long pfn, int trapno, int flags)
  1187. {
  1188. struct memory_failure_cpu *mf_cpu;
  1189. unsigned long proc_flags;
  1190. struct memory_failure_entry entry = {
  1191. .pfn = pfn,
  1192. .trapno = trapno,
  1193. .flags = flags,
  1194. };
  1195. mf_cpu = &get_cpu_var(memory_failure_cpu);
  1196. spin_lock_irqsave(&mf_cpu->lock, proc_flags);
  1197. if (kfifo_put(&mf_cpu->fifo, entry))
  1198. schedule_work_on(smp_processor_id(), &mf_cpu->work);
  1199. else
  1200. pr_err("Memory failure: buffer overflow when queuing memory failure at %#lx\n",
  1201. pfn);
  1202. spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
  1203. put_cpu_var(memory_failure_cpu);
  1204. }
  1205. EXPORT_SYMBOL_GPL(memory_failure_queue);
  1206. static void memory_failure_work_func(struct work_struct *work)
  1207. {
  1208. struct memory_failure_cpu *mf_cpu;
  1209. struct memory_failure_entry entry = { 0, };
  1210. unsigned long proc_flags;
  1211. int gotten;
  1212. mf_cpu = this_cpu_ptr(&memory_failure_cpu);
  1213. for (;;) {
  1214. spin_lock_irqsave(&mf_cpu->lock, proc_flags);
  1215. gotten = kfifo_get(&mf_cpu->fifo, &entry);
  1216. spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
  1217. if (!gotten)
  1218. break;
  1219. if (entry.flags & MF_SOFT_OFFLINE)
  1220. soft_offline_page(pfn_to_page(entry.pfn), entry.flags);
  1221. else
  1222. memory_failure(entry.pfn, entry.trapno, entry.flags);
  1223. }
  1224. }
  1225. static int __init memory_failure_init(void)
  1226. {
  1227. struct memory_failure_cpu *mf_cpu;
  1228. int cpu;
  1229. for_each_possible_cpu(cpu) {
  1230. mf_cpu = &per_cpu(memory_failure_cpu, cpu);
  1231. spin_lock_init(&mf_cpu->lock);
  1232. INIT_KFIFO(mf_cpu->fifo);
  1233. INIT_WORK(&mf_cpu->work, memory_failure_work_func);
  1234. }
  1235. return 0;
  1236. }
  1237. core_initcall(memory_failure_init);
  1238. #define unpoison_pr_info(fmt, pfn, rs) \
  1239. ({ \
  1240. if (__ratelimit(rs)) \
  1241. pr_info(fmt, pfn); \
  1242. })
  1243. /**
  1244. * unpoison_memory - Unpoison a previously poisoned page
  1245. * @pfn: Page number of the to be unpoisoned page
  1246. *
  1247. * Software-unpoison a page that has been poisoned by
  1248. * memory_failure() earlier.
  1249. *
  1250. * This is only done on the software-level, so it only works
  1251. * for linux injected failures, not real hardware failures
  1252. *
  1253. * Returns 0 for success, otherwise -errno.
  1254. */
  1255. int unpoison_memory(unsigned long pfn)
  1256. {
  1257. struct page *page;
  1258. struct page *p;
  1259. int freeit = 0;
  1260. unsigned int nr_pages;
  1261. static DEFINE_RATELIMIT_STATE(unpoison_rs, DEFAULT_RATELIMIT_INTERVAL,
  1262. DEFAULT_RATELIMIT_BURST);
  1263. if (!pfn_valid(pfn))
  1264. return -ENXIO;
  1265. p = pfn_to_page(pfn);
  1266. page = compound_head(p);
  1267. if (!PageHWPoison(p)) {
  1268. unpoison_pr_info("Unpoison: Page was already unpoisoned %#lx\n",
  1269. pfn, &unpoison_rs);
  1270. return 0;
  1271. }
  1272. if (page_count(page) > 1) {
  1273. unpoison_pr_info("Unpoison: Someone grabs the hwpoison page %#lx\n",
  1274. pfn, &unpoison_rs);
  1275. return 0;
  1276. }
  1277. if (page_mapped(page)) {
  1278. unpoison_pr_info("Unpoison: Someone maps the hwpoison page %#lx\n",
  1279. pfn, &unpoison_rs);
  1280. return 0;
  1281. }
  1282. if (page_mapping(page)) {
  1283. unpoison_pr_info("Unpoison: the hwpoison page has non-NULL mapping %#lx\n",
  1284. pfn, &unpoison_rs);
  1285. return 0;
  1286. }
  1287. /*
  1288. * unpoison_memory() can encounter thp only when the thp is being
  1289. * worked by memory_failure() and the page lock is not held yet.
  1290. * In such case, we yield to memory_failure() and make unpoison fail.
  1291. */
  1292. if (!PageHuge(page) && PageTransHuge(page)) {
  1293. unpoison_pr_info("Unpoison: Memory failure is now running on %#lx\n",
  1294. pfn, &unpoison_rs);
  1295. return 0;
  1296. }
  1297. nr_pages = 1 << compound_order(page);
  1298. if (!get_hwpoison_page(p)) {
  1299. /*
  1300. * Since HWPoisoned hugepage should have non-zero refcount,
  1301. * race between memory failure and unpoison seems to happen.
  1302. * In such case unpoison fails and memory failure runs
  1303. * to the end.
  1304. */
  1305. if (PageHuge(page)) {
  1306. unpoison_pr_info("Unpoison: Memory failure is now running on free hugepage %#lx\n",
  1307. pfn, &unpoison_rs);
  1308. return 0;
  1309. }
  1310. if (TestClearPageHWPoison(p))
  1311. num_poisoned_pages_dec();
  1312. unpoison_pr_info("Unpoison: Software-unpoisoned free page %#lx\n",
  1313. pfn, &unpoison_rs);
  1314. return 0;
  1315. }
  1316. lock_page(page);
  1317. /*
  1318. * This test is racy because PG_hwpoison is set outside of page lock.
  1319. * That's acceptable because that won't trigger kernel panic. Instead,
  1320. * the PG_hwpoison page will be caught and isolated on the entrance to
  1321. * the free buddy page pool.
  1322. */
  1323. if (TestClearPageHWPoison(page)) {
  1324. unpoison_pr_info("Unpoison: Software-unpoisoned page %#lx\n",
  1325. pfn, &unpoison_rs);
  1326. num_poisoned_pages_sub(nr_pages);
  1327. freeit = 1;
  1328. if (PageHuge(page))
  1329. clear_page_hwpoison_huge_page(page);
  1330. }
  1331. unlock_page(page);
  1332. put_hwpoison_page(page);
  1333. if (freeit && !(pfn == my_zero_pfn(0) && page_count(p) == 1))
  1334. put_hwpoison_page(page);
  1335. return 0;
  1336. }
  1337. EXPORT_SYMBOL(unpoison_memory);
  1338. static struct page *new_page(struct page *p, unsigned long private, int **x)
  1339. {
  1340. int nid = page_to_nid(p);
  1341. if (PageHuge(p)) {
  1342. struct hstate *hstate = page_hstate(compound_head(p));
  1343. if (hstate_is_gigantic(hstate))
  1344. return alloc_huge_page_node(hstate, NUMA_NO_NODE);
  1345. return alloc_huge_page_node(hstate, nid);
  1346. } else {
  1347. return __alloc_pages_node(nid, GFP_HIGHUSER_MOVABLE, 0);
  1348. }
  1349. }
  1350. /*
  1351. * Safely get reference count of an arbitrary page.
  1352. * Returns 0 for a free page, -EIO for a zero refcount page
  1353. * that is not free, and 1 for any other page type.
  1354. * For 1 the page is returned with increased page count, otherwise not.
  1355. */
  1356. static int __get_any_page(struct page *p, unsigned long pfn, int flags)
  1357. {
  1358. int ret;
  1359. if (flags & MF_COUNT_INCREASED)
  1360. return 1;
  1361. /*
  1362. * When the target page is a free hugepage, just remove it
  1363. * from free hugepage list.
  1364. */
  1365. if (!get_hwpoison_page(p)) {
  1366. if (PageHuge(p)) {
  1367. pr_info("%s: %#lx free huge page\n", __func__, pfn);
  1368. ret = 0;
  1369. } else if (is_free_buddy_page(p)) {
  1370. pr_info("%s: %#lx free buddy page\n", __func__, pfn);
  1371. ret = 0;
  1372. } else {
  1373. pr_info("%s: %#lx: unknown zero refcount page type %lx\n",
  1374. __func__, pfn, p->flags);
  1375. ret = -EIO;
  1376. }
  1377. } else {
  1378. /* Not a free page */
  1379. ret = 1;
  1380. }
  1381. return ret;
  1382. }
  1383. static int get_any_page(struct page *page, unsigned long pfn, int flags)
  1384. {
  1385. int ret = __get_any_page(page, pfn, flags);
  1386. if (ret == 1 && !PageHuge(page) &&
  1387. !PageLRU(page) && !__PageMovable(page)) {
  1388. /*
  1389. * Try to free it.
  1390. */
  1391. put_hwpoison_page(page);
  1392. shake_page(page, 1);
  1393. /*
  1394. * Did it turn free?
  1395. */
  1396. ret = __get_any_page(page, pfn, 0);
  1397. if (ret == 1 && !PageLRU(page)) {
  1398. /* Drop page reference which is from __get_any_page() */
  1399. put_hwpoison_page(page);
  1400. pr_info("soft_offline: %#lx: unknown non LRU page type %lx (%pGp)\n",
  1401. pfn, page->flags, &page->flags);
  1402. return -EIO;
  1403. }
  1404. }
  1405. return ret;
  1406. }
  1407. static int soft_offline_huge_page(struct page *page, int flags)
  1408. {
  1409. int ret;
  1410. unsigned long pfn = page_to_pfn(page);
  1411. struct page *hpage = compound_head(page);
  1412. LIST_HEAD(pagelist);
  1413. /*
  1414. * This double-check of PageHWPoison is to avoid the race with
  1415. * memory_failure(). See also comment in __soft_offline_page().
  1416. */
  1417. lock_page(hpage);
  1418. if (PageHWPoison(hpage)) {
  1419. unlock_page(hpage);
  1420. put_hwpoison_page(hpage);
  1421. pr_info("soft offline: %#lx hugepage already poisoned\n", pfn);
  1422. return -EBUSY;
  1423. }
  1424. unlock_page(hpage);
  1425. ret = isolate_huge_page(hpage, &pagelist);
  1426. /*
  1427. * get_any_page() and isolate_huge_page() takes a refcount each,
  1428. * so need to drop one here.
  1429. */
  1430. put_hwpoison_page(hpage);
  1431. if (!ret) {
  1432. pr_info("soft offline: %#lx hugepage failed to isolate\n", pfn);
  1433. return -EBUSY;
  1434. }
  1435. ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
  1436. MIGRATE_SYNC, MR_MEMORY_FAILURE);
  1437. if (ret) {
  1438. pr_info("soft offline: %#lx: migration failed %d, type %lx (%pGp)\n",
  1439. pfn, ret, page->flags, &page->flags);
  1440. if (!list_empty(&pagelist))
  1441. putback_movable_pages(&pagelist);
  1442. if (ret > 0)
  1443. ret = -EIO;
  1444. } else {
  1445. /* overcommit hugetlb page will be freed to buddy */
  1446. if (PageHuge(page)) {
  1447. set_page_hwpoison_huge_page(hpage);
  1448. dequeue_hwpoisoned_huge_page(hpage);
  1449. num_poisoned_pages_add(1 << compound_order(hpage));
  1450. } else {
  1451. SetPageHWPoison(page);
  1452. num_poisoned_pages_inc();
  1453. }
  1454. }
  1455. return ret;
  1456. }
  1457. static int __soft_offline_page(struct page *page, int flags)
  1458. {
  1459. int ret;
  1460. unsigned long pfn = page_to_pfn(page);
  1461. /*
  1462. * Check PageHWPoison again inside page lock because PageHWPoison
  1463. * is set by memory_failure() outside page lock. Note that
  1464. * memory_failure() also double-checks PageHWPoison inside page lock,
  1465. * so there's no race between soft_offline_page() and memory_failure().
  1466. */
  1467. lock_page(page);
  1468. wait_on_page_writeback(page);
  1469. if (PageHWPoison(page)) {
  1470. unlock_page(page);
  1471. put_hwpoison_page(page);
  1472. pr_info("soft offline: %#lx page already poisoned\n", pfn);
  1473. return -EBUSY;
  1474. }
  1475. /*
  1476. * Try to invalidate first. This should work for
  1477. * non dirty unmapped page cache pages.
  1478. */
  1479. ret = invalidate_inode_page(page);
  1480. unlock_page(page);
  1481. /*
  1482. * RED-PEN would be better to keep it isolated here, but we
  1483. * would need to fix isolation locking first.
  1484. */
  1485. if (ret == 1) {
  1486. put_hwpoison_page(page);
  1487. pr_info("soft_offline: %#lx: invalidated\n", pfn);
  1488. SetPageHWPoison(page);
  1489. num_poisoned_pages_inc();
  1490. return 0;
  1491. }
  1492. /*
  1493. * Simple invalidation didn't work.
  1494. * Try to migrate to a new page instead. migrate.c
  1495. * handles a large number of cases for us.
  1496. */
  1497. if (PageLRU(page))
  1498. ret = isolate_lru_page(page);
  1499. else
  1500. ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
  1501. /*
  1502. * Drop page reference which is came from get_any_page()
  1503. * successful isolate_lru_page() already took another one.
  1504. */
  1505. put_hwpoison_page(page);
  1506. if (!ret) {
  1507. LIST_HEAD(pagelist);
  1508. /*
  1509. * After isolated lru page, the PageLRU will be cleared,
  1510. * so use !__PageMovable instead for LRU page's mapping
  1511. * cannot have PAGE_MAPPING_MOVABLE.
  1512. */
  1513. if (!__PageMovable(page))
  1514. inc_node_page_state(page, NR_ISOLATED_ANON +
  1515. page_is_file_cache(page));
  1516. list_add(&page->lru, &pagelist);
  1517. ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
  1518. MIGRATE_SYNC, MR_MEMORY_FAILURE);
  1519. if (ret) {
  1520. if (!list_empty(&pagelist))
  1521. putback_movable_pages(&pagelist);
  1522. pr_info("soft offline: %#lx: migration failed %d, type %lx (%pGp)\n",
  1523. pfn, ret, page->flags, &page->flags);
  1524. if (ret > 0)
  1525. ret = -EIO;
  1526. }
  1527. } else {
  1528. pr_info("soft offline: %#lx: isolation failed: %d, page count %d, type %lx (%pGp)\n",
  1529. pfn, ret, page_count(page), page->flags, &page->flags);
  1530. }
  1531. return ret;
  1532. }
  1533. static int soft_offline_in_use_page(struct page *page, int flags)
  1534. {
  1535. int ret;
  1536. struct page *hpage = compound_head(page);
  1537. if (!PageHuge(page) && PageTransHuge(hpage)) {
  1538. lock_page(hpage);
  1539. if (!PageAnon(hpage) || unlikely(split_huge_page(hpage))) {
  1540. unlock_page(hpage);
  1541. if (!PageAnon(hpage))
  1542. pr_info("soft offline: %#lx: non anonymous thp\n", page_to_pfn(page));
  1543. else
  1544. pr_info("soft offline: %#lx: thp split failed\n", page_to_pfn(page));
  1545. put_hwpoison_page(hpage);
  1546. return -EBUSY;
  1547. }
  1548. unlock_page(hpage);
  1549. get_hwpoison_page(page);
  1550. put_hwpoison_page(hpage);
  1551. }
  1552. if (PageHuge(page))
  1553. ret = soft_offline_huge_page(page, flags);
  1554. else
  1555. ret = __soft_offline_page(page, flags);
  1556. return ret;
  1557. }
  1558. static void soft_offline_free_page(struct page *page)
  1559. {
  1560. if (PageHuge(page)) {
  1561. struct page *hpage = compound_head(page);
  1562. set_page_hwpoison_huge_page(hpage);
  1563. if (!dequeue_hwpoisoned_huge_page(hpage))
  1564. num_poisoned_pages_add(1 << compound_order(hpage));
  1565. } else {
  1566. if (!TestSetPageHWPoison(page))
  1567. num_poisoned_pages_inc();
  1568. }
  1569. }
  1570. /**
  1571. * soft_offline_page - Soft offline a page.
  1572. * @page: page to offline
  1573. * @flags: flags. Same as memory_failure().
  1574. *
  1575. * Returns 0 on success, otherwise negated errno.
  1576. *
  1577. * Soft offline a page, by migration or invalidation,
  1578. * without killing anything. This is for the case when
  1579. * a page is not corrupted yet (so it's still valid to access),
  1580. * but has had a number of corrected errors and is better taken
  1581. * out.
  1582. *
  1583. * The actual policy on when to do that is maintained by
  1584. * user space.
  1585. *
  1586. * This should never impact any application or cause data loss,
  1587. * however it might take some time.
  1588. *
  1589. * This is not a 100% solution for all memory, but tries to be
  1590. * ``good enough'' for the majority of memory.
  1591. */
  1592. int soft_offline_page(struct page *page, int flags)
  1593. {
  1594. int ret;
  1595. unsigned long pfn = page_to_pfn(page);
  1596. if (PageHWPoison(page)) {
  1597. pr_info("soft offline: %#lx page already poisoned\n", pfn);
  1598. if (flags & MF_COUNT_INCREASED)
  1599. put_hwpoison_page(page);
  1600. return -EBUSY;
  1601. }
  1602. get_online_mems();
  1603. ret = get_any_page(page, pfn, flags);
  1604. put_online_mems();
  1605. if (ret > 0)
  1606. ret = soft_offline_in_use_page(page, flags);
  1607. else if (ret == 0)
  1608. soft_offline_free_page(page);
  1609. return ret;
  1610. }