HWPOISON, hugetlb: isolate corrupted hugepage
[linux-2.6-block.git] / mm / memory-failure.c
1 /*
2  * Copyright (C) 2008, 2009 Intel Corporation
3  * Authors: Andi Kleen, Fengguang Wu
4  *
5  * This software may be redistributed and/or modified under the terms of
6  * the GNU General Public License ("GPL") version 2 only as published by the
7  * Free Software Foundation.
8  *
9  * High level machine check handler. Handles pages reported by the
10  * hardware as being corrupted usually due to a 2bit ECC memory or cache
11  * failure.
12  *
13  * Handles page cache pages in various states.  The tricky part
14  * here is that we can access any page asynchronous to other VM
15  * users, because memory failures could happen anytime and anywhere,
16  * possibly violating some of their assumptions. This is why this code
17  * has to be extremely careful. Generally it tries to use normal locking
18  * rules, as in get the standard locks, even if that means the
19  * error handling takes potentially a long time.
20  *
21  * The operation to map back from RMAP chains to processes has to walk
22  * the complete process list and has non linear complexity with the number
23  * mappings. In short it can be quite slow. But since memory corruptions
24  * are rare we hope to get away with this.
25  */
26
27 /*
28  * Notebook:
29  * - hugetlb needs more code
30  * - kcore/oldmem/vmcore/mem/kmem check for hwpoison pages
31  * - pass bad pages to kdump next kernel
32  */
33 #define DEBUG 1         /* remove me in 2.6.34 */
34 #include <linux/kernel.h>
35 #include <linux/mm.h>
36 #include <linux/page-flags.h>
37 #include <linux/kernel-page-flags.h>
38 #include <linux/sched.h>
39 #include <linux/ksm.h>
40 #include <linux/rmap.h>
41 #include <linux/pagemap.h>
42 #include <linux/swap.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>
48 #include <linux/hugetlb.h>
49 #include "internal.h"
50
51 int sysctl_memory_failure_early_kill __read_mostly = 0;
52
53 int sysctl_memory_failure_recovery __read_mostly = 1;
54
55 atomic_long_t mce_bad_pages __read_mostly = ATOMIC_LONG_INIT(0);
56
57 #if defined(CONFIG_HWPOISON_INJECT) || defined(CONFIG_HWPOISON_INJECT_MODULE)
58
59 u32 hwpoison_filter_enable = 0;
60 u32 hwpoison_filter_dev_major = ~0U;
61 u32 hwpoison_filter_dev_minor = ~0U;
62 u64 hwpoison_filter_flags_mask;
63 u64 hwpoison_filter_flags_value;
64 EXPORT_SYMBOL_GPL(hwpoison_filter_enable);
65 EXPORT_SYMBOL_GPL(hwpoison_filter_dev_major);
66 EXPORT_SYMBOL_GPL(hwpoison_filter_dev_minor);
67 EXPORT_SYMBOL_GPL(hwpoison_filter_flags_mask);
68 EXPORT_SYMBOL_GPL(hwpoison_filter_flags_value);
69
70 static int hwpoison_filter_dev(struct page *p)
71 {
72         struct address_space *mapping;
73         dev_t dev;
74
75         if (hwpoison_filter_dev_major == ~0U &&
76             hwpoison_filter_dev_minor == ~0U)
77                 return 0;
78
79         /*
80          * page_mapping() does not accept slab page
81          */
82         if (PageSlab(p))
83                 return -EINVAL;
84
85         mapping = page_mapping(p);
86         if (mapping == NULL || mapping->host == NULL)
87                 return -EINVAL;
88
89         dev = mapping->host->i_sb->s_dev;
90         if (hwpoison_filter_dev_major != ~0U &&
91             hwpoison_filter_dev_major != MAJOR(dev))
92                 return -EINVAL;
93         if (hwpoison_filter_dev_minor != ~0U &&
94             hwpoison_filter_dev_minor != MINOR(dev))
95                 return -EINVAL;
96
97         return 0;
98 }
99
100 static int hwpoison_filter_flags(struct page *p)
101 {
102         if (!hwpoison_filter_flags_mask)
103                 return 0;
104
105         if ((stable_page_flags(p) & hwpoison_filter_flags_mask) ==
106                                     hwpoison_filter_flags_value)
107                 return 0;
108         else
109                 return -EINVAL;
110 }
111
112 /*
113  * This allows stress tests to limit test scope to a collection of tasks
114  * by putting them under some memcg. This prevents killing unrelated/important
115  * processes such as /sbin/init. Note that the target task may share clean
116  * pages with init (eg. libc text), which is harmless. If the target task
117  * share _dirty_ pages with another task B, the test scheme must make sure B
118  * is also included in the memcg. At last, due to race conditions this filter
119  * can only guarantee that the page either belongs to the memcg tasks, or is
120  * a freed page.
121  */
122 #ifdef  CONFIG_CGROUP_MEM_RES_CTLR_SWAP
123 u64 hwpoison_filter_memcg;
124 EXPORT_SYMBOL_GPL(hwpoison_filter_memcg);
125 static int hwpoison_filter_task(struct page *p)
126 {
127         struct mem_cgroup *mem;
128         struct cgroup_subsys_state *css;
129         unsigned long ino;
130
131         if (!hwpoison_filter_memcg)
132                 return 0;
133
134         mem = try_get_mem_cgroup_from_page(p);
135         if (!mem)
136                 return -EINVAL;
137
138         css = mem_cgroup_css(mem);
139         /* root_mem_cgroup has NULL dentries */
140         if (!css->cgroup->dentry)
141                 return -EINVAL;
142
143         ino = css->cgroup->dentry->d_inode->i_ino;
144         css_put(css);
145
146         if (ino != hwpoison_filter_memcg)
147                 return -EINVAL;
148
149         return 0;
150 }
151 #else
152 static int hwpoison_filter_task(struct page *p) { return 0; }
153 #endif
154
155 int hwpoison_filter(struct page *p)
156 {
157         if (!hwpoison_filter_enable)
158                 return 0;
159
160         if (hwpoison_filter_dev(p))
161                 return -EINVAL;
162
163         if (hwpoison_filter_flags(p))
164                 return -EINVAL;
165
166         if (hwpoison_filter_task(p))
167                 return -EINVAL;
168
169         return 0;
170 }
171 #else
172 int hwpoison_filter(struct page *p)
173 {
174         return 0;
175 }
176 #endif
177
178 EXPORT_SYMBOL_GPL(hwpoison_filter);
179
180 /*
181  * Send all the processes who have the page mapped an ``action optional''
182  * signal.
183  */
184 static int kill_proc_ao(struct task_struct *t, unsigned long addr, int trapno,
185                         unsigned long pfn)
186 {
187         struct siginfo si;
188         int ret;
189
190         printk(KERN_ERR
191                 "MCE %#lx: Killing %s:%d early due to hardware memory corruption\n",
192                 pfn, t->comm, t->pid);
193         si.si_signo = SIGBUS;
194         si.si_errno = 0;
195         si.si_code = BUS_MCEERR_AO;
196         si.si_addr = (void *)addr;
197 #ifdef __ARCH_SI_TRAPNO
198         si.si_trapno = trapno;
199 #endif
200         si.si_addr_lsb = PAGE_SHIFT;
201         /*
202          * Don't use force here, it's convenient if the signal
203          * can be temporarily blocked.
204          * This could cause a loop when the user sets SIGBUS
205          * to SIG_IGN, but hopefully noone will do that?
206          */
207         ret = send_sig_info(SIGBUS, &si, t);  /* synchronous? */
208         if (ret < 0)
209                 printk(KERN_INFO "MCE: Error sending signal to %s:%d: %d\n",
210                        t->comm, t->pid, ret);
211         return ret;
212 }
213
214 /*
215  * When a unknown page type is encountered drain as many buffers as possible
216  * in the hope to turn the page into a LRU or free page, which we can handle.
217  */
218 void shake_page(struct page *p, int access)
219 {
220         if (!PageSlab(p)) {
221                 lru_add_drain_all();
222                 if (PageLRU(p))
223                         return;
224                 drain_all_pages();
225                 if (PageLRU(p) || is_free_buddy_page(p))
226                         return;
227         }
228
229         /*
230          * Only all shrink_slab here (which would also
231          * shrink other caches) if access is not potentially fatal.
232          */
233         if (access) {
234                 int nr;
235                 do {
236                         nr = shrink_slab(1000, GFP_KERNEL, 1000);
237                         if (page_count(p) == 0)
238                                 break;
239                 } while (nr > 10);
240         }
241 }
242 EXPORT_SYMBOL_GPL(shake_page);
243
244 /*
245  * Kill all processes that have a poisoned page mapped and then isolate
246  * the page.
247  *
248  * General strategy:
249  * Find all processes having the page mapped and kill them.
250  * But we keep a page reference around so that the page is not
251  * actually freed yet.
252  * Then stash the page away
253  *
254  * There's no convenient way to get back to mapped processes
255  * from the VMAs. So do a brute-force search over all
256  * running processes.
257  *
258  * Remember that machine checks are not common (or rather
259  * if they are common you have other problems), so this shouldn't
260  * be a performance issue.
261  *
262  * Also there are some races possible while we get from the
263  * error detection to actually handle it.
264  */
265
266 struct to_kill {
267         struct list_head nd;
268         struct task_struct *tsk;
269         unsigned long addr;
270         unsigned addr_valid:1;
271 };
272
273 /*
274  * Failure handling: if we can't find or can't kill a process there's
275  * not much we can do.  We just print a message and ignore otherwise.
276  */
277
278 /*
279  * Schedule a process for later kill.
280  * Uses GFP_ATOMIC allocations to avoid potential recursions in the VM.
281  * TBD would GFP_NOIO be enough?
282  */
283 static void add_to_kill(struct task_struct *tsk, struct page *p,
284                        struct vm_area_struct *vma,
285                        struct list_head *to_kill,
286                        struct to_kill **tkc)
287 {
288         struct to_kill *tk;
289
290         if (*tkc) {
291                 tk = *tkc;
292                 *tkc = NULL;
293         } else {
294                 tk = kmalloc(sizeof(struct to_kill), GFP_ATOMIC);
295                 if (!tk) {
296                         printk(KERN_ERR
297                 "MCE: Out of memory while machine check handling\n");
298                         return;
299                 }
300         }
301         tk->addr = page_address_in_vma(p, vma);
302         tk->addr_valid = 1;
303
304         /*
305          * In theory we don't have to kill when the page was
306          * munmaped. But it could be also a mremap. Since that's
307          * likely very rare kill anyways just out of paranoia, but use
308          * a SIGKILL because the error is not contained anymore.
309          */
310         if (tk->addr == -EFAULT) {
311                 pr_debug("MCE: Unable to find user space address %lx in %s\n",
312                         page_to_pfn(p), tsk->comm);
313                 tk->addr_valid = 0;
314         }
315         get_task_struct(tsk);
316         tk->tsk = tsk;
317         list_add_tail(&tk->nd, to_kill);
318 }
319
320 /*
321  * Kill the processes that have been collected earlier.
322  *
323  * Only do anything when DOIT is set, otherwise just free the list
324  * (this is used for clean pages which do not need killing)
325  * Also when FAIL is set do a force kill because something went
326  * wrong earlier.
327  */
328 static void kill_procs_ao(struct list_head *to_kill, int doit, int trapno,
329                           int fail, unsigned long pfn)
330 {
331         struct to_kill *tk, *next;
332
333         list_for_each_entry_safe (tk, next, to_kill, nd) {
334                 if (doit) {
335                         /*
336                          * In case something went wrong with munmapping
337                          * make sure the process doesn't catch the
338                          * signal and then access the memory. Just kill it.
339                          */
340                         if (fail || tk->addr_valid == 0) {
341                                 printk(KERN_ERR
342                 "MCE %#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
343                                         pfn, tk->tsk->comm, tk->tsk->pid);
344                                 force_sig(SIGKILL, tk->tsk);
345                         }
346
347                         /*
348                          * In theory the process could have mapped
349                          * something else on the address in-between. We could
350                          * check for that, but we need to tell the
351                          * process anyways.
352                          */
353                         else if (kill_proc_ao(tk->tsk, tk->addr, trapno,
354                                               pfn) < 0)
355                                 printk(KERN_ERR
356                 "MCE %#lx: Cannot send advisory machine check signal to %s:%d\n",
357                                         pfn, tk->tsk->comm, tk->tsk->pid);
358                 }
359                 put_task_struct(tk->tsk);
360                 kfree(tk);
361         }
362 }
363
364 static int task_early_kill(struct task_struct *tsk)
365 {
366         if (!tsk->mm)
367                 return 0;
368         if (tsk->flags & PF_MCE_PROCESS)
369                 return !!(tsk->flags & PF_MCE_EARLY);
370         return sysctl_memory_failure_early_kill;
371 }
372
373 /*
374  * Collect processes when the error hit an anonymous page.
375  */
376 static void collect_procs_anon(struct page *page, struct list_head *to_kill,
377                               struct to_kill **tkc)
378 {
379         struct vm_area_struct *vma;
380         struct task_struct *tsk;
381         struct anon_vma *av;
382
383         read_lock(&tasklist_lock);
384         av = page_lock_anon_vma(page);
385         if (av == NULL) /* Not actually mapped anymore */
386                 goto out;
387         for_each_process (tsk) {
388                 struct anon_vma_chain *vmac;
389
390                 if (!task_early_kill(tsk))
391                         continue;
392                 list_for_each_entry(vmac, &av->head, same_anon_vma) {
393                         vma = vmac->vma;
394                         if (!page_mapped_in_vma(page, vma))
395                                 continue;
396                         if (vma->vm_mm == tsk->mm)
397                                 add_to_kill(tsk, page, vma, to_kill, tkc);
398                 }
399         }
400         page_unlock_anon_vma(av);
401 out:
402         read_unlock(&tasklist_lock);
403 }
404
405 /*
406  * Collect processes when the error hit a file mapped page.
407  */
408 static void collect_procs_file(struct page *page, struct list_head *to_kill,
409                               struct to_kill **tkc)
410 {
411         struct vm_area_struct *vma;
412         struct task_struct *tsk;
413         struct prio_tree_iter iter;
414         struct address_space *mapping = page->mapping;
415
416         /*
417          * A note on the locking order between the two locks.
418          * We don't rely on this particular order.
419          * If you have some other code that needs a different order
420          * feel free to switch them around. Or add a reverse link
421          * from mm_struct to task_struct, then this could be all
422          * done without taking tasklist_lock and looping over all tasks.
423          */
424
425         read_lock(&tasklist_lock);
426         spin_lock(&mapping->i_mmap_lock);
427         for_each_process(tsk) {
428                 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
429
430                 if (!task_early_kill(tsk))
431                         continue;
432
433                 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff,
434                                       pgoff) {
435                         /*
436                          * Send early kill signal to tasks where a vma covers
437                          * the page but the corrupted page is not necessarily
438                          * mapped it in its pte.
439                          * Assume applications who requested early kill want
440                          * to be informed of all such data corruptions.
441                          */
442                         if (vma->vm_mm == tsk->mm)
443                                 add_to_kill(tsk, page, vma, to_kill, tkc);
444                 }
445         }
446         spin_unlock(&mapping->i_mmap_lock);
447         read_unlock(&tasklist_lock);
448 }
449
450 /*
451  * Collect the processes who have the corrupted page mapped to kill.
452  * This is done in two steps for locking reasons.
453  * First preallocate one tokill structure outside the spin locks,
454  * so that we can kill at least one process reasonably reliable.
455  */
456 static void collect_procs(struct page *page, struct list_head *tokill)
457 {
458         struct to_kill *tk;
459
460         if (!page->mapping)
461                 return;
462
463         tk = kmalloc(sizeof(struct to_kill), GFP_NOIO);
464         if (!tk)
465                 return;
466         if (PageAnon(page))
467                 collect_procs_anon(page, tokill, &tk);
468         else
469                 collect_procs_file(page, tokill, &tk);
470         kfree(tk);
471 }
472
473 /*
474  * Error handlers for various types of pages.
475  */
476
477 enum outcome {
478         IGNORED,        /* Error: cannot be handled */
479         FAILED,         /* Error: handling failed */
480         DELAYED,        /* Will be handled later */
481         RECOVERED,      /* Successfully recovered */
482 };
483
484 static const char *action_name[] = {
485         [IGNORED] = "Ignored",
486         [FAILED] = "Failed",
487         [DELAYED] = "Delayed",
488         [RECOVERED] = "Recovered",
489 };
490
491 /*
492  * XXX: It is possible that a page is isolated from LRU cache,
493  * and then kept in swap cache or failed to remove from page cache.
494  * The page count will stop it from being freed by unpoison.
495  * Stress tests should be aware of this memory leak problem.
496  */
497 static int delete_from_lru_cache(struct page *p)
498 {
499         if (!isolate_lru_page(p)) {
500                 /*
501                  * Clear sensible page flags, so that the buddy system won't
502                  * complain when the page is unpoison-and-freed.
503                  */
504                 ClearPageActive(p);
505                 ClearPageUnevictable(p);
506                 /*
507                  * drop the page count elevated by isolate_lru_page()
508                  */
509                 page_cache_release(p);
510                 return 0;
511         }
512         return -EIO;
513 }
514
515 /*
516  * Error hit kernel page.
517  * Do nothing, try to be lucky and not touch this instead. For a few cases we
518  * could be more sophisticated.
519  */
520 static int me_kernel(struct page *p, unsigned long pfn)
521 {
522         return IGNORED;
523 }
524
525 /*
526  * Page in unknown state. Do nothing.
527  */
528 static int me_unknown(struct page *p, unsigned long pfn)
529 {
530         printk(KERN_ERR "MCE %#lx: Unknown page state\n", pfn);
531         return FAILED;
532 }
533
534 /*
535  * Clean (or cleaned) page cache page.
536  */
537 static int me_pagecache_clean(struct page *p, unsigned long pfn)
538 {
539         int err;
540         int ret = FAILED;
541         struct address_space *mapping;
542
543         delete_from_lru_cache(p);
544
545         /*
546          * For anonymous pages we're done the only reference left
547          * should be the one m_f() holds.
548          */
549         if (PageAnon(p))
550                 return RECOVERED;
551
552         /*
553          * Now truncate the page in the page cache. This is really
554          * more like a "temporary hole punch"
555          * Don't do this for block devices when someone else
556          * has a reference, because it could be file system metadata
557          * and that's not safe to truncate.
558          */
559         mapping = page_mapping(p);
560         if (!mapping) {
561                 /*
562                  * Page has been teared down in the meanwhile
563                  */
564                 return FAILED;
565         }
566
567         /*
568          * Truncation is a bit tricky. Enable it per file system for now.
569          *
570          * Open: to take i_mutex or not for this? Right now we don't.
571          */
572         if (mapping->a_ops->error_remove_page) {
573                 err = mapping->a_ops->error_remove_page(mapping, p);
574                 if (err != 0) {
575                         printk(KERN_INFO "MCE %#lx: Failed to punch page: %d\n",
576                                         pfn, err);
577                 } else if (page_has_private(p) &&
578                                 !try_to_release_page(p, GFP_NOIO)) {
579                         pr_debug("MCE %#lx: failed to release buffers\n", pfn);
580                 } else {
581                         ret = RECOVERED;
582                 }
583         } else {
584                 /*
585                  * If the file system doesn't support it just invalidate
586                  * This fails on dirty or anything with private pages
587                  */
588                 if (invalidate_inode_page(p))
589                         ret = RECOVERED;
590                 else
591                         printk(KERN_INFO "MCE %#lx: Failed to invalidate\n",
592                                 pfn);
593         }
594         return ret;
595 }
596
597 /*
598  * Dirty cache page page
599  * Issues: when the error hit a hole page the error is not properly
600  * propagated.
601  */
602 static int me_pagecache_dirty(struct page *p, unsigned long pfn)
603 {
604         struct address_space *mapping = page_mapping(p);
605
606         SetPageError(p);
607         /* TBD: print more information about the file. */
608         if (mapping) {
609                 /*
610                  * IO error will be reported by write(), fsync(), etc.
611                  * who check the mapping.
612                  * This way the application knows that something went
613                  * wrong with its dirty file data.
614                  *
615                  * There's one open issue:
616                  *
617                  * The EIO will be only reported on the next IO
618                  * operation and then cleared through the IO map.
619                  * Normally Linux has two mechanisms to pass IO error
620                  * first through the AS_EIO flag in the address space
621                  * and then through the PageError flag in the page.
622                  * Since we drop pages on memory failure handling the
623                  * only mechanism open to use is through AS_AIO.
624                  *
625                  * This has the disadvantage that it gets cleared on
626                  * the first operation that returns an error, while
627                  * the PageError bit is more sticky and only cleared
628                  * when the page is reread or dropped.  If an
629                  * application assumes it will always get error on
630                  * fsync, but does other operations on the fd before
631                  * and the page is dropped inbetween then the error
632                  * will not be properly reported.
633                  *
634                  * This can already happen even without hwpoisoned
635                  * pages: first on metadata IO errors (which only
636                  * report through AS_EIO) or when the page is dropped
637                  * at the wrong time.
638                  *
639                  * So right now we assume that the application DTRT on
640                  * the first EIO, but we're not worse than other parts
641                  * of the kernel.
642                  */
643                 mapping_set_error(mapping, EIO);
644         }
645
646         return me_pagecache_clean(p, pfn);
647 }
648
649 /*
650  * Clean and dirty swap cache.
651  *
652  * Dirty swap cache page is tricky to handle. The page could live both in page
653  * cache and swap cache(ie. page is freshly swapped in). So it could be
654  * referenced concurrently by 2 types of PTEs:
655  * normal PTEs and swap PTEs. We try to handle them consistently by calling
656  * try_to_unmap(TTU_IGNORE_HWPOISON) to convert the normal PTEs to swap PTEs,
657  * and then
658  *      - clear dirty bit to prevent IO
659  *      - remove from LRU
660  *      - but keep in the swap cache, so that when we return to it on
661  *        a later page fault, we know the application is accessing
662  *        corrupted data and shall be killed (we installed simple
663  *        interception code in do_swap_page to catch it).
664  *
665  * Clean swap cache pages can be directly isolated. A later page fault will
666  * bring in the known good data from disk.
667  */
668 static int me_swapcache_dirty(struct page *p, unsigned long pfn)
669 {
670         ClearPageDirty(p);
671         /* Trigger EIO in shmem: */
672         ClearPageUptodate(p);
673
674         if (!delete_from_lru_cache(p))
675                 return DELAYED;
676         else
677                 return FAILED;
678 }
679
680 static int me_swapcache_clean(struct page *p, unsigned long pfn)
681 {
682         delete_from_swap_cache(p);
683
684         if (!delete_from_lru_cache(p))
685                 return RECOVERED;
686         else
687                 return FAILED;
688 }
689
690 /*
691  * Huge pages. Needs work.
692  * Issues:
693  * - Error on hugepage is contained in hugepage unit (not in raw page unit.)
694  *   To narrow down kill region to one page, we need to break up pmd.
695  * - To support soft-offlining for hugepage, we need to support hugepage
696  *   migration.
697  */
698 static int me_huge_page(struct page *p, unsigned long pfn)
699 {
700         struct page *hpage = compound_head(p);
701         /*
702          * We can safely recover from error on free or reserved (i.e.
703          * not in-use) hugepage by dequeuing it from freelist.
704          * To check whether a hugepage is in-use or not, we can't use
705          * page->lru because it can be used in other hugepage operations,
706          * such as __unmap_hugepage_range() and gather_surplus_pages().
707          * So instead we use page_mapping() and PageAnon().
708          * We assume that this function is called with page lock held,
709          * so there is no race between isolation and mapping/unmapping.
710          */
711         if (!(page_mapping(hpage) || PageAnon(hpage))) {
712                 __isolate_hwpoisoned_huge_page(hpage);
713                 return RECOVERED;
714         }
715         return DELAYED;
716 }
717
718 /*
719  * Various page states we can handle.
720  *
721  * A page state is defined by its current page->flags bits.
722  * The table matches them in order and calls the right handler.
723  *
724  * This is quite tricky because we can access page at any time
725  * in its live cycle, so all accesses have to be extremly careful.
726  *
727  * This is not complete. More states could be added.
728  * For any missing state don't attempt recovery.
729  */
730
731 #define dirty           (1UL << PG_dirty)
732 #define sc              (1UL << PG_swapcache)
733 #define unevict         (1UL << PG_unevictable)
734 #define mlock           (1UL << PG_mlocked)
735 #define writeback       (1UL << PG_writeback)
736 #define lru             (1UL << PG_lru)
737 #define swapbacked      (1UL << PG_swapbacked)
738 #define head            (1UL << PG_head)
739 #define tail            (1UL << PG_tail)
740 #define compound        (1UL << PG_compound)
741 #define slab            (1UL << PG_slab)
742 #define reserved        (1UL << PG_reserved)
743
744 static struct page_state {
745         unsigned long mask;
746         unsigned long res;
747         char *msg;
748         int (*action)(struct page *p, unsigned long pfn);
749 } error_states[] = {
750         { reserved,     reserved,       "reserved kernel",      me_kernel },
751         /*
752          * free pages are specially detected outside this table:
753          * PG_buddy pages only make a small fraction of all free pages.
754          */
755
756         /*
757          * Could in theory check if slab page is free or if we can drop
758          * currently unused objects without touching them. But just
759          * treat it as standard kernel for now.
760          */
761         { slab,         slab,           "kernel slab",  me_kernel },
762
763 #ifdef CONFIG_PAGEFLAGS_EXTENDED
764         { head,         head,           "huge",         me_huge_page },
765         { tail,         tail,           "huge",         me_huge_page },
766 #else
767         { compound,     compound,       "huge",         me_huge_page },
768 #endif
769
770         { sc|dirty,     sc|dirty,       "swapcache",    me_swapcache_dirty },
771         { sc|dirty,     sc,             "swapcache",    me_swapcache_clean },
772
773         { unevict|dirty, unevict|dirty, "unevictable LRU", me_pagecache_dirty},
774         { unevict,      unevict,        "unevictable LRU", me_pagecache_clean},
775
776         { mlock|dirty,  mlock|dirty,    "mlocked LRU",  me_pagecache_dirty },
777         { mlock,        mlock,          "mlocked LRU",  me_pagecache_clean },
778
779         { lru|dirty,    lru|dirty,      "LRU",          me_pagecache_dirty },
780         { lru|dirty,    lru,            "clean LRU",    me_pagecache_clean },
781
782         /*
783          * Catchall entry: must be at end.
784          */
785         { 0,            0,              "unknown page state",   me_unknown },
786 };
787
788 #undef dirty
789 #undef sc
790 #undef unevict
791 #undef mlock
792 #undef writeback
793 #undef lru
794 #undef swapbacked
795 #undef head
796 #undef tail
797 #undef compound
798 #undef slab
799 #undef reserved
800
801 static void action_result(unsigned long pfn, char *msg, int result)
802 {
803         struct page *page = pfn_to_page(pfn);
804
805         printk(KERN_ERR "MCE %#lx: %s%s page recovery: %s\n",
806                 pfn,
807                 PageDirty(page) ? "dirty " : "",
808                 msg, action_name[result]);
809 }
810
811 static int page_action(struct page_state *ps, struct page *p,
812                         unsigned long pfn)
813 {
814         int result;
815         int count;
816
817         result = ps->action(p, pfn);
818         action_result(pfn, ps->msg, result);
819
820         count = page_count(p) - 1;
821         if (ps->action == me_swapcache_dirty && result == DELAYED)
822                 count--;
823         if (count != 0) {
824                 printk(KERN_ERR
825                        "MCE %#lx: %s page still referenced by %d users\n",
826                        pfn, ps->msg, count);
827                 result = FAILED;
828         }
829
830         /* Could do more checks here if page looks ok */
831         /*
832          * Could adjust zone counters here to correct for the missing page.
833          */
834
835         return (result == RECOVERED || result == DELAYED) ? 0 : -EBUSY;
836 }
837
838 #define N_UNMAP_TRIES 5
839
840 /*
841  * Do all that is necessary to remove user space mappings. Unmap
842  * the pages and send SIGBUS to the processes if the data was dirty.
843  */
844 static int hwpoison_user_mappings(struct page *p, unsigned long pfn,
845                                   int trapno)
846 {
847         enum ttu_flags ttu = TTU_UNMAP | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS;
848         struct address_space *mapping;
849         LIST_HEAD(tokill);
850         int ret;
851         int i;
852         int kill = 1;
853         struct page *hpage = compound_head(p);
854
855         if (PageReserved(p) || PageSlab(p))
856                 return SWAP_SUCCESS;
857
858         /*
859          * This check implies we don't kill processes if their pages
860          * are in the swap cache early. Those are always late kills.
861          */
862         if (!page_mapped(hpage))
863                 return SWAP_SUCCESS;
864
865         if (PageKsm(p))
866                 return SWAP_FAIL;
867
868         if (PageSwapCache(p)) {
869                 printk(KERN_ERR
870                        "MCE %#lx: keeping poisoned page in swap cache\n", pfn);
871                 ttu |= TTU_IGNORE_HWPOISON;
872         }
873
874         /*
875          * Propagate the dirty bit from PTEs to struct page first, because we
876          * need this to decide if we should kill or just drop the page.
877          * XXX: the dirty test could be racy: set_page_dirty() may not always
878          * be called inside page lock (it's recommended but not enforced).
879          */
880         mapping = page_mapping(hpage);
881         if (!PageDirty(hpage) && mapping &&
882             mapping_cap_writeback_dirty(mapping)) {
883                 if (page_mkclean(hpage)) {
884                         SetPageDirty(hpage);
885                 } else {
886                         kill = 0;
887                         ttu |= TTU_IGNORE_HWPOISON;
888                         printk(KERN_INFO
889         "MCE %#lx: corrupted page was clean: dropped without side effects\n",
890                                 pfn);
891                 }
892         }
893
894         /*
895          * First collect all the processes that have the page
896          * mapped in dirty form.  This has to be done before try_to_unmap,
897          * because ttu takes the rmap data structures down.
898          *
899          * Error handling: We ignore errors here because
900          * there's nothing that can be done.
901          */
902         if (kill)
903                 collect_procs(hpage, &tokill);
904
905         /*
906          * try_to_unmap can fail temporarily due to races.
907          * Try a few times (RED-PEN better strategy?)
908          */
909         for (i = 0; i < N_UNMAP_TRIES; i++) {
910                 ret = try_to_unmap(hpage, ttu);
911                 if (ret == SWAP_SUCCESS)
912                         break;
913                 pr_debug("MCE %#lx: try_to_unmap retry needed %d\n", pfn,  ret);
914         }
915
916         if (ret != SWAP_SUCCESS)
917                 printk(KERN_ERR "MCE %#lx: failed to unmap page (mapcount=%d)\n",
918                                 pfn, page_mapcount(hpage));
919
920         /*
921          * Now that the dirty bit has been propagated to the
922          * struct page and all unmaps done we can decide if
923          * killing is needed or not.  Only kill when the page
924          * was dirty, otherwise the tokill list is merely
925          * freed.  When there was a problem unmapping earlier
926          * use a more force-full uncatchable kill to prevent
927          * any accesses to the poisoned memory.
928          */
929         kill_procs_ao(&tokill, !!PageDirty(hpage), trapno,
930                       ret != SWAP_SUCCESS, pfn);
931
932         return ret;
933 }
934
935 static void set_page_hwpoison_huge_page(struct page *hpage)
936 {
937         int i;
938         int nr_pages = 1 << compound_order(hpage);
939         for (i = 0; i < nr_pages; i++)
940                 SetPageHWPoison(hpage + i);
941 }
942
943 static void clear_page_hwpoison_huge_page(struct page *hpage)
944 {
945         int i;
946         int nr_pages = 1 << compound_order(hpage);
947         for (i = 0; i < nr_pages; i++)
948                 ClearPageHWPoison(hpage + i);
949 }
950
951 int __memory_failure(unsigned long pfn, int trapno, int flags)
952 {
953         struct page_state *ps;
954         struct page *p;
955         struct page *hpage;
956         int res;
957         unsigned int nr_pages;
958
959         if (!sysctl_memory_failure_recovery)
960                 panic("Memory failure from trap %d on page %lx", trapno, pfn);
961
962         if (!pfn_valid(pfn)) {
963                 printk(KERN_ERR
964                        "MCE %#lx: memory outside kernel control\n",
965                        pfn);
966                 return -ENXIO;
967         }
968
969         p = pfn_to_page(pfn);
970         hpage = compound_head(p);
971         if (TestSetPageHWPoison(p)) {
972                 printk(KERN_ERR "MCE %#lx: already hardware poisoned\n", pfn);
973                 return 0;
974         }
975
976         nr_pages = 1 << compound_order(hpage);
977         atomic_long_add(nr_pages, &mce_bad_pages);
978
979         /*
980          * We need/can do nothing about count=0 pages.
981          * 1) it's a free page, and therefore in safe hand:
982          *    prep_new_page() will be the gate keeper.
983          * 2) it's part of a non-compound high order page.
984          *    Implies some kernel user: cannot stop them from
985          *    R/W the page; let's pray that the page has been
986          *    used and will be freed some time later.
987          * In fact it's dangerous to directly bump up page count from 0,
988          * that may make page_freeze_refs()/page_unfreeze_refs() mismatch.
989          */
990         if (!(flags & MF_COUNT_INCREASED) &&
991                 !get_page_unless_zero(hpage)) {
992                 if (is_free_buddy_page(p)) {
993                         action_result(pfn, "free buddy", DELAYED);
994                         return 0;
995                 } else {
996                         action_result(pfn, "high order kernel", IGNORED);
997                         return -EBUSY;
998                 }
999         }
1000
1001         /*
1002          * We ignore non-LRU pages for good reasons.
1003          * - PG_locked is only well defined for LRU pages and a few others
1004          * - to avoid races with __set_page_locked()
1005          * - to avoid races with __SetPageSlab*() (and more non-atomic ops)
1006          * The check (unnecessarily) ignores LRU pages being isolated and
1007          * walked by the page reclaim code, however that's not a big loss.
1008          */
1009         if (!PageLRU(p) && !PageHuge(p))
1010                 shake_page(p, 0);
1011         if (!PageLRU(p) && !PageHuge(p)) {
1012                 /*
1013                  * shake_page could have turned it free.
1014                  */
1015                 if (is_free_buddy_page(p)) {
1016                         action_result(pfn, "free buddy, 2nd try", DELAYED);
1017                         return 0;
1018                 }
1019                 action_result(pfn, "non LRU", IGNORED);
1020                 put_page(p);
1021                 return -EBUSY;
1022         }
1023
1024         /*
1025          * Lock the page and wait for writeback to finish.
1026          * It's very difficult to mess with pages currently under IO
1027          * and in many cases impossible, so we just avoid it here.
1028          */
1029         lock_page_nosync(hpage);
1030
1031         /*
1032          * unpoison always clear PG_hwpoison inside page lock
1033          */
1034         if (!PageHWPoison(p)) {
1035                 printk(KERN_ERR "MCE %#lx: just unpoisoned\n", pfn);
1036                 res = 0;
1037                 goto out;
1038         }
1039         if (hwpoison_filter(p)) {
1040                 if (TestClearPageHWPoison(p))
1041                         atomic_long_sub(nr_pages, &mce_bad_pages);
1042                 unlock_page(hpage);
1043                 put_page(hpage);
1044                 return 0;
1045         }
1046
1047         /*
1048          * For error on the tail page, we should set PG_hwpoison
1049          * on the head page to show that the hugepage is hwpoisoned
1050          */
1051         if (PageTail(p) && TestSetPageHWPoison(hpage)) {
1052                 action_result(pfn, "hugepage already hardware poisoned",
1053                                 IGNORED);
1054                 unlock_page(hpage);
1055                 put_page(hpage);
1056                 return 0;
1057         }
1058         /*
1059          * Set PG_hwpoison on all pages in an error hugepage,
1060          * because containment is done in hugepage unit for now.
1061          * Since we have done TestSetPageHWPoison() for the head page with
1062          * page lock held, we can safely set PG_hwpoison bits on tail pages.
1063          */
1064         if (PageHuge(p))
1065                 set_page_hwpoison_huge_page(hpage);
1066
1067         wait_on_page_writeback(p);
1068
1069         /*
1070          * Now take care of user space mappings.
1071          * Abort on fail: __remove_from_page_cache() assumes unmapped page.
1072          */
1073         if (hwpoison_user_mappings(p, pfn, trapno) != SWAP_SUCCESS) {
1074                 printk(KERN_ERR "MCE %#lx: cannot unmap page, give up\n", pfn);
1075                 res = -EBUSY;
1076                 goto out;
1077         }
1078
1079         /*
1080          * Torn down by someone else?
1081          */
1082         if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
1083                 action_result(pfn, "already truncated LRU", IGNORED);
1084                 res = -EBUSY;
1085                 goto out;
1086         }
1087
1088         res = -EBUSY;
1089         for (ps = error_states;; ps++) {
1090                 if ((p->flags & ps->mask) == ps->res) {
1091                         res = page_action(ps, p, pfn);
1092                         break;
1093                 }
1094         }
1095 out:
1096         unlock_page(hpage);
1097         return res;
1098 }
1099 EXPORT_SYMBOL_GPL(__memory_failure);
1100
1101 /**
1102  * memory_failure - Handle memory failure of a page.
1103  * @pfn: Page Number of the corrupted page
1104  * @trapno: Trap number reported in the signal to user space.
1105  *
1106  * This function is called by the low level machine check code
1107  * of an architecture when it detects hardware memory corruption
1108  * of a page. It tries its best to recover, which includes
1109  * dropping pages, killing processes etc.
1110  *
1111  * The function is primarily of use for corruptions that
1112  * happen outside the current execution context (e.g. when
1113  * detected by a background scrubber)
1114  *
1115  * Must run in process context (e.g. a work queue) with interrupts
1116  * enabled and no spinlocks hold.
1117  */
1118 void memory_failure(unsigned long pfn, int trapno)
1119 {
1120         __memory_failure(pfn, trapno, 0);
1121 }
1122
1123 /**
1124  * unpoison_memory - Unpoison a previously poisoned page
1125  * @pfn: Page number of the to be unpoisoned page
1126  *
1127  * Software-unpoison a page that has been poisoned by
1128  * memory_failure() earlier.
1129  *
1130  * This is only done on the software-level, so it only works
1131  * for linux injected failures, not real hardware failures
1132  *
1133  * Returns 0 for success, otherwise -errno.
1134  */
1135 int unpoison_memory(unsigned long pfn)
1136 {
1137         struct page *page;
1138         struct page *p;
1139         int freeit = 0;
1140         unsigned int nr_pages;
1141
1142         if (!pfn_valid(pfn))
1143                 return -ENXIO;
1144
1145         p = pfn_to_page(pfn);
1146         page = compound_head(p);
1147
1148         if (!PageHWPoison(p)) {
1149                 pr_debug("MCE: Page was already unpoisoned %#lx\n", pfn);
1150                 return 0;
1151         }
1152
1153         nr_pages = 1 << compound_order(page);
1154
1155         if (!get_page_unless_zero(page)) {
1156                 if (TestClearPageHWPoison(p))
1157                         atomic_long_sub(nr_pages, &mce_bad_pages);
1158                 pr_debug("MCE: Software-unpoisoned free page %#lx\n", pfn);
1159                 return 0;
1160         }
1161
1162         lock_page_nosync(page);
1163         /*
1164          * This test is racy because PG_hwpoison is set outside of page lock.
1165          * That's acceptable because that won't trigger kernel panic. Instead,
1166          * the PG_hwpoison page will be caught and isolated on the entrance to
1167          * the free buddy page pool.
1168          */
1169         if (TestClearPageHWPoison(page)) {
1170                 pr_debug("MCE: Software-unpoisoned page %#lx\n", pfn);
1171                 atomic_long_sub(nr_pages, &mce_bad_pages);
1172                 freeit = 1;
1173         }
1174         if (PageHuge(p))
1175                 clear_page_hwpoison_huge_page(page);
1176         unlock_page(page);
1177
1178         put_page(page);
1179         if (freeit)
1180                 put_page(page);
1181
1182         return 0;
1183 }
1184 EXPORT_SYMBOL(unpoison_memory);
1185
1186 static struct page *new_page(struct page *p, unsigned long private, int **x)
1187 {
1188         int nid = page_to_nid(p);
1189         return alloc_pages_exact_node(nid, GFP_HIGHUSER_MOVABLE, 0);
1190 }
1191
1192 /*
1193  * Safely get reference count of an arbitrary page.
1194  * Returns 0 for a free page, -EIO for a zero refcount page
1195  * that is not free, and 1 for any other page type.
1196  * For 1 the page is returned with increased page count, otherwise not.
1197  */
1198 static int get_any_page(struct page *p, unsigned long pfn, int flags)
1199 {
1200         int ret;
1201
1202         if (flags & MF_COUNT_INCREASED)
1203                 return 1;
1204
1205         /*
1206          * The lock_system_sleep prevents a race with memory hotplug,
1207          * because the isolation assumes there's only a single user.
1208          * This is a big hammer, a better would be nicer.
1209          */
1210         lock_system_sleep();
1211
1212         /*
1213          * Isolate the page, so that it doesn't get reallocated if it
1214          * was free.
1215          */
1216         set_migratetype_isolate(p);
1217         if (!get_page_unless_zero(compound_head(p))) {
1218                 if (is_free_buddy_page(p)) {
1219                         pr_debug("get_any_page: %#lx free buddy page\n", pfn);
1220                         /* Set hwpoison bit while page is still isolated */
1221                         SetPageHWPoison(p);
1222                         ret = 0;
1223                 } else {
1224                         pr_debug("get_any_page: %#lx: unknown zero refcount page type %lx\n",
1225                                 pfn, p->flags);
1226                         ret = -EIO;
1227                 }
1228         } else {
1229                 /* Not a free page */
1230                 ret = 1;
1231         }
1232         unset_migratetype_isolate(p);
1233         unlock_system_sleep();
1234         return ret;
1235 }
1236
1237 /**
1238  * soft_offline_page - Soft offline a page.
1239  * @page: page to offline
1240  * @flags: flags. Same as memory_failure().
1241  *
1242  * Returns 0 on success, otherwise negated errno.
1243  *
1244  * Soft offline a page, by migration or invalidation,
1245  * without killing anything. This is for the case when
1246  * a page is not corrupted yet (so it's still valid to access),
1247  * but has had a number of corrected errors and is better taken
1248  * out.
1249  *
1250  * The actual policy on when to do that is maintained by
1251  * user space.
1252  *
1253  * This should never impact any application or cause data loss,
1254  * however it might take some time.
1255  *
1256  * This is not a 100% solution for all memory, but tries to be
1257  * ``good enough'' for the majority of memory.
1258  */
1259 int soft_offline_page(struct page *page, int flags)
1260 {
1261         int ret;
1262         unsigned long pfn = page_to_pfn(page);
1263
1264         ret = get_any_page(page, pfn, flags);
1265         if (ret < 0)
1266                 return ret;
1267         if (ret == 0)
1268                 goto done;
1269
1270         /*
1271          * Page cache page we can handle?
1272          */
1273         if (!PageLRU(page)) {
1274                 /*
1275                  * Try to free it.
1276                  */
1277                 put_page(page);
1278                 shake_page(page, 1);
1279
1280                 /*
1281                  * Did it turn free?
1282                  */
1283                 ret = get_any_page(page, pfn, 0);
1284                 if (ret < 0)
1285                         return ret;
1286                 if (ret == 0)
1287                         goto done;
1288         }
1289         if (!PageLRU(page)) {
1290                 pr_debug("soft_offline: %#lx: unknown non LRU page type %lx\n",
1291                                 pfn, page->flags);
1292                 return -EIO;
1293         }
1294
1295         lock_page(page);
1296         wait_on_page_writeback(page);
1297
1298         /*
1299          * Synchronized using the page lock with memory_failure()
1300          */
1301         if (PageHWPoison(page)) {
1302                 unlock_page(page);
1303                 put_page(page);
1304                 pr_debug("soft offline: %#lx page already poisoned\n", pfn);
1305                 return -EBUSY;
1306         }
1307
1308         /*
1309          * Try to invalidate first. This should work for
1310          * non dirty unmapped page cache pages.
1311          */
1312         ret = invalidate_inode_page(page);
1313         unlock_page(page);
1314
1315         /*
1316          * Drop count because page migration doesn't like raised
1317          * counts. The page could get re-allocated, but if it becomes
1318          * LRU the isolation will just fail.
1319          * RED-PEN would be better to keep it isolated here, but we
1320          * would need to fix isolation locking first.
1321          */
1322         put_page(page);
1323         if (ret == 1) {
1324                 ret = 0;
1325                 pr_debug("soft_offline: %#lx: invalidated\n", pfn);
1326                 goto done;
1327         }
1328
1329         /*
1330          * Simple invalidation didn't work.
1331          * Try to migrate to a new page instead. migrate.c
1332          * handles a large number of cases for us.
1333          */
1334         ret = isolate_lru_page(page);
1335         if (!ret) {
1336                 LIST_HEAD(pagelist);
1337
1338                 list_add(&page->lru, &pagelist);
1339                 ret = migrate_pages(&pagelist, new_page, MPOL_MF_MOVE_ALL, 0);
1340                 if (ret) {
1341                         pr_debug("soft offline: %#lx: migration failed %d, type %lx\n",
1342                                 pfn, ret, page->flags);
1343                         if (ret > 0)
1344                                 ret = -EIO;
1345                 }
1346         } else {
1347                 pr_debug("soft offline: %#lx: isolation failed: %d, page count %d, type %lx\n",
1348                                 pfn, ret, page_count(page), page->flags);
1349         }
1350         if (ret)
1351                 return ret;
1352
1353 done:
1354         atomic_long_add(1, &mce_bad_pages);
1355         SetPageHWPoison(page);
1356         /* keep elevated page count for bad page */
1357         return ret;
1358 }