aboutsummaryrefslogtreecommitdiffstats
path: root/mm/memory-failure.c
diff options
context:
space:
mode:
authorAndrea Bastoni <bastoni@cs.unc.edu>2010-05-30 19:16:45 -0400
committerAndrea Bastoni <bastoni@cs.unc.edu>2010-05-30 19:16:45 -0400
commitada47b5fe13d89735805b566185f4885f5a3f750 (patch)
tree644b88f8a71896307d71438e9b3af49126ffb22b /mm/memory-failure.c
parent43e98717ad40a4ae64545b5ba047c7b86aa44f4f (diff)
parent3280f21d43ee541f97f8cda5792150d2dbec20d5 (diff)
Merge branch 'wip-2.6.34' into old-private-masterarchived-private-master
Diffstat (limited to 'mm/memory-failure.c')
-rw-r--r--mm/memory-failure.c579
1 files changed, 521 insertions, 58 deletions
diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index dacc64183874..620b0b461593 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -34,12 +34,17 @@
34#include <linux/kernel.h> 34#include <linux/kernel.h>
35#include <linux/mm.h> 35#include <linux/mm.h>
36#include <linux/page-flags.h> 36#include <linux/page-flags.h>
37#include <linux/kernel-page-flags.h>
37#include <linux/sched.h> 38#include <linux/sched.h>
38#include <linux/ksm.h> 39#include <linux/ksm.h>
39#include <linux/rmap.h> 40#include <linux/rmap.h>
40#include <linux/pagemap.h> 41#include <linux/pagemap.h>
41#include <linux/swap.h> 42#include <linux/swap.h>
42#include <linux/backing-dev.h> 43#include <linux/backing-dev.h>
44#include <linux/migrate.h>
45#include <linux/page-isolation.h>
46#include <linux/suspend.h>
47#include <linux/slab.h>
43#include "internal.h" 48#include "internal.h"
44 49
45int sysctl_memory_failure_early_kill __read_mostly = 0; 50int sysctl_memory_failure_early_kill __read_mostly = 0;
@@ -48,6 +53,129 @@ int sysctl_memory_failure_recovery __read_mostly = 1;
48 53
49atomic_long_t mce_bad_pages __read_mostly = ATOMIC_LONG_INIT(0); 54atomic_long_t mce_bad_pages __read_mostly = ATOMIC_LONG_INIT(0);
50 55
56#if defined(CONFIG_HWPOISON_INJECT) || defined(CONFIG_HWPOISON_INJECT_MODULE)
57
58u32 hwpoison_filter_enable = 0;
59u32 hwpoison_filter_dev_major = ~0U;
60u32 hwpoison_filter_dev_minor = ~0U;
61u64 hwpoison_filter_flags_mask;
62u64 hwpoison_filter_flags_value;
63EXPORT_SYMBOL_GPL(hwpoison_filter_enable);
64EXPORT_SYMBOL_GPL(hwpoison_filter_dev_major);
65EXPORT_SYMBOL_GPL(hwpoison_filter_dev_minor);
66EXPORT_SYMBOL_GPL(hwpoison_filter_flags_mask);
67EXPORT_SYMBOL_GPL(hwpoison_filter_flags_value);
68
69static int hwpoison_filter_dev(struct page *p)
70{
71 struct address_space *mapping;
72 dev_t dev;
73
74 if (hwpoison_filter_dev_major == ~0U &&
75 hwpoison_filter_dev_minor == ~0U)
76 return 0;
77
78 /*
79 * page_mapping() does not accept slab page
80 */
81 if (PageSlab(p))
82 return -EINVAL;
83
84 mapping = page_mapping(p);
85 if (mapping == NULL || mapping->host == NULL)
86 return -EINVAL;
87
88 dev = mapping->host->i_sb->s_dev;
89 if (hwpoison_filter_dev_major != ~0U &&
90 hwpoison_filter_dev_major != MAJOR(dev))
91 return -EINVAL;
92 if (hwpoison_filter_dev_minor != ~0U &&
93 hwpoison_filter_dev_minor != MINOR(dev))
94 return -EINVAL;
95
96 return 0;
97}
98
99static int hwpoison_filter_flags(struct page *p)
100{
101 if (!hwpoison_filter_flags_mask)
102 return 0;
103
104 if ((stable_page_flags(p) & hwpoison_filter_flags_mask) ==
105 hwpoison_filter_flags_value)
106 return 0;
107 else
108 return -EINVAL;
109}
110
111/*
112 * This allows stress tests to limit test scope to a collection of tasks
113 * by putting them under some memcg. This prevents killing unrelated/important
114 * processes such as /sbin/init. Note that the target task may share clean
115 * pages with init (eg. libc text), which is harmless. If the target task
116 * share _dirty_ pages with another task B, the test scheme must make sure B
117 * is also included in the memcg. At last, due to race conditions this filter
118 * can only guarantee that the page either belongs to the memcg tasks, or is
119 * a freed page.
120 */
121#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
122u64 hwpoison_filter_memcg;
123EXPORT_SYMBOL_GPL(hwpoison_filter_memcg);
124static int hwpoison_filter_task(struct page *p)
125{
126 struct mem_cgroup *mem;
127 struct cgroup_subsys_state *css;
128 unsigned long ino;
129
130 if (!hwpoison_filter_memcg)
131 return 0;
132
133 mem = try_get_mem_cgroup_from_page(p);
134 if (!mem)
135 return -EINVAL;
136
137 css = mem_cgroup_css(mem);
138 /* root_mem_cgroup has NULL dentries */
139 if (!css->cgroup->dentry)
140 return -EINVAL;
141
142 ino = css->cgroup->dentry->d_inode->i_ino;
143 css_put(css);
144
145 if (ino != hwpoison_filter_memcg)
146 return -EINVAL;
147
148 return 0;
149}
150#else
151static int hwpoison_filter_task(struct page *p) { return 0; }
152#endif
153
154int hwpoison_filter(struct page *p)
155{
156 if (!hwpoison_filter_enable)
157 return 0;
158
159 if (hwpoison_filter_dev(p))
160 return -EINVAL;
161
162 if (hwpoison_filter_flags(p))
163 return -EINVAL;
164
165 if (hwpoison_filter_task(p))
166 return -EINVAL;
167
168 return 0;
169}
170#else
171int hwpoison_filter(struct page *p)
172{
173 return 0;
174}
175#endif
176
177EXPORT_SYMBOL_GPL(hwpoison_filter);
178
51/* 179/*
52 * Send all the processes who have the page mapped an ``action optional'' 180 * Send all the processes who have the page mapped an ``action optional''
53 * signal. 181 * signal.
@@ -83,6 +211,36 @@ static int kill_proc_ao(struct task_struct *t, unsigned long addr, int trapno,
83} 211}
84 212
85/* 213/*
214 * When a unknown page type is encountered drain as many buffers as possible
215 * in the hope to turn the page into a LRU or free page, which we can handle.
216 */
217void shake_page(struct page *p, int access)
218{
219 if (!PageSlab(p)) {
220 lru_add_drain_all();
221 if (PageLRU(p))
222 return;
223 drain_all_pages();
224 if (PageLRU(p) || is_free_buddy_page(p))
225 return;
226 }
227
228 /*
229 * Only all shrink_slab here (which would also
230 * shrink other caches) if access is not potentially fatal.
231 */
232 if (access) {
233 int nr;
234 do {
235 nr = shrink_slab(1000, GFP_KERNEL, 1000);
236 if (page_count(p) == 0)
237 break;
238 } while (nr > 10);
239 }
240}
241EXPORT_SYMBOL_GPL(shake_page);
242
243/*
86 * Kill all processes that have a poisoned page mapped and then isolate 244 * Kill all processes that have a poisoned page mapped and then isolate
87 * the page. 245 * the page.
88 * 246 *
@@ -174,10 +332,9 @@ static void kill_procs_ao(struct list_head *to_kill, int doit, int trapno,
174 list_for_each_entry_safe (tk, next, to_kill, nd) { 332 list_for_each_entry_safe (tk, next, to_kill, nd) {
175 if (doit) { 333 if (doit) {
176 /* 334 /*
177 * In case something went wrong with munmaping 335 * In case something went wrong with munmapping
178 * make sure the process doesn't catch the 336 * make sure the process doesn't catch the
179 * signal and then access the memory. Just kill it. 337 * signal and then access the memory. Just kill it.
180 * the signal handlers
181 */ 338 */
182 if (fail || tk->addr_valid == 0) { 339 if (fail || tk->addr_valid == 0) {
183 printk(KERN_ERR 340 printk(KERN_ERR
@@ -227,9 +384,12 @@ static void collect_procs_anon(struct page *page, struct list_head *to_kill,
227 if (av == NULL) /* Not actually mapped anymore */ 384 if (av == NULL) /* Not actually mapped anymore */
228 goto out; 385 goto out;
229 for_each_process (tsk) { 386 for_each_process (tsk) {
387 struct anon_vma_chain *vmac;
388
230 if (!task_early_kill(tsk)) 389 if (!task_early_kill(tsk))
231 continue; 390 continue;
232 list_for_each_entry (vma, &av->head, anon_vma_node) { 391 list_for_each_entry(vmac, &av->head, same_anon_vma) {
392 vma = vmac->vma;
233 if (!page_mapped_in_vma(page, vma)) 393 if (!page_mapped_in_vma(page, vma))
234 continue; 394 continue;
235 if (vma->vm_mm == tsk->mm) 395 if (vma->vm_mm == tsk->mm)
@@ -314,33 +474,49 @@ static void collect_procs(struct page *page, struct list_head *tokill)
314 */ 474 */
315 475
316enum outcome { 476enum outcome {
317 FAILED, /* Error handling failed */ 477 IGNORED, /* Error: cannot be handled */
478 FAILED, /* Error: handling failed */
318 DELAYED, /* Will be handled later */ 479 DELAYED, /* Will be handled later */
319 IGNORED, /* Error safely ignored */
320 RECOVERED, /* Successfully recovered */ 480 RECOVERED, /* Successfully recovered */
321}; 481};
322 482
323static const char *action_name[] = { 483static const char *action_name[] = {
484 [IGNORED] = "Ignored",
324 [FAILED] = "Failed", 485 [FAILED] = "Failed",
325 [DELAYED] = "Delayed", 486 [DELAYED] = "Delayed",
326 [IGNORED] = "Ignored",
327 [RECOVERED] = "Recovered", 487 [RECOVERED] = "Recovered",
328}; 488};
329 489
330/* 490/*
331 * Error hit kernel page. 491 * XXX: It is possible that a page is isolated from LRU cache,
332 * Do nothing, try to be lucky and not touch this instead. For a few cases we 492 * and then kept in swap cache or failed to remove from page cache.
333 * could be more sophisticated. 493 * The page count will stop it from being freed by unpoison.
494 * Stress tests should be aware of this memory leak problem.
334 */ 495 */
335static int me_kernel(struct page *p, unsigned long pfn) 496static int delete_from_lru_cache(struct page *p)
336{ 497{
337 return DELAYED; 498 if (!isolate_lru_page(p)) {
499 /*
500 * Clear sensible page flags, so that the buddy system won't
501 * complain when the page is unpoison-and-freed.
502 */
503 ClearPageActive(p);
504 ClearPageUnevictable(p);
505 /*
506 * drop the page count elevated by isolate_lru_page()
507 */
508 page_cache_release(p);
509 return 0;
510 }
511 return -EIO;
338} 512}
339 513
340/* 514/*
341 * Already poisoned page. 515 * Error hit kernel page.
516 * Do nothing, try to be lucky and not touch this instead. For a few cases we
517 * could be more sophisticated.
342 */ 518 */
343static int me_ignore(struct page *p, unsigned long pfn) 519static int me_kernel(struct page *p, unsigned long pfn)
344{ 520{
345 return IGNORED; 521 return IGNORED;
346} 522}
@@ -355,14 +531,6 @@ static int me_unknown(struct page *p, unsigned long pfn)
355} 531}
356 532
357/* 533/*
358 * Free memory
359 */
360static int me_free(struct page *p, unsigned long pfn)
361{
362 return DELAYED;
363}
364
365/*
366 * Clean (or cleaned) page cache page. 534 * Clean (or cleaned) page cache page.
367 */ 535 */
368static int me_pagecache_clean(struct page *p, unsigned long pfn) 536static int me_pagecache_clean(struct page *p, unsigned long pfn)
@@ -371,6 +539,8 @@ static int me_pagecache_clean(struct page *p, unsigned long pfn)
371 int ret = FAILED; 539 int ret = FAILED;
372 struct address_space *mapping; 540 struct address_space *mapping;
373 541
542 delete_from_lru_cache(p);
543
374 /* 544 /*
375 * For anonymous pages we're done the only reference left 545 * For anonymous pages we're done the only reference left
376 * should be the one m_f() holds. 546 * should be the one m_f() holds.
@@ -500,14 +670,20 @@ static int me_swapcache_dirty(struct page *p, unsigned long pfn)
500 /* Trigger EIO in shmem: */ 670 /* Trigger EIO in shmem: */
501 ClearPageUptodate(p); 671 ClearPageUptodate(p);
502 672
503 return DELAYED; 673 if (!delete_from_lru_cache(p))
674 return DELAYED;
675 else
676 return FAILED;
504} 677}
505 678
506static int me_swapcache_clean(struct page *p, unsigned long pfn) 679static int me_swapcache_clean(struct page *p, unsigned long pfn)
507{ 680{
508 delete_from_swap_cache(p); 681 delete_from_swap_cache(p);
509 682
510 return RECOVERED; 683 if (!delete_from_lru_cache(p))
684 return RECOVERED;
685 else
686 return FAILED;
511} 687}
512 688
513/* 689/*
@@ -550,7 +726,6 @@ static int me_huge_page(struct page *p, unsigned long pfn)
550#define tail (1UL << PG_tail) 726#define tail (1UL << PG_tail)
551#define compound (1UL << PG_compound) 727#define compound (1UL << PG_compound)
552#define slab (1UL << PG_slab) 728#define slab (1UL << PG_slab)
553#define buddy (1UL << PG_buddy)
554#define reserved (1UL << PG_reserved) 729#define reserved (1UL << PG_reserved)
555 730
556static struct page_state { 731static struct page_state {
@@ -559,8 +734,11 @@ static struct page_state {
559 char *msg; 734 char *msg;
560 int (*action)(struct page *p, unsigned long pfn); 735 int (*action)(struct page *p, unsigned long pfn);
561} error_states[] = { 736} error_states[] = {
562 { reserved, reserved, "reserved kernel", me_ignore }, 737 { reserved, reserved, "reserved kernel", me_kernel },
563 { buddy, buddy, "free kernel", me_free }, 738 /*
739 * free pages are specially detected outside this table:
740 * PG_buddy pages only make a small fraction of all free pages.
741 */
564 742
565 /* 743 /*
566 * Could in theory check if slab page is free or if we can drop 744 * Could in theory check if slab page is free or if we can drop
@@ -582,14 +760,11 @@ static struct page_state {
582 { unevict|dirty, unevict|dirty, "unevictable LRU", me_pagecache_dirty}, 760 { unevict|dirty, unevict|dirty, "unevictable LRU", me_pagecache_dirty},
583 { unevict, unevict, "unevictable LRU", me_pagecache_clean}, 761 { unevict, unevict, "unevictable LRU", me_pagecache_clean},
584 762
585#ifdef CONFIG_HAVE_MLOCKED_PAGE_BIT
586 { mlock|dirty, mlock|dirty, "mlocked LRU", me_pagecache_dirty }, 763 { mlock|dirty, mlock|dirty, "mlocked LRU", me_pagecache_dirty },
587 { mlock, mlock, "mlocked LRU", me_pagecache_clean }, 764 { mlock, mlock, "mlocked LRU", me_pagecache_clean },
588#endif
589 765
590 { lru|dirty, lru|dirty, "LRU", me_pagecache_dirty }, 766 { lru|dirty, lru|dirty, "LRU", me_pagecache_dirty },
591 { lru|dirty, lru, "clean LRU", me_pagecache_clean }, 767 { lru|dirty, lru, "clean LRU", me_pagecache_clean },
592 { swapbacked, swapbacked, "anonymous", me_pagecache_clean },
593 768
594 /* 769 /*
595 * Catchall entry: must be at end. 770 * Catchall entry: must be at end.
@@ -597,20 +772,31 @@ static struct page_state {
597 { 0, 0, "unknown page state", me_unknown }, 772 { 0, 0, "unknown page state", me_unknown },
598}; 773};
599 774
775#undef dirty
776#undef sc
777#undef unevict
778#undef mlock
779#undef writeback
780#undef lru
781#undef swapbacked
782#undef head
783#undef tail
784#undef compound
785#undef slab
786#undef reserved
787
600static void action_result(unsigned long pfn, char *msg, int result) 788static void action_result(unsigned long pfn, char *msg, int result)
601{ 789{
602 struct page *page = NULL; 790 struct page *page = pfn_to_page(pfn);
603 if (pfn_valid(pfn))
604 page = pfn_to_page(pfn);
605 791
606 printk(KERN_ERR "MCE %#lx: %s%s page recovery: %s\n", 792 printk(KERN_ERR "MCE %#lx: %s%s page recovery: %s\n",
607 pfn, 793 pfn,
608 page && PageDirty(page) ? "dirty " : "", 794 PageDirty(page) ? "dirty " : "",
609 msg, action_name[result]); 795 msg, action_name[result]);
610} 796}
611 797
612static int page_action(struct page_state *ps, struct page *p, 798static int page_action(struct page_state *ps, struct page *p,
613 unsigned long pfn, int ref) 799 unsigned long pfn)
614{ 800{
615 int result; 801 int result;
616 int count; 802 int count;
@@ -618,18 +804,22 @@ static int page_action(struct page_state *ps, struct page *p,
618 result = ps->action(p, pfn); 804 result = ps->action(p, pfn);
619 action_result(pfn, ps->msg, result); 805 action_result(pfn, ps->msg, result);
620 806
621 count = page_count(p) - 1 - ref; 807 count = page_count(p) - 1;
622 if (count != 0) 808 if (ps->action == me_swapcache_dirty && result == DELAYED)
809 count--;
810 if (count != 0) {
623 printk(KERN_ERR 811 printk(KERN_ERR
624 "MCE %#lx: %s page still referenced by %d users\n", 812 "MCE %#lx: %s page still referenced by %d users\n",
625 pfn, ps->msg, count); 813 pfn, ps->msg, count);
814 result = FAILED;
815 }
626 816
627 /* Could do more checks here if page looks ok */ 817 /* Could do more checks here if page looks ok */
628 /* 818 /*
629 * Could adjust zone counters here to correct for the missing page. 819 * Could adjust zone counters here to correct for the missing page.
630 */ 820 */
631 821
632 return result == RECOVERED ? 0 : -EBUSY; 822 return (result == RECOVERED || result == DELAYED) ? 0 : -EBUSY;
633} 823}
634 824
635#define N_UNMAP_TRIES 5 825#define N_UNMAP_TRIES 5
@@ -638,7 +828,7 @@ static int page_action(struct page_state *ps, struct page *p,
638 * Do all that is necessary to remove user space mappings. Unmap 828 * Do all that is necessary to remove user space mappings. Unmap
639 * the pages and send SIGBUS to the processes if the data was dirty. 829 * the pages and send SIGBUS to the processes if the data was dirty.
640 */ 830 */
641static void hwpoison_user_mappings(struct page *p, unsigned long pfn, 831static int hwpoison_user_mappings(struct page *p, unsigned long pfn,
642 int trapno) 832 int trapno)
643{ 833{
644 enum ttu_flags ttu = TTU_UNMAP | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS; 834 enum ttu_flags ttu = TTU_UNMAP | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
@@ -648,15 +838,18 @@ static void hwpoison_user_mappings(struct page *p, unsigned long pfn,
648 int i; 838 int i;
649 int kill = 1; 839 int kill = 1;
650 840
651 if (PageReserved(p) || PageCompound(p) || PageSlab(p) || PageKsm(p)) 841 if (PageReserved(p) || PageSlab(p))
652 return; 842 return SWAP_SUCCESS;
653 843
654 /* 844 /*
655 * This check implies we don't kill processes if their pages 845 * This check implies we don't kill processes if their pages
656 * are in the swap cache early. Those are always late kills. 846 * are in the swap cache early. Those are always late kills.
657 */ 847 */
658 if (!page_mapped(p)) 848 if (!page_mapped(p))
659 return; 849 return SWAP_SUCCESS;
850
851 if (PageCompound(p) || PageKsm(p))
852 return SWAP_FAIL;
660 853
661 if (PageSwapCache(p)) { 854 if (PageSwapCache(p)) {
662 printk(KERN_ERR 855 printk(KERN_ERR
@@ -667,6 +860,8 @@ static void hwpoison_user_mappings(struct page *p, unsigned long pfn,
667 /* 860 /*
668 * Propagate the dirty bit from PTEs to struct page first, because we 861 * Propagate the dirty bit from PTEs to struct page first, because we
669 * need this to decide if we should kill or just drop the page. 862 * need this to decide if we should kill or just drop the page.
863 * XXX: the dirty test could be racy: set_page_dirty() may not always
864 * be called inside page lock (it's recommended but not enforced).
670 */ 865 */
671 mapping = page_mapping(p); 866 mapping = page_mapping(p);
672 if (!PageDirty(p) && mapping && mapping_cap_writeback_dirty(mapping)) { 867 if (!PageDirty(p) && mapping && mapping_cap_writeback_dirty(mapping)) {
@@ -718,11 +913,12 @@ static void hwpoison_user_mappings(struct page *p, unsigned long pfn,
718 */ 913 */
719 kill_procs_ao(&tokill, !!PageDirty(p), trapno, 914 kill_procs_ao(&tokill, !!PageDirty(p), trapno,
720 ret != SWAP_SUCCESS, pfn); 915 ret != SWAP_SUCCESS, pfn);
916
917 return ret;
721} 918}
722 919
723int __memory_failure(unsigned long pfn, int trapno, int ref) 920int __memory_failure(unsigned long pfn, int trapno, int flags)
724{ 921{
725 unsigned long lru_flag;
726 struct page_state *ps; 922 struct page_state *ps;
727 struct page *p; 923 struct page *p;
728 int res; 924 int res;
@@ -731,13 +927,15 @@ int __memory_failure(unsigned long pfn, int trapno, int ref)
731 panic("Memory failure from trap %d on page %lx", trapno, pfn); 927 panic("Memory failure from trap %d on page %lx", trapno, pfn);
732 928
733 if (!pfn_valid(pfn)) { 929 if (!pfn_valid(pfn)) {
734 action_result(pfn, "memory outside kernel control", IGNORED); 930 printk(KERN_ERR
735 return -EIO; 931 "MCE %#lx: memory outside kernel control\n",
932 pfn);
933 return -ENXIO;
736 } 934 }
737 935
738 p = pfn_to_page(pfn); 936 p = pfn_to_page(pfn);
739 if (TestSetPageHWPoison(p)) { 937 if (TestSetPageHWPoison(p)) {
740 action_result(pfn, "already hardware poisoned", IGNORED); 938 printk(KERN_ERR "MCE %#lx: already hardware poisoned\n", pfn);
741 return 0; 939 return 0;
742 } 940 }
743 941
@@ -754,9 +952,15 @@ int __memory_failure(unsigned long pfn, int trapno, int ref)
754 * In fact it's dangerous to directly bump up page count from 0, 952 * In fact it's dangerous to directly bump up page count from 0,
755 * that may make page_freeze_refs()/page_unfreeze_refs() mismatch. 953 * that may make page_freeze_refs()/page_unfreeze_refs() mismatch.
756 */ 954 */
757 if (!get_page_unless_zero(compound_head(p))) { 955 if (!(flags & MF_COUNT_INCREASED) &&
758 action_result(pfn, "free or high order kernel", IGNORED); 956 !get_page_unless_zero(compound_head(p))) {
759 return PageBuddy(compound_head(p)) ? 0 : -EBUSY; 957 if (is_free_buddy_page(p)) {
958 action_result(pfn, "free buddy", DELAYED);
959 return 0;
960 } else {
961 action_result(pfn, "high order kernel", IGNORED);
962 return -EBUSY;
963 }
760 } 964 }
761 965
762 /* 966 /*
@@ -768,14 +972,19 @@ int __memory_failure(unsigned long pfn, int trapno, int ref)
768 * walked by the page reclaim code, however that's not a big loss. 972 * walked by the page reclaim code, however that's not a big loss.
769 */ 973 */
770 if (!PageLRU(p)) 974 if (!PageLRU(p))
771 lru_add_drain_all(); 975 shake_page(p, 0);
772 lru_flag = p->flags & lru; 976 if (!PageLRU(p)) {
773 if (isolate_lru_page(p)) { 977 /*
978 * shake_page could have turned it free.
979 */
980 if (is_free_buddy_page(p)) {
981 action_result(pfn, "free buddy, 2nd try", DELAYED);
982 return 0;
983 }
774 action_result(pfn, "non LRU", IGNORED); 984 action_result(pfn, "non LRU", IGNORED);
775 put_page(p); 985 put_page(p);
776 return -EBUSY; 986 return -EBUSY;
777 } 987 }
778 page_cache_release(p);
779 988
780 /* 989 /*
781 * Lock the page and wait for writeback to finish. 990 * Lock the page and wait for writeback to finish.
@@ -783,26 +992,48 @@ int __memory_failure(unsigned long pfn, int trapno, int ref)
783 * and in many cases impossible, so we just avoid it here. 992 * and in many cases impossible, so we just avoid it here.
784 */ 993 */
785 lock_page_nosync(p); 994 lock_page_nosync(p);
995
996 /*
997 * unpoison always clear PG_hwpoison inside page lock
998 */
999 if (!PageHWPoison(p)) {
1000 printk(KERN_ERR "MCE %#lx: just unpoisoned\n", pfn);
1001 res = 0;
1002 goto out;
1003 }
1004 if (hwpoison_filter(p)) {
1005 if (TestClearPageHWPoison(p))
1006 atomic_long_dec(&mce_bad_pages);
1007 unlock_page(p);
1008 put_page(p);
1009 return 0;
1010 }
1011
786 wait_on_page_writeback(p); 1012 wait_on_page_writeback(p);
787 1013
788 /* 1014 /*
789 * Now take care of user space mappings. 1015 * Now take care of user space mappings.
1016 * Abort on fail: __remove_from_page_cache() assumes unmapped page.
790 */ 1017 */
791 hwpoison_user_mappings(p, pfn, trapno); 1018 if (hwpoison_user_mappings(p, pfn, trapno) != SWAP_SUCCESS) {
1019 printk(KERN_ERR "MCE %#lx: cannot unmap page, give up\n", pfn);
1020 res = -EBUSY;
1021 goto out;
1022 }
792 1023
793 /* 1024 /*
794 * Torn down by someone else? 1025 * Torn down by someone else?
795 */ 1026 */
796 if ((lru_flag & lru) && !PageSwapCache(p) && p->mapping == NULL) { 1027 if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
797 action_result(pfn, "already truncated LRU", IGNORED); 1028 action_result(pfn, "already truncated LRU", IGNORED);
798 res = 0; 1029 res = -EBUSY;
799 goto out; 1030 goto out;
800 } 1031 }
801 1032
802 res = -EBUSY; 1033 res = -EBUSY;
803 for (ps = error_states;; ps++) { 1034 for (ps = error_states;; ps++) {
804 if (((p->flags | lru_flag)& ps->mask) == ps->res) { 1035 if ((p->flags & ps->mask) == ps->res) {
805 res = page_action(ps, p, pfn, ref); 1036 res = page_action(ps, p, pfn);
806 break; 1037 break;
807 } 1038 }
808 } 1039 }
@@ -833,3 +1064,235 @@ void memory_failure(unsigned long pfn, int trapno)
833{ 1064{
834 __memory_failure(pfn, trapno, 0); 1065 __memory_failure(pfn, trapno, 0);
835} 1066}
1067
1068/**
1069 * unpoison_memory - Unpoison a previously poisoned page
1070 * @pfn: Page number of the to be unpoisoned page
1071 *
1072 * Software-unpoison a page that has been poisoned by
1073 * memory_failure() earlier.
1074 *
1075 * This is only done on the software-level, so it only works
1076 * for linux injected failures, not real hardware failures
1077 *
1078 * Returns 0 for success, otherwise -errno.
1079 */
1080int unpoison_memory(unsigned long pfn)
1081{
1082 struct page *page;
1083 struct page *p;
1084 int freeit = 0;
1085
1086 if (!pfn_valid(pfn))
1087 return -ENXIO;
1088
1089 p = pfn_to_page(pfn);
1090 page = compound_head(p);
1091
1092 if (!PageHWPoison(p)) {
1093 pr_debug("MCE: Page was already unpoisoned %#lx\n", pfn);
1094 return 0;
1095 }
1096
1097 if (!get_page_unless_zero(page)) {
1098 if (TestClearPageHWPoison(p))
1099 atomic_long_dec(&mce_bad_pages);
1100 pr_debug("MCE: Software-unpoisoned free page %#lx\n", pfn);
1101 return 0;
1102 }
1103
1104 lock_page_nosync(page);
1105 /*
1106 * This test is racy because PG_hwpoison is set outside of page lock.
1107 * That's acceptable because that won't trigger kernel panic. Instead,
1108 * the PG_hwpoison page will be caught and isolated on the entrance to
1109 * the free buddy page pool.
1110 */
1111 if (TestClearPageHWPoison(p)) {
1112 pr_debug("MCE: Software-unpoisoned page %#lx\n", pfn);
1113 atomic_long_dec(&mce_bad_pages);
1114 freeit = 1;
1115 }
1116 unlock_page(page);
1117
1118 put_page(page);
1119 if (freeit)
1120 put_page(page);
1121
1122 return 0;
1123}
1124EXPORT_SYMBOL(unpoison_memory);
1125
1126static struct page *new_page(struct page *p, unsigned long private, int **x)
1127{
1128 int nid = page_to_nid(p);
1129 return alloc_pages_exact_node(nid, GFP_HIGHUSER_MOVABLE, 0);
1130}
1131
1132/*
1133 * Safely get reference count of an arbitrary page.
1134 * Returns 0 for a free page, -EIO for a zero refcount page
1135 * that is not free, and 1 for any other page type.
1136 * For 1 the page is returned with increased page count, otherwise not.
1137 */
1138static int get_any_page(struct page *p, unsigned long pfn, int flags)
1139{
1140 int ret;
1141
1142 if (flags & MF_COUNT_INCREASED)
1143 return 1;
1144
1145 /*
1146 * The lock_system_sleep prevents a race with memory hotplug,
1147 * because the isolation assumes there's only a single user.
1148 * This is a big hammer, a better would be nicer.
1149 */
1150 lock_system_sleep();
1151
1152 /*
1153 * Isolate the page, so that it doesn't get reallocated if it
1154 * was free.
1155 */
1156 set_migratetype_isolate(p);
1157 if (!get_page_unless_zero(compound_head(p))) {
1158 if (is_free_buddy_page(p)) {
1159 pr_debug("get_any_page: %#lx free buddy page\n", pfn);
1160 /* Set hwpoison bit while page is still isolated */
1161 SetPageHWPoison(p);
1162 ret = 0;
1163 } else {
1164 pr_debug("get_any_page: %#lx: unknown zero refcount page type %lx\n",
1165 pfn, p->flags);
1166 ret = -EIO;
1167 }
1168 } else {
1169 /* Not a free page */
1170 ret = 1;
1171 }
1172 unset_migratetype_isolate(p);
1173 unlock_system_sleep();
1174 return ret;
1175}
1176
1177/**
1178 * soft_offline_page - Soft offline a page.
1179 * @page: page to offline
1180 * @flags: flags. Same as memory_failure().
1181 *
1182 * Returns 0 on success, otherwise negated errno.
1183 *
1184 * Soft offline a page, by migration or invalidation,
1185 * without killing anything. This is for the case when
1186 * a page is not corrupted yet (so it's still valid to access),
1187 * but has had a number of corrected errors and is better taken
1188 * out.
1189 *
1190 * The actual policy on when to do that is maintained by
1191 * user space.
1192 *
1193 * This should never impact any application or cause data loss,
1194 * however it might take some time.
1195 *
1196 * This is not a 100% solution for all memory, but tries to be
1197 * ``good enough'' for the majority of memory.
1198 */
1199int soft_offline_page(struct page *page, int flags)
1200{
1201 int ret;
1202 unsigned long pfn = page_to_pfn(page);
1203
1204 ret = get_any_page(page, pfn, flags);
1205 if (ret < 0)
1206 return ret;
1207 if (ret == 0)
1208 goto done;
1209
1210 /*
1211 * Page cache page we can handle?
1212 */
1213 if (!PageLRU(page)) {
1214 /*
1215 * Try to free it.
1216 */
1217 put_page(page);
1218 shake_page(page, 1);
1219
1220 /*
1221 * Did it turn free?
1222 */
1223 ret = get_any_page(page, pfn, 0);
1224 if (ret < 0)
1225 return ret;
1226 if (ret == 0)
1227 goto done;
1228 }
1229 if (!PageLRU(page)) {
1230 pr_debug("soft_offline: %#lx: unknown non LRU page type %lx\n",
1231 pfn, page->flags);
1232 return -EIO;
1233 }
1234
1235 lock_page(page);
1236 wait_on_page_writeback(page);
1237
1238 /*
1239 * Synchronized using the page lock with memory_failure()
1240 */
1241 if (PageHWPoison(page)) {
1242 unlock_page(page);
1243 put_page(page);
1244 pr_debug("soft offline: %#lx page already poisoned\n", pfn);
1245 return -EBUSY;
1246 }
1247
1248 /*
1249 * Try to invalidate first. This should work for
1250 * non dirty unmapped page cache pages.
1251 */
1252 ret = invalidate_inode_page(page);
1253 unlock_page(page);
1254
1255 /*
1256 * Drop count because page migration doesn't like raised
1257 * counts. The page could get re-allocated, but if it becomes
1258 * LRU the isolation will just fail.
1259 * RED-PEN would be better to keep it isolated here, but we
1260 * would need to fix isolation locking first.
1261 */
1262 put_page(page);
1263 if (ret == 1) {
1264 ret = 0;
1265 pr_debug("soft_offline: %#lx: invalidated\n", pfn);
1266 goto done;
1267 }
1268
1269 /*
1270 * Simple invalidation didn't work.
1271 * Try to migrate to a new page instead. migrate.c
1272 * handles a large number of cases for us.
1273 */
1274 ret = isolate_lru_page(page);
1275 if (!ret) {
1276 LIST_HEAD(pagelist);
1277
1278 list_add(&page->lru, &pagelist);
1279 ret = migrate_pages(&pagelist, new_page, MPOL_MF_MOVE_ALL, 0);
1280 if (ret) {
1281 pr_debug("soft offline: %#lx: migration failed %d, type %lx\n",
1282 pfn, ret, page->flags);
1283 if (ret > 0)
1284 ret = -EIO;
1285 }
1286 } else {
1287 pr_debug("soft offline: %#lx: isolation failed: %d, page count %d, type %lx\n",
1288 pfn, ret, page_count(page), page->flags);
1289 }
1290 if (ret)
1291 return ret;
1292
1293done:
1294 atomic_long_add(1, &mce_bad_pages);
1295 SetPageHWPoison(page);
1296 /* keep elevated page count for bad page */
1297 return ret;
1298}