]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - arch/s390/mm/pgalloc.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[thirdparty/kernel/linux.git] / arch / s390 / mm / pgalloc.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1e133ab2
MS
2/*
3 * Page table allocation functions
4 *
5 * Copyright IBM Corp. 2016
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 */
8
9#include <linux/mm.h>
10#include <linux/sysctl.h>
11#include <asm/mmu_context.h>
12#include <asm/pgalloc.h>
13#include <asm/gmap.h>
14#include <asm/tlb.h>
15#include <asm/tlbflush.h>
16
17#ifdef CONFIG_PGSTE
18
19static int page_table_allocate_pgste_min = 0;
20static int page_table_allocate_pgste_max = 1;
21int page_table_allocate_pgste = 0;
22EXPORT_SYMBOL(page_table_allocate_pgste);
23
24static struct ctl_table page_table_sysctl[] = {
25 {
26 .procname = "allocate_pgste",
27 .data = &page_table_allocate_pgste,
28 .maxlen = sizeof(int),
29 .mode = S_IRUGO | S_IWUSR,
30 .proc_handler = proc_dointvec,
31 .extra1 = &page_table_allocate_pgste_min,
32 .extra2 = &page_table_allocate_pgste_max,
33 },
34 { }
35};
36
37static struct ctl_table page_table_sysctl_dir[] = {
38 {
39 .procname = "vm",
40 .maxlen = 0,
41 .mode = 0555,
42 .child = page_table_sysctl,
43 },
44 { }
45};
46
47static int __init page_table_register_sysctl(void)
48{
49 return register_sysctl_table(page_table_sysctl_dir) ? 0 : -ENOMEM;
50}
51__initcall(page_table_register_sysctl);
52
53#endif /* CONFIG_PGSTE */
54
55unsigned long *crst_table_alloc(struct mm_struct *mm)
56{
57 struct page *page = alloc_pages(GFP_KERNEL, 2);
58
59 if (!page)
60 return NULL;
c9b5ad54 61 arch_set_page_dat(page, 2);
1e133ab2
MS
62 return (unsigned long *) page_to_phys(page);
63}
64
65void crst_table_free(struct mm_struct *mm, unsigned long *table)
66{
67 free_pages((unsigned long) table, 2);
68}
69
70static void __crst_table_upgrade(void *arg)
71{
72 struct mm_struct *mm = arg;
73
74 if (current->active_mm == mm) {
75 clear_user_asce();
76 set_user_asce(mm);
77 }
78 __tlb_flush_local();
79}
80
1aea9b3f 81int crst_table_upgrade(struct mm_struct *mm, unsigned long end)
1e133ab2
MS
82{
83 unsigned long *table, *pgd;
1aea9b3f 84 int rc, notify;
1e133ab2 85
1aea9b3f 86 /* upgrade should only happen from 3 to 4, 3 to 5, or 4 to 5 levels */
2fc4876e 87 VM_BUG_ON(mm->context.asce_limit < _REGION2_SIZE);
1aea9b3f 88 if (end >= TASK_SIZE_MAX)
1e133ab2 89 return -ENOMEM;
1aea9b3f
MS
90 rc = 0;
91 notify = 0;
92 while (mm->context.asce_limit < end) {
93 table = crst_table_alloc(mm);
94 if (!table) {
95 rc = -ENOMEM;
96 break;
97 }
98 spin_lock_bh(&mm->page_table_lock);
99 pgd = (unsigned long *) mm->pgd;
f1c1174f 100 if (mm->context.asce_limit == _REGION2_SIZE) {
1aea9b3f
MS
101 crst_table_init(table, _REGION2_ENTRY_EMPTY);
102 p4d_populate(mm, (p4d_t *) table, (pud_t *) pgd);
103 mm->pgd = (pgd_t *) table;
f1c1174f 104 mm->context.asce_limit = _REGION1_SIZE;
1aea9b3f
MS
105 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
106 _ASCE_USER_BITS | _ASCE_TYPE_REGION2;
107 } else {
108 crst_table_init(table, _REGION1_ENTRY_EMPTY);
109 pgd_populate(mm, (pgd_t *) table, (p4d_t *) pgd);
110 mm->pgd = (pgd_t *) table;
111 mm->context.asce_limit = -PAGE_SIZE;
112 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
113 _ASCE_USER_BITS | _ASCE_TYPE_REGION1;
114 }
115 notify = 1;
116 spin_unlock_bh(&mm->page_table_lock);
117 }
118 if (notify)
119 on_each_cpu(__crst_table_upgrade, mm, 0);
120 return rc;
1e133ab2
MS
121}
122
723cacbd 123void crst_table_downgrade(struct mm_struct *mm)
1e133ab2
MS
124{
125 pgd_t *pgd;
126
723cacbd 127 /* downgrade should only happen from 3 to 2 levels (compat only) */
2fc4876e 128 VM_BUG_ON(mm->context.asce_limit != _REGION2_SIZE);
723cacbd 129
1e133ab2
MS
130 if (current->active_mm == mm) {
131 clear_user_asce();
132 __tlb_flush_mm(mm);
133 }
723cacbd
GS
134
135 pgd = mm->pgd;
136 mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
f1c1174f 137 mm->context.asce_limit = _REGION3_SIZE;
723cacbd
GS
138 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
139 _ASCE_USER_BITS | _ASCE_TYPE_SEGMENT;
723cacbd
GS
140 crst_table_free(mm, (unsigned long *) pgd);
141
1e133ab2
MS
142 if (current->active_mm == mm)
143 set_user_asce(mm);
144}
145
146static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
147{
148 unsigned int old, new;
149
150 do {
151 old = atomic_read(v);
152 new = old ^ bits;
153 } while (atomic_cmpxchg(v, old, new) != old);
154 return new;
155}
156
4be130a0
MS
157#ifdef CONFIG_PGSTE
158
159struct page *page_table_alloc_pgste(struct mm_struct *mm)
160{
161 struct page *page;
162 unsigned long *table;
163
faee35a5 164 page = alloc_page(GFP_KERNEL);
4be130a0
MS
165 if (page) {
166 table = (unsigned long *) page_to_phys(page);
167 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
168 clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
169 }
170 return page;
171}
172
173void page_table_free_pgste(struct page *page)
174{
175 __free_page(page);
176}
177
178#endif /* CONFIG_PGSTE */
179
1e133ab2
MS
180/*
181 * page table entry allocation/free routines.
182 */
183unsigned long *page_table_alloc(struct mm_struct *mm)
184{
185 unsigned long *table;
186 struct page *page;
187 unsigned int mask, bit;
188
189 /* Try to get a fragment of a 4K page as a 2K page table */
190 if (!mm_alloc_pgste(mm)) {
191 table = NULL;
f28a4b4d 192 spin_lock_bh(&mm->context.lock);
1e133ab2
MS
193 if (!list_empty(&mm->context.pgtable_list)) {
194 page = list_first_entry(&mm->context.pgtable_list,
195 struct page, lru);
196 mask = atomic_read(&page->_mapcount);
197 mask = (mask | (mask >> 4)) & 3;
198 if (mask != 3) {
199 table = (unsigned long *) page_to_phys(page);
200 bit = mask & 1; /* =1 -> second 2K */
201 if (bit)
202 table += PTRS_PER_PTE;
203 atomic_xor_bits(&page->_mapcount, 1U << bit);
204 list_del(&page->lru);
205 }
206 }
f28a4b4d 207 spin_unlock_bh(&mm->context.lock);
1e133ab2
MS
208 if (table)
209 return table;
210 }
211 /* Allocate a fresh page */
10d58bf2 212 page = alloc_page(GFP_KERNEL);
1e133ab2
MS
213 if (!page)
214 return NULL;
215 if (!pgtable_page_ctor(page)) {
216 __free_page(page);
217 return NULL;
218 }
c9b5ad54 219 arch_set_page_dat(page, 0);
1e133ab2
MS
220 /* Initialize page table */
221 table = (unsigned long *) page_to_phys(page);
222 if (mm_alloc_pgste(mm)) {
223 /* Return 4K page table with PGSTEs */
224 atomic_set(&page->_mapcount, 3);
225 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
226 clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
227 } else {
228 /* Return the first 2K fragment of the page */
229 atomic_set(&page->_mapcount, 1);
230 clear_table(table, _PAGE_INVALID, PAGE_SIZE);
f28a4b4d 231 spin_lock_bh(&mm->context.lock);
1e133ab2 232 list_add(&page->lru, &mm->context.pgtable_list);
f28a4b4d 233 spin_unlock_bh(&mm->context.lock);
1e133ab2
MS
234 }
235 return table;
236}
237
238void page_table_free(struct mm_struct *mm, unsigned long *table)
239{
240 struct page *page;
241 unsigned int bit, mask;
242
243 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
244 if (!mm_alloc_pgste(mm)) {
245 /* Free 2K page table fragment of a 4K page */
246 bit = (__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t));
f28a4b4d 247 spin_lock_bh(&mm->context.lock);
1e133ab2
MS
248 mask = atomic_xor_bits(&page->_mapcount, 1U << bit);
249 if (mask & 3)
250 list_add(&page->lru, &mm->context.pgtable_list);
251 else
252 list_del(&page->lru);
f28a4b4d 253 spin_unlock_bh(&mm->context.lock);
1e133ab2
MS
254 if (mask != 0)
255 return;
256 }
257
258 pgtable_page_dtor(page);
259 atomic_set(&page->_mapcount, -1);
260 __free_page(page);
261}
262
263void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
264 unsigned long vmaddr)
265{
266 struct mm_struct *mm;
267 struct page *page;
268 unsigned int bit, mask;
269
270 mm = tlb->mm;
271 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
272 if (mm_alloc_pgste(mm)) {
273 gmap_unlink(mm, table, vmaddr);
274 table = (unsigned long *) (__pa(table) | 3);
275 tlb_remove_table(tlb, table);
276 return;
277 }
278 bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
f28a4b4d 279 spin_lock_bh(&mm->context.lock);
1e133ab2
MS
280 mask = atomic_xor_bits(&page->_mapcount, 0x11U << bit);
281 if (mask & 3)
282 list_add_tail(&page->lru, &mm->context.pgtable_list);
283 else
284 list_del(&page->lru);
f28a4b4d 285 spin_unlock_bh(&mm->context.lock);
1e133ab2
MS
286 table = (unsigned long *) (__pa(table) | (1U << bit));
287 tlb_remove_table(tlb, table);
288}
289
290static void __tlb_remove_table(void *_table)
291{
292 unsigned int mask = (unsigned long) _table & 3;
293 void *table = (void *)((unsigned long) _table ^ mask);
294 struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
295
296 switch (mask) {
1aea9b3f 297 case 0: /* pmd, pud, or p4d */
1e133ab2
MS
298 free_pages((unsigned long) table, 2);
299 break;
300 case 1: /* lower 2K of a 4K page table */
301 case 2: /* higher 2K of a 4K page table */
302 if (atomic_xor_bits(&page->_mapcount, mask << 4) != 0)
303 break;
304 /* fallthrough */
305 case 3: /* 4K page table with pgstes */
306 pgtable_page_dtor(page);
307 atomic_set(&page->_mapcount, -1);
308 __free_page(page);
309 break;
310 }
311}
312
313static void tlb_remove_table_smp_sync(void *arg)
314{
315 /* Simply deliver the interrupt */
316}
317
318static void tlb_remove_table_one(void *table)
319{
320 /*
321 * This isn't an RCU grace period and hence the page-tables cannot be
322 * assumed to be actually RCU-freed.
323 *
324 * It is however sufficient for software page-table walkers that rely
325 * on IRQ disabling. See the comment near struct mmu_table_batch.
326 */
327 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
328 __tlb_remove_table(table);
329}
330
331static void tlb_remove_table_rcu(struct rcu_head *head)
332{
333 struct mmu_table_batch *batch;
334 int i;
335
336 batch = container_of(head, struct mmu_table_batch, rcu);
337
338 for (i = 0; i < batch->nr; i++)
339 __tlb_remove_table(batch->tables[i]);
340
341 free_page((unsigned long)batch);
342}
343
344void tlb_table_flush(struct mmu_gather *tlb)
345{
346 struct mmu_table_batch **batch = &tlb->batch;
347
348 if (*batch) {
349 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
350 *batch = NULL;
351 }
352}
353
354void tlb_remove_table(struct mmu_gather *tlb, void *table)
355{
356 struct mmu_table_batch **batch = &tlb->batch;
357
358 tlb->mm->context.flush_mm = 1;
359 if (*batch == NULL) {
360 *batch = (struct mmu_table_batch *)
361 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
362 if (*batch == NULL) {
363 __tlb_flush_mm_lazy(tlb->mm);
364 tlb_remove_table_one(table);
365 return;
366 }
367 (*batch)->nr = 0;
368 }
369 (*batch)->tables[(*batch)->nr++] = table;
370 if ((*batch)->nr == MAX_TABLE_BATCH)
371 tlb_flush_mmu(tlb);
372}