Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 1 | /* |
Heiko Carstens | 239a642 | 2009-06-12 10:26:33 +0200 | [diff] [blame] | 2 | * Copyright IBM Corp. 2007,2009 |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 3 | * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/sched.h> |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/errno.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/gfp.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | #include <linux/swap.h> |
| 12 | #include <linux/smp.h> |
| 13 | #include <linux/highmem.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 14 | #include <linux/pagemap.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/quicklist.h> |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 18 | #include <linux/rcupdate.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 19 | |
| 20 | #include <asm/system.h> |
| 21 | #include <asm/pgtable.h> |
| 22 | #include <asm/pgalloc.h> |
| 23 | #include <asm/tlb.h> |
| 24 | #include <asm/tlbflush.h> |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 25 | #include <asm/mmu_context.h> |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 26 | |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 27 | struct rcu_table_freelist { |
| 28 | struct rcu_head rcu; |
| 29 | struct mm_struct *mm; |
| 30 | unsigned int pgt_index; |
| 31 | unsigned int crst_index; |
| 32 | unsigned long *table[0]; |
| 33 | }; |
| 34 | |
| 35 | #define RCU_FREELIST_SIZE \ |
| 36 | ((PAGE_SIZE - sizeof(struct rcu_table_freelist)) \ |
| 37 | / sizeof(unsigned long)) |
| 38 | |
| 39 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); |
| 40 | static DEFINE_PER_CPU(struct rcu_table_freelist *, rcu_table_freelist); |
| 41 | |
| 42 | static void __page_table_free(struct mm_struct *mm, unsigned long *table); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 43 | |
| 44 | static struct rcu_table_freelist *rcu_table_freelist_get(struct mm_struct *mm) |
| 45 | { |
| 46 | struct rcu_table_freelist **batchp = &__get_cpu_var(rcu_table_freelist); |
| 47 | struct rcu_table_freelist *batch = *batchp; |
| 48 | |
| 49 | if (batch) |
| 50 | return batch; |
| 51 | batch = (struct rcu_table_freelist *) __get_free_page(GFP_ATOMIC); |
| 52 | if (batch) { |
| 53 | batch->mm = mm; |
| 54 | batch->pgt_index = 0; |
| 55 | batch->crst_index = RCU_FREELIST_SIZE; |
| 56 | *batchp = batch; |
| 57 | } |
| 58 | return batch; |
| 59 | } |
| 60 | |
| 61 | static void rcu_table_freelist_callback(struct rcu_head *head) |
| 62 | { |
| 63 | struct rcu_table_freelist *batch = |
| 64 | container_of(head, struct rcu_table_freelist, rcu); |
| 65 | |
| 66 | while (batch->pgt_index > 0) |
| 67 | __page_table_free(batch->mm, batch->table[--batch->pgt_index]); |
| 68 | while (batch->crst_index < RCU_FREELIST_SIZE) |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame^] | 69 | crst_table_free(batch->mm, batch->table[batch->crst_index++]); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 70 | free_page((unsigned long) batch); |
| 71 | } |
| 72 | |
| 73 | void rcu_table_freelist_finish(void) |
| 74 | { |
| 75 | struct rcu_table_freelist *batch = __get_cpu_var(rcu_table_freelist); |
| 76 | |
| 77 | if (!batch) |
| 78 | return; |
| 79 | call_rcu(&batch->rcu, rcu_table_freelist_callback); |
| 80 | __get_cpu_var(rcu_table_freelist) = NULL; |
| 81 | } |
| 82 | |
| 83 | static void smp_sync(void *arg) |
| 84 | { |
| 85 | } |
| 86 | |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 87 | #ifndef CONFIG_64BIT |
| 88 | #define ALLOC_ORDER 1 |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 89 | #define TABLES_PER_PAGE 4 |
| 90 | #define FRAG_MASK 15UL |
| 91 | #define SECOND_HALVES 10UL |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 92 | |
| 93 | void clear_table_pgstes(unsigned long *table) |
| 94 | { |
| 95 | clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/4); |
| 96 | memset(table + 256, 0, PAGE_SIZE/4); |
| 97 | clear_table(table + 512, _PAGE_TYPE_EMPTY, PAGE_SIZE/4); |
| 98 | memset(table + 768, 0, PAGE_SIZE/4); |
| 99 | } |
| 100 | |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 101 | #else |
| 102 | #define ALLOC_ORDER 2 |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 103 | #define TABLES_PER_PAGE 2 |
| 104 | #define FRAG_MASK 3UL |
| 105 | #define SECOND_HALVES 2UL |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 106 | |
| 107 | void clear_table_pgstes(unsigned long *table) |
| 108 | { |
| 109 | clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/2); |
| 110 | memset(table + 256, 0, PAGE_SIZE/2); |
| 111 | } |
| 112 | |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 113 | #endif |
| 114 | |
Heiko Carstens | 239a642 | 2009-06-12 10:26:33 +0200 | [diff] [blame] | 115 | unsigned long VMALLOC_START = VMALLOC_END - VMALLOC_SIZE; |
| 116 | EXPORT_SYMBOL(VMALLOC_START); |
| 117 | |
| 118 | static int __init parse_vmalloc(char *arg) |
| 119 | { |
| 120 | if (!arg) |
| 121 | return -EINVAL; |
| 122 | VMALLOC_START = (VMALLOC_END - memparse(arg, &arg)) & PAGE_MASK; |
| 123 | return 0; |
| 124 | } |
| 125 | early_param("vmalloc", parse_vmalloc); |
| 126 | |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame^] | 127 | unsigned long *crst_table_alloc(struct mm_struct *mm) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 128 | { |
| 129 | struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER); |
| 130 | |
| 131 | if (!page) |
| 132 | return NULL; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 133 | return (unsigned long *) page_to_phys(page); |
| 134 | } |
| 135 | |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 136 | void crst_table_free(struct mm_struct *mm, unsigned long *table) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 137 | { |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame^] | 138 | free_pages((unsigned long) table, ALLOC_ORDER); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void crst_table_free_rcu(struct mm_struct *mm, unsigned long *table) |
| 142 | { |
| 143 | struct rcu_table_freelist *batch; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 144 | |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 145 | if (atomic_read(&mm->mm_users) < 2 && |
| 146 | cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id()))) { |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame^] | 147 | crst_table_free(mm, table); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | batch = rcu_table_freelist_get(mm); |
| 151 | if (!batch) { |
| 152 | smp_call_function(smp_sync, NULL, 1); |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame^] | 153 | crst_table_free(mm, table); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | batch->table[--batch->crst_index] = table; |
| 157 | if (batch->pgt_index >= batch->crst_index) |
| 158 | rcu_table_freelist_finish(); |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 161 | #ifdef CONFIG_64BIT |
| 162 | int crst_table_upgrade(struct mm_struct *mm, unsigned long limit) |
| 163 | { |
| 164 | unsigned long *table, *pgd; |
| 165 | unsigned long entry; |
| 166 | |
| 167 | BUG_ON(limit > (1UL << 53)); |
| 168 | repeat: |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame^] | 169 | table = crst_table_alloc(mm); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 170 | if (!table) |
| 171 | return -ENOMEM; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 172 | spin_lock_bh(&mm->page_table_lock); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 173 | if (mm->context.asce_limit < limit) { |
| 174 | pgd = (unsigned long *) mm->pgd; |
| 175 | if (mm->context.asce_limit <= (1UL << 31)) { |
| 176 | entry = _REGION3_ENTRY_EMPTY; |
| 177 | mm->context.asce_limit = 1UL << 42; |
| 178 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 179 | _ASCE_USER_BITS | |
| 180 | _ASCE_TYPE_REGION3; |
| 181 | } else { |
| 182 | entry = _REGION2_ENTRY_EMPTY; |
| 183 | mm->context.asce_limit = 1UL << 53; |
| 184 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 185 | _ASCE_USER_BITS | |
| 186 | _ASCE_TYPE_REGION2; |
| 187 | } |
| 188 | crst_table_init(table, entry); |
| 189 | pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd); |
| 190 | mm->pgd = (pgd_t *) table; |
Martin Schwidefsky | f481bfa | 2009-03-18 13:27:36 +0100 | [diff] [blame] | 191 | mm->task_size = mm->context.asce_limit; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 192 | table = NULL; |
| 193 | } |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 194 | spin_unlock_bh(&mm->page_table_lock); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 195 | if (table) |
| 196 | crst_table_free(mm, table); |
| 197 | if (mm->context.asce_limit < limit) |
| 198 | goto repeat; |
| 199 | update_mm(mm, current); |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | void crst_table_downgrade(struct mm_struct *mm, unsigned long limit) |
| 204 | { |
| 205 | pgd_t *pgd; |
| 206 | |
| 207 | if (mm->context.asce_limit <= limit) |
| 208 | return; |
| 209 | __tlb_flush_mm(mm); |
| 210 | while (mm->context.asce_limit > limit) { |
| 211 | pgd = mm->pgd; |
| 212 | switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) { |
| 213 | case _REGION_ENTRY_TYPE_R2: |
| 214 | mm->context.asce_limit = 1UL << 42; |
| 215 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 216 | _ASCE_USER_BITS | |
| 217 | _ASCE_TYPE_REGION3; |
| 218 | break; |
| 219 | case _REGION_ENTRY_TYPE_R3: |
| 220 | mm->context.asce_limit = 1UL << 31; |
| 221 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 222 | _ASCE_USER_BITS | |
| 223 | _ASCE_TYPE_SEGMENT; |
| 224 | break; |
| 225 | default: |
| 226 | BUG(); |
| 227 | } |
| 228 | mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN); |
Martin Schwidefsky | f481bfa | 2009-03-18 13:27:36 +0100 | [diff] [blame] | 229 | mm->task_size = mm->context.asce_limit; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 230 | crst_table_free(mm, (unsigned long *) pgd); |
| 231 | } |
| 232 | update_mm(mm, current); |
| 233 | } |
| 234 | #endif |
| 235 | |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 236 | /* |
| 237 | * page table entry allocation/free routines. |
| 238 | */ |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 239 | unsigned long *page_table_alloc(struct mm_struct *mm) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 240 | { |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 241 | struct page *page; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 242 | unsigned long *table; |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 243 | unsigned long bits; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 244 | |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame^] | 245 | bits = (mm->context.has_pgste) ? 3UL : 1UL; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 246 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 247 | page = NULL; |
| 248 | if (!list_empty(&mm->context.pgtable_list)) { |
| 249 | page = list_first_entry(&mm->context.pgtable_list, |
| 250 | struct page, lru); |
| 251 | if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1)) |
| 252 | page = NULL; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 253 | } |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 254 | if (!page) { |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 255 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 256 | page = alloc_page(GFP_KERNEL|__GFP_REPEAT); |
| 257 | if (!page) |
| 258 | return NULL; |
| 259 | pgtable_page_ctor(page); |
| 260 | page->flags &= ~FRAG_MASK; |
| 261 | table = (unsigned long *) page_to_phys(page); |
Christian Borntraeger | 250cf77 | 2008-10-28 11:10:15 +0100 | [diff] [blame] | 262 | if (mm->context.has_pgste) |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 263 | clear_table_pgstes(table); |
| 264 | else |
| 265 | clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 266 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 267 | list_add(&page->lru, &mm->context.pgtable_list); |
| 268 | } |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 269 | table = (unsigned long *) page_to_phys(page); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 270 | while (page->flags & bits) { |
| 271 | table += 256; |
| 272 | bits <<= 1; |
| 273 | } |
| 274 | page->flags |= bits; |
| 275 | if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1)) |
| 276 | list_move_tail(&page->lru, &mm->context.pgtable_list); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 277 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 278 | return table; |
| 279 | } |
| 280 | |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 281 | static void __page_table_free(struct mm_struct *mm, unsigned long *table) |
| 282 | { |
| 283 | struct page *page; |
| 284 | unsigned long bits; |
| 285 | |
| 286 | bits = ((unsigned long) table) & 15; |
| 287 | table = (unsigned long *)(((unsigned long) table) ^ bits); |
| 288 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 289 | page->flags ^= bits; |
| 290 | if (!(page->flags & FRAG_MASK)) { |
| 291 | pgtable_page_dtor(page); |
| 292 | __free_page(page); |
| 293 | } |
| 294 | } |
| 295 | |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 296 | void page_table_free(struct mm_struct *mm, unsigned long *table) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 297 | { |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 298 | struct page *page; |
| 299 | unsigned long bits; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 300 | |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame^] | 301 | bits = (mm->context.has_pgste) ? 3UL : 1UL; |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 302 | bits <<= (__pa(table) & (PAGE_SIZE - 1)) / 256 / sizeof(unsigned long); |
| 303 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 304 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 305 | page->flags ^= bits; |
| 306 | if (page->flags & FRAG_MASK) { |
| 307 | /* Page now has some free pgtable fragments. */ |
Martin Schwidefsky | f1be77b | 2011-01-31 11:30:04 +0100 | [diff] [blame] | 308 | if (!list_empty(&page->lru)) |
| 309 | list_move(&page->lru, &mm->context.pgtable_list); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 310 | page = NULL; |
| 311 | } else |
| 312 | /* All fragments of the 4K page have been freed. */ |
| 313 | list_del(&page->lru); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 314 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 315 | if (page) { |
| 316 | pgtable_page_dtor(page); |
| 317 | __free_page(page); |
| 318 | } |
| 319 | } |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 320 | |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 321 | void page_table_free_rcu(struct mm_struct *mm, unsigned long *table) |
| 322 | { |
| 323 | struct rcu_table_freelist *batch; |
| 324 | struct page *page; |
| 325 | unsigned long bits; |
| 326 | |
| 327 | if (atomic_read(&mm->mm_users) < 2 && |
| 328 | cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id()))) { |
| 329 | page_table_free(mm, table); |
| 330 | return; |
| 331 | } |
| 332 | batch = rcu_table_freelist_get(mm); |
| 333 | if (!batch) { |
| 334 | smp_call_function(smp_sync, NULL, 1); |
| 335 | page_table_free(mm, table); |
| 336 | return; |
| 337 | } |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame^] | 338 | bits = (mm->context.has_pgste) ? 3UL : 1UL; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 339 | bits <<= (__pa(table) & (PAGE_SIZE - 1)) / 256 / sizeof(unsigned long); |
| 340 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 341 | spin_lock_bh(&mm->context.list_lock); |
| 342 | /* Delayed freeing with rcu prevents reuse of pgtable fragments */ |
| 343 | list_del_init(&page->lru); |
| 344 | spin_unlock_bh(&mm->context.list_lock); |
| 345 | table = (unsigned long *)(((unsigned long) table) | bits); |
| 346 | batch->table[batch->pgt_index++] = table; |
| 347 | if (batch->pgt_index >= batch->crst_index) |
| 348 | rcu_table_freelist_finish(); |
| 349 | } |
| 350 | |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 351 | /* |
| 352 | * switch on pgstes for its userspace process (for kvm) |
| 353 | */ |
| 354 | int s390_enable_sie(void) |
| 355 | { |
| 356 | struct task_struct *tsk = current; |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 357 | struct mm_struct *mm, *old_mm; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 358 | |
Carsten Otte | 702d9e5 | 2009-03-26 15:23:57 +0100 | [diff] [blame] | 359 | /* Do we have switched amode? If no, we cannot do sie */ |
Martin Schwidefsky | b11b533 | 2009-12-07 12:51:43 +0100 | [diff] [blame] | 360 | if (user_mode == HOME_SPACE_MODE) |
Carsten Otte | 702d9e5 | 2009-03-26 15:23:57 +0100 | [diff] [blame] | 361 | return -EINVAL; |
| 362 | |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 363 | /* Do we have pgstes? if yes, we are done */ |
Christian Borntraeger | 250cf77 | 2008-10-28 11:10:15 +0100 | [diff] [blame] | 364 | if (tsk->mm->context.has_pgste) |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 365 | return 0; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 366 | |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 367 | /* lets check if we are allowed to replace the mm */ |
| 368 | task_lock(tsk); |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 369 | if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 || |
Martin Schwidefsky | 52a21f2 | 2009-10-06 10:33:55 +0200 | [diff] [blame] | 370 | #ifdef CONFIG_AIO |
| 371 | !hlist_empty(&tsk->mm->ioctx_list) || |
| 372 | #endif |
| 373 | tsk->mm != tsk->active_mm) { |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 374 | task_unlock(tsk); |
| 375 | return -EINVAL; |
| 376 | } |
| 377 | task_unlock(tsk); |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 378 | |
Christian Borntraeger | 250cf77 | 2008-10-28 11:10:15 +0100 | [diff] [blame] | 379 | /* we copy the mm and let dup_mm create the page tables with_pgstes */ |
| 380 | tsk->mm->context.alloc_pgste = 1; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 381 | mm = dup_mm(tsk); |
Christian Borntraeger | 250cf77 | 2008-10-28 11:10:15 +0100 | [diff] [blame] | 382 | tsk->mm->context.alloc_pgste = 0; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 383 | if (!mm) |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 384 | return -ENOMEM; |
| 385 | |
Christian Borntraeger | 250cf77 | 2008-10-28 11:10:15 +0100 | [diff] [blame] | 386 | /* Now lets check again if something happened */ |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 387 | task_lock(tsk); |
| 388 | if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 || |
Martin Schwidefsky | 52a21f2 | 2009-10-06 10:33:55 +0200 | [diff] [blame] | 389 | #ifdef CONFIG_AIO |
| 390 | !hlist_empty(&tsk->mm->ioctx_list) || |
| 391 | #endif |
| 392 | tsk->mm != tsk->active_mm) { |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 393 | mmput(mm); |
| 394 | task_unlock(tsk); |
| 395 | return -EINVAL; |
| 396 | } |
| 397 | |
| 398 | /* ok, we are alone. No ptrace, no threads, etc. */ |
| 399 | old_mm = tsk->mm; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 400 | tsk->mm = tsk->active_mm = mm; |
| 401 | preempt_disable(); |
| 402 | update_mm(mm, tsk); |
Christian Borntraeger | e05ef9b | 2010-10-25 16:10:45 +0200 | [diff] [blame] | 403 | atomic_inc(&mm->context.attach_count); |
| 404 | atomic_dec(&old_mm->context.attach_count); |
Rusty Russell | 005f8ee | 2009-03-26 15:25:01 +0100 | [diff] [blame] | 405 | cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm)); |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 406 | preempt_enable(); |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 407 | task_unlock(tsk); |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 408 | mmput(old_mm); |
| 409 | return 0; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 410 | } |
| 411 | EXPORT_SYMBOL_GPL(s390_enable_sie); |
Hans-Joachim Picht | 7db11a3 | 2009-06-16 10:30:26 +0200 | [diff] [blame] | 412 | |
Heiko Carstens | 87458ff | 2009-09-22 22:58:46 +0200 | [diff] [blame] | 413 | #if defined(CONFIG_DEBUG_PAGEALLOC) && defined(CONFIG_HIBERNATION) |
Hans-Joachim Picht | 7db11a3 | 2009-06-16 10:30:26 +0200 | [diff] [blame] | 414 | bool kernel_page_present(struct page *page) |
| 415 | { |
| 416 | unsigned long addr; |
| 417 | int cc; |
| 418 | |
| 419 | addr = page_to_phys(page); |
Heiko Carstens | 87458ff | 2009-09-22 22:58:46 +0200 | [diff] [blame] | 420 | asm volatile( |
| 421 | " lra %1,0(%1)\n" |
| 422 | " ipm %0\n" |
| 423 | " srl %0,28" |
| 424 | : "=d" (cc), "+a" (addr) : : "cc"); |
Hans-Joachim Picht | 7db11a3 | 2009-06-16 10:30:26 +0200 | [diff] [blame] | 425 | return cc == 0; |
| 426 | } |
Heiko Carstens | 87458ff | 2009-09-22 22:58:46 +0200 | [diff] [blame] | 427 | #endif /* CONFIG_HIBERNATION && CONFIG_DEBUG_PAGEALLOC */ |