pgtable.c 39 KB

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