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 | |
| 27 | #ifndef CONFIG_64BIT |
| 28 | #define ALLOC_ORDER 1 |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 29 | #define FRAG_MASK 0x0f |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 30 | #else |
| 31 | #define ALLOC_ORDER 2 |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 32 | #define FRAG_MASK 0x03 |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 33 | #endif |
| 34 | |
Heiko Carstens | 239a642 | 2009-06-12 10:26:33 +0200 | [diff] [blame] | 35 | unsigned long VMALLOC_START = VMALLOC_END - VMALLOC_SIZE; |
| 36 | EXPORT_SYMBOL(VMALLOC_START); |
| 37 | |
| 38 | static int __init parse_vmalloc(char *arg) |
| 39 | { |
| 40 | if (!arg) |
| 41 | return -EINVAL; |
| 42 | VMALLOC_START = (VMALLOC_END - memparse(arg, &arg)) & PAGE_MASK; |
| 43 | return 0; |
| 44 | } |
| 45 | early_param("vmalloc", parse_vmalloc); |
| 46 | |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame] | 47 | unsigned long *crst_table_alloc(struct mm_struct *mm) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 48 | { |
| 49 | struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER); |
| 50 | |
| 51 | if (!page) |
| 52 | return NULL; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 53 | return (unsigned long *) page_to_phys(page); |
| 54 | } |
| 55 | |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 56 | void crst_table_free(struct mm_struct *mm, unsigned long *table) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 57 | { |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame] | 58 | free_pages((unsigned long) table, ALLOC_ORDER); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 61 | #ifdef CONFIG_64BIT |
| 62 | int crst_table_upgrade(struct mm_struct *mm, unsigned long limit) |
| 63 | { |
| 64 | unsigned long *table, *pgd; |
| 65 | unsigned long entry; |
| 66 | |
| 67 | BUG_ON(limit > (1UL << 53)); |
| 68 | repeat: |
Martin Schwidefsky | 043d070 | 2011-05-23 10:24:23 +0200 | [diff] [blame] | 69 | table = crst_table_alloc(mm); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 70 | if (!table) |
| 71 | return -ENOMEM; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 72 | spin_lock_bh(&mm->page_table_lock); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 73 | if (mm->context.asce_limit < limit) { |
| 74 | pgd = (unsigned long *) mm->pgd; |
| 75 | if (mm->context.asce_limit <= (1UL << 31)) { |
| 76 | entry = _REGION3_ENTRY_EMPTY; |
| 77 | mm->context.asce_limit = 1UL << 42; |
| 78 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 79 | _ASCE_USER_BITS | |
| 80 | _ASCE_TYPE_REGION3; |
| 81 | } else { |
| 82 | entry = _REGION2_ENTRY_EMPTY; |
| 83 | mm->context.asce_limit = 1UL << 53; |
| 84 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 85 | _ASCE_USER_BITS | |
| 86 | _ASCE_TYPE_REGION2; |
| 87 | } |
| 88 | crst_table_init(table, entry); |
| 89 | pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd); |
| 90 | mm->pgd = (pgd_t *) table; |
Martin Schwidefsky | f481bfa | 2009-03-18 13:27:36 +0100 | [diff] [blame] | 91 | mm->task_size = mm->context.asce_limit; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 92 | table = NULL; |
| 93 | } |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 94 | spin_unlock_bh(&mm->page_table_lock); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 95 | if (table) |
| 96 | crst_table_free(mm, table); |
| 97 | if (mm->context.asce_limit < limit) |
| 98 | goto repeat; |
| 99 | update_mm(mm, current); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | void crst_table_downgrade(struct mm_struct *mm, unsigned long limit) |
| 104 | { |
| 105 | pgd_t *pgd; |
| 106 | |
| 107 | if (mm->context.asce_limit <= limit) |
| 108 | return; |
| 109 | __tlb_flush_mm(mm); |
| 110 | while (mm->context.asce_limit > limit) { |
| 111 | pgd = mm->pgd; |
| 112 | switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) { |
| 113 | case _REGION_ENTRY_TYPE_R2: |
| 114 | mm->context.asce_limit = 1UL << 42; |
| 115 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 116 | _ASCE_USER_BITS | |
| 117 | _ASCE_TYPE_REGION3; |
| 118 | break; |
| 119 | case _REGION_ENTRY_TYPE_R3: |
| 120 | mm->context.asce_limit = 1UL << 31; |
| 121 | mm->context.asce_bits = _ASCE_TABLE_LENGTH | |
| 122 | _ASCE_USER_BITS | |
| 123 | _ASCE_TYPE_SEGMENT; |
| 124 | break; |
| 125 | default: |
| 126 | BUG(); |
| 127 | } |
| 128 | mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN); |
Martin Schwidefsky | f481bfa | 2009-03-18 13:27:36 +0100 | [diff] [blame] | 129 | mm->task_size = mm->context.asce_limit; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 130 | crst_table_free(mm, (unsigned long *) pgd); |
| 131 | } |
| 132 | update_mm(mm, current); |
| 133 | } |
| 134 | #endif |
| 135 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 136 | static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits) |
| 137 | { |
| 138 | unsigned int old, new; |
| 139 | |
| 140 | do { |
| 141 | old = atomic_read(v); |
| 142 | new = old ^ bits; |
| 143 | } while (atomic_cmpxchg(v, old, new) != old); |
| 144 | return new; |
| 145 | } |
| 146 | |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 147 | /* |
| 148 | * page table entry allocation/free routines. |
| 149 | */ |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 150 | #ifdef CONFIG_PGSTE |
| 151 | static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm) |
| 152 | { |
| 153 | struct page *page; |
| 154 | unsigned long *table; |
| 155 | |
| 156 | page = alloc_page(GFP_KERNEL|__GFP_REPEAT); |
| 157 | if (!page) |
| 158 | return NULL; |
| 159 | pgtable_page_ctor(page); |
| 160 | atomic_set(&page->_mapcount, 3); |
| 161 | table = (unsigned long *) page_to_phys(page); |
| 162 | clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/2); |
| 163 | clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2); |
| 164 | return table; |
| 165 | } |
| 166 | |
| 167 | static inline void page_table_free_pgste(unsigned long *table) |
| 168 | { |
| 169 | struct page *page; |
| 170 | |
| 171 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 172 | pgtable_page_ctor(page); |
| 173 | atomic_set(&page->_mapcount, -1); |
| 174 | __free_page(page); |
| 175 | } |
| 176 | #endif |
| 177 | |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 178 | unsigned long *page_table_alloc(struct mm_struct *mm) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 179 | { |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 180 | struct page *page; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 181 | unsigned long *table; |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 182 | unsigned int mask, bit; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 183 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 184 | #ifdef CONFIG_PGSTE |
| 185 | if (mm_has_pgste(mm)) |
| 186 | return page_table_alloc_pgste(mm); |
| 187 | #endif |
| 188 | /* Allocate fragments of a 4K page as 1K/2K page table */ |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 189 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 190 | mask = FRAG_MASK; |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 191 | if (!list_empty(&mm->context.pgtable_list)) { |
| 192 | page = list_first_entry(&mm->context.pgtable_list, |
| 193 | struct page, lru); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 194 | table = (unsigned long *) page_to_phys(page); |
| 195 | mask = atomic_read(&page->_mapcount); |
| 196 | mask = mask | (mask >> 4); |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 197 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 198 | if ((mask & FRAG_MASK) == FRAG_MASK) { |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 199 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 200 | page = alloc_page(GFP_KERNEL|__GFP_REPEAT); |
| 201 | if (!page) |
| 202 | return NULL; |
| 203 | pgtable_page_ctor(page); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 204 | atomic_set(&page->_mapcount, 1); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 205 | table = (unsigned long *) page_to_phys(page); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 206 | clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 207 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 208 | list_add(&page->lru, &mm->context.pgtable_list); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 209 | } else { |
| 210 | for (bit = 1; mask & bit; bit <<= 1) |
| 211 | table += PTRS_PER_PTE; |
| 212 | mask = atomic_xor_bits(&page->_mapcount, bit); |
| 213 | if ((mask & FRAG_MASK) == FRAG_MASK) |
| 214 | list_del(&page->lru); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 215 | } |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 216 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 217 | return table; |
| 218 | } |
| 219 | |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 220 | void page_table_free(struct mm_struct *mm, unsigned long *table) |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 221 | { |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 222 | struct page *page; |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 223 | unsigned int bit, mask; |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 224 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 225 | #ifdef CONFIG_PGSTE |
| 226 | if (mm_has_pgste(mm)) |
| 227 | return page_table_free_pgste(table); |
| 228 | #endif |
| 229 | /* Free 1K/2K page table fragment of a 4K page */ |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 230 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 231 | bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t))); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 232 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 233 | if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK) |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 234 | list_del(&page->lru); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 235 | mask = atomic_xor_bits(&page->_mapcount, bit); |
| 236 | if (mask & FRAG_MASK) |
| 237 | list_add(&page->lru, &mm->context.pgtable_list); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 238 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 239 | if (mask == 0) { |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 240 | pgtable_page_dtor(page); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 241 | atomic_set(&page->_mapcount, -1); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 242 | __free_page(page); |
| 243 | } |
| 244 | } |
Martin Schwidefsky | 3610cce | 2007-10-22 12:52:47 +0200 | [diff] [blame] | 245 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 246 | #ifdef CONFIG_HAVE_RCU_TABLE_FREE |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 247 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 248 | static void __page_table_free_rcu(void *table, unsigned bit) |
| 249 | { |
| 250 | struct page *page; |
| 251 | |
| 252 | #ifdef CONFIG_PGSTE |
| 253 | if (bit == FRAG_MASK) |
| 254 | return page_table_free_pgste(table); |
| 255 | #endif |
| 256 | /* Free 1K/2K page table fragment of a 4K page */ |
| 257 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 258 | if (atomic_xor_bits(&page->_mapcount, bit) == 0) { |
| 259 | pgtable_page_dtor(page); |
| 260 | atomic_set(&page->_mapcount, -1); |
| 261 | __free_page(page); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 262 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table) |
| 266 | { |
| 267 | struct mm_struct *mm; |
| 268 | struct page *page; |
| 269 | unsigned int bit, mask; |
| 270 | |
| 271 | mm = tlb->mm; |
| 272 | #ifdef CONFIG_PGSTE |
| 273 | if (mm_has_pgste(mm)) { |
| 274 | table = (unsigned long *) (__pa(table) | FRAG_MASK); |
| 275 | tlb_remove_table(tlb, table); |
| 276 | return; |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 277 | } |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 278 | #endif |
| 279 | bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t))); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 280 | page = pfn_to_page(__pa(table) >> PAGE_SHIFT); |
| 281 | spin_lock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 282 | if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK) |
| 283 | list_del(&page->lru); |
| 284 | mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4)); |
| 285 | if (mask & FRAG_MASK) |
| 286 | list_add_tail(&page->lru, &mm->context.pgtable_list); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 287 | spin_unlock_bh(&mm->context.list_lock); |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 288 | table = (unsigned long *) (__pa(table) | (bit << 4)); |
| 289 | tlb_remove_table(tlb, table); |
Martin Schwidefsky | 8021714 | 2010-10-25 16:10:11 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 292 | void __tlb_remove_table(void *_table) |
| 293 | { |
| 294 | void *table = (void *)((unsigned long) _table & PAGE_MASK); |
| 295 | unsigned type = (unsigned long) _table & ~PAGE_MASK; |
| 296 | |
| 297 | if (type) |
| 298 | __page_table_free_rcu(table, type); |
| 299 | else |
| 300 | free_pages((unsigned long) table, ALLOC_ORDER); |
| 301 | } |
| 302 | |
| 303 | #endif |
| 304 | |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 305 | /* |
| 306 | * switch on pgstes for its userspace process (for kvm) |
| 307 | */ |
| 308 | int s390_enable_sie(void) |
| 309 | { |
| 310 | struct task_struct *tsk = current; |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 311 | struct mm_struct *mm, *old_mm; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 312 | |
Carsten Otte | 702d9e5 | 2009-03-26 15:23:57 +0100 | [diff] [blame] | 313 | /* Do we have switched amode? If no, we cannot do sie */ |
Martin Schwidefsky | b11b533 | 2009-12-07 12:51:43 +0100 | [diff] [blame] | 314 | if (user_mode == HOME_SPACE_MODE) |
Carsten Otte | 702d9e5 | 2009-03-26 15:23:57 +0100 | [diff] [blame] | 315 | return -EINVAL; |
| 316 | |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 317 | /* Do we have pgstes? if yes, we are done */ |
Martin Schwidefsky | 36409f6 | 2011-06-06 14:14:41 +0200 | [diff] [blame] | 318 | if (mm_has_pgste(tsk->mm)) |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 319 | return 0; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 320 | |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 321 | /* lets check if we are allowed to replace the mm */ |
| 322 | task_lock(tsk); |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 323 | if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 || |
Martin Schwidefsky | 52a21f2 | 2009-10-06 10:33:55 +0200 | [diff] [blame] | 324 | #ifdef CONFIG_AIO |
| 325 | !hlist_empty(&tsk->mm->ioctx_list) || |
| 326 | #endif |
| 327 | tsk->mm != tsk->active_mm) { |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 328 | task_unlock(tsk); |
| 329 | return -EINVAL; |
| 330 | } |
| 331 | task_unlock(tsk); |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 332 | |
Christian Borntraeger | 250cf77 | 2008-10-28 11:10:15 +0100 | [diff] [blame] | 333 | /* we copy the mm and let dup_mm create the page tables with_pgstes */ |
| 334 | tsk->mm->context.alloc_pgste = 1; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 335 | mm = dup_mm(tsk); |
Christian Borntraeger | 250cf77 | 2008-10-28 11:10:15 +0100 | [diff] [blame] | 336 | tsk->mm->context.alloc_pgste = 0; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 337 | if (!mm) |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 338 | return -ENOMEM; |
| 339 | |
Christian Borntraeger | 250cf77 | 2008-10-28 11:10:15 +0100 | [diff] [blame] | 340 | /* Now lets check again if something happened */ |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 341 | task_lock(tsk); |
| 342 | if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 || |
Martin Schwidefsky | 52a21f2 | 2009-10-06 10:33:55 +0200 | [diff] [blame] | 343 | #ifdef CONFIG_AIO |
| 344 | !hlist_empty(&tsk->mm->ioctx_list) || |
| 345 | #endif |
| 346 | tsk->mm != tsk->active_mm) { |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 347 | mmput(mm); |
| 348 | task_unlock(tsk); |
| 349 | return -EINVAL; |
| 350 | } |
| 351 | |
| 352 | /* ok, we are alone. No ptrace, no threads, etc. */ |
| 353 | old_mm = tsk->mm; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 354 | tsk->mm = tsk->active_mm = mm; |
| 355 | preempt_disable(); |
| 356 | update_mm(mm, tsk); |
Christian Borntraeger | e05ef9b | 2010-10-25 16:10:45 +0200 | [diff] [blame] | 357 | atomic_inc(&mm->context.attach_count); |
| 358 | atomic_dec(&old_mm->context.attach_count); |
Rusty Russell | 005f8ee | 2009-03-26 15:25:01 +0100 | [diff] [blame] | 359 | cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm)); |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 360 | preempt_enable(); |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 361 | task_unlock(tsk); |
Christian Borntraeger | 74b6b52 | 2008-05-21 13:37:29 +0200 | [diff] [blame] | 362 | mmput(old_mm); |
| 363 | return 0; |
Carsten Otte | 402b086 | 2008-03-25 18:47:10 +0100 | [diff] [blame] | 364 | } |
| 365 | EXPORT_SYMBOL_GPL(s390_enable_sie); |
Hans-Joachim Picht | 7db11a3 | 2009-06-16 10:30:26 +0200 | [diff] [blame] | 366 | |
Heiko Carstens | 87458ff | 2009-09-22 22:58:46 +0200 | [diff] [blame] | 367 | #if defined(CONFIG_DEBUG_PAGEALLOC) && defined(CONFIG_HIBERNATION) |
Hans-Joachim Picht | 7db11a3 | 2009-06-16 10:30:26 +0200 | [diff] [blame] | 368 | bool kernel_page_present(struct page *page) |
| 369 | { |
| 370 | unsigned long addr; |
| 371 | int cc; |
| 372 | |
| 373 | addr = page_to_phys(page); |
Heiko Carstens | 87458ff | 2009-09-22 22:58:46 +0200 | [diff] [blame] | 374 | asm volatile( |
| 375 | " lra %1,0(%1)\n" |
| 376 | " ipm %0\n" |
| 377 | " srl %0,28" |
| 378 | : "=d" (cc), "+a" (addr) : : "cc"); |
Hans-Joachim Picht | 7db11a3 | 2009-06-16 10:30:26 +0200 | [diff] [blame] | 379 | return cc == 0; |
| 380 | } |
Heiko Carstens | 87458ff | 2009-09-22 22:58:46 +0200 | [diff] [blame] | 381 | #endif /* CONFIG_HIBERNATION && CONFIG_DEBUG_PAGEALLOC */ |