pgalloc.c 9.0 KB

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