vserver 2.0 rc7
[linux-2.6.git] / include / asm-generic / tlb.h
1 /* asm-generic/tlb.h
2  *
3  *      Generic TLB shootdown code
4  *
5  * Copyright 2001 Red Hat, Inc.
6  * Based on code from mm/memory.c Copyright Linus Torvalds and others.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version
11  * 2 of the License, or (at your option) any later version.
12  */
13 #ifndef _ASM_GENERIC__TLB_H
14 #define _ASM_GENERIC__TLB_H
15
16 #include <linux/config.h>
17 #include <linux/swap.h>
18 #include <linux/vs_memory.h>
19 #include <asm/pgalloc.h>
20 #include <asm/tlbflush.h>
21
22 /*
23  * For UP we don't need to worry about TLB flush
24  * and page free order so much..
25  */
26 #ifdef CONFIG_SMP
27   #define FREE_PTE_NR   506
28   #define tlb_fast_mode(tlb) ((tlb)->nr == ~0U)
29 #else
30   #define FREE_PTE_NR   1
31   #define tlb_fast_mode(tlb) 1
32 #endif
33
34 /* struct mmu_gather is an opaque type used by the mm code for passing around
35  * any data needed by arch specific code for tlb_remove_page.  This structure
36  * can be per-CPU or per-MM as the page table lock is held for the duration of
37  * TLB shootdown.
38  */
39 struct mmu_gather {
40         struct mm_struct        *mm;
41         unsigned int            nr;     /* set to ~0U means fast mode */
42         unsigned int            need_flush;/* Really unmapped some ptes? */
43         unsigned int            fullmm; /* non-zero means full mm flush */
44         unsigned long           freed;
45         struct page *           pages[FREE_PTE_NR];
46 };
47
48 /* Users of the generic TLB shootdown code must declare this storage space. */
49 DECLARE_PER_CPU(struct mmu_gather, mmu_gathers);
50
51 /* tlb_gather_mmu
52  *      Return a pointer to an initialized struct mmu_gather.
53  */
54 static inline struct mmu_gather *
55 tlb_gather_mmu(struct mm_struct *mm, unsigned int full_mm_flush)
56 {
57         struct mmu_gather *tlb = &per_cpu(mmu_gathers, smp_processor_id());
58
59         tlb->mm = mm;
60
61         /* Use fast mode if only one CPU is online */
62         tlb->nr = num_online_cpus() > 1 ? 0U : ~0U;
63
64         tlb->fullmm = full_mm_flush;
65         tlb->freed = 0;
66
67         return tlb;
68 }
69
70 static inline void
71 tlb_flush_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
72 {
73         if (!tlb->need_flush)
74                 return;
75         tlb->need_flush = 0;
76         tlb_flush(tlb);
77         if (!tlb_fast_mode(tlb)) {
78                 free_pages_and_swap_cache(tlb->pages, tlb->nr);
79                 tlb->nr = 0;
80         }
81 }
82
83 /* tlb_finish_mmu
84  *      Called at the end of the shootdown operation to free up any resources
85  *      that were required.  The page table lock is still held at this point.
86  */
87 static inline void
88 tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
89 {
90         int freed = tlb->freed;
91         struct mm_struct *mm = tlb->mm;
92         int rss = get_mm_counter(mm, rss);
93
94         if (rss < freed)
95                 freed = rss;
96         add_mm_counter(mm, rss, -freed);
97         tlb_flush_mmu(tlb, start, end);
98
99         /* keep the page table cache within bounds */
100         check_pgt_cache();
101 }
102
103 static inline unsigned int
104 tlb_is_full_mm(struct mmu_gather *tlb)
105 {
106         return tlb->fullmm;
107 }
108
109 /* tlb_remove_page
110  *      Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
111  *      handling the additional races in SMP caused by other CPUs caching valid
112  *      mappings in their TLBs.
113  */
114 static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
115 {
116         tlb->need_flush = 1;
117         if (tlb_fast_mode(tlb)) {
118                 free_page_and_swap_cache(page);
119                 return;
120         }
121         tlb->pages[tlb->nr++] = page;
122         if (tlb->nr >= FREE_PTE_NR)
123                 tlb_flush_mmu(tlb, 0, 0);
124 }
125
126 /**
127  * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
128  *
129  * Record the fact that pte's were really umapped in ->need_flush, so we can
130  * later optimise away the tlb invalidate.   This helps when userspace is
131  * unmapping already-unmapped pages, which happens quite a lot.
132  */
133 #define tlb_remove_tlb_entry(tlb, ptep, address)                \
134         do {                                                    \
135                 tlb->need_flush = 1;                            \
136                 __tlb_remove_tlb_entry(tlb, ptep, address);     \
137         } while (0)
138
139 #define pte_free_tlb(tlb, ptep)                                 \
140         do {                                                    \
141                 tlb->need_flush = 1;                            \
142                 __pte_free_tlb(tlb, ptep);                      \
143         } while (0)
144
145 #ifndef __ARCH_HAS_4LEVEL_HACK
146 #define pud_free_tlb(tlb, pudp)                                 \
147         do {                                                    \
148                 tlb->need_flush = 1;                            \
149                 __pud_free_tlb(tlb, pudp);                      \
150         } while (0)
151 #endif
152
153 #define pmd_free_tlb(tlb, pmdp)                                 \
154         do {                                                    \
155                 tlb->need_flush = 1;                            \
156                 __pmd_free_tlb(tlb, pmdp);                      \
157         } while (0)
158
159 #define tlb_migrate_finish(mm) do {} while (0)
160
161 #endif /* _ASM_GENERIC__TLB_H */