pgtable.c 34 KB

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