pgtable.c 34 KB

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