pgtable.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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 <linux/swapops.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/pgalloc.h>
  22. #include <asm/tlb.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/mmu_context.h>
  25. #ifndef CONFIG_64BIT
  26. #define ALLOC_ORDER 1
  27. #define FRAG_MASK 0x0f
  28. #else
  29. #define ALLOC_ORDER 2
  30. #define FRAG_MASK 0x03
  31. #endif
  32. unsigned long *crst_table_alloc(struct mm_struct *mm)
  33. {
  34. struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  35. if (!page)
  36. return NULL;
  37. return (unsigned long *) page_to_phys(page);
  38. }
  39. void crst_table_free(struct mm_struct *mm, unsigned long *table)
  40. {
  41. free_pages((unsigned long) table, ALLOC_ORDER);
  42. }
  43. #ifdef CONFIG_64BIT
  44. static void __crst_table_upgrade(void *arg)
  45. {
  46. struct mm_struct *mm = arg;
  47. if (current->active_mm == mm) {
  48. clear_user_asce();
  49. set_user_asce(mm);
  50. }
  51. __tlb_flush_local();
  52. }
  53. int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
  54. {
  55. unsigned long *table, *pgd;
  56. unsigned long entry;
  57. int flush;
  58. BUG_ON(limit > (1UL << 53));
  59. flush = 0;
  60. repeat:
  61. table = crst_table_alloc(mm);
  62. if (!table)
  63. return -ENOMEM;
  64. spin_lock_bh(&mm->page_table_lock);
  65. if (mm->context.asce_limit < limit) {
  66. pgd = (unsigned long *) mm->pgd;
  67. if (mm->context.asce_limit <= (1UL << 31)) {
  68. entry = _REGION3_ENTRY_EMPTY;
  69. mm->context.asce_limit = 1UL << 42;
  70. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  71. _ASCE_USER_BITS |
  72. _ASCE_TYPE_REGION3;
  73. } else {
  74. entry = _REGION2_ENTRY_EMPTY;
  75. mm->context.asce_limit = 1UL << 53;
  76. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  77. _ASCE_USER_BITS |
  78. _ASCE_TYPE_REGION2;
  79. }
  80. crst_table_init(table, entry);
  81. pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
  82. mm->pgd = (pgd_t *) table;
  83. mm->task_size = mm->context.asce_limit;
  84. table = NULL;
  85. flush = 1;
  86. }
  87. spin_unlock_bh(&mm->page_table_lock);
  88. if (table)
  89. crst_table_free(mm, table);
  90. if (mm->context.asce_limit < limit)
  91. goto repeat;
  92. if (flush)
  93. on_each_cpu(__crst_table_upgrade, mm, 0);
  94. return 0;
  95. }
  96. void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
  97. {
  98. pgd_t *pgd;
  99. if (current->active_mm == mm) {
  100. clear_user_asce();
  101. __tlb_flush_mm(mm);
  102. }
  103. while (mm->context.asce_limit > limit) {
  104. pgd = mm->pgd;
  105. switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
  106. case _REGION_ENTRY_TYPE_R2:
  107. mm->context.asce_limit = 1UL << 42;
  108. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  109. _ASCE_USER_BITS |
  110. _ASCE_TYPE_REGION3;
  111. break;
  112. case _REGION_ENTRY_TYPE_R3:
  113. mm->context.asce_limit = 1UL << 31;
  114. mm->context.asce_bits = _ASCE_TABLE_LENGTH |
  115. _ASCE_USER_BITS |
  116. _ASCE_TYPE_SEGMENT;
  117. break;
  118. default:
  119. BUG();
  120. }
  121. mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
  122. mm->task_size = mm->context.asce_limit;
  123. crst_table_free(mm, (unsigned long *) pgd);
  124. }
  125. if (current->active_mm == mm)
  126. set_user_asce(mm);
  127. }
  128. #endif
  129. #ifdef CONFIG_PGSTE
  130. /**
  131. * gmap_alloc - allocate a guest address space
  132. * @mm: pointer to the parent mm_struct
  133. *
  134. * Returns a guest address space structure.
  135. */
  136. struct gmap *gmap_alloc(struct mm_struct *mm)
  137. {
  138. struct gmap *gmap;
  139. struct page *page;
  140. unsigned long *table;
  141. gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
  142. if (!gmap)
  143. goto out;
  144. INIT_LIST_HEAD(&gmap->crst_list);
  145. gmap->mm = mm;
  146. page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  147. if (!page)
  148. goto out_free;
  149. list_add(&page->lru, &gmap->crst_list);
  150. table = (unsigned long *) page_to_phys(page);
  151. crst_table_init(table, _REGION1_ENTRY_EMPTY);
  152. gmap->table = table;
  153. gmap->asce = _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH |
  154. _ASCE_USER_BITS | __pa(table);
  155. list_add(&gmap->list, &mm->context.gmap_list);
  156. return gmap;
  157. out_free:
  158. kfree(gmap);
  159. out:
  160. return NULL;
  161. }
  162. EXPORT_SYMBOL_GPL(gmap_alloc);
  163. static int gmap_unlink_segment(struct gmap *gmap, unsigned long *table)
  164. {
  165. struct gmap_pgtable *mp;
  166. struct gmap_rmap *rmap;
  167. struct page *page;
  168. if (*table & _SEGMENT_ENTRY_INVALID)
  169. return 0;
  170. page = pfn_to_page(*table >> PAGE_SHIFT);
  171. mp = (struct gmap_pgtable *) page->index;
  172. list_for_each_entry(rmap, &mp->mapper, list) {
  173. if (rmap->entry != table)
  174. continue;
  175. list_del(&rmap->list);
  176. kfree(rmap);
  177. break;
  178. }
  179. *table = mp->vmaddr | _SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_PROTECT;
  180. return 1;
  181. }
  182. static void gmap_flush_tlb(struct gmap *gmap)
  183. {
  184. if (MACHINE_HAS_IDTE)
  185. __tlb_flush_asce(gmap->mm, (unsigned long) gmap->table |
  186. _ASCE_TYPE_REGION1);
  187. else
  188. __tlb_flush_global();
  189. }
  190. /**
  191. * gmap_free - free a guest address space
  192. * @gmap: pointer to the guest address space structure
  193. */
  194. void gmap_free(struct gmap *gmap)
  195. {
  196. struct page *page, *next;
  197. unsigned long *table;
  198. int i;
  199. /* Flush tlb. */
  200. if (MACHINE_HAS_IDTE)
  201. __tlb_flush_asce(gmap->mm, (unsigned long) gmap->table |
  202. _ASCE_TYPE_REGION1);
  203. else
  204. __tlb_flush_global();
  205. /* Free all segment & region tables. */
  206. down_read(&gmap->mm->mmap_sem);
  207. spin_lock(&gmap->mm->page_table_lock);
  208. list_for_each_entry_safe(page, next, &gmap->crst_list, lru) {
  209. table = (unsigned long *) page_to_phys(page);
  210. if ((*table & _REGION_ENTRY_TYPE_MASK) == 0)
  211. /* Remove gmap rmap structures for segment table. */
  212. for (i = 0; i < PTRS_PER_PMD; i++, table++)
  213. gmap_unlink_segment(gmap, table);
  214. __free_pages(page, ALLOC_ORDER);
  215. }
  216. spin_unlock(&gmap->mm->page_table_lock);
  217. up_read(&gmap->mm->mmap_sem);
  218. list_del(&gmap->list);
  219. kfree(gmap);
  220. }
  221. EXPORT_SYMBOL_GPL(gmap_free);
  222. /**
  223. * gmap_enable - switch primary space to the guest address space
  224. * @gmap: pointer to the guest address space structure
  225. */
  226. void gmap_enable(struct gmap *gmap)
  227. {
  228. S390_lowcore.gmap = (unsigned long) gmap;
  229. }
  230. EXPORT_SYMBOL_GPL(gmap_enable);
  231. /**
  232. * gmap_disable - switch back to the standard primary address space
  233. * @gmap: pointer to the guest address space structure
  234. */
  235. void gmap_disable(struct gmap *gmap)
  236. {
  237. S390_lowcore.gmap = 0UL;
  238. }
  239. EXPORT_SYMBOL_GPL(gmap_disable);
  240. /*
  241. * gmap_alloc_table is assumed to be called with mmap_sem held
  242. */
  243. static int gmap_alloc_table(struct gmap *gmap,
  244. unsigned long *table, unsigned long init)
  245. __releases(&gmap->mm->page_table_lock)
  246. __acquires(&gmap->mm->page_table_lock)
  247. {
  248. struct page *page;
  249. unsigned long *new;
  250. /* since we dont free the gmap table until gmap_free we can unlock */
  251. spin_unlock(&gmap->mm->page_table_lock);
  252. page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
  253. spin_lock(&gmap->mm->page_table_lock);
  254. if (!page)
  255. return -ENOMEM;
  256. new = (unsigned long *) page_to_phys(page);
  257. crst_table_init(new, init);
  258. if (*table & _REGION_ENTRY_INVALID) {
  259. list_add(&page->lru, &gmap->crst_list);
  260. *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
  261. (*table & _REGION_ENTRY_TYPE_MASK);
  262. } else
  263. __free_pages(page, ALLOC_ORDER);
  264. return 0;
  265. }
  266. /**
  267. * gmap_unmap_segment - unmap segment from the guest address space
  268. * @gmap: pointer to the guest address space structure
  269. * @addr: address in the guest address space
  270. * @len: length of the memory area to unmap
  271. *
  272. * Returns 0 if the unmap succeeded, -EINVAL if not.
  273. */
  274. int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
  275. {
  276. unsigned long *table;
  277. unsigned long off;
  278. int flush;
  279. if ((to | len) & (PMD_SIZE - 1))
  280. return -EINVAL;
  281. if (len == 0 || to + len < to)
  282. return -EINVAL;
  283. flush = 0;
  284. down_read(&gmap->mm->mmap_sem);
  285. spin_lock(&gmap->mm->page_table_lock);
  286. for (off = 0; off < len; off += PMD_SIZE) {
  287. /* Walk the guest addr space page table */
  288. table = gmap->table + (((to + off) >> 53) & 0x7ff);
  289. if (*table & _REGION_ENTRY_INVALID)
  290. goto out;
  291. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  292. table = table + (((to + off) >> 42) & 0x7ff);
  293. if (*table & _REGION_ENTRY_INVALID)
  294. goto out;
  295. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  296. table = table + (((to + off) >> 31) & 0x7ff);
  297. if (*table & _REGION_ENTRY_INVALID)
  298. goto out;
  299. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  300. table = table + (((to + off) >> 20) & 0x7ff);
  301. /* Clear segment table entry in guest address space. */
  302. flush |= gmap_unlink_segment(gmap, table);
  303. *table = _SEGMENT_ENTRY_INVALID;
  304. }
  305. out:
  306. spin_unlock(&gmap->mm->page_table_lock);
  307. up_read(&gmap->mm->mmap_sem);
  308. if (flush)
  309. gmap_flush_tlb(gmap);
  310. return 0;
  311. }
  312. EXPORT_SYMBOL_GPL(gmap_unmap_segment);
  313. /**
  314. * gmap_mmap_segment - map a segment to the guest address space
  315. * @gmap: pointer to the guest address space structure
  316. * @from: source address in the parent address space
  317. * @to: target address in the guest address space
  318. *
  319. * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
  320. */
  321. int gmap_map_segment(struct gmap *gmap, unsigned long from,
  322. unsigned long to, unsigned long len)
  323. {
  324. unsigned long *table;
  325. unsigned long off;
  326. int flush;
  327. if ((from | to | len) & (PMD_SIZE - 1))
  328. return -EINVAL;
  329. if (len == 0 || from + len > TASK_MAX_SIZE ||
  330. from + len < from || to + len < to)
  331. return -EINVAL;
  332. flush = 0;
  333. down_read(&gmap->mm->mmap_sem);
  334. spin_lock(&gmap->mm->page_table_lock);
  335. for (off = 0; off < len; off += PMD_SIZE) {
  336. /* Walk the gmap address space page table */
  337. table = gmap->table + (((to + off) >> 53) & 0x7ff);
  338. if ((*table & _REGION_ENTRY_INVALID) &&
  339. gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY))
  340. goto out_unmap;
  341. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  342. table = table + (((to + off) >> 42) & 0x7ff);
  343. if ((*table & _REGION_ENTRY_INVALID) &&
  344. gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY))
  345. goto out_unmap;
  346. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  347. table = table + (((to + off) >> 31) & 0x7ff);
  348. if ((*table & _REGION_ENTRY_INVALID) &&
  349. gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY))
  350. goto out_unmap;
  351. table = (unsigned long *) (*table & _REGION_ENTRY_ORIGIN);
  352. table = table + (((to + off) >> 20) & 0x7ff);
  353. /* Store 'from' address in an invalid segment table entry. */
  354. flush |= gmap_unlink_segment(gmap, table);
  355. *table = (from + off) | (_SEGMENT_ENTRY_INVALID |
  356. _SEGMENT_ENTRY_PROTECT);
  357. }
  358. spin_unlock(&gmap->mm->page_table_lock);
  359. up_read(&gmap->mm->mmap_sem);
  360. if (flush)
  361. gmap_flush_tlb(gmap);
  362. return 0;
  363. out_unmap:
  364. spin_unlock(&gmap->mm->page_table_lock);
  365. up_read(&gmap->mm->mmap_sem);
  366. gmap_unmap_segment(gmap, to, len);
  367. return -ENOMEM;
  368. }
  369. EXPORT_SYMBOL_GPL(gmap_map_segment);
  370. static unsigned long *gmap_table_walk(unsigned long address, struct gmap *gmap)
  371. {
  372. unsigned long *table;
  373. table = gmap->table + ((address >> 53) & 0x7ff);
  374. if (unlikely(*table & _REGION_ENTRY_INVALID))
  375. return ERR_PTR(-EFAULT);
  376. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  377. table = table + ((address >> 42) & 0x7ff);
  378. if (unlikely(*table & _REGION_ENTRY_INVALID))
  379. return ERR_PTR(-EFAULT);
  380. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  381. table = table + ((address >> 31) & 0x7ff);
  382. if (unlikely(*table & _REGION_ENTRY_INVALID))
  383. return ERR_PTR(-EFAULT);
  384. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  385. table = table + ((address >> 20) & 0x7ff);
  386. return table;
  387. }
  388. /**
  389. * __gmap_translate - translate a guest address to a user space address
  390. * @address: guest address
  391. * @gmap: pointer to guest mapping meta data structure
  392. *
  393. * Returns user space address which corresponds to the guest address or
  394. * -EFAULT if no such mapping exists.
  395. * This function does not establish potentially missing page table entries.
  396. * The mmap_sem of the mm that belongs to the address space must be held
  397. * when this function gets called.
  398. */
  399. unsigned long __gmap_translate(unsigned long address, struct gmap *gmap)
  400. {
  401. unsigned long *segment_ptr, vmaddr, segment;
  402. struct gmap_pgtable *mp;
  403. struct page *page;
  404. current->thread.gmap_addr = address;
  405. segment_ptr = gmap_table_walk(address, gmap);
  406. if (IS_ERR(segment_ptr))
  407. return PTR_ERR(segment_ptr);
  408. /* Convert the gmap address to an mm address. */
  409. segment = *segment_ptr;
  410. if (!(segment & _SEGMENT_ENTRY_INVALID)) {
  411. page = pfn_to_page(segment >> PAGE_SHIFT);
  412. mp = (struct gmap_pgtable *) page->index;
  413. return mp->vmaddr | (address & ~PMD_MASK);
  414. } else if (segment & _SEGMENT_ENTRY_PROTECT) {
  415. vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
  416. return vmaddr | (address & ~PMD_MASK);
  417. }
  418. return -EFAULT;
  419. }
  420. EXPORT_SYMBOL_GPL(__gmap_translate);
  421. /**
  422. * gmap_translate - translate a guest address to a user space address
  423. * @address: guest address
  424. * @gmap: pointer to guest mapping meta data structure
  425. *
  426. * Returns user space address which corresponds to the guest address or
  427. * -EFAULT if no such mapping exists.
  428. * This function does not establish potentially missing page table entries.
  429. */
  430. unsigned long gmap_translate(unsigned long address, struct gmap *gmap)
  431. {
  432. unsigned long rc;
  433. down_read(&gmap->mm->mmap_sem);
  434. rc = __gmap_translate(address, gmap);
  435. up_read(&gmap->mm->mmap_sem);
  436. return rc;
  437. }
  438. EXPORT_SYMBOL_GPL(gmap_translate);
  439. static int gmap_connect_pgtable(unsigned long address, unsigned long segment,
  440. unsigned long *segment_ptr, struct gmap *gmap)
  441. {
  442. unsigned long vmaddr;
  443. struct vm_area_struct *vma;
  444. struct gmap_pgtable *mp;
  445. struct gmap_rmap *rmap;
  446. struct mm_struct *mm;
  447. struct page *page;
  448. pgd_t *pgd;
  449. pud_t *pud;
  450. pmd_t *pmd;
  451. mm = gmap->mm;
  452. vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
  453. vma = find_vma(mm, vmaddr);
  454. if (!vma || vma->vm_start > vmaddr)
  455. return -EFAULT;
  456. /* Walk the parent mm page table */
  457. pgd = pgd_offset(mm, vmaddr);
  458. pud = pud_alloc(mm, pgd, vmaddr);
  459. if (!pud)
  460. return -ENOMEM;
  461. pmd = pmd_alloc(mm, pud, vmaddr);
  462. if (!pmd)
  463. return -ENOMEM;
  464. if (!pmd_present(*pmd) &&
  465. __pte_alloc(mm, vma, pmd, vmaddr))
  466. return -ENOMEM;
  467. /* large pmds cannot yet be handled */
  468. if (pmd_large(*pmd))
  469. return -EFAULT;
  470. /* pmd now points to a valid segment table entry. */
  471. rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT);
  472. if (!rmap)
  473. return -ENOMEM;
  474. /* Link gmap segment table entry location to page table. */
  475. page = pmd_page(*pmd);
  476. mp = (struct gmap_pgtable *) page->index;
  477. rmap->gmap = gmap;
  478. rmap->entry = segment_ptr;
  479. rmap->vmaddr = address & PMD_MASK;
  480. spin_lock(&mm->page_table_lock);
  481. if (*segment_ptr == segment) {
  482. list_add(&rmap->list, &mp->mapper);
  483. /* Set gmap segment table entry to page table. */
  484. *segment_ptr = pmd_val(*pmd) & PAGE_MASK;
  485. rmap = NULL;
  486. }
  487. spin_unlock(&mm->page_table_lock);
  488. kfree(rmap);
  489. return 0;
  490. }
  491. static void gmap_disconnect_pgtable(struct mm_struct *mm, unsigned long *table)
  492. {
  493. struct gmap_rmap *rmap, *next;
  494. struct gmap_pgtable *mp;
  495. struct page *page;
  496. int flush;
  497. flush = 0;
  498. spin_lock(&mm->page_table_lock);
  499. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  500. mp = (struct gmap_pgtable *) page->index;
  501. list_for_each_entry_safe(rmap, next, &mp->mapper, list) {
  502. *rmap->entry = mp->vmaddr | (_SEGMENT_ENTRY_INVALID |
  503. _SEGMENT_ENTRY_PROTECT);
  504. list_del(&rmap->list);
  505. kfree(rmap);
  506. flush = 1;
  507. }
  508. spin_unlock(&mm->page_table_lock);
  509. if (flush)
  510. __tlb_flush_global();
  511. }
  512. /*
  513. * this function is assumed to be called with mmap_sem held
  514. */
  515. unsigned long __gmap_fault(unsigned long address, struct gmap *gmap)
  516. {
  517. unsigned long *segment_ptr, segment;
  518. struct gmap_pgtable *mp;
  519. struct page *page;
  520. int rc;
  521. current->thread.gmap_addr = address;
  522. segment_ptr = gmap_table_walk(address, gmap);
  523. if (IS_ERR(segment_ptr))
  524. return -EFAULT;
  525. /* Convert the gmap address to an mm address. */
  526. while (1) {
  527. segment = *segment_ptr;
  528. if (!(segment & _SEGMENT_ENTRY_INVALID)) {
  529. /* Page table is present */
  530. page = pfn_to_page(segment >> PAGE_SHIFT);
  531. mp = (struct gmap_pgtable *) page->index;
  532. return mp->vmaddr | (address & ~PMD_MASK);
  533. }
  534. if (!(segment & _SEGMENT_ENTRY_PROTECT))
  535. /* Nothing mapped in the gmap address space. */
  536. break;
  537. rc = gmap_connect_pgtable(address, segment, segment_ptr, gmap);
  538. if (rc)
  539. return rc;
  540. }
  541. return -EFAULT;
  542. }
  543. unsigned long gmap_fault(unsigned long address, struct gmap *gmap)
  544. {
  545. unsigned long rc;
  546. down_read(&gmap->mm->mmap_sem);
  547. rc = __gmap_fault(address, gmap);
  548. up_read(&gmap->mm->mmap_sem);
  549. return rc;
  550. }
  551. EXPORT_SYMBOL_GPL(gmap_fault);
  552. static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
  553. {
  554. if (!non_swap_entry(entry))
  555. dec_mm_counter(mm, MM_SWAPENTS);
  556. else if (is_migration_entry(entry)) {
  557. struct page *page = migration_entry_to_page(entry);
  558. if (PageAnon(page))
  559. dec_mm_counter(mm, MM_ANONPAGES);
  560. else
  561. dec_mm_counter(mm, MM_FILEPAGES);
  562. }
  563. free_swap_and_cache(entry);
  564. }
  565. /**
  566. * The mm->mmap_sem lock must be held
  567. */
  568. static void gmap_zap_unused(struct mm_struct *mm, unsigned long address)
  569. {
  570. unsigned long ptev, pgstev;
  571. spinlock_t *ptl;
  572. pgste_t pgste;
  573. pte_t *ptep, pte;
  574. ptep = get_locked_pte(mm, address, &ptl);
  575. if (unlikely(!ptep))
  576. return;
  577. pte = *ptep;
  578. if (!pte_swap(pte))
  579. goto out_pte;
  580. /* Zap unused and logically-zero pages */
  581. pgste = pgste_get_lock(ptep);
  582. pgstev = pgste_val(pgste);
  583. ptev = pte_val(pte);
  584. if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
  585. ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID))) {
  586. gmap_zap_swap_entry(pte_to_swp_entry(pte), mm);
  587. pte_clear(mm, address, ptep);
  588. }
  589. pgste_set_unlock(ptep, pgste);
  590. out_pte:
  591. pte_unmap_unlock(*ptep, ptl);
  592. }
  593. /*
  594. * this function is assumed to be called with mmap_sem held
  595. */
  596. void __gmap_zap(unsigned long address, struct gmap *gmap)
  597. {
  598. unsigned long *table, *segment_ptr;
  599. unsigned long segment, pgstev, ptev;
  600. struct gmap_pgtable *mp;
  601. struct page *page;
  602. segment_ptr = gmap_table_walk(address, gmap);
  603. if (IS_ERR(segment_ptr))
  604. return;
  605. segment = *segment_ptr;
  606. if (segment & _SEGMENT_ENTRY_INVALID)
  607. return;
  608. page = pfn_to_page(segment >> PAGE_SHIFT);
  609. mp = (struct gmap_pgtable *) page->index;
  610. address = mp->vmaddr | (address & ~PMD_MASK);
  611. /* Page table is present */
  612. table = (unsigned long *)(segment & _SEGMENT_ENTRY_ORIGIN);
  613. table = table + ((address >> 12) & 0xff);
  614. pgstev = table[PTRS_PER_PTE];
  615. ptev = table[0];
  616. /* quick check, checked again with locks held */
  617. if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
  618. ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID)))
  619. gmap_zap_unused(gmap->mm, address);
  620. }
  621. EXPORT_SYMBOL_GPL(__gmap_zap);
  622. void gmap_discard(unsigned long from, unsigned long to, struct gmap *gmap)
  623. {
  624. unsigned long *table, address, size;
  625. struct vm_area_struct *vma;
  626. struct gmap_pgtable *mp;
  627. struct page *page;
  628. down_read(&gmap->mm->mmap_sem);
  629. address = from;
  630. while (address < to) {
  631. /* Walk the gmap address space page table */
  632. table = gmap->table + ((address >> 53) & 0x7ff);
  633. if (unlikely(*table & _REGION_ENTRY_INVALID)) {
  634. address = (address + PMD_SIZE) & PMD_MASK;
  635. continue;
  636. }
  637. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  638. table = table + ((address >> 42) & 0x7ff);
  639. if (unlikely(*table & _REGION_ENTRY_INVALID)) {
  640. address = (address + PMD_SIZE) & PMD_MASK;
  641. continue;
  642. }
  643. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  644. table = table + ((address >> 31) & 0x7ff);
  645. if (unlikely(*table & _REGION_ENTRY_INVALID)) {
  646. address = (address + PMD_SIZE) & PMD_MASK;
  647. continue;
  648. }
  649. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  650. table = table + ((address >> 20) & 0x7ff);
  651. if (unlikely(*table & _SEGMENT_ENTRY_INVALID)) {
  652. address = (address + PMD_SIZE) & PMD_MASK;
  653. continue;
  654. }
  655. page = pfn_to_page(*table >> PAGE_SHIFT);
  656. mp = (struct gmap_pgtable *) page->index;
  657. vma = find_vma(gmap->mm, mp->vmaddr);
  658. size = min(to - address, PMD_SIZE - (address & ~PMD_MASK));
  659. zap_page_range(vma, mp->vmaddr | (address & ~PMD_MASK),
  660. size, NULL);
  661. address = (address + PMD_SIZE) & PMD_MASK;
  662. }
  663. up_read(&gmap->mm->mmap_sem);
  664. }
  665. EXPORT_SYMBOL_GPL(gmap_discard);
  666. static LIST_HEAD(gmap_notifier_list);
  667. static DEFINE_SPINLOCK(gmap_notifier_lock);
  668. /**
  669. * gmap_register_ipte_notifier - register a pte invalidation callback
  670. * @nb: pointer to the gmap notifier block
  671. */
  672. void gmap_register_ipte_notifier(struct gmap_notifier *nb)
  673. {
  674. spin_lock(&gmap_notifier_lock);
  675. list_add(&nb->list, &gmap_notifier_list);
  676. spin_unlock(&gmap_notifier_lock);
  677. }
  678. EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
  679. /**
  680. * gmap_unregister_ipte_notifier - remove a pte invalidation callback
  681. * @nb: pointer to the gmap notifier block
  682. */
  683. void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
  684. {
  685. spin_lock(&gmap_notifier_lock);
  686. list_del_init(&nb->list);
  687. spin_unlock(&gmap_notifier_lock);
  688. }
  689. EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
  690. /**
  691. * gmap_ipte_notify - mark a range of ptes for invalidation notification
  692. * @gmap: pointer to guest mapping meta data structure
  693. * @start: virtual address in the guest address space
  694. * @len: size of area
  695. *
  696. * Returns 0 if for each page in the given range a gmap mapping exists and
  697. * the invalidation notification could be set. If the gmap mapping is missing
  698. * for one or more pages -EFAULT is returned. If no memory could be allocated
  699. * -ENOMEM is returned. This function establishes missing page table entries.
  700. */
  701. int gmap_ipte_notify(struct gmap *gmap, unsigned long start, unsigned long len)
  702. {
  703. unsigned long addr;
  704. spinlock_t *ptl;
  705. pte_t *ptep, entry;
  706. pgste_t pgste;
  707. int rc = 0;
  708. if ((start & ~PAGE_MASK) || (len & ~PAGE_MASK))
  709. return -EINVAL;
  710. down_read(&gmap->mm->mmap_sem);
  711. while (len) {
  712. /* Convert gmap address and connect the page tables */
  713. addr = __gmap_fault(start, gmap);
  714. if (IS_ERR_VALUE(addr)) {
  715. rc = addr;
  716. break;
  717. }
  718. /* Get the page mapped */
  719. if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
  720. rc = -EFAULT;
  721. break;
  722. }
  723. /* Walk the process page table, lock and get pte pointer */
  724. ptep = get_locked_pte(gmap->mm, addr, &ptl);
  725. if (unlikely(!ptep))
  726. continue;
  727. /* Set notification bit in the pgste of the pte */
  728. entry = *ptep;
  729. if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
  730. pgste = pgste_get_lock(ptep);
  731. pgste_val(pgste) |= PGSTE_IN_BIT;
  732. pgste_set_unlock(ptep, pgste);
  733. start += PAGE_SIZE;
  734. len -= PAGE_SIZE;
  735. }
  736. spin_unlock(ptl);
  737. }
  738. up_read(&gmap->mm->mmap_sem);
  739. return rc;
  740. }
  741. EXPORT_SYMBOL_GPL(gmap_ipte_notify);
  742. /**
  743. * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
  744. * @mm: pointer to the process mm_struct
  745. * @pte: pointer to the page table entry
  746. *
  747. * This function is assumed to be called with the page table lock held
  748. * for the pte to notify.
  749. */
  750. void gmap_do_ipte_notify(struct mm_struct *mm, pte_t *pte)
  751. {
  752. unsigned long segment_offset;
  753. struct gmap_notifier *nb;
  754. struct gmap_pgtable *mp;
  755. struct gmap_rmap *rmap;
  756. struct page *page;
  757. segment_offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
  758. segment_offset = segment_offset * (4096 / sizeof(pte_t));
  759. page = pfn_to_page(__pa(pte) >> PAGE_SHIFT);
  760. mp = (struct gmap_pgtable *) page->index;
  761. spin_lock(&gmap_notifier_lock);
  762. list_for_each_entry(rmap, &mp->mapper, list) {
  763. list_for_each_entry(nb, &gmap_notifier_list, list)
  764. nb->notifier_call(rmap->gmap,
  765. rmap->vmaddr + segment_offset);
  766. }
  767. spin_unlock(&gmap_notifier_lock);
  768. }
  769. EXPORT_SYMBOL_GPL(gmap_do_ipte_notify);
  770. static inline int page_table_with_pgste(struct page *page)
  771. {
  772. return atomic_read(&page->_mapcount) == 0;
  773. }
  774. static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
  775. unsigned long vmaddr)
  776. {
  777. struct page *page;
  778. unsigned long *table;
  779. struct gmap_pgtable *mp;
  780. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  781. if (!page)
  782. return NULL;
  783. mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT);
  784. if (!mp) {
  785. __free_page(page);
  786. return NULL;
  787. }
  788. if (!pgtable_page_ctor(page)) {
  789. kfree(mp);
  790. __free_page(page);
  791. return NULL;
  792. }
  793. mp->vmaddr = vmaddr & PMD_MASK;
  794. INIT_LIST_HEAD(&mp->mapper);
  795. page->index = (unsigned long) mp;
  796. atomic_set(&page->_mapcount, 0);
  797. table = (unsigned long *) page_to_phys(page);
  798. clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
  799. clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
  800. return table;
  801. }
  802. static inline void page_table_free_pgste(unsigned long *table)
  803. {
  804. struct page *page;
  805. struct gmap_pgtable *mp;
  806. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  807. mp = (struct gmap_pgtable *) page->index;
  808. BUG_ON(!list_empty(&mp->mapper));
  809. pgtable_page_dtor(page);
  810. atomic_set(&page->_mapcount, -1);
  811. kfree(mp);
  812. __free_page(page);
  813. }
  814. static inline unsigned long page_table_reset_pte(struct mm_struct *mm, pmd_t *pmd,
  815. unsigned long addr, unsigned long end, bool init_skey)
  816. {
  817. pte_t *start_pte, *pte;
  818. spinlock_t *ptl;
  819. pgste_t pgste;
  820. start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  821. pte = start_pte;
  822. do {
  823. pgste = pgste_get_lock(pte);
  824. pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
  825. if (init_skey) {
  826. unsigned long address;
  827. pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT |
  828. PGSTE_GR_BIT | PGSTE_GC_BIT);
  829. /* skip invalid and not writable pages */
  830. if (pte_val(*pte) & _PAGE_INVALID ||
  831. !(pte_val(*pte) & _PAGE_WRITE)) {
  832. pgste_set_unlock(pte, pgste);
  833. continue;
  834. }
  835. address = pte_val(*pte) & PAGE_MASK;
  836. page_set_storage_key(address, PAGE_DEFAULT_KEY, 1);
  837. }
  838. pgste_set_unlock(pte, pgste);
  839. } while (pte++, addr += PAGE_SIZE, addr != end);
  840. pte_unmap_unlock(start_pte, ptl);
  841. return addr;
  842. }
  843. static inline unsigned long page_table_reset_pmd(struct mm_struct *mm, pud_t *pud,
  844. unsigned long addr, unsigned long end, bool init_skey)
  845. {
  846. unsigned long next;
  847. pmd_t *pmd;
  848. pmd = pmd_offset(pud, addr);
  849. do {
  850. next = pmd_addr_end(addr, end);
  851. if (pmd_none_or_clear_bad(pmd))
  852. continue;
  853. next = page_table_reset_pte(mm, pmd, addr, next, init_skey);
  854. } while (pmd++, addr = next, addr != end);
  855. return addr;
  856. }
  857. static inline unsigned long page_table_reset_pud(struct mm_struct *mm, pgd_t *pgd,
  858. unsigned long addr, unsigned long end, bool init_skey)
  859. {
  860. unsigned long next;
  861. pud_t *pud;
  862. pud = pud_offset(pgd, addr);
  863. do {
  864. next = pud_addr_end(addr, end);
  865. if (pud_none_or_clear_bad(pud))
  866. continue;
  867. next = page_table_reset_pmd(mm, pud, addr, next, init_skey);
  868. } while (pud++, addr = next, addr != end);
  869. return addr;
  870. }
  871. void page_table_reset_pgste(struct mm_struct *mm, unsigned long start,
  872. unsigned long end, bool init_skey)
  873. {
  874. unsigned long addr, next;
  875. pgd_t *pgd;
  876. down_write(&mm->mmap_sem);
  877. if (init_skey && mm_use_skey(mm))
  878. goto out_up;
  879. addr = start;
  880. pgd = pgd_offset(mm, addr);
  881. do {
  882. next = pgd_addr_end(addr, end);
  883. if (pgd_none_or_clear_bad(pgd))
  884. continue;
  885. next = page_table_reset_pud(mm, pgd, addr, next, init_skey);
  886. } while (pgd++, addr = next, addr != end);
  887. if (init_skey)
  888. current->mm->context.use_skey = 1;
  889. out_up:
  890. up_write(&mm->mmap_sem);
  891. }
  892. EXPORT_SYMBOL(page_table_reset_pgste);
  893. int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
  894. unsigned long key, bool nq)
  895. {
  896. spinlock_t *ptl;
  897. pgste_t old, new;
  898. pte_t *ptep;
  899. down_read(&mm->mmap_sem);
  900. retry:
  901. ptep = get_locked_pte(current->mm, addr, &ptl);
  902. if (unlikely(!ptep)) {
  903. up_read(&mm->mmap_sem);
  904. return -EFAULT;
  905. }
  906. if (!(pte_val(*ptep) & _PAGE_INVALID) &&
  907. (pte_val(*ptep) & _PAGE_PROTECT)) {
  908. pte_unmap_unlock(*ptep, ptl);
  909. if (fixup_user_fault(current, mm, addr, FAULT_FLAG_WRITE)) {
  910. up_read(&mm->mmap_sem);
  911. return -EFAULT;
  912. }
  913. goto retry;
  914. }
  915. new = old = pgste_get_lock(ptep);
  916. pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
  917. PGSTE_ACC_BITS | PGSTE_FP_BIT);
  918. pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
  919. pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
  920. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  921. unsigned long address, bits, skey;
  922. address = pte_val(*ptep) & PAGE_MASK;
  923. skey = (unsigned long) page_get_storage_key(address);
  924. bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
  925. skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
  926. /* Set storage key ACC and FP */
  927. page_set_storage_key(address, skey, !nq);
  928. /* Merge host changed & referenced into pgste */
  929. pgste_val(new) |= bits << 52;
  930. }
  931. /* changing the guest storage key is considered a change of the page */
  932. if ((pgste_val(new) ^ pgste_val(old)) &
  933. (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
  934. pgste_val(new) |= PGSTE_UC_BIT;
  935. pgste_set_unlock(ptep, new);
  936. pte_unmap_unlock(*ptep, ptl);
  937. up_read(&mm->mmap_sem);
  938. return 0;
  939. }
  940. EXPORT_SYMBOL(set_guest_storage_key);
  941. #else /* CONFIG_PGSTE */
  942. static inline int page_table_with_pgste(struct page *page)
  943. {
  944. return 0;
  945. }
  946. static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
  947. unsigned long vmaddr)
  948. {
  949. return NULL;
  950. }
  951. void page_table_reset_pgste(struct mm_struct *mm, unsigned long start,
  952. unsigned long end, bool init_skey)
  953. {
  954. }
  955. static inline void page_table_free_pgste(unsigned long *table)
  956. {
  957. }
  958. static inline void gmap_disconnect_pgtable(struct mm_struct *mm,
  959. unsigned long *table)
  960. {
  961. }
  962. #endif /* CONFIG_PGSTE */
  963. static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
  964. {
  965. unsigned int old, new;
  966. do {
  967. old = atomic_read(v);
  968. new = old ^ bits;
  969. } while (atomic_cmpxchg(v, old, new) != old);
  970. return new;
  971. }
  972. /*
  973. * page table entry allocation/free routines.
  974. */
  975. unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr)
  976. {
  977. unsigned long *uninitialized_var(table);
  978. struct page *uninitialized_var(page);
  979. unsigned int mask, bit;
  980. if (mm_has_pgste(mm))
  981. return page_table_alloc_pgste(mm, vmaddr);
  982. /* Allocate fragments of a 4K page as 1K/2K page table */
  983. spin_lock_bh(&mm->context.list_lock);
  984. mask = FRAG_MASK;
  985. if (!list_empty(&mm->context.pgtable_list)) {
  986. page = list_first_entry(&mm->context.pgtable_list,
  987. struct page, lru);
  988. table = (unsigned long *) page_to_phys(page);
  989. mask = atomic_read(&page->_mapcount);
  990. mask = mask | (mask >> 4);
  991. }
  992. if ((mask & FRAG_MASK) == FRAG_MASK) {
  993. spin_unlock_bh(&mm->context.list_lock);
  994. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  995. if (!page)
  996. return NULL;
  997. if (!pgtable_page_ctor(page)) {
  998. __free_page(page);
  999. return NULL;
  1000. }
  1001. atomic_set(&page->_mapcount, 1);
  1002. table = (unsigned long *) page_to_phys(page);
  1003. clear_table(table, _PAGE_INVALID, PAGE_SIZE);
  1004. spin_lock_bh(&mm->context.list_lock);
  1005. list_add(&page->lru, &mm->context.pgtable_list);
  1006. } else {
  1007. for (bit = 1; mask & bit; bit <<= 1)
  1008. table += PTRS_PER_PTE;
  1009. mask = atomic_xor_bits(&page->_mapcount, bit);
  1010. if ((mask & FRAG_MASK) == FRAG_MASK)
  1011. list_del(&page->lru);
  1012. }
  1013. spin_unlock_bh(&mm->context.list_lock);
  1014. return table;
  1015. }
  1016. void page_table_free(struct mm_struct *mm, unsigned long *table)
  1017. {
  1018. struct page *page;
  1019. unsigned int bit, mask;
  1020. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  1021. if (page_table_with_pgste(page)) {
  1022. gmap_disconnect_pgtable(mm, table);
  1023. return page_table_free_pgste(table);
  1024. }
  1025. /* Free 1K/2K page table fragment of a 4K page */
  1026. bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
  1027. spin_lock_bh(&mm->context.list_lock);
  1028. if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
  1029. list_del(&page->lru);
  1030. mask = atomic_xor_bits(&page->_mapcount, bit);
  1031. if (mask & FRAG_MASK)
  1032. list_add(&page->lru, &mm->context.pgtable_list);
  1033. spin_unlock_bh(&mm->context.list_lock);
  1034. if (mask == 0) {
  1035. pgtable_page_dtor(page);
  1036. atomic_set(&page->_mapcount, -1);
  1037. __free_page(page);
  1038. }
  1039. }
  1040. static void __page_table_free_rcu(void *table, unsigned bit)
  1041. {
  1042. struct page *page;
  1043. if (bit == FRAG_MASK)
  1044. return page_table_free_pgste(table);
  1045. /* Free 1K/2K page table fragment of a 4K page */
  1046. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  1047. if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
  1048. pgtable_page_dtor(page);
  1049. atomic_set(&page->_mapcount, -1);
  1050. __free_page(page);
  1051. }
  1052. }
  1053. void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table)
  1054. {
  1055. struct mm_struct *mm;
  1056. struct page *page;
  1057. unsigned int bit, mask;
  1058. mm = tlb->mm;
  1059. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  1060. if (page_table_with_pgste(page)) {
  1061. gmap_disconnect_pgtable(mm, table);
  1062. table = (unsigned long *) (__pa(table) | FRAG_MASK);
  1063. tlb_remove_table(tlb, table);
  1064. return;
  1065. }
  1066. bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
  1067. spin_lock_bh(&mm->context.list_lock);
  1068. if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
  1069. list_del(&page->lru);
  1070. mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
  1071. if (mask & FRAG_MASK)
  1072. list_add_tail(&page->lru, &mm->context.pgtable_list);
  1073. spin_unlock_bh(&mm->context.list_lock);
  1074. table = (unsigned long *) (__pa(table) | (bit << 4));
  1075. tlb_remove_table(tlb, table);
  1076. }
  1077. static void __tlb_remove_table(void *_table)
  1078. {
  1079. const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
  1080. void *table = (void *)((unsigned long) _table & ~mask);
  1081. unsigned type = (unsigned long) _table & mask;
  1082. if (type)
  1083. __page_table_free_rcu(table, type);
  1084. else
  1085. free_pages((unsigned long) table, ALLOC_ORDER);
  1086. }
  1087. static void tlb_remove_table_smp_sync(void *arg)
  1088. {
  1089. /* Simply deliver the interrupt */
  1090. }
  1091. static void tlb_remove_table_one(void *table)
  1092. {
  1093. /*
  1094. * This isn't an RCU grace period and hence the page-tables cannot be
  1095. * assumed to be actually RCU-freed.
  1096. *
  1097. * It is however sufficient for software page-table walkers that rely
  1098. * on IRQ disabling. See the comment near struct mmu_table_batch.
  1099. */
  1100. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  1101. __tlb_remove_table(table);
  1102. }
  1103. static void tlb_remove_table_rcu(struct rcu_head *head)
  1104. {
  1105. struct mmu_table_batch *batch;
  1106. int i;
  1107. batch = container_of(head, struct mmu_table_batch, rcu);
  1108. for (i = 0; i < batch->nr; i++)
  1109. __tlb_remove_table(batch->tables[i]);
  1110. free_page((unsigned long)batch);
  1111. }
  1112. void tlb_table_flush(struct mmu_gather *tlb)
  1113. {
  1114. struct mmu_table_batch **batch = &tlb->batch;
  1115. if (*batch) {
  1116. call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
  1117. *batch = NULL;
  1118. }
  1119. }
  1120. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  1121. {
  1122. struct mmu_table_batch **batch = &tlb->batch;
  1123. tlb->mm->context.flush_mm = 1;
  1124. if (*batch == NULL) {
  1125. *batch = (struct mmu_table_batch *)
  1126. __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  1127. if (*batch == NULL) {
  1128. __tlb_flush_mm_lazy(tlb->mm);
  1129. tlb_remove_table_one(table);
  1130. return;
  1131. }
  1132. (*batch)->nr = 0;
  1133. }
  1134. (*batch)->tables[(*batch)->nr++] = table;
  1135. if ((*batch)->nr == MAX_TABLE_BATCH)
  1136. tlb_flush_mmu(tlb);
  1137. }
  1138. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1139. static inline void thp_split_vma(struct vm_area_struct *vma)
  1140. {
  1141. unsigned long addr;
  1142. for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
  1143. follow_page(vma, addr, FOLL_SPLIT);
  1144. }
  1145. static inline void thp_split_mm(struct mm_struct *mm)
  1146. {
  1147. struct vm_area_struct *vma;
  1148. for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
  1149. thp_split_vma(vma);
  1150. vma->vm_flags &= ~VM_HUGEPAGE;
  1151. vma->vm_flags |= VM_NOHUGEPAGE;
  1152. }
  1153. mm->def_flags |= VM_NOHUGEPAGE;
  1154. }
  1155. #else
  1156. static inline void thp_split_mm(struct mm_struct *mm)
  1157. {
  1158. }
  1159. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  1160. static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb,
  1161. struct mm_struct *mm, pud_t *pud,
  1162. unsigned long addr, unsigned long end)
  1163. {
  1164. unsigned long next, *table, *new;
  1165. struct page *page;
  1166. spinlock_t *ptl;
  1167. pmd_t *pmd;
  1168. pmd = pmd_offset(pud, addr);
  1169. do {
  1170. next = pmd_addr_end(addr, end);
  1171. again:
  1172. if (pmd_none_or_clear_bad(pmd))
  1173. continue;
  1174. table = (unsigned long *) pmd_deref(*pmd);
  1175. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  1176. if (page_table_with_pgste(page))
  1177. continue;
  1178. /* Allocate new page table with pgstes */
  1179. new = page_table_alloc_pgste(mm, addr);
  1180. if (!new)
  1181. return -ENOMEM;
  1182. ptl = pmd_lock(mm, pmd);
  1183. if (likely((unsigned long *) pmd_deref(*pmd) == table)) {
  1184. /* Nuke pmd entry pointing to the "short" page table */
  1185. pmdp_flush_lazy(mm, addr, pmd);
  1186. pmd_clear(pmd);
  1187. /* Copy ptes from old table to new table */
  1188. memcpy(new, table, PAGE_SIZE/2);
  1189. clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
  1190. /* Establish new table */
  1191. pmd_populate(mm, pmd, (pte_t *) new);
  1192. /* Free old table with rcu, there might be a walker! */
  1193. page_table_free_rcu(tlb, table);
  1194. new = NULL;
  1195. }
  1196. spin_unlock(ptl);
  1197. if (new) {
  1198. page_table_free_pgste(new);
  1199. goto again;
  1200. }
  1201. } while (pmd++, addr = next, addr != end);
  1202. return addr;
  1203. }
  1204. static unsigned long page_table_realloc_pud(struct mmu_gather *tlb,
  1205. struct mm_struct *mm, pgd_t *pgd,
  1206. unsigned long addr, unsigned long end)
  1207. {
  1208. unsigned long next;
  1209. pud_t *pud;
  1210. pud = pud_offset(pgd, addr);
  1211. do {
  1212. next = pud_addr_end(addr, end);
  1213. if (pud_none_or_clear_bad(pud))
  1214. continue;
  1215. next = page_table_realloc_pmd(tlb, mm, pud, addr, next);
  1216. if (unlikely(IS_ERR_VALUE(next)))
  1217. return next;
  1218. } while (pud++, addr = next, addr != end);
  1219. return addr;
  1220. }
  1221. static unsigned long page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm,
  1222. unsigned long addr, unsigned long end)
  1223. {
  1224. unsigned long next;
  1225. pgd_t *pgd;
  1226. pgd = pgd_offset(mm, addr);
  1227. do {
  1228. next = pgd_addr_end(addr, end);
  1229. if (pgd_none_or_clear_bad(pgd))
  1230. continue;
  1231. next = page_table_realloc_pud(tlb, mm, pgd, addr, next);
  1232. if (unlikely(IS_ERR_VALUE(next)))
  1233. return next;
  1234. } while (pgd++, addr = next, addr != end);
  1235. return 0;
  1236. }
  1237. /*
  1238. * switch on pgstes for its userspace process (for kvm)
  1239. */
  1240. int s390_enable_sie(void)
  1241. {
  1242. struct task_struct *tsk = current;
  1243. struct mm_struct *mm = tsk->mm;
  1244. struct mmu_gather tlb;
  1245. /* Do we have pgstes? if yes, we are done */
  1246. if (mm_has_pgste(tsk->mm))
  1247. return 0;
  1248. down_write(&mm->mmap_sem);
  1249. /* split thp mappings and disable thp for future mappings */
  1250. thp_split_mm(mm);
  1251. /* Reallocate the page tables with pgstes */
  1252. tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE);
  1253. if (!page_table_realloc(&tlb, mm, 0, TASK_SIZE))
  1254. mm->context.has_pgste = 1;
  1255. tlb_finish_mmu(&tlb, 0, TASK_SIZE);
  1256. up_write(&mm->mmap_sem);
  1257. return mm->context.has_pgste ? 0 : -ENOMEM;
  1258. }
  1259. EXPORT_SYMBOL_GPL(s390_enable_sie);
  1260. /*
  1261. * Enable storage key handling from now on and initialize the storage
  1262. * keys with the default key.
  1263. */
  1264. void s390_enable_skey(void)
  1265. {
  1266. page_table_reset_pgste(current->mm, 0, TASK_SIZE, true);
  1267. }
  1268. EXPORT_SYMBOL_GPL(s390_enable_skey);
  1269. /*
  1270. * Test and reset if a guest page is dirty
  1271. */
  1272. bool gmap_test_and_clear_dirty(unsigned long address, struct gmap *gmap)
  1273. {
  1274. pte_t *pte;
  1275. spinlock_t *ptl;
  1276. bool dirty = false;
  1277. pte = get_locked_pte(gmap->mm, address, &ptl);
  1278. if (unlikely(!pte))
  1279. return false;
  1280. if (ptep_test_and_clear_user_dirty(gmap->mm, address, pte))
  1281. dirty = true;
  1282. spin_unlock(ptl);
  1283. return dirty;
  1284. }
  1285. EXPORT_SYMBOL_GPL(gmap_test_and_clear_dirty);
  1286. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1287. int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
  1288. pmd_t *pmdp)
  1289. {
  1290. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1291. /* No need to flush TLB
  1292. * On s390 reference bits are in storage key and never in TLB */
  1293. return pmdp_test_and_clear_young(vma, address, pmdp);
  1294. }
  1295. int pmdp_set_access_flags(struct vm_area_struct *vma,
  1296. unsigned long address, pmd_t *pmdp,
  1297. pmd_t entry, int dirty)
  1298. {
  1299. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1300. entry = pmd_mkyoung(entry);
  1301. if (dirty)
  1302. entry = pmd_mkdirty(entry);
  1303. if (pmd_same(*pmdp, entry))
  1304. return 0;
  1305. pmdp_invalidate(vma, address, pmdp);
  1306. set_pmd_at(vma->vm_mm, address, pmdp, entry);
  1307. return 1;
  1308. }
  1309. static void pmdp_splitting_flush_sync(void *arg)
  1310. {
  1311. /* Simply deliver the interrupt */
  1312. }
  1313. void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
  1314. pmd_t *pmdp)
  1315. {
  1316. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1317. if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
  1318. (unsigned long *) pmdp)) {
  1319. /* need to serialize against gup-fast (IRQ disabled) */
  1320. smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
  1321. }
  1322. }
  1323. void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  1324. pgtable_t pgtable)
  1325. {
  1326. struct list_head *lh = (struct list_head *) pgtable;
  1327. assert_spin_locked(pmd_lockptr(mm, pmdp));
  1328. /* FIFO */
  1329. if (!pmd_huge_pte(mm, pmdp))
  1330. INIT_LIST_HEAD(lh);
  1331. else
  1332. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  1333. pmd_huge_pte(mm, pmdp) = pgtable;
  1334. }
  1335. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  1336. {
  1337. struct list_head *lh;
  1338. pgtable_t pgtable;
  1339. pte_t *ptep;
  1340. assert_spin_locked(pmd_lockptr(mm, pmdp));
  1341. /* FIFO */
  1342. pgtable = pmd_huge_pte(mm, pmdp);
  1343. lh = (struct list_head *) pgtable;
  1344. if (list_empty(lh))
  1345. pmd_huge_pte(mm, pmdp) = NULL;
  1346. else {
  1347. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  1348. list_del(lh);
  1349. }
  1350. ptep = (pte_t *) pgtable;
  1351. pte_val(*ptep) = _PAGE_INVALID;
  1352. ptep++;
  1353. pte_val(*ptep) = _PAGE_INVALID;
  1354. return pgtable;
  1355. }
  1356. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */