pgtable.c 38 KB

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