pgtable.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. static unsigned long *gmap_table_walk(unsigned long address, struct gmap *gmap)
  347. {
  348. unsigned long *table;
  349. table = gmap->table + ((address >> 53) & 0x7ff);
  350. if (unlikely(*table & _REGION_ENTRY_INV))
  351. return ERR_PTR(-EFAULT);
  352. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  353. table = table + ((address >> 42) & 0x7ff);
  354. if (unlikely(*table & _REGION_ENTRY_INV))
  355. return ERR_PTR(-EFAULT);
  356. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  357. table = table + ((address >> 31) & 0x7ff);
  358. if (unlikely(*table & _REGION_ENTRY_INV))
  359. return ERR_PTR(-EFAULT);
  360. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  361. table = table + ((address >> 20) & 0x7ff);
  362. return table;
  363. }
  364. /**
  365. * __gmap_translate - translate a guest address to a user space address
  366. * @address: guest address
  367. * @gmap: pointer to guest mapping meta data structure
  368. *
  369. * Returns user space address which corresponds to the guest address or
  370. * -EFAULT if no such mapping exists.
  371. * This function does not establish potentially missing page table entries.
  372. * The mmap_sem of the mm that belongs to the address space must be held
  373. * when this function gets called.
  374. */
  375. unsigned long __gmap_translate(unsigned long address, struct gmap *gmap)
  376. {
  377. unsigned long *segment_ptr, vmaddr, segment;
  378. struct gmap_pgtable *mp;
  379. struct page *page;
  380. current->thread.gmap_addr = address;
  381. segment_ptr = gmap_table_walk(address, gmap);
  382. if (IS_ERR(segment_ptr))
  383. return PTR_ERR(segment_ptr);
  384. /* Convert the gmap address to an mm address. */
  385. segment = *segment_ptr;
  386. if (!(segment & _SEGMENT_ENTRY_INV)) {
  387. page = pfn_to_page(segment >> PAGE_SHIFT);
  388. mp = (struct gmap_pgtable *) page->index;
  389. return mp->vmaddr | (address & ~PMD_MASK);
  390. } else if (segment & _SEGMENT_ENTRY_RO) {
  391. vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
  392. return vmaddr | (address & ~PMD_MASK);
  393. }
  394. return -EFAULT;
  395. }
  396. EXPORT_SYMBOL_GPL(__gmap_translate);
  397. /**
  398. * gmap_translate - translate a guest address to a user space address
  399. * @address: guest address
  400. * @gmap: pointer to guest mapping meta data structure
  401. *
  402. * Returns user space address which corresponds to the guest address or
  403. * -EFAULT if no such mapping exists.
  404. * This function does not establish potentially missing page table entries.
  405. */
  406. unsigned long gmap_translate(unsigned long address, struct gmap *gmap)
  407. {
  408. unsigned long rc;
  409. down_read(&gmap->mm->mmap_sem);
  410. rc = __gmap_translate(address, gmap);
  411. up_read(&gmap->mm->mmap_sem);
  412. return rc;
  413. }
  414. EXPORT_SYMBOL_GPL(gmap_translate);
  415. /*
  416. * this function is assumed to be called with mmap_sem held
  417. */
  418. unsigned long __gmap_fault(unsigned long address, struct gmap *gmap)
  419. {
  420. unsigned long *segment_ptr, vmaddr, segment;
  421. struct vm_area_struct *vma;
  422. struct gmap_pgtable *mp;
  423. struct gmap_rmap *rmap;
  424. struct mm_struct *mm;
  425. struct page *page;
  426. pgd_t *pgd;
  427. pud_t *pud;
  428. pmd_t *pmd;
  429. current->thread.gmap_addr = address;
  430. segment_ptr = gmap_table_walk(address, gmap);
  431. if (IS_ERR(segment_ptr))
  432. return -EFAULT;
  433. /* Convert the gmap address to an mm address. */
  434. segment = *segment_ptr;
  435. if (!(segment & _SEGMENT_ENTRY_INV)) {
  436. page = pfn_to_page(segment >> PAGE_SHIFT);
  437. mp = (struct gmap_pgtable *) page->index;
  438. return mp->vmaddr | (address & ~PMD_MASK);
  439. } else if (segment & _SEGMENT_ENTRY_RO) {
  440. mm = gmap->mm;
  441. vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
  442. vma = find_vma(mm, vmaddr);
  443. if (!vma || vma->vm_start > vmaddr)
  444. return -EFAULT;
  445. /* Walk the parent mm page table */
  446. pgd = pgd_offset(mm, vmaddr);
  447. pud = pud_alloc(mm, pgd, vmaddr);
  448. if (!pud)
  449. return -ENOMEM;
  450. pmd = pmd_alloc(mm, pud, vmaddr);
  451. if (!pmd)
  452. return -ENOMEM;
  453. if (!pmd_present(*pmd) &&
  454. __pte_alloc(mm, vma, pmd, vmaddr))
  455. return -ENOMEM;
  456. /* pmd now points to a valid segment table entry. */
  457. rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT);
  458. if (!rmap)
  459. return -ENOMEM;
  460. /* Link gmap segment table entry location to page table. */
  461. page = pmd_page(*pmd);
  462. mp = (struct gmap_pgtable *) page->index;
  463. rmap->entry = segment_ptr;
  464. spin_lock(&mm->page_table_lock);
  465. list_add(&rmap->list, &mp->mapper);
  466. spin_unlock(&mm->page_table_lock);
  467. /* Set gmap segment table entry to page table. */
  468. *segment_ptr = pmd_val(*pmd) & PAGE_MASK;
  469. return vmaddr | (address & ~PMD_MASK);
  470. }
  471. return -EFAULT;
  472. }
  473. unsigned long gmap_fault(unsigned long address, struct gmap *gmap)
  474. {
  475. unsigned long rc;
  476. down_read(&gmap->mm->mmap_sem);
  477. rc = __gmap_fault(address, gmap);
  478. up_read(&gmap->mm->mmap_sem);
  479. return rc;
  480. }
  481. EXPORT_SYMBOL_GPL(gmap_fault);
  482. void gmap_discard(unsigned long from, unsigned long to, struct gmap *gmap)
  483. {
  484. unsigned long *table, address, size;
  485. struct vm_area_struct *vma;
  486. struct gmap_pgtable *mp;
  487. struct page *page;
  488. down_read(&gmap->mm->mmap_sem);
  489. address = from;
  490. while (address < to) {
  491. /* Walk the gmap address space page table */
  492. table = gmap->table + ((address >> 53) & 0x7ff);
  493. if (unlikely(*table & _REGION_ENTRY_INV)) {
  494. address = (address + PMD_SIZE) & PMD_MASK;
  495. continue;
  496. }
  497. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  498. table = table + ((address >> 42) & 0x7ff);
  499. if (unlikely(*table & _REGION_ENTRY_INV)) {
  500. address = (address + PMD_SIZE) & PMD_MASK;
  501. continue;
  502. }
  503. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  504. table = table + ((address >> 31) & 0x7ff);
  505. if (unlikely(*table & _REGION_ENTRY_INV)) {
  506. address = (address + PMD_SIZE) & PMD_MASK;
  507. continue;
  508. }
  509. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  510. table = table + ((address >> 20) & 0x7ff);
  511. if (unlikely(*table & _SEGMENT_ENTRY_INV)) {
  512. address = (address + PMD_SIZE) & PMD_MASK;
  513. continue;
  514. }
  515. page = pfn_to_page(*table >> PAGE_SHIFT);
  516. mp = (struct gmap_pgtable *) page->index;
  517. vma = find_vma(gmap->mm, mp->vmaddr);
  518. size = min(to - address, PMD_SIZE - (address & ~PMD_MASK));
  519. zap_page_range(vma, mp->vmaddr | (address & ~PMD_MASK),
  520. size, NULL);
  521. address = (address + PMD_SIZE) & PMD_MASK;
  522. }
  523. up_read(&gmap->mm->mmap_sem);
  524. }
  525. EXPORT_SYMBOL_GPL(gmap_discard);
  526. void gmap_unmap_notifier(struct mm_struct *mm, unsigned long *table)
  527. {
  528. struct gmap_rmap *rmap, *next;
  529. struct gmap_pgtable *mp;
  530. struct page *page;
  531. int flush;
  532. flush = 0;
  533. spin_lock(&mm->page_table_lock);
  534. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  535. mp = (struct gmap_pgtable *) page->index;
  536. list_for_each_entry_safe(rmap, next, &mp->mapper, list) {
  537. *rmap->entry =
  538. _SEGMENT_ENTRY_INV | _SEGMENT_ENTRY_RO | mp->vmaddr;
  539. list_del(&rmap->list);
  540. kfree(rmap);
  541. flush = 1;
  542. }
  543. spin_unlock(&mm->page_table_lock);
  544. if (flush)
  545. __tlb_flush_global();
  546. }
  547. static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
  548. unsigned long vmaddr)
  549. {
  550. struct page *page;
  551. unsigned long *table;
  552. struct gmap_pgtable *mp;
  553. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  554. if (!page)
  555. return NULL;
  556. mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT);
  557. if (!mp) {
  558. __free_page(page);
  559. return NULL;
  560. }
  561. pgtable_page_ctor(page);
  562. mp->vmaddr = vmaddr & PMD_MASK;
  563. INIT_LIST_HEAD(&mp->mapper);
  564. page->index = (unsigned long) mp;
  565. atomic_set(&page->_mapcount, 3);
  566. table = (unsigned long *) page_to_phys(page);
  567. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE/2);
  568. clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
  569. return table;
  570. }
  571. static inline void page_table_free_pgste(unsigned long *table)
  572. {
  573. struct page *page;
  574. struct gmap_pgtable *mp;
  575. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  576. mp = (struct gmap_pgtable *) page->index;
  577. BUG_ON(!list_empty(&mp->mapper));
  578. pgtable_page_dtor(page);
  579. atomic_set(&page->_mapcount, -1);
  580. kfree(mp);
  581. __free_page(page);
  582. }
  583. #else /* CONFIG_PGSTE */
  584. static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
  585. unsigned long vmaddr)
  586. {
  587. return NULL;
  588. }
  589. static inline void page_table_free_pgste(unsigned long *table)
  590. {
  591. }
  592. static inline void gmap_unmap_notifier(struct mm_struct *mm,
  593. unsigned long *table)
  594. {
  595. }
  596. #endif /* CONFIG_PGSTE */
  597. static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
  598. {
  599. unsigned int old, new;
  600. do {
  601. old = atomic_read(v);
  602. new = old ^ bits;
  603. } while (atomic_cmpxchg(v, old, new) != old);
  604. return new;
  605. }
  606. /*
  607. * page table entry allocation/free routines.
  608. */
  609. unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr)
  610. {
  611. unsigned long *uninitialized_var(table);
  612. struct page *uninitialized_var(page);
  613. unsigned int mask, bit;
  614. if (mm_has_pgste(mm))
  615. return page_table_alloc_pgste(mm, vmaddr);
  616. /* Allocate fragments of a 4K page as 1K/2K page table */
  617. spin_lock_bh(&mm->context.list_lock);
  618. mask = FRAG_MASK;
  619. if (!list_empty(&mm->context.pgtable_list)) {
  620. page = list_first_entry(&mm->context.pgtable_list,
  621. struct page, lru);
  622. table = (unsigned long *) page_to_phys(page);
  623. mask = atomic_read(&page->_mapcount);
  624. mask = mask | (mask >> 4);
  625. }
  626. if ((mask & FRAG_MASK) == FRAG_MASK) {
  627. spin_unlock_bh(&mm->context.list_lock);
  628. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  629. if (!page)
  630. return NULL;
  631. pgtable_page_ctor(page);
  632. atomic_set(&page->_mapcount, 1);
  633. table = (unsigned long *) page_to_phys(page);
  634. clear_table(table, _PAGE_TYPE_EMPTY, PAGE_SIZE);
  635. spin_lock_bh(&mm->context.list_lock);
  636. list_add(&page->lru, &mm->context.pgtable_list);
  637. } else {
  638. for (bit = 1; mask & bit; bit <<= 1)
  639. table += PTRS_PER_PTE;
  640. mask = atomic_xor_bits(&page->_mapcount, bit);
  641. if ((mask & FRAG_MASK) == FRAG_MASK)
  642. list_del(&page->lru);
  643. }
  644. spin_unlock_bh(&mm->context.list_lock);
  645. return table;
  646. }
  647. void page_table_free(struct mm_struct *mm, unsigned long *table)
  648. {
  649. struct page *page;
  650. unsigned int bit, mask;
  651. if (mm_has_pgste(mm)) {
  652. gmap_unmap_notifier(mm, table);
  653. return page_table_free_pgste(table);
  654. }
  655. /* Free 1K/2K page table fragment of a 4K page */
  656. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  657. bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
  658. spin_lock_bh(&mm->context.list_lock);
  659. if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
  660. list_del(&page->lru);
  661. mask = atomic_xor_bits(&page->_mapcount, bit);
  662. if (mask & FRAG_MASK)
  663. list_add(&page->lru, &mm->context.pgtable_list);
  664. spin_unlock_bh(&mm->context.list_lock);
  665. if (mask == 0) {
  666. pgtable_page_dtor(page);
  667. atomic_set(&page->_mapcount, -1);
  668. __free_page(page);
  669. }
  670. }
  671. static void __page_table_free_rcu(void *table, unsigned bit)
  672. {
  673. struct page *page;
  674. if (bit == FRAG_MASK)
  675. return page_table_free_pgste(table);
  676. /* Free 1K/2K page table fragment of a 4K page */
  677. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  678. if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
  679. pgtable_page_dtor(page);
  680. atomic_set(&page->_mapcount, -1);
  681. __free_page(page);
  682. }
  683. }
  684. void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table)
  685. {
  686. struct mm_struct *mm;
  687. struct page *page;
  688. unsigned int bit, mask;
  689. mm = tlb->mm;
  690. if (mm_has_pgste(mm)) {
  691. gmap_unmap_notifier(mm, table);
  692. table = (unsigned long *) (__pa(table) | FRAG_MASK);
  693. tlb_remove_table(tlb, table);
  694. return;
  695. }
  696. bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
  697. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  698. spin_lock_bh(&mm->context.list_lock);
  699. if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
  700. list_del(&page->lru);
  701. mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
  702. if (mask & FRAG_MASK)
  703. list_add_tail(&page->lru, &mm->context.pgtable_list);
  704. spin_unlock_bh(&mm->context.list_lock);
  705. table = (unsigned long *) (__pa(table) | (bit << 4));
  706. tlb_remove_table(tlb, table);
  707. }
  708. void __tlb_remove_table(void *_table)
  709. {
  710. const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
  711. void *table = (void *)((unsigned long) _table & ~mask);
  712. unsigned type = (unsigned long) _table & mask;
  713. if (type)
  714. __page_table_free_rcu(table, type);
  715. else
  716. free_pages((unsigned long) table, ALLOC_ORDER);
  717. }
  718. static void tlb_remove_table_smp_sync(void *arg)
  719. {
  720. /* Simply deliver the interrupt */
  721. }
  722. static void tlb_remove_table_one(void *table)
  723. {
  724. /*
  725. * This isn't an RCU grace period and hence the page-tables cannot be
  726. * assumed to be actually RCU-freed.
  727. *
  728. * It is however sufficient for software page-table walkers that rely
  729. * on IRQ disabling. See the comment near struct mmu_table_batch.
  730. */
  731. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  732. __tlb_remove_table(table);
  733. }
  734. static void tlb_remove_table_rcu(struct rcu_head *head)
  735. {
  736. struct mmu_table_batch *batch;
  737. int i;
  738. batch = container_of(head, struct mmu_table_batch, rcu);
  739. for (i = 0; i < batch->nr; i++)
  740. __tlb_remove_table(batch->tables[i]);
  741. free_page((unsigned long)batch);
  742. }
  743. void tlb_table_flush(struct mmu_gather *tlb)
  744. {
  745. struct mmu_table_batch **batch = &tlb->batch;
  746. if (*batch) {
  747. __tlb_flush_mm(tlb->mm);
  748. call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
  749. *batch = NULL;
  750. }
  751. }
  752. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  753. {
  754. struct mmu_table_batch **batch = &tlb->batch;
  755. if (*batch == NULL) {
  756. *batch = (struct mmu_table_batch *)
  757. __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  758. if (*batch == NULL) {
  759. __tlb_flush_mm(tlb->mm);
  760. tlb_remove_table_one(table);
  761. return;
  762. }
  763. (*batch)->nr = 0;
  764. }
  765. (*batch)->tables[(*batch)->nr++] = table;
  766. if ((*batch)->nr == MAX_TABLE_BATCH)
  767. tlb_table_flush(tlb);
  768. }
  769. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  770. void thp_split_vma(struct vm_area_struct *vma)
  771. {
  772. unsigned long addr;
  773. struct page *page;
  774. for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE) {
  775. page = follow_page(vma, addr, FOLL_SPLIT);
  776. }
  777. }
  778. void thp_split_mm(struct mm_struct *mm)
  779. {
  780. struct vm_area_struct *vma = mm->mmap;
  781. while (vma != NULL) {
  782. thp_split_vma(vma);
  783. vma->vm_flags &= ~VM_HUGEPAGE;
  784. vma->vm_flags |= VM_NOHUGEPAGE;
  785. vma = vma->vm_next;
  786. }
  787. }
  788. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  789. /*
  790. * switch on pgstes for its userspace process (for kvm)
  791. */
  792. int s390_enable_sie(void)
  793. {
  794. struct task_struct *tsk = current;
  795. struct mm_struct *mm, *old_mm;
  796. /* Do we have switched amode? If no, we cannot do sie */
  797. if (s390_user_mode == HOME_SPACE_MODE)
  798. return -EINVAL;
  799. /* Do we have pgstes? if yes, we are done */
  800. if (mm_has_pgste(tsk->mm))
  801. return 0;
  802. /* lets check if we are allowed to replace the mm */
  803. task_lock(tsk);
  804. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  805. #ifdef CONFIG_AIO
  806. !hlist_empty(&tsk->mm->ioctx_list) ||
  807. #endif
  808. tsk->mm != tsk->active_mm) {
  809. task_unlock(tsk);
  810. return -EINVAL;
  811. }
  812. task_unlock(tsk);
  813. /* we copy the mm and let dup_mm create the page tables with_pgstes */
  814. tsk->mm->context.alloc_pgste = 1;
  815. /* make sure that both mms have a correct rss state */
  816. sync_mm_rss(tsk->mm);
  817. mm = dup_mm(tsk);
  818. tsk->mm->context.alloc_pgste = 0;
  819. if (!mm)
  820. return -ENOMEM;
  821. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  822. /* split thp mappings and disable thp for future mappings */
  823. thp_split_mm(mm);
  824. mm->def_flags |= VM_NOHUGEPAGE;
  825. #endif
  826. /* Now lets check again if something happened */
  827. task_lock(tsk);
  828. if (!tsk->mm || atomic_read(&tsk->mm->mm_users) > 1 ||
  829. #ifdef CONFIG_AIO
  830. !hlist_empty(&tsk->mm->ioctx_list) ||
  831. #endif
  832. tsk->mm != tsk->active_mm) {
  833. mmput(mm);
  834. task_unlock(tsk);
  835. return -EINVAL;
  836. }
  837. /* ok, we are alone. No ptrace, no threads, etc. */
  838. old_mm = tsk->mm;
  839. tsk->mm = tsk->active_mm = mm;
  840. preempt_disable();
  841. update_mm(mm, tsk);
  842. atomic_inc(&mm->context.attach_count);
  843. atomic_dec(&old_mm->context.attach_count);
  844. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  845. preempt_enable();
  846. task_unlock(tsk);
  847. mmput(old_mm);
  848. return 0;
  849. }
  850. EXPORT_SYMBOL_GPL(s390_enable_sie);
  851. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  852. int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
  853. pmd_t *pmdp)
  854. {
  855. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  856. /* No need to flush TLB
  857. * On s390 reference bits are in storage key and never in TLB */
  858. return pmdp_test_and_clear_young(vma, address, pmdp);
  859. }
  860. int pmdp_set_access_flags(struct vm_area_struct *vma,
  861. unsigned long address, pmd_t *pmdp,
  862. pmd_t entry, int dirty)
  863. {
  864. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  865. if (pmd_same(*pmdp, entry))
  866. return 0;
  867. pmdp_invalidate(vma, address, pmdp);
  868. set_pmd_at(vma->vm_mm, address, pmdp, entry);
  869. return 1;
  870. }
  871. static void pmdp_splitting_flush_sync(void *arg)
  872. {
  873. /* Simply deliver the interrupt */
  874. }
  875. void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
  876. pmd_t *pmdp)
  877. {
  878. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  879. if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
  880. (unsigned long *) pmdp)) {
  881. /* need to serialize against gup-fast (IRQ disabled) */
  882. smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
  883. }
  884. }
  885. void pgtable_trans_huge_deposit(struct mm_struct *mm, pgtable_t pgtable)
  886. {
  887. struct list_head *lh = (struct list_head *) pgtable;
  888. assert_spin_locked(&mm->page_table_lock);
  889. /* FIFO */
  890. if (!mm->pmd_huge_pte)
  891. INIT_LIST_HEAD(lh);
  892. else
  893. list_add(lh, (struct list_head *) mm->pmd_huge_pte);
  894. mm->pmd_huge_pte = pgtable;
  895. }
  896. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm)
  897. {
  898. struct list_head *lh;
  899. pgtable_t pgtable;
  900. pte_t *ptep;
  901. assert_spin_locked(&mm->page_table_lock);
  902. /* FIFO */
  903. pgtable = mm->pmd_huge_pte;
  904. lh = (struct list_head *) pgtable;
  905. if (list_empty(lh))
  906. mm->pmd_huge_pte = NULL;
  907. else {
  908. mm->pmd_huge_pte = (pgtable_t) lh->next;
  909. list_del(lh);
  910. }
  911. ptep = (pte_t *) pgtable;
  912. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  913. ptep++;
  914. pte_val(*ptep) = _PAGE_TYPE_EMPTY;
  915. return pgtable;
  916. }
  917. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */