memory-failure.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631
  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. #define pr_fmt(fmt) "Memory failure: " fmt
  37. #include <linux/kernel.h>
  38. #include <linux/mm.h>
  39. #include <linux/page-flags.h>
  40. #include <linux/kernel-page-flags.h>
  41. #include <linux/sched/signal.h>
  42. #include <linux/sched/task.h>
  43. #include <linux/dax.h>
  44. #include <linux/ksm.h>
  45. #include <linux/rmap.h>
  46. #include <linux/export.h>
  47. #include <linux/pagemap.h>
  48. #include <linux/swap.h>
  49. #include <linux/backing-dev.h>
  50. #include <linux/migrate.h>
  51. #include <linux/suspend.h>
  52. #include <linux/slab.h>
  53. #include <linux/swapops.h>
  54. #include <linux/hugetlb.h>
  55. #include <linux/memory_hotplug.h>
  56. #include <linux/mm_inline.h>
  57. #include <linux/memremap.h>
  58. #include <linux/kfifo.h>
  59. #include <linux/ratelimit.h>
  60. #include <linux/page-isolation.h>
  61. #include <linux/pagewalk.h>
  62. #include <linux/shmem_fs.h>
  63. #include "swap.h"
  64. #include "internal.h"
  65. #include "ras/ras_event.h"
  66. int sysctl_memory_failure_early_kill __read_mostly = 0;
  67. int sysctl_memory_failure_recovery __read_mostly = 1;
  68. atomic_long_t num_poisoned_pages __read_mostly = ATOMIC_LONG_INIT(0);
  69. static bool hw_memory_failure __read_mostly = false;
  70. /*
  71. * Return values:
  72. * 1: the page is dissolved (if needed) and taken off from buddy,
  73. * 0: the page is dissolved (if needed) and not taken off from buddy,
  74. * < 0: failed to dissolve.
  75. */
  76. static int __page_handle_poison(struct page *page)
  77. {
  78. int ret;
  79. zone_pcp_disable(page_zone(page));
  80. ret = dissolve_free_huge_page(page);
  81. if (!ret)
  82. ret = take_page_off_buddy(page);
  83. zone_pcp_enable(page_zone(page));
  84. return ret;
  85. }
  86. static bool page_handle_poison(struct page *page, bool hugepage_or_freepage, bool release)
  87. {
  88. if (hugepage_or_freepage) {
  89. /*
  90. * Doing this check for free pages is also fine since dissolve_free_huge_page
  91. * returns 0 for non-hugetlb pages as well.
  92. */
  93. if (__page_handle_poison(page) <= 0)
  94. /*
  95. * We could fail to take off the target page from buddy
  96. * for example due to racy page allocation, but that's
  97. * acceptable because soft-offlined page is not broken
  98. * and if someone really want to use it, they should
  99. * take it.
  100. */
  101. return false;
  102. }
  103. SetPageHWPoison(page);
  104. if (release)
  105. put_page(page);
  106. page_ref_inc(page);
  107. num_poisoned_pages_inc();
  108. return true;
  109. }
  110. #if defined(CONFIG_HWPOISON_INJECT) || defined(CONFIG_HWPOISON_INJECT_MODULE)
  111. u32 hwpoison_filter_enable = 0;
  112. u32 hwpoison_filter_dev_major = ~0U;
  113. u32 hwpoison_filter_dev_minor = ~0U;
  114. u64 hwpoison_filter_flags_mask;
  115. u64 hwpoison_filter_flags_value;
  116. EXPORT_SYMBOL_GPL(hwpoison_filter_enable);
  117. EXPORT_SYMBOL_GPL(hwpoison_filter_dev_major);
  118. EXPORT_SYMBOL_GPL(hwpoison_filter_dev_minor);
  119. EXPORT_SYMBOL_GPL(hwpoison_filter_flags_mask);
  120. EXPORT_SYMBOL_GPL(hwpoison_filter_flags_value);
  121. static int hwpoison_filter_dev(struct page *p)
  122. {
  123. struct address_space *mapping;
  124. dev_t dev;
  125. if (hwpoison_filter_dev_major == ~0U &&
  126. hwpoison_filter_dev_minor == ~0U)
  127. return 0;
  128. mapping = page_mapping(p);
  129. if (mapping == NULL || mapping->host == NULL)
  130. return -EINVAL;
  131. dev = mapping->host->i_sb->s_dev;
  132. if (hwpoison_filter_dev_major != ~0U &&
  133. hwpoison_filter_dev_major != MAJOR(dev))
  134. return -EINVAL;
  135. if (hwpoison_filter_dev_minor != ~0U &&
  136. hwpoison_filter_dev_minor != MINOR(dev))
  137. return -EINVAL;
  138. return 0;
  139. }
  140. static int hwpoison_filter_flags(struct page *p)
  141. {
  142. if (!hwpoison_filter_flags_mask)
  143. return 0;
  144. if ((stable_page_flags(p) & hwpoison_filter_flags_mask) ==
  145. hwpoison_filter_flags_value)
  146. return 0;
  147. else
  148. return -EINVAL;
  149. }
  150. /*
  151. * This allows stress tests to limit test scope to a collection of tasks
  152. * by putting them under some memcg. This prevents killing unrelated/important
  153. * processes such as /sbin/init. Note that the target task may share clean
  154. * pages with init (eg. libc text), which is harmless. If the target task
  155. * share _dirty_ pages with another task B, the test scheme must make sure B
  156. * is also included in the memcg. At last, due to race conditions this filter
  157. * can only guarantee that the page either belongs to the memcg tasks, or is
  158. * a freed page.
  159. */
  160. #ifdef CONFIG_MEMCG
  161. u64 hwpoison_filter_memcg;
  162. EXPORT_SYMBOL_GPL(hwpoison_filter_memcg);
  163. static int hwpoison_filter_task(struct page *p)
  164. {
  165. if (!hwpoison_filter_memcg)
  166. return 0;
  167. if (page_cgroup_ino(p) != hwpoison_filter_memcg)
  168. return -EINVAL;
  169. return 0;
  170. }
  171. #else
  172. static int hwpoison_filter_task(struct page *p) { return 0; }
  173. #endif
  174. int hwpoison_filter(struct page *p)
  175. {
  176. if (!hwpoison_filter_enable)
  177. return 0;
  178. if (hwpoison_filter_dev(p))
  179. return -EINVAL;
  180. if (hwpoison_filter_flags(p))
  181. return -EINVAL;
  182. if (hwpoison_filter_task(p))
  183. return -EINVAL;
  184. return 0;
  185. }
  186. #else
  187. int hwpoison_filter(struct page *p)
  188. {
  189. return 0;
  190. }
  191. #endif
  192. EXPORT_SYMBOL_GPL(hwpoison_filter);
  193. /*
  194. * Kill all processes that have a poisoned page mapped and then isolate
  195. * the page.
  196. *
  197. * General strategy:
  198. * Find all processes having the page mapped and kill them.
  199. * But we keep a page reference around so that the page is not
  200. * actually freed yet.
  201. * Then stash the page away
  202. *
  203. * There's no convenient way to get back to mapped processes
  204. * from the VMAs. So do a brute-force search over all
  205. * running processes.
  206. *
  207. * Remember that machine checks are not common (or rather
  208. * if they are common you have other problems), so this shouldn't
  209. * be a performance issue.
  210. *
  211. * Also there are some races possible while we get from the
  212. * error detection to actually handle it.
  213. */
  214. struct to_kill {
  215. struct list_head nd;
  216. struct task_struct *tsk;
  217. unsigned long addr;
  218. short size_shift;
  219. };
  220. /*
  221. * Send all the processes who have the page mapped a signal.
  222. * ``action optional'' if they are not immediately affected by the error
  223. * ``action required'' if error happened in current execution context
  224. */
  225. static int kill_proc(struct to_kill *tk, unsigned long pfn, int flags)
  226. {
  227. struct task_struct *t = tk->tsk;
  228. short addr_lsb = tk->size_shift;
  229. int ret = 0;
  230. pr_err("%#lx: Sending SIGBUS to %s:%d due to hardware memory corruption\n",
  231. pfn, t->comm, t->pid);
  232. if ((flags & MF_ACTION_REQUIRED) && (t == current))
  233. ret = force_sig_mceerr(BUS_MCEERR_AR,
  234. (void __user *)tk->addr, addr_lsb);
  235. else
  236. /*
  237. * Signal other processes sharing the page if they have
  238. * PF_MCE_EARLY set.
  239. * Don't use force here, it's convenient if the signal
  240. * can be temporarily blocked.
  241. * This could cause a loop when the user sets SIGBUS
  242. * to SIG_IGN, but hopefully no one will do that?
  243. */
  244. ret = send_sig_mceerr(BUS_MCEERR_AO, (void __user *)tk->addr,
  245. addr_lsb, t);
  246. if (ret < 0)
  247. pr_info("Error sending signal to %s:%d: %d\n",
  248. t->comm, t->pid, ret);
  249. return ret;
  250. }
  251. /*
  252. * Unknown page type encountered. Try to check whether it can turn PageLRU by
  253. * lru_add_drain_all.
  254. */
  255. void shake_page(struct page *p)
  256. {
  257. if (PageHuge(p))
  258. return;
  259. if (!PageSlab(p)) {
  260. lru_add_drain_all();
  261. if (PageLRU(p) || is_free_buddy_page(p))
  262. return;
  263. }
  264. /*
  265. * TODO: Could shrink slab caches here if a lightweight range-based
  266. * shrinker will be available.
  267. */
  268. }
  269. EXPORT_SYMBOL_GPL(shake_page);
  270. static unsigned long dev_pagemap_mapping_shift(struct vm_area_struct *vma,
  271. unsigned long address)
  272. {
  273. unsigned long ret = 0;
  274. pgd_t *pgd;
  275. p4d_t *p4d;
  276. pud_t *pud;
  277. pmd_t *pmd;
  278. pte_t *pte;
  279. VM_BUG_ON_VMA(address == -EFAULT, vma);
  280. pgd = pgd_offset(vma->vm_mm, address);
  281. if (!pgd_present(*pgd))
  282. return 0;
  283. p4d = p4d_offset(pgd, address);
  284. if (!p4d_present(*p4d))
  285. return 0;
  286. pud = pud_offset(p4d, address);
  287. if (!pud_present(*pud))
  288. return 0;
  289. if (pud_devmap(*pud))
  290. return PUD_SHIFT;
  291. pmd = pmd_offset(pud, address);
  292. if (!pmd_present(*pmd))
  293. return 0;
  294. if (pmd_devmap(*pmd))
  295. return PMD_SHIFT;
  296. pte = pte_offset_map(pmd, address);
  297. if (pte_present(*pte) && pte_devmap(*pte))
  298. ret = PAGE_SHIFT;
  299. pte_unmap(pte);
  300. return ret;
  301. }
  302. /*
  303. * Failure handling: if we can't find or can't kill a process there's
  304. * not much we can do. We just print a message and ignore otherwise.
  305. */
  306. #define FSDAX_INVALID_PGOFF ULONG_MAX
  307. /*
  308. * Schedule a process for later kill.
  309. * Uses GFP_ATOMIC allocations to avoid potential recursions in the VM.
  310. *
  311. * Note: @fsdax_pgoff is used only when @p is a fsdax page and a
  312. * filesystem with a memory failure handler has claimed the
  313. * memory_failure event. In all other cases, page->index and
  314. * page->mapping are sufficient for mapping the page back to its
  315. * corresponding user virtual address.
  316. */
  317. static void add_to_kill(struct task_struct *tsk, struct page *p,
  318. pgoff_t fsdax_pgoff, struct vm_area_struct *vma,
  319. struct list_head *to_kill)
  320. {
  321. struct to_kill *tk;
  322. tk = kmalloc(sizeof(struct to_kill), GFP_ATOMIC);
  323. if (!tk) {
  324. pr_err("Out of memory while machine check handling\n");
  325. return;
  326. }
  327. tk->addr = page_address_in_vma(p, vma);
  328. if (is_zone_device_page(p)) {
  329. if (fsdax_pgoff != FSDAX_INVALID_PGOFF)
  330. tk->addr = vma_pgoff_address(fsdax_pgoff, 1, vma);
  331. tk->size_shift = dev_pagemap_mapping_shift(vma, tk->addr);
  332. } else
  333. tk->size_shift = page_shift(compound_head(p));
  334. /*
  335. * Send SIGKILL if "tk->addr == -EFAULT". Also, as
  336. * "tk->size_shift" is always non-zero for !is_zone_device_page(),
  337. * so "tk->size_shift == 0" effectively checks no mapping on
  338. * ZONE_DEVICE. Indeed, when a devdax page is mmapped N times
  339. * to a process' address space, it's possible not all N VMAs
  340. * contain mappings for the page, but at least one VMA does.
  341. * Only deliver SIGBUS with payload derived from the VMA that
  342. * has a mapping for the page.
  343. */
  344. if (tk->addr == -EFAULT) {
  345. pr_info("Unable to find user space address %lx in %s\n",
  346. page_to_pfn(p), tsk->comm);
  347. } else if (tk->size_shift == 0) {
  348. kfree(tk);
  349. return;
  350. }
  351. get_task_struct(tsk);
  352. tk->tsk = tsk;
  353. list_add_tail(&tk->nd, to_kill);
  354. }
  355. /*
  356. * Kill the processes that have been collected earlier.
  357. *
  358. * Only do anything when FORCEKILL is set, otherwise just free the
  359. * list (this is used for clean pages which do not need killing)
  360. * Also when FAIL is set do a force kill because something went
  361. * wrong earlier.
  362. */
  363. static void kill_procs(struct list_head *to_kill, int forcekill, bool fail,
  364. unsigned long pfn, int flags)
  365. {
  366. struct to_kill *tk, *next;
  367. list_for_each_entry_safe(tk, next, to_kill, nd) {
  368. if (forcekill) {
  369. /*
  370. * In case something went wrong with munmapping
  371. * make sure the process doesn't catch the
  372. * signal and then access the memory. Just kill it.
  373. */
  374. if (fail || tk->addr == -EFAULT) {
  375. pr_err("%#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
  376. pfn, tk->tsk->comm, tk->tsk->pid);
  377. do_send_sig_info(SIGKILL, SEND_SIG_PRIV,
  378. tk->tsk, PIDTYPE_PID);
  379. }
  380. /*
  381. * In theory the process could have mapped
  382. * something else on the address in-between. We could
  383. * check for that, but we need to tell the
  384. * process anyways.
  385. */
  386. else if (kill_proc(tk, pfn, flags) < 0)
  387. pr_err("%#lx: Cannot send advisory machine check signal to %s:%d\n",
  388. pfn, tk->tsk->comm, tk->tsk->pid);
  389. }
  390. list_del(&tk->nd);
  391. put_task_struct(tk->tsk);
  392. kfree(tk);
  393. }
  394. }
  395. /*
  396. * Find a dedicated thread which is supposed to handle SIGBUS(BUS_MCEERR_AO)
  397. * on behalf of the thread group. Return task_struct of the (first found)
  398. * dedicated thread if found, and return NULL otherwise.
  399. *
  400. * We already hold read_lock(&tasklist_lock) in the caller, so we don't
  401. * have to call rcu_read_lock/unlock() in this function.
  402. */
  403. static struct task_struct *find_early_kill_thread(struct task_struct *tsk)
  404. {
  405. struct task_struct *t;
  406. for_each_thread(tsk, t) {
  407. if (t->flags & PF_MCE_PROCESS) {
  408. if (t->flags & PF_MCE_EARLY)
  409. return t;
  410. } else {
  411. if (sysctl_memory_failure_early_kill)
  412. return t;
  413. }
  414. }
  415. return NULL;
  416. }
  417. /*
  418. * Determine whether a given process is "early kill" process which expects
  419. * to be signaled when some page under the process is hwpoisoned.
  420. * Return task_struct of the dedicated thread (main thread unless explicitly
  421. * specified) if the process is "early kill" and otherwise returns NULL.
  422. *
  423. * Note that the above is true for Action Optional case. For Action Required
  424. * case, it's only meaningful to the current thread which need to be signaled
  425. * with SIGBUS, this error is Action Optional for other non current
  426. * processes sharing the same error page,if the process is "early kill", the
  427. * task_struct of the dedicated thread will also be returned.
  428. */
  429. static struct task_struct *task_early_kill(struct task_struct *tsk,
  430. int force_early)
  431. {
  432. if (!tsk->mm)
  433. return NULL;
  434. /*
  435. * Comparing ->mm here because current task might represent
  436. * a subthread, while tsk always points to the main thread.
  437. */
  438. if (force_early && tsk->mm == current->mm)
  439. return current;
  440. return find_early_kill_thread(tsk);
  441. }
  442. /*
  443. * Collect processes when the error hit an anonymous page.
  444. */
  445. static void collect_procs_anon(struct page *page, struct list_head *to_kill,
  446. int force_early)
  447. {
  448. struct folio *folio = page_folio(page);
  449. struct vm_area_struct *vma;
  450. struct task_struct *tsk;
  451. struct anon_vma *av;
  452. pgoff_t pgoff;
  453. av = folio_lock_anon_vma_read(folio, NULL);
  454. if (av == NULL) /* Not actually mapped anymore */
  455. return;
  456. pgoff = page_to_pgoff(page);
  457. read_lock(&tasklist_lock);
  458. for_each_process (tsk) {
  459. struct anon_vma_chain *vmac;
  460. struct task_struct *t = task_early_kill(tsk, force_early);
  461. if (!t)
  462. continue;
  463. anon_vma_interval_tree_foreach(vmac, &av->rb_root,
  464. pgoff, pgoff) {
  465. vma = vmac->vma;
  466. if (vma->vm_mm != t->mm)
  467. continue;
  468. if (!page_mapped_in_vma(page, vma))
  469. continue;
  470. add_to_kill(t, page, FSDAX_INVALID_PGOFF, vma, to_kill);
  471. }
  472. }
  473. read_unlock(&tasklist_lock);
  474. anon_vma_unlock_read(av);
  475. }
  476. /*
  477. * Collect processes when the error hit a file mapped page.
  478. */
  479. static void collect_procs_file(struct page *page, struct list_head *to_kill,
  480. int force_early)
  481. {
  482. struct vm_area_struct *vma;
  483. struct task_struct *tsk;
  484. struct address_space *mapping = page->mapping;
  485. pgoff_t pgoff;
  486. i_mmap_lock_read(mapping);
  487. read_lock(&tasklist_lock);
  488. pgoff = page_to_pgoff(page);
  489. for_each_process(tsk) {
  490. struct task_struct *t = task_early_kill(tsk, force_early);
  491. if (!t)
  492. continue;
  493. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff,
  494. pgoff) {
  495. /*
  496. * Send early kill signal to tasks where a vma covers
  497. * the page but the corrupted page is not necessarily
  498. * mapped it in its pte.
  499. * Assume applications who requested early kill want
  500. * to be informed of all such data corruptions.
  501. */
  502. if (vma->vm_mm == t->mm)
  503. add_to_kill(t, page, FSDAX_INVALID_PGOFF, vma,
  504. to_kill);
  505. }
  506. }
  507. read_unlock(&tasklist_lock);
  508. i_mmap_unlock_read(mapping);
  509. }
  510. #ifdef CONFIG_FS_DAX
  511. /*
  512. * Collect processes when the error hit a fsdax page.
  513. */
  514. static void collect_procs_fsdax(struct page *page,
  515. struct address_space *mapping, pgoff_t pgoff,
  516. struct list_head *to_kill)
  517. {
  518. struct vm_area_struct *vma;
  519. struct task_struct *tsk;
  520. i_mmap_lock_read(mapping);
  521. read_lock(&tasklist_lock);
  522. for_each_process(tsk) {
  523. struct task_struct *t = task_early_kill(tsk, true);
  524. if (!t)
  525. continue;
  526. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  527. if (vma->vm_mm == t->mm)
  528. add_to_kill(t, page, pgoff, vma, to_kill);
  529. }
  530. }
  531. read_unlock(&tasklist_lock);
  532. i_mmap_unlock_read(mapping);
  533. }
  534. #endif /* CONFIG_FS_DAX */
  535. /*
  536. * Collect the processes who have the corrupted page mapped to kill.
  537. */
  538. static void collect_procs(struct page *page, struct list_head *tokill,
  539. int force_early)
  540. {
  541. if (!page->mapping)
  542. return;
  543. if (PageAnon(page))
  544. collect_procs_anon(page, tokill, force_early);
  545. else
  546. collect_procs_file(page, tokill, force_early);
  547. }
  548. struct hwp_walk {
  549. struct to_kill tk;
  550. unsigned long pfn;
  551. int flags;
  552. };
  553. static void set_to_kill(struct to_kill *tk, unsigned long addr, short shift)
  554. {
  555. tk->addr = addr;
  556. tk->size_shift = shift;
  557. }
  558. static int check_hwpoisoned_entry(pte_t pte, unsigned long addr, short shift,
  559. unsigned long poisoned_pfn, struct to_kill *tk)
  560. {
  561. unsigned long pfn = 0;
  562. if (pte_present(pte)) {
  563. pfn = pte_pfn(pte);
  564. } else {
  565. swp_entry_t swp = pte_to_swp_entry(pte);
  566. if (is_hwpoison_entry(swp))
  567. pfn = swp_offset_pfn(swp);
  568. }
  569. if (!pfn || pfn != poisoned_pfn)
  570. return 0;
  571. set_to_kill(tk, addr, shift);
  572. return 1;
  573. }
  574. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  575. static int check_hwpoisoned_pmd_entry(pmd_t *pmdp, unsigned long addr,
  576. struct hwp_walk *hwp)
  577. {
  578. pmd_t pmd = *pmdp;
  579. unsigned long pfn;
  580. unsigned long hwpoison_vaddr;
  581. if (!pmd_present(pmd))
  582. return 0;
  583. pfn = pmd_pfn(pmd);
  584. if (pfn <= hwp->pfn && hwp->pfn < pfn + HPAGE_PMD_NR) {
  585. hwpoison_vaddr = addr + ((hwp->pfn - pfn) << PAGE_SHIFT);
  586. set_to_kill(&hwp->tk, hwpoison_vaddr, PAGE_SHIFT);
  587. return 1;
  588. }
  589. return 0;
  590. }
  591. #else
  592. static int check_hwpoisoned_pmd_entry(pmd_t *pmdp, unsigned long addr,
  593. struct hwp_walk *hwp)
  594. {
  595. return 0;
  596. }
  597. #endif
  598. static int hwpoison_pte_range(pmd_t *pmdp, unsigned long addr,
  599. unsigned long end, struct mm_walk *walk)
  600. {
  601. struct hwp_walk *hwp = walk->private;
  602. int ret = 0;
  603. pte_t *ptep, *mapped_pte;
  604. spinlock_t *ptl;
  605. ptl = pmd_trans_huge_lock(pmdp, walk->vma);
  606. if (ptl) {
  607. ret = check_hwpoisoned_pmd_entry(pmdp, addr, hwp);
  608. spin_unlock(ptl);
  609. goto out;
  610. }
  611. if (pmd_trans_unstable(pmdp))
  612. goto out;
  613. mapped_pte = ptep = pte_offset_map_lock(walk->vma->vm_mm, pmdp,
  614. addr, &ptl);
  615. for (; addr != end; ptep++, addr += PAGE_SIZE) {
  616. ret = check_hwpoisoned_entry(*ptep, addr, PAGE_SHIFT,
  617. hwp->pfn, &hwp->tk);
  618. if (ret == 1)
  619. break;
  620. }
  621. pte_unmap_unlock(mapped_pte, ptl);
  622. out:
  623. cond_resched();
  624. return ret;
  625. }
  626. #ifdef CONFIG_HUGETLB_PAGE
  627. static int hwpoison_hugetlb_range(pte_t *ptep, unsigned long hmask,
  628. unsigned long addr, unsigned long end,
  629. struct mm_walk *walk)
  630. {
  631. struct hwp_walk *hwp = walk->private;
  632. pte_t pte = huge_ptep_get(ptep);
  633. struct hstate *h = hstate_vma(walk->vma);
  634. return check_hwpoisoned_entry(pte, addr, huge_page_shift(h),
  635. hwp->pfn, &hwp->tk);
  636. }
  637. #else
  638. #define hwpoison_hugetlb_range NULL
  639. #endif
  640. static const struct mm_walk_ops hwp_walk_ops = {
  641. .pmd_entry = hwpoison_pte_range,
  642. .hugetlb_entry = hwpoison_hugetlb_range,
  643. .walk_lock = PGWALK_RDLOCK,
  644. };
  645. /*
  646. * Sends SIGBUS to the current process with error info.
  647. *
  648. * This function is intended to handle "Action Required" MCEs on already
  649. * hardware poisoned pages. They could happen, for example, when
  650. * memory_failure() failed to unmap the error page at the first call, or
  651. * when multiple local machine checks happened on different CPUs.
  652. *
  653. * MCE handler currently has no easy access to the error virtual address,
  654. * so this function walks page table to find it. The returned virtual address
  655. * is proper in most cases, but it could be wrong when the application
  656. * process has multiple entries mapping the error page.
  657. */
  658. static int kill_accessing_process(struct task_struct *p, unsigned long pfn,
  659. int flags)
  660. {
  661. int ret;
  662. struct hwp_walk priv = {
  663. .pfn = pfn,
  664. };
  665. priv.tk.tsk = p;
  666. if (!p->mm)
  667. return -EFAULT;
  668. mmap_read_lock(p->mm);
  669. ret = walk_page_range(p->mm, 0, TASK_SIZE, &hwp_walk_ops,
  670. (void *)&priv);
  671. if (ret == 1 && priv.tk.addr)
  672. kill_proc(&priv.tk, pfn, flags);
  673. else
  674. ret = 0;
  675. mmap_read_unlock(p->mm);
  676. return ret > 0 ? -EHWPOISON : -EFAULT;
  677. }
  678. static const char *action_name[] = {
  679. [MF_IGNORED] = "Ignored",
  680. [MF_FAILED] = "Failed",
  681. [MF_DELAYED] = "Delayed",
  682. [MF_RECOVERED] = "Recovered",
  683. };
  684. static const char * const action_page_types[] = {
  685. [MF_MSG_KERNEL] = "reserved kernel page",
  686. [MF_MSG_KERNEL_HIGH_ORDER] = "high-order kernel page",
  687. [MF_MSG_SLAB] = "kernel slab page",
  688. [MF_MSG_DIFFERENT_COMPOUND] = "different compound page after locking",
  689. [MF_MSG_HUGE] = "huge page",
  690. [MF_MSG_FREE_HUGE] = "free huge page",
  691. [MF_MSG_UNMAP_FAILED] = "unmapping failed page",
  692. [MF_MSG_DIRTY_SWAPCACHE] = "dirty swapcache page",
  693. [MF_MSG_CLEAN_SWAPCACHE] = "clean swapcache page",
  694. [MF_MSG_DIRTY_MLOCKED_LRU] = "dirty mlocked LRU page",
  695. [MF_MSG_CLEAN_MLOCKED_LRU] = "clean mlocked LRU page",
  696. [MF_MSG_DIRTY_UNEVICTABLE_LRU] = "dirty unevictable LRU page",
  697. [MF_MSG_CLEAN_UNEVICTABLE_LRU] = "clean unevictable LRU page",
  698. [MF_MSG_DIRTY_LRU] = "dirty LRU page",
  699. [MF_MSG_CLEAN_LRU] = "clean LRU page",
  700. [MF_MSG_TRUNCATED_LRU] = "already truncated LRU page",
  701. [MF_MSG_BUDDY] = "free buddy page",
  702. [MF_MSG_DAX] = "dax page",
  703. [MF_MSG_UNSPLIT_THP] = "unsplit thp",
  704. [MF_MSG_UNKNOWN] = "unknown page",
  705. };
  706. /*
  707. * XXX: It is possible that a page is isolated from LRU cache,
  708. * and then kept in swap cache or failed to remove from page cache.
  709. * The page count will stop it from being freed by unpoison.
  710. * Stress tests should be aware of this memory leak problem.
  711. */
  712. static int delete_from_lru_cache(struct page *p)
  713. {
  714. if (!isolate_lru_page(p)) {
  715. /*
  716. * Clear sensible page flags, so that the buddy system won't
  717. * complain when the page is unpoison-and-freed.
  718. */
  719. ClearPageActive(p);
  720. ClearPageUnevictable(p);
  721. /*
  722. * Poisoned page might never drop its ref count to 0 so we have
  723. * to uncharge it manually from its memcg.
  724. */
  725. mem_cgroup_uncharge(page_folio(p));
  726. /*
  727. * drop the page count elevated by isolate_lru_page()
  728. */
  729. put_page(p);
  730. return 0;
  731. }
  732. return -EIO;
  733. }
  734. static int truncate_error_page(struct page *p, unsigned long pfn,
  735. struct address_space *mapping)
  736. {
  737. int ret = MF_FAILED;
  738. if (mapping->a_ops->error_remove_page) {
  739. int err = mapping->a_ops->error_remove_page(mapping, p);
  740. if (err != 0) {
  741. pr_info("%#lx: Failed to punch page: %d\n", pfn, err);
  742. } else if (page_has_private(p) &&
  743. !try_to_release_page(p, GFP_NOIO)) {
  744. pr_info("%#lx: failed to release buffers\n", pfn);
  745. } else {
  746. ret = MF_RECOVERED;
  747. }
  748. } else {
  749. /*
  750. * If the file system doesn't support it just invalidate
  751. * This fails on dirty or anything with private pages
  752. */
  753. if (invalidate_inode_page(p))
  754. ret = MF_RECOVERED;
  755. else
  756. pr_info("%#lx: Failed to invalidate\n", pfn);
  757. }
  758. return ret;
  759. }
  760. struct page_state {
  761. unsigned long mask;
  762. unsigned long res;
  763. enum mf_action_page_type type;
  764. /* Callback ->action() has to unlock the relevant page inside it. */
  765. int (*action)(struct page_state *ps, struct page *p);
  766. };
  767. /*
  768. * Return true if page is still referenced by others, otherwise return
  769. * false.
  770. *
  771. * The extra_pins is true when one extra refcount is expected.
  772. */
  773. static bool has_extra_refcount(struct page_state *ps, struct page *p,
  774. bool extra_pins)
  775. {
  776. int count = page_count(p) - 1;
  777. if (extra_pins)
  778. count -= 1;
  779. if (count > 0) {
  780. pr_err("%#lx: %s still referenced by %d users\n",
  781. page_to_pfn(p), action_page_types[ps->type], count);
  782. return true;
  783. }
  784. return false;
  785. }
  786. /*
  787. * Error hit kernel page.
  788. * Do nothing, try to be lucky and not touch this instead. For a few cases we
  789. * could be more sophisticated.
  790. */
  791. static int me_kernel(struct page_state *ps, struct page *p)
  792. {
  793. unlock_page(p);
  794. return MF_IGNORED;
  795. }
  796. /*
  797. * Page in unknown state. Do nothing.
  798. */
  799. static int me_unknown(struct page_state *ps, struct page *p)
  800. {
  801. pr_err("%#lx: Unknown page state\n", page_to_pfn(p));
  802. unlock_page(p);
  803. return MF_FAILED;
  804. }
  805. /*
  806. * Clean (or cleaned) page cache page.
  807. */
  808. static int me_pagecache_clean(struct page_state *ps, struct page *p)
  809. {
  810. int ret;
  811. struct address_space *mapping;
  812. bool extra_pins;
  813. delete_from_lru_cache(p);
  814. /*
  815. * For anonymous pages we're done the only reference left
  816. * should be the one m_f() holds.
  817. */
  818. if (PageAnon(p)) {
  819. ret = MF_RECOVERED;
  820. goto out;
  821. }
  822. /*
  823. * Now truncate the page in the page cache. This is really
  824. * more like a "temporary hole punch"
  825. * Don't do this for block devices when someone else
  826. * has a reference, because it could be file system metadata
  827. * and that's not safe to truncate.
  828. */
  829. mapping = page_mapping(p);
  830. if (!mapping) {
  831. /*
  832. * Page has been teared down in the meanwhile
  833. */
  834. ret = MF_FAILED;
  835. goto out;
  836. }
  837. /*
  838. * The shmem page is kept in page cache instead of truncating
  839. * so is expected to have an extra refcount after error-handling.
  840. */
  841. extra_pins = shmem_mapping(mapping);
  842. /*
  843. * Truncation is a bit tricky. Enable it per file system for now.
  844. *
  845. * Open: to take i_rwsem or not for this? Right now we don't.
  846. */
  847. ret = truncate_error_page(p, page_to_pfn(p), mapping);
  848. if (has_extra_refcount(ps, p, extra_pins))
  849. ret = MF_FAILED;
  850. out:
  851. unlock_page(p);
  852. return ret;
  853. }
  854. /*
  855. * Dirty pagecache page
  856. * Issues: when the error hit a hole page the error is not properly
  857. * propagated.
  858. */
  859. static int me_pagecache_dirty(struct page_state *ps, struct page *p)
  860. {
  861. struct address_space *mapping = page_mapping(p);
  862. SetPageError(p);
  863. /* TBD: print more information about the file. */
  864. if (mapping) {
  865. /*
  866. * IO error will be reported by write(), fsync(), etc.
  867. * who check the mapping.
  868. * This way the application knows that something went
  869. * wrong with its dirty file data.
  870. *
  871. * There's one open issue:
  872. *
  873. * The EIO will be only reported on the next IO
  874. * operation and then cleared through the IO map.
  875. * Normally Linux has two mechanisms to pass IO error
  876. * first through the AS_EIO flag in the address space
  877. * and then through the PageError flag in the page.
  878. * Since we drop pages on memory failure handling the
  879. * only mechanism open to use is through AS_AIO.
  880. *
  881. * This has the disadvantage that it gets cleared on
  882. * the first operation that returns an error, while
  883. * the PageError bit is more sticky and only cleared
  884. * when the page is reread or dropped. If an
  885. * application assumes it will always get error on
  886. * fsync, but does other operations on the fd before
  887. * and the page is dropped between then the error
  888. * will not be properly reported.
  889. *
  890. * This can already happen even without hwpoisoned
  891. * pages: first on metadata IO errors (which only
  892. * report through AS_EIO) or when the page is dropped
  893. * at the wrong time.
  894. *
  895. * So right now we assume that the application DTRT on
  896. * the first EIO, but we're not worse than other parts
  897. * of the kernel.
  898. */
  899. mapping_set_error(mapping, -EIO);
  900. }
  901. return me_pagecache_clean(ps, p);
  902. }
  903. /*
  904. * Clean and dirty swap cache.
  905. *
  906. * Dirty swap cache page is tricky to handle. The page could live both in page
  907. * cache and swap cache(ie. page is freshly swapped in). So it could be
  908. * referenced concurrently by 2 types of PTEs:
  909. * normal PTEs and swap PTEs. We try to handle them consistently by calling
  910. * try_to_unmap(!TTU_HWPOISON) to convert the normal PTEs to swap PTEs,
  911. * and then
  912. * - clear dirty bit to prevent IO
  913. * - remove from LRU
  914. * - but keep in the swap cache, so that when we return to it on
  915. * a later page fault, we know the application is accessing
  916. * corrupted data and shall be killed (we installed simple
  917. * interception code in do_swap_page to catch it).
  918. *
  919. * Clean swap cache pages can be directly isolated. A later page fault will
  920. * bring in the known good data from disk.
  921. */
  922. static int me_swapcache_dirty(struct page_state *ps, struct page *p)
  923. {
  924. int ret;
  925. bool extra_pins = false;
  926. ClearPageDirty(p);
  927. /* Trigger EIO in shmem: */
  928. ClearPageUptodate(p);
  929. ret = delete_from_lru_cache(p) ? MF_FAILED : MF_DELAYED;
  930. unlock_page(p);
  931. if (ret == MF_DELAYED)
  932. extra_pins = true;
  933. if (has_extra_refcount(ps, p, extra_pins))
  934. ret = MF_FAILED;
  935. return ret;
  936. }
  937. static int me_swapcache_clean(struct page_state *ps, struct page *p)
  938. {
  939. struct folio *folio = page_folio(p);
  940. int ret;
  941. delete_from_swap_cache(folio);
  942. ret = delete_from_lru_cache(p) ? MF_FAILED : MF_RECOVERED;
  943. folio_unlock(folio);
  944. if (has_extra_refcount(ps, p, false))
  945. ret = MF_FAILED;
  946. return ret;
  947. }
  948. /*
  949. * Huge pages. Needs work.
  950. * Issues:
  951. * - Error on hugepage is contained in hugepage unit (not in raw page unit.)
  952. * To narrow down kill region to one page, we need to break up pmd.
  953. */
  954. static int me_huge_page(struct page_state *ps, struct page *p)
  955. {
  956. int res;
  957. struct page *hpage = compound_head(p);
  958. struct address_space *mapping;
  959. bool extra_pins = false;
  960. if (!PageHuge(hpage))
  961. return MF_DELAYED;
  962. mapping = page_mapping(hpage);
  963. if (mapping) {
  964. res = truncate_error_page(hpage, page_to_pfn(p), mapping);
  965. /* The page is kept in page cache. */
  966. extra_pins = true;
  967. unlock_page(hpage);
  968. } else {
  969. unlock_page(hpage);
  970. /*
  971. * migration entry prevents later access on error hugepage,
  972. * so we can free and dissolve it into buddy to save healthy
  973. * subpages.
  974. */
  975. put_page(hpage);
  976. if (__page_handle_poison(p) >= 0) {
  977. page_ref_inc(p);
  978. res = MF_RECOVERED;
  979. } else {
  980. res = MF_FAILED;
  981. }
  982. }
  983. if (has_extra_refcount(ps, p, extra_pins))
  984. res = MF_FAILED;
  985. return res;
  986. }
  987. /*
  988. * Various page states we can handle.
  989. *
  990. * A page state is defined by its current page->flags bits.
  991. * The table matches them in order and calls the right handler.
  992. *
  993. * This is quite tricky because we can access page at any time
  994. * in its live cycle, so all accesses have to be extremely careful.
  995. *
  996. * This is not complete. More states could be added.
  997. * For any missing state don't attempt recovery.
  998. */
  999. #define dirty (1UL << PG_dirty)
  1000. #define sc ((1UL << PG_swapcache) | (1UL << PG_swapbacked))
  1001. #define unevict (1UL << PG_unevictable)
  1002. #define mlock (1UL << PG_mlocked)
  1003. #define lru (1UL << PG_lru)
  1004. #define head (1UL << PG_head)
  1005. #define slab (1UL << PG_slab)
  1006. #define reserved (1UL << PG_reserved)
  1007. static struct page_state error_states[] = {
  1008. { reserved, reserved, MF_MSG_KERNEL, me_kernel },
  1009. /*
  1010. * free pages are specially detected outside this table:
  1011. * PG_buddy pages only make a small fraction of all free pages.
  1012. */
  1013. /*
  1014. * Could in theory check if slab page is free or if we can drop
  1015. * currently unused objects without touching them. But just
  1016. * treat it as standard kernel for now.
  1017. */
  1018. { slab, slab, MF_MSG_SLAB, me_kernel },
  1019. { head, head, MF_MSG_HUGE, me_huge_page },
  1020. { sc|dirty, sc|dirty, MF_MSG_DIRTY_SWAPCACHE, me_swapcache_dirty },
  1021. { sc|dirty, sc, MF_MSG_CLEAN_SWAPCACHE, me_swapcache_clean },
  1022. { mlock|dirty, mlock|dirty, MF_MSG_DIRTY_MLOCKED_LRU, me_pagecache_dirty },
  1023. { mlock|dirty, mlock, MF_MSG_CLEAN_MLOCKED_LRU, me_pagecache_clean },
  1024. { unevict|dirty, unevict|dirty, MF_MSG_DIRTY_UNEVICTABLE_LRU, me_pagecache_dirty },
  1025. { unevict|dirty, unevict, MF_MSG_CLEAN_UNEVICTABLE_LRU, me_pagecache_clean },
  1026. { lru|dirty, lru|dirty, MF_MSG_DIRTY_LRU, me_pagecache_dirty },
  1027. { lru|dirty, lru, MF_MSG_CLEAN_LRU, me_pagecache_clean },
  1028. /*
  1029. * Catchall entry: must be at end.
  1030. */
  1031. { 0, 0, MF_MSG_UNKNOWN, me_unknown },
  1032. };
  1033. #undef dirty
  1034. #undef sc
  1035. #undef unevict
  1036. #undef mlock
  1037. #undef lru
  1038. #undef head
  1039. #undef slab
  1040. #undef reserved
  1041. /*
  1042. * "Dirty/Clean" indication is not 100% accurate due to the possibility of
  1043. * setting PG_dirty outside page lock. See also comment above set_page_dirty().
  1044. */
  1045. static void action_result(unsigned long pfn, enum mf_action_page_type type,
  1046. enum mf_result result)
  1047. {
  1048. trace_memory_failure_event(pfn, type, result);
  1049. num_poisoned_pages_inc();
  1050. pr_err("%#lx: recovery action for %s: %s\n",
  1051. pfn, action_page_types[type], action_name[result]);
  1052. }
  1053. static int page_action(struct page_state *ps, struct page *p,
  1054. unsigned long pfn)
  1055. {
  1056. int result;
  1057. /* page p should be unlocked after returning from ps->action(). */
  1058. result = ps->action(ps, p);
  1059. action_result(pfn, ps->type, result);
  1060. /* Could do more checks here if page looks ok */
  1061. /*
  1062. * Could adjust zone counters here to correct for the missing page.
  1063. */
  1064. return (result == MF_RECOVERED || result == MF_DELAYED) ? 0 : -EBUSY;
  1065. }
  1066. static inline bool PageHWPoisonTakenOff(struct page *page)
  1067. {
  1068. return PageHWPoison(page) && page_private(page) == MAGIC_HWPOISON;
  1069. }
  1070. void SetPageHWPoisonTakenOff(struct page *page)
  1071. {
  1072. set_page_private(page, MAGIC_HWPOISON);
  1073. }
  1074. void ClearPageHWPoisonTakenOff(struct page *page)
  1075. {
  1076. if (PageHWPoison(page))
  1077. set_page_private(page, 0);
  1078. }
  1079. /*
  1080. * Return true if a page type of a given page is supported by hwpoison
  1081. * mechanism (while handling could fail), otherwise false. This function
  1082. * does not return true for hugetlb or device memory pages, so it's assumed
  1083. * to be called only in the context where we never have such pages.
  1084. */
  1085. static inline bool HWPoisonHandlable(struct page *page, unsigned long flags)
  1086. {
  1087. /* Soft offline could migrate non-LRU movable pages */
  1088. if ((flags & MF_SOFT_OFFLINE) && __PageMovable(page))
  1089. return true;
  1090. return PageLRU(page) || is_free_buddy_page(page);
  1091. }
  1092. static int __get_hwpoison_page(struct page *page, unsigned long flags)
  1093. {
  1094. struct page *head = compound_head(page);
  1095. int ret = 0;
  1096. bool hugetlb = false;
  1097. ret = get_hwpoison_huge_page(head, &hugetlb);
  1098. if (hugetlb)
  1099. return ret;
  1100. /*
  1101. * This check prevents from calling get_page_unless_zero() for any
  1102. * unsupported type of page in order to reduce the risk of unexpected
  1103. * races caused by taking a page refcount.
  1104. */
  1105. if (!HWPoisonHandlable(head, flags))
  1106. return -EBUSY;
  1107. if (get_page_unless_zero(head)) {
  1108. if (head == compound_head(page))
  1109. return 1;
  1110. pr_info("%#lx cannot catch tail\n", page_to_pfn(page));
  1111. put_page(head);
  1112. }
  1113. return 0;
  1114. }
  1115. static int get_any_page(struct page *p, unsigned long flags)
  1116. {
  1117. int ret = 0, pass = 0;
  1118. bool count_increased = false;
  1119. if (flags & MF_COUNT_INCREASED)
  1120. count_increased = true;
  1121. try_again:
  1122. if (!count_increased) {
  1123. ret = __get_hwpoison_page(p, flags);
  1124. if (!ret) {
  1125. if (page_count(p)) {
  1126. /* We raced with an allocation, retry. */
  1127. if (pass++ < 3)
  1128. goto try_again;
  1129. ret = -EBUSY;
  1130. } else if (!PageHuge(p) && !is_free_buddy_page(p)) {
  1131. /* We raced with put_page, retry. */
  1132. if (pass++ < 3)
  1133. goto try_again;
  1134. ret = -EIO;
  1135. }
  1136. goto out;
  1137. } else if (ret == -EBUSY) {
  1138. /*
  1139. * We raced with (possibly temporary) unhandlable
  1140. * page, retry.
  1141. */
  1142. if (pass++ < 3) {
  1143. shake_page(p);
  1144. goto try_again;
  1145. }
  1146. ret = -EIO;
  1147. goto out;
  1148. }
  1149. }
  1150. if (PageHuge(p) || HWPoisonHandlable(p, flags)) {
  1151. ret = 1;
  1152. } else {
  1153. /*
  1154. * A page we cannot handle. Check whether we can turn
  1155. * it into something we can handle.
  1156. */
  1157. if (pass++ < 3) {
  1158. put_page(p);
  1159. shake_page(p);
  1160. count_increased = false;
  1161. goto try_again;
  1162. }
  1163. put_page(p);
  1164. ret = -EIO;
  1165. }
  1166. out:
  1167. if (ret == -EIO)
  1168. pr_err("%#lx: unhandlable page.\n", page_to_pfn(p));
  1169. return ret;
  1170. }
  1171. static int __get_unpoison_page(struct page *page)
  1172. {
  1173. struct page *head = compound_head(page);
  1174. int ret = 0;
  1175. bool hugetlb = false;
  1176. ret = get_hwpoison_huge_page(head, &hugetlb);
  1177. if (hugetlb)
  1178. return ret;
  1179. /*
  1180. * PageHWPoisonTakenOff pages are not only marked as PG_hwpoison,
  1181. * but also isolated from buddy freelist, so need to identify the
  1182. * state and have to cancel both operations to unpoison.
  1183. */
  1184. if (PageHWPoisonTakenOff(page))
  1185. return -EHWPOISON;
  1186. return get_page_unless_zero(page) ? 1 : 0;
  1187. }
  1188. /**
  1189. * get_hwpoison_page() - Get refcount for memory error handling
  1190. * @p: Raw error page (hit by memory error)
  1191. * @flags: Flags controlling behavior of error handling
  1192. *
  1193. * get_hwpoison_page() takes a page refcount of an error page to handle memory
  1194. * error on it, after checking that the error page is in a well-defined state
  1195. * (defined as a page-type we can successfully handle the memory error on it,
  1196. * such as LRU page and hugetlb page).
  1197. *
  1198. * Memory error handling could be triggered at any time on any type of page,
  1199. * so it's prone to race with typical memory management lifecycle (like
  1200. * allocation and free). So to avoid such races, get_hwpoison_page() takes
  1201. * extra care for the error page's state (as done in __get_hwpoison_page()),
  1202. * and has some retry logic in get_any_page().
  1203. *
  1204. * When called from unpoison_memory(), the caller should already ensure that
  1205. * the given page has PG_hwpoison. So it's never reused for other page
  1206. * allocations, and __get_unpoison_page() never races with them.
  1207. *
  1208. * Return: 0 on failure,
  1209. * 1 on success for in-use pages in a well-defined state,
  1210. * -EIO for pages on which we can not handle memory errors,
  1211. * -EBUSY when get_hwpoison_page() has raced with page lifecycle
  1212. * operations like allocation and free,
  1213. * -EHWPOISON when the page is hwpoisoned and taken off from buddy.
  1214. */
  1215. static int get_hwpoison_page(struct page *p, unsigned long flags)
  1216. {
  1217. int ret;
  1218. zone_pcp_disable(page_zone(p));
  1219. if (flags & MF_UNPOISON)
  1220. ret = __get_unpoison_page(p);
  1221. else
  1222. ret = get_any_page(p, flags);
  1223. zone_pcp_enable(page_zone(p));
  1224. return ret;
  1225. }
  1226. /*
  1227. * Do all that is necessary to remove user space mappings. Unmap
  1228. * the pages and send SIGBUS to the processes if the data was dirty.
  1229. */
  1230. static bool hwpoison_user_mappings(struct page *p, unsigned long pfn,
  1231. int flags, struct page *hpage)
  1232. {
  1233. struct folio *folio = page_folio(hpage);
  1234. enum ttu_flags ttu = TTU_IGNORE_MLOCK | TTU_SYNC | TTU_HWPOISON;
  1235. struct address_space *mapping;
  1236. LIST_HEAD(tokill);
  1237. bool unmap_success;
  1238. int forcekill;
  1239. bool mlocked = PageMlocked(hpage);
  1240. /*
  1241. * Here we are interested only in user-mapped pages, so skip any
  1242. * other types of pages.
  1243. */
  1244. if (PageReserved(p) || PageSlab(p) || PageTable(p))
  1245. return true;
  1246. if (!(PageLRU(hpage) || PageHuge(p)))
  1247. return true;
  1248. /*
  1249. * This check implies we don't kill processes if their pages
  1250. * are in the swap cache early. Those are always late kills.
  1251. */
  1252. if (!page_mapped(hpage))
  1253. return true;
  1254. if (PageKsm(p)) {
  1255. pr_err("%#lx: can't handle KSM pages.\n", pfn);
  1256. return false;
  1257. }
  1258. if (PageSwapCache(p)) {
  1259. pr_err("%#lx: keeping poisoned page in swap cache\n", pfn);
  1260. ttu &= ~TTU_HWPOISON;
  1261. }
  1262. /*
  1263. * Propagate the dirty bit from PTEs to struct page first, because we
  1264. * need this to decide if we should kill or just drop the page.
  1265. * XXX: the dirty test could be racy: set_page_dirty() may not always
  1266. * be called inside page lock (it's recommended but not enforced).
  1267. */
  1268. mapping = page_mapping(hpage);
  1269. if (!(flags & MF_MUST_KILL) && !PageDirty(hpage) && mapping &&
  1270. mapping_can_writeback(mapping)) {
  1271. if (page_mkclean(hpage)) {
  1272. SetPageDirty(hpage);
  1273. } else {
  1274. ttu &= ~TTU_HWPOISON;
  1275. pr_info("%#lx: corrupted page was clean: dropped without side effects\n",
  1276. pfn);
  1277. }
  1278. }
  1279. /*
  1280. * First collect all the processes that have the page
  1281. * mapped in dirty form. This has to be done before try_to_unmap,
  1282. * because ttu takes the rmap data structures down.
  1283. */
  1284. collect_procs(hpage, &tokill, flags & MF_ACTION_REQUIRED);
  1285. if (PageHuge(hpage) && !PageAnon(hpage)) {
  1286. /*
  1287. * For hugetlb pages in shared mappings, try_to_unmap
  1288. * could potentially call huge_pmd_unshare. Because of
  1289. * this, take semaphore in write mode here and set
  1290. * TTU_RMAP_LOCKED to indicate we have taken the lock
  1291. * at this higher level.
  1292. */
  1293. mapping = hugetlb_page_mapping_lock_write(hpage);
  1294. if (mapping) {
  1295. try_to_unmap(folio, ttu|TTU_RMAP_LOCKED);
  1296. i_mmap_unlock_write(mapping);
  1297. } else
  1298. pr_info("%#lx: could not lock mapping for mapped huge page\n", pfn);
  1299. } else {
  1300. try_to_unmap(folio, ttu);
  1301. }
  1302. unmap_success = !page_mapped(hpage);
  1303. if (!unmap_success)
  1304. pr_err("%#lx: failed to unmap page (mapcount=%d)\n",
  1305. pfn, page_mapcount(hpage));
  1306. /*
  1307. * try_to_unmap() might put mlocked page in lru cache, so call
  1308. * shake_page() again to ensure that it's flushed.
  1309. */
  1310. if (mlocked)
  1311. shake_page(hpage);
  1312. /*
  1313. * Now that the dirty bit has been propagated to the
  1314. * struct page and all unmaps done we can decide if
  1315. * killing is needed or not. Only kill when the page
  1316. * was dirty or the process is not restartable,
  1317. * otherwise the tokill list is merely
  1318. * freed. When there was a problem unmapping earlier
  1319. * use a more force-full uncatchable kill to prevent
  1320. * any accesses to the poisoned memory.
  1321. */
  1322. forcekill = PageDirty(hpage) || (flags & MF_MUST_KILL) ||
  1323. !unmap_success;
  1324. kill_procs(&tokill, forcekill, !unmap_success, pfn, flags);
  1325. return unmap_success;
  1326. }
  1327. static int identify_page_state(unsigned long pfn, struct page *p,
  1328. unsigned long page_flags)
  1329. {
  1330. struct page_state *ps;
  1331. /*
  1332. * The first check uses the current page flags which may not have any
  1333. * relevant information. The second check with the saved page flags is
  1334. * carried out only if the first check can't determine the page status.
  1335. */
  1336. for (ps = error_states;; ps++)
  1337. if ((p->flags & ps->mask) == ps->res)
  1338. break;
  1339. page_flags |= (p->flags & (1UL << PG_dirty));
  1340. if (!ps->mask)
  1341. for (ps = error_states;; ps++)
  1342. if ((page_flags & ps->mask) == ps->res)
  1343. break;
  1344. return page_action(ps, p, pfn);
  1345. }
  1346. static int try_to_split_thp_page(struct page *page)
  1347. {
  1348. int ret;
  1349. lock_page(page);
  1350. ret = split_huge_page(page);
  1351. unlock_page(page);
  1352. if (unlikely(ret))
  1353. put_page(page);
  1354. return ret;
  1355. }
  1356. static void unmap_and_kill(struct list_head *to_kill, unsigned long pfn,
  1357. struct address_space *mapping, pgoff_t index, int flags)
  1358. {
  1359. struct to_kill *tk;
  1360. unsigned long size = 0;
  1361. list_for_each_entry(tk, to_kill, nd)
  1362. if (tk->size_shift)
  1363. size = max(size, 1UL << tk->size_shift);
  1364. if (size) {
  1365. /*
  1366. * Unmap the largest mapping to avoid breaking up device-dax
  1367. * mappings which are constant size. The actual size of the
  1368. * mapping being torn down is communicated in siginfo, see
  1369. * kill_proc()
  1370. */
  1371. loff_t start = (index << PAGE_SHIFT) & ~(size - 1);
  1372. unmap_mapping_range(mapping, start, size, 0);
  1373. }
  1374. kill_procs(to_kill, flags & MF_MUST_KILL, false, pfn, flags);
  1375. }
  1376. static int mf_generic_kill_procs(unsigned long long pfn, int flags,
  1377. struct dev_pagemap *pgmap)
  1378. {
  1379. struct page *page = pfn_to_page(pfn);
  1380. LIST_HEAD(to_kill);
  1381. dax_entry_t cookie;
  1382. int rc = 0;
  1383. /*
  1384. * Pages instantiated by device-dax (not filesystem-dax)
  1385. * may be compound pages.
  1386. */
  1387. page = compound_head(page);
  1388. /*
  1389. * Prevent the inode from being freed while we are interrogating
  1390. * the address_space, typically this would be handled by
  1391. * lock_page(), but dax pages do not use the page lock. This
  1392. * also prevents changes to the mapping of this pfn until
  1393. * poison signaling is complete.
  1394. */
  1395. cookie = dax_lock_page(page);
  1396. if (!cookie)
  1397. return -EBUSY;
  1398. if (hwpoison_filter(page)) {
  1399. rc = -EOPNOTSUPP;
  1400. goto unlock;
  1401. }
  1402. switch (pgmap->type) {
  1403. case MEMORY_DEVICE_PRIVATE:
  1404. case MEMORY_DEVICE_COHERENT:
  1405. /*
  1406. * TODO: Handle device pages which may need coordination
  1407. * with device-side memory.
  1408. */
  1409. rc = -ENXIO;
  1410. goto unlock;
  1411. default:
  1412. break;
  1413. }
  1414. /*
  1415. * Use this flag as an indication that the dax page has been
  1416. * remapped UC to prevent speculative consumption of poison.
  1417. */
  1418. SetPageHWPoison(page);
  1419. /*
  1420. * Unlike System-RAM there is no possibility to swap in a
  1421. * different physical page at a given virtual address, so all
  1422. * userspace consumption of ZONE_DEVICE memory necessitates
  1423. * SIGBUS (i.e. MF_MUST_KILL)
  1424. */
  1425. flags |= MF_ACTION_REQUIRED | MF_MUST_KILL;
  1426. collect_procs(page, &to_kill, true);
  1427. unmap_and_kill(&to_kill, pfn, page->mapping, page->index, flags);
  1428. unlock:
  1429. dax_unlock_page(page, cookie);
  1430. return rc;
  1431. }
  1432. #ifdef CONFIG_FS_DAX
  1433. /**
  1434. * mf_dax_kill_procs - Collect and kill processes who are using this file range
  1435. * @mapping: address_space of the file in use
  1436. * @index: start pgoff of the range within the file
  1437. * @count: length of the range, in unit of PAGE_SIZE
  1438. * @mf_flags: memory failure flags
  1439. */
  1440. int mf_dax_kill_procs(struct address_space *mapping, pgoff_t index,
  1441. unsigned long count, int mf_flags)
  1442. {
  1443. LIST_HEAD(to_kill);
  1444. dax_entry_t cookie;
  1445. struct page *page;
  1446. size_t end = index + count;
  1447. mf_flags |= MF_ACTION_REQUIRED | MF_MUST_KILL;
  1448. for (; index < end; index++) {
  1449. page = NULL;
  1450. cookie = dax_lock_mapping_entry(mapping, index, &page);
  1451. if (!cookie)
  1452. return -EBUSY;
  1453. if (!page)
  1454. goto unlock;
  1455. SetPageHWPoison(page);
  1456. collect_procs_fsdax(page, mapping, index, &to_kill);
  1457. unmap_and_kill(&to_kill, page_to_pfn(page), mapping,
  1458. index, mf_flags);
  1459. unlock:
  1460. dax_unlock_mapping_entry(mapping, index, cookie);
  1461. }
  1462. return 0;
  1463. }
  1464. EXPORT_SYMBOL_GPL(mf_dax_kill_procs);
  1465. #endif /* CONFIG_FS_DAX */
  1466. #ifdef CONFIG_HUGETLB_PAGE
  1467. /*
  1468. * Struct raw_hwp_page represents information about "raw error page",
  1469. * constructing singly linked list originated from ->private field of
  1470. * SUBPAGE_INDEX_HWPOISON-th tail page.
  1471. */
  1472. struct raw_hwp_page {
  1473. struct llist_node node;
  1474. struct page *page;
  1475. };
  1476. static inline struct llist_head *raw_hwp_list_head(struct page *hpage)
  1477. {
  1478. return (struct llist_head *)&page_private(hpage + SUBPAGE_INDEX_HWPOISON);
  1479. }
  1480. static unsigned long __free_raw_hwp_pages(struct page *hpage, bool move_flag)
  1481. {
  1482. struct llist_head *head;
  1483. struct llist_node *t, *tnode;
  1484. unsigned long count = 0;
  1485. head = raw_hwp_list_head(hpage);
  1486. llist_for_each_safe(tnode, t, head->first) {
  1487. struct raw_hwp_page *p = container_of(tnode, struct raw_hwp_page, node);
  1488. if (move_flag)
  1489. SetPageHWPoison(p->page);
  1490. kfree(p);
  1491. count++;
  1492. }
  1493. llist_del_all(head);
  1494. return count;
  1495. }
  1496. static int hugetlb_set_page_hwpoison(struct page *hpage, struct page *page)
  1497. {
  1498. struct llist_head *head;
  1499. struct raw_hwp_page *raw_hwp;
  1500. struct llist_node *t, *tnode;
  1501. int ret = TestSetPageHWPoison(hpage) ? -EHWPOISON : 0;
  1502. /*
  1503. * Once the hwpoison hugepage has lost reliable raw error info,
  1504. * there is little meaning to keep additional error info precisely,
  1505. * so skip to add additional raw error info.
  1506. */
  1507. if (HPageRawHwpUnreliable(hpage))
  1508. return -EHWPOISON;
  1509. head = raw_hwp_list_head(hpage);
  1510. llist_for_each_safe(tnode, t, head->first) {
  1511. struct raw_hwp_page *p = container_of(tnode, struct raw_hwp_page, node);
  1512. if (p->page == page)
  1513. return -EHWPOISON;
  1514. }
  1515. raw_hwp = kmalloc(sizeof(struct raw_hwp_page), GFP_ATOMIC);
  1516. if (raw_hwp) {
  1517. raw_hwp->page = page;
  1518. llist_add(&raw_hwp->node, head);
  1519. /* the first error event will be counted in action_result(). */
  1520. if (ret)
  1521. num_poisoned_pages_inc();
  1522. } else {
  1523. /*
  1524. * Failed to save raw error info. We no longer trace all
  1525. * hwpoisoned subpages, and we need refuse to free/dissolve
  1526. * this hwpoisoned hugepage.
  1527. */
  1528. SetHPageRawHwpUnreliable(hpage);
  1529. /*
  1530. * Once HPageRawHwpUnreliable is set, raw_hwp_page is not
  1531. * used any more, so free it.
  1532. */
  1533. __free_raw_hwp_pages(hpage, false);
  1534. }
  1535. return ret;
  1536. }
  1537. static unsigned long free_raw_hwp_pages(struct page *hpage, bool move_flag)
  1538. {
  1539. /*
  1540. * HPageVmemmapOptimized hugepages can't be freed because struct
  1541. * pages for tail pages are required but they don't exist.
  1542. */
  1543. if (move_flag && HPageVmemmapOptimized(hpage))
  1544. return 0;
  1545. /*
  1546. * HPageRawHwpUnreliable hugepages shouldn't be unpoisoned by
  1547. * definition.
  1548. */
  1549. if (HPageRawHwpUnreliable(hpage))
  1550. return 0;
  1551. return __free_raw_hwp_pages(hpage, move_flag);
  1552. }
  1553. void hugetlb_clear_page_hwpoison(struct page *hpage)
  1554. {
  1555. if (HPageRawHwpUnreliable(hpage))
  1556. return;
  1557. ClearPageHWPoison(hpage);
  1558. free_raw_hwp_pages(hpage, true);
  1559. }
  1560. /*
  1561. * Called from hugetlb code with hugetlb_lock held.
  1562. *
  1563. * Return values:
  1564. * 0 - free hugepage
  1565. * 1 - in-use hugepage
  1566. * 2 - not a hugepage
  1567. * -EBUSY - the hugepage is busy (try to retry)
  1568. * -EHWPOISON - the hugepage is already hwpoisoned
  1569. */
  1570. int __get_huge_page_for_hwpoison(unsigned long pfn, int flags)
  1571. {
  1572. struct page *page = pfn_to_page(pfn);
  1573. struct page *head = compound_head(page);
  1574. int ret = 2; /* fallback to normal page handling */
  1575. bool count_increased = false;
  1576. if (!PageHeadHuge(head))
  1577. goto out;
  1578. if (flags & MF_COUNT_INCREASED) {
  1579. ret = 1;
  1580. count_increased = true;
  1581. } else if (HPageFreed(head)) {
  1582. ret = 0;
  1583. } else if (HPageMigratable(head)) {
  1584. ret = get_page_unless_zero(head);
  1585. if (ret)
  1586. count_increased = true;
  1587. } else {
  1588. ret = -EBUSY;
  1589. if (!(flags & MF_NO_RETRY))
  1590. goto out;
  1591. }
  1592. if (hugetlb_set_page_hwpoison(head, page)) {
  1593. ret = -EHWPOISON;
  1594. goto out;
  1595. }
  1596. return ret;
  1597. out:
  1598. if (count_increased)
  1599. put_page(head);
  1600. return ret;
  1601. }
  1602. /*
  1603. * Taking refcount of hugetlb pages needs extra care about race conditions
  1604. * with basic operations like hugepage allocation/free/demotion.
  1605. * So some of prechecks for hwpoison (pinning, and testing/setting
  1606. * PageHWPoison) should be done in single hugetlb_lock range.
  1607. */
  1608. static int try_memory_failure_hugetlb(unsigned long pfn, int flags, int *hugetlb)
  1609. {
  1610. int res;
  1611. struct page *p = pfn_to_page(pfn);
  1612. struct page *head;
  1613. unsigned long page_flags;
  1614. *hugetlb = 1;
  1615. retry:
  1616. res = get_huge_page_for_hwpoison(pfn, flags);
  1617. if (res == 2) { /* fallback to normal page handling */
  1618. *hugetlb = 0;
  1619. return 0;
  1620. } else if (res == -EHWPOISON) {
  1621. pr_err("%#lx: already hardware poisoned\n", pfn);
  1622. if (flags & MF_ACTION_REQUIRED) {
  1623. head = compound_head(p);
  1624. res = kill_accessing_process(current, page_to_pfn(head), flags);
  1625. }
  1626. return res;
  1627. } else if (res == -EBUSY) {
  1628. if (!(flags & MF_NO_RETRY)) {
  1629. flags |= MF_NO_RETRY;
  1630. goto retry;
  1631. }
  1632. action_result(pfn, MF_MSG_UNKNOWN, MF_IGNORED);
  1633. return res;
  1634. }
  1635. head = compound_head(p);
  1636. lock_page(head);
  1637. if (hwpoison_filter(p)) {
  1638. hugetlb_clear_page_hwpoison(head);
  1639. unlock_page(head);
  1640. if (res == 1)
  1641. put_page(head);
  1642. return -EOPNOTSUPP;
  1643. }
  1644. /*
  1645. * Handling free hugepage. The possible race with hugepage allocation
  1646. * or demotion can be prevented by PageHWPoison flag.
  1647. */
  1648. if (res == 0) {
  1649. unlock_page(head);
  1650. if (__page_handle_poison(p) >= 0) {
  1651. page_ref_inc(p);
  1652. res = MF_RECOVERED;
  1653. } else {
  1654. res = MF_FAILED;
  1655. }
  1656. action_result(pfn, MF_MSG_FREE_HUGE, res);
  1657. return res == MF_RECOVERED ? 0 : -EBUSY;
  1658. }
  1659. page_flags = head->flags;
  1660. if (!hwpoison_user_mappings(p, pfn, flags, head)) {
  1661. action_result(pfn, MF_MSG_UNMAP_FAILED, MF_IGNORED);
  1662. res = -EBUSY;
  1663. goto out;
  1664. }
  1665. return identify_page_state(pfn, p, page_flags);
  1666. out:
  1667. unlock_page(head);
  1668. return res;
  1669. }
  1670. #else
  1671. static inline int try_memory_failure_hugetlb(unsigned long pfn, int flags, int *hugetlb)
  1672. {
  1673. return 0;
  1674. }
  1675. static inline unsigned long free_raw_hwp_pages(struct page *hpage, bool flag)
  1676. {
  1677. return 0;
  1678. }
  1679. #endif /* CONFIG_HUGETLB_PAGE */
  1680. static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
  1681. struct dev_pagemap *pgmap)
  1682. {
  1683. struct page *page = pfn_to_page(pfn);
  1684. int rc = -ENXIO;
  1685. if (flags & MF_COUNT_INCREASED)
  1686. /*
  1687. * Drop the extra refcount in case we come from madvise().
  1688. */
  1689. put_page(page);
  1690. /* device metadata space is not recoverable */
  1691. if (!pgmap_pfn_valid(pgmap, pfn))
  1692. goto out;
  1693. /*
  1694. * Call driver's implementation to handle the memory failure, otherwise
  1695. * fall back to generic handler.
  1696. */
  1697. if (pgmap_has_memory_failure(pgmap)) {
  1698. rc = pgmap->ops->memory_failure(pgmap, pfn, 1, flags);
  1699. /*
  1700. * Fall back to generic handler too if operation is not
  1701. * supported inside the driver/device/filesystem.
  1702. */
  1703. if (rc != -EOPNOTSUPP)
  1704. goto out;
  1705. }
  1706. rc = mf_generic_kill_procs(pfn, flags, pgmap);
  1707. out:
  1708. /* drop pgmap ref acquired in caller */
  1709. put_dev_pagemap(pgmap);
  1710. action_result(pfn, MF_MSG_DAX, rc ? MF_FAILED : MF_RECOVERED);
  1711. return rc;
  1712. }
  1713. static DEFINE_MUTEX(mf_mutex);
  1714. /**
  1715. * memory_failure - Handle memory failure of a page.
  1716. * @pfn: Page Number of the corrupted page
  1717. * @flags: fine tune action taken
  1718. *
  1719. * This function is called by the low level machine check code
  1720. * of an architecture when it detects hardware memory corruption
  1721. * of a page. It tries its best to recover, which includes
  1722. * dropping pages, killing processes etc.
  1723. *
  1724. * The function is primarily of use for corruptions that
  1725. * happen outside the current execution context (e.g. when
  1726. * detected by a background scrubber)
  1727. *
  1728. * Must run in process context (e.g. a work queue) with interrupts
  1729. * enabled and no spinlocks hold.
  1730. *
  1731. * Return: 0 for successfully handled the memory error,
  1732. * -EOPNOTSUPP for hwpoison_filter() filtered the error event,
  1733. * < 0(except -EOPNOTSUPP) on failure.
  1734. */
  1735. int memory_failure(unsigned long pfn, int flags)
  1736. {
  1737. struct page *p;
  1738. struct page *hpage;
  1739. struct dev_pagemap *pgmap;
  1740. int res = 0;
  1741. unsigned long page_flags;
  1742. bool retry = true;
  1743. int hugetlb = 0;
  1744. if (!sysctl_memory_failure_recovery)
  1745. panic("Memory failure on page %lx", pfn);
  1746. mutex_lock(&mf_mutex);
  1747. if (!(flags & MF_SW_SIMULATED))
  1748. hw_memory_failure = true;
  1749. p = pfn_to_online_page(pfn);
  1750. if (!p) {
  1751. res = arch_memory_failure(pfn, flags);
  1752. if (res == 0)
  1753. goto unlock_mutex;
  1754. if (pfn_valid(pfn)) {
  1755. pgmap = get_dev_pagemap(pfn, NULL);
  1756. if (pgmap) {
  1757. res = memory_failure_dev_pagemap(pfn, flags,
  1758. pgmap);
  1759. goto unlock_mutex;
  1760. }
  1761. }
  1762. pr_err("%#lx: memory outside kernel control\n", pfn);
  1763. res = -ENXIO;
  1764. goto unlock_mutex;
  1765. }
  1766. try_again:
  1767. res = try_memory_failure_hugetlb(pfn, flags, &hugetlb);
  1768. if (hugetlb)
  1769. goto unlock_mutex;
  1770. if (TestSetPageHWPoison(p)) {
  1771. pr_err("%#lx: already hardware poisoned\n", pfn);
  1772. res = -EHWPOISON;
  1773. if (flags & MF_ACTION_REQUIRED)
  1774. res = kill_accessing_process(current, pfn, flags);
  1775. if (flags & MF_COUNT_INCREASED)
  1776. put_page(p);
  1777. goto unlock_mutex;
  1778. }
  1779. hpage = compound_head(p);
  1780. /*
  1781. * We need/can do nothing about count=0 pages.
  1782. * 1) it's a free page, and therefore in safe hand:
  1783. * check_new_page() will be the gate keeper.
  1784. * 2) it's part of a non-compound high order page.
  1785. * Implies some kernel user: cannot stop them from
  1786. * R/W the page; let's pray that the page has been
  1787. * used and will be freed some time later.
  1788. * In fact it's dangerous to directly bump up page count from 0,
  1789. * that may make page_ref_freeze()/page_ref_unfreeze() mismatch.
  1790. */
  1791. if (!(flags & MF_COUNT_INCREASED)) {
  1792. res = get_hwpoison_page(p, flags);
  1793. if (!res) {
  1794. if (is_free_buddy_page(p)) {
  1795. if (take_page_off_buddy(p)) {
  1796. page_ref_inc(p);
  1797. res = MF_RECOVERED;
  1798. } else {
  1799. /* We lost the race, try again */
  1800. if (retry) {
  1801. ClearPageHWPoison(p);
  1802. retry = false;
  1803. goto try_again;
  1804. }
  1805. res = MF_FAILED;
  1806. }
  1807. action_result(pfn, MF_MSG_BUDDY, res);
  1808. res = res == MF_RECOVERED ? 0 : -EBUSY;
  1809. } else {
  1810. action_result(pfn, MF_MSG_KERNEL_HIGH_ORDER, MF_IGNORED);
  1811. res = -EBUSY;
  1812. }
  1813. goto unlock_mutex;
  1814. } else if (res < 0) {
  1815. action_result(pfn, MF_MSG_UNKNOWN, MF_IGNORED);
  1816. res = -EBUSY;
  1817. goto unlock_mutex;
  1818. }
  1819. }
  1820. if (PageTransHuge(hpage)) {
  1821. /*
  1822. * The flag must be set after the refcount is bumped
  1823. * otherwise it may race with THP split.
  1824. * And the flag can't be set in get_hwpoison_page() since
  1825. * it is called by soft offline too and it is just called
  1826. * for !MF_COUNT_INCREASE. So here seems to be the best
  1827. * place.
  1828. *
  1829. * Don't need care about the above error handling paths for
  1830. * get_hwpoison_page() since they handle either free page
  1831. * or unhandlable page. The refcount is bumped iff the
  1832. * page is a valid handlable page.
  1833. */
  1834. SetPageHasHWPoisoned(hpage);
  1835. if (try_to_split_thp_page(p) < 0) {
  1836. action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED);
  1837. res = -EBUSY;
  1838. goto unlock_mutex;
  1839. }
  1840. VM_BUG_ON_PAGE(!page_count(p), p);
  1841. }
  1842. /*
  1843. * We ignore non-LRU pages for good reasons.
  1844. * - PG_locked is only well defined for LRU pages and a few others
  1845. * - to avoid races with __SetPageLocked()
  1846. * - to avoid races with __SetPageSlab*() (and more non-atomic ops)
  1847. * The check (unnecessarily) ignores LRU pages being isolated and
  1848. * walked by the page reclaim code, however that's not a big loss.
  1849. */
  1850. shake_page(p);
  1851. lock_page(p);
  1852. /*
  1853. * We're only intended to deal with the non-Compound page here.
  1854. * However, the page could have changed compound pages due to
  1855. * race window. If this happens, we could try again to hopefully
  1856. * handle the page next round.
  1857. */
  1858. if (PageCompound(p)) {
  1859. if (retry) {
  1860. ClearPageHWPoison(p);
  1861. unlock_page(p);
  1862. put_page(p);
  1863. flags &= ~MF_COUNT_INCREASED;
  1864. retry = false;
  1865. goto try_again;
  1866. }
  1867. action_result(pfn, MF_MSG_DIFFERENT_COMPOUND, MF_IGNORED);
  1868. res = -EBUSY;
  1869. goto unlock_page;
  1870. }
  1871. /*
  1872. * We use page flags to determine what action should be taken, but
  1873. * the flags can be modified by the error containment action. One
  1874. * example is an mlocked page, where PG_mlocked is cleared by
  1875. * page_remove_rmap() in try_to_unmap_one(). So to determine page status
  1876. * correctly, we save a copy of the page flags at this time.
  1877. */
  1878. page_flags = p->flags;
  1879. if (hwpoison_filter(p)) {
  1880. ClearPageHWPoison(p);
  1881. unlock_page(p);
  1882. put_page(p);
  1883. res = -EOPNOTSUPP;
  1884. goto unlock_mutex;
  1885. }
  1886. /*
  1887. * __munlock_pagevec may clear a writeback page's LRU flag without
  1888. * page_lock. We need wait writeback completion for this page or it
  1889. * may trigger vfs BUG while evict inode.
  1890. */
  1891. if (!PageLRU(p) && !PageWriteback(p))
  1892. goto identify_page_state;
  1893. /*
  1894. * It's very difficult to mess with pages currently under IO
  1895. * and in many cases impossible, so we just avoid it here.
  1896. */
  1897. wait_on_page_writeback(p);
  1898. /*
  1899. * Now take care of user space mappings.
  1900. * Abort on fail: __filemap_remove_folio() assumes unmapped page.
  1901. */
  1902. if (!hwpoison_user_mappings(p, pfn, flags, p)) {
  1903. action_result(pfn, MF_MSG_UNMAP_FAILED, MF_IGNORED);
  1904. res = -EBUSY;
  1905. goto unlock_page;
  1906. }
  1907. /*
  1908. * Torn down by someone else?
  1909. */
  1910. if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
  1911. action_result(pfn, MF_MSG_TRUNCATED_LRU, MF_IGNORED);
  1912. res = -EBUSY;
  1913. goto unlock_page;
  1914. }
  1915. identify_page_state:
  1916. res = identify_page_state(pfn, p, page_flags);
  1917. mutex_unlock(&mf_mutex);
  1918. return res;
  1919. unlock_page:
  1920. unlock_page(p);
  1921. unlock_mutex:
  1922. mutex_unlock(&mf_mutex);
  1923. return res;
  1924. }
  1925. EXPORT_SYMBOL_GPL(memory_failure);
  1926. #define MEMORY_FAILURE_FIFO_ORDER 4
  1927. #define MEMORY_FAILURE_FIFO_SIZE (1 << MEMORY_FAILURE_FIFO_ORDER)
  1928. struct memory_failure_entry {
  1929. unsigned long pfn;
  1930. int flags;
  1931. };
  1932. struct memory_failure_cpu {
  1933. DECLARE_KFIFO(fifo, struct memory_failure_entry,
  1934. MEMORY_FAILURE_FIFO_SIZE);
  1935. spinlock_t lock;
  1936. struct work_struct work;
  1937. };
  1938. static DEFINE_PER_CPU(struct memory_failure_cpu, memory_failure_cpu);
  1939. /**
  1940. * memory_failure_queue - Schedule handling memory failure of a page.
  1941. * @pfn: Page Number of the corrupted page
  1942. * @flags: Flags for memory failure handling
  1943. *
  1944. * This function is called by the low level hardware error handler
  1945. * when it detects hardware memory corruption of a page. It schedules
  1946. * the recovering of error page, including dropping pages, killing
  1947. * processes etc.
  1948. *
  1949. * The function is primarily of use for corruptions that
  1950. * happen outside the current execution context (e.g. when
  1951. * detected by a background scrubber)
  1952. *
  1953. * Can run in IRQ context.
  1954. */
  1955. void memory_failure_queue(unsigned long pfn, int flags)
  1956. {
  1957. struct memory_failure_cpu *mf_cpu;
  1958. unsigned long proc_flags;
  1959. struct memory_failure_entry entry = {
  1960. .pfn = pfn,
  1961. .flags = flags,
  1962. };
  1963. mf_cpu = &get_cpu_var(memory_failure_cpu);
  1964. spin_lock_irqsave(&mf_cpu->lock, proc_flags);
  1965. if (kfifo_put(&mf_cpu->fifo, entry))
  1966. schedule_work_on(smp_processor_id(), &mf_cpu->work);
  1967. else
  1968. pr_err("buffer overflow when queuing memory failure at %#lx\n",
  1969. pfn);
  1970. spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
  1971. put_cpu_var(memory_failure_cpu);
  1972. }
  1973. EXPORT_SYMBOL_GPL(memory_failure_queue);
  1974. static void memory_failure_work_func(struct work_struct *work)
  1975. {
  1976. struct memory_failure_cpu *mf_cpu;
  1977. struct memory_failure_entry entry = { 0, };
  1978. unsigned long proc_flags;
  1979. int gotten;
  1980. mf_cpu = container_of(work, struct memory_failure_cpu, work);
  1981. for (;;) {
  1982. spin_lock_irqsave(&mf_cpu->lock, proc_flags);
  1983. gotten = kfifo_get(&mf_cpu->fifo, &entry);
  1984. spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
  1985. if (!gotten)
  1986. break;
  1987. if (entry.flags & MF_SOFT_OFFLINE)
  1988. soft_offline_page(entry.pfn, entry.flags);
  1989. else
  1990. memory_failure(entry.pfn, entry.flags);
  1991. }
  1992. }
  1993. /*
  1994. * Process memory_failure work queued on the specified CPU.
  1995. * Used to avoid return-to-userspace racing with the memory_failure workqueue.
  1996. */
  1997. void memory_failure_queue_kick(int cpu)
  1998. {
  1999. struct memory_failure_cpu *mf_cpu;
  2000. mf_cpu = &per_cpu(memory_failure_cpu, cpu);
  2001. cancel_work_sync(&mf_cpu->work);
  2002. memory_failure_work_func(&mf_cpu->work);
  2003. }
  2004. static int __init memory_failure_init(void)
  2005. {
  2006. struct memory_failure_cpu *mf_cpu;
  2007. int cpu;
  2008. for_each_possible_cpu(cpu) {
  2009. mf_cpu = &per_cpu(memory_failure_cpu, cpu);
  2010. spin_lock_init(&mf_cpu->lock);
  2011. INIT_KFIFO(mf_cpu->fifo);
  2012. INIT_WORK(&mf_cpu->work, memory_failure_work_func);
  2013. }
  2014. return 0;
  2015. }
  2016. core_initcall(memory_failure_init);
  2017. #undef pr_fmt
  2018. #define pr_fmt(fmt) "" fmt
  2019. #define unpoison_pr_info(fmt, pfn, rs) \
  2020. ({ \
  2021. if (__ratelimit(rs)) \
  2022. pr_info(fmt, pfn); \
  2023. })
  2024. /**
  2025. * unpoison_memory - Unpoison a previously poisoned page
  2026. * @pfn: Page number of the to be unpoisoned page
  2027. *
  2028. * Software-unpoison a page that has been poisoned by
  2029. * memory_failure() earlier.
  2030. *
  2031. * This is only done on the software-level, so it only works
  2032. * for linux injected failures, not real hardware failures
  2033. *
  2034. * Returns 0 for success, otherwise -errno.
  2035. */
  2036. int unpoison_memory(unsigned long pfn)
  2037. {
  2038. struct page *page;
  2039. struct page *p;
  2040. int ret = -EBUSY;
  2041. int freeit = 0;
  2042. unsigned long count = 1;
  2043. static DEFINE_RATELIMIT_STATE(unpoison_rs, DEFAULT_RATELIMIT_INTERVAL,
  2044. DEFAULT_RATELIMIT_BURST);
  2045. if (!pfn_valid(pfn))
  2046. return -ENXIO;
  2047. p = pfn_to_page(pfn);
  2048. page = compound_head(p);
  2049. mutex_lock(&mf_mutex);
  2050. if (hw_memory_failure) {
  2051. unpoison_pr_info("Unpoison: Disabled after HW memory failure %#lx\n",
  2052. pfn, &unpoison_rs);
  2053. ret = -EOPNOTSUPP;
  2054. goto unlock_mutex;
  2055. }
  2056. if (!PageHWPoison(p)) {
  2057. unpoison_pr_info("Unpoison: Page was already unpoisoned %#lx\n",
  2058. pfn, &unpoison_rs);
  2059. goto unlock_mutex;
  2060. }
  2061. if (page_count(page) > 1) {
  2062. unpoison_pr_info("Unpoison: Someone grabs the hwpoison page %#lx\n",
  2063. pfn, &unpoison_rs);
  2064. goto unlock_mutex;
  2065. }
  2066. if (page_mapped(page)) {
  2067. unpoison_pr_info("Unpoison: Someone maps the hwpoison page %#lx\n",
  2068. pfn, &unpoison_rs);
  2069. goto unlock_mutex;
  2070. }
  2071. if (page_mapping(page)) {
  2072. unpoison_pr_info("Unpoison: the hwpoison page has non-NULL mapping %#lx\n",
  2073. pfn, &unpoison_rs);
  2074. goto unlock_mutex;
  2075. }
  2076. if (PageSlab(page) || PageTable(page) || PageReserved(page))
  2077. goto unlock_mutex;
  2078. ret = get_hwpoison_page(p, MF_UNPOISON);
  2079. if (!ret) {
  2080. if (PageHuge(p)) {
  2081. count = free_raw_hwp_pages(page, false);
  2082. if (count == 0) {
  2083. ret = -EBUSY;
  2084. goto unlock_mutex;
  2085. }
  2086. }
  2087. ret = TestClearPageHWPoison(page) ? 0 : -EBUSY;
  2088. } else if (ret < 0) {
  2089. if (ret == -EHWPOISON) {
  2090. ret = put_page_back_buddy(p) ? 0 : -EBUSY;
  2091. } else
  2092. unpoison_pr_info("Unpoison: failed to grab page %#lx\n",
  2093. pfn, &unpoison_rs);
  2094. } else {
  2095. if (PageHuge(p)) {
  2096. count = free_raw_hwp_pages(page, false);
  2097. if (count == 0) {
  2098. ret = -EBUSY;
  2099. put_page(page);
  2100. goto unlock_mutex;
  2101. }
  2102. }
  2103. freeit = !!TestClearPageHWPoison(p);
  2104. put_page(page);
  2105. if (freeit) {
  2106. put_page(page);
  2107. ret = 0;
  2108. }
  2109. }
  2110. unlock_mutex:
  2111. mutex_unlock(&mf_mutex);
  2112. if (!ret || freeit) {
  2113. num_poisoned_pages_sub(count);
  2114. unpoison_pr_info("Unpoison: Software-unpoisoned page %#lx\n",
  2115. page_to_pfn(p), &unpoison_rs);
  2116. }
  2117. return ret;
  2118. }
  2119. EXPORT_SYMBOL(unpoison_memory);
  2120. static bool isolate_page(struct page *page, struct list_head *pagelist)
  2121. {
  2122. bool isolated = false;
  2123. if (PageHuge(page)) {
  2124. isolated = !isolate_hugetlb(page, pagelist);
  2125. } else {
  2126. bool lru = !__PageMovable(page);
  2127. if (lru)
  2128. isolated = !isolate_lru_page(page);
  2129. else
  2130. isolated = !isolate_movable_page(page,
  2131. ISOLATE_UNEVICTABLE);
  2132. if (isolated) {
  2133. list_add(&page->lru, pagelist);
  2134. if (lru)
  2135. inc_node_page_state(page, NR_ISOLATED_ANON +
  2136. page_is_file_lru(page));
  2137. }
  2138. }
  2139. /*
  2140. * If we succeed to isolate the page, we grabbed another refcount on
  2141. * the page, so we can safely drop the one we got from get_any_pages().
  2142. * If we failed to isolate the page, it means that we cannot go further
  2143. * and we will return an error, so drop the reference we got from
  2144. * get_any_pages() as well.
  2145. */
  2146. put_page(page);
  2147. return isolated;
  2148. }
  2149. /*
  2150. * soft_offline_in_use_page handles hugetlb-pages and non-hugetlb pages.
  2151. * If the page is a non-dirty unmapped page-cache page, it simply invalidates.
  2152. * If the page is mapped, it migrates the contents over.
  2153. */
  2154. static int soft_offline_in_use_page(struct page *page)
  2155. {
  2156. long ret = 0;
  2157. unsigned long pfn = page_to_pfn(page);
  2158. struct page *hpage = compound_head(page);
  2159. char const *msg_page[] = {"page", "hugepage"};
  2160. bool huge = PageHuge(page);
  2161. LIST_HEAD(pagelist);
  2162. struct migration_target_control mtc = {
  2163. .nid = NUMA_NO_NODE,
  2164. .gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL,
  2165. };
  2166. if (!huge && PageTransHuge(hpage)) {
  2167. if (try_to_split_thp_page(page)) {
  2168. pr_info("soft offline: %#lx: thp split failed\n", pfn);
  2169. return -EBUSY;
  2170. }
  2171. hpage = page;
  2172. }
  2173. lock_page(page);
  2174. if (!PageHuge(page))
  2175. wait_on_page_writeback(page);
  2176. if (PageHWPoison(page)) {
  2177. unlock_page(page);
  2178. put_page(page);
  2179. pr_info("soft offline: %#lx page already poisoned\n", pfn);
  2180. return 0;
  2181. }
  2182. if (!PageHuge(page) && PageLRU(page) && !PageSwapCache(page))
  2183. /*
  2184. * Try to invalidate first. This should work for
  2185. * non dirty unmapped page cache pages.
  2186. */
  2187. ret = invalidate_inode_page(page);
  2188. unlock_page(page);
  2189. if (ret) {
  2190. pr_info("soft_offline: %#lx: invalidated\n", pfn);
  2191. page_handle_poison(page, false, true);
  2192. return 0;
  2193. }
  2194. if (isolate_page(hpage, &pagelist)) {
  2195. ret = migrate_pages(&pagelist, alloc_migration_target, NULL,
  2196. (unsigned long)&mtc, MIGRATE_SYNC, MR_MEMORY_FAILURE, NULL);
  2197. if (!ret) {
  2198. bool release = !huge;
  2199. if (!page_handle_poison(page, huge, release))
  2200. ret = -EBUSY;
  2201. } else {
  2202. if (!list_empty(&pagelist))
  2203. putback_movable_pages(&pagelist);
  2204. pr_info("soft offline: %#lx: %s migration failed %ld, type %pGp\n",
  2205. pfn, msg_page[huge], ret, &page->flags);
  2206. if (ret > 0)
  2207. ret = -EBUSY;
  2208. }
  2209. } else {
  2210. pr_info("soft offline: %#lx: %s isolation failed, page count %d, type %pGp\n",
  2211. pfn, msg_page[huge], page_count(page), &page->flags);
  2212. ret = -EBUSY;
  2213. }
  2214. return ret;
  2215. }
  2216. static void put_ref_page(struct page *page)
  2217. {
  2218. if (page)
  2219. put_page(page);
  2220. }
  2221. /**
  2222. * soft_offline_page - Soft offline a page.
  2223. * @pfn: pfn to soft-offline
  2224. * @flags: flags. Same as memory_failure().
  2225. *
  2226. * Returns 0 on success
  2227. * -EOPNOTSUPP for hwpoison_filter() filtered the error event
  2228. * < 0 otherwise negated errno.
  2229. *
  2230. * Soft offline a page, by migration or invalidation,
  2231. * without killing anything. This is for the case when
  2232. * a page is not corrupted yet (so it's still valid to access),
  2233. * but has had a number of corrected errors and is better taken
  2234. * out.
  2235. *
  2236. * The actual policy on when to do that is maintained by
  2237. * user space.
  2238. *
  2239. * This should never impact any application or cause data loss,
  2240. * however it might take some time.
  2241. *
  2242. * This is not a 100% solution for all memory, but tries to be
  2243. * ``good enough'' for the majority of memory.
  2244. */
  2245. int soft_offline_page(unsigned long pfn, int flags)
  2246. {
  2247. int ret;
  2248. bool try_again = true;
  2249. struct page *page, *ref_page = NULL;
  2250. WARN_ON_ONCE(!pfn_valid(pfn) && (flags & MF_COUNT_INCREASED));
  2251. if (!pfn_valid(pfn))
  2252. return -ENXIO;
  2253. if (flags & MF_COUNT_INCREASED)
  2254. ref_page = pfn_to_page(pfn);
  2255. /* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
  2256. page = pfn_to_online_page(pfn);
  2257. if (!page) {
  2258. put_ref_page(ref_page);
  2259. return -EIO;
  2260. }
  2261. mutex_lock(&mf_mutex);
  2262. if (PageHWPoison(page)) {
  2263. pr_info("%s: %#lx page already poisoned\n", __func__, pfn);
  2264. put_ref_page(ref_page);
  2265. mutex_unlock(&mf_mutex);
  2266. return 0;
  2267. }
  2268. retry:
  2269. get_online_mems();
  2270. ret = get_hwpoison_page(page, flags | MF_SOFT_OFFLINE);
  2271. put_online_mems();
  2272. if (hwpoison_filter(page)) {
  2273. if (ret > 0)
  2274. put_page(page);
  2275. mutex_unlock(&mf_mutex);
  2276. return -EOPNOTSUPP;
  2277. }
  2278. if (ret > 0) {
  2279. ret = soft_offline_in_use_page(page);
  2280. } else if (ret == 0) {
  2281. if (!page_handle_poison(page, true, false)) {
  2282. if (try_again) {
  2283. try_again = false;
  2284. flags &= ~MF_COUNT_INCREASED;
  2285. goto retry;
  2286. }
  2287. ret = -EBUSY;
  2288. }
  2289. }
  2290. mutex_unlock(&mf_mutex);
  2291. return ret;
  2292. }
  2293. void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
  2294. {
  2295. int i, total = 0;
  2296. /*
  2297. * A further optimization is to have per section refcounted
  2298. * num_poisoned_pages. But that would need more space per memmap, so
  2299. * for now just do a quick global check to speed up this routine in the
  2300. * absence of bad pages.
  2301. */
  2302. if (atomic_long_read(&num_poisoned_pages) == 0)
  2303. return;
  2304. for (i = 0; i < nr_pages; i++) {
  2305. if (PageHWPoison(&memmap[i])) {
  2306. total++;
  2307. ClearPageHWPoison(&memmap[i]);
  2308. }
  2309. }
  2310. if (total)
  2311. num_poisoned_pages_sub(total);
  2312. }