pgtable.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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. static inline int page_table_with_pgste(struct page *page)
  768. {
  769. return atomic_read(&page->_mapcount) == 0;
  770. }
  771. static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
  772. unsigned long vmaddr)
  773. {
  774. struct page *page;
  775. unsigned long *table;
  776. struct gmap_pgtable *mp;
  777. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  778. if (!page)
  779. return NULL;
  780. mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT);
  781. if (!mp) {
  782. __free_page(page);
  783. return NULL;
  784. }
  785. if (!pgtable_page_ctor(page)) {
  786. kfree(mp);
  787. __free_page(page);
  788. return NULL;
  789. }
  790. mp->vmaddr = vmaddr & PMD_MASK;
  791. INIT_LIST_HEAD(&mp->mapper);
  792. page->index = (unsigned long) mp;
  793. atomic_set(&page->_mapcount, 0);
  794. table = (unsigned long *) page_to_phys(page);
  795. clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
  796. clear_table(table + PTRS_PER_PTE, PGSTE_HR_BIT | PGSTE_HC_BIT,
  797. 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,
  813. pmd_t *pmd, unsigned long addr, unsigned long end)
  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. pgste_set_unlock(pte, pgste);
  824. } while (pte++, addr += PAGE_SIZE, addr != end);
  825. pte_unmap_unlock(start_pte, ptl);
  826. return addr;
  827. }
  828. static inline unsigned long page_table_reset_pmd(struct mm_struct *mm,
  829. pud_t *pud, unsigned long addr, unsigned long end)
  830. {
  831. unsigned long next;
  832. pmd_t *pmd;
  833. pmd = pmd_offset(pud, addr);
  834. do {
  835. next = pmd_addr_end(addr, end);
  836. if (pmd_none_or_clear_bad(pmd))
  837. continue;
  838. next = page_table_reset_pte(mm, pmd, addr, next);
  839. } while (pmd++, addr = next, addr != end);
  840. return addr;
  841. }
  842. static inline unsigned long page_table_reset_pud(struct mm_struct *mm,
  843. pgd_t *pgd, unsigned long addr, unsigned long end)
  844. {
  845. unsigned long next;
  846. pud_t *pud;
  847. pud = pud_offset(pgd, addr);
  848. do {
  849. next = pud_addr_end(addr, end);
  850. if (pud_none_or_clear_bad(pud))
  851. continue;
  852. next = page_table_reset_pmd(mm, pud, addr, next);
  853. } while (pud++, addr = next, addr != end);
  854. return addr;
  855. }
  856. void page_table_reset_pgste(struct mm_struct *mm,
  857. unsigned long start, unsigned long end)
  858. {
  859. unsigned long addr, next;
  860. pgd_t *pgd;
  861. addr = start;
  862. down_read(&mm->mmap_sem);
  863. pgd = pgd_offset(mm, addr);
  864. do {
  865. next = pgd_addr_end(addr, end);
  866. if (pgd_none_or_clear_bad(pgd))
  867. continue;
  868. next = page_table_reset_pud(mm, pgd, addr, next);
  869. } while (pgd++, addr = next, addr != end);
  870. up_read(&mm->mmap_sem);
  871. }
  872. EXPORT_SYMBOL(page_table_reset_pgste);
  873. int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
  874. unsigned long key, bool nq)
  875. {
  876. spinlock_t *ptl;
  877. pgste_t old, new;
  878. pte_t *ptep;
  879. down_read(&mm->mmap_sem);
  880. ptep = get_locked_pte(current->mm, addr, &ptl);
  881. if (unlikely(!ptep)) {
  882. up_read(&mm->mmap_sem);
  883. return -EFAULT;
  884. }
  885. new = old = pgste_get_lock(ptep);
  886. pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
  887. PGSTE_ACC_BITS | PGSTE_FP_BIT);
  888. pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
  889. pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
  890. if (!(pte_val(*ptep) & _PAGE_INVALID)) {
  891. unsigned long address, bits, skey;
  892. address = pte_val(*ptep) & PAGE_MASK;
  893. skey = (unsigned long) page_get_storage_key(address);
  894. bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
  895. skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
  896. /* Set storage key ACC and FP */
  897. page_set_storage_key(address, skey, !nq);
  898. /* Merge host changed & referenced into pgste */
  899. pgste_val(new) |= bits << 52;
  900. }
  901. /* changing the guest storage key is considered a change of the page */
  902. if ((pgste_val(new) ^ pgste_val(old)) &
  903. (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
  904. pgste_val(new) |= PGSTE_HC_BIT;
  905. pgste_set_unlock(ptep, new);
  906. pte_unmap_unlock(*ptep, ptl);
  907. up_read(&mm->mmap_sem);
  908. return 0;
  909. }
  910. EXPORT_SYMBOL(set_guest_storage_key);
  911. #else /* CONFIG_PGSTE */
  912. static inline int page_table_with_pgste(struct page *page)
  913. {
  914. return 0;
  915. }
  916. static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
  917. unsigned long vmaddr)
  918. {
  919. return NULL;
  920. }
  921. static inline void page_table_free_pgste(unsigned long *table)
  922. {
  923. }
  924. static inline void gmap_disconnect_pgtable(struct mm_struct *mm,
  925. unsigned long *table)
  926. {
  927. }
  928. #endif /* CONFIG_PGSTE */
  929. static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
  930. {
  931. unsigned int old, new;
  932. do {
  933. old = atomic_read(v);
  934. new = old ^ bits;
  935. } while (atomic_cmpxchg(v, old, new) != old);
  936. return new;
  937. }
  938. /*
  939. * page table entry allocation/free routines.
  940. */
  941. unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr)
  942. {
  943. unsigned long *uninitialized_var(table);
  944. struct page *uninitialized_var(page);
  945. unsigned int mask, bit;
  946. if (mm_has_pgste(mm))
  947. return page_table_alloc_pgste(mm, vmaddr);
  948. /* Allocate fragments of a 4K page as 1K/2K page table */
  949. spin_lock_bh(&mm->context.list_lock);
  950. mask = FRAG_MASK;
  951. if (!list_empty(&mm->context.pgtable_list)) {
  952. page = list_first_entry(&mm->context.pgtable_list,
  953. struct page, lru);
  954. table = (unsigned long *) page_to_phys(page);
  955. mask = atomic_read(&page->_mapcount);
  956. mask = mask | (mask >> 4);
  957. }
  958. if ((mask & FRAG_MASK) == FRAG_MASK) {
  959. spin_unlock_bh(&mm->context.list_lock);
  960. page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
  961. if (!page)
  962. return NULL;
  963. if (!pgtable_page_ctor(page)) {
  964. __free_page(page);
  965. return NULL;
  966. }
  967. atomic_set(&page->_mapcount, 1);
  968. table = (unsigned long *) page_to_phys(page);
  969. clear_table(table, _PAGE_INVALID, PAGE_SIZE);
  970. spin_lock_bh(&mm->context.list_lock);
  971. list_add(&page->lru, &mm->context.pgtable_list);
  972. } else {
  973. for (bit = 1; mask & bit; bit <<= 1)
  974. table += PTRS_PER_PTE;
  975. mask = atomic_xor_bits(&page->_mapcount, bit);
  976. if ((mask & FRAG_MASK) == FRAG_MASK)
  977. list_del(&page->lru);
  978. }
  979. spin_unlock_bh(&mm->context.list_lock);
  980. return table;
  981. }
  982. void page_table_free(struct mm_struct *mm, unsigned long *table)
  983. {
  984. struct page *page;
  985. unsigned int bit, mask;
  986. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  987. if (page_table_with_pgste(page)) {
  988. gmap_disconnect_pgtable(mm, table);
  989. return page_table_free_pgste(table);
  990. }
  991. /* Free 1K/2K page table fragment of a 4K page */
  992. bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
  993. spin_lock_bh(&mm->context.list_lock);
  994. if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
  995. list_del(&page->lru);
  996. mask = atomic_xor_bits(&page->_mapcount, bit);
  997. if (mask & FRAG_MASK)
  998. list_add(&page->lru, &mm->context.pgtable_list);
  999. spin_unlock_bh(&mm->context.list_lock);
  1000. if (mask == 0) {
  1001. pgtable_page_dtor(page);
  1002. atomic_set(&page->_mapcount, -1);
  1003. __free_page(page);
  1004. }
  1005. }
  1006. static void __page_table_free_rcu(void *table, unsigned bit)
  1007. {
  1008. struct page *page;
  1009. if (bit == FRAG_MASK)
  1010. return page_table_free_pgste(table);
  1011. /* Free 1K/2K page table fragment of a 4K page */
  1012. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  1013. if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
  1014. pgtable_page_dtor(page);
  1015. atomic_set(&page->_mapcount, -1);
  1016. __free_page(page);
  1017. }
  1018. }
  1019. void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table)
  1020. {
  1021. struct mm_struct *mm;
  1022. struct page *page;
  1023. unsigned int bit, mask;
  1024. mm = tlb->mm;
  1025. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  1026. if (page_table_with_pgste(page)) {
  1027. gmap_disconnect_pgtable(mm, table);
  1028. table = (unsigned long *) (__pa(table) | FRAG_MASK);
  1029. tlb_remove_table(tlb, table);
  1030. return;
  1031. }
  1032. bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
  1033. spin_lock_bh(&mm->context.list_lock);
  1034. if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
  1035. list_del(&page->lru);
  1036. mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
  1037. if (mask & FRAG_MASK)
  1038. list_add_tail(&page->lru, &mm->context.pgtable_list);
  1039. spin_unlock_bh(&mm->context.list_lock);
  1040. table = (unsigned long *) (__pa(table) | (bit << 4));
  1041. tlb_remove_table(tlb, table);
  1042. }
  1043. static void __tlb_remove_table(void *_table)
  1044. {
  1045. const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
  1046. void *table = (void *)((unsigned long) _table & ~mask);
  1047. unsigned type = (unsigned long) _table & mask;
  1048. if (type)
  1049. __page_table_free_rcu(table, type);
  1050. else
  1051. free_pages((unsigned long) table, ALLOC_ORDER);
  1052. }
  1053. static void tlb_remove_table_smp_sync(void *arg)
  1054. {
  1055. /* Simply deliver the interrupt */
  1056. }
  1057. static void tlb_remove_table_one(void *table)
  1058. {
  1059. /*
  1060. * This isn't an RCU grace period and hence the page-tables cannot be
  1061. * assumed to be actually RCU-freed.
  1062. *
  1063. * It is however sufficient for software page-table walkers that rely
  1064. * on IRQ disabling. See the comment near struct mmu_table_batch.
  1065. */
  1066. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  1067. __tlb_remove_table(table);
  1068. }
  1069. static void tlb_remove_table_rcu(struct rcu_head *head)
  1070. {
  1071. struct mmu_table_batch *batch;
  1072. int i;
  1073. batch = container_of(head, struct mmu_table_batch, rcu);
  1074. for (i = 0; i < batch->nr; i++)
  1075. __tlb_remove_table(batch->tables[i]);
  1076. free_page((unsigned long)batch);
  1077. }
  1078. void tlb_table_flush(struct mmu_gather *tlb)
  1079. {
  1080. struct mmu_table_batch **batch = &tlb->batch;
  1081. if (*batch) {
  1082. call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
  1083. *batch = NULL;
  1084. }
  1085. }
  1086. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  1087. {
  1088. struct mmu_table_batch **batch = &tlb->batch;
  1089. tlb->mm->context.flush_mm = 1;
  1090. if (*batch == NULL) {
  1091. *batch = (struct mmu_table_batch *)
  1092. __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  1093. if (*batch == NULL) {
  1094. __tlb_flush_mm_lazy(tlb->mm);
  1095. tlb_remove_table_one(table);
  1096. return;
  1097. }
  1098. (*batch)->nr = 0;
  1099. }
  1100. (*batch)->tables[(*batch)->nr++] = table;
  1101. if ((*batch)->nr == MAX_TABLE_BATCH)
  1102. tlb_flush_mmu(tlb);
  1103. }
  1104. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1105. static inline void thp_split_vma(struct vm_area_struct *vma)
  1106. {
  1107. unsigned long addr;
  1108. for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
  1109. follow_page(vma, addr, FOLL_SPLIT);
  1110. }
  1111. static inline void thp_split_mm(struct mm_struct *mm)
  1112. {
  1113. struct vm_area_struct *vma;
  1114. for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
  1115. thp_split_vma(vma);
  1116. vma->vm_flags &= ~VM_HUGEPAGE;
  1117. vma->vm_flags |= VM_NOHUGEPAGE;
  1118. }
  1119. mm->def_flags |= VM_NOHUGEPAGE;
  1120. }
  1121. #else
  1122. static inline void thp_split_mm(struct mm_struct *mm)
  1123. {
  1124. }
  1125. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  1126. static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb,
  1127. struct mm_struct *mm, pud_t *pud,
  1128. unsigned long addr, unsigned long end)
  1129. {
  1130. unsigned long next, *table, *new;
  1131. struct page *page;
  1132. pmd_t *pmd;
  1133. pmd = pmd_offset(pud, addr);
  1134. do {
  1135. next = pmd_addr_end(addr, end);
  1136. again:
  1137. if (pmd_none_or_clear_bad(pmd))
  1138. continue;
  1139. table = (unsigned long *) pmd_deref(*pmd);
  1140. page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
  1141. if (page_table_with_pgste(page))
  1142. continue;
  1143. /* Allocate new page table with pgstes */
  1144. new = page_table_alloc_pgste(mm, addr);
  1145. if (!new)
  1146. return -ENOMEM;
  1147. spin_lock(&mm->page_table_lock);
  1148. if (likely((unsigned long *) pmd_deref(*pmd) == table)) {
  1149. /* Nuke pmd entry pointing to the "short" page table */
  1150. pmdp_flush_lazy(mm, addr, pmd);
  1151. pmd_clear(pmd);
  1152. /* Copy ptes from old table to new table */
  1153. memcpy(new, table, PAGE_SIZE/2);
  1154. clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
  1155. /* Establish new table */
  1156. pmd_populate(mm, pmd, (pte_t *) new);
  1157. /* Free old table with rcu, there might be a walker! */
  1158. page_table_free_rcu(tlb, table);
  1159. new = NULL;
  1160. }
  1161. spin_unlock(&mm->page_table_lock);
  1162. if (new) {
  1163. page_table_free_pgste(new);
  1164. goto again;
  1165. }
  1166. } while (pmd++, addr = next, addr != end);
  1167. return addr;
  1168. }
  1169. static unsigned long page_table_realloc_pud(struct mmu_gather *tlb,
  1170. struct mm_struct *mm, pgd_t *pgd,
  1171. unsigned long addr, unsigned long end)
  1172. {
  1173. unsigned long next;
  1174. pud_t *pud;
  1175. pud = pud_offset(pgd, addr);
  1176. do {
  1177. next = pud_addr_end(addr, end);
  1178. if (pud_none_or_clear_bad(pud))
  1179. continue;
  1180. next = page_table_realloc_pmd(tlb, mm, pud, addr, next);
  1181. if (unlikely(IS_ERR_VALUE(next)))
  1182. return next;
  1183. } while (pud++, addr = next, addr != end);
  1184. return addr;
  1185. }
  1186. static unsigned long page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm,
  1187. unsigned long addr, unsigned long end)
  1188. {
  1189. unsigned long next;
  1190. pgd_t *pgd;
  1191. pgd = pgd_offset(mm, addr);
  1192. do {
  1193. next = pgd_addr_end(addr, end);
  1194. if (pgd_none_or_clear_bad(pgd))
  1195. continue;
  1196. next = page_table_realloc_pud(tlb, mm, pgd, addr, next);
  1197. if (unlikely(IS_ERR_VALUE(next)))
  1198. return next;
  1199. } while (pgd++, addr = next, addr != end);
  1200. return 0;
  1201. }
  1202. /*
  1203. * switch on pgstes for its userspace process (for kvm)
  1204. */
  1205. int s390_enable_sie(void)
  1206. {
  1207. struct task_struct *tsk = current;
  1208. struct mm_struct *mm = tsk->mm;
  1209. struct mmu_gather tlb;
  1210. /* Do we have pgstes? if yes, we are done */
  1211. if (mm_has_pgste(tsk->mm))
  1212. return 0;
  1213. down_write(&mm->mmap_sem);
  1214. /* split thp mappings and disable thp for future mappings */
  1215. thp_split_mm(mm);
  1216. /* Reallocate the page tables with pgstes */
  1217. tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE);
  1218. if (!page_table_realloc(&tlb, mm, 0, TASK_SIZE))
  1219. mm->context.has_pgste = 1;
  1220. tlb_finish_mmu(&tlb, 0, TASK_SIZE);
  1221. up_write(&mm->mmap_sem);
  1222. return mm->context.has_pgste ? 0 : -ENOMEM;
  1223. }
  1224. EXPORT_SYMBOL_GPL(s390_enable_sie);
  1225. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1226. int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
  1227. pmd_t *pmdp)
  1228. {
  1229. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1230. /* No need to flush TLB
  1231. * On s390 reference bits are in storage key and never in TLB */
  1232. return pmdp_test_and_clear_young(vma, address, pmdp);
  1233. }
  1234. int pmdp_set_access_flags(struct vm_area_struct *vma,
  1235. unsigned long address, pmd_t *pmdp,
  1236. pmd_t entry, int dirty)
  1237. {
  1238. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1239. if (pmd_same(*pmdp, entry))
  1240. return 0;
  1241. pmdp_invalidate(vma, address, pmdp);
  1242. set_pmd_at(vma->vm_mm, address, pmdp, entry);
  1243. return 1;
  1244. }
  1245. static void pmdp_splitting_flush_sync(void *arg)
  1246. {
  1247. /* Simply deliver the interrupt */
  1248. }
  1249. void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
  1250. pmd_t *pmdp)
  1251. {
  1252. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1253. if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
  1254. (unsigned long *) pmdp)) {
  1255. /* need to serialize against gup-fast (IRQ disabled) */
  1256. smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
  1257. }
  1258. }
  1259. void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  1260. pgtable_t pgtable)
  1261. {
  1262. struct list_head *lh = (struct list_head *) pgtable;
  1263. assert_spin_locked(pmd_lockptr(mm, pmdp));
  1264. /* FIFO */
  1265. if (!pmd_huge_pte(mm, pmdp))
  1266. INIT_LIST_HEAD(lh);
  1267. else
  1268. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  1269. pmd_huge_pte(mm, pmdp) = pgtable;
  1270. }
  1271. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  1272. {
  1273. struct list_head *lh;
  1274. pgtable_t pgtable;
  1275. pte_t *ptep;
  1276. assert_spin_locked(pmd_lockptr(mm, pmdp));
  1277. /* FIFO */
  1278. pgtable = pmd_huge_pte(mm, pmdp);
  1279. lh = (struct list_head *) pgtable;
  1280. if (list_empty(lh))
  1281. pmd_huge_pte(mm, pmdp) = NULL;
  1282. else {
  1283. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  1284. list_del(lh);
  1285. }
  1286. ptep = (pte_t *) pgtable;
  1287. pte_val(*ptep) = _PAGE_INVALID;
  1288. ptep++;
  1289. pte_val(*ptep) = _PAGE_INVALID;
  1290. return pgtable;
  1291. }
  1292. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */