memory-failure.c 51 KB

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