Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 1 | /* include/asm-generic/tlb.h |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 8 | * Copyright 2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
| 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version |
| 13 | * 2 of the License, or (at your option) any later version. |
| 14 | */ |
| 15 | #ifndef _ASM_GENERIC__TLB_H |
| 16 | #define _ASM_GENERIC__TLB_H |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/swap.h> |
Ingo Molnar | 62152d0 | 2008-01-31 22:05:48 +0100 | [diff] [blame] | 19 | #include <asm/pgalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #define tlb_fast_mode(tlb) ((tlb)->nr == ~0U) |
| 28 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #define tlb_fast_mode(tlb) 1 |
| 30 | #endif |
| 31 | |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 32 | /* |
| 33 | * If we can't allocate a page to make a big batch of page pointers |
| 34 | * to work on, then just handle a few from the on-stack structure. |
| 35 | */ |
| 36 | #define MMU_GATHER_BUNDLE 8 |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* struct mmu_gather is an opaque type used by the mm code for passing around |
Hugh Dickins | 15a23ff | 2005-10-29 18:16:01 -0700 | [diff] [blame] | 39 | * any data needed by arch specific code for tlb_remove_page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | */ |
| 41 | struct mmu_gather { |
| 42 | struct mm_struct *mm; |
| 43 | unsigned int nr; /* set to ~0U means fast mode */ |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 44 | unsigned int max; /* nr < max */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | unsigned int need_flush;/* Really unmapped some ptes? */ |
| 46 | unsigned int fullmm; /* non-zero means full mm flush */ |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 47 | #ifdef HAVE_ARCH_MMU_GATHER |
| 48 | struct arch_mmu_gather arch; |
| 49 | #endif |
| 50 | struct page **pages; |
| 51 | struct page *local[MMU_GATHER_BUNDLE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 54 | static inline void __tlb_alloc_page(struct mmu_gather *tlb) |
| 55 | { |
| 56 | unsigned long addr = __get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0); |
| 57 | |
| 58 | if (addr) { |
| 59 | tlb->pages = (void *)addr; |
| 60 | tlb->max = PAGE_SIZE / sizeof(struct page *); |
| 61 | } |
| 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* tlb_gather_mmu |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 65 | * Called to initialize an (on-stack) mmu_gather structure for page-table |
| 66 | * tear-down from @mm. The @fullmm argument is used when @mm is without |
| 67 | * users and we're going to destroy the full address space (exit/execve). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | */ |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 69 | static inline void |
| 70 | tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | tlb->mm = mm; |
| 73 | |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 74 | tlb->max = ARRAY_SIZE(tlb->local); |
| 75 | tlb->pages = tlb->local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 77 | if (num_online_cpus() > 1) { |
| 78 | tlb->nr = 0; |
| 79 | __tlb_alloc_page(tlb); |
| 80 | } else /* Use fast mode if only one CPU is online */ |
| 81 | tlb->nr = ~0U; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 83 | tlb->fullmm = fullmm; |
| 84 | |
| 85 | #ifdef HAVE_ARCH_MMU_GATHER |
| 86 | tlb->arch = ARCH_MMU_GATHER_INIT; |
| 87 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static inline void |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 91 | tlb_flush_mmu(struct mmu_gather *tlb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
| 93 | if (!tlb->need_flush) |
| 94 | return; |
| 95 | tlb->need_flush = 0; |
| 96 | tlb_flush(tlb); |
| 97 | if (!tlb_fast_mode(tlb)) { |
| 98 | free_pages_and_swap_cache(tlb->pages, tlb->nr); |
| 99 | tlb->nr = 0; |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 100 | /* |
| 101 | * If we are using the local on-stack array of pages for MMU |
| 102 | * gather, try allocating an off-stack array again as we have |
| 103 | * recently freed pages. |
| 104 | */ |
| 105 | if (tlb->pages == tlb->local) |
| 106 | __tlb_alloc_page(tlb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | /* tlb_finish_mmu |
| 111 | * Called at the end of the shootdown operation to free up any resources |
Hugh Dickins | 15a23ff | 2005-10-29 18:16:01 -0700 | [diff] [blame] | 112 | * that were required. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | */ |
| 114 | static inline void |
| 115 | tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end) |
| 116 | { |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 117 | tlb_flush_mmu(tlb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | /* keep the page table cache within bounds */ |
| 120 | check_pgt_cache(); |
Hugh Dickins | 15a23ff | 2005-10-29 18:16:01 -0700 | [diff] [blame] | 121 | |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 122 | if (tlb->pages != tlb->local) |
| 123 | free_pages((unsigned long)tlb->pages, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 126 | /* __tlb_remove_page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while |
| 128 | * handling the additional races in SMP caused by other CPUs caching valid |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 129 | * mappings in their TLBs. Returns the number of free page slots left. |
| 130 | * When out of page slots we must call tlb_flush_mmu(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | */ |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 132 | static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | tlb->need_flush = 1; |
| 135 | if (tlb_fast_mode(tlb)) { |
| 136 | free_page_and_swap_cache(page); |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 137 | return 1; /* avoid calling tlb_flush_mmu() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | tlb->pages[tlb->nr++] = page; |
Peter Zijlstra | d16dfc5 | 2011-05-24 17:11:45 -0700 | [diff] [blame^] | 140 | VM_BUG_ON(tlb->nr > tlb->max); |
| 141 | |
| 142 | return tlb->max - tlb->nr; |
| 143 | } |
| 144 | |
| 145 | /* tlb_remove_page |
| 146 | * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when |
| 147 | * required. |
| 148 | */ |
| 149 | static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page) |
| 150 | { |
| 151 | if (!__tlb_remove_page(tlb, page)) |
| 152 | tlb_flush_mmu(tlb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation. |
| 157 | * |
| 158 | * Record the fact that pte's were really umapped in ->need_flush, so we can |
| 159 | * later optimise away the tlb invalidate. This helps when userspace is |
| 160 | * unmapping already-unmapped pages, which happens quite a lot. |
| 161 | */ |
| 162 | #define tlb_remove_tlb_entry(tlb, ptep, address) \ |
| 163 | do { \ |
| 164 | tlb->need_flush = 1; \ |
| 165 | __tlb_remove_tlb_entry(tlb, ptep, address); \ |
| 166 | } while (0) |
| 167 | |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 168 | #define pte_free_tlb(tlb, ptep, address) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | do { \ |
| 170 | tlb->need_flush = 1; \ |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 171 | __pte_free_tlb(tlb, ptep, address); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } while (0) |
| 173 | |
| 174 | #ifndef __ARCH_HAS_4LEVEL_HACK |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 175 | #define pud_free_tlb(tlb, pudp, address) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | do { \ |
| 177 | tlb->need_flush = 1; \ |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 178 | __pud_free_tlb(tlb, pudp, address); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } while (0) |
| 180 | #endif |
| 181 | |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 182 | #define pmd_free_tlb(tlb, pmdp, address) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | do { \ |
| 184 | tlb->need_flush = 1; \ |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 185 | __pmd_free_tlb(tlb, pmdp, address); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } while (0) |
| 187 | |
| 188 | #define tlb_migrate_finish(mm) do {} while (0) |
| 189 | |
| 190 | #endif /* _ASM_GENERIC__TLB_H */ |