pgtable.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright IBM Corp. 2007,2009
  3. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/kernel.h>
  7. #include <linux/errno.h>
  8. #include <linux/gfp.h>
  9. #include <linux/mm.h>
  10. #include <linux/swap.h>
  11. #include <linux/smp.h>
  12. #include <linux/highmem.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/module.h>
  16. #include <linux/quicklist.h>
  17. #include <linux/rcupdate.h>
  18. #include <asm/system.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/tlb.h>
  22. #include <asm/tlbflush.h>
  23. #include <asm/mmu_context.h>
  24. struct rcu_table_freelist {
  25. struct rcu_head rcu;
  26. struct mm_struct *mm;
  27. unsigned int pgt_index;
  28. unsigned int crst_index;
  29. unsigned long *table[0];
  30. };
  31. #define RCU_FREELIST_SIZE \
  32. ((PAGE_SIZE - sizeof(struct rcu_table_freelist)) \
  33. / sizeof(unsigned long))
  34. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  35. static DEFINE_PER_CPU(struct rcu_table_freelist *, rcu_table_freelist);
  36. static void __page_table_free(struct mm_struct *mm, unsigned long *table);
  37. static struct rcu_table_freelist *rcu_table_freelist_get(struct mm_struct *mm)
  38. {
  39. struct rcu_table_freelist **batchp = &__get_cpu_var(rcu_table_freelist);
  40. struct rcu_table_freelist *batch = *batchp;
  41. if (batch)
  42. return batch;
  43. batch = (struct rcu_table_freelist *) __get_free_page(GFP_ATOMIC);
  44. if (batch) {
  45. batch->mm = mm;
  46. batch->pgt_index = 0;
  47. batch->crst_index = RCU_FREELIST_SIZE;
  48. *batchp = batch;
  49. }
  50. return batch;
  51. }
  52. static void rcu_table_freelist_callback(struct rcu_head *head)
  53. {
  54. struct rcu_table_freelist *batch =
  55. container_of(head, struct rcu_table_freelist, rcu);
  56. while (batch->pgt_index > 0)
  57. __page_table_free(batch->mm, batch->table[--batch->pgt_index]);
  58. while (batch->crst_index < RCU_FREELIST_SIZE)
  59. crst_table_free(batch->mm, batch->table[batch->crst_index++]);
  60. free_page((unsigned long) batch);
  61. }
  62. void rcu_table_freelist_finish(void)
  63. {
  64. struct rcu_table_freelist *batch = __get_cpu_var(rcu_table_freelist);
  65. if (!batch)
  66. return;
  67. call_rcu(&batch->rcu, rcu_table_freelist_callback);
  68. __get_cpu_var(rcu_table_freelist) = NULL;
  69. }
  70. static void smp_sync(void *arg)
  71. {
  72. }
  73. #ifndef CONFIG_64BIT
  74. #define ALLOC_ORDER 1
  75. #define TABLES_PER_PAGE 4
  76. #define FRAG_MASK 15UL
  77. #define SECOND_HALVES 10UL
  78. void clear_table_pgstes(unsigned long *table)
  79. {
  80. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/4);
  81. memset(table + 256, 0, PAGE_SIZE/4);
  82. clear_table(table + 512, _PAGE_TYPE_EMPTY, PAGE_SIZE/4);
  83. memset(table + 768, 0, PAGE_SIZE/4);
  84. }
  85. #else
  86. #define ALLOC_ORDER 2
  87. #define TABLES_PER_PAGE 2
  88. #define FRAG_MASK 3UL
  89. #define SECOND_HALVES 2UL
  90. void clear_table_pgstes(unsigned long *table)
  91. {
  92. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/2);
  93. memset(table + 256, 0, PAGE_SIZE/2);
  94. }
  95. #endif
  96. unsigned long VMALLOC_START = VMALLOC_END - VMALLOC_SIZE;
  97. EXPORT_SYMBOL(VMALLOC_START);
  98. static int __init parse_vmalloc(char *arg)
  99. {
  100. if (!arg)
  101. return -EINVAL;
  102. VMALLOC_START = (VMALLOC_END - memparse(arg, &arg)) & PAGE_MASK;
  103. return 0;
  104. }
  105. early_param("vmalloc", parse_vmalloc);
  106. unsigned long *crst_table_alloc(struct mm_struct *mm)
  107. {
  108. struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  109. if (!page)
  110. return NULL;
  111. return (unsigned long *) page_to_phys(page);
  112. }
  113. void crst_table_free(struct mm_struct *mm, unsigned long *table)
  114. {
  115. free_pages((unsigned long) table, ALLOC_ORDER);
  116. }
  117. void crst_table_free_rcu(struct mm_struct *mm, unsigned long *table)
  118. {
  119. struct rcu_table_freelist *batch;
  120. if (atomic_read(&mm->mm_users) < 2 &&
  121. cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id()))) {
  122. crst_table_free(mm, table);
  123. return;
  124. }
  125. batch = rcu_table_freelist_get(mm);
  126. if (!batch) {
  127. smp_call_function(smp_sync, NULL, 1);
  128. crst_table_free(mm, table);
  129. return;
  130. }
  131. batch->table[--batch->crst_index] = table;
  132. if (batch->pgt_index >= batch->crst_index)
  133. rcu_table_freelist_finish();
  134. }
  135. #ifdef CONFIG_64BIT
  136. int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
  137. {
  138. unsigned long *table, *pgd;
  139. unsigned long entry;
  140. BUG_ON(limit > (1UL << 53));
  141. repeat:
  142. table = crst_table_alloc(mm);
  143. if (!table)
  144. return -ENOMEM;
  145. spin_lock_bh(&mm->page_table_lock);
  146. if (mm->context.asce_limit < limit) {
  147. pgd = (unsigned long *) mm->pgd;
  148. if (mm->context.asce_limit <= (1UL << 31)) {
  149. entry = _REGION3_ENTRY_EMPTY;
  150. mm->context.asce_limit = 1UL << 42;
  151. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  152. _ASCE_USER_BITS |
  153. _ASCE_TYPE_REGION3;
  154. } else {
  155. entry = _REGION2_ENTRY_EMPTY;
  156. mm->context.asce_limit = 1UL << 53;
  157. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  158. _ASCE_USER_BITS |
  159. _ASCE_TYPE_REGION2;
  160. }
  161. crst_table_init(table, entry);
  162. pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
  163. mm->pgd = (pgd_t *) table;
  164. mm->task_size = mm->context.asce_limit;
  165. table = NULL;
  166. }
  167. spin_unlock_bh(&mm->page_table_lock);
  168. if (table)
  169. crst_table_free(mm, table);
  170. if (mm->context.asce_limit < limit)
  171. goto repeat;
  172. update_mm(mm, current);
  173. return 0;
  174. }
  175. void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
  176. {
  177. pgd_t *pgd;
  178. if (mm->context.asce_limit <= limit)
  179. return;
  180. __tlb_flush_mm(mm);
  181. while (mm->context.asce_limit > limit) {
  182. pgd = mm->pgd;
  183. switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
  184. case _REGION_ENTRY_TYPE_R2:
  185. mm->context.asce_limit = 1UL << 42;
  186. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  187. _ASCE_USER_BITS |
  188. _ASCE_TYPE_REGION3;
  189. break;
  190. case _REGION_ENTRY_TYPE_R3:
  191. mm->context.asce_limit = 1UL << 31;
  192. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  193. _ASCE_USER_BITS |
  194. _ASCE_TYPE_SEGMENT;
  195. break;
  196. default:
  197. BUG();
  198. }
  199. mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
  200. mm->task_size = mm->context.asce_limit;
  201. crst_table_free(mm, (unsigned long *) pgd);
  202. }
  203. update_mm(mm, current);
  204. }
  205. #endif
  206. /*
  207. * page table entry allocation/free routines.
  208. */
  209. unsigned long *page_table_alloc(struct mm_struct *mm)
  210. {
  211. struct page *page;
  212. unsigned long *table;
  213. unsigned long bits;
  214. bits = (mm->context.has_pgste) ? 3UL : 1UL;
  215. spin_lock_bh(&mm->context.list_lock);
  216. page = NULL;
  217. if (!list_empty(&mm->context.pgtable_list)) {
  218. page = list_first_entry(&mm->context.pgtable_list,
  219. struct page, lru);
  220. if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1))
  221. page = NULL;
  222. }
  223. if (!page) {
  224. spin_unlock_bh(&mm->context.list_lock);
  225. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  226. if (!page)
  227. return NULL;
  228. pgtable_page_ctor(page);
  229. page->flags &= ~FRAG_MASK;
  230. table = (unsigned long *) page_to_phys(page);
  231. if (mm->context.has_pgste)
  232. clear_table_pgstes(table);
  233. else
  234. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE);
  235. spin_lock_bh(&mm->context.list_lock);
  236. list_add(&page->lru, &mm->context.pgtable_list);
  237. }
  238. table = (unsigned long *) page_to_phys(page);
  239. while (page->flags & bits) {
  240. table += 256;
  241. bits <<= 1;
  242. }
  243. page->flags |= bits;
  244. if ((page->flags & FRAG_MASK) == ((1UL << TABLES_PER_PAGE) - 1))
  245. list_move_tail(&page->lru, &mm->context.pgtable_list);
  246. spin_unlock_bh(&mm->context.list_lock);
  247. return table;
  248. }
  249. static void __page_table_free(struct mm_struct *mm, unsigned long *table)
  250. {
  251. struct page *page;
  252. unsigned long bits;
  253. bits = ((unsigned long) table) & 15;
  254. table = (unsigned long *)(((unsigned long) table) ^ bits);
  255. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  256. page->flags ^= bits;
  257. if (!(page->flags & FRAG_MASK)) {
  258. pgtable_page_dtor(page);
  259. __free_page(page);
  260. }
  261. }
  262. void page_table_free(struct mm_struct *mm, unsigned long *table)
  263. {
  264. struct page *page;
  265. unsigned long bits;
  266. bits = (mm->context.has_pgste) ? 3UL : 1UL;
  267. bits <<= (__pa(table) & (PAGE_SIZE - 1)) / 256 / sizeof(unsigned long);
  268. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  269. spin_lock_bh(&mm->context.list_lock);
  270. page->flags ^= bits;
  271. if (page->flags & FRAG_MASK) {
  272. /* Page now has some free pgtable fragments. */
  273. if (!list_empty(&page->lru))
  274. list_move(&page->lru, &mm->context.pgtable_list);
  275. page = NULL;
  276. } else
  277. /* All fragments of the 4K page have been freed. */
  278. list_del(&page->lru);
  279. spin_unlock_bh(&mm->context.list_lock);
  280. if (page) {
  281. pgtable_page_dtor(page);
  282. __free_page(page);
  283. }
  284. }
  285. void page_table_free_rcu(struct mm_struct *mm, unsigned long *table)
  286. {
  287. struct rcu_table_freelist *batch;
  288. struct page *page;
  289. unsigned long bits;
  290. if (atomic_read(&mm->mm_users) < 2 &&
  291. cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id()))) {
  292. page_table_free(mm, table);
  293. return;
  294. }
  295. batch = rcu_table_freelist_get(mm);
  296. if (!batch) {
  297. smp_call_function(smp_sync, NULL, 1);
  298. page_table_free(mm, table);
  299. return;
  300. }
  301. bits = (mm->context.has_pgste) ? 3UL : 1UL;
  302. bits <<= (__pa(table) & (PAGE_SIZE - 1)) / 256 / sizeof(unsigned long);
  303. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  304. spin_lock_bh(&mm->context.list_lock);
  305. /* Delayed freeing with rcu prevents reuse of pgtable fragments */
  306. list_del_init(&page->lru);
  307. spin_unlock_bh(&mm->context.list_lock);
  308. table = (unsigned long *)(((unsigned long) table) | bits);
  309. batch->table[batch->pgt_index++] = table;
  310. if (batch->pgt_index >= batch->crst_index)
  311. rcu_table_freelist_finish();
  312. }
  313. /*
  314. * switch on pgstes for its userspace process (for kvm)
  315. */
  316. int s390_enable_sie(void)
  317. {
  318. struct task_struct *tsk = current;
  319. struct mm_struct *mm, *old_mm;
  320. /* Do we have switched amode? If no, we cannot do sie */
  321. if (user_mode == HOME_SPACE_MODE)
  322. return -EINVAL;
  323. /* Do we have pgstes? if yes, we are done */
  324. if (tsk->mm->context.has_pgste)
  325. return 0;
  326. /* lets check if we are allowed to replace the mm */
  327. task_lock(tsk);
  328. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  329. #ifdef CONFIG_AIO
  330. !hlist_empty(&tsk->mm->ioctx_list) ||
  331. #endif
  332. tsk->mm != tsk->active_mm) {
  333. task_unlock(tsk);
  334. return -EINVAL;
  335. }
  336. task_unlock(tsk);
  337. /* we copy the mm and let dup_mm create the page tables with_pgstes */
  338. tsk->mm->context.alloc_pgste = 1;
  339. mm = dup_mm(tsk);
  340. tsk->mm->context.alloc_pgste = 0;
  341. if (!mm)
  342. return -ENOMEM;
  343. /* Now lets check again if something happened */
  344. task_lock(tsk);
  345. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  346. #ifdef CONFIG_AIO
  347. !hlist_empty(&tsk->mm->ioctx_list) ||
  348. #endif
  349. tsk->mm != tsk->active_mm) {
  350. mmput(mm);
  351. task_unlock(tsk);
  352. return -EINVAL;
  353. }
  354. /* ok, we are alone. No ptrace, no threads, etc. */
  355. old_mm = tsk->mm;
  356. tsk->mm = tsk->active_mm = mm;
  357. preempt_disable();
  358. update_mm(mm, tsk);
  359. atomic_inc(&mm->context.attach_count);
  360. atomic_dec(&old_mm->context.attach_count);
  361. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  362. preempt_enable();
  363. task_unlock(tsk);
  364. mmput(old_mm);
  365. return 0;
  366. }
  367. EXPORT_SYMBOL_GPL(s390_enable_sie);
  368. #if defined(CONFIG_DEBUG_PAGEALLOC) && defined(CONFIG_HIBERNATION)
  369. bool kernel_page_present(struct page *page)
  370. {
  371. unsigned long addr;
  372. int cc;
  373. addr = page_to_phys(page);
  374. asm volatile(
  375. " lra %1,0(%1)\n"
  376. " ipm %0\n"
  377. " srl %0,28"
  378. : "=d" (cc), "+a" (addr) : : "cc");
  379. return cc == 0;
  380. }
  381. #endif /* CONFIG_HIBERNATION && CONFIG_DEBUG_PAGEALLOC */