pgtable.c 23 KB

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