blob: 8d4330642512554edffaccd51cd7954f18897e1d [file] [log] [blame]
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001/*
Heiko Carstens239a6422009-06-12 10:26:33 +02002 * Copyright IBM Corp. 2007,2009
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02003 * 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 Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/gfp.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020010#include <linux/mm.h>
11#include <linux/swap.h>
12#include <linux/smp.h>
13#include <linux/highmem.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020014#include <linux/pagemap.h>
15#include <linux/spinlock.h>
16#include <linux/module.h>
17#include <linux/quicklist.h>
Martin Schwidefsky80217142010-10-25 16:10:11 +020018#include <linux/rcupdate.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020019
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 Schwidefsky6252d702008-02-09 18:24:37 +010025#include <asm/mmu_context.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020026
Martin Schwidefsky80217142010-10-25 16:10:11 +020027struct 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
39DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
40static DEFINE_PER_CPU(struct rcu_table_freelist *, rcu_table_freelist);
41
42static void __page_table_free(struct mm_struct *mm, unsigned long *table);
Martin Schwidefsky80217142010-10-25 16:10:11 +020043
44static 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
61static 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 Schwidefsky043d0702011-05-23 10:24:23 +020069 crst_table_free(batch->mm, batch->table[batch->crst_index++]);
Martin Schwidefsky80217142010-10-25 16:10:11 +020070 free_page((unsigned long) batch);
71}
72
73void 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
83static void smp_sync(void *arg)
84{
85}
86
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020087#ifndef CONFIG_64BIT
88#define ALLOC_ORDER 1
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010089#define TABLES_PER_PAGE 4
90#define FRAG_MASK 15UL
91#define SECOND_HALVES 10UL
Carsten Otte402b0862008-03-25 18:47:10 +010092
93void 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 Schwidefsky3610cce2007-10-22 12:52:47 +0200101#else
102#define ALLOC_ORDER 2
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100103#define TABLES_PER_PAGE 2
104#define FRAG_MASK 3UL
105#define SECOND_HALVES 2UL
Carsten Otte402b0862008-03-25 18:47:10 +0100106
107void 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 Schwidefsky3610cce2007-10-22 12:52:47 +0200113#endif
114
Heiko Carstens239a6422009-06-12 10:26:33 +0200115unsigned long VMALLOC_START = VMALLOC_END - VMALLOC_SIZE;
116EXPORT_SYMBOL(VMALLOC_START);
117
118static 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}
125early_param("vmalloc", parse_vmalloc);
126
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200127unsigned long *crst_table_alloc(struct mm_struct *mm)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200128{
129 struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
130
131 if (!page)
132 return NULL;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200133 return (unsigned long *) page_to_phys(page);
134}
135
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100136void crst_table_free(struct mm_struct *mm, unsigned long *table)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200137{
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200138 free_pages((unsigned long) table, ALLOC_ORDER);
Martin Schwidefsky80217142010-10-25 16:10:11 +0200139}
140
141void crst_table_free_rcu(struct mm_struct *mm, unsigned long *table)
142{
143 struct rcu_table_freelist *batch;
Martin Schwidefsky80217142010-10-25 16:10:11 +0200144
Martin Schwidefsky80217142010-10-25 16:10:11 +0200145 if (atomic_read(&mm->mm_users) < 2 &&
146 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id()))) {
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200147 crst_table_free(mm, table);
Martin Schwidefsky80217142010-10-25 16:10:11 +0200148 return;
149 }
150 batch = rcu_table_freelist_get(mm);
151 if (!batch) {
152 smp_call_function(smp_sync, NULL, 1);
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200153 crst_table_free(mm, table);
Martin Schwidefsky80217142010-10-25 16:10:11 +0200154 return;
155 }
156 batch->table[--batch->crst_index] = table;
157 if (batch->pgt_index >= batch->crst_index)
158 rcu_table_freelist_finish();
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200159}
160
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100161#ifdef CONFIG_64BIT
162int 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));
168repeat:
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200169 table = crst_table_alloc(mm);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100170 if (!table)
171 return -ENOMEM;
Martin Schwidefsky80217142010-10-25 16:10:11 +0200172 spin_lock_bh(&mm->page_table_lock);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100173 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 Schwidefskyf481bfa2009-03-18 13:27:36 +0100191 mm->task_size = mm->context.asce_limit;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100192 table = NULL;
193 }
Martin Schwidefsky80217142010-10-25 16:10:11 +0200194 spin_unlock_bh(&mm->page_table_lock);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100195 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
203void 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 Schwidefskyf481bfa2009-03-18 13:27:36 +0100229 mm->task_size = mm->context.asce_limit;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100230 crst_table_free(mm, (unsigned long *) pgd);
231 }
232 update_mm(mm, current);
233}
234#endif
235
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200236/*
237 * page table entry allocation/free routines.
238 */
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100239unsigned long *page_table_alloc(struct mm_struct *mm)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200240{
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100241 struct page *page;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200242 unsigned long *table;
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100243 unsigned long bits;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200244
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200245 bits = (mm->context.has_pgste) ? 3UL : 1UL;
Martin Schwidefsky80217142010-10-25 16:10:11 +0200246 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100247 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 Schwidefsky3610cce2007-10-22 12:52:47 +0200253 }
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100254 if (!page) {
Martin Schwidefsky80217142010-10-25 16:10:11 +0200255 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100256 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 Borntraeger250cf772008-10-28 11:10:15 +0100262 if (mm->context.has_pgste)
Carsten Otte402b0862008-03-25 18:47:10 +0100263 clear_table_pgstes(table);
264 else
265 clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE);
Martin Schwidefsky80217142010-10-25 16:10:11 +0200266 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100267 list_add(&page->lru, &mm->context.pgtable_list);
268 }
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200269 table = (unsigned long *) page_to_phys(page);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100270 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 Schwidefsky80217142010-10-25 16:10:11 +0200277 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200278 return table;
279}
280
Martin Schwidefsky80217142010-10-25 16:10:11 +0200281static 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 Schwidefsky146e4b32008-02-09 18:24:35 +0100296void page_table_free(struct mm_struct *mm, unsigned long *table)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200297{
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100298 struct page *page;
299 unsigned long bits;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200300
Martin Schwidefsky043d0702011-05-23 10:24:23 +0200301 bits = (mm->context.has_pgste) ? 3UL : 1UL;
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100302 bits <<= (__pa(table) & (PAGE_SIZE - 1)) / 256 / sizeof(unsigned long);
303 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
Martin Schwidefsky80217142010-10-25 16:10:11 +0200304 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100305 page->flags ^= bits;
306 if (page->flags & FRAG_MASK) {
307 /* Page now has some free pgtable fragments. */
Martin Schwidefskyf1be77b2011-01-31 11:30:04 +0100308 if (!list_empty(&page->lru))
309 list_move(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100310 page = NULL;
311 } else
312 /* All fragments of the 4K page have been freed. */
313 list_del(&page->lru);
Martin Schwidefsky80217142010-10-25 16:10:11 +0200314 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100315 if (page) {
316 pgtable_page_dtor(page);
317 __free_page(page);
318 }
319}
Martin Schwidefsky3610cce2007-10-22 12:52:47 +0200320
Martin Schwidefsky80217142010-10-25 16:10:11 +0200321void 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 Schwidefsky043d0702011-05-23 10:24:23 +0200338 bits = (mm->context.has_pgste) ? 3UL : 1UL;
Martin Schwidefsky80217142010-10-25 16:10:11 +0200339 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 Otte402b0862008-03-25 18:47:10 +0100351/*
352 * switch on pgstes for its userspace process (for kvm)
353 */
354int s390_enable_sie(void)
355{
356 struct task_struct *tsk = current;
Christian Borntraeger74b6b522008-05-21 13:37:29 +0200357 struct mm_struct *mm, *old_mm;
Carsten Otte402b0862008-03-25 18:47:10 +0100358
Carsten Otte702d9e52009-03-26 15:23:57 +0100359 /* Do we have switched amode? If no, we cannot do sie */
Martin Schwidefskyb11b5332009-12-07 12:51:43 +0100360 if (user_mode == HOME_SPACE_MODE)
Carsten Otte702d9e52009-03-26 15:23:57 +0100361 return -EINVAL;
362
Christian Borntraeger74b6b522008-05-21 13:37:29 +0200363 /* Do we have pgstes? if yes, we are done */
Christian Borntraeger250cf772008-10-28 11:10:15 +0100364 if (tsk->mm->context.has_pgste)
Christian Borntraeger74b6b522008-05-21 13:37:29 +0200365 return 0;
Carsten Otte402b0862008-03-25 18:47:10 +0100366
Christian Borntraeger74b6b522008-05-21 13:37:29 +0200367 /* lets check if we are allowed to replace the mm */
368 task_lock(tsk);
Carsten Otte402b0862008-03-25 18:47:10 +0100369 if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
Martin Schwidefsky52a21f22009-10-06 10:33:55 +0200370#ifdef CONFIG_AIO
371 !hlist_empty(&tsk->mm->ioctx_list) ||
372#endif
373 tsk->mm != tsk->active_mm) {
Christian Borntraeger74b6b522008-05-21 13:37:29 +0200374 task_unlock(tsk);
375 return -EINVAL;
376 }
377 task_unlock(tsk);
Carsten Otte402b0862008-03-25 18:47:10 +0100378
Christian Borntraeger250cf772008-10-28 11:10:15 +0100379 /* we copy the mm and let dup_mm create the page tables with_pgstes */
380 tsk->mm->context.alloc_pgste = 1;
Carsten Otte402b0862008-03-25 18:47:10 +0100381 mm = dup_mm(tsk);
Christian Borntraeger250cf772008-10-28 11:10:15 +0100382 tsk->mm->context.alloc_pgste = 0;
Carsten Otte402b0862008-03-25 18:47:10 +0100383 if (!mm)
Christian Borntraeger74b6b522008-05-21 13:37:29 +0200384 return -ENOMEM;
385
Christian Borntraeger250cf772008-10-28 11:10:15 +0100386 /* Now lets check again if something happened */
Christian Borntraeger74b6b522008-05-21 13:37:29 +0200387 task_lock(tsk);
388 if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
Martin Schwidefsky52a21f22009-10-06 10:33:55 +0200389#ifdef CONFIG_AIO
390 !hlist_empty(&tsk->mm->ioctx_list) ||
391#endif
392 tsk->mm != tsk->active_mm) {
Christian Borntraeger74b6b522008-05-21 13:37:29 +0200393 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 Otte402b0862008-03-25 18:47:10 +0100400 tsk->mm = tsk->active_mm = mm;
401 preempt_disable();
402 update_mm(mm, tsk);
Christian Borntraegere05ef9b2010-10-25 16:10:45 +0200403 atomic_inc(&mm->context.attach_count);
404 atomic_dec(&old_mm->context.attach_count);
Rusty Russell005f8ee2009-03-26 15:25:01 +0100405 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
Carsten Otte402b0862008-03-25 18:47:10 +0100406 preempt_enable();
Carsten Otte402b0862008-03-25 18:47:10 +0100407 task_unlock(tsk);
Christian Borntraeger74b6b522008-05-21 13:37:29 +0200408 mmput(old_mm);
409 return 0;
Carsten Otte402b0862008-03-25 18:47:10 +0100410}
411EXPORT_SYMBOL_GPL(s390_enable_sie);
Hans-Joachim Picht7db11a32009-06-16 10:30:26 +0200412
Heiko Carstens87458ff2009-09-22 22:58:46 +0200413#if defined(CONFIG_DEBUG_PAGEALLOC) && defined(CONFIG_HIBERNATION)
Hans-Joachim Picht7db11a32009-06-16 10:30:26 +0200414bool kernel_page_present(struct page *page)
415{
416 unsigned long addr;
417 int cc;
418
419 addr = page_to_phys(page);
Heiko Carstens87458ff2009-09-22 22:58:46 +0200420 asm volatile(
421 " lra %1,0(%1)\n"
422 " ipm %0\n"
423 " srl %0,28"
424 : "=d" (cc), "+a" (addr) : : "cc");
Hans-Joachim Picht7db11a32009-06-16 10:30:26 +0200425 return cc == 0;
426}
Heiko Carstens87458ff2009-09-22 22:58:46 +0200427#endif /* CONFIG_HIBERNATION && CONFIG_DEBUG_PAGEALLOC */