pgtable.c 35 KB

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