pgalloc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. #include <linux/sysctl.h>
  9. #include <linux/slab.h>
  10. #include <linux/mm.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. #ifdef CONFIG_PGSTE
  17. int page_table_allocate_pgste = 0;
  18. EXPORT_SYMBOL(page_table_allocate_pgste);
  19. static struct ctl_table page_table_sysctl[] = {
  20. {
  21. .procname = "allocate_pgste",
  22. .data = &page_table_allocate_pgste,
  23. .maxlen = sizeof(int),
  24. .mode = S_IRUGO | S_IWUSR,
  25. .proc_handler = proc_dointvec_minmax,
  26. .extra1 = SYSCTL_ZERO,
  27. .extra2 = SYSCTL_ONE,
  28. },
  29. { }
  30. };
  31. static struct ctl_table page_table_sysctl_dir[] = {
  32. {
  33. .procname = "vm",
  34. .maxlen = 0,
  35. .mode = 0555,
  36. .child = page_table_sysctl,
  37. },
  38. { }
  39. };
  40. static int __init page_table_register_sysctl(void)
  41. {
  42. return register_sysctl_table(page_table_sysctl_dir) ? 0 : -ENOMEM;
  43. }
  44. __initcall(page_table_register_sysctl);
  45. #endif /* CONFIG_PGSTE */
  46. unsigned long *crst_table_alloc(struct mm_struct *mm)
  47. {
  48. struct page *page = alloc_pages(GFP_KERNEL, 2);
  49. if (!page)
  50. return NULL;
  51. arch_set_page_dat(page, 2);
  52. return (unsigned long *) page_to_phys(page);
  53. }
  54. void crst_table_free(struct mm_struct *mm, unsigned long *table)
  55. {
  56. free_pages((unsigned long) table, 2);
  57. }
  58. static void __crst_table_upgrade(void *arg)
  59. {
  60. struct mm_struct *mm = arg;
  61. /* we must change all active ASCEs to avoid the creation of new TLBs */
  62. if (current->active_mm == mm) {
  63. S390_lowcore.user_asce = mm->context.asce;
  64. if (current->thread.mm_segment == USER_DS) {
  65. __ctl_load(S390_lowcore.user_asce, 1, 1);
  66. /* Mark user-ASCE present in CR1 */
  67. clear_cpu_flag(CIF_ASCE_PRIMARY);
  68. }
  69. if (current->thread.mm_segment == USER_DS_SACF) {
  70. __ctl_load(S390_lowcore.user_asce, 7, 7);
  71. /* enable_sacf_uaccess does all or nothing */
  72. WARN_ON(!test_cpu_flag(CIF_ASCE_SECONDARY));
  73. }
  74. }
  75. __tlb_flush_local();
  76. }
  77. int crst_table_upgrade(struct mm_struct *mm, unsigned long end)
  78. {
  79. unsigned long *pgd = NULL, *p4d = NULL, *__pgd;
  80. unsigned long asce_limit = mm->context.asce_limit;
  81. /* upgrade should only happen from 3 to 4, 3 to 5, or 4 to 5 levels */
  82. VM_BUG_ON(asce_limit < _REGION2_SIZE);
  83. if (end <= asce_limit)
  84. return 0;
  85. if (asce_limit == _REGION2_SIZE) {
  86. p4d = crst_table_alloc(mm);
  87. if (unlikely(!p4d))
  88. goto err_p4d;
  89. crst_table_init(p4d, _REGION2_ENTRY_EMPTY);
  90. }
  91. if (end > _REGION1_SIZE) {
  92. pgd = crst_table_alloc(mm);
  93. if (unlikely(!pgd))
  94. goto err_pgd;
  95. crst_table_init(pgd, _REGION1_ENTRY_EMPTY);
  96. }
  97. spin_lock_bh(&mm->page_table_lock);
  98. /*
  99. * This routine gets called with mmap_lock lock held and there is
  100. * no reason to optimize for the case of otherwise. However, if
  101. * that would ever change, the below check will let us know.
  102. */
  103. VM_BUG_ON(asce_limit != mm->context.asce_limit);
  104. if (p4d) {
  105. __pgd = (unsigned long *) mm->pgd;
  106. p4d_populate(mm, (p4d_t *) p4d, (pud_t *) __pgd);
  107. mm->pgd = (pgd_t *) p4d;
  108. mm->context.asce_limit = _REGION1_SIZE;
  109. mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
  110. _ASCE_USER_BITS | _ASCE_TYPE_REGION2;
  111. mm_inc_nr_puds(mm);
  112. }
  113. if (pgd) {
  114. __pgd = (unsigned long *) mm->pgd;
  115. pgd_populate(mm, (pgd_t *) pgd, (p4d_t *) __pgd);
  116. mm->pgd = (pgd_t *) pgd;
  117. mm->context.asce_limit = TASK_SIZE_MAX;
  118. mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
  119. _ASCE_USER_BITS | _ASCE_TYPE_REGION1;
  120. }
  121. spin_unlock_bh(&mm->page_table_lock);
  122. on_each_cpu(__crst_table_upgrade, mm, 0);
  123. return 0;
  124. err_pgd:
  125. crst_table_free(mm, p4d);
  126. err_p4d:
  127. return -ENOMEM;
  128. }
  129. static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
  130. {
  131. unsigned int old, new;
  132. do {
  133. old = atomic_read(v);
  134. new = old ^ bits;
  135. } while (atomic_cmpxchg(v, old, new) != old);
  136. return new;
  137. }
  138. #ifdef CONFIG_PGSTE
  139. struct page *page_table_alloc_pgste(struct mm_struct *mm)
  140. {
  141. struct page *page;
  142. u64 *table;
  143. page = alloc_page(GFP_KERNEL);
  144. if (page) {
  145. table = (u64 *)page_to_phys(page);
  146. memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
  147. memset64(table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
  148. }
  149. return page;
  150. }
  151. void page_table_free_pgste(struct page *page)
  152. {
  153. __free_page(page);
  154. }
  155. #endif /* CONFIG_PGSTE */
  156. /*
  157. * page table entry allocation/free routines.
  158. */
  159. unsigned long *page_table_alloc(struct mm_struct *mm)
  160. {
  161. unsigned long *table;
  162. struct page *page;
  163. unsigned int mask, bit;
  164. /* Try to get a fragment of a 4K page as a 2K page table */
  165. if (!mm_alloc_pgste(mm)) {
  166. table = NULL;
  167. spin_lock_bh(&mm->context.lock);
  168. if (!list_empty(&mm->context.pgtable_list)) {
  169. page = list_first_entry(&mm->context.pgtable_list,
  170. struct page, lru);
  171. mask = atomic_read(&page->_refcount) >> 24;
  172. mask = (mask | (mask >> 4)) & 3;
  173. if (mask != 3) {
  174. table = (unsigned long *) page_to_phys(page);
  175. bit = mask & 1; /* =1 -> second 2K */
  176. if (bit)
  177. table += PTRS_PER_PTE;
  178. atomic_xor_bits(&page->_refcount,
  179. 1U << (bit + 24));
  180. list_del(&page->lru);
  181. }
  182. }
  183. spin_unlock_bh(&mm->context.lock);
  184. if (table)
  185. return table;
  186. }
  187. /* Allocate a fresh page */
  188. page = alloc_page(GFP_KERNEL);
  189. if (!page)
  190. return NULL;
  191. if (!pgtable_pte_page_ctor(page)) {
  192. __free_page(page);
  193. return NULL;
  194. }
  195. arch_set_page_dat(page, 0);
  196. /* Initialize page table */
  197. table = (unsigned long *) page_to_phys(page);
  198. if (mm_alloc_pgste(mm)) {
  199. /* Return 4K page table with PGSTEs */
  200. atomic_xor_bits(&page->_refcount, 3 << 24);
  201. memset64((u64 *)table, _PAGE_INVALID, PTRS_PER_PTE);
  202. memset64((u64 *)table + PTRS_PER_PTE, 0, PTRS_PER_PTE);
  203. } else {
  204. /* Return the first 2K fragment of the page */
  205. atomic_xor_bits(&page->_refcount, 1 << 24);
  206. memset64((u64 *)table, _PAGE_INVALID, 2 * PTRS_PER_PTE);
  207. spin_lock_bh(&mm->context.lock);
  208. list_add(&page->lru, &mm->context.pgtable_list);
  209. spin_unlock_bh(&mm->context.lock);
  210. }
  211. return table;
  212. }
  213. void page_table_free(struct mm_struct *mm, unsigned long *table)
  214. {
  215. struct page *page;
  216. unsigned int bit, mask;
  217. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  218. if (!mm_alloc_pgste(mm)) {
  219. /* Free 2K page table fragment of a 4K page */
  220. bit = (__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t));
  221. spin_lock_bh(&mm->context.lock);
  222. mask = atomic_xor_bits(&page->_refcount, 0x11U << (bit + 24));
  223. mask >>= 24;
  224. if (mask & 3)
  225. list_add(&page->lru, &mm->context.pgtable_list);
  226. else
  227. list_del(&page->lru);
  228. spin_unlock_bh(&mm->context.lock);
  229. mask = atomic_xor_bits(&page->_refcount, 0x10U << (bit + 24));
  230. mask >>= 24;
  231. if (mask != 0)
  232. return;
  233. } else {
  234. atomic_xor_bits(&page->_refcount, 3U << 24);
  235. }
  236. pgtable_pte_page_dtor(page);
  237. __free_page(page);
  238. }
  239. void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
  240. unsigned long vmaddr)
  241. {
  242. struct mm_struct *mm;
  243. struct page *page;
  244. unsigned int bit, mask;
  245. mm = tlb->mm;
  246. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  247. if (mm_alloc_pgste(mm)) {
  248. gmap_unlink(mm, table, vmaddr);
  249. table = (unsigned long *) (__pa(table) | 3);
  250. tlb_remove_table(tlb, table);
  251. return;
  252. }
  253. bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
  254. spin_lock_bh(&mm->context.lock);
  255. mask = atomic_xor_bits(&page->_refcount, 0x11U << (bit + 24));
  256. mask >>= 24;
  257. if (mask & 3)
  258. list_add_tail(&page->lru, &mm->context.pgtable_list);
  259. else
  260. list_del(&page->lru);
  261. spin_unlock_bh(&mm->context.lock);
  262. table = (unsigned long *) (__pa(table) | (1U << bit));
  263. tlb_remove_table(tlb, table);
  264. }
  265. void __tlb_remove_table(void *_table)
  266. {
  267. unsigned int mask = (unsigned long) _table & 3;
  268. void *table = (void *)((unsigned long) _table ^ mask);
  269. struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  270. switch (mask) {
  271. case 0: /* pmd, pud, or p4d */
  272. free_pages((unsigned long) table, 2);
  273. break;
  274. case 1: /* lower 2K of a 4K page table */
  275. case 2: /* higher 2K of a 4K page table */
  276. mask = atomic_xor_bits(&page->_refcount, mask << (4 + 24));
  277. mask >>= 24;
  278. if (mask != 0)
  279. break;
  280. fallthrough;
  281. case 3: /* 4K page table with pgstes */
  282. if (mask & 3)
  283. atomic_xor_bits(&page->_refcount, 3 << 24);
  284. pgtable_pte_page_dtor(page);
  285. __free_page(page);
  286. break;
  287. }
  288. }
  289. /*
  290. * Base infrastructure required to generate basic asces, region, segment,
  291. * and page tables that do not make use of enhanced features like EDAT1.
  292. */
  293. static struct kmem_cache *base_pgt_cache;
  294. static unsigned long base_pgt_alloc(void)
  295. {
  296. u64 *table;
  297. table = kmem_cache_alloc(base_pgt_cache, GFP_KERNEL);
  298. if (table)
  299. memset64(table, _PAGE_INVALID, PTRS_PER_PTE);
  300. return (unsigned long) table;
  301. }
  302. static void base_pgt_free(unsigned long table)
  303. {
  304. kmem_cache_free(base_pgt_cache, (void *) table);
  305. }
  306. static unsigned long base_crst_alloc(unsigned long val)
  307. {
  308. unsigned long table;
  309. table = __get_free_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
  310. if (table)
  311. crst_table_init((unsigned long *)table, val);
  312. return table;
  313. }
  314. static void base_crst_free(unsigned long table)
  315. {
  316. free_pages(table, CRST_ALLOC_ORDER);
  317. }
  318. #define BASE_ADDR_END_FUNC(NAME, SIZE) \
  319. static inline unsigned long base_##NAME##_addr_end(unsigned long addr, \
  320. unsigned long end) \
  321. { \
  322. unsigned long next = (addr + (SIZE)) & ~((SIZE) - 1); \
  323. \
  324. return (next - 1) < (end - 1) ? next : end; \
  325. }
  326. BASE_ADDR_END_FUNC(page, _PAGE_SIZE)
  327. BASE_ADDR_END_FUNC(segment, _SEGMENT_SIZE)
  328. BASE_ADDR_END_FUNC(region3, _REGION3_SIZE)
  329. BASE_ADDR_END_FUNC(region2, _REGION2_SIZE)
  330. BASE_ADDR_END_FUNC(region1, _REGION1_SIZE)
  331. static inline unsigned long base_lra(unsigned long address)
  332. {
  333. unsigned long real;
  334. asm volatile(
  335. " lra %0,0(%1)\n"
  336. : "=d" (real) : "a" (address) : "cc");
  337. return real;
  338. }
  339. static int base_page_walk(unsigned long origin, unsigned long addr,
  340. unsigned long end, int alloc)
  341. {
  342. unsigned long *pte, next;
  343. if (!alloc)
  344. return 0;
  345. pte = (unsigned long *) origin;
  346. pte += (addr & _PAGE_INDEX) >> _PAGE_SHIFT;
  347. do {
  348. next = base_page_addr_end(addr, end);
  349. *pte = base_lra(addr);
  350. } while (pte++, addr = next, addr < end);
  351. return 0;
  352. }
  353. static int base_segment_walk(unsigned long origin, unsigned long addr,
  354. unsigned long end, int alloc)
  355. {
  356. unsigned long *ste, next, table;
  357. int rc;
  358. ste = (unsigned long *) origin;
  359. ste += (addr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
  360. do {
  361. next = base_segment_addr_end(addr, end);
  362. if (*ste & _SEGMENT_ENTRY_INVALID) {
  363. if (!alloc)
  364. continue;
  365. table = base_pgt_alloc();
  366. if (!table)
  367. return -ENOMEM;
  368. *ste = table | _SEGMENT_ENTRY;
  369. }
  370. table = *ste & _SEGMENT_ENTRY_ORIGIN;
  371. rc = base_page_walk(table, addr, next, alloc);
  372. if (rc)
  373. return rc;
  374. if (!alloc)
  375. base_pgt_free(table);
  376. cond_resched();
  377. } while (ste++, addr = next, addr < end);
  378. return 0;
  379. }
  380. static int base_region3_walk(unsigned long origin, unsigned long addr,
  381. unsigned long end, int alloc)
  382. {
  383. unsigned long *rtte, next, table;
  384. int rc;
  385. rtte = (unsigned long *) origin;
  386. rtte += (addr & _REGION3_INDEX) >> _REGION3_SHIFT;
  387. do {
  388. next = base_region3_addr_end(addr, end);
  389. if (*rtte & _REGION_ENTRY_INVALID) {
  390. if (!alloc)
  391. continue;
  392. table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
  393. if (!table)
  394. return -ENOMEM;
  395. *rtte = table | _REGION3_ENTRY;
  396. }
  397. table = *rtte & _REGION_ENTRY_ORIGIN;
  398. rc = base_segment_walk(table, addr, next, alloc);
  399. if (rc)
  400. return rc;
  401. if (!alloc)
  402. base_crst_free(table);
  403. } while (rtte++, addr = next, addr < end);
  404. return 0;
  405. }
  406. static int base_region2_walk(unsigned long origin, unsigned long addr,
  407. unsigned long end, int alloc)
  408. {
  409. unsigned long *rste, next, table;
  410. int rc;
  411. rste = (unsigned long *) origin;
  412. rste += (addr & _REGION2_INDEX) >> _REGION2_SHIFT;
  413. do {
  414. next = base_region2_addr_end(addr, end);
  415. if (*rste & _REGION_ENTRY_INVALID) {
  416. if (!alloc)
  417. continue;
  418. table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
  419. if (!table)
  420. return -ENOMEM;
  421. *rste = table | _REGION2_ENTRY;
  422. }
  423. table = *rste & _REGION_ENTRY_ORIGIN;
  424. rc = base_region3_walk(table, addr, next, alloc);
  425. if (rc)
  426. return rc;
  427. if (!alloc)
  428. base_crst_free(table);
  429. } while (rste++, addr = next, addr < end);
  430. return 0;
  431. }
  432. static int base_region1_walk(unsigned long origin, unsigned long addr,
  433. unsigned long end, int alloc)
  434. {
  435. unsigned long *rfte, next, table;
  436. int rc;
  437. rfte = (unsigned long *) origin;
  438. rfte += (addr & _REGION1_INDEX) >> _REGION1_SHIFT;
  439. do {
  440. next = base_region1_addr_end(addr, end);
  441. if (*rfte & _REGION_ENTRY_INVALID) {
  442. if (!alloc)
  443. continue;
  444. table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
  445. if (!table)
  446. return -ENOMEM;
  447. *rfte = table | _REGION1_ENTRY;
  448. }
  449. table = *rfte & _REGION_ENTRY_ORIGIN;
  450. rc = base_region2_walk(table, addr, next, alloc);
  451. if (rc)
  452. return rc;
  453. if (!alloc)
  454. base_crst_free(table);
  455. } while (rfte++, addr = next, addr < end);
  456. return 0;
  457. }
  458. /**
  459. * base_asce_free - free asce and tables returned from base_asce_alloc()
  460. * @asce: asce to be freed
  461. *
  462. * Frees all region, segment, and page tables that were allocated with a
  463. * corresponding base_asce_alloc() call.
  464. */
  465. void base_asce_free(unsigned long asce)
  466. {
  467. unsigned long table = asce & _ASCE_ORIGIN;
  468. if (!asce)
  469. return;
  470. switch (asce & _ASCE_TYPE_MASK) {
  471. case _ASCE_TYPE_SEGMENT:
  472. base_segment_walk(table, 0, _REGION3_SIZE, 0);
  473. break;
  474. case _ASCE_TYPE_REGION3:
  475. base_region3_walk(table, 0, _REGION2_SIZE, 0);
  476. break;
  477. case _ASCE_TYPE_REGION2:
  478. base_region2_walk(table, 0, _REGION1_SIZE, 0);
  479. break;
  480. case _ASCE_TYPE_REGION1:
  481. base_region1_walk(table, 0, TASK_SIZE_MAX, 0);
  482. break;
  483. }
  484. base_crst_free(table);
  485. }
  486. static int base_pgt_cache_init(void)
  487. {
  488. static DEFINE_MUTEX(base_pgt_cache_mutex);
  489. unsigned long sz = _PAGE_TABLE_SIZE;
  490. if (base_pgt_cache)
  491. return 0;
  492. mutex_lock(&base_pgt_cache_mutex);
  493. if (!base_pgt_cache)
  494. base_pgt_cache = kmem_cache_create("base_pgt", sz, sz, 0, NULL);
  495. mutex_unlock(&base_pgt_cache_mutex);
  496. return base_pgt_cache ? 0 : -ENOMEM;
  497. }
  498. /**
  499. * base_asce_alloc - create kernel mapping without enhanced DAT features
  500. * @addr: virtual start address of kernel mapping
  501. * @num_pages: number of consecutive pages
  502. *
  503. * Generate an asce, including all required region, segment and page tables,
  504. * that can be used to access the virtual kernel mapping. The difference is
  505. * that the returned asce does not make use of any enhanced DAT features like
  506. * e.g. large pages. This is required for some I/O functions that pass an
  507. * asce, like e.g. some service call requests.
  508. *
  509. * Note: the returned asce may NEVER be attached to any cpu. It may only be
  510. * used for I/O requests. tlb entries that might result because the
  511. * asce was attached to a cpu won't be cleared.
  512. */
  513. unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages)
  514. {
  515. unsigned long asce, table, end;
  516. int rc;
  517. if (base_pgt_cache_init())
  518. return 0;
  519. end = addr + num_pages * PAGE_SIZE;
  520. if (end <= _REGION3_SIZE) {
  521. table = base_crst_alloc(_SEGMENT_ENTRY_EMPTY);
  522. if (!table)
  523. return 0;
  524. rc = base_segment_walk(table, addr, end, 1);
  525. asce = table | _ASCE_TYPE_SEGMENT | _ASCE_TABLE_LENGTH;
  526. } else if (end <= _REGION2_SIZE) {
  527. table = base_crst_alloc(_REGION3_ENTRY_EMPTY);
  528. if (!table)
  529. return 0;
  530. rc = base_region3_walk(table, addr, end, 1);
  531. asce = table | _ASCE_TYPE_REGION3 | _ASCE_TABLE_LENGTH;
  532. } else if (end <= _REGION1_SIZE) {
  533. table = base_crst_alloc(_REGION2_ENTRY_EMPTY);
  534. if (!table)
  535. return 0;
  536. rc = base_region2_walk(table, addr, end, 1);
  537. asce = table | _ASCE_TYPE_REGION2 | _ASCE_TABLE_LENGTH;
  538. } else {
  539. table = base_crst_alloc(_REGION1_ENTRY_EMPTY);
  540. if (!table)
  541. return 0;
  542. rc = base_region1_walk(table, addr, end, 1);
  543. asce = table | _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH;
  544. }
  545. if (rc) {
  546. base_asce_free(asce);
  547. asce = 0;
  548. }
  549. return asce;
  550. }