memory-failure.c 52 KB

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