pgtable.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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 <linux/slab.h>
  19. #include <asm/system.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/pgalloc.h>
  22. #include <asm/tlb.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/mmu_context.h>
  25. #ifndef CONFIG_64BIT
  26. #define ALLOC_ORDER 1
  27. #define FRAG_MASK 0x0f
  28. #else
  29. #define ALLOC_ORDER 2
  30. #define FRAG_MASK 0x03
  31. #endif
  32. unsigned long VMALLOC_START = VMALLOC_END - VMALLOC_SIZE;
  33. EXPORT_SYMBOL(VMALLOC_START);
  34. static int __init parse_vmalloc(char *arg)
  35. {
  36. if (!arg)
  37. return -EINVAL;
  38. VMALLOC_START = (VMALLOC_END - memparse(arg, &arg)) & PAGE_MASK;
  39. return 0;
  40. }
  41. early_param("vmalloc", parse_vmalloc);
  42. unsigned long *crst_table_alloc(struct mm_struct *mm)
  43. {
  44. struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  45. if (!page)
  46. return NULL;
  47. return (unsigned long *) page_to_phys(page);
  48. }
  49. void crst_table_free(struct mm_struct *mm, unsigned long *table)
  50. {
  51. free_pages((unsigned long) table, ALLOC_ORDER);
  52. }
  53. #ifdef CONFIG_64BIT
  54. int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
  55. {
  56. unsigned long *table, *pgd;
  57. unsigned long entry;
  58. BUG_ON(limit > (1UL << 53));
  59. repeat:
  60. table = crst_table_alloc(mm);
  61. if (!table)
  62. return -ENOMEM;
  63. spin_lock_bh(&mm->page_table_lock);
  64. if (mm->context.asce_limit < limit) {
  65. pgd = (unsigned long *) mm->pgd;
  66. if (mm->context.asce_limit <= (1UL << 31)) {
  67. entry = _REGION3_ENTRY_EMPTY;
  68. mm->context.asce_limit = 1UL << 42;
  69. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  70. _ASCE_USER_BITS |
  71. _ASCE_TYPE_REGION3;
  72. } else {
  73. entry = _REGION2_ENTRY_EMPTY;
  74. mm->context.asce_limit = 1UL << 53;
  75. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  76. _ASCE_USER_BITS |
  77. _ASCE_TYPE_REGION2;
  78. }
  79. crst_table_init(table, entry);
  80. pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
  81. mm->pgd = (pgd_t *) table;
  82. mm->task_size = mm->context.asce_limit;
  83. table = NULL;
  84. }
  85. spin_unlock_bh(&mm->page_table_lock);
  86. if (table)
  87. crst_table_free(mm, table);
  88. if (mm->context.asce_limit < limit)
  89. goto repeat;
  90. update_mm(mm, current);
  91. return 0;
  92. }
  93. void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
  94. {
  95. pgd_t *pgd;
  96. if (mm->context.asce_limit <= limit)
  97. return;
  98. __tlb_flush_mm(mm);
  99. while (mm->context.asce_limit > limit) {
  100. pgd = mm->pgd;
  101. switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
  102. case _REGION_ENTRY_TYPE_R2:
  103. mm->context.asce_limit = 1UL << 42;
  104. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  105. _ASCE_USER_BITS |
  106. _ASCE_TYPE_REGION3;
  107. break;
  108. case _REGION_ENTRY_TYPE_R3:
  109. mm->context.asce_limit = 1UL << 31;
  110. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  111. _ASCE_USER_BITS |
  112. _ASCE_TYPE_SEGMENT;
  113. break;
  114. default:
  115. BUG();
  116. }
  117. mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
  118. mm->task_size = mm->context.asce_limit;
  119. crst_table_free(mm, (unsigned long *) pgd);
  120. }
  121. update_mm(mm, current);
  122. }
  123. #endif
  124. #ifdef CONFIG_PGSTE
  125. /**
  126. * gmap_alloc - allocate a guest address space
  127. * @mm: pointer to the parent mm_struct
  128. *
  129. * Returns a guest address space structure.
  130. */
  131. struct gmap *gmap_alloc(struct mm_struct *mm)
  132. {
  133. struct gmap *gmap;
  134. struct page *page;
  135. unsigned long *table;
  136. gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
  137. if (!gmap)
  138. goto out;
  139. INIT_LIST_HEAD(&gmap->crst_list);
  140. gmap->mm = mm;
  141. page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  142. if (!page)
  143. goto out_free;
  144. list_add(&page->lru, &gmap->crst_list);
  145. table = (unsigned long *) page_to_phys(page);
  146. crst_table_init(table, _REGION1_ENTRY_EMPTY);
  147. gmap->table = table;
  148. gmap->asce = _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH |
  149. _ASCE_USER_BITS | __pa(table);
  150. list_add(&gmap->list, &mm->context.gmap_list);
  151. return gmap;
  152. out_free:
  153. kfree(gmap);
  154. out:
  155. return NULL;
  156. }
  157. EXPORT_SYMBOL_GPL(gmap_alloc);
  158. static int gmap_unlink_segment(struct gmap *gmap, unsigned long *table)
  159. {
  160. struct gmap_pgtable *mp;
  161. struct gmap_rmap *rmap;
  162. struct page *page;
  163. if (*table & _SEGMENT_ENTRY_INV)
  164. return 0;
  165. page = pfn_to_page(*table >> PAGE_SHIFT);
  166. mp = (struct gmap_pgtable *) page->index;
  167. list_for_each_entry(rmap, &mp->mapper, list) {
  168. if (rmap->entry != table)
  169. continue;
  170. list_del(&rmap->list);
  171. kfree(rmap);
  172. break;
  173. }
  174. *table = _SEGMENT_ENTRY_INV | _SEGMENT_ENTRY_RO | mp->vmaddr;
  175. return 1;
  176. }
  177. static void gmap_flush_tlb(struct gmap *gmap)
  178. {
  179. if (MACHINE_HAS_IDTE)
  180. __tlb_flush_idte((unsigned long) gmap->table |
  181. _ASCE_TYPE_REGION1);
  182. else
  183. __tlb_flush_global();
  184. }
  185. /**
  186. * gmap_free - free a guest address space
  187. * @gmap: pointer to the guest address space structure
  188. */
  189. void gmap_free(struct gmap *gmap)
  190. {
  191. struct page *page, *next;
  192. unsigned long *table;
  193. int i;
  194. /* Flush tlb. */
  195. if (MACHINE_HAS_IDTE)
  196. __tlb_flush_idte((unsigned long) gmap->table |
  197. _ASCE_TYPE_REGION1);
  198. else
  199. __tlb_flush_global();
  200. /* Free all segment & region tables. */
  201. down_read(&gmap->mm->mmap_sem);
  202. list_for_each_entry_safe(page, next, &gmap->crst_list, lru) {
  203. table = (unsigned long *) page_to_phys(page);
  204. if ((*table & _REGION_ENTRY_TYPE_MASK) == 0)
  205. /* Remove gmap rmap structures for segment table. */
  206. for (i = 0; i < PTRS_PER_PMD; i++, table++)
  207. gmap_unlink_segment(gmap, table);
  208. __free_pages(page, ALLOC_ORDER);
  209. }
  210. up_read(&gmap->mm->mmap_sem);
  211. list_del(&gmap->list);
  212. kfree(gmap);
  213. }
  214. EXPORT_SYMBOL_GPL(gmap_free);
  215. /**
  216. * gmap_enable - switch primary space to the guest address space
  217. * @gmap: pointer to the guest address space structure
  218. */
  219. void gmap_enable(struct gmap *gmap)
  220. {
  221. S390_lowcore.gmap = (unsigned long) gmap;
  222. }
  223. EXPORT_SYMBOL_GPL(gmap_enable);
  224. /**
  225. * gmap_disable - switch back to the standard primary address space
  226. * @gmap: pointer to the guest address space structure
  227. */
  228. void gmap_disable(struct gmap *gmap)
  229. {
  230. S390_lowcore.gmap = 0UL;
  231. }
  232. EXPORT_SYMBOL_GPL(gmap_disable);
  233. static int gmap_alloc_table(struct gmap *gmap,
  234. unsigned long *table, unsigned long init)
  235. {
  236. struct page *page;
  237. unsigned long *new;
  238. page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  239. if (!page)
  240. return -ENOMEM;
  241. new = (unsigned long *) page_to_phys(page);
  242. crst_table_init(new, init);
  243. down_read(&gmap->mm->mmap_sem);
  244. if (*table & _REGION_ENTRY_INV) {
  245. list_add(&page->lru, &gmap->crst_list);
  246. *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
  247. (*table & _REGION_ENTRY_TYPE_MASK);
  248. } else
  249. __free_pages(page, ALLOC_ORDER);
  250. up_read(&gmap->mm->mmap_sem);
  251. return 0;
  252. }
  253. /**
  254. * gmap_unmap_segment - unmap segment from the guest address space
  255. * @gmap: pointer to the guest address space structure
  256. * @addr: address in the guest address space
  257. * @len: length of the memory area to unmap
  258. *
  259. * Returns 0 if the unmap succeded, -EINVAL if not.
  260. */
  261. int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
  262. {
  263. unsigned long *table;
  264. unsigned long off;
  265. int flush;
  266. if ((to | len) & (PMD_SIZE - 1))
  267. return -EINVAL;
  268. if (len == 0 || to + len < to)
  269. return -EINVAL;
  270. flush = 0;
  271. down_read(&gmap->mm->mmap_sem);
  272. for (off = 0; off < len; off += PMD_SIZE) {
  273. /* Walk the guest addr space page table */
  274. table = gmap->table + (((to + off) >> 53) & 0x7ff);
  275. if (*table & _REGION_ENTRY_INV)
  276. goto out;
  277. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  278. table = table + (((to + off) >> 42) & 0x7ff);
  279. if (*table & _REGION_ENTRY_INV)
  280. goto out;
  281. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  282. table = table + (((to + off) >> 31) & 0x7ff);
  283. if (*table & _REGION_ENTRY_INV)
  284. goto out;
  285. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  286. table = table + (((to + off) >> 20) & 0x7ff);
  287. /* Clear segment table entry in guest address space. */
  288. flush |= gmap_unlink_segment(gmap, table);
  289. *table = _SEGMENT_ENTRY_INV;
  290. }
  291. out:
  292. up_read(&gmap->mm->mmap_sem);
  293. if (flush)
  294. gmap_flush_tlb(gmap);
  295. return 0;
  296. }
  297. EXPORT_SYMBOL_GPL(gmap_unmap_segment);
  298. /**
  299. * gmap_mmap_segment - map a segment to the guest address space
  300. * @gmap: pointer to the guest address space structure
  301. * @from: source address in the parent address space
  302. * @to: target address in the guest address space
  303. *
  304. * Returns 0 if the mmap succeded, -EINVAL or -ENOMEM if not.
  305. */
  306. int gmap_map_segment(struct gmap *gmap, unsigned long from,
  307. unsigned long to, unsigned long len)
  308. {
  309. unsigned long *table;
  310. unsigned long off;
  311. int flush;
  312. if ((from | to | len) & (PMD_SIZE - 1))
  313. return -EINVAL;
  314. if (len == 0 || from + len > PGDIR_SIZE ||
  315. from + len < from || to + len < to)
  316. return -EINVAL;
  317. flush = 0;
  318. down_read(&gmap->mm->mmap_sem);
  319. for (off = 0; off < len; off += PMD_SIZE) {
  320. /* Walk the gmap address space page table */
  321. table = gmap->table + (((to + off) >> 53) & 0x7ff);
  322. if ((*table & _REGION_ENTRY_INV) &&
  323. gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY))
  324. goto out_unmap;
  325. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  326. table = table + (((to + off) >> 42) & 0x7ff);
  327. if ((*table & _REGION_ENTRY_INV) &&
  328. gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY))
  329. goto out_unmap;
  330. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  331. table = table + (((to + off) >> 31) & 0x7ff);
  332. if ((*table & _REGION_ENTRY_INV) &&
  333. gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY))
  334. goto out_unmap;
  335. table = (unsigned long *) (*table & _REGION_ENTRY_ORIGIN);
  336. table = table + (((to + off) >> 20) & 0x7ff);
  337. /* Store 'from' address in an invalid segment table entry. */
  338. flush |= gmap_unlink_segment(gmap, table);
  339. *table = _SEGMENT_ENTRY_INV | _SEGMENT_ENTRY_RO | (from + off);
  340. }
  341. up_read(&gmap->mm->mmap_sem);
  342. if (flush)
  343. gmap_flush_tlb(gmap);
  344. return 0;
  345. out_unmap:
  346. up_read(&gmap->mm->mmap_sem);
  347. gmap_unmap_segment(gmap, to, len);
  348. return -ENOMEM;
  349. }
  350. EXPORT_SYMBOL_GPL(gmap_map_segment);
  351. unsigned long gmap_fault(unsigned long address, struct gmap *gmap)
  352. {
  353. unsigned long *table, vmaddr, segment;
  354. struct mm_struct *mm;
  355. struct gmap_pgtable *mp;
  356. struct gmap_rmap *rmap;
  357. struct vm_area_struct *vma;
  358. struct page *page;
  359. pgd_t *pgd;
  360. pud_t *pud;
  361. pmd_t *pmd;
  362. current->thread.gmap_addr = address;
  363. mm = gmap->mm;
  364. /* Walk the gmap address space page table */
  365. table = gmap->table + ((address >> 53) & 0x7ff);
  366. if (unlikely(*table & _REGION_ENTRY_INV))
  367. return -EFAULT;
  368. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  369. table = table + ((address >> 42) & 0x7ff);
  370. if (unlikely(*table & _REGION_ENTRY_INV))
  371. return -EFAULT;
  372. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  373. table = table + ((address >> 31) & 0x7ff);
  374. if (unlikely(*table & _REGION_ENTRY_INV))
  375. return -EFAULT;
  376. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  377. table = table + ((address >> 20) & 0x7ff);
  378. /* Convert the gmap address to an mm address. */
  379. segment = *table;
  380. if (likely(!(segment & _SEGMENT_ENTRY_INV))) {
  381. page = pfn_to_page(segment >> PAGE_SHIFT);
  382. mp = (struct gmap_pgtable *) page->index;
  383. return mp->vmaddr | (address & ~PMD_MASK);
  384. } else if (segment & _SEGMENT_ENTRY_RO) {
  385. vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
  386. vma = find_vma(mm, vmaddr);
  387. if (!vma || vma->vm_start > vmaddr)
  388. return -EFAULT;
  389. /* Walk the parent mm page table */
  390. pgd = pgd_offset(mm, vmaddr);
  391. pud = pud_alloc(mm, pgd, vmaddr);
  392. if (!pud)
  393. return -ENOMEM;
  394. pmd = pmd_alloc(mm, pud, vmaddr);
  395. if (!pmd)
  396. return -ENOMEM;
  397. if (!pmd_present(*pmd) &&
  398. __pte_alloc(mm, vma, pmd, vmaddr))
  399. return -ENOMEM;
  400. /* pmd now points to a valid segment table entry. */
  401. rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT);
  402. if (!rmap)
  403. return -ENOMEM;
  404. /* Link gmap segment table entry location to page table. */
  405. page = pmd_page(*pmd);
  406. mp = (struct gmap_pgtable *) page->index;
  407. rmap->entry = table;
  408. list_add(&rmap->list, &mp->mapper);
  409. /* Set gmap segment table entry to page table. */
  410. *table = pmd_val(*pmd) & PAGE_MASK;
  411. return vmaddr | (address & ~PMD_MASK);
  412. }
  413. return -EFAULT;
  414. }
  415. EXPORT_SYMBOL_GPL(gmap_fault);
  416. void gmap_unmap_notifier(struct mm_struct *mm, unsigned long *table)
  417. {
  418. struct gmap_rmap *rmap, *next;
  419. struct gmap_pgtable *mp;
  420. struct page *page;
  421. int flush;
  422. flush = 0;
  423. spin_lock(&mm->page_table_lock);
  424. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  425. mp = (struct gmap_pgtable *) page->index;
  426. list_for_each_entry_safe(rmap, next, &mp->mapper, list) {
  427. *rmap->entry =
  428. _SEGMENT_ENTRY_INV | _SEGMENT_ENTRY_RO | mp->vmaddr;
  429. list_del(&rmap->list);
  430. kfree(rmap);
  431. flush = 1;
  432. }
  433. spin_unlock(&mm->page_table_lock);
  434. if (flush)
  435. __tlb_flush_global();
  436. }
  437. static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
  438. unsigned long vmaddr)
  439. {
  440. struct page *page;
  441. unsigned long *table;
  442. struct gmap_pgtable *mp;
  443. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  444. if (!page)
  445. return NULL;
  446. mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT);
  447. if (!mp) {
  448. __free_page(page);
  449. return NULL;
  450. }
  451. pgtable_page_ctor(page);
  452. mp->vmaddr = vmaddr & PMD_MASK;
  453. INIT_LIST_HEAD(&mp->mapper);
  454. page->index = (unsigned long) mp;
  455. atomic_set(&page->_mapcount, 3);
  456. table = (unsigned long *) page_to_phys(page);
  457. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/2);
  458. clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
  459. return table;
  460. }
  461. static inline void page_table_free_pgste(unsigned long *table)
  462. {
  463. struct page *page;
  464. struct gmap_pgtable *mp;
  465. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  466. mp = (struct gmap_pgtable *) page->index;
  467. BUG_ON(!list_empty(&mp->mapper));
  468. pgtable_page_ctor(page);
  469. atomic_set(&page->_mapcount, -1);
  470. kfree(mp);
  471. __free_page(page);
  472. }
  473. #else /* CONFIG_PGSTE */
  474. static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
  475. unsigned long vmaddr)
  476. {
  477. return NULL;
  478. }
  479. static inline void page_table_free_pgste(unsigned long *table)
  480. {
  481. }
  482. static inline void gmap_unmap_notifier(struct mm_struct *mm,
  483. unsigned long *table)
  484. {
  485. }
  486. #endif /* CONFIG_PGSTE */
  487. static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
  488. {
  489. unsigned int old, new;
  490. do {
  491. old = atomic_read(v);
  492. new = old ^ bits;
  493. } while (atomic_cmpxchg(v, old, new) != old);
  494. return new;
  495. }
  496. /*
  497. * page table entry allocation/free routines.
  498. */
  499. unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr)
  500. {
  501. struct page *page;
  502. unsigned long *table;
  503. unsigned int mask, bit;
  504. if (mm_has_pgste(mm))
  505. return page_table_alloc_pgste(mm, vmaddr);
  506. /* Allocate fragments of a 4K page as 1K/2K page table */
  507. spin_lock_bh(&mm->context.list_lock);
  508. mask = FRAG_MASK;
  509. if (!list_empty(&mm->context.pgtable_list)) {
  510. page = list_first_entry(&mm->context.pgtable_list,
  511. struct page, lru);
  512. table = (unsigned long *) page_to_phys(page);
  513. mask = atomic_read(&page->_mapcount);
  514. mask = mask | (mask >> 4);
  515. }
  516. if ((mask & FRAG_MASK) == FRAG_MASK) {
  517. spin_unlock_bh(&mm->context.list_lock);
  518. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  519. if (!page)
  520. return NULL;
  521. pgtable_page_ctor(page);
  522. atomic_set(&page->_mapcount, 1);
  523. table = (unsigned long *) page_to_phys(page);
  524. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE);
  525. spin_lock_bh(&mm->context.list_lock);
  526. list_add(&page->lru, &mm->context.pgtable_list);
  527. } else {
  528. for (bit = 1; mask & bit; bit <<= 1)
  529. table += PTRS_PER_PTE;
  530. mask = atomic_xor_bits(&page->_mapcount, bit);
  531. if ((mask & FRAG_MASK) == FRAG_MASK)
  532. list_del(&page->lru);
  533. }
  534. spin_unlock_bh(&mm->context.list_lock);
  535. return table;
  536. }
  537. void page_table_free(struct mm_struct *mm, unsigned long *table)
  538. {
  539. struct page *page;
  540. unsigned int bit, mask;
  541. if (mm_has_pgste(mm)) {
  542. gmap_unmap_notifier(mm, table);
  543. return page_table_free_pgste(table);
  544. }
  545. /* Free 1K/2K page table fragment of a 4K page */
  546. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  547. bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
  548. spin_lock_bh(&mm->context.list_lock);
  549. if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
  550. list_del(&page->lru);
  551. mask = atomic_xor_bits(&page->_mapcount, bit);
  552. if (mask & FRAG_MASK)
  553. list_add(&page->lru, &mm->context.pgtable_list);
  554. spin_unlock_bh(&mm->context.list_lock);
  555. if (mask == 0) {
  556. pgtable_page_dtor(page);
  557. atomic_set(&page->_mapcount, -1);
  558. __free_page(page);
  559. }
  560. }
  561. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  562. static void __page_table_free_rcu(void *table, unsigned bit)
  563. {
  564. struct page *page;
  565. if (bit == FRAG_MASK)
  566. return page_table_free_pgste(table);
  567. /* Free 1K/2K page table fragment of a 4K page */
  568. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  569. if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
  570. pgtable_page_dtor(page);
  571. atomic_set(&page->_mapcount, -1);
  572. __free_page(page);
  573. }
  574. }
  575. void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table)
  576. {
  577. struct mm_struct *mm;
  578. struct page *page;
  579. unsigned int bit, mask;
  580. mm = tlb->mm;
  581. if (mm_has_pgste(mm)) {
  582. gmap_unmap_notifier(mm, table);
  583. table = (unsigned long *) (__pa(table) | FRAG_MASK);
  584. tlb_remove_table(tlb, table);
  585. return;
  586. }
  587. bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
  588. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  589. spin_lock_bh(&mm->context.list_lock);
  590. if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
  591. list_del(&page->lru);
  592. mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
  593. if (mask & FRAG_MASK)
  594. list_add_tail(&page->lru, &mm->context.pgtable_list);
  595. spin_unlock_bh(&mm->context.list_lock);
  596. table = (unsigned long *) (__pa(table) | (bit << 4));
  597. tlb_remove_table(tlb, table);
  598. }
  599. void __tlb_remove_table(void *_table)
  600. {
  601. void *table = (void *)((unsigned long) _table & PAGE_MASK);
  602. unsigned type = (unsigned long) _table & ~PAGE_MASK;
  603. if (type)
  604. __page_table_free_rcu(table, type);
  605. else
  606. free_pages((unsigned long) table, ALLOC_ORDER);
  607. }
  608. #endif
  609. /*
  610. * switch on pgstes for its userspace process (for kvm)
  611. */
  612. int s390_enable_sie(void)
  613. {
  614. struct task_struct *tsk = current;
  615. struct mm_struct *mm, *old_mm;
  616. /* Do we have switched amode? If no, we cannot do sie */
  617. if (user_mode == HOME_SPACE_MODE)
  618. return -EINVAL;
  619. /* Do we have pgstes? if yes, we are done */
  620. if (mm_has_pgste(tsk->mm))
  621. return 0;
  622. /* lets check if we are allowed to replace the mm */
  623. task_lock(tsk);
  624. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  625. #ifdef CONFIG_AIO
  626. !hlist_empty(&tsk->mm->ioctx_list) ||
  627. #endif
  628. tsk->mm != tsk->active_mm) {
  629. task_unlock(tsk);
  630. return -EINVAL;
  631. }
  632. task_unlock(tsk);
  633. /* we copy the mm and let dup_mm create the page tables with_pgstes */
  634. tsk->mm->context.alloc_pgste = 1;
  635. mm = dup_mm(tsk);
  636. tsk->mm->context.alloc_pgste = 0;
  637. if (!mm)
  638. return -ENOMEM;
  639. /* Now lets check again if something happened */
  640. task_lock(tsk);
  641. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  642. #ifdef CONFIG_AIO
  643. !hlist_empty(&tsk->mm->ioctx_list) ||
  644. #endif
  645. tsk->mm != tsk->active_mm) {
  646. mmput(mm);
  647. task_unlock(tsk);
  648. return -EINVAL;
  649. }
  650. /* ok, we are alone. No ptrace, no threads, etc. */
  651. old_mm = tsk->mm;
  652. tsk->mm = tsk->active_mm = mm;
  653. preempt_disable();
  654. update_mm(mm, tsk);
  655. atomic_inc(&mm->context.attach_count);
  656. atomic_dec(&old_mm->context.attach_count);
  657. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  658. preempt_enable();
  659. task_unlock(tsk);
  660. mmput(old_mm);
  661. return 0;
  662. }
  663. EXPORT_SYMBOL_GPL(s390_enable_sie);
  664. #if defined(CONFIG_DEBUG_PAGEALLOC) && defined(CONFIG_HIBERNATION)
  665. bool kernel_page_present(struct page *page)
  666. {
  667. unsigned long addr;
  668. int cc;
  669. addr = page_to_phys(page);
  670. asm volatile(
  671. " lra %1,0(%1)\n"
  672. " ipm %0\n"
  673. " srl %0,28"
  674. : "=d" (cc), "+a" (addr) : : "cc");
  675. return cc == 0;
  676. }
  677. #endif /* CONFIG_HIBERNATION && CONFIG_DEBUG_PAGEALLOC */