x86, suspend: Restore MISC_ENABLE MSR in realmode wakeup
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / memory-failure.c
blob9568e0b11333c50751f681bb7eeab06c744fd8ca
1 /*
2 * Copyright (C) 2008, 2009 Intel Corporation
3 * Authors: Andi Kleen, Fengguang Wu
5 * This software may be redistributed and/or modified under the terms of
6 * the GNU General Public License ("GPL") version 2 only as published by the
7 * Free Software Foundation.
9 * High level machine check handler. Handles pages reported by the
10 * hardware as being corrupted usually due to a multi-bit ECC memory or cache
11 * failure.
13 * In addition there is a "soft offline" entry point that allows stop using
14 * not-yet-corrupted-by-suspicious pages without killing anything.
16 * Handles page cache pages in various states. The tricky part
17 * here is that we can access any page asynchronously in respect to
18 * other VM users, because memory failures could happen anytime and
19 * anywhere. This could violate some of their assumptions. This is why
20 * this code has to be extremely careful. Generally it tries to use
21 * normal locking rules, as in get the standard locks, even if that means
22 * the error handling takes potentially a long time.
24 * There are several operations here with exponential complexity because
25 * of unsuitable VM data structures. For example the operation to map back
26 * from RMAP chains to processes has to walk the complete process list and
27 * has non linear complexity with the number. But since memory corruptions
28 * are rare we hope to get away with this. This avoids impacting the core
29 * VM.
33 * Notebook:
34 * - hugetlb needs more code
35 * - kcore/oldmem/vmcore/mem/kmem check for hwpoison pages
36 * - pass bad pages to kdump next kernel
38 #include <linux/kernel.h>
39 #include <linux/mm.h>
40 #include <linux/page-flags.h>
41 #include <linux/kernel-page-flags.h>
42 #include <linux/sched.h>
43 #include <linux/ksm.h>
44 #include <linux/rmap.h>
45 #include <linux/pagemap.h>
46 #include <linux/swap.h>
47 #include <linux/backing-dev.h>
48 #include <linux/migrate.h>
49 #include <linux/page-isolation.h>
50 #include <linux/suspend.h>
51 #include <linux/slab.h>
52 #include <linux/swapops.h>
53 #include <linux/hugetlb.h>
54 #include <linux/memory_hotplug.h>
55 #include <linux/mm_inline.h>
56 #include "internal.h"
58 int sysctl_memory_failure_early_kill __read_mostly = 0;
60 int sysctl_memory_failure_recovery __read_mostly = 1;
62 atomic_long_t mce_bad_pages __read_mostly = ATOMIC_LONG_INIT(0);
64 #if defined(CONFIG_HWPOISON_INJECT) || defined(CONFIG_HWPOISON_INJECT_MODULE)
66 u32 hwpoison_filter_enable = 0;
67 u32 hwpoison_filter_dev_major = ~0U;
68 u32 hwpoison_filter_dev_minor = ~0U;
69 u64 hwpoison_filter_flags_mask;
70 u64 hwpoison_filter_flags_value;
71 EXPORT_SYMBOL_GPL(hwpoison_filter_enable);
72 EXPORT_SYMBOL_GPL(hwpoison_filter_dev_major);
73 EXPORT_SYMBOL_GPL(hwpoison_filter_dev_minor);
74 EXPORT_SYMBOL_GPL(hwpoison_filter_flags_mask);
75 EXPORT_SYMBOL_GPL(hwpoison_filter_flags_value);
77 static int hwpoison_filter_dev(struct page *p)
79 struct address_space *mapping;
80 dev_t dev;
82 if (hwpoison_filter_dev_major == ~0U &&
83 hwpoison_filter_dev_minor == ~0U)
84 return 0;
87 * page_mapping() does not accept slab pages.
89 if (PageSlab(p))
90 return -EINVAL;
92 mapping = page_mapping(p);
93 if (mapping == NULL || mapping->host == NULL)
94 return -EINVAL;
96 dev = mapping->host->i_sb->s_dev;
97 if (hwpoison_filter_dev_major != ~0U &&
98 hwpoison_filter_dev_major != MAJOR(dev))
99 return -EINVAL;
100 if (hwpoison_filter_dev_minor != ~0U &&
101 hwpoison_filter_dev_minor != MINOR(dev))
102 return -EINVAL;
104 return 0;
107 static int hwpoison_filter_flags(struct page *p)
109 if (!hwpoison_filter_flags_mask)
110 return 0;
112 if ((stable_page_flags(p) & hwpoison_filter_flags_mask) ==
113 hwpoison_filter_flags_value)
114 return 0;
115 else
116 return -EINVAL;
120 * This allows stress tests to limit test scope to a collection of tasks
121 * by putting them under some memcg. This prevents killing unrelated/important
122 * processes such as /sbin/init. Note that the target task may share clean
123 * pages with init (eg. libc text), which is harmless. If the target task
124 * share _dirty_ pages with another task B, the test scheme must make sure B
125 * is also included in the memcg. At last, due to race conditions this filter
126 * can only guarantee that the page either belongs to the memcg tasks, or is
127 * a freed page.
129 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
130 u64 hwpoison_filter_memcg;
131 EXPORT_SYMBOL_GPL(hwpoison_filter_memcg);
132 static int hwpoison_filter_task(struct page *p)
134 struct mem_cgroup *mem;
135 struct cgroup_subsys_state *css;
136 unsigned long ino;
138 if (!hwpoison_filter_memcg)
139 return 0;
141 mem = try_get_mem_cgroup_from_page(p);
142 if (!mem)
143 return -EINVAL;
145 css = mem_cgroup_css(mem);
146 /* root_mem_cgroup has NULL dentries */
147 if (!css->cgroup->dentry)
148 return -EINVAL;
150 ino = css->cgroup->dentry->d_inode->i_ino;
151 css_put(css);
153 if (ino != hwpoison_filter_memcg)
154 return -EINVAL;
156 return 0;
158 #else
159 static int hwpoison_filter_task(struct page *p) { return 0; }
160 #endif
162 int hwpoison_filter(struct page *p)
164 if (!hwpoison_filter_enable)
165 return 0;
167 if (hwpoison_filter_dev(p))
168 return -EINVAL;
170 if (hwpoison_filter_flags(p))
171 return -EINVAL;
173 if (hwpoison_filter_task(p))
174 return -EINVAL;
176 return 0;
178 #else
179 int hwpoison_filter(struct page *p)
181 return 0;
183 #endif
185 EXPORT_SYMBOL_GPL(hwpoison_filter);
188 * Send all the processes who have the page mapped an ``action optional''
189 * signal.
191 static int kill_proc_ao(struct task_struct *t, unsigned long addr, int trapno,
192 unsigned long pfn, struct page *page)
194 struct siginfo si;
195 int ret;
197 printk(KERN_ERR
198 "MCE %#lx: Killing %s:%d early due to hardware memory corruption\n",
199 pfn, t->comm, t->pid);
200 si.si_signo = SIGBUS;
201 si.si_errno = 0;
202 si.si_code = BUS_MCEERR_AO;
203 si.si_addr = (void *)addr;
204 #ifdef __ARCH_SI_TRAPNO
205 si.si_trapno = trapno;
206 #endif
207 si.si_addr_lsb = compound_trans_order(compound_head(page)) + PAGE_SHIFT;
209 * Don't use force here, it's convenient if the signal
210 * can be temporarily blocked.
211 * This could cause a loop when the user sets SIGBUS
212 * to SIG_IGN, but hopefully no one will do that?
214 ret = send_sig_info(SIGBUS, &si, t); /* synchronous? */
215 if (ret < 0)
216 printk(KERN_INFO "MCE: Error sending signal to %s:%d: %d\n",
217 t->comm, t->pid, ret);
218 return ret;
222 * When a unknown page type is encountered drain as many buffers as possible
223 * in the hope to turn the page into a LRU or free page, which we can handle.
225 void shake_page(struct page *p, int access)
227 if (!PageSlab(p)) {
228 lru_add_drain_all();
229 if (PageLRU(p))
230 return;
231 drain_all_pages();
232 if (PageLRU(p) || is_free_buddy_page(p))
233 return;
237 * Only call shrink_slab here (which would also shrink other caches) if
238 * access is not potentially fatal.
240 if (access) {
241 int nr;
242 do {
243 nr = shrink_slab(1000, GFP_KERNEL, 1000);
244 if (page_count(p) == 1)
245 break;
246 } while (nr > 10);
249 EXPORT_SYMBOL_GPL(shake_page);
252 * Kill all processes that have a poisoned page mapped and then isolate
253 * the page.
255 * General strategy:
256 * Find all processes having the page mapped and kill them.
257 * But we keep a page reference around so that the page is not
258 * actually freed yet.
259 * Then stash the page away
261 * There's no convenient way to get back to mapped processes
262 * from the VMAs. So do a brute-force search over all
263 * running processes.
265 * Remember that machine checks are not common (or rather
266 * if they are common you have other problems), so this shouldn't
267 * be a performance issue.
269 * Also there are some races possible while we get from the
270 * error detection to actually handle it.
273 struct to_kill {
274 struct list_head nd;
275 struct task_struct *tsk;
276 unsigned long addr;
277 char addr_valid;
281 * Failure handling: if we can't find or can't kill a process there's
282 * not much we can do. We just print a message and ignore otherwise.
286 * Schedule a process for later kill.
287 * Uses GFP_ATOMIC allocations to avoid potential recursions in the VM.
288 * TBD would GFP_NOIO be enough?
290 static void add_to_kill(struct task_struct *tsk, struct page *p,
291 struct vm_area_struct *vma,
292 struct list_head *to_kill,
293 struct to_kill **tkc)
295 struct to_kill *tk;
297 if (*tkc) {
298 tk = *tkc;
299 *tkc = NULL;
300 } else {
301 tk = kmalloc(sizeof(struct to_kill), GFP_ATOMIC);
302 if (!tk) {
303 printk(KERN_ERR
304 "MCE: Out of memory while machine check handling\n");
305 return;
308 tk->addr = page_address_in_vma(p, vma);
309 tk->addr_valid = 1;
312 * In theory we don't have to kill when the page was
313 * munmaped. But it could be also a mremap. Since that's
314 * likely very rare kill anyways just out of paranoia, but use
315 * a SIGKILL because the error is not contained anymore.
317 if (tk->addr == -EFAULT) {
318 pr_info("MCE: Unable to find user space address %lx in %s\n",
319 page_to_pfn(p), tsk->comm);
320 tk->addr_valid = 0;
322 get_task_struct(tsk);
323 tk->tsk = tsk;
324 list_add_tail(&tk->nd, to_kill);
328 * Kill the processes that have been collected earlier.
330 * Only do anything when DOIT is set, otherwise just free the list
331 * (this is used for clean pages which do not need killing)
332 * Also when FAIL is set do a force kill because something went
333 * wrong earlier.
335 static void kill_procs_ao(struct list_head *to_kill, int doit, int trapno,
336 int fail, struct page *page, unsigned long pfn)
338 struct to_kill *tk, *next;
340 list_for_each_entry_safe (tk, next, to_kill, nd) {
341 if (doit) {
343 * In case something went wrong with munmapping
344 * make sure the process doesn't catch the
345 * signal and then access the memory. Just kill it.
347 if (fail || tk->addr_valid == 0) {
348 printk(KERN_ERR
349 "MCE %#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
350 pfn, tk->tsk->comm, tk->tsk->pid);
351 force_sig(SIGKILL, tk->tsk);
355 * In theory the process could have mapped
356 * something else on the address in-between. We could
357 * check for that, but we need to tell the
358 * process anyways.
360 else if (kill_proc_ao(tk->tsk, tk->addr, trapno,
361 pfn, page) < 0)
362 printk(KERN_ERR
363 "MCE %#lx: Cannot send advisory machine check signal to %s:%d\n",
364 pfn, tk->tsk->comm, tk->tsk->pid);
366 put_task_struct(tk->tsk);
367 kfree(tk);
371 static int task_early_kill(struct task_struct *tsk)
373 if (!tsk->mm)
374 return 0;
375 if (tsk->flags & PF_MCE_PROCESS)
376 return !!(tsk->flags & PF_MCE_EARLY);
377 return sysctl_memory_failure_early_kill;
381 * Collect processes when the error hit an anonymous page.
383 static void collect_procs_anon(struct page *page, struct list_head *to_kill,
384 struct to_kill **tkc)
386 struct vm_area_struct *vma;
387 struct task_struct *tsk;
388 struct anon_vma *av;
390 read_lock(&tasklist_lock);
391 av = page_lock_anon_vma(page);
392 if (av == NULL) /* Not actually mapped anymore */
393 goto out;
394 for_each_process (tsk) {
395 struct anon_vma_chain *vmac;
397 if (!task_early_kill(tsk))
398 continue;
399 list_for_each_entry(vmac, &av->head, same_anon_vma) {
400 vma = vmac->vma;
401 if (!page_mapped_in_vma(page, vma))
402 continue;
403 if (vma->vm_mm == tsk->mm)
404 add_to_kill(tsk, page, vma, to_kill, tkc);
407 page_unlock_anon_vma(av);
408 out:
409 read_unlock(&tasklist_lock);
413 * Collect processes when the error hit a file mapped page.
415 static void collect_procs_file(struct page *page, struct list_head *to_kill,
416 struct to_kill **tkc)
418 struct vm_area_struct *vma;
419 struct task_struct *tsk;
420 struct prio_tree_iter iter;
421 struct address_space *mapping = page->mapping;
424 * A note on the locking order between the two locks.
425 * We don't rely on this particular order.
426 * If you have some other code that needs a different order
427 * feel free to switch them around. Or add a reverse link
428 * from mm_struct to task_struct, then this could be all
429 * done without taking tasklist_lock and looping over all tasks.
432 read_lock(&tasklist_lock);
433 spin_lock(&mapping->i_mmap_lock);
434 for_each_process(tsk) {
435 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
437 if (!task_early_kill(tsk))
438 continue;
440 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff,
441 pgoff) {
443 * Send early kill signal to tasks where a vma covers
444 * the page but the corrupted page is not necessarily
445 * mapped it in its pte.
446 * Assume applications who requested early kill want
447 * to be informed of all such data corruptions.
449 if (vma->vm_mm == tsk->mm)
450 add_to_kill(tsk, page, vma, to_kill, tkc);
453 spin_unlock(&mapping->i_mmap_lock);
454 read_unlock(&tasklist_lock);
458 * Collect the processes who have the corrupted page mapped to kill.
459 * This is done in two steps for locking reasons.
460 * First preallocate one tokill structure outside the spin locks,
461 * so that we can kill at least one process reasonably reliable.
463 static void collect_procs(struct page *page, struct list_head *tokill)
465 struct to_kill *tk;
467 if (!page->mapping)
468 return;
470 tk = kmalloc(sizeof(struct to_kill), GFP_NOIO);
471 if (!tk)
472 return;
473 if (PageAnon(page))
474 collect_procs_anon(page, tokill, &tk);
475 else
476 collect_procs_file(page, tokill, &tk);
477 kfree(tk);
481 * Error handlers for various types of pages.
484 enum outcome {
485 IGNORED, /* Error: cannot be handled */
486 FAILED, /* Error: handling failed */
487 DELAYED, /* Will be handled later */
488 RECOVERED, /* Successfully recovered */
491 static const char *action_name[] = {
492 [IGNORED] = "Ignored",
493 [FAILED] = "Failed",
494 [DELAYED] = "Delayed",
495 [RECOVERED] = "Recovered",
499 * XXX: It is possible that a page is isolated from LRU cache,
500 * and then kept in swap cache or failed to remove from page cache.
501 * The page count will stop it from being freed by unpoison.
502 * Stress tests should be aware of this memory leak problem.
504 static int delete_from_lru_cache(struct page *p)
506 if (!isolate_lru_page(p)) {
508 * Clear sensible page flags, so that the buddy system won't
509 * complain when the page is unpoison-and-freed.
511 ClearPageActive(p);
512 ClearPageUnevictable(p);
514 * drop the page count elevated by isolate_lru_page()
516 page_cache_release(p);
517 return 0;
519 return -EIO;
523 * Error hit kernel page.
524 * Do nothing, try to be lucky and not touch this instead. For a few cases we
525 * could be more sophisticated.
527 static int me_kernel(struct page *p, unsigned long pfn)
529 return IGNORED;
533 * Page in unknown state. Do nothing.
535 static int me_unknown(struct page *p, unsigned long pfn)
537 printk(KERN_ERR "MCE %#lx: Unknown page state\n", pfn);
538 return FAILED;
542 * Clean (or cleaned) page cache page.
544 static int me_pagecache_clean(struct page *p, unsigned long pfn)
546 int err;
547 int ret = FAILED;
548 struct address_space *mapping;
550 delete_from_lru_cache(p);
553 * For anonymous pages we're done the only reference left
554 * should be the one m_f() holds.
556 if (PageAnon(p))
557 return RECOVERED;
560 * Now truncate the page in the page cache. This is really
561 * more like a "temporary hole punch"
562 * Don't do this for block devices when someone else
563 * has a reference, because it could be file system metadata
564 * and that's not safe to truncate.
566 mapping = page_mapping(p);
567 if (!mapping) {
569 * Page has been teared down in the meanwhile
571 return FAILED;
575 * Truncation is a bit tricky. Enable it per file system for now.
577 * Open: to take i_mutex or not for this? Right now we don't.
579 if (mapping->a_ops->error_remove_page) {
580 err = mapping->a_ops->error_remove_page(mapping, p);
581 if (err != 0) {
582 printk(KERN_INFO "MCE %#lx: Failed to punch page: %d\n",
583 pfn, err);
584 } else if (page_has_private(p) &&
585 !try_to_release_page(p, GFP_NOIO)) {
586 pr_info("MCE %#lx: failed to release buffers\n", pfn);
587 } else {
588 ret = RECOVERED;
590 } else {
592 * If the file system doesn't support it just invalidate
593 * This fails on dirty or anything with private pages
595 if (invalidate_inode_page(p))
596 ret = RECOVERED;
597 else
598 printk(KERN_INFO "MCE %#lx: Failed to invalidate\n",
599 pfn);
601 return ret;
605 * Dirty cache page page
606 * Issues: when the error hit a hole page the error is not properly
607 * propagated.
609 static int me_pagecache_dirty(struct page *p, unsigned long pfn)
611 struct address_space *mapping = page_mapping(p);
613 SetPageError(p);
614 /* TBD: print more information about the file. */
615 if (mapping) {
617 * IO error will be reported by write(), fsync(), etc.
618 * who check the mapping.
619 * This way the application knows that something went
620 * wrong with its dirty file data.
622 * There's one open issue:
624 * The EIO will be only reported on the next IO
625 * operation and then cleared through the IO map.
626 * Normally Linux has two mechanisms to pass IO error
627 * first through the AS_EIO flag in the address space
628 * and then through the PageError flag in the page.
629 * Since we drop pages on memory failure handling the
630 * only mechanism open to use is through AS_AIO.
632 * This has the disadvantage that it gets cleared on
633 * the first operation that returns an error, while
634 * the PageError bit is more sticky and only cleared
635 * when the page is reread or dropped. If an
636 * application assumes it will always get error on
637 * fsync, but does other operations on the fd before
638 * and the page is dropped between then the error
639 * will not be properly reported.
641 * This can already happen even without hwpoisoned
642 * pages: first on metadata IO errors (which only
643 * report through AS_EIO) or when the page is dropped
644 * at the wrong time.
646 * So right now we assume that the application DTRT on
647 * the first EIO, but we're not worse than other parts
648 * of the kernel.
650 mapping_set_error(mapping, EIO);
653 return me_pagecache_clean(p, pfn);
657 * Clean and dirty swap cache.
659 * Dirty swap cache page is tricky to handle. The page could live both in page
660 * cache and swap cache(ie. page is freshly swapped in). So it could be
661 * referenced concurrently by 2 types of PTEs:
662 * normal PTEs and swap PTEs. We try to handle them consistently by calling
663 * try_to_unmap(TTU_IGNORE_HWPOISON) to convert the normal PTEs to swap PTEs,
664 * and then
665 * - clear dirty bit to prevent IO
666 * - remove from LRU
667 * - but keep in the swap cache, so that when we return to it on
668 * a later page fault, we know the application is accessing
669 * corrupted data and shall be killed (we installed simple
670 * interception code in do_swap_page to catch it).
672 * Clean swap cache pages can be directly isolated. A later page fault will
673 * bring in the known good data from disk.
675 static int me_swapcache_dirty(struct page *p, unsigned long pfn)
677 ClearPageDirty(p);
678 /* Trigger EIO in shmem: */
679 ClearPageUptodate(p);
681 if (!delete_from_lru_cache(p))
682 return DELAYED;
683 else
684 return FAILED;
687 static int me_swapcache_clean(struct page *p, unsigned long pfn)
689 delete_from_swap_cache(p);
691 if (!delete_from_lru_cache(p))
692 return RECOVERED;
693 else
694 return FAILED;
698 * Huge pages. Needs work.
699 * Issues:
700 * - Error on hugepage is contained in hugepage unit (not in raw page unit.)
701 * To narrow down kill region to one page, we need to break up pmd.
703 static int me_huge_page(struct page *p, unsigned long pfn)
705 int res = 0;
706 struct page *hpage = compound_head(p);
708 * We can safely recover from error on free or reserved (i.e.
709 * not in-use) hugepage by dequeuing it from freelist.
710 * To check whether a hugepage is in-use or not, we can't use
711 * page->lru because it can be used in other hugepage operations,
712 * such as __unmap_hugepage_range() and gather_surplus_pages().
713 * So instead we use page_mapping() and PageAnon().
714 * We assume that this function is called with page lock held,
715 * so there is no race between isolation and mapping/unmapping.
717 if (!(page_mapping(hpage) || PageAnon(hpage))) {
718 res = dequeue_hwpoisoned_huge_page(hpage);
719 if (!res)
720 return RECOVERED;
722 return DELAYED;
726 * Various page states we can handle.
728 * A page state is defined by its current page->flags bits.
729 * The table matches them in order and calls the right handler.
731 * This is quite tricky because we can access page at any time
732 * in its live cycle, so all accesses have to be extremely careful.
734 * This is not complete. More states could be added.
735 * For any missing state don't attempt recovery.
738 #define dirty (1UL << PG_dirty)
739 #define sc (1UL << PG_swapcache)
740 #define unevict (1UL << PG_unevictable)
741 #define mlock (1UL << PG_mlocked)
742 #define writeback (1UL << PG_writeback)
743 #define lru (1UL << PG_lru)
744 #define swapbacked (1UL << PG_swapbacked)
745 #define head (1UL << PG_head)
746 #define tail (1UL << PG_tail)
747 #define compound (1UL << PG_compound)
748 #define slab (1UL << PG_slab)
749 #define reserved (1UL << PG_reserved)
751 static struct page_state {
752 unsigned long mask;
753 unsigned long res;
754 char *msg;
755 int (*action)(struct page *p, unsigned long pfn);
756 } error_states[] = {
757 { reserved, reserved, "reserved kernel", me_kernel },
759 * free pages are specially detected outside this table:
760 * PG_buddy pages only make a small fraction of all free pages.
764 * Could in theory check if slab page is free or if we can drop
765 * currently unused objects without touching them. But just
766 * treat it as standard kernel for now.
768 { slab, slab, "kernel slab", me_kernel },
770 #ifdef CONFIG_PAGEFLAGS_EXTENDED
771 { head, head, "huge", me_huge_page },
772 { tail, tail, "huge", me_huge_page },
773 #else
774 { compound, compound, "huge", me_huge_page },
775 #endif
777 { sc|dirty, sc|dirty, "swapcache", me_swapcache_dirty },
778 { sc|dirty, sc, "swapcache", me_swapcache_clean },
780 { unevict|dirty, unevict|dirty, "unevictable LRU", me_pagecache_dirty},
781 { unevict, unevict, "unevictable LRU", me_pagecache_clean},
783 { mlock|dirty, mlock|dirty, "mlocked LRU", me_pagecache_dirty },
784 { mlock, mlock, "mlocked LRU", me_pagecache_clean },
786 { lru|dirty, lru|dirty, "LRU", me_pagecache_dirty },
787 { lru|dirty, lru, "clean LRU", me_pagecache_clean },
790 * Catchall entry: must be at end.
792 { 0, 0, "unknown page state", me_unknown },
795 #undef dirty
796 #undef sc
797 #undef unevict
798 #undef mlock
799 #undef writeback
800 #undef lru
801 #undef swapbacked
802 #undef head
803 #undef tail
804 #undef compound
805 #undef slab
806 #undef reserved
808 static void action_result(unsigned long pfn, char *msg, int result)
810 struct page *page = pfn_to_page(pfn);
812 printk(KERN_ERR "MCE %#lx: %s%s page recovery: %s\n",
813 pfn,
814 PageDirty(page) ? "dirty " : "",
815 msg, action_name[result]);
818 static int page_action(struct page_state *ps, struct page *p,
819 unsigned long pfn)
821 int result;
822 int count;
824 result = ps->action(p, pfn);
825 action_result(pfn, ps->msg, result);
827 count = page_count(p) - 1;
828 if (ps->action == me_swapcache_dirty && result == DELAYED)
829 count--;
830 if (count != 0) {
831 printk(KERN_ERR
832 "MCE %#lx: %s page still referenced by %d users\n",
833 pfn, ps->msg, count);
834 result = FAILED;
837 /* Could do more checks here if page looks ok */
839 * Could adjust zone counters here to correct for the missing page.
842 return (result == RECOVERED || result == DELAYED) ? 0 : -EBUSY;
846 * Do all that is necessary to remove user space mappings. Unmap
847 * the pages and send SIGBUS to the processes if the data was dirty.
849 static int hwpoison_user_mappings(struct page *p, unsigned long pfn,
850 int trapno)
852 enum ttu_flags ttu = TTU_UNMAP | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
853 struct address_space *mapping;
854 LIST_HEAD(tokill);
855 int ret;
856 int kill = 1;
857 struct page *hpage = compound_head(p);
858 struct page *ppage;
860 if (PageReserved(p) || PageSlab(p))
861 return SWAP_SUCCESS;
864 * This check implies we don't kill processes if their pages
865 * are in the swap cache early. Those are always late kills.
867 if (!page_mapped(hpage))
868 return SWAP_SUCCESS;
870 if (PageKsm(p))
871 return SWAP_FAIL;
873 if (PageSwapCache(p)) {
874 printk(KERN_ERR
875 "MCE %#lx: keeping poisoned page in swap cache\n", pfn);
876 ttu |= TTU_IGNORE_HWPOISON;
880 * Propagate the dirty bit from PTEs to struct page first, because we
881 * need this to decide if we should kill or just drop the page.
882 * XXX: the dirty test could be racy: set_page_dirty() may not always
883 * be called inside page lock (it's recommended but not enforced).
885 mapping = page_mapping(hpage);
886 if (!PageDirty(hpage) && mapping &&
887 mapping_cap_writeback_dirty(mapping)) {
888 if (page_mkclean(hpage)) {
889 SetPageDirty(hpage);
890 } else {
891 kill = 0;
892 ttu |= TTU_IGNORE_HWPOISON;
893 printk(KERN_INFO
894 "MCE %#lx: corrupted page was clean: dropped without side effects\n",
895 pfn);
900 * ppage: poisoned page
901 * if p is regular page(4k page)
902 * ppage == real poisoned page;
903 * else p is hugetlb or THP, ppage == head page.
905 ppage = hpage;
907 if (PageTransHuge(hpage)) {
909 * Verify that this isn't a hugetlbfs head page, the check for
910 * PageAnon is just for avoid tripping a split_huge_page
911 * internal debug check, as split_huge_page refuses to deal with
912 * anything that isn't an anon page. PageAnon can't go away fro
913 * under us because we hold a refcount on the hpage, without a
914 * refcount on the hpage. split_huge_page can't be safely called
915 * in the first place, having a refcount on the tail isn't
916 * enough * to be safe.
918 if (!PageHuge(hpage) && PageAnon(hpage)) {
919 if (unlikely(split_huge_page(hpage))) {
921 * FIXME: if splitting THP is failed, it is
922 * better to stop the following operation rather
923 * than causing panic by unmapping. System might
924 * survive if the page is freed later.
926 printk(KERN_INFO
927 "MCE %#lx: failed to split THP\n", pfn);
929 BUG_ON(!PageHWPoison(p));
930 return SWAP_FAIL;
932 /* THP is split, so ppage should be the real poisoned page. */
933 ppage = p;
938 * First collect all the processes that have the page
939 * mapped in dirty form. This has to be done before try_to_unmap,
940 * because ttu takes the rmap data structures down.
942 * Error handling: We ignore errors here because
943 * there's nothing that can be done.
945 if (kill)
946 collect_procs(ppage, &tokill);
948 if (hpage != ppage)
949 lock_page(ppage);
951 ret = try_to_unmap(ppage, ttu);
952 if (ret != SWAP_SUCCESS)
953 printk(KERN_ERR "MCE %#lx: failed to unmap page (mapcount=%d)\n",
954 pfn, page_mapcount(ppage));
956 if (hpage != ppage)
957 unlock_page(ppage);
960 * Now that the dirty bit has been propagated to the
961 * struct page and all unmaps done we can decide if
962 * killing is needed or not. Only kill when the page
963 * was dirty, otherwise the tokill list is merely
964 * freed. When there was a problem unmapping earlier
965 * use a more force-full uncatchable kill to prevent
966 * any accesses to the poisoned memory.
968 kill_procs_ao(&tokill, !!PageDirty(ppage), trapno,
969 ret != SWAP_SUCCESS, p, pfn);
971 return ret;
974 static void set_page_hwpoison_huge_page(struct page *hpage)
976 int i;
977 int nr_pages = 1 << compound_trans_order(hpage);
978 for (i = 0; i < nr_pages; i++)
979 SetPageHWPoison(hpage + i);
982 static void clear_page_hwpoison_huge_page(struct page *hpage)
984 int i;
985 int nr_pages = 1 << compound_trans_order(hpage);
986 for (i = 0; i < nr_pages; i++)
987 ClearPageHWPoison(hpage + i);
990 int __memory_failure(unsigned long pfn, int trapno, int flags)
992 struct page_state *ps;
993 struct page *p;
994 struct page *hpage;
995 int res;
996 unsigned int nr_pages;
998 if (!sysctl_memory_failure_recovery)
999 panic("Memory failure from trap %d on page %lx", trapno, pfn);
1001 if (!pfn_valid(pfn)) {
1002 printk(KERN_ERR
1003 "MCE %#lx: memory outside kernel control\n",
1004 pfn);
1005 return -ENXIO;
1008 p = pfn_to_page(pfn);
1009 hpage = compound_head(p);
1010 if (TestSetPageHWPoison(p)) {
1011 printk(KERN_ERR "MCE %#lx: already hardware poisoned\n", pfn);
1012 return 0;
1015 nr_pages = 1 << compound_trans_order(hpage);
1016 atomic_long_add(nr_pages, &mce_bad_pages);
1019 * We need/can do nothing about count=0 pages.
1020 * 1) it's a free page, and therefore in safe hand:
1021 * prep_new_page() will be the gate keeper.
1022 * 2) it's a free hugepage, which is also safe:
1023 * an affected hugepage will be dequeued from hugepage freelist,
1024 * so there's no concern about reusing it ever after.
1025 * 3) it's part of a non-compound high order page.
1026 * Implies some kernel user: cannot stop them from
1027 * R/W the page; let's pray that the page has been
1028 * used and will be freed some time later.
1029 * In fact it's dangerous to directly bump up page count from 0,
1030 * that may make page_freeze_refs()/page_unfreeze_refs() mismatch.
1032 if (!(flags & MF_COUNT_INCREASED) &&
1033 !get_page_unless_zero(hpage)) {
1034 if (is_free_buddy_page(p)) {
1035 action_result(pfn, "free buddy", DELAYED);
1036 return 0;
1037 } else if (PageHuge(hpage)) {
1039 * Check "just unpoisoned", "filter hit", and
1040 * "race with other subpage."
1042 lock_page(hpage);
1043 if (!PageHWPoison(hpage)
1044 || (hwpoison_filter(p) && TestClearPageHWPoison(p))
1045 || (p != hpage && TestSetPageHWPoison(hpage))) {
1046 atomic_long_sub(nr_pages, &mce_bad_pages);
1047 return 0;
1049 set_page_hwpoison_huge_page(hpage);
1050 res = dequeue_hwpoisoned_huge_page(hpage);
1051 action_result(pfn, "free huge",
1052 res ? IGNORED : DELAYED);
1053 unlock_page(hpage);
1054 return res;
1055 } else {
1056 action_result(pfn, "high order kernel", IGNORED);
1057 return -EBUSY;
1062 * We ignore non-LRU pages for good reasons.
1063 * - PG_locked is only well defined for LRU pages and a few others
1064 * - to avoid races with __set_page_locked()
1065 * - to avoid races with __SetPageSlab*() (and more non-atomic ops)
1066 * The check (unnecessarily) ignores LRU pages being isolated and
1067 * walked by the page reclaim code, however that's not a big loss.
1069 if (!PageHuge(p) && !PageTransCompound(p)) {
1070 if (!PageLRU(p))
1071 shake_page(p, 0);
1072 if (!PageLRU(p)) {
1074 * shake_page could have turned it free.
1076 if (is_free_buddy_page(p)) {
1077 action_result(pfn, "free buddy, 2nd try",
1078 DELAYED);
1079 return 0;
1081 action_result(pfn, "non LRU", IGNORED);
1082 put_page(p);
1083 return -EBUSY;
1088 * Lock the page and wait for writeback to finish.
1089 * It's very difficult to mess with pages currently under IO
1090 * and in many cases impossible, so we just avoid it here.
1092 lock_page(hpage);
1095 * unpoison always clear PG_hwpoison inside page lock
1097 if (!PageHWPoison(p)) {
1098 printk(KERN_ERR "MCE %#lx: just unpoisoned\n", pfn);
1099 res = 0;
1100 goto out;
1102 if (hwpoison_filter(p)) {
1103 if (TestClearPageHWPoison(p))
1104 atomic_long_sub(nr_pages, &mce_bad_pages);
1105 unlock_page(hpage);
1106 put_page(hpage);
1107 return 0;
1111 * For error on the tail page, we should set PG_hwpoison
1112 * on the head page to show that the hugepage is hwpoisoned
1114 if (PageHuge(p) && PageTail(p) && TestSetPageHWPoison(hpage)) {
1115 action_result(pfn, "hugepage already hardware poisoned",
1116 IGNORED);
1117 unlock_page(hpage);
1118 put_page(hpage);
1119 return 0;
1122 * Set PG_hwpoison on all pages in an error hugepage,
1123 * because containment is done in hugepage unit for now.
1124 * Since we have done TestSetPageHWPoison() for the head page with
1125 * page lock held, we can safely set PG_hwpoison bits on tail pages.
1127 if (PageHuge(p))
1128 set_page_hwpoison_huge_page(hpage);
1130 wait_on_page_writeback(p);
1133 * Now take care of user space mappings.
1134 * Abort on fail: __delete_from_page_cache() assumes unmapped page.
1136 if (hwpoison_user_mappings(p, pfn, trapno) != SWAP_SUCCESS) {
1137 printk(KERN_ERR "MCE %#lx: cannot unmap page, give up\n", pfn);
1138 res = -EBUSY;
1139 goto out;
1143 * Torn down by someone else?
1145 if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
1146 action_result(pfn, "already truncated LRU", IGNORED);
1147 res = -EBUSY;
1148 goto out;
1151 res = -EBUSY;
1152 for (ps = error_states;; ps++) {
1153 if ((p->flags & ps->mask) == ps->res) {
1154 res = page_action(ps, p, pfn);
1155 break;
1158 out:
1159 unlock_page(hpage);
1160 return res;
1162 EXPORT_SYMBOL_GPL(__memory_failure);
1165 * memory_failure - Handle memory failure of a page.
1166 * @pfn: Page Number of the corrupted page
1167 * @trapno: Trap number reported in the signal to user space.
1169 * This function is called by the low level machine check code
1170 * of an architecture when it detects hardware memory corruption
1171 * of a page. It tries its best to recover, which includes
1172 * dropping pages, killing processes etc.
1174 * The function is primarily of use for corruptions that
1175 * happen outside the current execution context (e.g. when
1176 * detected by a background scrubber)
1178 * Must run in process context (e.g. a work queue) with interrupts
1179 * enabled and no spinlocks hold.
1181 void memory_failure(unsigned long pfn, int trapno)
1183 __memory_failure(pfn, trapno, 0);
1187 * unpoison_memory - Unpoison a previously poisoned page
1188 * @pfn: Page number of the to be unpoisoned page
1190 * Software-unpoison a page that has been poisoned by
1191 * memory_failure() earlier.
1193 * This is only done on the software-level, so it only works
1194 * for linux injected failures, not real hardware failures
1196 * Returns 0 for success, otherwise -errno.
1198 int unpoison_memory(unsigned long pfn)
1200 struct page *page;
1201 struct page *p;
1202 int freeit = 0;
1203 unsigned int nr_pages;
1205 if (!pfn_valid(pfn))
1206 return -ENXIO;
1208 p = pfn_to_page(pfn);
1209 page = compound_head(p);
1211 if (!PageHWPoison(p)) {
1212 pr_info("MCE: Page was already unpoisoned %#lx\n", pfn);
1213 return 0;
1216 nr_pages = 1 << compound_trans_order(page);
1218 if (!get_page_unless_zero(page)) {
1220 * Since HWPoisoned hugepage should have non-zero refcount,
1221 * race between memory failure and unpoison seems to happen.
1222 * In such case unpoison fails and memory failure runs
1223 * to the end.
1225 if (PageHuge(page)) {
1226 pr_debug("MCE: Memory failure is now running on free hugepage %#lx\n", pfn);
1227 return 0;
1229 if (TestClearPageHWPoison(p))
1230 atomic_long_sub(nr_pages, &mce_bad_pages);
1231 pr_info("MCE: Software-unpoisoned free page %#lx\n", pfn);
1232 return 0;
1235 lock_page(page);
1237 * This test is racy because PG_hwpoison is set outside of page lock.
1238 * That's acceptable because that won't trigger kernel panic. Instead,
1239 * the PG_hwpoison page will be caught and isolated on the entrance to
1240 * the free buddy page pool.
1242 if (TestClearPageHWPoison(page)) {
1243 pr_info("MCE: Software-unpoisoned page %#lx\n", pfn);
1244 atomic_long_sub(nr_pages, &mce_bad_pages);
1245 freeit = 1;
1246 if (PageHuge(page))
1247 clear_page_hwpoison_huge_page(page);
1249 unlock_page(page);
1251 put_page(page);
1252 if (freeit)
1253 put_page(page);
1255 return 0;
1257 EXPORT_SYMBOL(unpoison_memory);
1259 static struct page *new_page(struct page *p, unsigned long private, int **x)
1261 int nid = page_to_nid(p);
1262 if (PageHuge(p))
1263 return alloc_huge_page_node(page_hstate(compound_head(p)),
1264 nid);
1265 else
1266 return alloc_pages_exact_node(nid, GFP_HIGHUSER_MOVABLE, 0);
1270 * Safely get reference count of an arbitrary page.
1271 * Returns 0 for a free page, -EIO for a zero refcount page
1272 * that is not free, and 1 for any other page type.
1273 * For 1 the page is returned with increased page count, otherwise not.
1275 static int get_any_page(struct page *p, unsigned long pfn, int flags)
1277 int ret;
1279 if (flags & MF_COUNT_INCREASED)
1280 return 1;
1283 * The lock_memory_hotplug prevents a race with memory hotplug.
1284 * This is a big hammer, a better would be nicer.
1286 lock_memory_hotplug();
1289 * Isolate the page, so that it doesn't get reallocated if it
1290 * was free.
1292 set_migratetype_isolate(p);
1294 * When the target page is a free hugepage, just remove it
1295 * from free hugepage list.
1297 if (!get_page_unless_zero(compound_head(p))) {
1298 if (PageHuge(p)) {
1299 pr_info("get_any_page: %#lx free huge page\n", pfn);
1300 ret = dequeue_hwpoisoned_huge_page(compound_head(p));
1301 } else if (is_free_buddy_page(p)) {
1302 pr_info("get_any_page: %#lx free buddy page\n", pfn);
1303 /* Set hwpoison bit while page is still isolated */
1304 SetPageHWPoison(p);
1305 ret = 0;
1306 } else {
1307 pr_info("get_any_page: %#lx: unknown zero refcount page type %lx\n",
1308 pfn, p->flags);
1309 ret = -EIO;
1311 } else {
1312 /* Not a free page */
1313 ret = 1;
1315 unset_migratetype_isolate(p);
1316 unlock_memory_hotplug();
1317 return ret;
1320 static int soft_offline_huge_page(struct page *page, int flags)
1322 int ret;
1323 unsigned long pfn = page_to_pfn(page);
1324 struct page *hpage = compound_head(page);
1325 LIST_HEAD(pagelist);
1327 ret = get_any_page(page, pfn, flags);
1328 if (ret < 0)
1329 return ret;
1330 if (ret == 0)
1331 goto done;
1333 if (PageHWPoison(hpage)) {
1334 put_page(hpage);
1335 pr_debug("soft offline: %#lx hugepage already poisoned\n", pfn);
1336 return -EBUSY;
1339 /* Keep page count to indicate a given hugepage is isolated. */
1341 list_add(&hpage->lru, &pagelist);
1342 ret = migrate_huge_pages(&pagelist, new_page, MPOL_MF_MOVE_ALL, 0,
1343 true);
1344 if (ret) {
1345 struct page *page1, *page2;
1346 list_for_each_entry_safe(page1, page2, &pagelist, lru)
1347 put_page(page1);
1349 pr_debug("soft offline: %#lx: migration failed %d, type %lx\n",
1350 pfn, ret, page->flags);
1351 if (ret > 0)
1352 ret = -EIO;
1353 return ret;
1355 done:
1356 if (!PageHWPoison(hpage))
1357 atomic_long_add(1 << compound_trans_order(hpage), &mce_bad_pages);
1358 set_page_hwpoison_huge_page(hpage);
1359 dequeue_hwpoisoned_huge_page(hpage);
1360 /* keep elevated page count for bad page */
1361 return ret;
1365 * soft_offline_page - Soft offline a page.
1366 * @page: page to offline
1367 * @flags: flags. Same as memory_failure().
1369 * Returns 0 on success, otherwise negated errno.
1371 * Soft offline a page, by migration or invalidation,
1372 * without killing anything. This is for the case when
1373 * a page is not corrupted yet (so it's still valid to access),
1374 * but has had a number of corrected errors and is better taken
1375 * out.
1377 * The actual policy on when to do that is maintained by
1378 * user space.
1380 * This should never impact any application or cause data loss,
1381 * however it might take some time.
1383 * This is not a 100% solution for all memory, but tries to be
1384 * ``good enough'' for the majority of memory.
1386 int soft_offline_page(struct page *page, int flags)
1388 int ret;
1389 unsigned long pfn = page_to_pfn(page);
1391 if (PageHuge(page))
1392 return soft_offline_huge_page(page, flags);
1394 ret = get_any_page(page, pfn, flags);
1395 if (ret < 0)
1396 return ret;
1397 if (ret == 0)
1398 goto done;
1401 * Page cache page we can handle?
1403 if (!PageLRU(page)) {
1405 * Try to free it.
1407 put_page(page);
1408 shake_page(page, 1);
1411 * Did it turn free?
1413 ret = get_any_page(page, pfn, 0);
1414 if (ret < 0)
1415 return ret;
1416 if (ret == 0)
1417 goto done;
1419 if (!PageLRU(page)) {
1420 pr_info("soft_offline: %#lx: unknown non LRU page type %lx\n",
1421 pfn, page->flags);
1422 return -EIO;
1425 lock_page(page);
1426 wait_on_page_writeback(page);
1429 * Synchronized using the page lock with memory_failure()
1431 if (PageHWPoison(page)) {
1432 unlock_page(page);
1433 put_page(page);
1434 pr_info("soft offline: %#lx page already poisoned\n", pfn);
1435 return -EBUSY;
1439 * Try to invalidate first. This should work for
1440 * non dirty unmapped page cache pages.
1442 ret = invalidate_inode_page(page);
1443 unlock_page(page);
1446 * Drop count because page migration doesn't like raised
1447 * counts. The page could get re-allocated, but if it becomes
1448 * LRU the isolation will just fail.
1449 * RED-PEN would be better to keep it isolated here, but we
1450 * would need to fix isolation locking first.
1452 put_page(page);
1453 if (ret == 1) {
1454 ret = 0;
1455 pr_info("soft_offline: %#lx: invalidated\n", pfn);
1456 goto done;
1460 * Simple invalidation didn't work.
1461 * Try to migrate to a new page instead. migrate.c
1462 * handles a large number of cases for us.
1464 ret = isolate_lru_page(page);
1465 if (!ret) {
1466 LIST_HEAD(pagelist);
1467 inc_zone_page_state(page, NR_ISOLATED_ANON +
1468 page_is_file_cache(page));
1469 list_add(&page->lru, &pagelist);
1470 ret = migrate_pages(&pagelist, new_page, MPOL_MF_MOVE_ALL,
1471 0, true);
1472 if (ret) {
1473 putback_lru_pages(&pagelist);
1474 pr_info("soft offline: %#lx: migration failed %d, type %lx\n",
1475 pfn, ret, page->flags);
1476 if (ret > 0)
1477 ret = -EIO;
1479 } else {
1480 pr_info("soft offline: %#lx: isolation failed: %d, page count %d, type %lx\n",
1481 pfn, ret, page_count(page), page->flags);
1483 if (ret)
1484 return ret;
1486 done:
1487 atomic_long_add(1, &mce_bad_pages);
1488 SetPageHWPoison(page);
1489 /* keep elevated page count for bad page */
1490 return ret;