pgtable.c 32 KB

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