pgalloc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. mm->task_size = mm->context.asce_limit;
  83. spin_unlock_bh(&mm->page_table_lock);
  84. on_each_cpu(__crst_table_upgrade, mm, 0);
  85. return 0;
  86. }
  87. void crst_table_downgrade(struct mm_struct *mm)
  88. {
  89. pgd_t *pgd;
  90. /* downgrade should only happen from 3 to 2 levels (compat only) */
  91. BUG_ON(mm->context.asce_limit != (1UL << 42));
  92. if (current->active_mm == mm) {
  93. clear_user_asce();
  94. __tlb_flush_mm(mm);
  95. }
  96. pgd = mm->pgd;
  97. mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
  98. mm->context.asce_limit = 1UL << 31;
  99. mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
  100. _ASCE_USER_BITS | _ASCE_TYPE_SEGMENT;
  101. mm->task_size = mm->context.asce_limit;
  102. crst_table_free(mm, (unsigned long *) pgd);
  103. if (current->active_mm == mm)
  104. set_user_asce(mm);
  105. }
  106. static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
  107. {
  108. unsigned int old, new;
  109. do {
  110. old = atomic_read(v);
  111. new = old ^ bits;
  112. } while (atomic_cmpxchg(v, old, new) != old);
  113. return new;
  114. }
  115. /*
  116. * page table entry allocation/free routines.
  117. */
  118. unsigned long *page_table_alloc(struct mm_struct *mm)
  119. {
  120. unsigned long *table;
  121. struct page *page;
  122. unsigned int mask, bit;
  123. /* Try to get a fragment of a 4K page as a 2K page table */
  124. if (!mm_alloc_pgste(mm)) {
  125. table = NULL;
  126. spin_lock_bh(&mm->context.list_lock);
  127. if (!list_empty(&mm->context.pgtable_list)) {
  128. page = list_first_entry(&mm->context.pgtable_list,
  129. struct page, lru);
  130. mask = atomic_read(&page->_mapcount);
  131. mask = (mask | (mask >> 4)) & 3;
  132. if (mask != 3) {
  133. table = (unsigned long *) page_to_phys(page);
  134. bit = mask & 1; /* =1 -> second 2K */
  135. if (bit)
  136. table += PTRS_PER_PTE;
  137. atomic_xor_bits(&page->_mapcount, 1U << bit);
  138. list_del(&page->lru);
  139. }
  140. }
  141. spin_unlock_bh(&mm->context.list_lock);
  142. if (table)
  143. return table;
  144. }
  145. /* Allocate a fresh page */
  146. page = alloc_page(GFP_KERNEL);
  147. if (!page)
  148. return NULL;
  149. if (!pgtable_page_ctor(page)) {
  150. __free_page(page);
  151. return NULL;
  152. }
  153. /* Initialize page table */
  154. table = (unsigned long *) page_to_phys(page);
  155. if (mm_alloc_pgste(mm)) {
  156. /* Return 4K page table with PGSTEs */
  157. atomic_set(&page->_mapcount, 3);
  158. clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
  159. clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
  160. } else {
  161. /* Return the first 2K fragment of the page */
  162. atomic_set(&page->_mapcount, 1);
  163. clear_table(table, _PAGE_INVALID, PAGE_SIZE);
  164. spin_lock_bh(&mm->context.list_lock);
  165. list_add(&page->lru, &mm->context.pgtable_list);
  166. spin_unlock_bh(&mm->context.list_lock);
  167. }
  168. return table;
  169. }
  170. void page_table_free(struct mm_struct *mm, unsigned long *table)
  171. {
  172. struct page *page;
  173. unsigned int bit, mask;
  174. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  175. if (!mm_alloc_pgste(mm)) {
  176. /* Free 2K page table fragment of a 4K page */
  177. bit = (__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t));
  178. spin_lock_bh(&mm->context.list_lock);
  179. mask = atomic_xor_bits(&page->_mapcount, 1U << bit);
  180. if (mask & 3)
  181. list_add(&page->lru, &mm->context.pgtable_list);
  182. else
  183. list_del(&page->lru);
  184. spin_unlock_bh(&mm->context.list_lock);
  185. if (mask != 0)
  186. return;
  187. }
  188. pgtable_page_dtor(page);
  189. atomic_set(&page->_mapcount, -1);
  190. __free_page(page);
  191. }
  192. void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
  193. unsigned long vmaddr)
  194. {
  195. struct mm_struct *mm;
  196. struct page *page;
  197. unsigned int bit, mask;
  198. mm = tlb->mm;
  199. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  200. if (mm_alloc_pgste(mm)) {
  201. gmap_unlink(mm, table, vmaddr);
  202. table = (unsigned long *) (__pa(table) | 3);
  203. tlb_remove_table(tlb, table);
  204. return;
  205. }
  206. bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
  207. spin_lock_bh(&mm->context.list_lock);
  208. mask = atomic_xor_bits(&page->_mapcount, 0x11U << bit);
  209. if (mask & 3)
  210. list_add_tail(&page->lru, &mm->context.pgtable_list);
  211. else
  212. list_del(&page->lru);
  213. spin_unlock_bh(&mm->context.list_lock);
  214. table = (unsigned long *) (__pa(table) | (1U << bit));
  215. tlb_remove_table(tlb, table);
  216. }
  217. static void __tlb_remove_table(void *_table)
  218. {
  219. unsigned int mask = (unsigned long) _table & 3;
  220. void *table = (void *)((unsigned long) _table ^ mask);
  221. struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  222. switch (mask) {
  223. case 0: /* pmd or pud */
  224. free_pages((unsigned long) table, 2);
  225. break;
  226. case 1: /* lower 2K of a 4K page table */
  227. case 2: /* higher 2K of a 4K page table */
  228. if (atomic_xor_bits(&page->_mapcount, mask << 4) != 0)
  229. break;
  230. /* fallthrough */
  231. case 3: /* 4K page table with pgstes */
  232. pgtable_page_dtor(page);
  233. atomic_set(&page->_mapcount, -1);
  234. __free_page(page);
  235. break;
  236. }
  237. }
  238. static void tlb_remove_table_smp_sync(void *arg)
  239. {
  240. /* Simply deliver the interrupt */
  241. }
  242. static void tlb_remove_table_one(void *table)
  243. {
  244. /*
  245. * This isn't an RCU grace period and hence the page-tables cannot be
  246. * assumed to be actually RCU-freed.
  247. *
  248. * It is however sufficient for software page-table walkers that rely
  249. * on IRQ disabling. See the comment near struct mmu_table_batch.
  250. */
  251. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  252. __tlb_remove_table(table);
  253. }
  254. static void tlb_remove_table_rcu(struct rcu_head *head)
  255. {
  256. struct mmu_table_batch *batch;
  257. int i;
  258. batch = container_of(head, struct mmu_table_batch, rcu);
  259. for (i = 0; i < batch->nr; i++)
  260. __tlb_remove_table(batch->tables[i]);
  261. free_page((unsigned long)batch);
  262. }
  263. void tlb_table_flush(struct mmu_gather *tlb)
  264. {
  265. struct mmu_table_batch **batch = &tlb->batch;
  266. if (*batch) {
  267. call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
  268. *batch = NULL;
  269. }
  270. }
  271. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  272. {
  273. struct mmu_table_batch **batch = &tlb->batch;
  274. tlb->mm->context.flush_mm = 1;
  275. if (*batch == NULL) {
  276. *batch = (struct mmu_table_batch *)
  277. __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  278. if (*batch == NULL) {
  279. __tlb_flush_mm_lazy(tlb->mm);
  280. tlb_remove_table_one(table);
  281. return;
  282. }
  283. (*batch)->nr = 0;
  284. }
  285. (*batch)->tables[(*batch)->nr++] = table;
  286. if ((*batch)->nr == MAX_TABLE_BATCH)
  287. tlb_flush_mmu(tlb);
  288. }