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