pgalloc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #ifdef CONFIG_PGSTE
  116. struct page *page_table_alloc_pgste(struct mm_struct *mm)
  117. {
  118. struct page *page;
  119. unsigned long *table;
  120. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  121. if (page) {
  122. table = (unsigned long *) page_to_phys(page);
  123. clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
  124. clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
  125. }
  126. return page;
  127. }
  128. void page_table_free_pgste(struct page *page)
  129. {
  130. __free_page(page);
  131. }
  132. #endif /* CONFIG_PGSTE */
  133. /*
  134. * page table entry allocation/free routines.
  135. */
  136. unsigned long *page_table_alloc(struct mm_struct *mm)
  137. {
  138. unsigned long *table;
  139. struct page *page;
  140. unsigned int mask, bit;
  141. /* Try to get a fragment of a 4K page as a 2K page table */
  142. if (!mm_alloc_pgste(mm)) {
  143. table = NULL;
  144. spin_lock_bh(&mm->context.pgtable_lock);
  145. if (!list_empty(&mm->context.pgtable_list)) {
  146. page = list_first_entry(&mm->context.pgtable_list,
  147. struct page, lru);
  148. mask = atomic_read(&page->_mapcount);
  149. mask = (mask | (mask >> 4)) & 3;
  150. if (mask != 3) {
  151. table = (unsigned long *) page_to_phys(page);
  152. bit = mask & 1; /* =1 -> second 2K */
  153. if (bit)
  154. table += PTRS_PER_PTE;
  155. atomic_xor_bits(&page->_mapcount, 1U << bit);
  156. list_del(&page->lru);
  157. }
  158. }
  159. spin_unlock_bh(&mm->context.pgtable_lock);
  160. if (table)
  161. return table;
  162. }
  163. /* Allocate a fresh page */
  164. page = alloc_page(GFP_KERNEL);
  165. if (!page)
  166. return NULL;
  167. if (!pgtable_page_ctor(page)) {
  168. __free_page(page);
  169. return NULL;
  170. }
  171. /* Initialize page table */
  172. table = (unsigned long *) page_to_phys(page);
  173. if (mm_alloc_pgste(mm)) {
  174. /* Return 4K page table with PGSTEs */
  175. atomic_set(&page->_mapcount, 3);
  176. clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
  177. clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
  178. } else {
  179. /* Return the first 2K fragment of the page */
  180. atomic_set(&page->_mapcount, 1);
  181. clear_table(table, _PAGE_INVALID, PAGE_SIZE);
  182. spin_lock_bh(&mm->context.pgtable_lock);
  183. list_add(&page->lru, &mm->context.pgtable_list);
  184. spin_unlock_bh(&mm->context.pgtable_lock);
  185. }
  186. return table;
  187. }
  188. void page_table_free(struct mm_struct *mm, unsigned long *table)
  189. {
  190. struct page *page;
  191. unsigned int bit, mask;
  192. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  193. if (!mm_alloc_pgste(mm)) {
  194. /* Free 2K page table fragment of a 4K page */
  195. bit = (__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t));
  196. spin_lock_bh(&mm->context.pgtable_lock);
  197. mask = atomic_xor_bits(&page->_mapcount, 1U << bit);
  198. if (mask & 3)
  199. list_add(&page->lru, &mm->context.pgtable_list);
  200. else
  201. list_del(&page->lru);
  202. spin_unlock_bh(&mm->context.pgtable_lock);
  203. if (mask != 0)
  204. return;
  205. }
  206. pgtable_page_dtor(page);
  207. atomic_set(&page->_mapcount, -1);
  208. __free_page(page);
  209. }
  210. void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
  211. unsigned long vmaddr)
  212. {
  213. struct mm_struct *mm;
  214. struct page *page;
  215. unsigned int bit, mask;
  216. mm = tlb->mm;
  217. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  218. if (mm_alloc_pgste(mm)) {
  219. gmap_unlink(mm, table, vmaddr);
  220. table = (unsigned long *) (__pa(table) | 3);
  221. tlb_remove_table(tlb, table);
  222. return;
  223. }
  224. bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
  225. spin_lock_bh(&mm->context.pgtable_lock);
  226. mask = atomic_xor_bits(&page->_mapcount, 0x11U << bit);
  227. if (mask & 3)
  228. list_add_tail(&page->lru, &mm->context.pgtable_list);
  229. else
  230. list_del(&page->lru);
  231. spin_unlock_bh(&mm->context.pgtable_lock);
  232. table = (unsigned long *) (__pa(table) | (1U << bit));
  233. tlb_remove_table(tlb, table);
  234. }
  235. static void __tlb_remove_table(void *_table)
  236. {
  237. unsigned int mask = (unsigned long) _table & 3;
  238. void *table = (void *)((unsigned long) _table ^ mask);
  239. struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  240. switch (mask) {
  241. case 0: /* pmd or pud */
  242. free_pages((unsigned long) table, 2);
  243. break;
  244. case 1: /* lower 2K of a 4K page table */
  245. case 2: /* higher 2K of a 4K page table */
  246. if (atomic_xor_bits(&page->_mapcount, mask << 4) != 0)
  247. break;
  248. /* fallthrough */
  249. case 3: /* 4K page table with pgstes */
  250. pgtable_page_dtor(page);
  251. atomic_set(&page->_mapcount, -1);
  252. __free_page(page);
  253. break;
  254. }
  255. }
  256. static void tlb_remove_table_smp_sync(void *arg)
  257. {
  258. /* Simply deliver the interrupt */
  259. }
  260. static void tlb_remove_table_one(void *table)
  261. {
  262. /*
  263. * This isn't an RCU grace period and hence the page-tables cannot be
  264. * assumed to be actually RCU-freed.
  265. *
  266. * It is however sufficient for software page-table walkers that rely
  267. * on IRQ disabling. See the comment near struct mmu_table_batch.
  268. */
  269. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  270. __tlb_remove_table(table);
  271. }
  272. static void tlb_remove_table_rcu(struct rcu_head *head)
  273. {
  274. struct mmu_table_batch *batch;
  275. int i;
  276. batch = container_of(head, struct mmu_table_batch, rcu);
  277. for (i = 0; i < batch->nr; i++)
  278. __tlb_remove_table(batch->tables[i]);
  279. free_page((unsigned long)batch);
  280. }
  281. void tlb_table_flush(struct mmu_gather *tlb)
  282. {
  283. struct mmu_table_batch **batch = &tlb->batch;
  284. if (*batch) {
  285. call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
  286. *batch = NULL;
  287. }
  288. }
  289. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  290. {
  291. struct mmu_table_batch **batch = &tlb->batch;
  292. tlb->mm->context.flush_mm = 1;
  293. if (*batch == NULL) {
  294. *batch = (struct mmu_table_batch *)
  295. __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  296. if (*batch == NULL) {
  297. __tlb_flush_mm_lazy(tlb->mm);
  298. tlb_remove_table_one(table);
  299. return;
  300. }
  301. (*batch)->nr = 0;
  302. }
  303. (*batch)->tables[(*batch)->nr++] = table;
  304. if ((*batch)->nr == MAX_TABLE_BATCH)
  305. tlb_flush_mmu(tlb);
  306. }