pgalloc.c 8.4 KB

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