pv.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hosting Protected Virtual Machines
  4. *
  5. * Copyright IBM Corp. 2019, 2020
  6. * Author(s): Janosch Frank <frankja@linux.ibm.com>
  7. */
  8. #include <linux/export.h>
  9. #include <linux/kvm.h>
  10. #include <linux/kvm_host.h>
  11. #include <linux/minmax.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/sched/signal.h>
  14. #include <asm/uv.h>
  15. #include <asm/mman.h>
  16. #include <linux/pagewalk.h>
  17. #include <linux/sched/mm.h>
  18. #include <linux/mmu_notifier.h>
  19. #include "kvm-s390.h"
  20. #include "dat.h"
  21. #include "gaccess.h"
  22. #include "gmap.h"
  23. #include "faultin.h"
  24. bool kvm_s390_pv_is_protected(struct kvm *kvm)
  25. {
  26. lockdep_assert_held(&kvm->lock);
  27. return !!kvm_s390_pv_get_handle(kvm);
  28. }
  29. EXPORT_SYMBOL_GPL(kvm_s390_pv_is_protected);
  30. bool kvm_s390_pv_cpu_is_protected(struct kvm_vcpu *vcpu)
  31. {
  32. lockdep_assert_held(&vcpu->mutex);
  33. return !!kvm_s390_pv_cpu_get_handle(vcpu);
  34. }
  35. EXPORT_SYMBOL_GPL(kvm_s390_pv_cpu_is_protected);
  36. /**
  37. * should_export_before_import() - Determine whether an export is needed
  38. * before an import-like operation.
  39. * @uvcb: The Ultravisor control block of the UVC to be performed.
  40. * @mm: The mm of the process.
  41. *
  42. * Returns whether an export is needed before every import-like operation.
  43. * This is needed for shared pages, which don't trigger a secure storage
  44. * exception when accessed from a different guest.
  45. *
  46. * Although considered as one, the Unpin Page UVC is not an actual import,
  47. * so it is not affected.
  48. *
  49. * No export is needed also when there is only one protected VM, because the
  50. * page cannot belong to the wrong VM in that case (there is no "other VM"
  51. * it can belong to).
  52. *
  53. * Return: %true if an export is needed before every import, otherwise %false.
  54. */
  55. static bool should_export_before_import(struct uv_cb_header *uvcb, struct mm_struct *mm)
  56. {
  57. /*
  58. * The misc feature indicates, among other things, that importing a
  59. * shared page from a different protected VM will automatically also
  60. * transfer its ownership.
  61. */
  62. if (uv_has_feature(BIT_UV_FEAT_MISC))
  63. return false;
  64. if (uvcb->cmd == UVC_CMD_UNPIN_PAGE_SHARED)
  65. return false;
  66. return atomic_read(&mm->context.protected_count) > 1;
  67. }
  68. struct pv_make_secure {
  69. void *uvcb;
  70. struct folio *folio;
  71. int rc;
  72. bool needs_export;
  73. };
  74. static int __kvm_s390_pv_make_secure(struct guest_fault *f, struct folio *folio)
  75. {
  76. struct pv_make_secure *priv = f->priv;
  77. int rc;
  78. if (priv->needs_export)
  79. uv_convert_from_secure(folio_to_phys(folio));
  80. if (folio_test_hugetlb(folio))
  81. return -EFAULT;
  82. if (folio_test_large(folio))
  83. return -E2BIG;
  84. if (!f->page)
  85. folio_get(folio);
  86. rc = __make_folio_secure(folio, priv->uvcb);
  87. if (!f->page)
  88. folio_put(folio);
  89. return rc;
  90. }
  91. static void _kvm_s390_pv_make_secure(struct guest_fault *f)
  92. {
  93. struct pv_make_secure *priv = f->priv;
  94. struct folio *folio;
  95. folio = pfn_folio(f->pfn);
  96. priv->rc = -EAGAIN;
  97. if (folio_trylock(folio)) {
  98. priv->rc = __kvm_s390_pv_make_secure(f, folio);
  99. if (priv->rc == -E2BIG || priv->rc == -EBUSY) {
  100. priv->folio = folio;
  101. folio_get(folio);
  102. }
  103. folio_unlock(folio);
  104. }
  105. }
  106. /**
  107. * kvm_s390_pv_make_secure() - make one guest page secure
  108. * @kvm: the guest
  109. * @gaddr: the guest address that needs to be made secure
  110. * @uvcb: the UVCB specifying which operation needs to be performed
  111. *
  112. * Context: needs to be called with kvm->srcu held.
  113. * Return: 0 on success, < 0 in case of error.
  114. */
  115. int kvm_s390_pv_make_secure(struct kvm *kvm, unsigned long gaddr, void *uvcb)
  116. {
  117. struct pv_make_secure priv = { .uvcb = uvcb };
  118. struct guest_fault f = {
  119. .write_attempt = true,
  120. .gfn = gpa_to_gfn(gaddr),
  121. .callback = _kvm_s390_pv_make_secure,
  122. .priv = &priv,
  123. };
  124. int rc;
  125. lockdep_assert_held(&kvm->srcu);
  126. priv.needs_export = should_export_before_import(uvcb, kvm->mm);
  127. scoped_guard(mutex, &kvm->arch.pv.import_lock) {
  128. rc = kvm_s390_faultin_gfn(NULL, kvm, &f);
  129. if (!rc) {
  130. rc = priv.rc;
  131. if (priv.folio) {
  132. rc = s390_wiggle_split_folio(kvm->mm, priv.folio);
  133. if (!rc)
  134. rc = -EAGAIN;
  135. }
  136. }
  137. }
  138. if (priv.folio)
  139. folio_put(priv.folio);
  140. return rc;
  141. }
  142. int kvm_s390_pv_convert_to_secure(struct kvm *kvm, unsigned long gaddr)
  143. {
  144. struct uv_cb_cts uvcb = {
  145. .header.cmd = UVC_CMD_CONV_TO_SEC_STOR,
  146. .header.len = sizeof(uvcb),
  147. .guest_handle = kvm_s390_pv_get_handle(kvm),
  148. .gaddr = gaddr,
  149. };
  150. return kvm_s390_pv_make_secure(kvm, gaddr, &uvcb);
  151. }
  152. /**
  153. * kvm_s390_pv_destroy_page() - Destroy a guest page.
  154. * @kvm: the guest
  155. * @gaddr: the guest address to destroy
  156. *
  157. * An attempt will be made to destroy the given guest page. If the attempt
  158. * fails, an attempt is made to export the page. If both attempts fail, an
  159. * appropriate error is returned.
  160. *
  161. * Context: may sleep.
  162. */
  163. int kvm_s390_pv_destroy_page(struct kvm *kvm, unsigned long gaddr)
  164. {
  165. struct page *page;
  166. int rc = 0;
  167. mmap_read_lock(kvm->mm);
  168. page = gfn_to_page(kvm, gpa_to_gfn(gaddr));
  169. if (page)
  170. rc = __kvm_s390_pv_destroy_page(page);
  171. kvm_release_page_clean(page);
  172. mmap_read_unlock(kvm->mm);
  173. return rc;
  174. }
  175. /**
  176. * struct pv_vm_to_be_destroyed - Represents a protected VM that needs to
  177. * be destroyed
  178. *
  179. * @list: list head for the list of leftover VMs
  180. * @old_gmap_table: the gmap table of the leftover protected VM
  181. * @handle: the handle of the leftover protected VM
  182. * @stor_var: pointer to the variable storage of the leftover protected VM
  183. * @stor_base: address of the base storage of the leftover protected VM
  184. *
  185. * Represents a protected VM that is still registered with the Ultravisor,
  186. * but which does not correspond any longer to an active KVM VM. It should
  187. * be destroyed at some point later, either asynchronously or when the
  188. * process terminates.
  189. */
  190. struct pv_vm_to_be_destroyed {
  191. struct list_head list;
  192. unsigned long old_gmap_table;
  193. u64 handle;
  194. void *stor_var;
  195. unsigned long stor_base;
  196. };
  197. static void kvm_s390_clear_pv_state(struct kvm *kvm)
  198. {
  199. kvm->arch.pv.handle = 0;
  200. kvm->arch.pv.guest_len = 0;
  201. kvm->arch.pv.stor_base = 0;
  202. kvm->arch.pv.stor_var = NULL;
  203. }
  204. int kvm_s390_pv_destroy_cpu(struct kvm_vcpu *vcpu, u16 *rc, u16 *rrc)
  205. {
  206. int cc;
  207. if (!kvm_s390_pv_cpu_get_handle(vcpu))
  208. return 0;
  209. cc = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), UVC_CMD_DESTROY_SEC_CPU, rc, rrc);
  210. KVM_UV_EVENT(vcpu->kvm, 3, "PROTVIRT DESTROY VCPU %d: rc %x rrc %x",
  211. vcpu->vcpu_id, *rc, *rrc);
  212. WARN_ONCE(cc, "protvirt destroy cpu failed rc %x rrc %x", *rc, *rrc);
  213. /* Intended memory leak for something that should never happen. */
  214. if (!cc)
  215. free_pages(vcpu->arch.pv.stor_base,
  216. get_order(uv_info.guest_cpu_stor_len));
  217. free_page((unsigned long)sida_addr(vcpu->arch.sie_block));
  218. vcpu->arch.sie_block->pv_handle_cpu = 0;
  219. vcpu->arch.sie_block->pv_handle_config = 0;
  220. memset(&vcpu->arch.pv, 0, sizeof(vcpu->arch.pv));
  221. vcpu->arch.sie_block->sdf = 0;
  222. /*
  223. * The sidad field (for sdf == 2) is now the gbea field (for sdf == 0).
  224. * Use the reset value of gbea to avoid leaking the kernel pointer of
  225. * the just freed sida.
  226. */
  227. vcpu->arch.sie_block->gbea = 1;
  228. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  229. return cc ? EIO : 0;
  230. }
  231. int kvm_s390_pv_create_cpu(struct kvm_vcpu *vcpu, u16 *rc, u16 *rrc)
  232. {
  233. struct uv_cb_csc uvcb = {
  234. .header.cmd = UVC_CMD_CREATE_SEC_CPU,
  235. .header.len = sizeof(uvcb),
  236. };
  237. void *sida_addr;
  238. int cc;
  239. if (kvm_s390_pv_cpu_get_handle(vcpu))
  240. return -EINVAL;
  241. vcpu->arch.pv.stor_base = __get_free_pages(GFP_KERNEL_ACCOUNT,
  242. get_order(uv_info.guest_cpu_stor_len));
  243. if (!vcpu->arch.pv.stor_base)
  244. return -ENOMEM;
  245. /* Input */
  246. uvcb.guest_handle = kvm_s390_pv_get_handle(vcpu->kvm);
  247. uvcb.num = vcpu->arch.sie_block->icpua;
  248. uvcb.state_origin = virt_to_phys(vcpu->arch.sie_block);
  249. uvcb.stor_origin = virt_to_phys((void *)vcpu->arch.pv.stor_base);
  250. /* Alloc Secure Instruction Data Area Designation */
  251. sida_addr = (void *)__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
  252. if (!sida_addr) {
  253. free_pages(vcpu->arch.pv.stor_base,
  254. get_order(uv_info.guest_cpu_stor_len));
  255. return -ENOMEM;
  256. }
  257. vcpu->arch.sie_block->sidad = virt_to_phys(sida_addr);
  258. cc = uv_call(0, (u64)&uvcb);
  259. *rc = uvcb.header.rc;
  260. *rrc = uvcb.header.rrc;
  261. KVM_UV_EVENT(vcpu->kvm, 3,
  262. "PROTVIRT CREATE VCPU: cpu %d handle %llx rc %x rrc %x",
  263. vcpu->vcpu_id, uvcb.cpu_handle, uvcb.header.rc,
  264. uvcb.header.rrc);
  265. if (cc) {
  266. u16 dummy;
  267. kvm_s390_pv_destroy_cpu(vcpu, &dummy, &dummy);
  268. return -EIO;
  269. }
  270. /* Output */
  271. vcpu->arch.pv.handle = uvcb.cpu_handle;
  272. vcpu->arch.sie_block->pv_handle_cpu = uvcb.cpu_handle;
  273. vcpu->arch.sie_block->pv_handle_config = kvm_s390_pv_get_handle(vcpu->kvm);
  274. vcpu->arch.sie_block->sdf = 2;
  275. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  276. return 0;
  277. }
  278. /* only free resources when the destroy was successful */
  279. static void kvm_s390_pv_dealloc_vm(struct kvm *kvm)
  280. {
  281. vfree(kvm->arch.pv.stor_var);
  282. free_pages(kvm->arch.pv.stor_base,
  283. get_order(uv_info.guest_base_stor_len));
  284. kvm_s390_clear_pv_state(kvm);
  285. }
  286. static int kvm_s390_pv_alloc_vm(struct kvm *kvm)
  287. {
  288. unsigned long base = uv_info.guest_base_stor_len;
  289. unsigned long virt = uv_info.guest_virt_var_stor_len;
  290. unsigned long npages = 0, vlen = 0;
  291. kvm->arch.pv.stor_var = NULL;
  292. kvm->arch.pv.stor_base = __get_free_pages(GFP_KERNEL_ACCOUNT, get_order(base));
  293. if (!kvm->arch.pv.stor_base)
  294. return -ENOMEM;
  295. /*
  296. * Calculate current guest storage for allocation of the
  297. * variable storage, which is based on the length in MB.
  298. *
  299. * Slots are sorted by GFN
  300. */
  301. mutex_lock(&kvm->slots_lock);
  302. npages = kvm_s390_get_gfn_end(kvm_memslots(kvm));
  303. mutex_unlock(&kvm->slots_lock);
  304. kvm->arch.pv.guest_len = npages * PAGE_SIZE;
  305. /* Allocate variable storage */
  306. vlen = ALIGN(virt * ((npages * PAGE_SIZE) / HPAGE_SIZE), PAGE_SIZE);
  307. vlen += uv_info.guest_virt_base_stor_len;
  308. kvm->arch.pv.stor_var = vzalloc(vlen);
  309. if (!kvm->arch.pv.stor_var)
  310. goto out_err;
  311. return 0;
  312. out_err:
  313. kvm_s390_pv_dealloc_vm(kvm);
  314. return -ENOMEM;
  315. }
  316. /**
  317. * kvm_s390_pv_dispose_one_leftover - Clean up one leftover protected VM.
  318. * @kvm: the KVM that was associated with this leftover protected VM
  319. * @leftover: details about the leftover protected VM that needs a clean up
  320. * @rc: the RC code of the Destroy Secure Configuration UVC
  321. * @rrc: the RRC code of the Destroy Secure Configuration UVC
  322. *
  323. * Destroy one leftover protected VM.
  324. * On success, kvm->mm->context.protected_count will be decremented atomically
  325. * and all other resources used by the VM will be freed.
  326. *
  327. * Return: 0 in case of success, otherwise 1
  328. */
  329. static int kvm_s390_pv_dispose_one_leftover(struct kvm *kvm,
  330. struct pv_vm_to_be_destroyed *leftover,
  331. u16 *rc, u16 *rrc)
  332. {
  333. int cc;
  334. /* It used the destroy-fast UVC, nothing left to do here */
  335. if (!leftover->handle)
  336. goto done_fast;
  337. cc = uv_cmd_nodata(leftover->handle, UVC_CMD_DESTROY_SEC_CONF, rc, rrc);
  338. KVM_UV_EVENT(kvm, 3, "PROTVIRT DESTROY LEFTOVER VM: rc %x rrc %x", *rc, *rrc);
  339. WARN_ONCE(cc, "protvirt destroy leftover vm failed rc %x rrc %x", *rc, *rrc);
  340. if (cc)
  341. return cc;
  342. /*
  343. * Intentionally leak unusable memory. If the UVC fails, the memory
  344. * used for the VM and its metadata is permanently unusable.
  345. * This can only happen in case of a serious KVM or hardware bug; it
  346. * is not expected to happen in normal operation.
  347. */
  348. free_pages(leftover->stor_base, get_order(uv_info.guest_base_stor_len));
  349. free_pages(leftover->old_gmap_table, CRST_ALLOC_ORDER);
  350. vfree(leftover->stor_var);
  351. done_fast:
  352. atomic_dec(&kvm->mm->context.protected_count);
  353. return 0;
  354. }
  355. static int kvm_s390_pv_deinit_vm_fast(struct kvm *kvm, u16 *rc, u16 *rrc)
  356. {
  357. struct uv_cb_destroy_fast uvcb = {
  358. .header.cmd = UVC_CMD_DESTROY_SEC_CONF_FAST,
  359. .header.len = sizeof(uvcb),
  360. .handle = kvm_s390_pv_get_handle(kvm),
  361. };
  362. int cc;
  363. cc = uv_call_sched(0, (u64)&uvcb);
  364. if (rc)
  365. *rc = uvcb.header.rc;
  366. if (rrc)
  367. *rrc = uvcb.header.rrc;
  368. KVM_UV_EVENT(kvm, 3, "PROTVIRT DESTROY VM FAST: rc %x rrc %x",
  369. uvcb.header.rc, uvcb.header.rrc);
  370. WARN_ONCE(cc && uvcb.header.rc != 0x104,
  371. "protvirt destroy vm fast failed handle %llx rc %x rrc %x",
  372. kvm_s390_pv_get_handle(kvm), uvcb.header.rc, uvcb.header.rrc);
  373. /* Intended memory leak on "impossible" error */
  374. if (!cc)
  375. kvm_s390_pv_dealloc_vm(kvm);
  376. return cc ? -EIO : 0;
  377. }
  378. static inline bool is_destroy_fast_available(void)
  379. {
  380. return test_bit_inv(BIT_UVC_CMD_DESTROY_SEC_CONF_FAST, uv_info.inst_calls_list);
  381. }
  382. /**
  383. * kvm_s390_pv_set_aside - Set aside a protected VM for later teardown.
  384. * @kvm: the VM
  385. * @rc: return value for the RC field of the UVCB
  386. * @rrc: return value for the RRC field of the UVCB
  387. *
  388. * Set aside the protected VM for a subsequent teardown. The VM will be able
  389. * to continue immediately as a non-secure VM, and the information needed to
  390. * properly tear down the protected VM is set aside. If another protected VM
  391. * was already set aside without starting its teardown, this function will
  392. * fail.
  393. * The CPUs of the protected VM need to be destroyed beforehand.
  394. *
  395. * Context: kvm->lock needs to be held
  396. *
  397. * Return: 0 in case of success, -EINVAL if another protected VM was already set
  398. * aside, -ENOMEM if the system ran out of memory.
  399. */
  400. int kvm_s390_pv_set_aside(struct kvm *kvm, u16 *rc, u16 *rrc)
  401. {
  402. struct pv_vm_to_be_destroyed *priv;
  403. int res = 0;
  404. lockdep_assert_held(&kvm->lock);
  405. /*
  406. * If another protected VM was already prepared for teardown, refuse.
  407. * A normal deinitialization has to be performed instead.
  408. */
  409. if (kvm->arch.pv.set_aside)
  410. return -EINVAL;
  411. /* Guest with segment type ASCE, refuse to destroy asynchronously */
  412. if (kvm->arch.gmap->asce.dt == TABLE_TYPE_SEGMENT)
  413. return -EINVAL;
  414. priv = kzalloc_obj(*priv);
  415. if (!priv)
  416. return -ENOMEM;
  417. if (is_destroy_fast_available()) {
  418. res = kvm_s390_pv_deinit_vm_fast(kvm, rc, rrc);
  419. } else {
  420. priv->stor_var = kvm->arch.pv.stor_var;
  421. priv->stor_base = kvm->arch.pv.stor_base;
  422. priv->handle = kvm_s390_pv_get_handle(kvm);
  423. priv->old_gmap_table = (unsigned long)dereference_asce(kvm->arch.gmap->asce);
  424. if (s390_replace_asce(kvm->arch.gmap))
  425. res = -ENOMEM;
  426. }
  427. if (res) {
  428. kfree(priv);
  429. return res;
  430. }
  431. gmap_pv_destroy_range(kvm->arch.gmap, 0, gpa_to_gfn(SZ_2G), false);
  432. kvm_s390_clear_pv_state(kvm);
  433. kvm->arch.pv.set_aside = priv;
  434. *rc = UVC_RC_EXECUTED;
  435. *rrc = 42;
  436. return 0;
  437. }
  438. /**
  439. * kvm_s390_pv_deinit_vm - Deinitialize the current protected VM
  440. * @kvm: the KVM whose protected VM needs to be deinitialized
  441. * @rc: the RC code of the UVC
  442. * @rrc: the RRC code of the UVC
  443. *
  444. * Deinitialize the current protected VM. This function will destroy and
  445. * cleanup the current protected VM, but it will not cleanup the guest
  446. * memory. This function should only be called when the protected VM has
  447. * just been created and therefore does not have any guest memory, or when
  448. * the caller cleans up the guest memory separately.
  449. *
  450. * This function should not fail, but if it does, the donated memory must
  451. * not be freed.
  452. *
  453. * Context: kvm->lock needs to be held
  454. *
  455. * Return: 0 in case of success, otherwise -EIO
  456. */
  457. int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
  458. {
  459. int cc;
  460. cc = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
  461. UVC_CMD_DESTROY_SEC_CONF, rc, rrc);
  462. if (!cc) {
  463. atomic_dec(&kvm->mm->context.protected_count);
  464. kvm_s390_pv_dealloc_vm(kvm);
  465. } else {
  466. /* Intended memory leak on "impossible" error */
  467. s390_replace_asce(kvm->arch.gmap);
  468. }
  469. KVM_UV_EVENT(kvm, 3, "PROTVIRT DESTROY VM: rc %x rrc %x", *rc, *rrc);
  470. WARN_ONCE(cc, "protvirt destroy vm failed rc %x rrc %x", *rc, *rrc);
  471. return cc ? -EIO : 0;
  472. }
  473. /**
  474. * kvm_s390_pv_deinit_cleanup_all - Clean up all protected VMs associated
  475. * with a specific KVM.
  476. * @kvm: the KVM to be cleaned up
  477. * @rc: the RC code of the first failing UVC
  478. * @rrc: the RRC code of the first failing UVC
  479. *
  480. * This function will clean up all protected VMs associated with a KVM.
  481. * This includes the active one, the one prepared for deinitialization with
  482. * kvm_s390_pv_set_aside, and any still pending in the need_cleanup list.
  483. *
  484. * Context: kvm->lock needs to be held unless being called from
  485. * kvm_arch_destroy_vm.
  486. *
  487. * Return: 0 if all VMs are successfully cleaned up, otherwise -EIO
  488. */
  489. int kvm_s390_pv_deinit_cleanup_all(struct kvm *kvm, u16 *rc, u16 *rrc)
  490. {
  491. struct pv_vm_to_be_destroyed *cur;
  492. bool need_zap = false;
  493. u16 _rc, _rrc;
  494. int cc = 0;
  495. /*
  496. * Nothing to do if the counter was already 0. Otherwise make sure
  497. * the counter does not reach 0 before calling s390_uv_destroy_range.
  498. */
  499. if (!atomic_inc_not_zero(&kvm->mm->context.protected_count))
  500. return 0;
  501. *rc = 1;
  502. /* If the current VM is protected, destroy it */
  503. if (kvm_s390_pv_get_handle(kvm)) {
  504. cc = kvm_s390_pv_deinit_vm(kvm, rc, rrc);
  505. need_zap = true;
  506. }
  507. /* If a previous protected VM was set aside, put it in the need_cleanup list */
  508. if (kvm->arch.pv.set_aside) {
  509. list_add(kvm->arch.pv.set_aside, &kvm->arch.pv.need_cleanup);
  510. kvm->arch.pv.set_aside = NULL;
  511. }
  512. /* Cleanup all protected VMs in the need_cleanup list */
  513. while (!list_empty(&kvm->arch.pv.need_cleanup)) {
  514. cur = list_first_entry(&kvm->arch.pv.need_cleanup, typeof(*cur), list);
  515. need_zap = true;
  516. if (kvm_s390_pv_dispose_one_leftover(kvm, cur, &_rc, &_rrc)) {
  517. cc = 1;
  518. /*
  519. * Only return the first error rc and rrc, so make
  520. * sure it is not overwritten. All destroys will
  521. * additionally be reported via KVM_UV_EVENT().
  522. */
  523. if (*rc == UVC_RC_EXECUTED) {
  524. *rc = _rc;
  525. *rrc = _rrc;
  526. }
  527. }
  528. list_del(&cur->list);
  529. kfree(cur);
  530. }
  531. /*
  532. * If the mm still has a mapping, try to mark all its pages as
  533. * accessible. The counter should not reach zero before this
  534. * cleanup has been performed.
  535. */
  536. if (need_zap && mmget_not_zero(kvm->mm)) {
  537. gmap_pv_destroy_range(kvm->arch.gmap, 0, asce_end(kvm->arch.gmap->asce), false);
  538. mmput(kvm->mm);
  539. }
  540. /* Now the counter can safely reach 0 */
  541. atomic_dec(&kvm->mm->context.protected_count);
  542. return cc ? -EIO : 0;
  543. }
  544. /**
  545. * kvm_s390_pv_deinit_aside_vm - Teardown a previously set aside protected VM.
  546. * @kvm: the VM previously associated with the protected VM
  547. * @rc: return value for the RC field of the UVCB
  548. * @rrc: return value for the RRC field of the UVCB
  549. *
  550. * Tear down the protected VM that had been previously prepared for teardown
  551. * using kvm_s390_pv_set_aside_vm. Ideally this should be called by
  552. * userspace asynchronously from a separate thread.
  553. *
  554. * Context: kvm->lock must not be held.
  555. *
  556. * Return: 0 in case of success, -EINVAL if no protected VM had been
  557. * prepared for asynchronous teardowm, -EIO in case of other errors.
  558. */
  559. int kvm_s390_pv_deinit_aside_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
  560. {
  561. struct pv_vm_to_be_destroyed *p;
  562. int ret = 0;
  563. lockdep_assert_not_held(&kvm->lock);
  564. mutex_lock(&kvm->lock);
  565. p = kvm->arch.pv.set_aside;
  566. kvm->arch.pv.set_aside = NULL;
  567. mutex_unlock(&kvm->lock);
  568. if (!p)
  569. return -EINVAL;
  570. /* When a fatal signal is received, stop immediately */
  571. if (gmap_pv_destroy_range(kvm->arch.gmap, 0, asce_end(kvm->arch.gmap->asce), true))
  572. goto done;
  573. if (kvm_s390_pv_dispose_one_leftover(kvm, p, rc, rrc))
  574. ret = -EIO;
  575. kfree(p);
  576. p = NULL;
  577. done:
  578. /*
  579. * p is not NULL if we aborted because of a fatal signal, in which
  580. * case queue the leftover for later cleanup.
  581. */
  582. if (p) {
  583. mutex_lock(&kvm->lock);
  584. list_add(&p->list, &kvm->arch.pv.need_cleanup);
  585. mutex_unlock(&kvm->lock);
  586. /* Did not finish, but pretend things went well */
  587. *rc = UVC_RC_EXECUTED;
  588. *rrc = 42;
  589. }
  590. return ret;
  591. }
  592. static void kvm_s390_pv_mmu_notifier_release(struct mmu_notifier *subscription,
  593. struct mm_struct *mm)
  594. {
  595. struct kvm *kvm = container_of(subscription, struct kvm, arch.pv.mmu_notifier);
  596. u16 dummy;
  597. int r;
  598. /*
  599. * No locking is needed since this is the last thread of the last user of this
  600. * struct mm.
  601. * When the struct kvm gets deinitialized, this notifier is also
  602. * unregistered. This means that if this notifier runs, then the
  603. * struct kvm is still valid.
  604. */
  605. r = kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
  606. if (!r && is_destroy_fast_available() && kvm_s390_pv_get_handle(kvm))
  607. kvm_s390_pv_deinit_vm_fast(kvm, &dummy, &dummy);
  608. set_bit(GMAP_FLAG_EXPORT_ON_UNMAP, &kvm->arch.gmap->flags);
  609. }
  610. static const struct mmu_notifier_ops kvm_s390_pv_mmu_notifier_ops = {
  611. .release = kvm_s390_pv_mmu_notifier_release,
  612. };
  613. int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
  614. {
  615. struct uv_cb_cgc uvcb = {
  616. .header.cmd = UVC_CMD_CREATE_SEC_CONF,
  617. .header.len = sizeof(uvcb)
  618. };
  619. int cc, ret;
  620. u16 dummy;
  621. /* Add the notifier only once. No races because we hold kvm->lock */
  622. if (kvm->arch.pv.mmu_notifier.ops != &kvm_s390_pv_mmu_notifier_ops) {
  623. /* The notifier will be unregistered when the VM is destroyed */
  624. kvm->arch.pv.mmu_notifier.ops = &kvm_s390_pv_mmu_notifier_ops;
  625. ret = mmu_notifier_register(&kvm->arch.pv.mmu_notifier, kvm->mm);
  626. if (ret) {
  627. kvm->arch.pv.mmu_notifier.ops = NULL;
  628. return ret;
  629. }
  630. }
  631. ret = kvm_s390_pv_alloc_vm(kvm);
  632. if (ret)
  633. return ret;
  634. /* Inputs */
  635. uvcb.guest_stor_origin = 0; /* MSO is 0 for KVM */
  636. uvcb.guest_stor_len = kvm->arch.pv.guest_len;
  637. uvcb.guest_asce = kvm->arch.gmap->asce.val;
  638. uvcb.guest_sca = virt_to_phys(kvm->arch.sca);
  639. uvcb.conf_base_stor_origin =
  640. virt_to_phys((void *)kvm->arch.pv.stor_base);
  641. uvcb.conf_virt_stor_origin = (u64)kvm->arch.pv.stor_var;
  642. uvcb.flags.ap_allow_instr = kvm->arch.model.uv_feat_guest.ap;
  643. uvcb.flags.ap_instr_intr = kvm->arch.model.uv_feat_guest.ap_intr;
  644. clear_bit(GMAP_FLAG_ALLOW_HPAGE_1M, &kvm->arch.gmap->flags);
  645. gmap_split_huge_pages(kvm->arch.gmap);
  646. cc = uv_call_sched(0, (u64)&uvcb);
  647. *rc = uvcb.header.rc;
  648. *rrc = uvcb.header.rrc;
  649. KVM_UV_EVENT(kvm, 3, "PROTVIRT CREATE VM: handle %llx len %llx rc %x rrc %x flags %04x",
  650. uvcb.guest_handle, uvcb.guest_stor_len, *rc, *rrc, uvcb.flags.raw);
  651. /* Outputs */
  652. kvm->arch.pv.handle = uvcb.guest_handle;
  653. atomic_inc(&kvm->mm->context.protected_count);
  654. if (cc) {
  655. if (uvcb.header.rc & UVC_RC_NEED_DESTROY) {
  656. kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
  657. } else {
  658. atomic_dec(&kvm->mm->context.protected_count);
  659. kvm_s390_pv_dealloc_vm(kvm);
  660. }
  661. return -EIO;
  662. }
  663. return 0;
  664. }
  665. int kvm_s390_pv_set_sec_parms(struct kvm *kvm, void *hdr, u64 length, u16 *rc,
  666. u16 *rrc)
  667. {
  668. struct uv_cb_ssc uvcb = {
  669. .header.cmd = UVC_CMD_SET_SEC_CONF_PARAMS,
  670. .header.len = sizeof(uvcb),
  671. .sec_header_origin = (u64)hdr,
  672. .sec_header_len = length,
  673. .guest_handle = kvm_s390_pv_get_handle(kvm),
  674. };
  675. int cc = uv_call(0, (u64)&uvcb);
  676. *rc = uvcb.header.rc;
  677. *rrc = uvcb.header.rrc;
  678. KVM_UV_EVENT(kvm, 3, "PROTVIRT VM SET PARMS: rc %x rrc %x",
  679. *rc, *rrc);
  680. return cc ? -EINVAL : 0;
  681. }
  682. static int unpack_one(struct kvm *kvm, unsigned long addr, u64 tweak,
  683. u64 offset, u16 *rc, u16 *rrc)
  684. {
  685. struct uv_cb_unp uvcb = {
  686. .header.cmd = UVC_CMD_UNPACK_IMG,
  687. .header.len = sizeof(uvcb),
  688. .guest_handle = kvm_s390_pv_get_handle(kvm),
  689. .gaddr = addr,
  690. .tweak[0] = tweak,
  691. .tweak[1] = offset,
  692. };
  693. int ret = kvm_s390_pv_make_secure(kvm, addr, &uvcb);
  694. *rc = uvcb.header.rc;
  695. *rrc = uvcb.header.rrc;
  696. if (ret == -ENXIO) {
  697. ret = kvm_s390_faultin_gfn_simple(NULL, kvm, gpa_to_gfn(addr), true);
  698. if (!ret)
  699. return -EAGAIN;
  700. }
  701. if (ret && ret != -EAGAIN)
  702. KVM_UV_EVENT(kvm, 3, "PROTVIRT VM UNPACK: failed addr %llx with rc %x rrc %x",
  703. uvcb.gaddr, *rc, *rrc);
  704. return ret;
  705. }
  706. int kvm_s390_pv_unpack(struct kvm *kvm, unsigned long addr, unsigned long size,
  707. unsigned long tweak, u16 *rc, u16 *rrc)
  708. {
  709. u64 offset = 0;
  710. int ret = 0;
  711. if (addr & ~PAGE_MASK || !size || size & ~PAGE_MASK)
  712. return -EINVAL;
  713. KVM_UV_EVENT(kvm, 3, "PROTVIRT VM UNPACK: start addr %lx size %lx",
  714. addr, size);
  715. guard(srcu)(&kvm->srcu);
  716. while (offset < size) {
  717. ret = unpack_one(kvm, addr, tweak, offset, rc, rrc);
  718. if (ret == -EAGAIN) {
  719. cond_resched();
  720. if (fatal_signal_pending(current))
  721. break;
  722. continue;
  723. }
  724. if (ret)
  725. break;
  726. addr += PAGE_SIZE;
  727. offset += PAGE_SIZE;
  728. }
  729. if (!ret)
  730. KVM_UV_EVENT(kvm, 3, "%s", "PROTVIRT VM UNPACK: successful");
  731. return ret;
  732. }
  733. int kvm_s390_pv_set_cpu_state(struct kvm_vcpu *vcpu, u8 state)
  734. {
  735. struct uv_cb_cpu_set_state uvcb = {
  736. .header.cmd = UVC_CMD_CPU_SET_STATE,
  737. .header.len = sizeof(uvcb),
  738. .cpu_handle = kvm_s390_pv_cpu_get_handle(vcpu),
  739. .state = state,
  740. };
  741. int cc;
  742. cc = uv_call(0, (u64)&uvcb);
  743. KVM_UV_EVENT(vcpu->kvm, 3, "PROTVIRT SET CPU %d STATE %d rc %x rrc %x",
  744. vcpu->vcpu_id, state, uvcb.header.rc, uvcb.header.rrc);
  745. if (cc)
  746. return -EINVAL;
  747. return 0;
  748. }
  749. int kvm_s390_pv_dump_cpu(struct kvm_vcpu *vcpu, void *buff, u16 *rc, u16 *rrc)
  750. {
  751. struct uv_cb_dump_cpu uvcb = {
  752. .header.cmd = UVC_CMD_DUMP_CPU,
  753. .header.len = sizeof(uvcb),
  754. .cpu_handle = vcpu->arch.pv.handle,
  755. .dump_area_origin = (u64)buff,
  756. };
  757. int cc;
  758. cc = uv_call_sched(0, (u64)&uvcb);
  759. *rc = uvcb.header.rc;
  760. *rrc = uvcb.header.rrc;
  761. return cc;
  762. }
  763. /* Size of the cache for the storage state dump data. 1MB for now */
  764. #define DUMP_BUFF_LEN HPAGE_SIZE
  765. /**
  766. * kvm_s390_pv_dump_stor_state
  767. *
  768. * @kvm: pointer to the guest's KVM struct
  769. * @buff_user: Userspace pointer where we will write the results to
  770. * @gaddr: Starting absolute guest address for which the storage state
  771. * is requested.
  772. * @buff_user_len: Length of the buff_user buffer
  773. * @rc: Pointer to where the uvcb return code is stored
  774. * @rrc: Pointer to where the uvcb return reason code is stored
  775. *
  776. * Stores buff_len bytes of tweak component values to buff_user
  777. * starting with the 1MB block specified by the absolute guest address
  778. * (gaddr). The gaddr pointer will be updated with the last address
  779. * for which data was written when returning to userspace. buff_user
  780. * might be written to even if an error rc is returned. For instance
  781. * if we encounter a fault after writing the first page of data.
  782. *
  783. * Context: kvm->lock needs to be held
  784. *
  785. * Return:
  786. * 0 on success
  787. * -ENOMEM if allocating the cache fails
  788. * -EINVAL if gaddr is not aligned to 1MB
  789. * -EINVAL if buff_user_len is not aligned to uv_info.conf_dump_storage_state_len
  790. * -EINVAL if the UV call fails, rc and rrc will be set in this case
  791. * -EFAULT if copying the result to buff_user failed
  792. */
  793. int kvm_s390_pv_dump_stor_state(struct kvm *kvm, void __user *buff_user,
  794. u64 *gaddr, u64 buff_user_len, u16 *rc, u16 *rrc)
  795. {
  796. struct uv_cb_dump_stor_state uvcb = {
  797. .header.cmd = UVC_CMD_DUMP_CONF_STOR_STATE,
  798. .header.len = sizeof(uvcb),
  799. .config_handle = kvm->arch.pv.handle,
  800. .gaddr = *gaddr,
  801. .dump_area_origin = 0,
  802. };
  803. const u64 increment_len = uv_info.conf_dump_storage_state_len;
  804. size_t buff_kvm_size;
  805. size_t size_done = 0;
  806. u8 *buff_kvm = NULL;
  807. int cc, ret;
  808. ret = -EINVAL;
  809. /* UV call processes 1MB guest storage chunks at a time */
  810. if (!IS_ALIGNED(*gaddr, HPAGE_SIZE))
  811. goto out;
  812. /*
  813. * We provide the storage state for 1MB chunks of guest
  814. * storage. The buffer will need to be aligned to
  815. * conf_dump_storage_state_len so we don't end on a partial
  816. * chunk.
  817. */
  818. if (!buff_user_len ||
  819. !IS_ALIGNED(buff_user_len, increment_len))
  820. goto out;
  821. /*
  822. * Allocate a buffer from which we will later copy to the user
  823. * process. We don't want userspace to dictate our buffer size
  824. * so we limit it to DUMP_BUFF_LEN.
  825. */
  826. ret = -ENOMEM;
  827. buff_kvm_size = min_t(u64, buff_user_len, DUMP_BUFF_LEN);
  828. buff_kvm = vzalloc(buff_kvm_size);
  829. if (!buff_kvm)
  830. goto out;
  831. ret = 0;
  832. uvcb.dump_area_origin = (u64)buff_kvm;
  833. /* We will loop until the user buffer is filled or an error occurs */
  834. do {
  835. /* Get 1MB worth of guest storage state data */
  836. cc = uv_call_sched(0, (u64)&uvcb);
  837. /* All or nothing */
  838. if (cc) {
  839. ret = -EINVAL;
  840. break;
  841. }
  842. size_done += increment_len;
  843. uvcb.dump_area_origin += increment_len;
  844. buff_user_len -= increment_len;
  845. uvcb.gaddr += HPAGE_SIZE;
  846. /* KVM Buffer full, time to copy to the process */
  847. if (!buff_user_len || size_done == DUMP_BUFF_LEN) {
  848. if (copy_to_user(buff_user, buff_kvm, size_done)) {
  849. ret = -EFAULT;
  850. break;
  851. }
  852. buff_user += size_done;
  853. size_done = 0;
  854. uvcb.dump_area_origin = (u64)buff_kvm;
  855. }
  856. } while (buff_user_len);
  857. /* Report back where we ended dumping */
  858. *gaddr = uvcb.gaddr;
  859. /* Lets only log errors, we don't want to spam */
  860. out:
  861. if (ret)
  862. KVM_UV_EVENT(kvm, 3,
  863. "PROTVIRT DUMP STORAGE STATE: addr %llx ret %d, uvcb rc %x rrc %x",
  864. uvcb.gaddr, ret, uvcb.header.rc, uvcb.header.rrc);
  865. *rc = uvcb.header.rc;
  866. *rrc = uvcb.header.rrc;
  867. vfree(buff_kvm);
  868. return ret;
  869. }
  870. /**
  871. * kvm_s390_pv_dump_complete
  872. *
  873. * @kvm: pointer to the guest's KVM struct
  874. * @buff_user: Userspace pointer where we will write the results to
  875. * @rc: Pointer to where the uvcb return code is stored
  876. * @rrc: Pointer to where the uvcb return reason code is stored
  877. *
  878. * Completes the dumping operation and writes the completion data to
  879. * user space.
  880. *
  881. * Context: kvm->lock needs to be held
  882. *
  883. * Return:
  884. * 0 on success
  885. * -ENOMEM if allocating the completion buffer fails
  886. * -EINVAL if the UV call fails, rc and rrc will be set in this case
  887. * -EFAULT if copying the result to buff_user failed
  888. */
  889. int kvm_s390_pv_dump_complete(struct kvm *kvm, void __user *buff_user,
  890. u16 *rc, u16 *rrc)
  891. {
  892. struct uv_cb_dump_complete complete = {
  893. .header.len = sizeof(complete),
  894. .header.cmd = UVC_CMD_DUMP_COMPLETE,
  895. .config_handle = kvm_s390_pv_get_handle(kvm),
  896. };
  897. u64 *compl_data;
  898. int ret;
  899. /* Allocate dump area */
  900. compl_data = vzalloc(uv_info.conf_dump_finalize_len);
  901. if (!compl_data)
  902. return -ENOMEM;
  903. complete.dump_area_origin = (u64)compl_data;
  904. ret = uv_call_sched(0, (u64)&complete);
  905. *rc = complete.header.rc;
  906. *rrc = complete.header.rrc;
  907. KVM_UV_EVENT(kvm, 3, "PROTVIRT DUMP COMPLETE: rc %x rrc %x",
  908. complete.header.rc, complete.header.rrc);
  909. if (!ret) {
  910. /*
  911. * kvm_s390_pv_dealloc_vm() will also (mem)set
  912. * this to false on a reboot or other destroy
  913. * operation for this vm.
  914. */
  915. kvm->arch.pv.dumping = false;
  916. kvm_s390_vcpu_unblock_all(kvm);
  917. ret = copy_to_user(buff_user, compl_data, uv_info.conf_dump_finalize_len);
  918. if (ret)
  919. ret = -EFAULT;
  920. }
  921. vfree(compl_data);
  922. /* If the UVC returned an error, translate it to -EINVAL */
  923. if (ret > 0)
  924. ret = -EINVAL;
  925. return ret;
  926. }