1 // SPDX-License-Identifier: GPL-2.0 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/sysctl.h> 10 #include <linux/slab.h> 11 #include <linux/mm.h> 12 #include <asm/mmu_context.h> 13 #include <asm/page-states.h> 14 #include <asm/pgalloc.h> 15 #include <asm/gmap.h> 16 #include <asm/tlb.h> 17 #include <asm/tlbflush.h> 18 19 #ifdef CONFIG_PGSTE 20 21 int page_table_allocate_pgste = 0; 22 EXPORT_SYMBOL(page_table_allocate_pgste); 23 24 static 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_minmax, 31 .extra1 = SYSCTL_ZERO, 32 .extra2 = SYSCTL_ONE, 33 }, 34 }; 35 36 static int __init page_table_register_sysctl(void) 37 { 38 return register_sysctl("vm", page_table_sysctl) ? 0 : -ENOMEM; 39 } 40 __initcall(page_table_register_sysctl); 41 42 #endif /* CONFIG_PGSTE */ 43 44 unsigned long *crst_table_alloc(struct mm_struct *mm) 45 { 46 struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL, CRST_ALLOC_ORDER); 47 unsigned long *table; 48 49 if (!ptdesc) 50 return NULL; 51 table = ptdesc_to_virt(ptdesc); 52 __arch_set_page_dat(table, 1UL << CRST_ALLOC_ORDER); 53 return table; 54 } 55 56 void crst_table_free(struct mm_struct *mm, unsigned long *table) 57 { 58 if (!table) 59 return; 60 pagetable_free(virt_to_ptdesc(table)); 61 } 62 63 static void __crst_table_upgrade(void *arg) 64 { 65 struct mm_struct *mm = arg; 66 67 /* change all active ASCEs to avoid the creation of new TLBs */ 68 if (current->active_mm == mm) { 69 get_lowcore()->user_asce.val = mm->context.asce; 70 local_ctl_load(7, &get_lowcore()->user_asce); 71 } 72 __tlb_flush_local(); 73 } 74 75 int crst_table_upgrade(struct mm_struct *mm, unsigned long end) 76 { 77 unsigned long *pgd = NULL, *p4d = NULL, *__pgd; 78 unsigned long asce_limit = mm->context.asce_limit; 79 80 /* upgrade should only happen from 3 to 4, 3 to 5, or 4 to 5 levels */ 81 VM_BUG_ON(asce_limit < _REGION2_SIZE); 82 83 if (end <= asce_limit) 84 return 0; 85 86 if (asce_limit == _REGION2_SIZE) { 87 p4d = crst_table_alloc(mm); 88 if (unlikely(!p4d)) 89 goto err_p4d; 90 crst_table_init(p4d, _REGION2_ENTRY_EMPTY); 91 } 92 if (end > _REGION1_SIZE) { 93 pgd = crst_table_alloc(mm); 94 if (unlikely(!pgd)) 95 goto err_pgd; 96 crst_table_init(pgd, _REGION1_ENTRY_EMPTY); 97 } 98 99 spin_lock_bh(&mm->page_table_lock); 100 101 /* 102 * This routine gets called with mmap_lock lock held and there is 103 * no reason to optimize for the case of otherwise. However, if 104 * that would ever change, the below check will let us know. 105 */ 106 VM_BUG_ON(asce_limit != mm->context.asce_limit); 107 108 if (p4d) { 109 __pgd = (unsigned long *) mm->pgd; 110 p4d_populate(mm, (p4d_t *) p4d, (pud_t *) __pgd); 111 mm->pgd = (pgd_t *) p4d; 112 mm->context.asce_limit = _REGION1_SIZE; 113 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH | 114 _ASCE_USER_BITS | _ASCE_TYPE_REGION2; 115 mm_inc_nr_puds(mm); 116 } 117 if (pgd) { 118 __pgd = (unsigned long *) mm->pgd; 119 pgd_populate(mm, (pgd_t *) pgd, (p4d_t *) __pgd); 120 mm->pgd = (pgd_t *) pgd; 121 mm->context.asce_limit = TASK_SIZE_MAX; 122 mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH | 123 _ASCE_USER_BITS | _ASCE_TYPE_REGION1; 124 } 125 126 spin_unlock_bh(&mm->page_table_lock); 127 128 on_each_cpu(__crst_table_upgrade, mm, 0); 129 130 return 0; 131 132 err_pgd: 133 crst_table_free(mm, p4d); 134 err_p4d: 135 return -ENOMEM; 136 } 137 138 #ifdef CONFIG_PGSTE 139 140 struct ptdesc *page_table_alloc_pgste(struct mm_struct *mm) 141 { 142 struct ptdesc *ptdesc; 143 u64 *table; 144 145 ptdesc = pagetable_alloc(GFP_KERNEL, 0); 146 if (ptdesc) { 147 table = (u64 *)ptdesc_to_virt(ptdesc); 148 __arch_set_page_dat(table, 1); 149 memset64(table, _PAGE_INVALID, PTRS_PER_PTE); 150 memset64(table + PTRS_PER_PTE, 0, PTRS_PER_PTE); 151 } 152 return ptdesc; 153 } 154 155 void page_table_free_pgste(struct ptdesc *ptdesc) 156 { 157 pagetable_free(ptdesc); 158 } 159 160 #endif /* CONFIG_PGSTE */ 161 162 unsigned long *page_table_alloc(struct mm_struct *mm) 163 { 164 struct ptdesc *ptdesc; 165 unsigned long *table; 166 167 ptdesc = pagetable_alloc(GFP_KERNEL, 0); 168 if (!ptdesc) 169 return NULL; 170 if (!pagetable_pte_ctor(ptdesc)) { 171 pagetable_free(ptdesc); 172 return NULL; 173 } 174 table = ptdesc_to_virt(ptdesc); 175 __arch_set_page_dat(table, 1); 176 /* pt_list is used by gmap only */ 177 INIT_LIST_HEAD(&ptdesc->pt_list); 178 memset64((u64 *)table, _PAGE_INVALID, PTRS_PER_PTE); 179 memset64((u64 *)table + PTRS_PER_PTE, 0, PTRS_PER_PTE); 180 return table; 181 } 182 183 void page_table_free(struct mm_struct *mm, unsigned long *table) 184 { 185 struct ptdesc *ptdesc = virt_to_ptdesc(table); 186 187 pagetable_dtor_free(ptdesc); 188 } 189 190 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 191 static void pte_free_now(struct rcu_head *head) 192 { 193 struct ptdesc *ptdesc = container_of(head, struct ptdesc, pt_rcu_head); 194 195 pagetable_dtor_free(ptdesc); 196 } 197 198 void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable) 199 { 200 struct ptdesc *ptdesc = virt_to_ptdesc(pgtable); 201 202 call_rcu(&ptdesc->pt_rcu_head, pte_free_now); 203 /* 204 * THPs are not allowed for KVM guests. Warn if pgste ever reaches here. 205 * Turn to the generic pte_free_defer() version once gmap is removed. 206 */ 207 WARN_ON_ONCE(mm_has_pgste(mm)); 208 } 209 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 210 211 /* 212 * Base infrastructure required to generate basic asces, region, segment, 213 * and page tables that do not make use of enhanced features like EDAT1. 214 */ 215 216 static struct kmem_cache *base_pgt_cache; 217 218 static unsigned long *base_pgt_alloc(void) 219 { 220 unsigned long *table; 221 222 table = kmem_cache_alloc(base_pgt_cache, GFP_KERNEL); 223 if (table) 224 memset64((u64 *)table, _PAGE_INVALID, PTRS_PER_PTE); 225 return table; 226 } 227 228 static void base_pgt_free(unsigned long *table) 229 { 230 kmem_cache_free(base_pgt_cache, table); 231 } 232 233 static unsigned long *base_crst_alloc(unsigned long val) 234 { 235 unsigned long *table; 236 struct ptdesc *ptdesc; 237 238 ptdesc = pagetable_alloc(GFP_KERNEL, CRST_ALLOC_ORDER); 239 if (!ptdesc) 240 return NULL; 241 table = ptdesc_address(ptdesc); 242 crst_table_init(table, val); 243 return table; 244 } 245 246 static void base_crst_free(unsigned long *table) 247 { 248 if (!table) 249 return; 250 pagetable_free(virt_to_ptdesc(table)); 251 } 252 253 #define BASE_ADDR_END_FUNC(NAME, SIZE) \ 254 static inline unsigned long base_##NAME##_addr_end(unsigned long addr, \ 255 unsigned long end) \ 256 { \ 257 unsigned long next = (addr + (SIZE)) & ~((SIZE) - 1); \ 258 \ 259 return (next - 1) < (end - 1) ? next : end; \ 260 } 261 262 BASE_ADDR_END_FUNC(page, PAGE_SIZE) 263 BASE_ADDR_END_FUNC(segment, _SEGMENT_SIZE) 264 BASE_ADDR_END_FUNC(region3, _REGION3_SIZE) 265 BASE_ADDR_END_FUNC(region2, _REGION2_SIZE) 266 BASE_ADDR_END_FUNC(region1, _REGION1_SIZE) 267 268 static inline unsigned long base_lra(unsigned long address) 269 { 270 unsigned long real; 271 272 asm volatile( 273 " lra %0,0(%1)\n" 274 : "=d" (real) : "a" (address) : "cc"); 275 return real; 276 } 277 278 static int base_page_walk(unsigned long *origin, unsigned long addr, 279 unsigned long end, int alloc) 280 { 281 unsigned long *pte, next; 282 283 if (!alloc) 284 return 0; 285 pte = origin; 286 pte += (addr & _PAGE_INDEX) >> PAGE_SHIFT; 287 do { 288 next = base_page_addr_end(addr, end); 289 *pte = base_lra(addr); 290 } while (pte++, addr = next, addr < end); 291 return 0; 292 } 293 294 static int base_segment_walk(unsigned long *origin, unsigned long addr, 295 unsigned long end, int alloc) 296 { 297 unsigned long *ste, next, *table; 298 int rc; 299 300 ste = origin; 301 ste += (addr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT; 302 do { 303 next = base_segment_addr_end(addr, end); 304 if (*ste & _SEGMENT_ENTRY_INVALID) { 305 if (!alloc) 306 continue; 307 table = base_pgt_alloc(); 308 if (!table) 309 return -ENOMEM; 310 *ste = __pa(table) | _SEGMENT_ENTRY; 311 } 312 table = __va(*ste & _SEGMENT_ENTRY_ORIGIN); 313 rc = base_page_walk(table, addr, next, alloc); 314 if (rc) 315 return rc; 316 if (!alloc) 317 base_pgt_free(table); 318 cond_resched(); 319 } while (ste++, addr = next, addr < end); 320 return 0; 321 } 322 323 static int base_region3_walk(unsigned long *origin, unsigned long addr, 324 unsigned long end, int alloc) 325 { 326 unsigned long *rtte, next, *table; 327 int rc; 328 329 rtte = origin; 330 rtte += (addr & _REGION3_INDEX) >> _REGION3_SHIFT; 331 do { 332 next = base_region3_addr_end(addr, end); 333 if (*rtte & _REGION_ENTRY_INVALID) { 334 if (!alloc) 335 continue; 336 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY); 337 if (!table) 338 return -ENOMEM; 339 *rtte = __pa(table) | _REGION3_ENTRY; 340 } 341 table = __va(*rtte & _REGION_ENTRY_ORIGIN); 342 rc = base_segment_walk(table, addr, next, alloc); 343 if (rc) 344 return rc; 345 if (!alloc) 346 base_crst_free(table); 347 } while (rtte++, addr = next, addr < end); 348 return 0; 349 } 350 351 static int base_region2_walk(unsigned long *origin, unsigned long addr, 352 unsigned long end, int alloc) 353 { 354 unsigned long *rste, next, *table; 355 int rc; 356 357 rste = origin; 358 rste += (addr & _REGION2_INDEX) >> _REGION2_SHIFT; 359 do { 360 next = base_region2_addr_end(addr, end); 361 if (*rste & _REGION_ENTRY_INVALID) { 362 if (!alloc) 363 continue; 364 table = base_crst_alloc(_REGION3_ENTRY_EMPTY); 365 if (!table) 366 return -ENOMEM; 367 *rste = __pa(table) | _REGION2_ENTRY; 368 } 369 table = __va(*rste & _REGION_ENTRY_ORIGIN); 370 rc = base_region3_walk(table, addr, next, alloc); 371 if (rc) 372 return rc; 373 if (!alloc) 374 base_crst_free(table); 375 } while (rste++, addr = next, addr < end); 376 return 0; 377 } 378 379 static int base_region1_walk(unsigned long *origin, unsigned long addr, 380 unsigned long end, int alloc) 381 { 382 unsigned long *rfte, next, *table; 383 int rc; 384 385 rfte = origin; 386 rfte += (addr & _REGION1_INDEX) >> _REGION1_SHIFT; 387 do { 388 next = base_region1_addr_end(addr, end); 389 if (*rfte & _REGION_ENTRY_INVALID) { 390 if (!alloc) 391 continue; 392 table = base_crst_alloc(_REGION2_ENTRY_EMPTY); 393 if (!table) 394 return -ENOMEM; 395 *rfte = __pa(table) | _REGION1_ENTRY; 396 } 397 table = __va(*rfte & _REGION_ENTRY_ORIGIN); 398 rc = base_region2_walk(table, addr, next, alloc); 399 if (rc) 400 return rc; 401 if (!alloc) 402 base_crst_free(table); 403 } while (rfte++, addr = next, addr < end); 404 return 0; 405 } 406 407 /** 408 * base_asce_free - free asce and tables returned from base_asce_alloc() 409 * @asce: asce to be freed 410 * 411 * Frees all region, segment, and page tables that were allocated with a 412 * corresponding base_asce_alloc() call. 413 */ 414 void base_asce_free(unsigned long asce) 415 { 416 unsigned long *table = __va(asce & _ASCE_ORIGIN); 417 418 if (!asce) 419 return; 420 switch (asce & _ASCE_TYPE_MASK) { 421 case _ASCE_TYPE_SEGMENT: 422 base_segment_walk(table, 0, _REGION3_SIZE, 0); 423 break; 424 case _ASCE_TYPE_REGION3: 425 base_region3_walk(table, 0, _REGION2_SIZE, 0); 426 break; 427 case _ASCE_TYPE_REGION2: 428 base_region2_walk(table, 0, _REGION1_SIZE, 0); 429 break; 430 case _ASCE_TYPE_REGION1: 431 base_region1_walk(table, 0, TASK_SIZE_MAX, 0); 432 break; 433 } 434 base_crst_free(table); 435 } 436 437 static int base_pgt_cache_init(void) 438 { 439 static DEFINE_MUTEX(base_pgt_cache_mutex); 440 unsigned long sz = _PAGE_TABLE_SIZE; 441 442 if (base_pgt_cache) 443 return 0; 444 mutex_lock(&base_pgt_cache_mutex); 445 if (!base_pgt_cache) 446 base_pgt_cache = kmem_cache_create("base_pgt", sz, sz, 0, NULL); 447 mutex_unlock(&base_pgt_cache_mutex); 448 return base_pgt_cache ? 0 : -ENOMEM; 449 } 450 451 /** 452 * base_asce_alloc - create kernel mapping without enhanced DAT features 453 * @addr: virtual start address of kernel mapping 454 * @num_pages: number of consecutive pages 455 * 456 * Generate an asce, including all required region, segment and page tables, 457 * that can be used to access the virtual kernel mapping. The difference is 458 * that the returned asce does not make use of any enhanced DAT features like 459 * e.g. large pages. This is required for some I/O functions that pass an 460 * asce, like e.g. some service call requests. 461 * 462 * Note: the returned asce may NEVER be attached to any cpu. It may only be 463 * used for I/O requests. tlb entries that might result because the 464 * asce was attached to a cpu won't be cleared. 465 */ 466 unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages) 467 { 468 unsigned long asce, *table, end; 469 int rc; 470 471 if (base_pgt_cache_init()) 472 return 0; 473 end = addr + num_pages * PAGE_SIZE; 474 if (end <= _REGION3_SIZE) { 475 table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY); 476 if (!table) 477 return 0; 478 rc = base_segment_walk(table, addr, end, 1); 479 asce = __pa(table) | _ASCE_TYPE_SEGMENT | _ASCE_TABLE_LENGTH; 480 } else if (end <= _REGION2_SIZE) { 481 table = base_crst_alloc(_REGION3_ENTRY_EMPTY); 482 if (!table) 483 return 0; 484 rc = base_region3_walk(table, addr, end, 1); 485 asce = __pa(table) | _ASCE_TYPE_REGION3 | _ASCE_TABLE_LENGTH; 486 } else if (end <= _REGION1_SIZE) { 487 table = base_crst_alloc(_REGION2_ENTRY_EMPTY); 488 if (!table) 489 return 0; 490 rc = base_region2_walk(table, addr, end, 1); 491 asce = __pa(table) | _ASCE_TYPE_REGION2 | _ASCE_TABLE_LENGTH; 492 } else { 493 table = base_crst_alloc(_REGION1_ENTRY_EMPTY); 494 if (!table) 495 return 0; 496 rc = base_region1_walk(table, addr, end, 1); 497 asce = __pa(table) | _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH; 498 } 499 if (rc) { 500 base_asce_free(asce); 501 asce = 0; 502 } 503 return asce; 504 } 505