pgtable.c 39 KB

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