pgtable.c 32 KB

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