gmap.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KVM guest address space mapping code
  4. *
  5. * Copyright IBM Corp. 2007, 2016, 2018
  6. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. * David Hildenbrand <david@redhat.com>
  8. * Janosch Frank <frankja@linux.vnet.ibm.com>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/swap.h>
  13. #include <linux/smp.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/slab.h>
  16. #include <linux/swapops.h>
  17. #include <linux/ksm.h>
  18. #include <linux/mman.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/gmap.h>
  22. #include <asm/tlb.h>
  23. #define GMAP_SHADOW_FAKE_TABLE 1ULL
  24. /**
  25. * gmap_alloc - allocate and initialize a guest address space
  26. * @mm: pointer to the parent mm_struct
  27. * @limit: maximum address of the gmap address space
  28. *
  29. * Returns a guest address space structure.
  30. */
  31. static struct gmap *gmap_alloc(unsigned long limit)
  32. {
  33. struct gmap *gmap;
  34. struct page *page;
  35. unsigned long *table;
  36. unsigned long etype, atype;
  37. if (limit < _REGION3_SIZE) {
  38. limit = _REGION3_SIZE - 1;
  39. atype = _ASCE_TYPE_SEGMENT;
  40. etype = _SEGMENT_ENTRY_EMPTY;
  41. } else if (limit < _REGION2_SIZE) {
  42. limit = _REGION2_SIZE - 1;
  43. atype = _ASCE_TYPE_REGION3;
  44. etype = _REGION3_ENTRY_EMPTY;
  45. } else if (limit < _REGION1_SIZE) {
  46. limit = _REGION1_SIZE - 1;
  47. atype = _ASCE_TYPE_REGION2;
  48. etype = _REGION2_ENTRY_EMPTY;
  49. } else {
  50. limit = -1UL;
  51. atype = _ASCE_TYPE_REGION1;
  52. etype = _REGION1_ENTRY_EMPTY;
  53. }
  54. gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
  55. if (!gmap)
  56. goto out;
  57. INIT_LIST_HEAD(&gmap->crst_list);
  58. INIT_LIST_HEAD(&gmap->children);
  59. INIT_LIST_HEAD(&gmap->pt_list);
  60. INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL);
  61. INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC);
  62. INIT_RADIX_TREE(&gmap->host_to_rmap, GFP_ATOMIC);
  63. spin_lock_init(&gmap->guest_table_lock);
  64. spin_lock_init(&gmap->shadow_lock);
  65. atomic_set(&gmap->ref_count, 1);
  66. page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
  67. if (!page)
  68. goto out_free;
  69. page->index = 0;
  70. list_add(&page->lru, &gmap->crst_list);
  71. table = (unsigned long *) page_to_phys(page);
  72. crst_table_init(table, etype);
  73. gmap->table = table;
  74. gmap->asce = atype | _ASCE_TABLE_LENGTH |
  75. _ASCE_USER_BITS | __pa(table);
  76. gmap->asce_end = limit;
  77. return gmap;
  78. out_free:
  79. kfree(gmap);
  80. out:
  81. return NULL;
  82. }
  83. /**
  84. * gmap_create - create a guest address space
  85. * @mm: pointer to the parent mm_struct
  86. * @limit: maximum size of the gmap address space
  87. *
  88. * Returns a guest address space structure.
  89. */
  90. struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
  91. {
  92. struct gmap *gmap;
  93. unsigned long gmap_asce;
  94. gmap = gmap_alloc(limit);
  95. if (!gmap)
  96. return NULL;
  97. gmap->mm = mm;
  98. spin_lock(&mm->context.lock);
  99. list_add_rcu(&gmap->list, &mm->context.gmap_list);
  100. if (list_is_singular(&mm->context.gmap_list))
  101. gmap_asce = gmap->asce;
  102. else
  103. gmap_asce = -1UL;
  104. WRITE_ONCE(mm->context.gmap_asce, gmap_asce);
  105. spin_unlock(&mm->context.lock);
  106. return gmap;
  107. }
  108. EXPORT_SYMBOL_GPL(gmap_create);
  109. static void gmap_flush_tlb(struct gmap *gmap)
  110. {
  111. if (MACHINE_HAS_IDTE)
  112. __tlb_flush_idte(gmap->asce);
  113. else
  114. __tlb_flush_global();
  115. }
  116. static void gmap_radix_tree_free(struct radix_tree_root *root)
  117. {
  118. struct radix_tree_iter iter;
  119. unsigned long indices[16];
  120. unsigned long index;
  121. void __rcu **slot;
  122. int i, nr;
  123. /* A radix tree is freed by deleting all of its entries */
  124. index = 0;
  125. do {
  126. nr = 0;
  127. radix_tree_for_each_slot(slot, root, &iter, index) {
  128. indices[nr] = iter.index;
  129. if (++nr == 16)
  130. break;
  131. }
  132. for (i = 0; i < nr; i++) {
  133. index = indices[i];
  134. radix_tree_delete(root, index);
  135. }
  136. } while (nr > 0);
  137. }
  138. static void gmap_rmap_radix_tree_free(struct radix_tree_root *root)
  139. {
  140. struct gmap_rmap *rmap, *rnext, *head;
  141. struct radix_tree_iter iter;
  142. unsigned long indices[16];
  143. unsigned long index;
  144. void __rcu **slot;
  145. int i, nr;
  146. /* A radix tree is freed by deleting all of its entries */
  147. index = 0;
  148. do {
  149. nr = 0;
  150. radix_tree_for_each_slot(slot, root, &iter, index) {
  151. indices[nr] = iter.index;
  152. if (++nr == 16)
  153. break;
  154. }
  155. for (i = 0; i < nr; i++) {
  156. index = indices[i];
  157. head = radix_tree_delete(root, index);
  158. gmap_for_each_rmap_safe(rmap, rnext, head)
  159. kfree(rmap);
  160. }
  161. } while (nr > 0);
  162. }
  163. /**
  164. * gmap_free - free a guest address space
  165. * @gmap: pointer to the guest address space structure
  166. *
  167. * No locks required. There are no references to this gmap anymore.
  168. */
  169. static void gmap_free(struct gmap *gmap)
  170. {
  171. struct page *page, *next;
  172. /* Flush tlb of all gmaps (if not already done for shadows) */
  173. if (!(gmap_is_shadow(gmap) && gmap->removed))
  174. gmap_flush_tlb(gmap);
  175. /* Free all segment & region tables. */
  176. list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
  177. __free_pages(page, CRST_ALLOC_ORDER);
  178. gmap_radix_tree_free(&gmap->guest_to_host);
  179. gmap_radix_tree_free(&gmap->host_to_guest);
  180. /* Free additional data for a shadow gmap */
  181. if (gmap_is_shadow(gmap)) {
  182. /* Free all page tables. */
  183. list_for_each_entry_safe(page, next, &gmap->pt_list, lru)
  184. page_table_free_pgste(page);
  185. gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
  186. /* Release reference to the parent */
  187. gmap_put(gmap->parent);
  188. }
  189. kfree(gmap);
  190. }
  191. /**
  192. * gmap_get - increase reference counter for guest address space
  193. * @gmap: pointer to the guest address space structure
  194. *
  195. * Returns the gmap pointer
  196. */
  197. struct gmap *gmap_get(struct gmap *gmap)
  198. {
  199. atomic_inc(&gmap->ref_count);
  200. return gmap;
  201. }
  202. EXPORT_SYMBOL_GPL(gmap_get);
  203. /**
  204. * gmap_put - decrease reference counter for guest address space
  205. * @gmap: pointer to the guest address space structure
  206. *
  207. * If the reference counter reaches zero the guest address space is freed.
  208. */
  209. void gmap_put(struct gmap *gmap)
  210. {
  211. if (atomic_dec_return(&gmap->ref_count) == 0)
  212. gmap_free(gmap);
  213. }
  214. EXPORT_SYMBOL_GPL(gmap_put);
  215. /**
  216. * gmap_remove - remove a guest address space but do not free it yet
  217. * @gmap: pointer to the guest address space structure
  218. */
  219. void gmap_remove(struct gmap *gmap)
  220. {
  221. struct gmap *sg, *next;
  222. unsigned long gmap_asce;
  223. /* Remove all shadow gmaps linked to this gmap */
  224. if (!list_empty(&gmap->children)) {
  225. spin_lock(&gmap->shadow_lock);
  226. list_for_each_entry_safe(sg, next, &gmap->children, list) {
  227. list_del(&sg->list);
  228. gmap_put(sg);
  229. }
  230. spin_unlock(&gmap->shadow_lock);
  231. }
  232. /* Remove gmap from the pre-mm list */
  233. spin_lock(&gmap->mm->context.lock);
  234. list_del_rcu(&gmap->list);
  235. if (list_empty(&gmap->mm->context.gmap_list))
  236. gmap_asce = 0;
  237. else if (list_is_singular(&gmap->mm->context.gmap_list))
  238. gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
  239. struct gmap, list)->asce;
  240. else
  241. gmap_asce = -1UL;
  242. WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
  243. spin_unlock(&gmap->mm->context.lock);
  244. synchronize_rcu();
  245. /* Put reference */
  246. gmap_put(gmap);
  247. }
  248. EXPORT_SYMBOL_GPL(gmap_remove);
  249. /**
  250. * gmap_enable - switch primary space to the guest address space
  251. * @gmap: pointer to the guest address space structure
  252. */
  253. void gmap_enable(struct gmap *gmap)
  254. {
  255. S390_lowcore.gmap = (unsigned long) gmap;
  256. }
  257. EXPORT_SYMBOL_GPL(gmap_enable);
  258. /**
  259. * gmap_disable - switch back to the standard primary address space
  260. * @gmap: pointer to the guest address space structure
  261. */
  262. void gmap_disable(struct gmap *gmap)
  263. {
  264. S390_lowcore.gmap = 0UL;
  265. }
  266. EXPORT_SYMBOL_GPL(gmap_disable);
  267. /**
  268. * gmap_get_enabled - get a pointer to the currently enabled gmap
  269. *
  270. * Returns a pointer to the currently enabled gmap. 0 if none is enabled.
  271. */
  272. struct gmap *gmap_get_enabled(void)
  273. {
  274. return (struct gmap *) S390_lowcore.gmap;
  275. }
  276. EXPORT_SYMBOL_GPL(gmap_get_enabled);
  277. /*
  278. * gmap_alloc_table is assumed to be called with mmap_sem held
  279. */
  280. static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
  281. unsigned long init, unsigned long gaddr)
  282. {
  283. struct page *page;
  284. unsigned long *new;
  285. /* since we dont free the gmap table until gmap_free we can unlock */
  286. page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
  287. if (!page)
  288. return -ENOMEM;
  289. new = (unsigned long *) page_to_phys(page);
  290. crst_table_init(new, init);
  291. spin_lock(&gmap->guest_table_lock);
  292. if (*table & _REGION_ENTRY_INVALID) {
  293. list_add(&page->lru, &gmap->crst_list);
  294. *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
  295. (*table & _REGION_ENTRY_TYPE_MASK);
  296. page->index = gaddr;
  297. page = NULL;
  298. }
  299. spin_unlock(&gmap->guest_table_lock);
  300. if (page)
  301. __free_pages(page, CRST_ALLOC_ORDER);
  302. return 0;
  303. }
  304. /**
  305. * __gmap_segment_gaddr - find virtual address from segment pointer
  306. * @entry: pointer to a segment table entry in the guest address space
  307. *
  308. * Returns the virtual address in the guest address space for the segment
  309. */
  310. static unsigned long __gmap_segment_gaddr(unsigned long *entry)
  311. {
  312. struct page *page;
  313. unsigned long offset, mask;
  314. offset = (unsigned long) entry / sizeof(unsigned long);
  315. offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
  316. mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
  317. page = virt_to_page((void *)((unsigned long) entry & mask));
  318. return page->index + offset;
  319. }
  320. /**
  321. * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
  322. * @gmap: pointer to the guest address space structure
  323. * @vmaddr: address in the host process address space
  324. *
  325. * Returns 1 if a TLB flush is required
  326. */
  327. static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
  328. {
  329. unsigned long *entry;
  330. int flush = 0;
  331. BUG_ON(gmap_is_shadow(gmap));
  332. spin_lock(&gmap->guest_table_lock);
  333. entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
  334. if (entry) {
  335. flush = (*entry != _SEGMENT_ENTRY_EMPTY);
  336. *entry = _SEGMENT_ENTRY_EMPTY;
  337. }
  338. spin_unlock(&gmap->guest_table_lock);
  339. return flush;
  340. }
  341. /**
  342. * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
  343. * @gmap: pointer to the guest address space structure
  344. * @gaddr: address in the guest address space
  345. *
  346. * Returns 1 if a TLB flush is required
  347. */
  348. static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
  349. {
  350. unsigned long vmaddr;
  351. vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
  352. gaddr >> PMD_SHIFT);
  353. return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
  354. }
  355. /**
  356. * gmap_unmap_segment - unmap segment from the guest address space
  357. * @gmap: pointer to the guest address space structure
  358. * @to: address in the guest address space
  359. * @len: length of the memory area to unmap
  360. *
  361. * Returns 0 if the unmap succeeded, -EINVAL if not.
  362. */
  363. int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
  364. {
  365. unsigned long off;
  366. int flush;
  367. BUG_ON(gmap_is_shadow(gmap));
  368. if ((to | len) & (PMD_SIZE - 1))
  369. return -EINVAL;
  370. if (len == 0 || to + len < to)
  371. return -EINVAL;
  372. flush = 0;
  373. down_write(&gmap->mm->mmap_sem);
  374. for (off = 0; off < len; off += PMD_SIZE)
  375. flush |= __gmap_unmap_by_gaddr(gmap, to + off);
  376. up_write(&gmap->mm->mmap_sem);
  377. if (flush)
  378. gmap_flush_tlb(gmap);
  379. return 0;
  380. }
  381. EXPORT_SYMBOL_GPL(gmap_unmap_segment);
  382. /**
  383. * gmap_map_segment - map a segment to the guest address space
  384. * @gmap: pointer to the guest address space structure
  385. * @from: source address in the parent address space
  386. * @to: target address in the guest address space
  387. * @len: length of the memory area to map
  388. *
  389. * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
  390. */
  391. int gmap_map_segment(struct gmap *gmap, unsigned long from,
  392. unsigned long to, unsigned long len)
  393. {
  394. unsigned long off;
  395. int flush;
  396. BUG_ON(gmap_is_shadow(gmap));
  397. if ((from | to | len) & (PMD_SIZE - 1))
  398. return -EINVAL;
  399. if (len == 0 || from + len < from || to + len < to ||
  400. from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
  401. return -EINVAL;
  402. flush = 0;
  403. down_write(&gmap->mm->mmap_sem);
  404. for (off = 0; off < len; off += PMD_SIZE) {
  405. /* Remove old translation */
  406. flush |= __gmap_unmap_by_gaddr(gmap, to + off);
  407. /* Store new translation */
  408. if (radix_tree_insert(&gmap->guest_to_host,
  409. (to + off) >> PMD_SHIFT,
  410. (void *) from + off))
  411. break;
  412. }
  413. up_write(&gmap->mm->mmap_sem);
  414. if (flush)
  415. gmap_flush_tlb(gmap);
  416. if (off >= len)
  417. return 0;
  418. gmap_unmap_segment(gmap, to, len);
  419. return -ENOMEM;
  420. }
  421. EXPORT_SYMBOL_GPL(gmap_map_segment);
  422. /**
  423. * __gmap_translate - translate a guest address to a user space address
  424. * @gmap: pointer to guest mapping meta data structure
  425. * @gaddr: guest address
  426. *
  427. * Returns user space address which corresponds to the guest address or
  428. * -EFAULT if no such mapping exists.
  429. * This function does not establish potentially missing page table entries.
  430. * The mmap_sem of the mm that belongs to the address space must be held
  431. * when this function gets called.
  432. *
  433. * Note: Can also be called for shadow gmaps.
  434. */
  435. unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
  436. {
  437. unsigned long vmaddr;
  438. vmaddr = (unsigned long)
  439. radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
  440. /* Note: guest_to_host is empty for a shadow gmap */
  441. return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
  442. }
  443. EXPORT_SYMBOL_GPL(__gmap_translate);
  444. /**
  445. * gmap_translate - translate a guest address to a user space address
  446. * @gmap: pointer to guest mapping meta data structure
  447. * @gaddr: guest address
  448. *
  449. * Returns user space address which corresponds to the guest address or
  450. * -EFAULT if no such mapping exists.
  451. * This function does not establish potentially missing page table entries.
  452. */
  453. unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
  454. {
  455. unsigned long rc;
  456. down_read(&gmap->mm->mmap_sem);
  457. rc = __gmap_translate(gmap, gaddr);
  458. up_read(&gmap->mm->mmap_sem);
  459. return rc;
  460. }
  461. EXPORT_SYMBOL_GPL(gmap_translate);
  462. /**
  463. * gmap_unlink - disconnect a page table from the gmap shadow tables
  464. * @gmap: pointer to guest mapping meta data structure
  465. * @table: pointer to the host page table
  466. * @vmaddr: vm address associated with the host page table
  467. */
  468. void gmap_unlink(struct mm_struct *mm, unsigned long *table,
  469. unsigned long vmaddr)
  470. {
  471. struct gmap *gmap;
  472. int flush;
  473. rcu_read_lock();
  474. list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
  475. flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
  476. if (flush)
  477. gmap_flush_tlb(gmap);
  478. }
  479. rcu_read_unlock();
  480. }
  481. static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *old, pmd_t new,
  482. unsigned long gaddr);
  483. /**
  484. * gmap_link - set up shadow page tables to connect a host to a guest address
  485. * @gmap: pointer to guest mapping meta data structure
  486. * @gaddr: guest address
  487. * @vmaddr: vm address
  488. *
  489. * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
  490. * if the vm address is already mapped to a different guest segment.
  491. * The mmap_sem of the mm that belongs to the address space must be held
  492. * when this function gets called.
  493. */
  494. int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
  495. {
  496. struct mm_struct *mm;
  497. unsigned long *table;
  498. spinlock_t *ptl;
  499. pgd_t *pgd;
  500. p4d_t *p4d;
  501. pud_t *pud;
  502. pmd_t *pmd;
  503. u64 unprot;
  504. int rc;
  505. BUG_ON(gmap_is_shadow(gmap));
  506. /* Create higher level tables in the gmap page table */
  507. table = gmap->table;
  508. if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
  509. table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
  510. if ((*table & _REGION_ENTRY_INVALID) &&
  511. gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
  512. gaddr & _REGION1_MASK))
  513. return -ENOMEM;
  514. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  515. }
  516. if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
  517. table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
  518. if ((*table & _REGION_ENTRY_INVALID) &&
  519. gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
  520. gaddr & _REGION2_MASK))
  521. return -ENOMEM;
  522. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  523. }
  524. if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
  525. table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
  526. if ((*table & _REGION_ENTRY_INVALID) &&
  527. gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
  528. gaddr & _REGION3_MASK))
  529. return -ENOMEM;
  530. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  531. }
  532. table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
  533. /* Walk the parent mm page table */
  534. mm = gmap->mm;
  535. pgd = pgd_offset(mm, vmaddr);
  536. VM_BUG_ON(pgd_none(*pgd));
  537. p4d = p4d_offset(pgd, vmaddr);
  538. VM_BUG_ON(p4d_none(*p4d));
  539. pud = pud_offset(p4d, vmaddr);
  540. VM_BUG_ON(pud_none(*pud));
  541. /* large puds cannot yet be handled */
  542. if (pud_large(*pud))
  543. return -EFAULT;
  544. pmd = pmd_offset(pud, vmaddr);
  545. VM_BUG_ON(pmd_none(*pmd));
  546. /* Are we allowed to use huge pages? */
  547. if (pmd_large(*pmd) && !gmap->mm->context.allow_gmap_hpage_1m)
  548. return -EFAULT;
  549. /* Link gmap segment table entry location to page table. */
  550. rc = radix_tree_preload(GFP_KERNEL);
  551. if (rc)
  552. return rc;
  553. ptl = pmd_lock(mm, pmd);
  554. spin_lock(&gmap->guest_table_lock);
  555. if (*table == _SEGMENT_ENTRY_EMPTY) {
  556. rc = radix_tree_insert(&gmap->host_to_guest,
  557. vmaddr >> PMD_SHIFT, table);
  558. if (!rc) {
  559. if (pmd_large(*pmd)) {
  560. *table = (pmd_val(*pmd) &
  561. _SEGMENT_ENTRY_HARDWARE_BITS_LARGE)
  562. | _SEGMENT_ENTRY_GMAP_UC;
  563. } else
  564. *table = pmd_val(*pmd) &
  565. _SEGMENT_ENTRY_HARDWARE_BITS;
  566. }
  567. } else if (*table & _SEGMENT_ENTRY_PROTECT &&
  568. !(pmd_val(*pmd) & _SEGMENT_ENTRY_PROTECT)) {
  569. unprot = (u64)*table;
  570. unprot &= ~_SEGMENT_ENTRY_PROTECT;
  571. unprot |= _SEGMENT_ENTRY_GMAP_UC;
  572. gmap_pmdp_xchg(gmap, (pmd_t *)table, __pmd(unprot), gaddr);
  573. }
  574. spin_unlock(&gmap->guest_table_lock);
  575. spin_unlock(ptl);
  576. radix_tree_preload_end();
  577. return rc;
  578. }
  579. /**
  580. * gmap_fault - resolve a fault on a guest address
  581. * @gmap: pointer to guest mapping meta data structure
  582. * @gaddr: guest address
  583. * @fault_flags: flags to pass down to handle_mm_fault()
  584. *
  585. * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
  586. * if the vm address is already mapped to a different guest segment.
  587. */
  588. int gmap_fault(struct gmap *gmap, unsigned long gaddr,
  589. unsigned int fault_flags)
  590. {
  591. unsigned long vmaddr;
  592. int rc;
  593. bool unlocked;
  594. down_read(&gmap->mm->mmap_sem);
  595. retry:
  596. unlocked = false;
  597. vmaddr = __gmap_translate(gmap, gaddr);
  598. if (IS_ERR_VALUE(vmaddr)) {
  599. rc = vmaddr;
  600. goto out_up;
  601. }
  602. if (fixup_user_fault(current, gmap->mm, vmaddr, fault_flags,
  603. &unlocked)) {
  604. rc = -EFAULT;
  605. goto out_up;
  606. }
  607. /*
  608. * In the case that fixup_user_fault unlocked the mmap_sem during
  609. * faultin redo __gmap_translate to not race with a map/unmap_segment.
  610. */
  611. if (unlocked)
  612. goto retry;
  613. rc = __gmap_link(gmap, gaddr, vmaddr);
  614. out_up:
  615. up_read(&gmap->mm->mmap_sem);
  616. return rc;
  617. }
  618. EXPORT_SYMBOL_GPL(gmap_fault);
  619. /*
  620. * this function is assumed to be called with mmap_sem held
  621. */
  622. void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
  623. {
  624. unsigned long vmaddr;
  625. spinlock_t *ptl;
  626. pte_t *ptep;
  627. /* Find the vm address for the guest address */
  628. vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
  629. gaddr >> PMD_SHIFT);
  630. if (vmaddr) {
  631. vmaddr |= gaddr & ~PMD_MASK;
  632. /* Get pointer to the page table entry */
  633. ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
  634. if (likely(ptep))
  635. ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
  636. pte_unmap_unlock(ptep, ptl);
  637. }
  638. }
  639. EXPORT_SYMBOL_GPL(__gmap_zap);
  640. void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
  641. {
  642. unsigned long gaddr, vmaddr, size;
  643. struct vm_area_struct *vma;
  644. down_read(&gmap->mm->mmap_sem);
  645. for (gaddr = from; gaddr < to;
  646. gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
  647. /* Find the vm address for the guest address */
  648. vmaddr = (unsigned long)
  649. radix_tree_lookup(&gmap->guest_to_host,
  650. gaddr >> PMD_SHIFT);
  651. if (!vmaddr)
  652. continue;
  653. vmaddr |= gaddr & ~PMD_MASK;
  654. /* Find vma in the parent mm */
  655. vma = find_vma(gmap->mm, vmaddr);
  656. if (!vma)
  657. continue;
  658. /*
  659. * We do not discard pages that are backed by
  660. * hugetlbfs, so we don't have to refault them.
  661. */
  662. if (is_vm_hugetlb_page(vma))
  663. continue;
  664. size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
  665. zap_page_range(vma, vmaddr, size);
  666. }
  667. up_read(&gmap->mm->mmap_sem);
  668. }
  669. EXPORT_SYMBOL_GPL(gmap_discard);
  670. static LIST_HEAD(gmap_notifier_list);
  671. static DEFINE_SPINLOCK(gmap_notifier_lock);
  672. /**
  673. * gmap_register_pte_notifier - register a pte invalidation callback
  674. * @nb: pointer to the gmap notifier block
  675. */
  676. void gmap_register_pte_notifier(struct gmap_notifier *nb)
  677. {
  678. spin_lock(&gmap_notifier_lock);
  679. list_add_rcu(&nb->list, &gmap_notifier_list);
  680. spin_unlock(&gmap_notifier_lock);
  681. }
  682. EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
  683. /**
  684. * gmap_unregister_pte_notifier - remove a pte invalidation callback
  685. * @nb: pointer to the gmap notifier block
  686. */
  687. void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
  688. {
  689. spin_lock(&gmap_notifier_lock);
  690. list_del_rcu(&nb->list);
  691. spin_unlock(&gmap_notifier_lock);
  692. synchronize_rcu();
  693. }
  694. EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
  695. /**
  696. * gmap_call_notifier - call all registered invalidation callbacks
  697. * @gmap: pointer to guest mapping meta data structure
  698. * @start: start virtual address in the guest address space
  699. * @end: end virtual address in the guest address space
  700. */
  701. static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
  702. unsigned long end)
  703. {
  704. struct gmap_notifier *nb;
  705. list_for_each_entry(nb, &gmap_notifier_list, list)
  706. nb->notifier_call(gmap, start, end);
  707. }
  708. /**
  709. * gmap_table_walk - walk the gmap page tables
  710. * @gmap: pointer to guest mapping meta data structure
  711. * @gaddr: virtual address in the guest address space
  712. * @level: page table level to stop at
  713. *
  714. * Returns a table entry pointer for the given guest address and @level
  715. * @level=0 : returns a pointer to a page table table entry (or NULL)
  716. * @level=1 : returns a pointer to a segment table entry (or NULL)
  717. * @level=2 : returns a pointer to a region-3 table entry (or NULL)
  718. * @level=3 : returns a pointer to a region-2 table entry (or NULL)
  719. * @level=4 : returns a pointer to a region-1 table entry (or NULL)
  720. *
  721. * Returns NULL if the gmap page tables could not be walked to the
  722. * requested level.
  723. *
  724. * Note: Can also be called for shadow gmaps.
  725. */
  726. static inline unsigned long *gmap_table_walk(struct gmap *gmap,
  727. unsigned long gaddr, int level)
  728. {
  729. unsigned long *table;
  730. if ((gmap->asce & _ASCE_TYPE_MASK) + 4 < (level * 4))
  731. return NULL;
  732. if (gmap_is_shadow(gmap) && gmap->removed)
  733. return NULL;
  734. if (gaddr & (-1UL << (31 + ((gmap->asce & _ASCE_TYPE_MASK) >> 2)*11)))
  735. return NULL;
  736. table = gmap->table;
  737. switch (gmap->asce & _ASCE_TYPE_MASK) {
  738. case _ASCE_TYPE_REGION1:
  739. table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
  740. if (level == 4)
  741. break;
  742. if (*table & _REGION_ENTRY_INVALID)
  743. return NULL;
  744. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  745. /* Fallthrough */
  746. case _ASCE_TYPE_REGION2:
  747. table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
  748. if (level == 3)
  749. break;
  750. if (*table & _REGION_ENTRY_INVALID)
  751. return NULL;
  752. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  753. /* Fallthrough */
  754. case _ASCE_TYPE_REGION3:
  755. table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
  756. if (level == 2)
  757. break;
  758. if (*table & _REGION_ENTRY_INVALID)
  759. return NULL;
  760. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  761. /* Fallthrough */
  762. case _ASCE_TYPE_SEGMENT:
  763. table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
  764. if (level == 1)
  765. break;
  766. if (*table & _REGION_ENTRY_INVALID)
  767. return NULL;
  768. table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
  769. table += (gaddr & _PAGE_INDEX) >> _PAGE_SHIFT;
  770. }
  771. return table;
  772. }
  773. /**
  774. * gmap_pte_op_walk - walk the gmap page table, get the page table lock
  775. * and return the pte pointer
  776. * @gmap: pointer to guest mapping meta data structure
  777. * @gaddr: virtual address in the guest address space
  778. * @ptl: pointer to the spinlock pointer
  779. *
  780. * Returns a pointer to the locked pte for a guest address, or NULL
  781. */
  782. static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
  783. spinlock_t **ptl)
  784. {
  785. unsigned long *table;
  786. BUG_ON(gmap_is_shadow(gmap));
  787. /* Walk the gmap page table, lock and get pte pointer */
  788. table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
  789. if (!table || *table & _SEGMENT_ENTRY_INVALID)
  790. return NULL;
  791. return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
  792. }
  793. /**
  794. * gmap_pte_op_fixup - force a page in and connect the gmap page table
  795. * @gmap: pointer to guest mapping meta data structure
  796. * @gaddr: virtual address in the guest address space
  797. * @vmaddr: address in the host process address space
  798. * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
  799. *
  800. * Returns 0 if the caller can retry __gmap_translate (might fail again),
  801. * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
  802. * up or connecting the gmap page table.
  803. */
  804. static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
  805. unsigned long vmaddr, int prot)
  806. {
  807. struct mm_struct *mm = gmap->mm;
  808. unsigned int fault_flags;
  809. bool unlocked = false;
  810. BUG_ON(gmap_is_shadow(gmap));
  811. fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
  812. if (fixup_user_fault(current, mm, vmaddr, fault_flags, &unlocked))
  813. return -EFAULT;
  814. if (unlocked)
  815. /* lost mmap_sem, caller has to retry __gmap_translate */
  816. return 0;
  817. /* Connect the page tables */
  818. return __gmap_link(gmap, gaddr, vmaddr);
  819. }
  820. /**
  821. * gmap_pte_op_end - release the page table lock
  822. * @ptl: pointer to the spinlock pointer
  823. */
  824. static void gmap_pte_op_end(spinlock_t *ptl)
  825. {
  826. if (ptl)
  827. spin_unlock(ptl);
  828. }
  829. /**
  830. * gmap_pmd_op_walk - walk the gmap tables, get the guest table lock
  831. * and return the pmd pointer
  832. * @gmap: pointer to guest mapping meta data structure
  833. * @gaddr: virtual address in the guest address space
  834. *
  835. * Returns a pointer to the pmd for a guest address, or NULL
  836. */
  837. static inline pmd_t *gmap_pmd_op_walk(struct gmap *gmap, unsigned long gaddr)
  838. {
  839. pmd_t *pmdp;
  840. BUG_ON(gmap_is_shadow(gmap));
  841. spin_lock(&gmap->guest_table_lock);
  842. pmdp = (pmd_t *) gmap_table_walk(gmap, gaddr, 1);
  843. if (!pmdp || pmd_none(*pmdp)) {
  844. spin_unlock(&gmap->guest_table_lock);
  845. return NULL;
  846. }
  847. /* 4k page table entries are locked via the pte (pte_alloc_map_lock). */
  848. if (!pmd_large(*pmdp))
  849. spin_unlock(&gmap->guest_table_lock);
  850. return pmdp;
  851. }
  852. /**
  853. * gmap_pmd_op_end - release the guest_table_lock if needed
  854. * @gmap: pointer to the guest mapping meta data structure
  855. * @pmdp: pointer to the pmd
  856. */
  857. static inline void gmap_pmd_op_end(struct gmap *gmap, pmd_t *pmdp)
  858. {
  859. if (pmd_large(*pmdp))
  860. spin_unlock(&gmap->guest_table_lock);
  861. }
  862. /*
  863. * gmap_protect_pmd - remove access rights to memory and set pmd notification bits
  864. * @pmdp: pointer to the pmd to be protected
  865. * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
  866. * @bits: notification bits to set
  867. *
  868. * Returns:
  869. * 0 if successfully protected
  870. * -EAGAIN if a fixup is needed
  871. * -EINVAL if unsupported notifier bits have been specified
  872. *
  873. * Expected to be called with sg->mm->mmap_sem in read and
  874. * guest_table_lock held.
  875. */
  876. static int gmap_protect_pmd(struct gmap *gmap, unsigned long gaddr,
  877. pmd_t *pmdp, int prot, unsigned long bits)
  878. {
  879. int pmd_i = pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID;
  880. int pmd_p = pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT;
  881. pmd_t new = *pmdp;
  882. /* Fixup needed */
  883. if ((pmd_i && (prot != PROT_NONE)) || (pmd_p && (prot == PROT_WRITE)))
  884. return -EAGAIN;
  885. if (prot == PROT_NONE && !pmd_i) {
  886. pmd_val(new) |= _SEGMENT_ENTRY_INVALID;
  887. gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
  888. }
  889. if (prot == PROT_READ && !pmd_p) {
  890. pmd_val(new) &= ~_SEGMENT_ENTRY_INVALID;
  891. pmd_val(new) |= _SEGMENT_ENTRY_PROTECT;
  892. gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
  893. }
  894. if (bits & GMAP_NOTIFY_MPROT)
  895. pmd_val(*pmdp) |= _SEGMENT_ENTRY_GMAP_IN;
  896. /* Shadow GMAP protection needs split PMDs */
  897. if (bits & GMAP_NOTIFY_SHADOW)
  898. return -EINVAL;
  899. return 0;
  900. }
  901. /*
  902. * gmap_protect_pte - remove access rights to memory and set pgste bits
  903. * @gmap: pointer to guest mapping meta data structure
  904. * @gaddr: virtual address in the guest address space
  905. * @pmdp: pointer to the pmd associated with the pte
  906. * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
  907. * @bits: notification bits to set
  908. *
  909. * Returns 0 if successfully protected, -ENOMEM if out of memory and
  910. * -EAGAIN if a fixup is needed.
  911. *
  912. * Expected to be called with sg->mm->mmap_sem in read
  913. */
  914. static int gmap_protect_pte(struct gmap *gmap, unsigned long gaddr,
  915. pmd_t *pmdp, int prot, unsigned long bits)
  916. {
  917. int rc;
  918. pte_t *ptep;
  919. spinlock_t *ptl = NULL;
  920. unsigned long pbits = 0;
  921. if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
  922. return -EAGAIN;
  923. ptep = pte_alloc_map_lock(gmap->mm, pmdp, gaddr, &ptl);
  924. if (!ptep)
  925. return -ENOMEM;
  926. pbits |= (bits & GMAP_NOTIFY_MPROT) ? PGSTE_IN_BIT : 0;
  927. pbits |= (bits & GMAP_NOTIFY_SHADOW) ? PGSTE_VSIE_BIT : 0;
  928. /* Protect and unlock. */
  929. rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, pbits);
  930. gmap_pte_op_end(ptl);
  931. return rc;
  932. }
  933. /*
  934. * gmap_protect_range - remove access rights to memory and set pgste bits
  935. * @gmap: pointer to guest mapping meta data structure
  936. * @gaddr: virtual address in the guest address space
  937. * @len: size of area
  938. * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
  939. * @bits: pgste notification bits to set
  940. *
  941. * Returns 0 if successfully protected, -ENOMEM if out of memory and
  942. * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
  943. *
  944. * Called with sg->mm->mmap_sem in read.
  945. */
  946. static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
  947. unsigned long len, int prot, unsigned long bits)
  948. {
  949. unsigned long vmaddr, dist;
  950. pmd_t *pmdp;
  951. int rc;
  952. BUG_ON(gmap_is_shadow(gmap));
  953. while (len) {
  954. rc = -EAGAIN;
  955. pmdp = gmap_pmd_op_walk(gmap, gaddr);
  956. if (pmdp) {
  957. if (!pmd_large(*pmdp)) {
  958. rc = gmap_protect_pte(gmap, gaddr, pmdp, prot,
  959. bits);
  960. if (!rc) {
  961. len -= PAGE_SIZE;
  962. gaddr += PAGE_SIZE;
  963. }
  964. } else {
  965. rc = gmap_protect_pmd(gmap, gaddr, pmdp, prot,
  966. bits);
  967. if (!rc) {
  968. dist = HPAGE_SIZE - (gaddr & ~HPAGE_MASK);
  969. len = len < dist ? 0 : len - dist;
  970. gaddr = (gaddr & HPAGE_MASK) + HPAGE_SIZE;
  971. }
  972. }
  973. gmap_pmd_op_end(gmap, pmdp);
  974. }
  975. if (rc) {
  976. if (rc == -EINVAL)
  977. return rc;
  978. /* -EAGAIN, fixup of userspace mm and gmap */
  979. vmaddr = __gmap_translate(gmap, gaddr);
  980. if (IS_ERR_VALUE(vmaddr))
  981. return vmaddr;
  982. rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
  983. if (rc)
  984. return rc;
  985. }
  986. }
  987. return 0;
  988. }
  989. /**
  990. * gmap_mprotect_notify - change access rights for a range of ptes and
  991. * call the notifier if any pte changes again
  992. * @gmap: pointer to guest mapping meta data structure
  993. * @gaddr: virtual address in the guest address space
  994. * @len: size of area
  995. * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
  996. *
  997. * Returns 0 if for each page in the given range a gmap mapping exists,
  998. * the new access rights could be set and the notifier could be armed.
  999. * If the gmap mapping is missing for one or more pages -EFAULT is
  1000. * returned. If no memory could be allocated -ENOMEM is returned.
  1001. * This function establishes missing page table entries.
  1002. */
  1003. int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
  1004. unsigned long len, int prot)
  1005. {
  1006. int rc;
  1007. if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
  1008. return -EINVAL;
  1009. if (!MACHINE_HAS_ESOP && prot == PROT_READ)
  1010. return -EINVAL;
  1011. down_read(&gmap->mm->mmap_sem);
  1012. rc = gmap_protect_range(gmap, gaddr, len, prot, GMAP_NOTIFY_MPROT);
  1013. up_read(&gmap->mm->mmap_sem);
  1014. return rc;
  1015. }
  1016. EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
  1017. /**
  1018. * gmap_read_table - get an unsigned long value from a guest page table using
  1019. * absolute addressing, without marking the page referenced.
  1020. * @gmap: pointer to guest mapping meta data structure
  1021. * @gaddr: virtual address in the guest address space
  1022. * @val: pointer to the unsigned long value to return
  1023. *
  1024. * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
  1025. * if reading using the virtual address failed. -EINVAL if called on a gmap
  1026. * shadow.
  1027. *
  1028. * Called with gmap->mm->mmap_sem in read.
  1029. */
  1030. int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
  1031. {
  1032. unsigned long address, vmaddr;
  1033. spinlock_t *ptl;
  1034. pte_t *ptep, pte;
  1035. int rc;
  1036. if (gmap_is_shadow(gmap))
  1037. return -EINVAL;
  1038. while (1) {
  1039. rc = -EAGAIN;
  1040. ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
  1041. if (ptep) {
  1042. pte = *ptep;
  1043. if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
  1044. address = pte_val(pte) & PAGE_MASK;
  1045. address += gaddr & ~PAGE_MASK;
  1046. *val = *(unsigned long *) address;
  1047. pte_val(*ptep) |= _PAGE_YOUNG;
  1048. /* Do *NOT* clear the _PAGE_INVALID bit! */
  1049. rc = 0;
  1050. }
  1051. gmap_pte_op_end(ptl);
  1052. }
  1053. if (!rc)
  1054. break;
  1055. vmaddr = __gmap_translate(gmap, gaddr);
  1056. if (IS_ERR_VALUE(vmaddr)) {
  1057. rc = vmaddr;
  1058. break;
  1059. }
  1060. rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
  1061. if (rc)
  1062. break;
  1063. }
  1064. return rc;
  1065. }
  1066. EXPORT_SYMBOL_GPL(gmap_read_table);
  1067. /**
  1068. * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
  1069. * @sg: pointer to the shadow guest address space structure
  1070. * @vmaddr: vm address associated with the rmap
  1071. * @rmap: pointer to the rmap structure
  1072. *
  1073. * Called with the sg->guest_table_lock
  1074. */
  1075. static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
  1076. struct gmap_rmap *rmap)
  1077. {
  1078. void __rcu **slot;
  1079. BUG_ON(!gmap_is_shadow(sg));
  1080. slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
  1081. if (slot) {
  1082. rmap->next = radix_tree_deref_slot_protected(slot,
  1083. &sg->guest_table_lock);
  1084. radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
  1085. } else {
  1086. rmap->next = NULL;
  1087. radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
  1088. rmap);
  1089. }
  1090. }
  1091. /**
  1092. * gmap_protect_rmap - restrict access rights to memory (RO) and create an rmap
  1093. * @sg: pointer to the shadow guest address space structure
  1094. * @raddr: rmap address in the shadow gmap
  1095. * @paddr: address in the parent guest address space
  1096. * @len: length of the memory area to protect
  1097. *
  1098. * Returns 0 if successfully protected and the rmap was created, -ENOMEM
  1099. * if out of memory and -EFAULT if paddr is invalid.
  1100. */
  1101. static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
  1102. unsigned long paddr, unsigned long len)
  1103. {
  1104. struct gmap *parent;
  1105. struct gmap_rmap *rmap;
  1106. unsigned long vmaddr;
  1107. spinlock_t *ptl;
  1108. pte_t *ptep;
  1109. int rc;
  1110. BUG_ON(!gmap_is_shadow(sg));
  1111. parent = sg->parent;
  1112. while (len) {
  1113. vmaddr = __gmap_translate(parent, paddr);
  1114. if (IS_ERR_VALUE(vmaddr))
  1115. return vmaddr;
  1116. rmap = kzalloc(sizeof(*rmap), GFP_KERNEL);
  1117. if (!rmap)
  1118. return -ENOMEM;
  1119. rmap->raddr = raddr;
  1120. rc = radix_tree_preload(GFP_KERNEL);
  1121. if (rc) {
  1122. kfree(rmap);
  1123. return rc;
  1124. }
  1125. rc = -EAGAIN;
  1126. ptep = gmap_pte_op_walk(parent, paddr, &ptl);
  1127. if (ptep) {
  1128. spin_lock(&sg->guest_table_lock);
  1129. rc = ptep_force_prot(parent->mm, paddr, ptep, PROT_READ,
  1130. PGSTE_VSIE_BIT);
  1131. if (!rc)
  1132. gmap_insert_rmap(sg, vmaddr, rmap);
  1133. spin_unlock(&sg->guest_table_lock);
  1134. gmap_pte_op_end(ptl);
  1135. }
  1136. radix_tree_preload_end();
  1137. if (rc) {
  1138. kfree(rmap);
  1139. rc = gmap_pte_op_fixup(parent, paddr, vmaddr, PROT_READ);
  1140. if (rc)
  1141. return rc;
  1142. continue;
  1143. }
  1144. paddr += PAGE_SIZE;
  1145. len -= PAGE_SIZE;
  1146. }
  1147. return 0;
  1148. }
  1149. #define _SHADOW_RMAP_MASK 0x7
  1150. #define _SHADOW_RMAP_REGION1 0x5
  1151. #define _SHADOW_RMAP_REGION2 0x4
  1152. #define _SHADOW_RMAP_REGION3 0x3
  1153. #define _SHADOW_RMAP_SEGMENT 0x2
  1154. #define _SHADOW_RMAP_PGTABLE 0x1
  1155. /**
  1156. * gmap_idte_one - invalidate a single region or segment table entry
  1157. * @asce: region or segment table *origin* + table-type bits
  1158. * @vaddr: virtual address to identify the table entry to flush
  1159. *
  1160. * The invalid bit of a single region or segment table entry is set
  1161. * and the associated TLB entries depending on the entry are flushed.
  1162. * The table-type of the @asce identifies the portion of the @vaddr
  1163. * that is used as the invalidation index.
  1164. */
  1165. static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
  1166. {
  1167. asm volatile(
  1168. " .insn rrf,0xb98e0000,%0,%1,0,0"
  1169. : : "a" (asce), "a" (vaddr) : "cc", "memory");
  1170. }
  1171. /**
  1172. * gmap_unshadow_page - remove a page from a shadow page table
  1173. * @sg: pointer to the shadow guest address space structure
  1174. * @raddr: rmap address in the shadow guest address space
  1175. *
  1176. * Called with the sg->guest_table_lock
  1177. */
  1178. static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
  1179. {
  1180. unsigned long *table;
  1181. BUG_ON(!gmap_is_shadow(sg));
  1182. table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
  1183. if (!table || *table & _PAGE_INVALID)
  1184. return;
  1185. gmap_call_notifier(sg, raddr, raddr + _PAGE_SIZE - 1);
  1186. ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
  1187. }
  1188. /**
  1189. * __gmap_unshadow_pgt - remove all entries from a shadow page table
  1190. * @sg: pointer to the shadow guest address space structure
  1191. * @raddr: rmap address in the shadow guest address space
  1192. * @pgt: pointer to the start of a shadow page table
  1193. *
  1194. * Called with the sg->guest_table_lock
  1195. */
  1196. static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
  1197. unsigned long *pgt)
  1198. {
  1199. int i;
  1200. BUG_ON(!gmap_is_shadow(sg));
  1201. for (i = 0; i < _PAGE_ENTRIES; i++, raddr += _PAGE_SIZE)
  1202. pgt[i] = _PAGE_INVALID;
  1203. }
  1204. /**
  1205. * gmap_unshadow_pgt - remove a shadow page table from a segment entry
  1206. * @sg: pointer to the shadow guest address space structure
  1207. * @raddr: address in the shadow guest address space
  1208. *
  1209. * Called with the sg->guest_table_lock
  1210. */
  1211. static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
  1212. {
  1213. unsigned long sto, *ste, *pgt;
  1214. struct page *page;
  1215. BUG_ON(!gmap_is_shadow(sg));
  1216. ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
  1217. if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
  1218. return;
  1219. gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
  1220. sto = (unsigned long) (ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
  1221. gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
  1222. pgt = (unsigned long *)(*ste & _SEGMENT_ENTRY_ORIGIN);
  1223. *ste = _SEGMENT_ENTRY_EMPTY;
  1224. __gmap_unshadow_pgt(sg, raddr, pgt);
  1225. /* Free page table */
  1226. page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
  1227. list_del(&page->lru);
  1228. page_table_free_pgste(page);
  1229. }
  1230. /**
  1231. * __gmap_unshadow_sgt - remove all entries from a shadow segment table
  1232. * @sg: pointer to the shadow guest address space structure
  1233. * @raddr: rmap address in the shadow guest address space
  1234. * @sgt: pointer to the start of a shadow segment table
  1235. *
  1236. * Called with the sg->guest_table_lock
  1237. */
  1238. static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
  1239. unsigned long *sgt)
  1240. {
  1241. unsigned long *pgt;
  1242. struct page *page;
  1243. int i;
  1244. BUG_ON(!gmap_is_shadow(sg));
  1245. for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
  1246. if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
  1247. continue;
  1248. pgt = (unsigned long *)(sgt[i] & _REGION_ENTRY_ORIGIN);
  1249. sgt[i] = _SEGMENT_ENTRY_EMPTY;
  1250. __gmap_unshadow_pgt(sg, raddr, pgt);
  1251. /* Free page table */
  1252. page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
  1253. list_del(&page->lru);
  1254. page_table_free_pgste(page);
  1255. }
  1256. }
  1257. /**
  1258. * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
  1259. * @sg: pointer to the shadow guest address space structure
  1260. * @raddr: rmap address in the shadow guest address space
  1261. *
  1262. * Called with the shadow->guest_table_lock
  1263. */
  1264. static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
  1265. {
  1266. unsigned long r3o, *r3e, *sgt;
  1267. struct page *page;
  1268. BUG_ON(!gmap_is_shadow(sg));
  1269. r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
  1270. if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
  1271. return;
  1272. gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
  1273. r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
  1274. gmap_idte_one(r3o | _ASCE_TYPE_REGION3, raddr);
  1275. sgt = (unsigned long *)(*r3e & _REGION_ENTRY_ORIGIN);
  1276. *r3e = _REGION3_ENTRY_EMPTY;
  1277. __gmap_unshadow_sgt(sg, raddr, sgt);
  1278. /* Free segment table */
  1279. page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
  1280. list_del(&page->lru);
  1281. __free_pages(page, CRST_ALLOC_ORDER);
  1282. }
  1283. /**
  1284. * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
  1285. * @sg: pointer to the shadow guest address space structure
  1286. * @raddr: address in the shadow guest address space
  1287. * @r3t: pointer to the start of a shadow region-3 table
  1288. *
  1289. * Called with the sg->guest_table_lock
  1290. */
  1291. static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
  1292. unsigned long *r3t)
  1293. {
  1294. unsigned long *sgt;
  1295. struct page *page;
  1296. int i;
  1297. BUG_ON(!gmap_is_shadow(sg));
  1298. for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
  1299. if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
  1300. continue;
  1301. sgt = (unsigned long *)(r3t[i] & _REGION_ENTRY_ORIGIN);
  1302. r3t[i] = _REGION3_ENTRY_EMPTY;
  1303. __gmap_unshadow_sgt(sg, raddr, sgt);
  1304. /* Free segment table */
  1305. page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
  1306. list_del(&page->lru);
  1307. __free_pages(page, CRST_ALLOC_ORDER);
  1308. }
  1309. }
  1310. /**
  1311. * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
  1312. * @sg: pointer to the shadow guest address space structure
  1313. * @raddr: rmap address in the shadow guest address space
  1314. *
  1315. * Called with the sg->guest_table_lock
  1316. */
  1317. static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
  1318. {
  1319. unsigned long r2o, *r2e, *r3t;
  1320. struct page *page;
  1321. BUG_ON(!gmap_is_shadow(sg));
  1322. r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
  1323. if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
  1324. return;
  1325. gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
  1326. r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
  1327. gmap_idte_one(r2o | _ASCE_TYPE_REGION2, raddr);
  1328. r3t = (unsigned long *)(*r2e & _REGION_ENTRY_ORIGIN);
  1329. *r2e = _REGION2_ENTRY_EMPTY;
  1330. __gmap_unshadow_r3t(sg, raddr, r3t);
  1331. /* Free region 3 table */
  1332. page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
  1333. list_del(&page->lru);
  1334. __free_pages(page, CRST_ALLOC_ORDER);
  1335. }
  1336. /**
  1337. * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
  1338. * @sg: pointer to the shadow guest address space structure
  1339. * @raddr: rmap address in the shadow guest address space
  1340. * @r2t: pointer to the start of a shadow region-2 table
  1341. *
  1342. * Called with the sg->guest_table_lock
  1343. */
  1344. static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
  1345. unsigned long *r2t)
  1346. {
  1347. unsigned long *r3t;
  1348. struct page *page;
  1349. int i;
  1350. BUG_ON(!gmap_is_shadow(sg));
  1351. for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
  1352. if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
  1353. continue;
  1354. r3t = (unsigned long *)(r2t[i] & _REGION_ENTRY_ORIGIN);
  1355. r2t[i] = _REGION2_ENTRY_EMPTY;
  1356. __gmap_unshadow_r3t(sg, raddr, r3t);
  1357. /* Free region 3 table */
  1358. page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
  1359. list_del(&page->lru);
  1360. __free_pages(page, CRST_ALLOC_ORDER);
  1361. }
  1362. }
  1363. /**
  1364. * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
  1365. * @sg: pointer to the shadow guest address space structure
  1366. * @raddr: rmap address in the shadow guest address space
  1367. *
  1368. * Called with the sg->guest_table_lock
  1369. */
  1370. static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
  1371. {
  1372. unsigned long r1o, *r1e, *r2t;
  1373. struct page *page;
  1374. BUG_ON(!gmap_is_shadow(sg));
  1375. r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
  1376. if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
  1377. return;
  1378. gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
  1379. r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
  1380. gmap_idte_one(r1o | _ASCE_TYPE_REGION1, raddr);
  1381. r2t = (unsigned long *)(*r1e & _REGION_ENTRY_ORIGIN);
  1382. *r1e = _REGION1_ENTRY_EMPTY;
  1383. __gmap_unshadow_r2t(sg, raddr, r2t);
  1384. /* Free region 2 table */
  1385. page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
  1386. list_del(&page->lru);
  1387. __free_pages(page, CRST_ALLOC_ORDER);
  1388. }
  1389. /**
  1390. * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
  1391. * @sg: pointer to the shadow guest address space structure
  1392. * @raddr: rmap address in the shadow guest address space
  1393. * @r1t: pointer to the start of a shadow region-1 table
  1394. *
  1395. * Called with the shadow->guest_table_lock
  1396. */
  1397. static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
  1398. unsigned long *r1t)
  1399. {
  1400. unsigned long asce, *r2t;
  1401. struct page *page;
  1402. int i;
  1403. BUG_ON(!gmap_is_shadow(sg));
  1404. asce = (unsigned long) r1t | _ASCE_TYPE_REGION1;
  1405. for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
  1406. if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
  1407. continue;
  1408. r2t = (unsigned long *)(r1t[i] & _REGION_ENTRY_ORIGIN);
  1409. __gmap_unshadow_r2t(sg, raddr, r2t);
  1410. /* Clear entry and flush translation r1t -> r2t */
  1411. gmap_idte_one(asce, raddr);
  1412. r1t[i] = _REGION1_ENTRY_EMPTY;
  1413. /* Free region 2 table */
  1414. page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
  1415. list_del(&page->lru);
  1416. __free_pages(page, CRST_ALLOC_ORDER);
  1417. }
  1418. }
  1419. /**
  1420. * gmap_unshadow - remove a shadow page table completely
  1421. * @sg: pointer to the shadow guest address space structure
  1422. *
  1423. * Called with sg->guest_table_lock
  1424. */
  1425. static void gmap_unshadow(struct gmap *sg)
  1426. {
  1427. unsigned long *table;
  1428. BUG_ON(!gmap_is_shadow(sg));
  1429. if (sg->removed)
  1430. return;
  1431. sg->removed = 1;
  1432. gmap_call_notifier(sg, 0, -1UL);
  1433. gmap_flush_tlb(sg);
  1434. table = (unsigned long *)(sg->asce & _ASCE_ORIGIN);
  1435. switch (sg->asce & _ASCE_TYPE_MASK) {
  1436. case _ASCE_TYPE_REGION1:
  1437. __gmap_unshadow_r1t(sg, 0, table);
  1438. break;
  1439. case _ASCE_TYPE_REGION2:
  1440. __gmap_unshadow_r2t(sg, 0, table);
  1441. break;
  1442. case _ASCE_TYPE_REGION3:
  1443. __gmap_unshadow_r3t(sg, 0, table);
  1444. break;
  1445. case _ASCE_TYPE_SEGMENT:
  1446. __gmap_unshadow_sgt(sg, 0, table);
  1447. break;
  1448. }
  1449. }
  1450. /**
  1451. * gmap_find_shadow - find a specific asce in the list of shadow tables
  1452. * @parent: pointer to the parent gmap
  1453. * @asce: ASCE for which the shadow table is created
  1454. * @edat_level: edat level to be used for the shadow translation
  1455. *
  1456. * Returns the pointer to a gmap if a shadow table with the given asce is
  1457. * already available, ERR_PTR(-EAGAIN) if another one is just being created,
  1458. * otherwise NULL
  1459. */
  1460. static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
  1461. int edat_level)
  1462. {
  1463. struct gmap *sg;
  1464. list_for_each_entry(sg, &parent->children, list) {
  1465. if (sg->orig_asce != asce || sg->edat_level != edat_level ||
  1466. sg->removed)
  1467. continue;
  1468. if (!sg->initialized)
  1469. return ERR_PTR(-EAGAIN);
  1470. atomic_inc(&sg->ref_count);
  1471. return sg;
  1472. }
  1473. return NULL;
  1474. }
  1475. /**
  1476. * gmap_shadow_valid - check if a shadow guest address space matches the
  1477. * given properties and is still valid
  1478. * @sg: pointer to the shadow guest address space structure
  1479. * @asce: ASCE for which the shadow table is requested
  1480. * @edat_level: edat level to be used for the shadow translation
  1481. *
  1482. * Returns 1 if the gmap shadow is still valid and matches the given
  1483. * properties, the caller can continue using it. Returns 0 otherwise, the
  1484. * caller has to request a new shadow gmap in this case.
  1485. *
  1486. */
  1487. int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
  1488. {
  1489. if (sg->removed)
  1490. return 0;
  1491. return sg->orig_asce == asce && sg->edat_level == edat_level;
  1492. }
  1493. EXPORT_SYMBOL_GPL(gmap_shadow_valid);
  1494. /**
  1495. * gmap_shadow - create/find a shadow guest address space
  1496. * @parent: pointer to the parent gmap
  1497. * @asce: ASCE for which the shadow table is created
  1498. * @edat_level: edat level to be used for the shadow translation
  1499. *
  1500. * The pages of the top level page table referred by the asce parameter
  1501. * will be set to read-only and marked in the PGSTEs of the kvm process.
  1502. * The shadow table will be removed automatically on any change to the
  1503. * PTE mapping for the source table.
  1504. *
  1505. * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
  1506. * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
  1507. * parent gmap table could not be protected.
  1508. */
  1509. struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
  1510. int edat_level)
  1511. {
  1512. struct gmap *sg, *new;
  1513. unsigned long limit;
  1514. int rc;
  1515. BUG_ON(parent->mm->context.allow_gmap_hpage_1m);
  1516. BUG_ON(gmap_is_shadow(parent));
  1517. spin_lock(&parent->shadow_lock);
  1518. sg = gmap_find_shadow(parent, asce, edat_level);
  1519. spin_unlock(&parent->shadow_lock);
  1520. if (sg)
  1521. return sg;
  1522. /* Create a new shadow gmap */
  1523. limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
  1524. if (asce & _ASCE_REAL_SPACE)
  1525. limit = -1UL;
  1526. new = gmap_alloc(limit);
  1527. if (!new)
  1528. return ERR_PTR(-ENOMEM);
  1529. new->mm = parent->mm;
  1530. new->parent = gmap_get(parent);
  1531. new->orig_asce = asce;
  1532. new->edat_level = edat_level;
  1533. new->initialized = false;
  1534. spin_lock(&parent->shadow_lock);
  1535. /* Recheck if another CPU created the same shadow */
  1536. sg = gmap_find_shadow(parent, asce, edat_level);
  1537. if (sg) {
  1538. spin_unlock(&parent->shadow_lock);
  1539. gmap_free(new);
  1540. return sg;
  1541. }
  1542. if (asce & _ASCE_REAL_SPACE) {
  1543. /* only allow one real-space gmap shadow */
  1544. list_for_each_entry(sg, &parent->children, list) {
  1545. if (sg->orig_asce & _ASCE_REAL_SPACE) {
  1546. spin_lock(&sg->guest_table_lock);
  1547. gmap_unshadow(sg);
  1548. spin_unlock(&sg->guest_table_lock);
  1549. list_del(&sg->list);
  1550. gmap_put(sg);
  1551. break;
  1552. }
  1553. }
  1554. }
  1555. atomic_set(&new->ref_count, 2);
  1556. list_add(&new->list, &parent->children);
  1557. if (asce & _ASCE_REAL_SPACE) {
  1558. /* nothing to protect, return right away */
  1559. new->initialized = true;
  1560. spin_unlock(&parent->shadow_lock);
  1561. return new;
  1562. }
  1563. spin_unlock(&parent->shadow_lock);
  1564. /* protect after insertion, so it will get properly invalidated */
  1565. down_read(&parent->mm->mmap_sem);
  1566. rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
  1567. ((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
  1568. PROT_READ, GMAP_NOTIFY_SHADOW);
  1569. up_read(&parent->mm->mmap_sem);
  1570. spin_lock(&parent->shadow_lock);
  1571. new->initialized = true;
  1572. if (rc) {
  1573. list_del(&new->list);
  1574. gmap_free(new);
  1575. new = ERR_PTR(rc);
  1576. }
  1577. spin_unlock(&parent->shadow_lock);
  1578. return new;
  1579. }
  1580. EXPORT_SYMBOL_GPL(gmap_shadow);
  1581. /**
  1582. * gmap_shadow_r2t - create an empty shadow region 2 table
  1583. * @sg: pointer to the shadow guest address space structure
  1584. * @saddr: faulting address in the shadow gmap
  1585. * @r2t: parent gmap address of the region 2 table to get shadowed
  1586. * @fake: r2t references contiguous guest memory block, not a r2t
  1587. *
  1588. * The r2t parameter specifies the address of the source table. The
  1589. * four pages of the source table are made read-only in the parent gmap
  1590. * address space. A write to the source table area @r2t will automatically
  1591. * remove the shadow r2 table and all of its decendents.
  1592. *
  1593. * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
  1594. * shadow table structure is incomplete, -ENOMEM if out of memory and
  1595. * -EFAULT if an address in the parent gmap could not be resolved.
  1596. *
  1597. * Called with sg->mm->mmap_sem in read.
  1598. */
  1599. int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
  1600. int fake)
  1601. {
  1602. unsigned long raddr, origin, offset, len;
  1603. unsigned long *s_r2t, *table;
  1604. struct page *page;
  1605. int rc;
  1606. BUG_ON(!gmap_is_shadow(sg));
  1607. /* Allocate a shadow region second table */
  1608. page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
  1609. if (!page)
  1610. return -ENOMEM;
  1611. page->index = r2t & _REGION_ENTRY_ORIGIN;
  1612. if (fake)
  1613. page->index |= GMAP_SHADOW_FAKE_TABLE;
  1614. s_r2t = (unsigned long *) page_to_phys(page);
  1615. /* Install shadow region second table */
  1616. spin_lock(&sg->guest_table_lock);
  1617. table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
  1618. if (!table) {
  1619. rc = -EAGAIN; /* Race with unshadow */
  1620. goto out_free;
  1621. }
  1622. if (!(*table & _REGION_ENTRY_INVALID)) {
  1623. rc = 0; /* Already established */
  1624. goto out_free;
  1625. } else if (*table & _REGION_ENTRY_ORIGIN) {
  1626. rc = -EAGAIN; /* Race with shadow */
  1627. goto out_free;
  1628. }
  1629. crst_table_init(s_r2t, _REGION2_ENTRY_EMPTY);
  1630. /* mark as invalid as long as the parent table is not protected */
  1631. *table = (unsigned long) s_r2t | _REGION_ENTRY_LENGTH |
  1632. _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
  1633. if (sg->edat_level >= 1)
  1634. *table |= (r2t & _REGION_ENTRY_PROTECT);
  1635. list_add(&page->lru, &sg->crst_list);
  1636. if (fake) {
  1637. /* nothing to protect for fake tables */
  1638. *table &= ~_REGION_ENTRY_INVALID;
  1639. spin_unlock(&sg->guest_table_lock);
  1640. return 0;
  1641. }
  1642. spin_unlock(&sg->guest_table_lock);
  1643. /* Make r2t read-only in parent gmap page table */
  1644. raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
  1645. origin = r2t & _REGION_ENTRY_ORIGIN;
  1646. offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
  1647. len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
  1648. rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
  1649. spin_lock(&sg->guest_table_lock);
  1650. if (!rc) {
  1651. table = gmap_table_walk(sg, saddr, 4);
  1652. if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
  1653. (unsigned long) s_r2t)
  1654. rc = -EAGAIN; /* Race with unshadow */
  1655. else
  1656. *table &= ~_REGION_ENTRY_INVALID;
  1657. } else {
  1658. gmap_unshadow_r2t(sg, raddr);
  1659. }
  1660. spin_unlock(&sg->guest_table_lock);
  1661. return rc;
  1662. out_free:
  1663. spin_unlock(&sg->guest_table_lock);
  1664. __free_pages(page, CRST_ALLOC_ORDER);
  1665. return rc;
  1666. }
  1667. EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
  1668. /**
  1669. * gmap_shadow_r3t - create a shadow region 3 table
  1670. * @sg: pointer to the shadow guest address space structure
  1671. * @saddr: faulting address in the shadow gmap
  1672. * @r3t: parent gmap address of the region 3 table to get shadowed
  1673. * @fake: r3t references contiguous guest memory block, not a r3t
  1674. *
  1675. * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
  1676. * shadow table structure is incomplete, -ENOMEM if out of memory and
  1677. * -EFAULT if an address in the parent gmap could not be resolved.
  1678. *
  1679. * Called with sg->mm->mmap_sem in read.
  1680. */
  1681. int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
  1682. int fake)
  1683. {
  1684. unsigned long raddr, origin, offset, len;
  1685. unsigned long *s_r3t, *table;
  1686. struct page *page;
  1687. int rc;
  1688. BUG_ON(!gmap_is_shadow(sg));
  1689. /* Allocate a shadow region second table */
  1690. page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
  1691. if (!page)
  1692. return -ENOMEM;
  1693. page->index = r3t & _REGION_ENTRY_ORIGIN;
  1694. if (fake)
  1695. page->index |= GMAP_SHADOW_FAKE_TABLE;
  1696. s_r3t = (unsigned long *) page_to_phys(page);
  1697. /* Install shadow region second table */
  1698. spin_lock(&sg->guest_table_lock);
  1699. table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
  1700. if (!table) {
  1701. rc = -EAGAIN; /* Race with unshadow */
  1702. goto out_free;
  1703. }
  1704. if (!(*table & _REGION_ENTRY_INVALID)) {
  1705. rc = 0; /* Already established */
  1706. goto out_free;
  1707. } else if (*table & _REGION_ENTRY_ORIGIN) {
  1708. rc = -EAGAIN; /* Race with shadow */
  1709. }
  1710. crst_table_init(s_r3t, _REGION3_ENTRY_EMPTY);
  1711. /* mark as invalid as long as the parent table is not protected */
  1712. *table = (unsigned long) s_r3t | _REGION_ENTRY_LENGTH |
  1713. _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
  1714. if (sg->edat_level >= 1)
  1715. *table |= (r3t & _REGION_ENTRY_PROTECT);
  1716. list_add(&page->lru, &sg->crst_list);
  1717. if (fake) {
  1718. /* nothing to protect for fake tables */
  1719. *table &= ~_REGION_ENTRY_INVALID;
  1720. spin_unlock(&sg->guest_table_lock);
  1721. return 0;
  1722. }
  1723. spin_unlock(&sg->guest_table_lock);
  1724. /* Make r3t read-only in parent gmap page table */
  1725. raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
  1726. origin = r3t & _REGION_ENTRY_ORIGIN;
  1727. offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
  1728. len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
  1729. rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
  1730. spin_lock(&sg->guest_table_lock);
  1731. if (!rc) {
  1732. table = gmap_table_walk(sg, saddr, 3);
  1733. if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
  1734. (unsigned long) s_r3t)
  1735. rc = -EAGAIN; /* Race with unshadow */
  1736. else
  1737. *table &= ~_REGION_ENTRY_INVALID;
  1738. } else {
  1739. gmap_unshadow_r3t(sg, raddr);
  1740. }
  1741. spin_unlock(&sg->guest_table_lock);
  1742. return rc;
  1743. out_free:
  1744. spin_unlock(&sg->guest_table_lock);
  1745. __free_pages(page, CRST_ALLOC_ORDER);
  1746. return rc;
  1747. }
  1748. EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
  1749. /**
  1750. * gmap_shadow_sgt - create a shadow segment table
  1751. * @sg: pointer to the shadow guest address space structure
  1752. * @saddr: faulting address in the shadow gmap
  1753. * @sgt: parent gmap address of the segment table to get shadowed
  1754. * @fake: sgt references contiguous guest memory block, not a sgt
  1755. *
  1756. * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
  1757. * shadow table structure is incomplete, -ENOMEM if out of memory and
  1758. * -EFAULT if an address in the parent gmap could not be resolved.
  1759. *
  1760. * Called with sg->mm->mmap_sem in read.
  1761. */
  1762. int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
  1763. int fake)
  1764. {
  1765. unsigned long raddr, origin, offset, len;
  1766. unsigned long *s_sgt, *table;
  1767. struct page *page;
  1768. int rc;
  1769. BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
  1770. /* Allocate a shadow segment table */
  1771. page = alloc_pages(GFP_KERNEL, CRST_ALLOC_ORDER);
  1772. if (!page)
  1773. return -ENOMEM;
  1774. page->index = sgt & _REGION_ENTRY_ORIGIN;
  1775. if (fake)
  1776. page->index |= GMAP_SHADOW_FAKE_TABLE;
  1777. s_sgt = (unsigned long *) page_to_phys(page);
  1778. /* Install shadow region second table */
  1779. spin_lock(&sg->guest_table_lock);
  1780. table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
  1781. if (!table) {
  1782. rc = -EAGAIN; /* Race with unshadow */
  1783. goto out_free;
  1784. }
  1785. if (!(*table & _REGION_ENTRY_INVALID)) {
  1786. rc = 0; /* Already established */
  1787. goto out_free;
  1788. } else if (*table & _REGION_ENTRY_ORIGIN) {
  1789. rc = -EAGAIN; /* Race with shadow */
  1790. goto out_free;
  1791. }
  1792. crst_table_init(s_sgt, _SEGMENT_ENTRY_EMPTY);
  1793. /* mark as invalid as long as the parent table is not protected */
  1794. *table = (unsigned long) s_sgt | _REGION_ENTRY_LENGTH |
  1795. _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
  1796. if (sg->edat_level >= 1)
  1797. *table |= sgt & _REGION_ENTRY_PROTECT;
  1798. list_add(&page->lru, &sg->crst_list);
  1799. if (fake) {
  1800. /* nothing to protect for fake tables */
  1801. *table &= ~_REGION_ENTRY_INVALID;
  1802. spin_unlock(&sg->guest_table_lock);
  1803. return 0;
  1804. }
  1805. spin_unlock(&sg->guest_table_lock);
  1806. /* Make sgt read-only in parent gmap page table */
  1807. raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
  1808. origin = sgt & _REGION_ENTRY_ORIGIN;
  1809. offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
  1810. len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
  1811. rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
  1812. spin_lock(&sg->guest_table_lock);
  1813. if (!rc) {
  1814. table = gmap_table_walk(sg, saddr, 2);
  1815. if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
  1816. (unsigned long) s_sgt)
  1817. rc = -EAGAIN; /* Race with unshadow */
  1818. else
  1819. *table &= ~_REGION_ENTRY_INVALID;
  1820. } else {
  1821. gmap_unshadow_sgt(sg, raddr);
  1822. }
  1823. spin_unlock(&sg->guest_table_lock);
  1824. return rc;
  1825. out_free:
  1826. spin_unlock(&sg->guest_table_lock);
  1827. __free_pages(page, CRST_ALLOC_ORDER);
  1828. return rc;
  1829. }
  1830. EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
  1831. /**
  1832. * gmap_shadow_lookup_pgtable - find a shadow page table
  1833. * @sg: pointer to the shadow guest address space structure
  1834. * @saddr: the address in the shadow aguest address space
  1835. * @pgt: parent gmap address of the page table to get shadowed
  1836. * @dat_protection: if the pgtable is marked as protected by dat
  1837. * @fake: pgt references contiguous guest memory block, not a pgtable
  1838. *
  1839. * Returns 0 if the shadow page table was found and -EAGAIN if the page
  1840. * table was not found.
  1841. *
  1842. * Called with sg->mm->mmap_sem in read.
  1843. */
  1844. int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
  1845. unsigned long *pgt, int *dat_protection,
  1846. int *fake)
  1847. {
  1848. unsigned long *table;
  1849. struct page *page;
  1850. int rc;
  1851. BUG_ON(!gmap_is_shadow(sg));
  1852. spin_lock(&sg->guest_table_lock);
  1853. table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
  1854. if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
  1855. /* Shadow page tables are full pages (pte+pgste) */
  1856. page = pfn_to_page(*table >> PAGE_SHIFT);
  1857. *pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
  1858. *dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
  1859. *fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
  1860. rc = 0;
  1861. } else {
  1862. rc = -EAGAIN;
  1863. }
  1864. spin_unlock(&sg->guest_table_lock);
  1865. return rc;
  1866. }
  1867. EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
  1868. /**
  1869. * gmap_shadow_pgt - instantiate a shadow page table
  1870. * @sg: pointer to the shadow guest address space structure
  1871. * @saddr: faulting address in the shadow gmap
  1872. * @pgt: parent gmap address of the page table to get shadowed
  1873. * @fake: pgt references contiguous guest memory block, not a pgtable
  1874. *
  1875. * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
  1876. * shadow table structure is incomplete, -ENOMEM if out of memory,
  1877. * -EFAULT if an address in the parent gmap could not be resolved and
  1878. *
  1879. * Called with gmap->mm->mmap_sem in read
  1880. */
  1881. int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
  1882. int fake)
  1883. {
  1884. unsigned long raddr, origin;
  1885. unsigned long *s_pgt, *table;
  1886. struct page *page;
  1887. int rc;
  1888. BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
  1889. /* Allocate a shadow page table */
  1890. page = page_table_alloc_pgste(sg->mm);
  1891. if (!page)
  1892. return -ENOMEM;
  1893. page->index = pgt & _SEGMENT_ENTRY_ORIGIN;
  1894. if (fake)
  1895. page->index |= GMAP_SHADOW_FAKE_TABLE;
  1896. s_pgt = (unsigned long *) page_to_phys(page);
  1897. /* Install shadow page table */
  1898. spin_lock(&sg->guest_table_lock);
  1899. table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
  1900. if (!table) {
  1901. rc = -EAGAIN; /* Race with unshadow */
  1902. goto out_free;
  1903. }
  1904. if (!(*table & _SEGMENT_ENTRY_INVALID)) {
  1905. rc = 0; /* Already established */
  1906. goto out_free;
  1907. } else if (*table & _SEGMENT_ENTRY_ORIGIN) {
  1908. rc = -EAGAIN; /* Race with shadow */
  1909. goto out_free;
  1910. }
  1911. /* mark as invalid as long as the parent table is not protected */
  1912. *table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
  1913. (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
  1914. list_add(&page->lru, &sg->pt_list);
  1915. if (fake) {
  1916. /* nothing to protect for fake tables */
  1917. *table &= ~_SEGMENT_ENTRY_INVALID;
  1918. spin_unlock(&sg->guest_table_lock);
  1919. return 0;
  1920. }
  1921. spin_unlock(&sg->guest_table_lock);
  1922. /* Make pgt read-only in parent gmap page table (not the pgste) */
  1923. raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
  1924. origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
  1925. rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE);
  1926. spin_lock(&sg->guest_table_lock);
  1927. if (!rc) {
  1928. table = gmap_table_walk(sg, saddr, 1);
  1929. if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) !=
  1930. (unsigned long) s_pgt)
  1931. rc = -EAGAIN; /* Race with unshadow */
  1932. else
  1933. *table &= ~_SEGMENT_ENTRY_INVALID;
  1934. } else {
  1935. gmap_unshadow_pgt(sg, raddr);
  1936. }
  1937. spin_unlock(&sg->guest_table_lock);
  1938. return rc;
  1939. out_free:
  1940. spin_unlock(&sg->guest_table_lock);
  1941. page_table_free_pgste(page);
  1942. return rc;
  1943. }
  1944. EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
  1945. /**
  1946. * gmap_shadow_page - create a shadow page mapping
  1947. * @sg: pointer to the shadow guest address space structure
  1948. * @saddr: faulting address in the shadow gmap
  1949. * @pte: pte in parent gmap address space to get shadowed
  1950. *
  1951. * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
  1952. * shadow table structure is incomplete, -ENOMEM if out of memory and
  1953. * -EFAULT if an address in the parent gmap could not be resolved.
  1954. *
  1955. * Called with sg->mm->mmap_sem in read.
  1956. */
  1957. int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
  1958. {
  1959. struct gmap *parent;
  1960. struct gmap_rmap *rmap;
  1961. unsigned long vmaddr, paddr;
  1962. spinlock_t *ptl;
  1963. pte_t *sptep, *tptep;
  1964. int prot;
  1965. int rc;
  1966. BUG_ON(!gmap_is_shadow(sg));
  1967. parent = sg->parent;
  1968. prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
  1969. rmap = kzalloc(sizeof(*rmap), GFP_KERNEL);
  1970. if (!rmap)
  1971. return -ENOMEM;
  1972. rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
  1973. while (1) {
  1974. paddr = pte_val(pte) & PAGE_MASK;
  1975. vmaddr = __gmap_translate(parent, paddr);
  1976. if (IS_ERR_VALUE(vmaddr)) {
  1977. rc = vmaddr;
  1978. break;
  1979. }
  1980. rc = radix_tree_preload(GFP_KERNEL);
  1981. if (rc)
  1982. break;
  1983. rc = -EAGAIN;
  1984. sptep = gmap_pte_op_walk(parent, paddr, &ptl);
  1985. if (sptep) {
  1986. spin_lock(&sg->guest_table_lock);
  1987. /* Get page table pointer */
  1988. tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
  1989. if (!tptep) {
  1990. spin_unlock(&sg->guest_table_lock);
  1991. gmap_pte_op_end(ptl);
  1992. radix_tree_preload_end();
  1993. break;
  1994. }
  1995. rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
  1996. if (rc > 0) {
  1997. /* Success and a new mapping */
  1998. gmap_insert_rmap(sg, vmaddr, rmap);
  1999. rmap = NULL;
  2000. rc = 0;
  2001. }
  2002. gmap_pte_op_end(ptl);
  2003. spin_unlock(&sg->guest_table_lock);
  2004. }
  2005. radix_tree_preload_end();
  2006. if (!rc)
  2007. break;
  2008. rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
  2009. if (rc)
  2010. break;
  2011. }
  2012. kfree(rmap);
  2013. return rc;
  2014. }
  2015. EXPORT_SYMBOL_GPL(gmap_shadow_page);
  2016. /**
  2017. * gmap_shadow_notify - handle notifications for shadow gmap
  2018. *
  2019. * Called with sg->parent->shadow_lock.
  2020. */
  2021. static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
  2022. unsigned long gaddr)
  2023. {
  2024. struct gmap_rmap *rmap, *rnext, *head;
  2025. unsigned long start, end, bits, raddr;
  2026. BUG_ON(!gmap_is_shadow(sg));
  2027. spin_lock(&sg->guest_table_lock);
  2028. if (sg->removed) {
  2029. spin_unlock(&sg->guest_table_lock);
  2030. return;
  2031. }
  2032. /* Check for top level table */
  2033. start = sg->orig_asce & _ASCE_ORIGIN;
  2034. end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
  2035. if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
  2036. gaddr < end) {
  2037. /* The complete shadow table has to go */
  2038. gmap_unshadow(sg);
  2039. spin_unlock(&sg->guest_table_lock);
  2040. list_del(&sg->list);
  2041. gmap_put(sg);
  2042. return;
  2043. }
  2044. /* Remove the page table tree from on specific entry */
  2045. head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
  2046. gmap_for_each_rmap_safe(rmap, rnext, head) {
  2047. bits = rmap->raddr & _SHADOW_RMAP_MASK;
  2048. raddr = rmap->raddr ^ bits;
  2049. switch (bits) {
  2050. case _SHADOW_RMAP_REGION1:
  2051. gmap_unshadow_r2t(sg, raddr);
  2052. break;
  2053. case _SHADOW_RMAP_REGION2:
  2054. gmap_unshadow_r3t(sg, raddr);
  2055. break;
  2056. case _SHADOW_RMAP_REGION3:
  2057. gmap_unshadow_sgt(sg, raddr);
  2058. break;
  2059. case _SHADOW_RMAP_SEGMENT:
  2060. gmap_unshadow_pgt(sg, raddr);
  2061. break;
  2062. case _SHADOW_RMAP_PGTABLE:
  2063. gmap_unshadow_page(sg, raddr);
  2064. break;
  2065. }
  2066. kfree(rmap);
  2067. }
  2068. spin_unlock(&sg->guest_table_lock);
  2069. }
  2070. /**
  2071. * ptep_notify - call all invalidation callbacks for a specific pte.
  2072. * @mm: pointer to the process mm_struct
  2073. * @addr: virtual address in the process address space
  2074. * @pte: pointer to the page table entry
  2075. * @bits: bits from the pgste that caused the notify call
  2076. *
  2077. * This function is assumed to be called with the page table lock held
  2078. * for the pte to notify.
  2079. */
  2080. void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
  2081. pte_t *pte, unsigned long bits)
  2082. {
  2083. unsigned long offset, gaddr = 0;
  2084. unsigned long *table;
  2085. struct gmap *gmap, *sg, *next;
  2086. offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
  2087. offset = offset * (PAGE_SIZE / sizeof(pte_t));
  2088. rcu_read_lock();
  2089. list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
  2090. spin_lock(&gmap->guest_table_lock);
  2091. table = radix_tree_lookup(&gmap->host_to_guest,
  2092. vmaddr >> PMD_SHIFT);
  2093. if (table)
  2094. gaddr = __gmap_segment_gaddr(table) + offset;
  2095. spin_unlock(&gmap->guest_table_lock);
  2096. if (!table)
  2097. continue;
  2098. if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
  2099. spin_lock(&gmap->shadow_lock);
  2100. list_for_each_entry_safe(sg, next,
  2101. &gmap->children, list)
  2102. gmap_shadow_notify(sg, vmaddr, gaddr);
  2103. spin_unlock(&gmap->shadow_lock);
  2104. }
  2105. if (bits & PGSTE_IN_BIT)
  2106. gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
  2107. }
  2108. rcu_read_unlock();
  2109. }
  2110. EXPORT_SYMBOL_GPL(ptep_notify);
  2111. static void pmdp_notify_gmap(struct gmap *gmap, pmd_t *pmdp,
  2112. unsigned long gaddr)
  2113. {
  2114. pmd_val(*pmdp) &= ~_SEGMENT_ENTRY_GMAP_IN;
  2115. gmap_call_notifier(gmap, gaddr, gaddr + HPAGE_SIZE - 1);
  2116. }
  2117. /**
  2118. * gmap_pmdp_xchg - exchange a gmap pmd with another
  2119. * @gmap: pointer to the guest address space structure
  2120. * @pmdp: pointer to the pmd entry
  2121. * @new: replacement entry
  2122. * @gaddr: the affected guest address
  2123. *
  2124. * This function is assumed to be called with the guest_table_lock
  2125. * held.
  2126. */
  2127. static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *pmdp, pmd_t new,
  2128. unsigned long gaddr)
  2129. {
  2130. gaddr &= HPAGE_MASK;
  2131. pmdp_notify_gmap(gmap, pmdp, gaddr);
  2132. pmd_val(new) &= ~_SEGMENT_ENTRY_GMAP_IN;
  2133. if (MACHINE_HAS_TLB_GUEST)
  2134. __pmdp_idte(gaddr, (pmd_t *)pmdp, IDTE_GUEST_ASCE, gmap->asce,
  2135. IDTE_GLOBAL);
  2136. else if (MACHINE_HAS_IDTE)
  2137. __pmdp_idte(gaddr, (pmd_t *)pmdp, 0, 0, IDTE_GLOBAL);
  2138. else
  2139. __pmdp_csp(pmdp);
  2140. *pmdp = new;
  2141. }
  2142. static void gmap_pmdp_clear(struct mm_struct *mm, unsigned long vmaddr,
  2143. int purge)
  2144. {
  2145. pmd_t *pmdp;
  2146. struct gmap *gmap;
  2147. unsigned long gaddr;
  2148. rcu_read_lock();
  2149. list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
  2150. spin_lock(&gmap->guest_table_lock);
  2151. pmdp = (pmd_t *)radix_tree_delete(&gmap->host_to_guest,
  2152. vmaddr >> PMD_SHIFT);
  2153. if (pmdp) {
  2154. gaddr = __gmap_segment_gaddr((unsigned long *)pmdp);
  2155. pmdp_notify_gmap(gmap, pmdp, gaddr);
  2156. WARN_ON(pmd_val(*pmdp) & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
  2157. _SEGMENT_ENTRY_GMAP_UC));
  2158. if (purge)
  2159. __pmdp_csp(pmdp);
  2160. pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
  2161. }
  2162. spin_unlock(&gmap->guest_table_lock);
  2163. }
  2164. rcu_read_unlock();
  2165. }
  2166. /**
  2167. * gmap_pmdp_invalidate - invalidate all affected guest pmd entries without
  2168. * flushing
  2169. * @mm: pointer to the process mm_struct
  2170. * @vmaddr: virtual address in the process address space
  2171. */
  2172. void gmap_pmdp_invalidate(struct mm_struct *mm, unsigned long vmaddr)
  2173. {
  2174. gmap_pmdp_clear(mm, vmaddr, 0);
  2175. }
  2176. EXPORT_SYMBOL_GPL(gmap_pmdp_invalidate);
  2177. /**
  2178. * gmap_pmdp_csp - csp all affected guest pmd entries
  2179. * @mm: pointer to the process mm_struct
  2180. * @vmaddr: virtual address in the process address space
  2181. */
  2182. void gmap_pmdp_csp(struct mm_struct *mm, unsigned long vmaddr)
  2183. {
  2184. gmap_pmdp_clear(mm, vmaddr, 1);
  2185. }
  2186. EXPORT_SYMBOL_GPL(gmap_pmdp_csp);
  2187. /**
  2188. * gmap_pmdp_idte_local - invalidate and clear a guest pmd entry
  2189. * @mm: pointer to the process mm_struct
  2190. * @vmaddr: virtual address in the process address space
  2191. */
  2192. void gmap_pmdp_idte_local(struct mm_struct *mm, unsigned long vmaddr)
  2193. {
  2194. unsigned long *entry, gaddr;
  2195. struct gmap *gmap;
  2196. pmd_t *pmdp;
  2197. rcu_read_lock();
  2198. list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
  2199. spin_lock(&gmap->guest_table_lock);
  2200. entry = radix_tree_delete(&gmap->host_to_guest,
  2201. vmaddr >> PMD_SHIFT);
  2202. if (entry) {
  2203. pmdp = (pmd_t *)entry;
  2204. gaddr = __gmap_segment_gaddr(entry);
  2205. pmdp_notify_gmap(gmap, pmdp, gaddr);
  2206. WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
  2207. _SEGMENT_ENTRY_GMAP_UC));
  2208. if (MACHINE_HAS_TLB_GUEST)
  2209. __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
  2210. gmap->asce, IDTE_LOCAL);
  2211. else if (MACHINE_HAS_IDTE)
  2212. __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_LOCAL);
  2213. *entry = _SEGMENT_ENTRY_EMPTY;
  2214. }
  2215. spin_unlock(&gmap->guest_table_lock);
  2216. }
  2217. rcu_read_unlock();
  2218. }
  2219. EXPORT_SYMBOL_GPL(gmap_pmdp_idte_local);
  2220. /**
  2221. * gmap_pmdp_idte_global - invalidate and clear a guest pmd entry
  2222. * @mm: pointer to the process mm_struct
  2223. * @vmaddr: virtual address in the process address space
  2224. */
  2225. void gmap_pmdp_idte_global(struct mm_struct *mm, unsigned long vmaddr)
  2226. {
  2227. unsigned long *entry, gaddr;
  2228. struct gmap *gmap;
  2229. pmd_t *pmdp;
  2230. rcu_read_lock();
  2231. list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
  2232. spin_lock(&gmap->guest_table_lock);
  2233. entry = radix_tree_delete(&gmap->host_to_guest,
  2234. vmaddr >> PMD_SHIFT);
  2235. if (entry) {
  2236. pmdp = (pmd_t *)entry;
  2237. gaddr = __gmap_segment_gaddr(entry);
  2238. pmdp_notify_gmap(gmap, pmdp, gaddr);
  2239. WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
  2240. _SEGMENT_ENTRY_GMAP_UC));
  2241. if (MACHINE_HAS_TLB_GUEST)
  2242. __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
  2243. gmap->asce, IDTE_GLOBAL);
  2244. else if (MACHINE_HAS_IDTE)
  2245. __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_GLOBAL);
  2246. else
  2247. __pmdp_csp(pmdp);
  2248. *entry = _SEGMENT_ENTRY_EMPTY;
  2249. }
  2250. spin_unlock(&gmap->guest_table_lock);
  2251. }
  2252. rcu_read_unlock();
  2253. }
  2254. EXPORT_SYMBOL_GPL(gmap_pmdp_idte_global);
  2255. /**
  2256. * gmap_test_and_clear_dirty_pmd - test and reset segment dirty status
  2257. * @gmap: pointer to guest address space
  2258. * @pmdp: pointer to the pmd to be tested
  2259. * @gaddr: virtual address in the guest address space
  2260. *
  2261. * This function is assumed to be called with the guest_table_lock
  2262. * held.
  2263. */
  2264. bool gmap_test_and_clear_dirty_pmd(struct gmap *gmap, pmd_t *pmdp,
  2265. unsigned long gaddr)
  2266. {
  2267. if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
  2268. return false;
  2269. /* Already protected memory, which did not change is clean */
  2270. if (pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT &&
  2271. !(pmd_val(*pmdp) & _SEGMENT_ENTRY_GMAP_UC))
  2272. return false;
  2273. /* Clear UC indication and reset protection */
  2274. pmd_val(*pmdp) &= ~_SEGMENT_ENTRY_GMAP_UC;
  2275. gmap_protect_pmd(gmap, gaddr, pmdp, PROT_READ, 0);
  2276. return true;
  2277. }
  2278. /**
  2279. * gmap_sync_dirty_log_pmd - set bitmap based on dirty status of segment
  2280. * @gmap: pointer to guest address space
  2281. * @bitmap: dirty bitmap for this pmd
  2282. * @gaddr: virtual address in the guest address space
  2283. * @vmaddr: virtual address in the host address space
  2284. *
  2285. * This function is assumed to be called with the guest_table_lock
  2286. * held.
  2287. */
  2288. void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long bitmap[4],
  2289. unsigned long gaddr, unsigned long vmaddr)
  2290. {
  2291. int i;
  2292. pmd_t *pmdp;
  2293. pte_t *ptep;
  2294. spinlock_t *ptl;
  2295. pmdp = gmap_pmd_op_walk(gmap, gaddr);
  2296. if (!pmdp)
  2297. return;
  2298. if (pmd_large(*pmdp)) {
  2299. if (gmap_test_and_clear_dirty_pmd(gmap, pmdp, gaddr))
  2300. bitmap_fill(bitmap, _PAGE_ENTRIES);
  2301. } else {
  2302. for (i = 0; i < _PAGE_ENTRIES; i++, vmaddr += PAGE_SIZE) {
  2303. ptep = pte_alloc_map_lock(gmap->mm, pmdp, vmaddr, &ptl);
  2304. if (!ptep)
  2305. continue;
  2306. if (ptep_test_and_clear_uc(gmap->mm, vmaddr, ptep))
  2307. set_bit(i, bitmap);
  2308. spin_unlock(ptl);
  2309. }
  2310. }
  2311. gmap_pmd_op_end(gmap, pmdp);
  2312. }
  2313. EXPORT_SYMBOL_GPL(gmap_sync_dirty_log_pmd);
  2314. static inline void thp_split_mm(struct mm_struct *mm)
  2315. {
  2316. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  2317. struct vm_area_struct *vma;
  2318. unsigned long addr;
  2319. for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
  2320. for (addr = vma->vm_start;
  2321. addr < vma->vm_end;
  2322. addr += PAGE_SIZE)
  2323. follow_page(vma, addr, FOLL_SPLIT);
  2324. vma->vm_flags &= ~VM_HUGEPAGE;
  2325. vma->vm_flags |= VM_NOHUGEPAGE;
  2326. }
  2327. mm->def_flags |= VM_NOHUGEPAGE;
  2328. #endif
  2329. }
  2330. /*
  2331. * Remove all empty zero pages from the mapping for lazy refaulting
  2332. * - This must be called after mm->context.has_pgste is set, to avoid
  2333. * future creation of zero pages
  2334. * - This must be called after THP was enabled
  2335. */
  2336. static int __zap_zero_pages(pmd_t *pmd, unsigned long start,
  2337. unsigned long end, struct mm_walk *walk)
  2338. {
  2339. unsigned long addr;
  2340. for (addr = start; addr != end; addr += PAGE_SIZE) {
  2341. pte_t *ptep;
  2342. spinlock_t *ptl;
  2343. ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
  2344. if (is_zero_pfn(pte_pfn(*ptep)))
  2345. ptep_xchg_direct(walk->mm, addr, ptep, __pte(_PAGE_INVALID));
  2346. pte_unmap_unlock(ptep, ptl);
  2347. }
  2348. return 0;
  2349. }
  2350. static inline void zap_zero_pages(struct mm_struct *mm)
  2351. {
  2352. struct mm_walk walk = { .pmd_entry = __zap_zero_pages };
  2353. walk.mm = mm;
  2354. walk_page_range(0, TASK_SIZE, &walk);
  2355. }
  2356. /*
  2357. * switch on pgstes for its userspace process (for kvm)
  2358. */
  2359. int s390_enable_sie(void)
  2360. {
  2361. struct mm_struct *mm = current->mm;
  2362. /* Do we have pgstes? if yes, we are done */
  2363. if (mm_has_pgste(mm))
  2364. return 0;
  2365. /* Fail if the page tables are 2K */
  2366. if (!mm_alloc_pgste(mm))
  2367. return -EINVAL;
  2368. down_write(&mm->mmap_sem);
  2369. mm->context.has_pgste = 1;
  2370. /* split thp mappings and disable thp for future mappings */
  2371. thp_split_mm(mm);
  2372. zap_zero_pages(mm);
  2373. up_write(&mm->mmap_sem);
  2374. return 0;
  2375. }
  2376. EXPORT_SYMBOL_GPL(s390_enable_sie);
  2377. /*
  2378. * Enable storage key handling from now on and initialize the storage
  2379. * keys with the default key.
  2380. */
  2381. static int __s390_enable_skey_pte(pte_t *pte, unsigned long addr,
  2382. unsigned long next, struct mm_walk *walk)
  2383. {
  2384. /* Clear storage key */
  2385. ptep_zap_key(walk->mm, addr, pte);
  2386. return 0;
  2387. }
  2388. static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
  2389. unsigned long hmask, unsigned long next,
  2390. struct mm_walk *walk)
  2391. {
  2392. pmd_t *pmd = (pmd_t *)pte;
  2393. unsigned long start, end;
  2394. struct page *page = pmd_page(*pmd);
  2395. /*
  2396. * The write check makes sure we do not set a key on shared
  2397. * memory. This is needed as the walker does not differentiate
  2398. * between actual guest memory and the process executable or
  2399. * shared libraries.
  2400. */
  2401. if (pmd_val(*pmd) & _SEGMENT_ENTRY_INVALID ||
  2402. !(pmd_val(*pmd) & _SEGMENT_ENTRY_WRITE))
  2403. return 0;
  2404. start = pmd_val(*pmd) & HPAGE_MASK;
  2405. end = start + HPAGE_SIZE - 1;
  2406. __storage_key_init_range(start, end);
  2407. set_bit(PG_arch_1, &page->flags);
  2408. return 0;
  2409. }
  2410. int s390_enable_skey(void)
  2411. {
  2412. struct mm_walk walk = {
  2413. .hugetlb_entry = __s390_enable_skey_hugetlb,
  2414. .pte_entry = __s390_enable_skey_pte,
  2415. };
  2416. struct mm_struct *mm = current->mm;
  2417. struct vm_area_struct *vma;
  2418. int rc = 0;
  2419. down_write(&mm->mmap_sem);
  2420. if (mm_uses_skeys(mm))
  2421. goto out_up;
  2422. mm->context.uses_skeys = 1;
  2423. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2424. if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
  2425. MADV_UNMERGEABLE, &vma->vm_flags)) {
  2426. mm->context.uses_skeys = 0;
  2427. rc = -ENOMEM;
  2428. goto out_up;
  2429. }
  2430. }
  2431. mm->def_flags &= ~VM_MERGEABLE;
  2432. walk.mm = mm;
  2433. walk_page_range(0, TASK_SIZE, &walk);
  2434. out_up:
  2435. up_write(&mm->mmap_sem);
  2436. return rc;
  2437. }
  2438. EXPORT_SYMBOL_GPL(s390_enable_skey);
  2439. /*
  2440. * Reset CMMA state, make all pages stable again.
  2441. */
  2442. static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
  2443. unsigned long next, struct mm_walk *walk)
  2444. {
  2445. ptep_zap_unused(walk->mm, addr, pte, 1);
  2446. return 0;
  2447. }
  2448. void s390_reset_cmma(struct mm_struct *mm)
  2449. {
  2450. struct mm_walk walk = { .pte_entry = __s390_reset_cmma };
  2451. down_write(&mm->mmap_sem);
  2452. walk.mm = mm;
  2453. walk_page_range(0, TASK_SIZE, &walk);
  2454. up_write(&mm->mmap_sem);
  2455. }
  2456. EXPORT_SYMBOL_GPL(s390_reset_cmma);