memory-failure.c 52 KB

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