pgtable.c 11 KB

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