pgtable.c 36 KB

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