memory-failure.c 52 KB

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