memory-failure.c 52 KB

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