pgtable.c 39 KB

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