blob: 32a644d9c2eeb8a179f6cfe72021a7e6c21fdaf8 [file] [log] [blame]
Andi Kleen6a460792009-09-16 11:50:15 +02001/*
2 * Copyright (C) 2008, 2009 Intel Corporation
3 * Authors: Andi Kleen, Fengguang Wu
4 *
5 * This software may be redistributed and/or modified under the terms of
6 * the GNU General Public License ("GPL") version 2 only as published by the
7 * Free Software Foundation.
8 *
9 * High level machine check handler. Handles pages reported by the
Andi Kleen1c80b992010-09-27 23:09:51 +020010 * hardware as being corrupted usually due to a multi-bit ECC memory or cache
Andi Kleen6a460792009-09-16 11:50:15 +020011 * failure.
Andi Kleen1c80b992010-09-27 23:09:51 +020012 *
13 * In addition there is a "soft offline" entry point that allows stop using
14 * not-yet-corrupted-by-suspicious pages without killing anything.
Andi Kleen6a460792009-09-16 11:50:15 +020015 *
16 * Handles page cache pages in various states. The tricky part
Andi Kleen1c80b992010-09-27 23:09:51 +020017 * here is that we can access any page asynchronously in respect to
18 * other VM users, because memory failures could happen anytime and
19 * anywhere. This could violate some of their assumptions. This is why
20 * this code has to be extremely careful. Generally it tries to use
21 * normal locking rules, as in get the standard locks, even if that means
22 * the error handling takes potentially a long time.
Andi Kleene0de78d2015-06-24 16:56:02 -070023 *
24 * It can be very tempting to add handling for obscure cases here.
25 * In general any code for handling new cases should only be added iff:
26 * - You know how to test it.
27 * - You have a test that can be added to mce-test
28 * https://git.kernel.org/cgit/utils/cpu/mce/mce-test.git/
29 * - The case actually shows up as a frequent (top 10) page state in
30 * tools/vm/page-types when running a real workload.
Andi Kleen1c80b992010-09-27 23:09:51 +020031 *
32 * There are several operations here with exponential complexity because
33 * of unsuitable VM data structures. For example the operation to map back
34 * from RMAP chains to processes has to walk the complete process list and
35 * has non linear complexity with the number. But since memory corruptions
36 * are rare we hope to get away with this. This avoids impacting the core
37 * VM.
Andi Kleen6a460792009-09-16 11:50:15 +020038 */
Andi Kleen6a460792009-09-16 11:50:15 +020039#include <linux/kernel.h>
40#include <linux/mm.h>
41#include <linux/page-flags.h>
Wu Fengguang478c5ff2009-12-16 12:19:59 +010042#include <linux/kernel-page-flags.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010043#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010044#include <linux/sched/task.h>
Hugh Dickins01e00f82009-10-13 15:02:11 +010045#include <linux/ksm.h>
Andi Kleen6a460792009-09-16 11:50:15 +020046#include <linux/rmap.h>
Paul Gortmakerb9e15ba2011-05-26 16:00:52 -040047#include <linux/export.h>
Andi Kleen6a460792009-09-16 11:50:15 +020048#include <linux/pagemap.h>
49#include <linux/swap.h>
50#include <linux/backing-dev.h>
Andi Kleenfacb6012009-12-16 12:20:00 +010051#include <linux/migrate.h>
Andi Kleenfacb6012009-12-16 12:20:00 +010052#include <linux/suspend.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
Huang Yingbf998152010-05-31 14:28:19 +080054#include <linux/swapops.h>
Naoya Horiguchi7af446a2010-05-28 09:29:17 +090055#include <linux/hugetlb.h>
KOSAKI Motohiro20d6c962010-12-02 14:31:19 -080056#include <linux/memory_hotplug.h>
Minchan Kim5db8a732011-06-15 15:08:48 -070057#include <linux/mm_inline.h>
Dan Williams6100e342018-07-13 21:50:21 -070058#include <linux/memremap.h>
Huang Yingea8f5fb2011-07-13 13:14:27 +080059#include <linux/kfifo.h>
Naoya Horiguchia5f65102015-11-05 18:47:26 -080060#include <linux/ratelimit.h>
Andi Kleen6a460792009-09-16 11:50:15 +020061#include "internal.h"
Xie XiuQi97f0b132015-06-24 16:57:36 -070062#include "ras/ras_event.h"
Andi Kleen6a460792009-09-16 11:50:15 +020063
64int sysctl_memory_failure_early_kill __read_mostly = 0;
65
66int sysctl_memory_failure_recovery __read_mostly = 1;
67
Xishi Qiu293c07e2013-02-22 16:34:02 -080068atomic_long_t num_poisoned_pages __read_mostly = ATOMIC_LONG_INIT(0);
Andi Kleen6a460792009-09-16 11:50:15 +020069
Andi Kleen27df5062009-12-21 19:56:42 +010070#if defined(CONFIG_HWPOISON_INJECT) || defined(CONFIG_HWPOISON_INJECT_MODULE)
71
Haicheng Li1bfe5fe2009-12-16 12:19:59 +010072u32 hwpoison_filter_enable = 0;
Wu Fengguang7c116f22009-12-16 12:19:59 +010073u32 hwpoison_filter_dev_major = ~0U;
74u32 hwpoison_filter_dev_minor = ~0U;
Wu Fengguang478c5ff2009-12-16 12:19:59 +010075u64 hwpoison_filter_flags_mask;
76u64 hwpoison_filter_flags_value;
Haicheng Li1bfe5fe2009-12-16 12:19:59 +010077EXPORT_SYMBOL_GPL(hwpoison_filter_enable);
Wu Fengguang7c116f22009-12-16 12:19:59 +010078EXPORT_SYMBOL_GPL(hwpoison_filter_dev_major);
79EXPORT_SYMBOL_GPL(hwpoison_filter_dev_minor);
Wu Fengguang478c5ff2009-12-16 12:19:59 +010080EXPORT_SYMBOL_GPL(hwpoison_filter_flags_mask);
81EXPORT_SYMBOL_GPL(hwpoison_filter_flags_value);
Wu Fengguang7c116f22009-12-16 12:19:59 +010082
83static int hwpoison_filter_dev(struct page *p)
84{
85 struct address_space *mapping;
86 dev_t dev;
87
88 if (hwpoison_filter_dev_major == ~0U &&
89 hwpoison_filter_dev_minor == ~0U)
90 return 0;
91
92 /*
Andi Kleen1c80b992010-09-27 23:09:51 +020093 * page_mapping() does not accept slab pages.
Wu Fengguang7c116f22009-12-16 12:19:59 +010094 */
95 if (PageSlab(p))
96 return -EINVAL;
97
98 mapping = page_mapping(p);
99 if (mapping == NULL || mapping->host == NULL)
100 return -EINVAL;
101
102 dev = mapping->host->i_sb->s_dev;
103 if (hwpoison_filter_dev_major != ~0U &&
104 hwpoison_filter_dev_major != MAJOR(dev))
105 return -EINVAL;
106 if (hwpoison_filter_dev_minor != ~0U &&
107 hwpoison_filter_dev_minor != MINOR(dev))
108 return -EINVAL;
109
110 return 0;
111}
112
Wu Fengguang478c5ff2009-12-16 12:19:59 +0100113static int hwpoison_filter_flags(struct page *p)
114{
115 if (!hwpoison_filter_flags_mask)
116 return 0;
117
118 if ((stable_page_flags(p) & hwpoison_filter_flags_mask) ==
119 hwpoison_filter_flags_value)
120 return 0;
121 else
122 return -EINVAL;
123}
124
Andi Kleen4fd466e2009-12-16 12:19:59 +0100125/*
126 * This allows stress tests to limit test scope to a collection of tasks
127 * by putting them under some memcg. This prevents killing unrelated/important
128 * processes such as /sbin/init. Note that the target task may share clean
129 * pages with init (eg. libc text), which is harmless. If the target task
130 * share _dirty_ pages with another task B, the test scheme must make sure B
131 * is also included in the memcg. At last, due to race conditions this filter
132 * can only guarantee that the page either belongs to the memcg tasks, or is
133 * a freed page.
134 */
Vladimir Davydov94a59fb2015-09-09 15:35:31 -0700135#ifdef CONFIG_MEMCG
Andi Kleen4fd466e2009-12-16 12:19:59 +0100136u64 hwpoison_filter_memcg;
137EXPORT_SYMBOL_GPL(hwpoison_filter_memcg);
138static int hwpoison_filter_task(struct page *p)
139{
Andi Kleen4fd466e2009-12-16 12:19:59 +0100140 if (!hwpoison_filter_memcg)
141 return 0;
142
Vladimir Davydov94a59fb2015-09-09 15:35:31 -0700143 if (page_cgroup_ino(p) != hwpoison_filter_memcg)
Andi Kleen4fd466e2009-12-16 12:19:59 +0100144 return -EINVAL;
145
146 return 0;
147}
148#else
149static int hwpoison_filter_task(struct page *p) { return 0; }
150#endif
151
Wu Fengguang7c116f22009-12-16 12:19:59 +0100152int hwpoison_filter(struct page *p)
153{
Haicheng Li1bfe5fe2009-12-16 12:19:59 +0100154 if (!hwpoison_filter_enable)
155 return 0;
156
Wu Fengguang7c116f22009-12-16 12:19:59 +0100157 if (hwpoison_filter_dev(p))
158 return -EINVAL;
159
Wu Fengguang478c5ff2009-12-16 12:19:59 +0100160 if (hwpoison_filter_flags(p))
161 return -EINVAL;
162
Andi Kleen4fd466e2009-12-16 12:19:59 +0100163 if (hwpoison_filter_task(p))
164 return -EINVAL;
165
Wu Fengguang7c116f22009-12-16 12:19:59 +0100166 return 0;
167}
Andi Kleen27df5062009-12-21 19:56:42 +0100168#else
169int hwpoison_filter(struct page *p)
170{
171 return 0;
172}
173#endif
174
Wu Fengguang7c116f22009-12-16 12:19:59 +0100175EXPORT_SYMBOL_GPL(hwpoison_filter);
176
Andi Kleen6a460792009-09-16 11:50:15 +0200177/*
Dan Williamsae1139e2018-07-13 21:50:11 -0700178 * Kill all processes that have a poisoned page mapped and then isolate
179 * the page.
180 *
181 * General strategy:
182 * Find all processes having the page mapped and kill them.
183 * But we keep a page reference around so that the page is not
184 * actually freed yet.
185 * Then stash the page away
186 *
187 * There's no convenient way to get back to mapped processes
188 * from the VMAs. So do a brute-force search over all
189 * running processes.
190 *
191 * Remember that machine checks are not common (or rather
192 * if they are common you have other problems), so this shouldn't
193 * be a performance issue.
194 *
195 * Also there are some races possible while we get from the
196 * error detection to actually handle it.
197 */
198
199struct to_kill {
200 struct list_head nd;
201 struct task_struct *tsk;
202 unsigned long addr;
203 short size_shift;
204 char addr_valid;
205};
206
207/*
Tony Luck7329bbe2011-12-13 09:27:58 -0800208 * Send all the processes who have the page mapped a signal.
209 * ``action optional'' if they are not immediately affected by the error
210 * ``action required'' if error happened in current execution context
Andi Kleen6a460792009-09-16 11:50:15 +0200211 */
Dan Williamsae1139e2018-07-13 21:50:11 -0700212static int kill_proc(struct to_kill *tk, unsigned long pfn, int flags)
Andi Kleen6a460792009-09-16 11:50:15 +0200213{
Dan Williamsae1139e2018-07-13 21:50:11 -0700214 struct task_struct *t = tk->tsk;
215 short addr_lsb = tk->size_shift;
Andi Kleen6a460792009-09-16 11:50:15 +0200216 int ret;
217
Chen Yucong495367c02016-05-20 16:57:32 -0700218 pr_err("Memory failure: %#lx: Killing %s:%d due to hardware memory corruption\n",
219 pfn, t->comm, t->pid);
Tony Luck7329bbe2011-12-13 09:27:58 -0800220
Tony Lucka70ffca2014-06-04 16:10:59 -0700221 if ((flags & MF_ACTION_REQUIRED) && t->mm == current->mm) {
Dan Williamsae1139e2018-07-13 21:50:11 -0700222 ret = force_sig_mceerr(BUS_MCEERR_AR, (void __user *)tk->addr,
Eric W. Biedermanc0f45552017-08-02 13:51:22 -0500223 addr_lsb, current);
Tony Luck7329bbe2011-12-13 09:27:58 -0800224 } else {
225 /*
226 * Don't use force here, it's convenient if the signal
227 * can be temporarily blocked.
228 * This could cause a loop when the user sets SIGBUS
229 * to SIG_IGN, but hopefully no one will do that?
230 */
Dan Williamsae1139e2018-07-13 21:50:11 -0700231 ret = send_sig_mceerr(BUS_MCEERR_AO, (void __user *)tk->addr,
Eric W. Biedermanc0f45552017-08-02 13:51:22 -0500232 addr_lsb, t); /* synchronous? */
Tony Luck7329bbe2011-12-13 09:27:58 -0800233 }
Andi Kleen6a460792009-09-16 11:50:15 +0200234 if (ret < 0)
Chen Yucong495367c02016-05-20 16:57:32 -0700235 pr_info("Memory failure: Error sending signal to %s:%d: %d\n",
Joe Perches11705322016-03-17 14:19:50 -0700236 t->comm, t->pid, ret);
Andi Kleen6a460792009-09-16 11:50:15 +0200237 return ret;
238}
239
240/*
Andi Kleen588f9ce2009-12-16 12:19:57 +0100241 * When a unknown page type is encountered drain as many buffers as possible
242 * in the hope to turn the page into a LRU or free page, which we can handle.
243 */
Andi Kleenfacb6012009-12-16 12:20:00 +0100244void shake_page(struct page *p, int access)
Andi Kleen588f9ce2009-12-16 12:19:57 +0100245{
Naoya Horiguchi8bcb74d2017-05-03 14:56:19 -0700246 if (PageHuge(p))
247 return;
248
Andi Kleen588f9ce2009-12-16 12:19:57 +0100249 if (!PageSlab(p)) {
250 lru_add_drain_all();
251 if (PageLRU(p))
252 return;
Vlastimil Babkac0554322014-12-10 15:43:10 -0800253 drain_all_pages(page_zone(p));
Andi Kleen588f9ce2009-12-16 12:19:57 +0100254 if (PageLRU(p) || is_free_buddy_page(p))
255 return;
256 }
Andi Kleenfacb6012009-12-16 12:20:00 +0100257
Andi Kleen588f9ce2009-12-16 12:19:57 +0100258 /*
Johannes Weiner6b4f7792014-12-12 16:56:13 -0800259 * Only call shrink_node_slabs here (which would also shrink
260 * other caches) if access is not potentially fatal.
Andi Kleen588f9ce2009-12-16 12:19:57 +0100261 */
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800262 if (access)
263 drop_slab_node(page_to_nid(p));
Andi Kleen588f9ce2009-12-16 12:19:57 +0100264}
265EXPORT_SYMBOL_GPL(shake_page);
266
Dan Williams6100e342018-07-13 21:50:21 -0700267static unsigned long dev_pagemap_mapping_shift(struct page *page,
268 struct vm_area_struct *vma)
269{
270 unsigned long address = vma_address(page, vma);
271 pgd_t *pgd;
272 p4d_t *p4d;
273 pud_t *pud;
274 pmd_t *pmd;
275 pte_t *pte;
276
277 pgd = pgd_offset(vma->vm_mm, address);
278 if (!pgd_present(*pgd))
279 return 0;
280 p4d = p4d_offset(pgd, address);
281 if (!p4d_present(*p4d))
282 return 0;
283 pud = pud_offset(p4d, address);
284 if (!pud_present(*pud))
285 return 0;
286 if (pud_devmap(*pud))
287 return PUD_SHIFT;
288 pmd = pmd_offset(pud, address);
289 if (!pmd_present(*pmd))
290 return 0;
291 if (pmd_devmap(*pmd))
292 return PMD_SHIFT;
293 pte = pte_offset_map(pmd, address);
294 if (!pte_present(*pte))
295 return 0;
296 if (pte_devmap(*pte))
297 return PAGE_SHIFT;
298 return 0;
299}
300
Andi Kleen588f9ce2009-12-16 12:19:57 +0100301/*
Andi Kleen6a460792009-09-16 11:50:15 +0200302 * Failure handling: if we can't find or can't kill a process there's
303 * not much we can do. We just print a message and ignore otherwise.
304 */
305
306/*
307 * Schedule a process for later kill.
308 * Uses GFP_ATOMIC allocations to avoid potential recursions in the VM.
309 * TBD would GFP_NOIO be enough?
310 */
311static void add_to_kill(struct task_struct *tsk, struct page *p,
312 struct vm_area_struct *vma,
313 struct list_head *to_kill,
314 struct to_kill **tkc)
315{
316 struct to_kill *tk;
317
318 if (*tkc) {
319 tk = *tkc;
320 *tkc = NULL;
321 } else {
322 tk = kmalloc(sizeof(struct to_kill), GFP_ATOMIC);
323 if (!tk) {
Chen Yucong495367c02016-05-20 16:57:32 -0700324 pr_err("Memory failure: Out of memory while machine check handling\n");
Andi Kleen6a460792009-09-16 11:50:15 +0200325 return;
326 }
327 }
328 tk->addr = page_address_in_vma(p, vma);
329 tk->addr_valid = 1;
Dan Williams6100e342018-07-13 21:50:21 -0700330 if (is_zone_device_page(p))
331 tk->size_shift = dev_pagemap_mapping_shift(p, vma);
332 else
333 tk->size_shift = compound_order(compound_head(p)) + PAGE_SHIFT;
Andi Kleen6a460792009-09-16 11:50:15 +0200334
335 /*
336 * In theory we don't have to kill when the page was
337 * munmaped. But it could be also a mremap. Since that's
338 * likely very rare kill anyways just out of paranoia, but use
339 * a SIGKILL because the error is not contained anymore.
340 */
Dan Williams6100e342018-07-13 21:50:21 -0700341 if (tk->addr == -EFAULT || tk->size_shift == 0) {
Chen Yucong495367c02016-05-20 16:57:32 -0700342 pr_info("Memory failure: Unable to find user space address %lx in %s\n",
Andi Kleen6a460792009-09-16 11:50:15 +0200343 page_to_pfn(p), tsk->comm);
344 tk->addr_valid = 0;
345 }
346 get_task_struct(tsk);
347 tk->tsk = tsk;
348 list_add_tail(&tk->nd, to_kill);
349}
350
351/*
352 * Kill the processes that have been collected earlier.
353 *
354 * Only do anything when DOIT is set, otherwise just free the list
355 * (this is used for clean pages which do not need killing)
356 * Also when FAIL is set do a force kill because something went
357 * wrong earlier.
358 */
Dan Williamsae1139e2018-07-13 21:50:11 -0700359static void kill_procs(struct list_head *to_kill, int forcekill, bool fail,
360 unsigned long pfn, int flags)
Andi Kleen6a460792009-09-16 11:50:15 +0200361{
362 struct to_kill *tk, *next;
363
364 list_for_each_entry_safe (tk, next, to_kill, nd) {
Tony Luck6751ed62012-07-11 10:20:47 -0700365 if (forcekill) {
Andi Kleen6a460792009-09-16 11:50:15 +0200366 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200367 * In case something went wrong with munmapping
Andi Kleen6a460792009-09-16 11:50:15 +0200368 * make sure the process doesn't catch the
369 * signal and then access the memory. Just kill it.
Andi Kleen6a460792009-09-16 11:50:15 +0200370 */
371 if (fail || tk->addr_valid == 0) {
Chen Yucong495367c02016-05-20 16:57:32 -0700372 pr_err("Memory failure: %#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
Joe Perches11705322016-03-17 14:19:50 -0700373 pfn, tk->tsk->comm, tk->tsk->pid);
Andi Kleen6a460792009-09-16 11:50:15 +0200374 force_sig(SIGKILL, tk->tsk);
375 }
376
377 /*
378 * In theory the process could have mapped
379 * something else on the address in-between. We could
380 * check for that, but we need to tell the
381 * process anyways.
382 */
Dan Williamsae1139e2018-07-13 21:50:11 -0700383 else if (kill_proc(tk, pfn, flags) < 0)
Chen Yucong495367c02016-05-20 16:57:32 -0700384 pr_err("Memory failure: %#lx: Cannot send advisory machine check signal to %s:%d\n",
Joe Perches11705322016-03-17 14:19:50 -0700385 pfn, tk->tsk->comm, tk->tsk->pid);
Andi Kleen6a460792009-09-16 11:50:15 +0200386 }
387 put_task_struct(tk->tsk);
388 kfree(tk);
389 }
390}
391
Naoya Horiguchi3ba08122014-06-04 16:11:02 -0700392/*
393 * Find a dedicated thread which is supposed to handle SIGBUS(BUS_MCEERR_AO)
394 * on behalf of the thread group. Return task_struct of the (first found)
395 * dedicated thread if found, and return NULL otherwise.
396 *
397 * We already hold read_lock(&tasklist_lock) in the caller, so we don't
398 * have to call rcu_read_lock/unlock() in this function.
399 */
400static struct task_struct *find_early_kill_thread(struct task_struct *tsk)
Andi Kleen6a460792009-09-16 11:50:15 +0200401{
Naoya Horiguchi3ba08122014-06-04 16:11:02 -0700402 struct task_struct *t;
403
404 for_each_thread(tsk, t)
405 if ((t->flags & PF_MCE_PROCESS) && (t->flags & PF_MCE_EARLY))
406 return t;
407 return NULL;
408}
409
410/*
411 * Determine whether a given process is "early kill" process which expects
412 * to be signaled when some page under the process is hwpoisoned.
413 * Return task_struct of the dedicated thread (main thread unless explicitly
414 * specified) if the process is "early kill," and otherwise returns NULL.
415 */
416static struct task_struct *task_early_kill(struct task_struct *tsk,
417 int force_early)
418{
419 struct task_struct *t;
Andi Kleen6a460792009-09-16 11:50:15 +0200420 if (!tsk->mm)
Naoya Horiguchi3ba08122014-06-04 16:11:02 -0700421 return NULL;
Tony Luck74614de2014-06-04 16:11:01 -0700422 if (force_early)
Naoya Horiguchi3ba08122014-06-04 16:11:02 -0700423 return tsk;
424 t = find_early_kill_thread(tsk);
425 if (t)
426 return t;
427 if (sysctl_memory_failure_early_kill)
428 return tsk;
429 return NULL;
Andi Kleen6a460792009-09-16 11:50:15 +0200430}
431
432/*
433 * Collect processes when the error hit an anonymous page.
434 */
435static void collect_procs_anon(struct page *page, struct list_head *to_kill,
Tony Luck74614de2014-06-04 16:11:01 -0700436 struct to_kill **tkc, int force_early)
Andi Kleen6a460792009-09-16 11:50:15 +0200437{
438 struct vm_area_struct *vma;
439 struct task_struct *tsk;
440 struct anon_vma *av;
Michel Lespinassebf181b92012-10-08 16:31:39 -0700441 pgoff_t pgoff;
Andi Kleen6a460792009-09-16 11:50:15 +0200442
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000443 av = page_lock_anon_vma_read(page);
Andi Kleen6a460792009-09-16 11:50:15 +0200444 if (av == NULL) /* Not actually mapped anymore */
Peter Zijlstra9b679322011-06-27 16:18:09 -0700445 return;
446
Naoya Horiguchia0f7a752014-07-23 14:00:01 -0700447 pgoff = page_to_pgoff(page);
Peter Zijlstra9b679322011-06-27 16:18:09 -0700448 read_lock(&tasklist_lock);
Andi Kleen6a460792009-09-16 11:50:15 +0200449 for_each_process (tsk) {
Rik van Riel5beb4932010-03-05 13:42:07 -0800450 struct anon_vma_chain *vmac;
Naoya Horiguchi3ba08122014-06-04 16:11:02 -0700451 struct task_struct *t = task_early_kill(tsk, force_early);
Rik van Riel5beb4932010-03-05 13:42:07 -0800452
Naoya Horiguchi3ba08122014-06-04 16:11:02 -0700453 if (!t)
Andi Kleen6a460792009-09-16 11:50:15 +0200454 continue;
Michel Lespinassebf181b92012-10-08 16:31:39 -0700455 anon_vma_interval_tree_foreach(vmac, &av->rb_root,
456 pgoff, pgoff) {
Rik van Riel5beb4932010-03-05 13:42:07 -0800457 vma = vmac->vma;
Andi Kleen6a460792009-09-16 11:50:15 +0200458 if (!page_mapped_in_vma(page, vma))
459 continue;
Naoya Horiguchi3ba08122014-06-04 16:11:02 -0700460 if (vma->vm_mm == t->mm)
461 add_to_kill(t, page, vma, to_kill, tkc);
Andi Kleen6a460792009-09-16 11:50:15 +0200462 }
463 }
Andi Kleen6a460792009-09-16 11:50:15 +0200464 read_unlock(&tasklist_lock);
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000465 page_unlock_anon_vma_read(av);
Andi Kleen6a460792009-09-16 11:50:15 +0200466}
467
468/*
469 * Collect processes when the error hit a file mapped page.
470 */
471static void collect_procs_file(struct page *page, struct list_head *to_kill,
Tony Luck74614de2014-06-04 16:11:01 -0700472 struct to_kill **tkc, int force_early)
Andi Kleen6a460792009-09-16 11:50:15 +0200473{
474 struct vm_area_struct *vma;
475 struct task_struct *tsk;
Andi Kleen6a460792009-09-16 11:50:15 +0200476 struct address_space *mapping = page->mapping;
477
Davidlohr Buesod28eb9c2014-12-12 16:54:36 -0800478 i_mmap_lock_read(mapping);
Peter Zijlstra9b679322011-06-27 16:18:09 -0700479 read_lock(&tasklist_lock);
Andi Kleen6a460792009-09-16 11:50:15 +0200480 for_each_process(tsk) {
Naoya Horiguchia0f7a752014-07-23 14:00:01 -0700481 pgoff_t pgoff = page_to_pgoff(page);
Naoya Horiguchi3ba08122014-06-04 16:11:02 -0700482 struct task_struct *t = task_early_kill(tsk, force_early);
Andi Kleen6a460792009-09-16 11:50:15 +0200483
Naoya Horiguchi3ba08122014-06-04 16:11:02 -0700484 if (!t)
Andi Kleen6a460792009-09-16 11:50:15 +0200485 continue;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700486 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff,
Andi Kleen6a460792009-09-16 11:50:15 +0200487 pgoff) {
488 /*
489 * Send early kill signal to tasks where a vma covers
490 * the page but the corrupted page is not necessarily
491 * mapped it in its pte.
492 * Assume applications who requested early kill want
493 * to be informed of all such data corruptions.
494 */
Naoya Horiguchi3ba08122014-06-04 16:11:02 -0700495 if (vma->vm_mm == t->mm)
496 add_to_kill(t, page, vma, to_kill, tkc);
Andi Kleen6a460792009-09-16 11:50:15 +0200497 }
498 }
Andi Kleen6a460792009-09-16 11:50:15 +0200499 read_unlock(&tasklist_lock);
Davidlohr Buesod28eb9c2014-12-12 16:54:36 -0800500 i_mmap_unlock_read(mapping);
Andi Kleen6a460792009-09-16 11:50:15 +0200501}
502
503/*
504 * Collect the processes who have the corrupted page mapped to kill.
505 * This is done in two steps for locking reasons.
506 * First preallocate one tokill structure outside the spin locks,
507 * so that we can kill at least one process reasonably reliable.
508 */
Tony Luck74614de2014-06-04 16:11:01 -0700509static void collect_procs(struct page *page, struct list_head *tokill,
510 int force_early)
Andi Kleen6a460792009-09-16 11:50:15 +0200511{
512 struct to_kill *tk;
513
514 if (!page->mapping)
515 return;
516
517 tk = kmalloc(sizeof(struct to_kill), GFP_NOIO);
518 if (!tk)
519 return;
520 if (PageAnon(page))
Tony Luck74614de2014-06-04 16:11:01 -0700521 collect_procs_anon(page, tokill, &tk, force_early);
Andi Kleen6a460792009-09-16 11:50:15 +0200522 else
Tony Luck74614de2014-06-04 16:11:01 -0700523 collect_procs_file(page, tokill, &tk, force_early);
Andi Kleen6a460792009-09-16 11:50:15 +0200524 kfree(tk);
525}
526
Andi Kleen6a460792009-09-16 11:50:15 +0200527static const char *action_name[] = {
Xie XiuQicc637b12015-06-24 16:57:30 -0700528 [MF_IGNORED] = "Ignored",
529 [MF_FAILED] = "Failed",
530 [MF_DELAYED] = "Delayed",
531 [MF_RECOVERED] = "Recovered",
Naoya Horiguchi64d37a22015-04-15 16:13:05 -0700532};
533
534static const char * const action_page_types[] = {
Xie XiuQicc637b12015-06-24 16:57:30 -0700535 [MF_MSG_KERNEL] = "reserved kernel page",
536 [MF_MSG_KERNEL_HIGH_ORDER] = "high-order kernel page",
537 [MF_MSG_SLAB] = "kernel slab page",
538 [MF_MSG_DIFFERENT_COMPOUND] = "different compound page after locking",
539 [MF_MSG_POISONED_HUGE] = "huge page already hardware poisoned",
540 [MF_MSG_HUGE] = "huge page",
541 [MF_MSG_FREE_HUGE] = "free huge page",
Naoya Horiguchi31286a82018-04-05 16:23:05 -0700542 [MF_MSG_NON_PMD_HUGE] = "non-pmd-sized huge page",
Xie XiuQicc637b12015-06-24 16:57:30 -0700543 [MF_MSG_UNMAP_FAILED] = "unmapping failed page",
544 [MF_MSG_DIRTY_SWAPCACHE] = "dirty swapcache page",
545 [MF_MSG_CLEAN_SWAPCACHE] = "clean swapcache page",
546 [MF_MSG_DIRTY_MLOCKED_LRU] = "dirty mlocked LRU page",
547 [MF_MSG_CLEAN_MLOCKED_LRU] = "clean mlocked LRU page",
548 [MF_MSG_DIRTY_UNEVICTABLE_LRU] = "dirty unevictable LRU page",
549 [MF_MSG_CLEAN_UNEVICTABLE_LRU] = "clean unevictable LRU page",
550 [MF_MSG_DIRTY_LRU] = "dirty LRU page",
551 [MF_MSG_CLEAN_LRU] = "clean LRU page",
552 [MF_MSG_TRUNCATED_LRU] = "already truncated LRU page",
553 [MF_MSG_BUDDY] = "free buddy page",
554 [MF_MSG_BUDDY_2ND] = "free buddy page (2nd try)",
Dan Williams6100e342018-07-13 21:50:21 -0700555 [MF_MSG_DAX] = "dax page",
Xie XiuQicc637b12015-06-24 16:57:30 -0700556 [MF_MSG_UNKNOWN] = "unknown page",
Naoya Horiguchi64d37a22015-04-15 16:13:05 -0700557};
558
Andi Kleen6a460792009-09-16 11:50:15 +0200559/*
Wu Fengguangdc2a1cb2009-12-16 12:19:58 +0100560 * XXX: It is possible that a page is isolated from LRU cache,
561 * and then kept in swap cache or failed to remove from page cache.
562 * The page count will stop it from being freed by unpoison.
563 * Stress tests should be aware of this memory leak problem.
564 */
565static int delete_from_lru_cache(struct page *p)
566{
567 if (!isolate_lru_page(p)) {
568 /*
569 * Clear sensible page flags, so that the buddy system won't
570 * complain when the page is unpoison-and-freed.
571 */
572 ClearPageActive(p);
573 ClearPageUnevictable(p);
Michal Hocko18365222017-05-12 15:46:26 -0700574
575 /*
576 * Poisoned page might never drop its ref count to 0 so we have
577 * to uncharge it manually from its memcg.
578 */
579 mem_cgroup_uncharge(p);
580
Wu Fengguangdc2a1cb2009-12-16 12:19:58 +0100581 /*
582 * drop the page count elevated by isolate_lru_page()
583 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300584 put_page(p);
Wu Fengguangdc2a1cb2009-12-16 12:19:58 +0100585 return 0;
586 }
587 return -EIO;
588}
589
Naoya Horiguchi78bb9202017-07-10 15:47:50 -0700590static int truncate_error_page(struct page *p, unsigned long pfn,
591 struct address_space *mapping)
592{
593 int ret = MF_FAILED;
594
595 if (mapping->a_ops->error_remove_page) {
596 int err = mapping->a_ops->error_remove_page(mapping, p);
597
598 if (err != 0) {
599 pr_info("Memory failure: %#lx: Failed to punch page: %d\n",
600 pfn, err);
601 } else if (page_has_private(p) &&
602 !try_to_release_page(p, GFP_NOIO)) {
603 pr_info("Memory failure: %#lx: failed to release buffers\n",
604 pfn);
605 } else {
606 ret = MF_RECOVERED;
607 }
608 } else {
609 /*
610 * If the file system doesn't support it just invalidate
611 * This fails on dirty or anything with private pages
612 */
613 if (invalidate_inode_page(p))
614 ret = MF_RECOVERED;
615 else
616 pr_info("Memory failure: %#lx: Failed to invalidate\n",
617 pfn);
618 }
619
620 return ret;
621}
622
Wu Fengguangdc2a1cb2009-12-16 12:19:58 +0100623/*
Andi Kleen6a460792009-09-16 11:50:15 +0200624 * Error hit kernel page.
625 * Do nothing, try to be lucky and not touch this instead. For a few cases we
626 * could be more sophisticated.
627 */
628static int me_kernel(struct page *p, unsigned long pfn)
629{
Xie XiuQicc637b12015-06-24 16:57:30 -0700630 return MF_IGNORED;
Andi Kleen6a460792009-09-16 11:50:15 +0200631}
632
633/*
634 * Page in unknown state. Do nothing.
635 */
636static int me_unknown(struct page *p, unsigned long pfn)
637{
Chen Yucong495367c02016-05-20 16:57:32 -0700638 pr_err("Memory failure: %#lx: Unknown page state\n", pfn);
Xie XiuQicc637b12015-06-24 16:57:30 -0700639 return MF_FAILED;
Andi Kleen6a460792009-09-16 11:50:15 +0200640}
641
642/*
Andi Kleen6a460792009-09-16 11:50:15 +0200643 * Clean (or cleaned) page cache page.
644 */
645static int me_pagecache_clean(struct page *p, unsigned long pfn)
646{
Andi Kleen6a460792009-09-16 11:50:15 +0200647 struct address_space *mapping;
648
Wu Fengguangdc2a1cb2009-12-16 12:19:58 +0100649 delete_from_lru_cache(p);
650
Andi Kleen6a460792009-09-16 11:50:15 +0200651 /*
652 * For anonymous pages we're done the only reference left
653 * should be the one m_f() holds.
654 */
655 if (PageAnon(p))
Xie XiuQicc637b12015-06-24 16:57:30 -0700656 return MF_RECOVERED;
Andi Kleen6a460792009-09-16 11:50:15 +0200657
658 /*
659 * Now truncate the page in the page cache. This is really
660 * more like a "temporary hole punch"
661 * Don't do this for block devices when someone else
662 * has a reference, because it could be file system metadata
663 * and that's not safe to truncate.
664 */
665 mapping = page_mapping(p);
666 if (!mapping) {
667 /*
668 * Page has been teared down in the meanwhile
669 */
Xie XiuQicc637b12015-06-24 16:57:30 -0700670 return MF_FAILED;
Andi Kleen6a460792009-09-16 11:50:15 +0200671 }
672
673 /*
674 * Truncation is a bit tricky. Enable it per file system for now.
675 *
676 * Open: to take i_mutex or not for this? Right now we don't.
677 */
Naoya Horiguchi78bb9202017-07-10 15:47:50 -0700678 return truncate_error_page(p, pfn, mapping);
Andi Kleen6a460792009-09-16 11:50:15 +0200679}
680
681/*
Zhi Yong Wu549543d2014-01-21 15:49:08 -0800682 * Dirty pagecache page
Andi Kleen6a460792009-09-16 11:50:15 +0200683 * Issues: when the error hit a hole page the error is not properly
684 * propagated.
685 */
686static int me_pagecache_dirty(struct page *p, unsigned long pfn)
687{
688 struct address_space *mapping = page_mapping(p);
689
690 SetPageError(p);
691 /* TBD: print more information about the file. */
692 if (mapping) {
693 /*
694 * IO error will be reported by write(), fsync(), etc.
695 * who check the mapping.
696 * This way the application knows that something went
697 * wrong with its dirty file data.
698 *
699 * There's one open issue:
700 *
701 * The EIO will be only reported on the next IO
702 * operation and then cleared through the IO map.
703 * Normally Linux has two mechanisms to pass IO error
704 * first through the AS_EIO flag in the address space
705 * and then through the PageError flag in the page.
706 * Since we drop pages on memory failure handling the
707 * only mechanism open to use is through AS_AIO.
708 *
709 * This has the disadvantage that it gets cleared on
710 * the first operation that returns an error, while
711 * the PageError bit is more sticky and only cleared
712 * when the page is reread or dropped. If an
713 * application assumes it will always get error on
714 * fsync, but does other operations on the fd before
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300715 * and the page is dropped between then the error
Andi Kleen6a460792009-09-16 11:50:15 +0200716 * will not be properly reported.
717 *
718 * This can already happen even without hwpoisoned
719 * pages: first on metadata IO errors (which only
720 * report through AS_EIO) or when the page is dropped
721 * at the wrong time.
722 *
723 * So right now we assume that the application DTRT on
724 * the first EIO, but we're not worse than other parts
725 * of the kernel.
726 */
Jeff Laytonaf21bfa2017-07-06 07:02:19 -0400727 mapping_set_error(mapping, -EIO);
Andi Kleen6a460792009-09-16 11:50:15 +0200728 }
729
730 return me_pagecache_clean(p, pfn);
731}
732
733/*
734 * Clean and dirty swap cache.
735 *
736 * Dirty swap cache page is tricky to handle. The page could live both in page
737 * cache and swap cache(ie. page is freshly swapped in). So it could be
738 * referenced concurrently by 2 types of PTEs:
739 * normal PTEs and swap PTEs. We try to handle them consistently by calling
740 * try_to_unmap(TTU_IGNORE_HWPOISON) to convert the normal PTEs to swap PTEs,
741 * and then
742 * - clear dirty bit to prevent IO
743 * - remove from LRU
744 * - but keep in the swap cache, so that when we return to it on
745 * a later page fault, we know the application is accessing
746 * corrupted data and shall be killed (we installed simple
747 * interception code in do_swap_page to catch it).
748 *
749 * Clean swap cache pages can be directly isolated. A later page fault will
750 * bring in the known good data from disk.
751 */
752static int me_swapcache_dirty(struct page *p, unsigned long pfn)
753{
Andi Kleen6a460792009-09-16 11:50:15 +0200754 ClearPageDirty(p);
755 /* Trigger EIO in shmem: */
756 ClearPageUptodate(p);
757
Wu Fengguangdc2a1cb2009-12-16 12:19:58 +0100758 if (!delete_from_lru_cache(p))
Xie XiuQicc637b12015-06-24 16:57:30 -0700759 return MF_DELAYED;
Wu Fengguangdc2a1cb2009-12-16 12:19:58 +0100760 else
Xie XiuQicc637b12015-06-24 16:57:30 -0700761 return MF_FAILED;
Andi Kleen6a460792009-09-16 11:50:15 +0200762}
763
764static int me_swapcache_clean(struct page *p, unsigned long pfn)
765{
Andi Kleen6a460792009-09-16 11:50:15 +0200766 delete_from_swap_cache(p);
Wu Fengguange43c3af2009-09-29 13:16:20 +0800767
Wu Fengguangdc2a1cb2009-12-16 12:19:58 +0100768 if (!delete_from_lru_cache(p))
Xie XiuQicc637b12015-06-24 16:57:30 -0700769 return MF_RECOVERED;
Wu Fengguangdc2a1cb2009-12-16 12:19:58 +0100770 else
Xie XiuQicc637b12015-06-24 16:57:30 -0700771 return MF_FAILED;
Andi Kleen6a460792009-09-16 11:50:15 +0200772}
773
774/*
775 * Huge pages. Needs work.
776 * Issues:
Naoya Horiguchi93f70f92010-05-28 09:29:20 +0900777 * - Error on hugepage is contained in hugepage unit (not in raw page unit.)
778 * To narrow down kill region to one page, we need to break up pmd.
Andi Kleen6a460792009-09-16 11:50:15 +0200779 */
780static int me_huge_page(struct page *p, unsigned long pfn)
781{
Naoya Horiguchi6de2b1a2010-09-08 10:19:36 +0900782 int res = 0;
Naoya Horiguchi93f70f92010-05-28 09:29:20 +0900783 struct page *hpage = compound_head(p);
Naoya Horiguchi78bb9202017-07-10 15:47:50 -0700784 struct address_space *mapping;
Naoya Horiguchi2491ffe2015-06-24 16:56:53 -0700785
786 if (!PageHuge(hpage))
787 return MF_DELAYED;
788
Naoya Horiguchi78bb9202017-07-10 15:47:50 -0700789 mapping = page_mapping(hpage);
790 if (mapping) {
791 res = truncate_error_page(hpage, pfn, mapping);
792 } else {
793 unlock_page(hpage);
794 /*
795 * migration entry prevents later access on error anonymous
796 * hugepage, so we can free and dissolve it into buddy to
797 * save healthy subpages.
798 */
799 if (PageAnon(hpage))
800 put_page(hpage);
801 dissolve_free_huge_page(p);
802 res = MF_RECOVERED;
803 lock_page(hpage);
Naoya Horiguchi93f70f92010-05-28 09:29:20 +0900804 }
Naoya Horiguchi78bb9202017-07-10 15:47:50 -0700805
806 return res;
Andi Kleen6a460792009-09-16 11:50:15 +0200807}
808
809/*
810 * Various page states we can handle.
811 *
812 * A page state is defined by its current page->flags bits.
813 * The table matches them in order and calls the right handler.
814 *
815 * This is quite tricky because we can access page at any time
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300816 * in its live cycle, so all accesses have to be extremely careful.
Andi Kleen6a460792009-09-16 11:50:15 +0200817 *
818 * This is not complete. More states could be added.
819 * For any missing state don't attempt recovery.
820 */
821
822#define dirty (1UL << PG_dirty)
Nicholas Piggin6326fec2016-12-25 13:00:29 +1000823#define sc ((1UL << PG_swapcache) | (1UL << PG_swapbacked))
Andi Kleen6a460792009-09-16 11:50:15 +0200824#define unevict (1UL << PG_unevictable)
825#define mlock (1UL << PG_mlocked)
826#define writeback (1UL << PG_writeback)
827#define lru (1UL << PG_lru)
Andi Kleen6a460792009-09-16 11:50:15 +0200828#define head (1UL << PG_head)
Andi Kleen6a460792009-09-16 11:50:15 +0200829#define slab (1UL << PG_slab)
Andi Kleen6a460792009-09-16 11:50:15 +0200830#define reserved (1UL << PG_reserved)
831
832static struct page_state {
833 unsigned long mask;
834 unsigned long res;
Xie XiuQicc637b12015-06-24 16:57:30 -0700835 enum mf_action_page_type type;
Andi Kleen6a460792009-09-16 11:50:15 +0200836 int (*action)(struct page *p, unsigned long pfn);
837} error_states[] = {
Xie XiuQicc637b12015-06-24 16:57:30 -0700838 { reserved, reserved, MF_MSG_KERNEL, me_kernel },
Wu Fengguang95d01fc2009-12-16 12:19:58 +0100839 /*
840 * free pages are specially detected outside this table:
841 * PG_buddy pages only make a small fraction of all free pages.
842 */
Andi Kleen6a460792009-09-16 11:50:15 +0200843
844 /*
845 * Could in theory check if slab page is free or if we can drop
846 * currently unused objects without touching them. But just
847 * treat it as standard kernel for now.
848 */
Xie XiuQicc637b12015-06-24 16:57:30 -0700849 { slab, slab, MF_MSG_SLAB, me_kernel },
Andi Kleen6a460792009-09-16 11:50:15 +0200850
Xie XiuQicc637b12015-06-24 16:57:30 -0700851 { head, head, MF_MSG_HUGE, me_huge_page },
Andi Kleen6a460792009-09-16 11:50:15 +0200852
Xie XiuQicc637b12015-06-24 16:57:30 -0700853 { sc|dirty, sc|dirty, MF_MSG_DIRTY_SWAPCACHE, me_swapcache_dirty },
854 { sc|dirty, sc, MF_MSG_CLEAN_SWAPCACHE, me_swapcache_clean },
Andi Kleen6a460792009-09-16 11:50:15 +0200855
Xie XiuQicc637b12015-06-24 16:57:30 -0700856 { mlock|dirty, mlock|dirty, MF_MSG_DIRTY_MLOCKED_LRU, me_pagecache_dirty },
857 { mlock|dirty, mlock, MF_MSG_CLEAN_MLOCKED_LRU, me_pagecache_clean },
Andi Kleen6a460792009-09-16 11:50:15 +0200858
Xie XiuQicc637b12015-06-24 16:57:30 -0700859 { unevict|dirty, unevict|dirty, MF_MSG_DIRTY_UNEVICTABLE_LRU, me_pagecache_dirty },
860 { unevict|dirty, unevict, MF_MSG_CLEAN_UNEVICTABLE_LRU, me_pagecache_clean },
Naoya Horiguchi5f4b9fc2013-02-22 16:35:53 -0800861
Xie XiuQicc637b12015-06-24 16:57:30 -0700862 { lru|dirty, lru|dirty, MF_MSG_DIRTY_LRU, me_pagecache_dirty },
863 { lru|dirty, lru, MF_MSG_CLEAN_LRU, me_pagecache_clean },
Andi Kleen6a460792009-09-16 11:50:15 +0200864
865 /*
866 * Catchall entry: must be at end.
867 */
Xie XiuQicc637b12015-06-24 16:57:30 -0700868 { 0, 0, MF_MSG_UNKNOWN, me_unknown },
Andi Kleen6a460792009-09-16 11:50:15 +0200869};
870
Andi Kleen2326c462009-12-16 12:20:00 +0100871#undef dirty
872#undef sc
873#undef unevict
874#undef mlock
875#undef writeback
876#undef lru
Andi Kleen2326c462009-12-16 12:20:00 +0100877#undef head
Andi Kleen2326c462009-12-16 12:20:00 +0100878#undef slab
879#undef reserved
880
Naoya Horiguchiff604cf2012-12-11 16:01:32 -0800881/*
882 * "Dirty/Clean" indication is not 100% accurate due to the possibility of
883 * setting PG_dirty outside page lock. See also comment above set_page_dirty().
884 */
Xie XiuQicc3e2af2015-06-24 16:57:33 -0700885static void action_result(unsigned long pfn, enum mf_action_page_type type,
886 enum mf_result result)
Andi Kleen6a460792009-09-16 11:50:15 +0200887{
Xie XiuQi97f0b132015-06-24 16:57:36 -0700888 trace_memory_failure_event(pfn, type, result);
889
Chen Yucong495367c02016-05-20 16:57:32 -0700890 pr_err("Memory failure: %#lx: recovery action for %s: %s\n",
Naoya Horiguchi64d37a22015-04-15 16:13:05 -0700891 pfn, action_page_types[type], action_name[result]);
Andi Kleen6a460792009-09-16 11:50:15 +0200892}
893
894static int page_action(struct page_state *ps, struct page *p,
Wu Fengguangbd1ce5f2009-12-16 12:19:57 +0100895 unsigned long pfn)
Andi Kleen6a460792009-09-16 11:50:15 +0200896{
897 int result;
Wu Fengguang7456b042009-10-19 08:15:01 +0200898 int count;
Andi Kleen6a460792009-09-16 11:50:15 +0200899
900 result = ps->action(p, pfn);
Wu Fengguang7456b042009-10-19 08:15:01 +0200901
Wu Fengguangbd1ce5f2009-12-16 12:19:57 +0100902 count = page_count(p) - 1;
Xie XiuQicc637b12015-06-24 16:57:30 -0700903 if (ps->action == me_swapcache_dirty && result == MF_DELAYED)
Wu Fengguang138ce282009-12-16 12:19:58 +0100904 count--;
Naoya Horiguchi78bb9202017-07-10 15:47:50 -0700905 if (count > 0) {
Chen Yucong495367c02016-05-20 16:57:32 -0700906 pr_err("Memory failure: %#lx: %s still referenced by %d users\n",
Naoya Horiguchi64d37a22015-04-15 16:13:05 -0700907 pfn, action_page_types[ps->type], count);
Xie XiuQicc637b12015-06-24 16:57:30 -0700908 result = MF_FAILED;
Wu Fengguang138ce282009-12-16 12:19:58 +0100909 }
Naoya Horiguchi64d37a22015-04-15 16:13:05 -0700910 action_result(pfn, ps->type, result);
Andi Kleen6a460792009-09-16 11:50:15 +0200911
912 /* Could do more checks here if page looks ok */
913 /*
914 * Could adjust zone counters here to correct for the missing page.
915 */
916
Xie XiuQicc637b12015-06-24 16:57:30 -0700917 return (result == MF_RECOVERED || result == MF_DELAYED) ? 0 : -EBUSY;
Andi Kleen6a460792009-09-16 11:50:15 +0200918}
919
Naoya Horiguchiead07f62015-06-24 16:56:48 -0700920/**
921 * get_hwpoison_page() - Get refcount for memory error handling:
922 * @page: raw error page (hit by memory error)
923 *
924 * Return: return 0 if failed to grab the refcount, otherwise true (some
925 * non-zero value.)
926 */
927int get_hwpoison_page(struct page *page)
928{
929 struct page *head = compound_head(page);
930
Naoya Horiguchi4e41a302016-01-15 16:54:07 -0800931 if (!PageHuge(head) && PageTransHuge(head)) {
Naoya Horiguchi98ed2b02015-08-06 15:47:04 -0700932 /*
933 * Non anonymous thp exists only in allocation/free time. We
934 * can't handle such a case correctly, so let's give it up.
935 * This should be better than triggering BUG_ON when kernel
936 * tries to touch the "partially handled" page.
937 */
938 if (!PageAnon(head)) {
Chen Yucong495367c02016-05-20 16:57:32 -0700939 pr_err("Memory failure: %#lx: non anonymous thp\n",
Naoya Horiguchi98ed2b02015-08-06 15:47:04 -0700940 page_to_pfn(page));
941 return 0;
942 }
Naoya Horiguchiead07f62015-06-24 16:56:48 -0700943 }
944
Konstantin Khlebnikovc2e7e002016-04-28 16:19:03 -0700945 if (get_page_unless_zero(head)) {
946 if (head == compound_head(page))
947 return 1;
948
Chen Yucong495367c02016-05-20 16:57:32 -0700949 pr_info("Memory failure: %#lx cannot catch tail\n",
950 page_to_pfn(page));
Konstantin Khlebnikovc2e7e002016-04-28 16:19:03 -0700951 put_page(head);
952 }
953
954 return 0;
Naoya Horiguchiead07f62015-06-24 16:56:48 -0700955}
956EXPORT_SYMBOL_GPL(get_hwpoison_page);
957
Andi Kleen6a460792009-09-16 11:50:15 +0200958/*
959 * Do all that is necessary to remove user space mappings. Unmap
960 * the pages and send SIGBUS to the processes if the data was dirty.
961 */
Minchan Kim666e5a42017-05-03 14:54:20 -0700962static bool hwpoison_user_mappings(struct page *p, unsigned long pfn,
Eric W. Biederman83b57532017-07-09 18:14:01 -0500963 int flags, struct page **hpagep)
Andi Kleen6a460792009-09-16 11:50:15 +0200964{
Shaohua Lia128ca72017-05-03 14:52:22 -0700965 enum ttu_flags ttu = TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
Andi Kleen6a460792009-09-16 11:50:15 +0200966 struct address_space *mapping;
967 LIST_HEAD(tokill);
Minchan Kim666e5a42017-05-03 14:54:20 -0700968 bool unmap_success;
Tony Luck6751ed62012-07-11 10:20:47 -0700969 int kill = 1, forcekill;
Naoya Horiguchi54b9dd12014-01-23 15:53:14 -0800970 struct page *hpage = *hpagep;
Naoya Horiguchi286c4692017-05-03 14:56:22 -0700971 bool mlocked = PageMlocked(hpage);
Andi Kleen6a460792009-09-16 11:50:15 +0200972
Naoya Horiguchi93a9eb32014-07-30 16:08:28 -0700973 /*
974 * Here we are interested only in user-mapped pages, so skip any
975 * other types of pages.
976 */
977 if (PageReserved(p) || PageSlab(p))
Minchan Kim666e5a42017-05-03 14:54:20 -0700978 return true;
Naoya Horiguchi93a9eb32014-07-30 16:08:28 -0700979 if (!(PageLRU(hpage) || PageHuge(p)))
Minchan Kim666e5a42017-05-03 14:54:20 -0700980 return true;
Andi Kleen6a460792009-09-16 11:50:15 +0200981
Andi Kleen6a460792009-09-16 11:50:15 +0200982 /*
983 * This check implies we don't kill processes if their pages
984 * are in the swap cache early. Those are always late kills.
985 */
Naoya Horiguchi7af446a2010-05-28 09:29:17 +0900986 if (!page_mapped(hpage))
Minchan Kim666e5a42017-05-03 14:54:20 -0700987 return true;
Wu Fengguang1668bfd2009-12-16 12:19:58 +0100988
Naoya Horiguchi52089b12014-07-30 16:08:30 -0700989 if (PageKsm(p)) {
Chen Yucong495367c02016-05-20 16:57:32 -0700990 pr_err("Memory failure: %#lx: can't handle KSM pages.\n", pfn);
Minchan Kim666e5a42017-05-03 14:54:20 -0700991 return false;
Naoya Horiguchi52089b12014-07-30 16:08:30 -0700992 }
Andi Kleen6a460792009-09-16 11:50:15 +0200993
994 if (PageSwapCache(p)) {
Chen Yucong495367c02016-05-20 16:57:32 -0700995 pr_err("Memory failure: %#lx: keeping poisoned page in swap cache\n",
996 pfn);
Andi Kleen6a460792009-09-16 11:50:15 +0200997 ttu |= TTU_IGNORE_HWPOISON;
998 }
999
1000 /*
1001 * Propagate the dirty bit from PTEs to struct page first, because we
1002 * need this to decide if we should kill or just drop the page.
Wu Fengguangdb0480b2009-12-16 12:19:58 +01001003 * XXX: the dirty test could be racy: set_page_dirty() may not always
1004 * be called inside page lock (it's recommended but not enforced).
Andi Kleen6a460792009-09-16 11:50:15 +02001005 */
Naoya Horiguchi7af446a2010-05-28 09:29:17 +09001006 mapping = page_mapping(hpage);
Tony Luck6751ed62012-07-11 10:20:47 -07001007 if (!(flags & MF_MUST_KILL) && !PageDirty(hpage) && mapping &&
Naoya Horiguchi7af446a2010-05-28 09:29:17 +09001008 mapping_cap_writeback_dirty(mapping)) {
1009 if (page_mkclean(hpage)) {
1010 SetPageDirty(hpage);
Andi Kleen6a460792009-09-16 11:50:15 +02001011 } else {
1012 kill = 0;
1013 ttu |= TTU_IGNORE_HWPOISON;
Chen Yucong495367c02016-05-20 16:57:32 -07001014 pr_info("Memory failure: %#lx: corrupted page was clean: dropped without side effects\n",
Andi Kleen6a460792009-09-16 11:50:15 +02001015 pfn);
1016 }
1017 }
1018
Jin Dongminga6d30dd2011-02-01 15:52:40 -08001019 /*
Andi Kleen6a460792009-09-16 11:50:15 +02001020 * First collect all the processes that have the page
1021 * mapped in dirty form. This has to be done before try_to_unmap,
1022 * because ttu takes the rmap data structures down.
1023 *
1024 * Error handling: We ignore errors here because
1025 * there's nothing that can be done.
1026 */
1027 if (kill)
Naoya Horiguchi415c64c2015-06-24 16:56:45 -07001028 collect_procs(hpage, &tokill, flags & MF_ACTION_REQUIRED);
Andi Kleen6a460792009-09-16 11:50:15 +02001029
Minchan Kim666e5a42017-05-03 14:54:20 -07001030 unmap_success = try_to_unmap(hpage, ttu);
1031 if (!unmap_success)
Chen Yucong495367c02016-05-20 16:57:32 -07001032 pr_err("Memory failure: %#lx: failed to unmap page (mapcount=%d)\n",
Joe Perches11705322016-03-17 14:19:50 -07001033 pfn, page_mapcount(hpage));
Jin Dongminga6d30dd2011-02-01 15:52:40 -08001034
Andi Kleen6a460792009-09-16 11:50:15 +02001035 /*
Naoya Horiguchi286c4692017-05-03 14:56:22 -07001036 * try_to_unmap() might put mlocked page in lru cache, so call
1037 * shake_page() again to ensure that it's flushed.
1038 */
1039 if (mlocked)
1040 shake_page(hpage, 0);
1041
1042 /*
Andi Kleen6a460792009-09-16 11:50:15 +02001043 * Now that the dirty bit has been propagated to the
1044 * struct page and all unmaps done we can decide if
1045 * killing is needed or not. Only kill when the page
Tony Luck6751ed62012-07-11 10:20:47 -07001046 * was dirty or the process is not restartable,
1047 * otherwise the tokill list is merely
Andi Kleen6a460792009-09-16 11:50:15 +02001048 * freed. When there was a problem unmapping earlier
1049 * use a more force-full uncatchable kill to prevent
1050 * any accesses to the poisoned memory.
1051 */
Naoya Horiguchi415c64c2015-06-24 16:56:45 -07001052 forcekill = PageDirty(hpage) || (flags & MF_MUST_KILL);
Dan Williamsae1139e2018-07-13 21:50:11 -07001053 kill_procs(&tokill, forcekill, !unmap_success, pfn, flags);
Wu Fengguang1668bfd2009-12-16 12:19:58 +01001054
Minchan Kim666e5a42017-05-03 14:54:20 -07001055 return unmap_success;
Andi Kleen6a460792009-09-16 11:50:15 +02001056}
1057
Naoya Horiguchi0348d2e2017-07-10 15:47:56 -07001058static int identify_page_state(unsigned long pfn, struct page *p,
1059 unsigned long page_flags)
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001060{
1061 struct page_state *ps;
Naoya Horiguchi0348d2e2017-07-10 15:47:56 -07001062
1063 /*
1064 * The first check uses the current page flags which may not have any
1065 * relevant information. The second check with the saved page flags is
1066 * carried out only if the first check can't determine the page status.
1067 */
1068 for (ps = error_states;; ps++)
1069 if ((p->flags & ps->mask) == ps->res)
1070 break;
1071
1072 page_flags |= (p->flags & (1UL << PG_dirty));
1073
1074 if (!ps->mask)
1075 for (ps = error_states;; ps++)
1076 if ((page_flags & ps->mask) == ps->res)
1077 break;
1078 return page_action(ps, p, pfn);
1079}
1080
Eric W. Biederman83b57532017-07-09 18:14:01 -05001081static int memory_failure_hugetlb(unsigned long pfn, int flags)
Naoya Horiguchi0348d2e2017-07-10 15:47:56 -07001082{
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001083 struct page *p = pfn_to_page(pfn);
1084 struct page *head = compound_head(p);
1085 int res;
1086 unsigned long page_flags;
1087
1088 if (TestSetPageHWPoison(head)) {
1089 pr_err("Memory failure: %#lx: already hardware poisoned\n",
1090 pfn);
1091 return 0;
1092 }
1093
1094 num_poisoned_pages_inc();
1095
1096 if (!(flags & MF_COUNT_INCREASED) && !get_hwpoison_page(p)) {
1097 /*
1098 * Check "filter hit" and "race with other subpage."
1099 */
1100 lock_page(head);
1101 if (PageHWPoison(head)) {
1102 if ((hwpoison_filter(p) && TestClearPageHWPoison(p))
1103 || (p != head && TestSetPageHWPoison(head))) {
1104 num_poisoned_pages_dec();
1105 unlock_page(head);
1106 return 0;
1107 }
1108 }
1109 unlock_page(head);
1110 dissolve_free_huge_page(p);
1111 action_result(pfn, MF_MSG_FREE_HUGE, MF_DELAYED);
1112 return 0;
1113 }
1114
1115 lock_page(head);
1116 page_flags = head->flags;
1117
1118 if (!PageHWPoison(head)) {
1119 pr_err("Memory failure: %#lx: just unpoisoned\n", pfn);
1120 num_poisoned_pages_dec();
1121 unlock_page(head);
1122 put_hwpoison_page(head);
1123 return 0;
1124 }
1125
Naoya Horiguchi31286a82018-04-05 16:23:05 -07001126 /*
1127 * TODO: hwpoison for pud-sized hugetlb doesn't work right now, so
1128 * simply disable it. In order to make it work properly, we need
1129 * make sure that:
1130 * - conversion of a pud that maps an error hugetlb into hwpoison
1131 * entry properly works, and
1132 * - other mm code walking over page table is aware of pud-aligned
1133 * hwpoison entries.
1134 */
1135 if (huge_page_size(page_hstate(head)) > PMD_SIZE) {
1136 action_result(pfn, MF_MSG_NON_PMD_HUGE, MF_IGNORED);
1137 res = -EBUSY;
1138 goto out;
1139 }
1140
Eric W. Biederman83b57532017-07-09 18:14:01 -05001141 if (!hwpoison_user_mappings(p, pfn, flags, &head)) {
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001142 action_result(pfn, MF_MSG_UNMAP_FAILED, MF_IGNORED);
1143 res = -EBUSY;
1144 goto out;
1145 }
1146
Naoya Horiguchi0348d2e2017-07-10 15:47:56 -07001147 res = identify_page_state(pfn, p, page_flags);
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001148out:
1149 unlock_page(head);
1150 return res;
1151}
1152
Dan Williams6100e342018-07-13 21:50:21 -07001153static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
1154 struct dev_pagemap *pgmap)
1155{
1156 struct page *page = pfn_to_page(pfn);
1157 const bool unmap_success = true;
1158 unsigned long size = 0;
1159 struct to_kill *tk;
1160 LIST_HEAD(tokill);
1161 int rc = -EBUSY;
1162 loff_t start;
1163
1164 /*
1165 * Prevent the inode from being freed while we are interrogating
1166 * the address_space, typically this would be handled by
1167 * lock_page(), but dax pages do not use the page lock. This
1168 * also prevents changes to the mapping of this pfn until
1169 * poison signaling is complete.
1170 */
1171 if (!dax_lock_mapping_entry(page))
1172 goto out;
1173
1174 if (hwpoison_filter(page)) {
1175 rc = 0;
1176 goto unlock;
1177 }
1178
1179 switch (pgmap->type) {
1180 case MEMORY_DEVICE_PRIVATE:
1181 case MEMORY_DEVICE_PUBLIC:
1182 /*
1183 * TODO: Handle HMM pages which may need coordination
1184 * with device-side memory.
1185 */
1186 goto unlock;
1187 default:
1188 break;
1189 }
1190
1191 /*
1192 * Use this flag as an indication that the dax page has been
1193 * remapped UC to prevent speculative consumption of poison.
1194 */
1195 SetPageHWPoison(page);
1196
1197 /*
1198 * Unlike System-RAM there is no possibility to swap in a
1199 * different physical page at a given virtual address, so all
1200 * userspace consumption of ZONE_DEVICE memory necessitates
1201 * SIGBUS (i.e. MF_MUST_KILL)
1202 */
1203 flags |= MF_ACTION_REQUIRED | MF_MUST_KILL;
1204 collect_procs(page, &tokill, flags & MF_ACTION_REQUIRED);
1205
1206 list_for_each_entry(tk, &tokill, nd)
1207 if (tk->size_shift)
1208 size = max(size, 1UL << tk->size_shift);
1209 if (size) {
1210 /*
1211 * Unmap the largest mapping to avoid breaking up
1212 * device-dax mappings which are constant size. The
1213 * actual size of the mapping being torn down is
1214 * communicated in siginfo, see kill_proc()
1215 */
1216 start = (page->index << PAGE_SHIFT) & ~(size - 1);
1217 unmap_mapping_range(page->mapping, start, start + size, 0);
1218 }
1219 kill_procs(&tokill, flags & MF_MUST_KILL, !unmap_success, pfn, flags);
1220 rc = 0;
1221unlock:
1222 dax_unlock_mapping_entry(page);
1223out:
1224 /* drop pgmap ref acquired in caller */
1225 put_dev_pagemap(pgmap);
1226 action_result(pfn, MF_MSG_DAX, rc ? MF_FAILED : MF_RECOVERED);
1227 return rc;
1228}
1229
Tony Luckcd42f4a2011-12-15 10:48:12 -08001230/**
1231 * memory_failure - Handle memory failure of a page.
1232 * @pfn: Page Number of the corrupted page
Tony Luckcd42f4a2011-12-15 10:48:12 -08001233 * @flags: fine tune action taken
1234 *
1235 * This function is called by the low level machine check code
1236 * of an architecture when it detects hardware memory corruption
1237 * of a page. It tries its best to recover, which includes
1238 * dropping pages, killing processes etc.
1239 *
1240 * The function is primarily of use for corruptions that
1241 * happen outside the current execution context (e.g. when
1242 * detected by a background scrubber)
1243 *
1244 * Must run in process context (e.g. a work queue) with interrupts
1245 * enabled and no spinlocks hold.
1246 */
Eric W. Biederman83b57532017-07-09 18:14:01 -05001247int memory_failure(unsigned long pfn, int flags)
Andi Kleen6a460792009-09-16 11:50:15 +02001248{
Andi Kleen6a460792009-09-16 11:50:15 +02001249 struct page *p;
Naoya Horiguchi7af446a2010-05-28 09:29:17 +09001250 struct page *hpage;
Naoya Horiguchi415c64c2015-06-24 16:56:45 -07001251 struct page *orig_head;
Dan Williams6100e342018-07-13 21:50:21 -07001252 struct dev_pagemap *pgmap;
Andi Kleen6a460792009-09-16 11:50:15 +02001253 int res;
Naoya Horiguchi524fca12013-02-22 16:35:51 -08001254 unsigned long page_flags;
Andi Kleen6a460792009-09-16 11:50:15 +02001255
1256 if (!sysctl_memory_failure_recovery)
Eric W. Biederman83b57532017-07-09 18:14:01 -05001257 panic("Memory failure on page %lx", pfn);
Andi Kleen6a460792009-09-16 11:50:15 +02001258
1259 if (!pfn_valid(pfn)) {
Chen Yucong495367c02016-05-20 16:57:32 -07001260 pr_err("Memory failure: %#lx: memory outside kernel control\n",
1261 pfn);
Wu Fengguanga7560fc2009-12-16 12:19:57 +01001262 return -ENXIO;
Andi Kleen6a460792009-09-16 11:50:15 +02001263 }
1264
Dan Williams6100e342018-07-13 21:50:21 -07001265 pgmap = get_dev_pagemap(pfn, NULL);
1266 if (pgmap)
1267 return memory_failure_dev_pagemap(pfn, flags, pgmap);
1268
Andi Kleen6a460792009-09-16 11:50:15 +02001269 p = pfn_to_page(pfn);
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001270 if (PageHuge(p))
Eric W. Biederman83b57532017-07-09 18:14:01 -05001271 return memory_failure_hugetlb(pfn, flags);
Andi Kleen6a460792009-09-16 11:50:15 +02001272 if (TestSetPageHWPoison(p)) {
Chen Yucong495367c02016-05-20 16:57:32 -07001273 pr_err("Memory failure: %#lx: already hardware poisoned\n",
1274 pfn);
Andi Kleen6a460792009-09-16 11:50:15 +02001275 return 0;
1276 }
1277
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001278 orig_head = hpage = compound_head(p);
Naoya Horiguchib37ff712017-07-10 15:47:38 -07001279 num_poisoned_pages_inc();
Andi Kleen6a460792009-09-16 11:50:15 +02001280
1281 /*
1282 * We need/can do nothing about count=0 pages.
1283 * 1) it's a free page, and therefore in safe hand:
1284 * prep_new_page() will be the gate keeper.
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001285 * 2) it's part of a non-compound high order page.
Andi Kleen6a460792009-09-16 11:50:15 +02001286 * Implies some kernel user: cannot stop them from
1287 * R/W the page; let's pray that the page has been
1288 * used and will be freed some time later.
1289 * In fact it's dangerous to directly bump up page count from 0,
1290 * that may make page_freeze_refs()/page_unfreeze_refs() mismatch.
1291 */
Naoya Horiguchiead07f62015-06-24 16:56:48 -07001292 if (!(flags & MF_COUNT_INCREASED) && !get_hwpoison_page(p)) {
Wu Fengguang8d22ba12009-12-16 12:19:58 +01001293 if (is_free_buddy_page(p)) {
Xie XiuQicc637b12015-06-24 16:57:30 -07001294 action_result(pfn, MF_MSG_BUDDY, MF_DELAYED);
Wu Fengguang8d22ba12009-12-16 12:19:58 +01001295 return 0;
1296 } else {
Xie XiuQicc637b12015-06-24 16:57:30 -07001297 action_result(pfn, MF_MSG_KERNEL_HIGH_ORDER, MF_IGNORED);
Wu Fengguang8d22ba12009-12-16 12:19:58 +01001298 return -EBUSY;
1299 }
Andi Kleen6a460792009-09-16 11:50:15 +02001300 }
1301
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001302 if (PageTransHuge(hpage)) {
Naoya Horiguchic3901e72016-11-10 10:46:23 -08001303 lock_page(p);
1304 if (!PageAnon(p) || unlikely(split_huge_page(p))) {
1305 unlock_page(p);
1306 if (!PageAnon(p))
Chen Yucong495367c02016-05-20 16:57:32 -07001307 pr_err("Memory failure: %#lx: non anonymous thp\n",
1308 pfn);
Wanpeng Li7f6bf392015-08-14 15:35:08 -07001309 else
Chen Yucong495367c02016-05-20 16:57:32 -07001310 pr_err("Memory failure: %#lx: thp split failed\n",
1311 pfn);
Naoya Horiguchiead07f62015-06-24 16:56:48 -07001312 if (TestClearPageHWPoison(p))
Naoya Horiguchib37ff712017-07-10 15:47:38 -07001313 num_poisoned_pages_dec();
Wanpeng Li665d9da2015-09-08 15:03:21 -07001314 put_hwpoison_page(p);
Naoya Horiguchi415c64c2015-06-24 16:56:45 -07001315 return -EBUSY;
1316 }
Naoya Horiguchic3901e72016-11-10 10:46:23 -08001317 unlock_page(p);
Naoya Horiguchi415c64c2015-06-24 16:56:45 -07001318 VM_BUG_ON_PAGE(!page_count(p), p);
1319 hpage = compound_head(p);
1320 }
1321
Andi Kleen6a460792009-09-16 11:50:15 +02001322 /*
Wu Fengguange43c3af2009-09-29 13:16:20 +08001323 * We ignore non-LRU pages for good reasons.
1324 * - PG_locked is only well defined for LRU pages and a few others
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001325 * - to avoid races with __SetPageLocked()
Wu Fengguange43c3af2009-09-29 13:16:20 +08001326 * - to avoid races with __SetPageSlab*() (and more non-atomic ops)
1327 * The check (unnecessarily) ignores LRU pages being isolated and
1328 * walked by the page reclaim code, however that's not a big loss.
1329 */
Naoya Horiguchi8bcb74d2017-05-03 14:56:19 -07001330 shake_page(p, 0);
1331 /* shake_page could have turned it free. */
1332 if (!PageLRU(p) && is_free_buddy_page(p)) {
1333 if (flags & MF_COUNT_INCREASED)
1334 action_result(pfn, MF_MSG_BUDDY, MF_DELAYED);
1335 else
1336 action_result(pfn, MF_MSG_BUDDY_2ND, MF_DELAYED);
1337 return 0;
Wu Fengguange43c3af2009-09-29 13:16:20 +08001338 }
Wu Fengguange43c3af2009-09-29 13:16:20 +08001339
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001340 lock_page(p);
Wu Fengguang847ce402009-12-16 12:19:58 +01001341
1342 /*
Andi Kleenf37d4292014-08-06 16:06:49 -07001343 * The page could have changed compound pages during the locking.
1344 * If this happens just bail out.
1345 */
Naoya Horiguchi415c64c2015-06-24 16:56:45 -07001346 if (PageCompound(p) && compound_head(p) != orig_head) {
Xie XiuQicc637b12015-06-24 16:57:30 -07001347 action_result(pfn, MF_MSG_DIFFERENT_COMPOUND, MF_IGNORED);
Andi Kleenf37d4292014-08-06 16:06:49 -07001348 res = -EBUSY;
1349 goto out;
1350 }
1351
1352 /*
Naoya Horiguchi524fca12013-02-22 16:35:51 -08001353 * We use page flags to determine what action should be taken, but
1354 * the flags can be modified by the error containment action. One
1355 * example is an mlocked page, where PG_mlocked is cleared by
1356 * page_remove_rmap() in try_to_unmap_one(). So to determine page status
1357 * correctly, we save a copy of the page flags at this time.
1358 */
James Morse7258ae52017-06-16 14:02:29 -07001359 if (PageHuge(p))
1360 page_flags = hpage->flags;
1361 else
1362 page_flags = p->flags;
Naoya Horiguchi524fca12013-02-22 16:35:51 -08001363
1364 /*
Wu Fengguang847ce402009-12-16 12:19:58 +01001365 * unpoison always clear PG_hwpoison inside page lock
1366 */
1367 if (!PageHWPoison(p)) {
Chen Yucong495367c02016-05-20 16:57:32 -07001368 pr_err("Memory failure: %#lx: just unpoisoned\n", pfn);
Naoya Horiguchib37ff712017-07-10 15:47:38 -07001369 num_poisoned_pages_dec();
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001370 unlock_page(p);
1371 put_hwpoison_page(p);
Naoya Horiguchia09233f2015-08-06 15:46:58 -07001372 return 0;
Wu Fengguang847ce402009-12-16 12:19:58 +01001373 }
Wu Fengguang7c116f22009-12-16 12:19:59 +01001374 if (hwpoison_filter(p)) {
1375 if (TestClearPageHWPoison(p))
Naoya Horiguchib37ff712017-07-10 15:47:38 -07001376 num_poisoned_pages_dec();
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001377 unlock_page(p);
1378 put_hwpoison_page(p);
Wu Fengguang7c116f22009-12-16 12:19:59 +01001379 return 0;
1380 }
Wu Fengguang847ce402009-12-16 12:19:58 +01001381
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001382 if (!PageTransTail(p) && !PageLRU(p))
Chen Yucong0bc1f8b2014-07-02 15:22:37 -07001383 goto identify_page_state;
1384
Naoya Horiguchi7013feb2010-05-28 09:29:18 +09001385 /*
Naoya Horiguchi6edd6cc2014-06-04 16:10:35 -07001386 * It's very difficult to mess with pages currently under IO
1387 * and in many cases impossible, so we just avoid it here.
1388 */
Andi Kleen6a460792009-09-16 11:50:15 +02001389 wait_on_page_writeback(p);
1390
1391 /*
1392 * Now take care of user space mappings.
Minchan Kime64a7822011-03-22 16:32:44 -07001393 * Abort on fail: __delete_from_page_cache() assumes unmapped page.
Naoya Horiguchi54b9dd12014-01-23 15:53:14 -08001394 *
1395 * When the raw error page is thp tail page, hpage points to the raw
1396 * page after thp split.
Andi Kleen6a460792009-09-16 11:50:15 +02001397 */
Eric W. Biederman83b57532017-07-09 18:14:01 -05001398 if (!hwpoison_user_mappings(p, pfn, flags, &hpage)) {
Xie XiuQicc637b12015-06-24 16:57:30 -07001399 action_result(pfn, MF_MSG_UNMAP_FAILED, MF_IGNORED);
Wu Fengguang1668bfd2009-12-16 12:19:58 +01001400 res = -EBUSY;
1401 goto out;
1402 }
Andi Kleen6a460792009-09-16 11:50:15 +02001403
1404 /*
1405 * Torn down by someone else?
1406 */
Wu Fengguangdc2a1cb2009-12-16 12:19:58 +01001407 if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
Xie XiuQicc637b12015-06-24 16:57:30 -07001408 action_result(pfn, MF_MSG_TRUNCATED_LRU, MF_IGNORED);
Wu Fengguangd95ea512009-12-16 12:19:58 +01001409 res = -EBUSY;
Andi Kleen6a460792009-09-16 11:50:15 +02001410 goto out;
1411 }
1412
Chen Yucong0bc1f8b2014-07-02 15:22:37 -07001413identify_page_state:
Naoya Horiguchi0348d2e2017-07-10 15:47:56 -07001414 res = identify_page_state(pfn, p, page_flags);
Andi Kleen6a460792009-09-16 11:50:15 +02001415out:
Naoya Horiguchi761ad8d2017-07-10 15:47:47 -07001416 unlock_page(p);
Andi Kleen6a460792009-09-16 11:50:15 +02001417 return res;
1418}
Tony Luckcd42f4a2011-12-15 10:48:12 -08001419EXPORT_SYMBOL_GPL(memory_failure);
Wu Fengguang847ce402009-12-16 12:19:58 +01001420
Huang Yingea8f5fb2011-07-13 13:14:27 +08001421#define MEMORY_FAILURE_FIFO_ORDER 4
1422#define MEMORY_FAILURE_FIFO_SIZE (1 << MEMORY_FAILURE_FIFO_ORDER)
1423
1424struct memory_failure_entry {
1425 unsigned long pfn;
Huang Yingea8f5fb2011-07-13 13:14:27 +08001426 int flags;
1427};
1428
1429struct memory_failure_cpu {
1430 DECLARE_KFIFO(fifo, struct memory_failure_entry,
1431 MEMORY_FAILURE_FIFO_SIZE);
1432 spinlock_t lock;
1433 struct work_struct work;
1434};
1435
1436static DEFINE_PER_CPU(struct memory_failure_cpu, memory_failure_cpu);
1437
1438/**
1439 * memory_failure_queue - Schedule handling memory failure of a page.
1440 * @pfn: Page Number of the corrupted page
Huang Yingea8f5fb2011-07-13 13:14:27 +08001441 * @flags: Flags for memory failure handling
1442 *
1443 * This function is called by the low level hardware error handler
1444 * when it detects hardware memory corruption of a page. It schedules
1445 * the recovering of error page, including dropping pages, killing
1446 * processes etc.
1447 *
1448 * The function is primarily of use for corruptions that
1449 * happen outside the current execution context (e.g. when
1450 * detected by a background scrubber)
1451 *
1452 * Can run in IRQ context.
1453 */
Eric W. Biederman83b57532017-07-09 18:14:01 -05001454void memory_failure_queue(unsigned long pfn, int flags)
Huang Yingea8f5fb2011-07-13 13:14:27 +08001455{
1456 struct memory_failure_cpu *mf_cpu;
1457 unsigned long proc_flags;
1458 struct memory_failure_entry entry = {
1459 .pfn = pfn,
Huang Yingea8f5fb2011-07-13 13:14:27 +08001460 .flags = flags,
1461 };
1462
1463 mf_cpu = &get_cpu_var(memory_failure_cpu);
1464 spin_lock_irqsave(&mf_cpu->lock, proc_flags);
Stefani Seibold498d3192013-11-14 14:32:17 -08001465 if (kfifo_put(&mf_cpu->fifo, entry))
Huang Yingea8f5fb2011-07-13 13:14:27 +08001466 schedule_work_on(smp_processor_id(), &mf_cpu->work);
1467 else
Joe Perches8e33a522013-07-25 11:53:25 -07001468 pr_err("Memory failure: buffer overflow when queuing memory failure at %#lx\n",
Huang Yingea8f5fb2011-07-13 13:14:27 +08001469 pfn);
1470 spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
1471 put_cpu_var(memory_failure_cpu);
1472}
1473EXPORT_SYMBOL_GPL(memory_failure_queue);
1474
1475static void memory_failure_work_func(struct work_struct *work)
1476{
1477 struct memory_failure_cpu *mf_cpu;
1478 struct memory_failure_entry entry = { 0, };
1479 unsigned long proc_flags;
1480 int gotten;
1481
Christoph Lameter7c8e0182014-06-04 16:07:56 -07001482 mf_cpu = this_cpu_ptr(&memory_failure_cpu);
Huang Yingea8f5fb2011-07-13 13:14:27 +08001483 for (;;) {
1484 spin_lock_irqsave(&mf_cpu->lock, proc_flags);
1485 gotten = kfifo_get(&mf_cpu->fifo, &entry);
1486 spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
1487 if (!gotten)
1488 break;
Naveen N. Raocf870c72013-07-10 14:57:01 +05301489 if (entry.flags & MF_SOFT_OFFLINE)
1490 soft_offline_page(pfn_to_page(entry.pfn), entry.flags);
1491 else
Eric W. Biederman83b57532017-07-09 18:14:01 -05001492 memory_failure(entry.pfn, entry.flags);
Huang Yingea8f5fb2011-07-13 13:14:27 +08001493 }
1494}
1495
1496static int __init memory_failure_init(void)
1497{
1498 struct memory_failure_cpu *mf_cpu;
1499 int cpu;
1500
1501 for_each_possible_cpu(cpu) {
1502 mf_cpu = &per_cpu(memory_failure_cpu, cpu);
1503 spin_lock_init(&mf_cpu->lock);
1504 INIT_KFIFO(mf_cpu->fifo);
1505 INIT_WORK(&mf_cpu->work, memory_failure_work_func);
1506 }
1507
1508 return 0;
1509}
1510core_initcall(memory_failure_init);
1511
Naoya Horiguchia5f65102015-11-05 18:47:26 -08001512#define unpoison_pr_info(fmt, pfn, rs) \
1513({ \
1514 if (__ratelimit(rs)) \
1515 pr_info(fmt, pfn); \
1516})
1517
Wu Fengguang847ce402009-12-16 12:19:58 +01001518/**
1519 * unpoison_memory - Unpoison a previously poisoned page
1520 * @pfn: Page number of the to be unpoisoned page
1521 *
1522 * Software-unpoison a page that has been poisoned by
1523 * memory_failure() earlier.
1524 *
1525 * This is only done on the software-level, so it only works
1526 * for linux injected failures, not real hardware failures
1527 *
1528 * Returns 0 for success, otherwise -errno.
1529 */
1530int unpoison_memory(unsigned long pfn)
1531{
1532 struct page *page;
1533 struct page *p;
1534 int freeit = 0;
Naoya Horiguchia5f65102015-11-05 18:47:26 -08001535 static DEFINE_RATELIMIT_STATE(unpoison_rs, DEFAULT_RATELIMIT_INTERVAL,
1536 DEFAULT_RATELIMIT_BURST);
Wu Fengguang847ce402009-12-16 12:19:58 +01001537
1538 if (!pfn_valid(pfn))
1539 return -ENXIO;
1540
1541 p = pfn_to_page(pfn);
1542 page = compound_head(p);
1543
1544 if (!PageHWPoison(p)) {
Chen Yucong495367c02016-05-20 16:57:32 -07001545 unpoison_pr_info("Unpoison: Page was already unpoisoned %#lx\n",
Naoya Horiguchia5f65102015-11-05 18:47:26 -08001546 pfn, &unpoison_rs);
Wu Fengguang847ce402009-12-16 12:19:58 +01001547 return 0;
1548 }
1549
Naoya Horiguchi230ac712015-09-08 15:03:29 -07001550 if (page_count(page) > 1) {
Chen Yucong495367c02016-05-20 16:57:32 -07001551 unpoison_pr_info("Unpoison: Someone grabs the hwpoison page %#lx\n",
Naoya Horiguchia5f65102015-11-05 18:47:26 -08001552 pfn, &unpoison_rs);
Naoya Horiguchi230ac712015-09-08 15:03:29 -07001553 return 0;
1554 }
1555
1556 if (page_mapped(page)) {
Chen Yucong495367c02016-05-20 16:57:32 -07001557 unpoison_pr_info("Unpoison: Someone maps the hwpoison page %#lx\n",
Naoya Horiguchia5f65102015-11-05 18:47:26 -08001558 pfn, &unpoison_rs);
Naoya Horiguchi230ac712015-09-08 15:03:29 -07001559 return 0;
1560 }
1561
1562 if (page_mapping(page)) {
Chen Yucong495367c02016-05-20 16:57:32 -07001563 unpoison_pr_info("Unpoison: the hwpoison page has non-NULL mapping %#lx\n",
Naoya Horiguchia5f65102015-11-05 18:47:26 -08001564 pfn, &unpoison_rs);
Naoya Horiguchi230ac712015-09-08 15:03:29 -07001565 return 0;
1566 }
1567
Wanpeng Li0cea3fd2013-09-11 14:22:53 -07001568 /*
1569 * unpoison_memory() can encounter thp only when the thp is being
1570 * worked by memory_failure() and the page lock is not held yet.
1571 * In such case, we yield to memory_failure() and make unpoison fail.
1572 */
Wanpeng Lie76d30e2013-09-30 13:45:22 -07001573 if (!PageHuge(page) && PageTransHuge(page)) {
Chen Yucong495367c02016-05-20 16:57:32 -07001574 unpoison_pr_info("Unpoison: Memory failure is now running on %#lx\n",
Naoya Horiguchia5f65102015-11-05 18:47:26 -08001575 pfn, &unpoison_rs);
Naoya Horiguchiead07f62015-06-24 16:56:48 -07001576 return 0;
Wanpeng Li0cea3fd2013-09-11 14:22:53 -07001577 }
1578
Naoya Horiguchiead07f62015-06-24 16:56:48 -07001579 if (!get_hwpoison_page(p)) {
Wu Fengguang847ce402009-12-16 12:19:58 +01001580 if (TestClearPageHWPoison(p))
Naoya Horiguchi8e304562015-09-08 15:03:24 -07001581 num_poisoned_pages_dec();
Chen Yucong495367c02016-05-20 16:57:32 -07001582 unpoison_pr_info("Unpoison: Software-unpoisoned free page %#lx\n",
Naoya Horiguchia5f65102015-11-05 18:47:26 -08001583 pfn, &unpoison_rs);
Wu Fengguang847ce402009-12-16 12:19:58 +01001584 return 0;
1585 }
1586
Jens Axboe7eaceac2011-03-10 08:52:07 +01001587 lock_page(page);
Wu Fengguang847ce402009-12-16 12:19:58 +01001588 /*
1589 * This test is racy because PG_hwpoison is set outside of page lock.
1590 * That's acceptable because that won't trigger kernel panic. Instead,
1591 * the PG_hwpoison page will be caught and isolated on the entrance to
1592 * the free buddy page pool.
1593 */
Naoya Horiguchic9fbdd52010-05-28 09:29:19 +09001594 if (TestClearPageHWPoison(page)) {
Chen Yucong495367c02016-05-20 16:57:32 -07001595 unpoison_pr_info("Unpoison: Software-unpoisoned page %#lx\n",
Naoya Horiguchia5f65102015-11-05 18:47:26 -08001596 pfn, &unpoison_rs);
Naoya Horiguchib37ff712017-07-10 15:47:38 -07001597 num_poisoned_pages_dec();
Wu Fengguang847ce402009-12-16 12:19:58 +01001598 freeit = 1;
1599 }
1600 unlock_page(page);
1601
Wanpeng Li665d9da2015-09-08 15:03:21 -07001602 put_hwpoison_page(page);
Wanpeng Li3ba5eeb2013-09-11 14:23:01 -07001603 if (freeit && !(pfn == my_zero_pfn(0) && page_count(p) == 1))
Wanpeng Li665d9da2015-09-08 15:03:21 -07001604 put_hwpoison_page(page);
Wu Fengguang847ce402009-12-16 12:19:58 +01001605
1606 return 0;
1607}
1608EXPORT_SYMBOL(unpoison_memory);
Andi Kleenfacb6012009-12-16 12:20:00 +01001609
Michal Hocko666feb22018-04-10 16:30:03 -07001610static struct page *new_page(struct page *p, unsigned long private)
Andi Kleenfacb6012009-12-16 12:20:00 +01001611{
Andi Kleen12686d12009-12-16 12:20:01 +01001612 int nid = page_to_nid(p);
Anshuman Khandual94310cb2017-07-06 15:38:38 -07001613
Michal Hockoef77ba52017-07-10 15:49:14 -07001614 return new_page_nodemask(p, nid, &node_states[N_MEMORY]);
Andi Kleenfacb6012009-12-16 12:20:00 +01001615}
1616
1617/*
1618 * Safely get reference count of an arbitrary page.
1619 * Returns 0 for a free page, -EIO for a zero refcount page
1620 * that is not free, and 1 for any other page type.
1621 * For 1 the page is returned with increased page count, otherwise not.
1622 */
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001623static int __get_any_page(struct page *p, unsigned long pfn, int flags)
Andi Kleenfacb6012009-12-16 12:20:00 +01001624{
1625 int ret;
1626
1627 if (flags & MF_COUNT_INCREASED)
1628 return 1;
1629
1630 /*
Naoya Horiguchid950b952010-09-08 10:19:39 +09001631 * When the target page is a free hugepage, just remove it
1632 * from free hugepage list.
1633 */
Naoya Horiguchiead07f62015-06-24 16:56:48 -07001634 if (!get_hwpoison_page(p)) {
Naoya Horiguchid950b952010-09-08 10:19:39 +09001635 if (PageHuge(p)) {
Borislav Petkov71dd0b82012-05-29 15:06:16 -07001636 pr_info("%s: %#lx free huge page\n", __func__, pfn);
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001637 ret = 0;
Naoya Horiguchid950b952010-09-08 10:19:39 +09001638 } else if (is_free_buddy_page(p)) {
Borislav Petkov71dd0b82012-05-29 15:06:16 -07001639 pr_info("%s: %#lx free buddy page\n", __func__, pfn);
Andi Kleenfacb6012009-12-16 12:20:00 +01001640 ret = 0;
1641 } else {
Borislav Petkov71dd0b82012-05-29 15:06:16 -07001642 pr_info("%s: %#lx: unknown zero refcount page type %lx\n",
1643 __func__, pfn, p->flags);
Andi Kleenfacb6012009-12-16 12:20:00 +01001644 ret = -EIO;
1645 }
1646 } else {
1647 /* Not a free page */
1648 ret = 1;
1649 }
Andi Kleenfacb6012009-12-16 12:20:00 +01001650 return ret;
1651}
1652
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001653static int get_any_page(struct page *page, unsigned long pfn, int flags)
1654{
1655 int ret = __get_any_page(page, pfn, flags);
1656
Yisheng Xie85fbe5d2017-02-24 14:57:35 -08001657 if (ret == 1 && !PageHuge(page) &&
1658 !PageLRU(page) && !__PageMovable(page)) {
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001659 /*
1660 * Try to free it.
1661 */
Wanpeng Li665d9da2015-09-08 15:03:21 -07001662 put_hwpoison_page(page);
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001663 shake_page(page, 1);
1664
1665 /*
1666 * Did it turn free?
1667 */
1668 ret = __get_any_page(page, pfn, 0);
Naoya Horiguchid96b3392016-01-15 16:54:03 -08001669 if (ret == 1 && !PageLRU(page)) {
Wanpeng Li4f32be62015-08-14 15:34:56 -07001670 /* Drop page reference which is from __get_any_page() */
Wanpeng Li665d9da2015-09-08 15:03:21 -07001671 put_hwpoison_page(page);
Anshuman Khandual82a24812017-05-03 14:55:31 -07001672 pr_info("soft_offline: %#lx: unknown non LRU page type %lx (%pGp)\n",
1673 pfn, page->flags, &page->flags);
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001674 return -EIO;
1675 }
1676 }
1677 return ret;
1678}
1679
Naoya Horiguchid950b952010-09-08 10:19:39 +09001680static int soft_offline_huge_page(struct page *page, int flags)
1681{
1682 int ret;
1683 unsigned long pfn = page_to_pfn(page);
1684 struct page *hpage = compound_head(page);
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001685 LIST_HEAD(pagelist);
Naoya Horiguchid950b952010-09-08 10:19:39 +09001686
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001687 /*
1688 * This double-check of PageHWPoison is to avoid the race with
1689 * memory_failure(). See also comment in __soft_offline_page().
1690 */
1691 lock_page(hpage);
Xishi Qiu0ebff322013-02-22 16:33:59 -08001692 if (PageHWPoison(hpage)) {
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001693 unlock_page(hpage);
Wanpeng Li665d9da2015-09-08 15:03:21 -07001694 put_hwpoison_page(hpage);
Xishi Qiu0ebff322013-02-22 16:33:59 -08001695 pr_info("soft offline: %#lx hugepage already poisoned\n", pfn);
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001696 return -EBUSY;
Xishi Qiu0ebff322013-02-22 16:33:59 -08001697 }
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001698 unlock_page(hpage);
Naoya Horiguchid950b952010-09-08 10:19:39 +09001699
Naoya Horiguchibcc54222015-04-15 16:14:38 -07001700 ret = isolate_huge_page(hpage, &pagelist);
Wanpeng Li03613802015-08-14 15:34:59 -07001701 /*
1702 * get_any_page() and isolate_huge_page() takes a refcount each,
1703 * so need to drop one here.
1704 */
Wanpeng Li665d9da2015-09-08 15:03:21 -07001705 put_hwpoison_page(hpage);
Wanpeng Li03613802015-08-14 15:34:59 -07001706 if (!ret) {
Naoya Horiguchibcc54222015-04-15 16:14:38 -07001707 pr_info("soft offline: %#lx hugepage failed to isolate\n", pfn);
1708 return -EBUSY;
1709 }
1710
David Rientjes68711a72014-06-04 16:08:25 -07001711 ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001712 MIGRATE_SYNC, MR_MEMORY_FAILURE);
Naoya Horiguchid950b952010-09-08 10:19:39 +09001713 if (ret) {
Laszlo Tothb6b18aa2017-11-15 17:37:00 -08001714 pr_info("soft offline: %#lx: hugepage migration failed %d, type %lx (%pGp)\n",
Anshuman Khandual82a24812017-05-03 14:55:31 -07001715 pfn, ret, page->flags, &page->flags);
Punit Agrawal30809f52017-06-02 14:46:40 -07001716 if (!list_empty(&pagelist))
1717 putback_movable_pages(&pagelist);
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001718 if (ret > 0)
1719 ret = -EIO;
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001720 } else {
Naoya Horiguchib37ff712017-07-10 15:47:38 -07001721 if (PageHuge(page))
Anshuman Khandualc3114a82017-07-10 15:47:41 -07001722 dissolve_free_huge_page(page);
Naoya Horiguchid950b952010-09-08 10:19:39 +09001723 }
Naoya Horiguchid950b952010-09-08 10:19:39 +09001724 return ret;
1725}
1726
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001727static int __soft_offline_page(struct page *page, int flags)
1728{
1729 int ret;
1730 unsigned long pfn = page_to_pfn(page);
Andi Kleenfacb6012009-12-16 12:20:00 +01001731
1732 /*
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001733 * Check PageHWPoison again inside page lock because PageHWPoison
1734 * is set by memory_failure() outside page lock. Note that
1735 * memory_failure() also double-checks PageHWPoison inside page lock,
1736 * so there's no race between soft_offline_page() and memory_failure().
Andi Kleenfacb6012009-12-16 12:20:00 +01001737 */
Xishi Qiu0ebff322013-02-22 16:33:59 -08001738 lock_page(page);
1739 wait_on_page_writeback(page);
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001740 if (PageHWPoison(page)) {
1741 unlock_page(page);
Wanpeng Li665d9da2015-09-08 15:03:21 -07001742 put_hwpoison_page(page);
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001743 pr_info("soft offline: %#lx page already poisoned\n", pfn);
1744 return -EBUSY;
1745 }
Andi Kleenfacb6012009-12-16 12:20:00 +01001746 /*
1747 * Try to invalidate first. This should work for
1748 * non dirty unmapped page cache pages.
1749 */
1750 ret = invalidate_inode_page(page);
1751 unlock_page(page);
Andi Kleenfacb6012009-12-16 12:20:00 +01001752 /*
Andi Kleenfacb6012009-12-16 12:20:00 +01001753 * RED-PEN would be better to keep it isolated here, but we
1754 * would need to fix isolation locking first.
1755 */
Andi Kleenfacb6012009-12-16 12:20:00 +01001756 if (ret == 1) {
Wanpeng Li665d9da2015-09-08 15:03:21 -07001757 put_hwpoison_page(page);
Andi Kleenfb46e732010-09-27 23:31:30 +02001758 pr_info("soft_offline: %#lx: invalidated\n", pfn);
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001759 SetPageHWPoison(page);
Naoya Horiguchi8e304562015-09-08 15:03:24 -07001760 num_poisoned_pages_inc();
Naoya Horiguchiaf8fae72013-02-22 16:34:03 -08001761 return 0;
Andi Kleenfacb6012009-12-16 12:20:00 +01001762 }
1763
1764 /*
1765 * Simple invalidation didn't work.
1766 * Try to migrate to a new page instead. migrate.c
1767 * handles a large number of cases for us.
1768 */
Yisheng Xie85fbe5d2017-02-24 14:57:35 -08001769 if (PageLRU(page))
1770 ret = isolate_lru_page(page);
1771 else
1772 ret = isolate_movable_page(page, ISOLATE_UNEVICTABLE);
Konstantin Khlebnikovbd486282011-05-24 17:12:20 -07001773 /*
1774 * Drop page reference which is came from get_any_page()
1775 * successful isolate_lru_page() already took another one.
1776 */
Wanpeng Li665d9da2015-09-08 15:03:21 -07001777 put_hwpoison_page(page);
Andi Kleenfacb6012009-12-16 12:20:00 +01001778 if (!ret) {
1779 LIST_HEAD(pagelist);
Yisheng Xie85fbe5d2017-02-24 14:57:35 -08001780 /*
1781 * After isolated lru page, the PageLRU will be cleared,
1782 * so use !__PageMovable instead for LRU page's mapping
1783 * cannot have PAGE_MAPPING_MOVABLE.
1784 */
1785 if (!__PageMovable(page))
1786 inc_node_page_state(page, NR_ISOLATED_ANON +
1787 page_is_file_cache(page));
Andi Kleenfacb6012009-12-16 12:20:00 +01001788 list_add(&page->lru, &pagelist);
David Rientjes68711a72014-06-04 16:08:25 -07001789 ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001790 MIGRATE_SYNC, MR_MEMORY_FAILURE);
Andi Kleenfacb6012009-12-16 12:20:00 +01001791 if (ret) {
Yisheng Xie85fbe5d2017-02-24 14:57:35 -08001792 if (!list_empty(&pagelist))
1793 putback_movable_pages(&pagelist);
Joonsoo Kim59c82b72014-01-21 15:51:17 -08001794
Anshuman Khandual82a24812017-05-03 14:55:31 -07001795 pr_info("soft offline: %#lx: migration failed %d, type %lx (%pGp)\n",
1796 pfn, ret, page->flags, &page->flags);
Andi Kleenfacb6012009-12-16 12:20:00 +01001797 if (ret > 0)
1798 ret = -EIO;
1799 }
1800 } else {
Anshuman Khandual82a24812017-05-03 14:55:31 -07001801 pr_info("soft offline: %#lx: isolation failed: %d, page count %d, type %lx (%pGp)\n",
1802 pfn, ret, page_count(page), page->flags, &page->flags);
Andi Kleenfacb6012009-12-16 12:20:00 +01001803 }
Andi Kleenfacb6012009-12-16 12:20:00 +01001804 return ret;
1805}
Wanpeng Li86e05772013-09-11 14:22:56 -07001806
Naoya Horiguchiacc14dc2016-01-15 16:57:43 -08001807static int soft_offline_in_use_page(struct page *page, int flags)
1808{
1809 int ret;
1810 struct page *hpage = compound_head(page);
1811
1812 if (!PageHuge(page) && PageTransHuge(hpage)) {
1813 lock_page(hpage);
Naoya Horiguchi98fd1ef2016-01-15 16:57:46 -08001814 if (!PageAnon(hpage) || unlikely(split_huge_page(hpage))) {
1815 unlock_page(hpage);
1816 if (!PageAnon(hpage))
1817 pr_info("soft offline: %#lx: non anonymous thp\n", page_to_pfn(page));
1818 else
1819 pr_info("soft offline: %#lx: thp split failed\n", page_to_pfn(page));
1820 put_hwpoison_page(hpage);
Naoya Horiguchiacc14dc2016-01-15 16:57:43 -08001821 return -EBUSY;
1822 }
Naoya Horiguchi98fd1ef2016-01-15 16:57:46 -08001823 unlock_page(hpage);
Naoya Horiguchiacc14dc2016-01-15 16:57:43 -08001824 get_hwpoison_page(page);
1825 put_hwpoison_page(hpage);
1826 }
1827
1828 if (PageHuge(page))
1829 ret = soft_offline_huge_page(page, flags);
1830 else
1831 ret = __soft_offline_page(page, flags);
1832
1833 return ret;
1834}
1835
1836static void soft_offline_free_page(struct page *page)
1837{
Naoya Horiguchib37ff712017-07-10 15:47:38 -07001838 struct page *head = compound_head(page);
Naoya Horiguchiacc14dc2016-01-15 16:57:43 -08001839
Naoya Horiguchib37ff712017-07-10 15:47:38 -07001840 if (!TestSetPageHWPoison(head)) {
1841 num_poisoned_pages_inc();
1842 if (PageHuge(head))
Naoya Horiguchid4a3a602017-07-10 15:47:44 -07001843 dissolve_free_huge_page(page);
Naoya Horiguchiacc14dc2016-01-15 16:57:43 -08001844 }
1845}
1846
Wanpeng Li86e05772013-09-11 14:22:56 -07001847/**
1848 * soft_offline_page - Soft offline a page.
1849 * @page: page to offline
1850 * @flags: flags. Same as memory_failure().
1851 *
1852 * Returns 0 on success, otherwise negated errno.
1853 *
1854 * Soft offline a page, by migration or invalidation,
1855 * without killing anything. This is for the case when
1856 * a page is not corrupted yet (so it's still valid to access),
1857 * but has had a number of corrected errors and is better taken
1858 * out.
1859 *
1860 * The actual policy on when to do that is maintained by
1861 * user space.
1862 *
1863 * This should never impact any application or cause data loss,
1864 * however it might take some time.
1865 *
1866 * This is not a 100% solution for all memory, but tries to be
1867 * ``good enough'' for the majority of memory.
1868 */
1869int soft_offline_page(struct page *page, int flags)
1870{
1871 int ret;
1872 unsigned long pfn = page_to_pfn(page);
Wanpeng Li86e05772013-09-11 14:22:56 -07001873
Dan Williams86a66812018-07-13 21:49:56 -07001874 if (is_zone_device_page(page)) {
1875 pr_debug_ratelimited("soft_offline: %#lx page is device page\n",
1876 pfn);
1877 if (flags & MF_COUNT_INCREASED)
1878 put_page(page);
1879 return -EIO;
1880 }
1881
Wanpeng Li86e05772013-09-11 14:22:56 -07001882 if (PageHWPoison(page)) {
1883 pr_info("soft offline: %#lx page already poisoned\n", pfn);
Wanpeng Li1e0e6352015-09-08 15:03:13 -07001884 if (flags & MF_COUNT_INCREASED)
Wanpeng Li665d9da2015-09-08 15:03:21 -07001885 put_hwpoison_page(page);
Wanpeng Li86e05772013-09-11 14:22:56 -07001886 return -EBUSY;
1887 }
Wanpeng Li86e05772013-09-11 14:22:56 -07001888
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001889 get_online_mems();
Wanpeng Li86e05772013-09-11 14:22:56 -07001890 ret = get_any_page(page, pfn, flags);
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001891 put_online_mems();
Naoya Horiguchi4e41a302016-01-15 16:54:07 -08001892
Naoya Horiguchiacc14dc2016-01-15 16:57:43 -08001893 if (ret > 0)
1894 ret = soft_offline_in_use_page(page, flags);
1895 else if (ret == 0)
1896 soft_offline_free_page(page);
Naoya Horiguchi4e41a302016-01-15 16:54:07 -08001897
Wanpeng Li86e05772013-09-11 14:22:56 -07001898 return ret;
1899}