pgtable.c 39 KB

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