pgtable.c 38 KB

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