pgtable.c 34 KB

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