2 * Page table allocation functions
4 * Copyright IBM Corp. 2016
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
9 #include <linux/sysctl.h>
10 #include <asm/mmu_context.h>
11 #include <asm/pgalloc.h>
14 #include <asm/tlbflush.h>
18 static int page_table_allocate_pgste_min
= 0;
19 static int page_table_allocate_pgste_max
= 1;
20 int page_table_allocate_pgste
= 0;
21 EXPORT_SYMBOL(page_table_allocate_pgste
);
23 static struct ctl_table page_table_sysctl
[] = {
25 .procname
= "allocate_pgste",
26 .data
= &page_table_allocate_pgste
,
27 .maxlen
= sizeof(int),
28 .mode
= S_IRUGO
| S_IWUSR
,
29 .proc_handler
= proc_dointvec
,
30 .extra1
= &page_table_allocate_pgste_min
,
31 .extra2
= &page_table_allocate_pgste_max
,
36 static struct ctl_table page_table_sysctl_dir
[] = {
41 .child
= page_table_sysctl
,
46 static int __init
page_table_register_sysctl(void)
48 return register_sysctl_table(page_table_sysctl_dir
) ? 0 : -ENOMEM
;
50 __initcall(page_table_register_sysctl
);
52 #endif /* CONFIG_PGSTE */
54 unsigned long *crst_table_alloc(struct mm_struct
*mm
)
56 struct page
*page
= alloc_pages(GFP_KERNEL
, 2);
60 return (unsigned long *) page_to_phys(page
);
63 void crst_table_free(struct mm_struct
*mm
, unsigned long *table
)
65 free_pages((unsigned long) table
, 2);
68 static void __crst_table_upgrade(void *arg
)
70 struct mm_struct
*mm
= arg
;
72 if (current
->active_mm
== mm
) {
79 int crst_table_upgrade(struct mm_struct
*mm
)
81 unsigned long *table
, *pgd
;
83 /* upgrade should only happen from 3 to 4 levels */
84 BUG_ON(mm
->context
.asce_limit
!= (1UL << 42));
86 table
= crst_table_alloc(mm
);
90 spin_lock_bh(&mm
->page_table_lock
);
91 pgd
= (unsigned long *) mm
->pgd
;
92 crst_table_init(table
, _REGION2_ENTRY_EMPTY
);
93 pgd_populate(mm
, (pgd_t
*) table
, (pud_t
*) pgd
);
94 mm
->pgd
= (pgd_t
*) table
;
95 mm
->context
.asce_limit
= 1UL << 53;
96 mm
->context
.asce
= __pa(mm
->pgd
) | _ASCE_TABLE_LENGTH
|
97 _ASCE_USER_BITS
| _ASCE_TYPE_REGION2
;
98 mm
->task_size
= mm
->context
.asce_limit
;
99 spin_unlock_bh(&mm
->page_table_lock
);
101 on_each_cpu(__crst_table_upgrade
, mm
, 0);
105 void crst_table_downgrade(struct mm_struct
*mm
)
109 /* downgrade should only happen from 3 to 2 levels (compat only) */
110 BUG_ON(mm
->context
.asce_limit
!= (1UL << 42));
112 if (current
->active_mm
== mm
) {
118 mm
->pgd
= (pgd_t
*) (pgd_val(*pgd
) & _REGION_ENTRY_ORIGIN
);
119 mm
->context
.asce_limit
= 1UL << 31;
120 mm
->context
.asce
= __pa(mm
->pgd
) | _ASCE_TABLE_LENGTH
|
121 _ASCE_USER_BITS
| _ASCE_TYPE_SEGMENT
;
122 mm
->task_size
= mm
->context
.asce_limit
;
123 crst_table_free(mm
, (unsigned long *) pgd
);
125 if (current
->active_mm
== mm
)
129 static inline unsigned int atomic_xor_bits(atomic_t
*v
, unsigned int bits
)
131 unsigned int old
, new;
134 old
= atomic_read(v
);
136 } while (atomic_cmpxchg(v
, old
, new) != old
);
142 struct page
*page_table_alloc_pgste(struct mm_struct
*mm
)
145 unsigned long *table
;
147 page
= alloc_page(GFP_KERNEL
|__GFP_REPEAT
);
149 table
= (unsigned long *) page_to_phys(page
);
150 clear_table(table
, _PAGE_INVALID
, PAGE_SIZE
/2);
151 clear_table(table
+ PTRS_PER_PTE
, 0, PAGE_SIZE
/2);
156 void page_table_free_pgste(struct page
*page
)
161 #endif /* CONFIG_PGSTE */
164 * page table entry allocation/free routines.
166 unsigned long *page_table_alloc(struct mm_struct
*mm
)
168 unsigned long *table
;
170 unsigned int mask
, bit
;
172 /* Try to get a fragment of a 4K page as a 2K page table */
173 if (!mm_alloc_pgste(mm
)) {
175 spin_lock_bh(&mm
->context
.pgtable_lock
);
176 if (!list_empty(&mm
->context
.pgtable_list
)) {
177 page
= list_first_entry(&mm
->context
.pgtable_list
,
179 mask
= atomic_read(&page
->_mapcount
);
180 mask
= (mask
| (mask
>> 4)) & 3;
182 table
= (unsigned long *) page_to_phys(page
);
183 bit
= mask
& 1; /* =1 -> second 2K */
185 table
+= PTRS_PER_PTE
;
186 atomic_xor_bits(&page
->_mapcount
, 1U << bit
);
187 list_del(&page
->lru
);
190 spin_unlock_bh(&mm
->context
.pgtable_lock
);
194 /* Allocate a fresh page */
195 page
= alloc_page(GFP_KERNEL
);
198 if (!pgtable_page_ctor(page
)) {
202 /* Initialize page table */
203 table
= (unsigned long *) page_to_phys(page
);
204 if (mm_alloc_pgste(mm
)) {
205 /* Return 4K page table with PGSTEs */
206 atomic_set(&page
->_mapcount
, 3);
207 clear_table(table
, _PAGE_INVALID
, PAGE_SIZE
/2);
208 clear_table(table
+ PTRS_PER_PTE
, 0, PAGE_SIZE
/2);
210 /* Return the first 2K fragment of the page */
211 atomic_set(&page
->_mapcount
, 1);
212 clear_table(table
, _PAGE_INVALID
, PAGE_SIZE
);
213 spin_lock_bh(&mm
->context
.pgtable_lock
);
214 list_add(&page
->lru
, &mm
->context
.pgtable_list
);
215 spin_unlock_bh(&mm
->context
.pgtable_lock
);
220 void page_table_free(struct mm_struct
*mm
, unsigned long *table
)
223 unsigned int bit
, mask
;
225 page
= pfn_to_page(__pa(table
) >> PAGE_SHIFT
);
226 if (!mm_alloc_pgste(mm
)) {
227 /* Free 2K page table fragment of a 4K page */
228 bit
= (__pa(table
) & ~PAGE_MASK
)/(PTRS_PER_PTE
*sizeof(pte_t
));
229 spin_lock_bh(&mm
->context
.pgtable_lock
);
230 mask
= atomic_xor_bits(&page
->_mapcount
, 1U << bit
);
232 list_add(&page
->lru
, &mm
->context
.pgtable_list
);
234 list_del(&page
->lru
);
235 spin_unlock_bh(&mm
->context
.pgtable_lock
);
240 pgtable_page_dtor(page
);
241 atomic_set(&page
->_mapcount
, -1);
245 void page_table_free_rcu(struct mmu_gather
*tlb
, unsigned long *table
,
246 unsigned long vmaddr
)
248 struct mm_struct
*mm
;
250 unsigned int bit
, mask
;
253 page
= pfn_to_page(__pa(table
) >> PAGE_SHIFT
);
254 if (mm_alloc_pgste(mm
)) {
255 gmap_unlink(mm
, table
, vmaddr
);
256 table
= (unsigned long *) (__pa(table
) | 3);
257 tlb_remove_table(tlb
, table
);
260 bit
= (__pa(table
) & ~PAGE_MASK
) / (PTRS_PER_PTE
*sizeof(pte_t
));
261 spin_lock_bh(&mm
->context
.pgtable_lock
);
262 mask
= atomic_xor_bits(&page
->_mapcount
, 0x11U
<< bit
);
264 list_add_tail(&page
->lru
, &mm
->context
.pgtable_list
);
266 list_del(&page
->lru
);
267 spin_unlock_bh(&mm
->context
.pgtable_lock
);
268 table
= (unsigned long *) (__pa(table
) | (1U << bit
));
269 tlb_remove_table(tlb
, table
);
272 static void __tlb_remove_table(void *_table
)
274 unsigned int mask
= (unsigned long) _table
& 3;
275 void *table
= (void *)((unsigned long) _table
^ mask
);
276 struct page
*page
= pfn_to_page(__pa(table
) >> PAGE_SHIFT
);
279 case 0: /* pmd or pud */
280 free_pages((unsigned long) table
, 2);
282 case 1: /* lower 2K of a 4K page table */
283 case 2: /* higher 2K of a 4K page table */
284 if (atomic_xor_bits(&page
->_mapcount
, mask
<< 4) != 0)
287 case 3: /* 4K page table with pgstes */
288 pgtable_page_dtor(page
);
289 atomic_set(&page
->_mapcount
, -1);
295 static void tlb_remove_table_smp_sync(void *arg
)
297 /* Simply deliver the interrupt */
300 static void tlb_remove_table_one(void *table
)
303 * This isn't an RCU grace period and hence the page-tables cannot be
304 * assumed to be actually RCU-freed.
306 * It is however sufficient for software page-table walkers that rely
307 * on IRQ disabling. See the comment near struct mmu_table_batch.
309 smp_call_function(tlb_remove_table_smp_sync
, NULL
, 1);
310 __tlb_remove_table(table
);
313 static void tlb_remove_table_rcu(struct rcu_head
*head
)
315 struct mmu_table_batch
*batch
;
318 batch
= container_of(head
, struct mmu_table_batch
, rcu
);
320 for (i
= 0; i
< batch
->nr
; i
++)
321 __tlb_remove_table(batch
->tables
[i
]);
323 free_page((unsigned long)batch
);
326 void tlb_table_flush(struct mmu_gather
*tlb
)
328 struct mmu_table_batch
**batch
= &tlb
->batch
;
331 call_rcu_sched(&(*batch
)->rcu
, tlb_remove_table_rcu
);
336 void tlb_remove_table(struct mmu_gather
*tlb
, void *table
)
338 struct mmu_table_batch
**batch
= &tlb
->batch
;
340 tlb
->mm
->context
.flush_mm
= 1;
341 if (*batch
== NULL
) {
342 *batch
= (struct mmu_table_batch
*)
343 __get_free_page(GFP_NOWAIT
| __GFP_NOWARN
);
344 if (*batch
== NULL
) {
345 __tlb_flush_mm_lazy(tlb
->mm
);
346 tlb_remove_table_one(table
);
351 (*batch
)->tables
[(*batch
)->nr
++] = table
;
352 if ((*batch
)->nr
== MAX_TABLE_BATCH
)