pgtable.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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_INVALID)
  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 = mp->vmaddr | _SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_PROTECT;
  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. __releases(&gmap->mm->page_table_lock)
  225. __acquires(&gmap->mm->page_table_lock)
  226. {
  227. struct page *page;
  228. unsigned long *new;
  229. /* since we dont free the gmap table until gmap_free we can unlock */
  230. spin_unlock(&gmap->mm->page_table_lock);
  231. page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  232. spin_lock(&gmap->mm->page_table_lock);
  233. if (!page)
  234. return -ENOMEM;
  235. new = (unsigned long *) page_to_phys(page);
  236. crst_table_init(new, init);
  237. if (*table & _REGION_ENTRY_INVALID) {
  238. list_add(&page->lru, &gmap->crst_list);
  239. *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
  240. (*table & _REGION_ENTRY_TYPE_MASK);
  241. } else
  242. __free_pages(page, ALLOC_ORDER);
  243. return 0;
  244. }
  245. /**
  246. * gmap_unmap_segment - unmap segment from the guest address space
  247. * @gmap: pointer to the guest address space structure
  248. * @addr: address in the guest address space
  249. * @len: length of the memory area to unmap
  250. *
  251. * Returns 0 if the unmap succeded, -EINVAL if not.
  252. */
  253. int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
  254. {
  255. unsigned long *table;
  256. unsigned long off;
  257. int flush;
  258. if ((to | len) & (PMD_SIZE - 1))
  259. return -EINVAL;
  260. if (len == 0 || to + len < to)
  261. return -EINVAL;
  262. flush = 0;
  263. down_read(&gmap->mm->mmap_sem);
  264. spin_lock(&gmap->mm->page_table_lock);
  265. for (off = 0; off < len; off += PMD_SIZE) {
  266. /* Walk the guest addr space page table */
  267. table = gmap->table + (((to + off) >> 53) & 0x7ff);
  268. if (*table & _REGION_ENTRY_INVALID)
  269. goto out;
  270. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  271. table = table + (((to + off) >> 42) & 0x7ff);
  272. if (*table & _REGION_ENTRY_INVALID)
  273. goto out;
  274. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  275. table = table + (((to + off) >> 31) & 0x7ff);
  276. if (*table & _REGION_ENTRY_INVALID)
  277. goto out;
  278. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  279. table = table + (((to + off) >> 20) & 0x7ff);
  280. /* Clear segment table entry in guest address space. */
  281. flush |= gmap_unlink_segment(gmap, table);
  282. *table = _SEGMENT_ENTRY_INVALID;
  283. }
  284. out:
  285. spin_unlock(&gmap->mm->page_table_lock);
  286. up_read(&gmap->mm->mmap_sem);
  287. if (flush)
  288. gmap_flush_tlb(gmap);
  289. return 0;
  290. }
  291. EXPORT_SYMBOL_GPL(gmap_unmap_segment);
  292. /**
  293. * gmap_mmap_segment - map a segment to the guest address space
  294. * @gmap: pointer to the guest address space structure
  295. * @from: source address in the parent address space
  296. * @to: target address in the guest address space
  297. *
  298. * Returns 0 if the mmap succeded, -EINVAL or -ENOMEM if not.
  299. */
  300. int gmap_map_segment(struct gmap *gmap, unsigned long from,
  301. unsigned long to, unsigned long len)
  302. {
  303. unsigned long *table;
  304. unsigned long off;
  305. int flush;
  306. if ((from | to | len) & (PMD_SIZE - 1))
  307. return -EINVAL;
  308. if (len == 0 || from + len > TASK_MAX_SIZE ||
  309. from + len < from || to + len < to)
  310. return -EINVAL;
  311. flush = 0;
  312. down_read(&gmap->mm->mmap_sem);
  313. spin_lock(&gmap->mm->page_table_lock);
  314. for (off = 0; off < len; off += PMD_SIZE) {
  315. /* Walk the gmap address space page table */
  316. table = gmap->table + (((to + off) >> 53) & 0x7ff);
  317. if ((*table & _REGION_ENTRY_INVALID) &&
  318. gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY))
  319. goto out_unmap;
  320. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  321. table = table + (((to + off) >> 42) & 0x7ff);
  322. if ((*table & _REGION_ENTRY_INVALID) &&
  323. gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY))
  324. goto out_unmap;
  325. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  326. table = table + (((to + off) >> 31) & 0x7ff);
  327. if ((*table & _REGION_ENTRY_INVALID) &&
  328. gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY))
  329. goto out_unmap;
  330. table = (unsigned long *) (*table & _REGION_ENTRY_ORIGIN);
  331. table = table + (((to + off) >> 20) & 0x7ff);
  332. /* Store 'from' address in an invalid segment table entry. */
  333. flush |= gmap_unlink_segment(gmap, table);
  334. *table = (from + off) | (_SEGMENT_ENTRY_INVALID |
  335. _SEGMENT_ENTRY_PROTECT);
  336. }
  337. spin_unlock(&gmap->mm->page_table_lock);
  338. up_read(&gmap->mm->mmap_sem);
  339. if (flush)
  340. gmap_flush_tlb(gmap);
  341. return 0;
  342. out_unmap:
  343. spin_unlock(&gmap->mm->page_table_lock);
  344. up_read(&gmap->mm->mmap_sem);
  345. gmap_unmap_segment(gmap, to, len);
  346. return -ENOMEM;
  347. }
  348. EXPORT_SYMBOL_GPL(gmap_map_segment);
  349. static unsigned long *gmap_table_walk(unsigned long address, struct gmap *gmap)
  350. {
  351. unsigned long *table;
  352. table = gmap->table + ((address >> 53) & 0x7ff);
  353. if (unlikely(*table & _REGION_ENTRY_INVALID))
  354. return ERR_PTR(-EFAULT);
  355. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  356. table = table + ((address >> 42) & 0x7ff);
  357. if (unlikely(*table & _REGION_ENTRY_INVALID))
  358. return ERR_PTR(-EFAULT);
  359. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  360. table = table + ((address >> 31) & 0x7ff);
  361. if (unlikely(*table & _REGION_ENTRY_INVALID))
  362. return ERR_PTR(-EFAULT);
  363. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  364. table = table + ((address >> 20) & 0x7ff);
  365. return table;
  366. }
  367. /**
  368. * __gmap_translate - translate a guest address to a user space address
  369. * @address: guest address
  370. * @gmap: pointer to guest mapping meta data structure
  371. *
  372. * Returns user space address which corresponds to the guest address or
  373. * -EFAULT if no such mapping exists.
  374. * This function does not establish potentially missing page table entries.
  375. * The mmap_sem of the mm that belongs to the address space must be held
  376. * when this function gets called.
  377. */
  378. unsigned long __gmap_translate(unsigned long address, struct gmap *gmap)
  379. {
  380. unsigned long *segment_ptr, vmaddr, segment;
  381. struct gmap_pgtable *mp;
  382. struct page *page;
  383. current->thread.gmap_addr = address;
  384. segment_ptr = gmap_table_walk(address, gmap);
  385. if (IS_ERR(segment_ptr))
  386. return PTR_ERR(segment_ptr);
  387. /* Convert the gmap address to an mm address. */
  388. segment = *segment_ptr;
  389. if (!(segment & _SEGMENT_ENTRY_INVALID)) {
  390. page = pfn_to_page(segment >> PAGE_SHIFT);
  391. mp = (struct gmap_pgtable *) page->index;
  392. return mp->vmaddr | (address & ~PMD_MASK);
  393. } else if (segment & _SEGMENT_ENTRY_PROTECT) {
  394. vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
  395. return vmaddr | (address & ~PMD_MASK);
  396. }
  397. return -EFAULT;
  398. }
  399. EXPORT_SYMBOL_GPL(__gmap_translate);
  400. /**
  401. * gmap_translate - translate a guest address to a user space address
  402. * @address: guest address
  403. * @gmap: pointer to guest mapping meta data structure
  404. *
  405. * Returns user space address which corresponds to the guest address or
  406. * -EFAULT if no such mapping exists.
  407. * This function does not establish potentially missing page table entries.
  408. */
  409. unsigned long gmap_translate(unsigned long address, struct gmap *gmap)
  410. {
  411. unsigned long rc;
  412. down_read(&gmap->mm->mmap_sem);
  413. rc = __gmap_translate(address, gmap);
  414. up_read(&gmap->mm->mmap_sem);
  415. return rc;
  416. }
  417. EXPORT_SYMBOL_GPL(gmap_translate);
  418. static int gmap_connect_pgtable(unsigned long address, unsigned long segment,
  419. unsigned long *segment_ptr, struct gmap *gmap)
  420. {
  421. unsigned long vmaddr;
  422. struct vm_area_struct *vma;
  423. struct gmap_pgtable *mp;
  424. struct gmap_rmap *rmap;
  425. struct mm_struct *mm;
  426. struct page *page;
  427. pgd_t *pgd;
  428. pud_t *pud;
  429. pmd_t *pmd;
  430. mm = gmap->mm;
  431. vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
  432. vma = find_vma(mm, vmaddr);
  433. if (!vma || vma->vm_start > vmaddr)
  434. return -EFAULT;
  435. /* Walk the parent mm page table */
  436. pgd = pgd_offset(mm, vmaddr);
  437. pud = pud_alloc(mm, pgd, vmaddr);
  438. if (!pud)
  439. return -ENOMEM;
  440. pmd = pmd_alloc(mm, pud, vmaddr);
  441. if (!pmd)
  442. return -ENOMEM;
  443. if (!pmd_present(*pmd) &&
  444. __pte_alloc(mm, vma, pmd, vmaddr))
  445. return -ENOMEM;
  446. /* pmd now points to a valid segment table entry. */
  447. rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT);
  448. if (!rmap)
  449. return -ENOMEM;
  450. /* Link gmap segment table entry location to page table. */
  451. page = pmd_page(*pmd);
  452. mp = (struct gmap_pgtable *) page->index;
  453. rmap->gmap = gmap;
  454. rmap->entry = segment_ptr;
  455. rmap->vmaddr = address & PMD_MASK;
  456. spin_lock(&mm->page_table_lock);
  457. if (*segment_ptr == segment) {
  458. list_add(&rmap->list, &mp->mapper);
  459. /* Set gmap segment table entry to page table. */
  460. *segment_ptr = pmd_val(*pmd) & PAGE_MASK;
  461. rmap = NULL;
  462. }
  463. spin_unlock(&mm->page_table_lock);
  464. kfree(rmap);
  465. return 0;
  466. }
  467. static void gmap_disconnect_pgtable(struct mm_struct *mm, unsigned long *table)
  468. {
  469. struct gmap_rmap *rmap, *next;
  470. struct gmap_pgtable *mp;
  471. struct page *page;
  472. int flush;
  473. flush = 0;
  474. spin_lock(&mm->page_table_lock);
  475. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  476. mp = (struct gmap_pgtable *) page->index;
  477. list_for_each_entry_safe(rmap, next, &mp->mapper, list) {
  478. *rmap->entry = mp->vmaddr | (_SEGMENT_ENTRY_INVALID |
  479. _SEGMENT_ENTRY_PROTECT);
  480. list_del(&rmap->list);
  481. kfree(rmap);
  482. flush = 1;
  483. }
  484. spin_unlock(&mm->page_table_lock);
  485. if (flush)
  486. __tlb_flush_global();
  487. }
  488. /*
  489. * this function is assumed to be called with mmap_sem held
  490. */
  491. unsigned long __gmap_fault(unsigned long address, struct gmap *gmap)
  492. {
  493. unsigned long *segment_ptr, segment;
  494. struct gmap_pgtable *mp;
  495. struct page *page;
  496. int rc;
  497. current->thread.gmap_addr = address;
  498. segment_ptr = gmap_table_walk(address, gmap);
  499. if (IS_ERR(segment_ptr))
  500. return -EFAULT;
  501. /* Convert the gmap address to an mm address. */
  502. while (1) {
  503. segment = *segment_ptr;
  504. if (!(segment & _SEGMENT_ENTRY_INVALID)) {
  505. /* Page table is present */
  506. page = pfn_to_page(segment >> PAGE_SHIFT);
  507. mp = (struct gmap_pgtable *) page->index;
  508. return mp->vmaddr | (address & ~PMD_MASK);
  509. }
  510. if (!(segment & _SEGMENT_ENTRY_PROTECT))
  511. /* Nothing mapped in the gmap address space. */
  512. break;
  513. rc = gmap_connect_pgtable(address, segment, segment_ptr, gmap);
  514. if (rc)
  515. return rc;
  516. }
  517. return -EFAULT;
  518. }
  519. unsigned long gmap_fault(unsigned long address, struct gmap *gmap)
  520. {
  521. unsigned long rc;
  522. down_read(&gmap->mm->mmap_sem);
  523. rc = __gmap_fault(address, gmap);
  524. up_read(&gmap->mm->mmap_sem);
  525. return rc;
  526. }
  527. EXPORT_SYMBOL_GPL(gmap_fault);
  528. void gmap_discard(unsigned long from, unsigned long to, struct gmap *gmap)
  529. {
  530. unsigned long *table, address, size;
  531. struct vm_area_struct *vma;
  532. struct gmap_pgtable *mp;
  533. struct page *page;
  534. down_read(&gmap->mm->mmap_sem);
  535. address = from;
  536. while (address < to) {
  537. /* Walk the gmap address space page table */
  538. table = gmap->table + ((address >> 53) & 0x7ff);
  539. if (unlikely(*table & _REGION_ENTRY_INVALID)) {
  540. address = (address + PMD_SIZE) & PMD_MASK;
  541. continue;
  542. }
  543. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  544. table = table + ((address >> 42) & 0x7ff);
  545. if (unlikely(*table & _REGION_ENTRY_INVALID)) {
  546. address = (address + PMD_SIZE) & PMD_MASK;
  547. continue;
  548. }
  549. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  550. table = table + ((address >> 31) & 0x7ff);
  551. if (unlikely(*table & _REGION_ENTRY_INVALID)) {
  552. address = (address + PMD_SIZE) & PMD_MASK;
  553. continue;
  554. }
  555. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  556. table = table + ((address >> 20) & 0x7ff);
  557. if (unlikely(*table & _SEGMENT_ENTRY_INVALID)) {
  558. address = (address + PMD_SIZE) & PMD_MASK;
  559. continue;
  560. }
  561. page = pfn_to_page(*table >> PAGE_SHIFT);
  562. mp = (struct gmap_pgtable *) page->index;
  563. vma = find_vma(gmap->mm, mp->vmaddr);
  564. size = min(to - address, PMD_SIZE - (address & ~PMD_MASK));
  565. zap_page_range(vma, mp->vmaddr | (address & ~PMD_MASK),
  566. size, NULL);
  567. address = (address + PMD_SIZE) & PMD_MASK;
  568. }
  569. up_read(&gmap->mm->mmap_sem);
  570. }
  571. EXPORT_SYMBOL_GPL(gmap_discard);
  572. static LIST_HEAD(gmap_notifier_list);
  573. static DEFINE_SPINLOCK(gmap_notifier_lock);
  574. /**
  575. * gmap_register_ipte_notifier - register a pte invalidation callback
  576. * @nb: pointer to the gmap notifier block
  577. */
  578. void gmap_register_ipte_notifier(struct gmap_notifier *nb)
  579. {
  580. spin_lock(&gmap_notifier_lock);
  581. list_add(&nb->list, &gmap_notifier_list);
  582. spin_unlock(&gmap_notifier_lock);
  583. }
  584. EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
  585. /**
  586. * gmap_unregister_ipte_notifier - remove a pte invalidation callback
  587. * @nb: pointer to the gmap notifier block
  588. */
  589. void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
  590. {
  591. spin_lock(&gmap_notifier_lock);
  592. list_del_init(&nb->list);
  593. spin_unlock(&gmap_notifier_lock);
  594. }
  595. EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
  596. /**
  597. * gmap_ipte_notify - mark a range of ptes for invalidation notification
  598. * @gmap: pointer to guest mapping meta data structure
  599. * @address: virtual address in the guest address space
  600. * @len: size of area
  601. *
  602. * Returns 0 if for each page in the given range a gmap mapping exists and
  603. * the invalidation notification could be set. If the gmap mapping is missing
  604. * for one or more pages -EFAULT is returned. If no memory could be allocated
  605. * -ENOMEM is returned. This function establishes missing page table entries.
  606. */
  607. int gmap_ipte_notify(struct gmap *gmap, unsigned long start, unsigned long len)
  608. {
  609. unsigned long addr;
  610. spinlock_t *ptl;
  611. pte_t *ptep, entry;
  612. pgste_t pgste;
  613. int rc = 0;
  614. if ((start & ~PAGE_MASK) || (len & ~PAGE_MASK))
  615. return -EINVAL;
  616. down_read(&gmap->mm->mmap_sem);
  617. while (len) {
  618. /* Convert gmap address and connect the page tables */
  619. addr = __gmap_fault(start, gmap);
  620. if (IS_ERR_VALUE(addr)) {
  621. rc = addr;
  622. break;
  623. }
  624. /* Get the page mapped */
  625. if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
  626. rc = -EFAULT;
  627. break;
  628. }
  629. /* Walk the process page table, lock and get pte pointer */
  630. ptep = get_locked_pte(gmap->mm, addr, &ptl);
  631. if (unlikely(!ptep))
  632. continue;
  633. /* Set notification bit in the pgste of the pte */
  634. entry = *ptep;
  635. if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
  636. pgste = pgste_get_lock(ptep);
  637. pgste_val(pgste) |= PGSTE_IN_BIT;
  638. pgste_set_unlock(ptep, pgste);
  639. start += PAGE_SIZE;
  640. len -= PAGE_SIZE;
  641. }
  642. spin_unlock(ptl);
  643. }
  644. up_read(&gmap->mm->mmap_sem);
  645. return rc;
  646. }
  647. EXPORT_SYMBOL_GPL(gmap_ipte_notify);
  648. /**
  649. * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
  650. * @mm: pointer to the process mm_struct
  651. * @addr: virtual address in the process address space
  652. * @pte: pointer to the page table entry
  653. *
  654. * This function is assumed to be called with the page table lock held
  655. * for the pte to notify.
  656. */
  657. void gmap_do_ipte_notify(struct mm_struct *mm, unsigned long addr, pte_t *pte)
  658. {
  659. unsigned long segment_offset;
  660. struct gmap_notifier *nb;
  661. struct gmap_pgtable *mp;
  662. struct gmap_rmap *rmap;
  663. struct page *page;
  664. segment_offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
  665. segment_offset = segment_offset * (4096 / sizeof(pte_t));
  666. page = pfn_to_page(__pa(pte) >> PAGE_SHIFT);
  667. mp = (struct gmap_pgtable *) page->index;
  668. spin_lock(&gmap_notifier_lock);
  669. list_for_each_entry(rmap, &mp->mapper, list) {
  670. list_for_each_entry(nb, &gmap_notifier_list, list)
  671. nb->notifier_call(rmap->gmap,
  672. rmap->vmaddr + segment_offset);
  673. }
  674. spin_unlock(&gmap_notifier_lock);
  675. }
  676. static inline int page_table_with_pgste(struct page *page)
  677. {
  678. return atomic_read(&page->_mapcount) == 0;
  679. }
  680. static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
  681. unsigned long vmaddr)
  682. {
  683. struct page *page;
  684. unsigned long *table;
  685. struct gmap_pgtable *mp;
  686. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  687. if (!page)
  688. return NULL;
  689. mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT);
  690. if (!mp) {
  691. __free_page(page);
  692. return NULL;
  693. }
  694. pgtable_page_ctor(page);
  695. mp->vmaddr = vmaddr & PMD_MASK;
  696. INIT_LIST_HEAD(&mp->mapper);
  697. page->index = (unsigned long) mp;
  698. atomic_set(&page->_mapcount, 0);
  699. table = (unsigned long *) page_to_phys(page);
  700. clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
  701. clear_table(table + PTRS_PER_PTE, PGSTE_HR_BIT | PGSTE_HC_BIT,
  702. PAGE_SIZE/2);
  703. return table;
  704. }
  705. static inline void page_table_free_pgste(unsigned long *table)
  706. {
  707. struct page *page;
  708. struct gmap_pgtable *mp;
  709. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  710. mp = (struct gmap_pgtable *) page->index;
  711. BUG_ON(!list_empty(&mp->mapper));
  712. pgtable_page_dtor(page);
  713. atomic_set(&page->_mapcount, -1);
  714. kfree(mp);
  715. __free_page(page);
  716. }
  717. int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
  718. unsigned long key, bool nq)
  719. {
  720. spinlock_t *ptl;
  721. pgste_t old, new;
  722. pte_t *ptep;
  723. down_read(&mm->mmap_sem);
  724. ptep = get_locked_pte(current->mm, addr, &ptl);
  725. if (unlikely(!ptep)) {
  726. up_read(&mm->mmap_sem);
  727. return -EFAULT;
  728. }
  729. new = old = pgste_get_lock(ptep);
  730. pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
  731. PGSTE_ACC_BITS | PGSTE_FP_BIT);
  732. pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
  733. pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
  734. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  735. unsigned long address, bits, skey;
  736. address = pte_val(*ptep) & PAGE_MASK;
  737. skey = (unsigned long) page_get_storage_key(address);
  738. bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
  739. skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
  740. /* Set storage key ACC and FP */
  741. page_set_storage_key(address, skey, !nq);
  742. /* Merge host changed & referenced into pgste */
  743. pgste_val(new) |= bits << 52;
  744. }
  745. /* changing the guest storage key is considered a change of the page */
  746. if ((pgste_val(new) ^ pgste_val(old)) &
  747. (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
  748. pgste_val(new) |= PGSTE_HC_BIT;
  749. pgste_set_unlock(ptep, new);
  750. pte_unmap_unlock(*ptep, ptl);
  751. up_read(&mm->mmap_sem);
  752. return 0;
  753. }
  754. EXPORT_SYMBOL(set_guest_storage_key);
  755. #else /* CONFIG_PGSTE */
  756. static inline int page_table_with_pgste(struct page *page)
  757. {
  758. return 0;
  759. }
  760. static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
  761. unsigned long vmaddr)
  762. {
  763. return NULL;
  764. }
  765. static inline void page_table_free_pgste(unsigned long *table)
  766. {
  767. }
  768. static inline void gmap_disconnect_pgtable(struct mm_struct *mm,
  769. unsigned long *table)
  770. {
  771. }
  772. #endif /* CONFIG_PGSTE */
  773. static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
  774. {
  775. unsigned int old, new;
  776. do {
  777. old = atomic_read(v);
  778. new = old ^ bits;
  779. } while (atomic_cmpxchg(v, old, new) != old);
  780. return new;
  781. }
  782. /*
  783. * page table entry allocation/free routines.
  784. */
  785. unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr)
  786. {
  787. unsigned long *uninitialized_var(table);
  788. struct page *uninitialized_var(page);
  789. unsigned int mask, bit;
  790. if (mm_has_pgste(mm))
  791. return page_table_alloc_pgste(mm, vmaddr);
  792. /* Allocate fragments of a 4K page as 1K/2K page table */
  793. spin_lock_bh(&mm->context.list_lock);
  794. mask = FRAG_MASK;
  795. if (!list_empty(&mm->context.pgtable_list)) {
  796. page = list_first_entry(&mm->context.pgtable_list,
  797. struct page, lru);
  798. table = (unsigned long *) page_to_phys(page);
  799. mask = atomic_read(&page->_mapcount);
  800. mask = mask | (mask >> 4);
  801. }
  802. if ((mask & FRAG_MASK) == FRAG_MASK) {
  803. spin_unlock_bh(&mm->context.list_lock);
  804. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  805. if (!page)
  806. return NULL;
  807. pgtable_page_ctor(page);
  808. atomic_set(&page->_mapcount, 1);
  809. table = (unsigned long *) page_to_phys(page);
  810. clear_table(table, _PAGE_INVALID, PAGE_SIZE);
  811. spin_lock_bh(&mm->context.list_lock);
  812. list_add(&page->lru, &mm->context.pgtable_list);
  813. } else {
  814. for (bit = 1; mask & bit; bit <<= 1)
  815. table += PTRS_PER_PTE;
  816. mask = atomic_xor_bits(&page->_mapcount, bit);
  817. if ((mask & FRAG_MASK) == FRAG_MASK)
  818. list_del(&page->lru);
  819. }
  820. spin_unlock_bh(&mm->context.list_lock);
  821. return table;
  822. }
  823. void page_table_free(struct mm_struct *mm, unsigned long *table)
  824. {
  825. struct page *page;
  826. unsigned int bit, mask;
  827. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  828. if (page_table_with_pgste(page)) {
  829. gmap_disconnect_pgtable(mm, table);
  830. return page_table_free_pgste(table);
  831. }
  832. /* Free 1K/2K page table fragment of a 4K page */
  833. bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
  834. spin_lock_bh(&mm->context.list_lock);
  835. if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
  836. list_del(&page->lru);
  837. mask = atomic_xor_bits(&page->_mapcount, bit);
  838. if (mask & FRAG_MASK)
  839. list_add(&page->lru, &mm->context.pgtable_list);
  840. spin_unlock_bh(&mm->context.list_lock);
  841. if (mask == 0) {
  842. pgtable_page_dtor(page);
  843. atomic_set(&page->_mapcount, -1);
  844. __free_page(page);
  845. }
  846. }
  847. static void __page_table_free_rcu(void *table, unsigned bit)
  848. {
  849. struct page *page;
  850. if (bit == FRAG_MASK)
  851. return page_table_free_pgste(table);
  852. /* Free 1K/2K page table fragment of a 4K page */
  853. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  854. if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
  855. pgtable_page_dtor(page);
  856. atomic_set(&page->_mapcount, -1);
  857. __free_page(page);
  858. }
  859. }
  860. void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table)
  861. {
  862. struct mm_struct *mm;
  863. struct page *page;
  864. unsigned int bit, mask;
  865. mm = tlb->mm;
  866. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  867. if (page_table_with_pgste(page)) {
  868. gmap_disconnect_pgtable(mm, table);
  869. table = (unsigned long *) (__pa(table) | FRAG_MASK);
  870. tlb_remove_table(tlb, table);
  871. return;
  872. }
  873. bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
  874. spin_lock_bh(&mm->context.list_lock);
  875. if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
  876. list_del(&page->lru);
  877. mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
  878. if (mask & FRAG_MASK)
  879. list_add_tail(&page->lru, &mm->context.pgtable_list);
  880. spin_unlock_bh(&mm->context.list_lock);
  881. table = (unsigned long *) (__pa(table) | (bit << 4));
  882. tlb_remove_table(tlb, table);
  883. }
  884. static void __tlb_remove_table(void *_table)
  885. {
  886. const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
  887. void *table = (void *)((unsigned long) _table & ~mask);
  888. unsigned type = (unsigned long) _table & mask;
  889. if (type)
  890. __page_table_free_rcu(table, type);
  891. else
  892. free_pages((unsigned long) table, ALLOC_ORDER);
  893. }
  894. static void tlb_remove_table_smp_sync(void *arg)
  895. {
  896. /* Simply deliver the interrupt */
  897. }
  898. static void tlb_remove_table_one(void *table)
  899. {
  900. /*
  901. * This isn't an RCU grace period and hence the page-tables cannot be
  902. * assumed to be actually RCU-freed.
  903. *
  904. * It is however sufficient for software page-table walkers that rely
  905. * on IRQ disabling. See the comment near struct mmu_table_batch.
  906. */
  907. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  908. __tlb_remove_table(table);
  909. }
  910. static void tlb_remove_table_rcu(struct rcu_head *head)
  911. {
  912. struct mmu_table_batch *batch;
  913. int i;
  914. batch = container_of(head, struct mmu_table_batch, rcu);
  915. for (i = 0; i < batch->nr; i++)
  916. __tlb_remove_table(batch->tables[i]);
  917. free_page((unsigned long)batch);
  918. }
  919. void tlb_table_flush(struct mmu_gather *tlb)
  920. {
  921. struct mmu_table_batch **batch = &tlb->batch;
  922. if (*batch) {
  923. call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
  924. *batch = NULL;
  925. }
  926. }
  927. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  928. {
  929. struct mmu_table_batch **batch = &tlb->batch;
  930. tlb->mm->context.flush_mm = 1;
  931. if (*batch == NULL) {
  932. *batch = (struct mmu_table_batch *)
  933. __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  934. if (*batch == NULL) {
  935. __tlb_flush_mm_lazy(tlb->mm);
  936. tlb_remove_table_one(table);
  937. return;
  938. }
  939. (*batch)->nr = 0;
  940. }
  941. (*batch)->tables[(*batch)->nr++] = table;
  942. if ((*batch)->nr == MAX_TABLE_BATCH)
  943. tlb_flush_mmu(tlb);
  944. }
  945. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  946. static inline void thp_split_vma(struct vm_area_struct *vma)
  947. {
  948. unsigned long addr;
  949. for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
  950. follow_page(vma, addr, FOLL_SPLIT);
  951. }
  952. static inline void thp_split_mm(struct mm_struct *mm)
  953. {
  954. struct vm_area_struct *vma;
  955. for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
  956. thp_split_vma(vma);
  957. vma->vm_flags &= ~VM_HUGEPAGE;
  958. vma->vm_flags |= VM_NOHUGEPAGE;
  959. }
  960. mm->def_flags |= VM_NOHUGEPAGE;
  961. }
  962. #else
  963. static inline void thp_split_mm(struct mm_struct *mm)
  964. {
  965. }
  966. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  967. static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb,
  968. struct mm_struct *mm, pud_t *pud,
  969. unsigned long addr, unsigned long end)
  970. {
  971. unsigned long next, *table, *new;
  972. struct page *page;
  973. pmd_t *pmd;
  974. pmd = pmd_offset(pud, addr);
  975. do {
  976. next = pmd_addr_end(addr, end);
  977. again:
  978. if (pmd_none_or_clear_bad(pmd))
  979. continue;
  980. table = (unsigned long *) pmd_deref(*pmd);
  981. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  982. if (page_table_with_pgste(page))
  983. continue;
  984. /* Allocate new page table with pgstes */
  985. new = page_table_alloc_pgste(mm, addr);
  986. if (!new) {
  987. mm->context.has_pgste = 0;
  988. continue;
  989. }
  990. spin_lock(&mm->page_table_lock);
  991. if (likely((unsigned long *) pmd_deref(*pmd) == table)) {
  992. /* Nuke pmd entry pointing to the "short" page table */
  993. pmdp_flush_lazy(mm, addr, pmd);
  994. pmd_clear(pmd);
  995. /* Copy ptes from old table to new table */
  996. memcpy(new, table, PAGE_SIZE/2);
  997. clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
  998. /* Establish new table */
  999. pmd_populate(mm, pmd, (pte_t *) new);
  1000. /* Free old table with rcu, there might be a walker! */
  1001. page_table_free_rcu(tlb, table);
  1002. new = NULL;
  1003. }
  1004. spin_unlock(&mm->page_table_lock);
  1005. if (new) {
  1006. page_table_free_pgste(new);
  1007. goto again;
  1008. }
  1009. } while (pmd++, addr = next, addr != end);
  1010. return addr;
  1011. }
  1012. static unsigned long page_table_realloc_pud(struct mmu_gather *tlb,
  1013. struct mm_struct *mm, pgd_t *pgd,
  1014. unsigned long addr, unsigned long end)
  1015. {
  1016. unsigned long next;
  1017. pud_t *pud;
  1018. pud = pud_offset(pgd, addr);
  1019. do {
  1020. next = pud_addr_end(addr, end);
  1021. if (pud_none_or_clear_bad(pud))
  1022. continue;
  1023. next = page_table_realloc_pmd(tlb, mm, pud, addr, next);
  1024. } while (pud++, addr = next, addr != end);
  1025. return addr;
  1026. }
  1027. static void page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm,
  1028. unsigned long addr, unsigned long end)
  1029. {
  1030. unsigned long next;
  1031. pgd_t *pgd;
  1032. pgd = pgd_offset(mm, addr);
  1033. do {
  1034. next = pgd_addr_end(addr, end);
  1035. if (pgd_none_or_clear_bad(pgd))
  1036. continue;
  1037. next = page_table_realloc_pud(tlb, mm, pgd, addr, next);
  1038. } while (pgd++, addr = next, addr != end);
  1039. }
  1040. /*
  1041. * switch on pgstes for its userspace process (for kvm)
  1042. */
  1043. int s390_enable_sie(void)
  1044. {
  1045. struct task_struct *tsk = current;
  1046. struct mm_struct *mm = tsk->mm;
  1047. struct mmu_gather tlb;
  1048. /* Do we have switched amode? If no, we cannot do sie */
  1049. if (s390_user_mode == HOME_SPACE_MODE)
  1050. return -EINVAL;
  1051. /* Do we have pgstes? if yes, we are done */
  1052. if (mm_has_pgste(tsk->mm))
  1053. return 0;
  1054. down_write(&mm->mmap_sem);
  1055. /* split thp mappings and disable thp for future mappings */
  1056. thp_split_mm(mm);
  1057. /* Reallocate the page tables with pgstes */
  1058. mm->context.has_pgste = 1;
  1059. tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE);
  1060. page_table_realloc(&tlb, mm, 0, TASK_SIZE);
  1061. tlb_finish_mmu(&tlb, 0, TASK_SIZE);
  1062. up_write(&mm->mmap_sem);
  1063. return mm->context.has_pgste ? 0 : -ENOMEM;
  1064. }
  1065. EXPORT_SYMBOL_GPL(s390_enable_sie);
  1066. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1067. int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
  1068. pmd_t *pmdp)
  1069. {
  1070. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1071. /* No need to flush TLB
  1072. * On s390 reference bits are in storage key and never in TLB */
  1073. return pmdp_test_and_clear_young(vma, address, pmdp);
  1074. }
  1075. int pmdp_set_access_flags(struct vm_area_struct *vma,
  1076. unsigned long address, pmd_t *pmdp,
  1077. pmd_t entry, int dirty)
  1078. {
  1079. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1080. if (pmd_same(*pmdp, entry))
  1081. return 0;
  1082. pmdp_invalidate(vma, address, pmdp);
  1083. set_pmd_at(vma->vm_mm, address, pmdp, entry);
  1084. return 1;
  1085. }
  1086. static void pmdp_splitting_flush_sync(void *arg)
  1087. {
  1088. /* Simply deliver the interrupt */
  1089. }
  1090. void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
  1091. pmd_t *pmdp)
  1092. {
  1093. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1094. if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
  1095. (unsigned long *) pmdp)) {
  1096. /* need to serialize against gup-fast (IRQ disabled) */
  1097. smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
  1098. }
  1099. }
  1100. void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  1101. pgtable_t pgtable)
  1102. {
  1103. struct list_head *lh = (struct list_head *) pgtable;
  1104. assert_spin_locked(&mm->page_table_lock);
  1105. /* FIFO */
  1106. if (!mm->pmd_huge_pte)
  1107. INIT_LIST_HEAD(lh);
  1108. else
  1109. list_add(lh, (struct list_head *) mm->pmd_huge_pte);
  1110. mm->pmd_huge_pte = pgtable;
  1111. }
  1112. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  1113. {
  1114. struct list_head *lh;
  1115. pgtable_t pgtable;
  1116. pte_t *ptep;
  1117. assert_spin_locked(&mm->page_table_lock);
  1118. /* FIFO */
  1119. pgtable = mm->pmd_huge_pte;
  1120. lh = (struct list_head *) pgtable;
  1121. if (list_empty(lh))
  1122. mm->pmd_huge_pte = NULL;
  1123. else {
  1124. mm->pmd_huge_pte = (pgtable_t) lh->next;
  1125. list_del(lh);
  1126. }
  1127. ptep = (pte_t *) pgtable;
  1128. pte_val(*ptep) = _PAGE_INVALID;
  1129. ptep++;
  1130. pte_val(*ptep) = _PAGE_INVALID;
  1131. return pgtable;
  1132. }
  1133. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */