| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/08/27 16:16 | flow | patch-triage |
1mArgs:null Results: {
"EnableConfigs": [
"KVM"
],
"FocusSymbols": [
"kvm_s390_handle_vsie",
"kvm_s390_vsie_gmap_notifier"
],
"KMSANReasoning": "The patch introduces new structures (`vsie_sca`, `vsie_page`) and logic for shadowing the System Control Area (SCA) in s390 KVM nested virtualization (VSIE). All new memory allocations for these structures explicitly use `__GFP_ZERO` (e.g., `alloc_pages_exact(..., GFP_KERNEL_ACCOUNT | __GFP_ZERO)` and `__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA)`). When these structures are reused from caches, they are explicitly cleared using `memset` (e.g., in `clear_vsie_sca` and `reset_vsie_page`). The patch does not introduce any new data copies to userspace, and all hardware-shared shadow structures are fully zero-initialized before use. Any potential bugs introduced by this patch would be logic errors, out-of-bounds accesses, or use-after-frees, which are effectively caught by KASAN. There is no risk of uninitialized memory usage or info-leaks, making a dedicated KMSAN session unnecessary.",
"NeedsKMSAN": false,
"Reasoning": "The patch implements shadowing of the System Control Area (SCA) for nested virtualization (vSIE) on s390x architecture. This allows nested guests to use the SSCA for SIGPI and SRSI instructions. The changes involve significant modifications to the vSIE page management, SCA shadowing logic, and memory pinning in `arch/s390/kvm/s390/vsie.c`. These are reachable core KVM features when running nested guests, making them highly relevant for fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/08/27 16:16 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 25a55cc0e16c4ee5e1af3cd4c4c18a54af94e43c\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Thu Aug 27 16:16:25 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/arch/s390/include/asm/kvm_host_s390.h b/arch/s390/include/asm/kvm_host_s390.h\nindex cd692f8fb7643..a19d3a5f550ce 100644\n--- a/arch/s390/include/asm/kvm_host_s390.h\n+++ b/arch/s390/include/asm/kvm_host_s390.h\n@@ -412,6 +412,10 @@ struct kvm_vm_stat {\n \tu64 gmap_shadow_r3_entry;\n \tu64 gmap_shadow_sg_entry;\n \tu64 gmap_shadow_pg_entry;\n+\tu64 vsie_shadow_scb;\n+\tu64 vsie_shadow_scb_reuse;\n+\tu64 vsie_shadow_sca;\n+\tu64 vsie_shadow_sca_reuse;\n };\n \n struct kvm_arch_memory_slot {\n@@ -572,13 +576,32 @@ struct sie_page2 {\n };\n \n struct vsie_page;\n+struct vsie_sca;\n \n+/*\n+ * vsie_pages, scas and accompanied management vars\n+ */\n struct kvm_s390_vsie {\n+\t/*\n+\t * protects pages[], page_count, next, addr_to_page\n+\t */\n \tstruct mutex mutex;\n-\tstruct radix_tree_root addr_to_page;\n+\tstruct xarray addr_to_page;\n \tint page_count;\n \tint next;\n-\tstruct vsie_page *pages[KVM_MAX_VCPUS];\n+\tstruct vsie_page *pages[KVM_S390_MAX_VSIE_VCPUS];\n+\t/*\n+\t * The vsie_sca_lock is used to synchronize access to\n+\t * - the kvm_s390_vsie.scas[]\n+\t * - the kvm_s390_vsie.osca_to_sca map\n+\t * - sca_count and sca_next\n+\t * - new vsie_sca creation and initialization\n+\t */\n+\tstruct rw_semaphore vsie_sca_lock;\n+\tstruct xarray osca_to_sca;\n+\tint sca_count;\n+\tint sca_next;\n+\tstruct vsie_sca *scas[KVM_S390_MAX_VSIE_VCPUS];\n };\n \n struct kvm_s390_gisa_iam {\n@@ -623,6 +646,7 @@ struct kvm_arch {\n \tint use_pfmfi;\n \tint use_skf;\n \tint use_zpci_interp;\n+\tint use_ssca;\n \tint user_cpu_state_ctrl;\n \tint user_sigp;\n \tint user_stsi;\ndiff --git a/arch/s390/include/asm/kvm_host_s390_types.h b/arch/s390/include/asm/kvm_host_s390_types.h\nindex 9e348a5304217..6eef71072b4ed 100644\n--- a/arch/s390/include/asm/kvm_host_s390_types.h\n+++ b/arch/s390/include/asm/kvm_host_s390_types.h\n@@ -6,10 +6,15 @@\n #include \u003clinux/atomic.h\u003e\n #include \u003clinux/types.h\u003e\n \n+#define KVM_S390_CPU_MASK 0xff\n+#define KVM_S390_MAX_VSIE_VCPUS 256\n+#define KVM_S390_MAX_SCA_PAGES 5\n+\n #define KVM_S390_BSCA_CPU_SLOTS 64\n #define KVM_S390_ESCA_CPU_SLOTS 248\n \n #define SCB_ALIGNMENT_SHIFT 9\n+#define SCA_ALIGNMENT_SHIFT 6\n \n #define SIGP_CTRL_C\t\t0x80\n #define SIGP_CTRL_SCN_MASK\t0x3f\n@@ -47,6 +52,13 @@ struct bsca_entry {\n \t__u64\treserved2[2];\n };\n \n+struct ssca_entry {\n+\t__u64\treserved1;\n+\t__u64\tssda;\n+\t__u64\tossea;\n+\t__u64\treserved2;\n+};\n+\n union ipte_control {\n \tunsigned long val;\n \tstruct {\n@@ -88,6 +100,20 @@ struct esca_block {\n \tstruct esca_entry cpu[KVM_S390_ESCA_CPU_SLOTS];\n };\n \n+/*\n+ * The shadow sca / ssca needs to cover both bsca and esca depending on what the\n+ * guest uses so we allocate space for 256 entries that are defined in the\n+ * architecture.\n+ * The header part of the struct must not cross page boundaries.\n+ */\n+struct ssca_block {\n+\t__u64\tosca;\n+\t__u64\treserved08[7];\n+\tstruct ssca_entry cpu[KVM_S390_MAX_VSIE_VCPUS];\n+};\n+\n+static_assert(offsetof(struct ssca_block, cpu) == 64);\n+\n /*\n * This struct is used to store some machine check info from lowcore\n * for machine checks that happen while the guest is running.\n@@ -317,7 +343,7 @@ struct kvm_s390_sie_block {\n \t__u32\tfac;\t\t\t/* 0x01a0 */\n \t__u8\treserved1a4[20];\t/* 0x01a4 */\n \t__u64\tcbrlo;\t\t\t/* 0x01b8 */\n-\t__u8\treserved1c0[8];\t\t/* 0x01c0 */\n+\t__u64\tosda;\t\t\t/* 0x01c0 */\n #define ECD_HOSTREGMGMT\t0x20000000\n #define ECD_MEF\t\t0x08000000\n #define ECD_ETOKENF\t0x02000000\ndiff --git a/arch/s390/include/asm/sclp.h b/arch/s390/include/asm/sclp.h\nindex 44066550b9b10..9e960c09ce0b3 100644\n--- a/arch/s390/include/asm/sclp.h\n+++ b/arch/s390/include/asm/sclp.h\n@@ -105,6 +105,7 @@ struct sclp_info {\n \tunsigned char has_dirq : 1;\n \tunsigned char has_iplcc : 1;\n \tunsigned char has_zpci_lsi : 1;\n+\tunsigned char has_vsie_interp_extf : 1;\n \tunsigned char has_aisii : 1;\n \tunsigned char has_aeni : 1;\n \tunsigned char has_aisi : 1;\ndiff --git a/arch/s390/kvm/s390/s390.c b/arch/s390/kvm/s390/s390.c\nindex b0839e887221e..ffa5bbb8713bc 100644\n--- a/arch/s390/kvm/s390/s390.c\n+++ b/arch/s390/kvm/s390/s390.c\n@@ -87,6 +87,10 @@ const struct kvm_stats_desc kvm_vm_stats_desc[] = {\n \tSTATS_DESC_COUNTER(VM, gmap_shadow_r3_entry),\n \tSTATS_DESC_COUNTER(VM, gmap_shadow_sg_entry),\n \tSTATS_DESC_COUNTER(VM, gmap_shadow_pg_entry),\n+\tSTATS_DESC_COUNTER(VM, vsie_shadow_scb),\n+\tSTATS_DESC_COUNTER(VM, vsie_shadow_scb_reuse),\n+\tSTATS_DESC_COUNTER(VM, vsie_shadow_sca),\n+\tSTATS_DESC_COUNTER(VM, vsie_shadow_sca_reuse),\n };\n \n const struct kvm_stats_header kvm_vm_stats_header = {\n@@ -244,6 +248,11 @@ static int async_destroy = 1;\n module_param(async_destroy, int, 0444);\n MODULE_PARM_DESC(async_destroy, \"Asynchronous destroy for protected guests\");\n \n+/* allow vsie sigp and srs interpretation if available */\n+static bool ssca = true;\n+module_param(ssca, bool, 0444);\n+MODULE_PARM_DESC(ssca, \"VSIE SIGPI and SRSI using the SSCA\");\n+\n #define HMFAI_DWORDS 16\n /*\n * Base feature mask that defines default mask for facilities. Consists of the\n@@ -473,6 +482,8 @@ static void __init kvm_s390_cpu_feat_init(void)\n \t\tallow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);\n \tif (sclp.has_astfleie2)\n \t\tallow_cpu_feat(KVM_S390_VM_CPU_FEAT_ASTFLEIE2);\n+\tif (sclp.has_vsie_interp_extf \u0026\u0026 ssca)\n+\t\tallow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIGPIF);\n \t/*\n \t * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make\n \t * all skey handling functions read/set the skey from the PGSTE\n@@ -487,9 +498,6 @@ static void __init kvm_s390_cpu_feat_init(void)\n \t * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and\n \t * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be\n \t * correctly shadowed. We can do that for the PGSTE but not for PTE.I.\n-\t *\n-\t * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We\n-\t * cannot easily shadow the SCA because of the ipte lock.\n \t */\n }\n \n@@ -3359,6 +3367,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)\n \n \tkvm-\u003earch.use_pfmfi = sclp.has_pfmfi;\n \tkvm-\u003earch.use_skf = sclp.has_skey;\n+\tkvm-\u003earch.use_ssca = sclp.has_vsie_interp_extf \u0026\u0026 ssca;\n \tspin_lock_init(\u0026kvm-\u003earch.start_stop_lock);\n \tkvm_s390_vsie_init(kvm);\n \tif (use_gisa)\ndiff --git a/arch/s390/kvm/s390/vsie.c b/arch/s390/kvm/s390/vsie.c\nindex 5926689fa2267..67b68f4302b74 100644\n--- a/arch/s390/kvm/s390/vsie.c\n+++ b/arch/s390/kvm/s390/vsie.c\n@@ -29,6 +29,7 @@\n \n enum vsie_page_flags {\n \tVSIE_PAGE_IN_USE = 0,\n+\tVSIE_PAGE_SCB_PINNED = 1,\n };\n \n struct vsie_page {\n@@ -56,13 +57,13 @@ struct vsie_page {\n \tgpa_t sdnx_gpa;\t\t\t\t/* 0x0250 */\n \t/*\n \t * guest address of the original SCB. Remains set for free vsie\n-\t * pages, so we can properly look them up in our addr_to_page\n-\t * radix tree.\n+\t * pages, so we can properly look them up in our addr_to_page map.\n \t */\n \tgpa_t scb_gpa;\t\t\t\t/* 0x0258 */\n \t/* the shadow gmap in use by the vsie_page */\n \tstruct gmap_cache gmap_cache;\t\t/* 0x0260 */\n-\t__u8 reserved[0x06f8 - 0x0278];\t\t/* 0x0278 */\n+\tstruct vsie_sca *vsie_sca;\t\t/* 0x0278 */\n+\t__u8 reserved[0x06f8 - 0x0280];\t\t/* 0x0280 */\n \tstruct kvm_s390_crypto_cb crycb;\t/* 0x06f8 */\n \t__u8 fac[8 + S390_ARCH_FAC_LIST_SIZE_BYTE];/* 0x07f8 */\n };\n@@ -71,6 +72,88 @@ static_assert(sizeof(struct vsie_page) == PAGE_SIZE);\n static_assert(offsetof(struct vsie_page, mcck_info) == offsetof(struct sie_page, mcck_info));\n static_assert(IS_ALIGNED(offsetof(struct vsie_page, crycb), 8));\n \n+struct kvm_address_pair {\n+\tgpa_t gpa;\n+\thpa_t hpa;\n+};\n+\n+enum vsie_sca_flags {\n+\tVSIE_SCA_ESCA = 0,\n+\tVSIE_SCA_SCA_PINNED = 1,\n+};\n+\n+struct vsie_sca {\n+\tstruct_group(head,\n+\t\tstruct ssca_block\tssca;\n+\t);\n+\tstruct_group(keep,\n+\t\tstruct vsie_page\t*pages[KVM_S390_MAX_VSIE_VCPUS];\n+\t\t/* The mutex is used to synchronize access to the pages[] */\n+\t\tstruct mutex\t\tmutex;\n+\t\tatomic_t\t\tref_count;\n+\t);\n+\tstruct_group(tail,\n+\t\tgpa_t\t\t\tsca_gpa;\n+\t\tunsigned long\t\tflags;\n+\t\tu64\t\t\tmcn[4];\n+\t\tunsigned int\t\tsca_o_nr_pages;\n+\t\tstruct kvm_address_pair\tsca_o_pages[KVM_S390_MAX_SCA_PAGES];\n+\t);\n+};\n+\n+/*\n+ * SSCA needs to be 32-byte aligned and members before the cpu field need to be on the same page.\n+ * Forcing it to offset 0 will always fulfill the requirements.\n+ */\n+static_assert(!(offsetof(struct vsie_sca, ssca)));\n+\n+/* maximum vsie shadow scb */\n+static unsigned int vsie_shadow_scb_max = 1;\n+module_param(vsie_shadow_scb_max, uint, 0644);\n+MODULE_PARM_DESC(vsie_shadow_scb_max, \"Maximum number of VSIE shadow control blocks to keep. Values smaller number VCPUs uses number of VCPUs; maximum 256\");\n+\n+/* maximum vsie shadow sca */\n+static unsigned int vsie_shadow_sca_max = 1;\n+module_param(vsie_shadow_sca_max, uint, 0644);\n+MODULE_PARM_DESC(vsie_shadow_sca_max, \"Maximum number of VSIE shadow system control areas to keep. Values smaller number of VCPUs uses number of VCPUs; maximum 256\");\n+\n+static inline hpa_t sca_o_hpa(struct vsie_sca *vsie_sca)\n+{\n+\treturn vsie_sca-\u003esca_o_pages[0].hpa | (vsie_sca-\u003esca_gpa \u0026 ~PAGE_MASK);\n+}\n+\n+static inline bool sie_uses_esca(struct kvm_s390_sie_block *scb)\n+{\n+\treturn (scb-\u003eecb2 \u0026 ECB2_ESCA);\n+}\n+\n+static unsigned long read_scao(struct kvm *kvm, struct kvm_s390_sie_block *scb)\n+{\n+\tunsigned long vsie_sca = READ_ONCE(scb-\u003escaol) \u0026 ~0xfUL;\n+\n+\tif (test_kvm_cpu_feat(kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))\n+\t\tvsie_sca |= (u64)READ_ONCE(scb-\u003escaoh) \u003c\u003c 32;\n+\n+\treturn vsie_sca;\n+}\n+\n+static void write_scao(struct kvm_s390_sie_block *scb, unsigned long hpa)\n+{\n+\tscb-\u003escaoh = (u32)((u64)hpa \u003e\u003e 32);\n+\tscb-\u003escaol = (u32)(u64)hpa;\n+}\n+\n+static inline bool use_ssca(struct kvm *kvm, struct kvm_s390_sie_block *scb)\n+{\n+\tif (!kvm-\u003earch.use_ssca)\n+\t\treturn false;\n+\tif (!(scb-\u003eeca \u0026 ECA_SIGPI) \u0026\u0026 !(scb-\u003eecb \u0026 ECB_SRSI))\n+\t\treturn false;\n+\tif (!read_scao(kvm, scb))\n+\t\treturn false;\n+\treturn true;\n+}\n+\n /* trigger a validity icpt for the given scb */\n static int set_validity_icpt(struct kvm_s390_sie_block *scb,\n \t\t\t __u16 reason_code)\n@@ -81,6 +164,25 @@ static int set_validity_icpt(struct kvm_s390_sie_block *scb,\n \treturn 1;\n }\n \n+/* The sca header must not cross pages etc. */\n+static int validate_scao(struct kvm_vcpu *vcpu, struct kvm_s390_sie_block *scb, gpa_t gpa)\n+{\n+\tint offset;\n+\n+\tif (gpa \u003c 2 * PAGE_SIZE)\n+\t\treturn set_validity_icpt(scb, 0x0038U);\n+\tif ((gpa \u0026 ~0x1fffUL) == kvm_s390_get_prefix(vcpu))\n+\t\treturn set_validity_icpt(scb, 0x0011U);\n+\n+\tif (sie_uses_esca(scb))\n+\t\toffset = offsetof(struct esca_block, cpu[0]) - 1;\n+\telse\n+\t\toffset = offsetof(struct bsca_block, cpu[0]) - 1;\n+\tif ((gpa \u0026 PAGE_MASK) != ((gpa + offset) \u0026 PAGE_MASK))\n+\t\treturn set_validity_icpt(scb, 0x003bU);\n+\treturn 0;\n+}\n+\n /* mark the prefix as unmapped, this will block the VSIE */\n static void prefix_unmapped(struct vsie_page *vsie_page)\n {\n@@ -109,6 +211,148 @@ static int prefix_is_mapped(struct vsie_page *vsie_page)\n \treturn !(atomic_read(\u0026vsie_page-\u003escb_s.prog20) \u0026 PROG_REQUEST);\n }\n \n+static void release_gmap_shadow(struct vsie_page *vsie_page)\n+{\n+\tstruct gmap *gmap = vsie_page-\u003egmap_cache.gmap;\n+\n+\tlockdep_assert_held(\u0026gmap-\u003ekvm-\u003earch.gmap-\u003echildren_lock);\n+\n+\tlist_del(\u0026vsie_page-\u003egmap_cache.list);\n+\tvsie_page-\u003egmap_cache.gmap = NULL;\n+\tprefix_unmapped(vsie_page);\n+\n+\tif (list_empty(\u0026gmap-\u003escb_users)) {\n+\t\tgmap_remove_child(gmap);\n+\t\tgmap_put(gmap);\n+\t}\n+}\n+\n+static void release_gmap_shadow_safe(struct kvm *kvm, struct vsie_page *vsie_page)\n+{\n+\tif (!vsie_page-\u003egmap_cache.gmap)\n+\t\treturn;\n+\n+\tguard(spinlock)(\u0026kvm-\u003earch.gmap-\u003echildren_lock);\n+\n+\tif (vsie_page-\u003egmap_cache.gmap)\n+\t\trelease_gmap_shadow(vsie_page);\n+}\n+\n+static struct gmap *acquire_gmap_shadow(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n+{\n+\tunion ctlreg0 cr0;\n+\tstruct gmap *gmap;\n+\tunion asce asce;\n+\tint edat;\n+\n+\tasce.val = vcpu-\u003earch.sie_block-\u003egcr[1];\n+\tcr0.val = vcpu-\u003earch.sie_block-\u003egcr[0];\n+\tedat = cr0.edat \u0026\u0026 test_kvm_facility(vcpu-\u003ekvm, 8);\n+\tedat += edat \u0026\u0026 test_kvm_facility(vcpu-\u003ekvm, 78);\n+\n+\tscoped_guard(spinlock, \u0026vcpu-\u003ekvm-\u003earch.gmap-\u003echildren_lock) {\n+\t\tgmap = vsie_page-\u003egmap_cache.gmap;\n+\t\tif (gmap) {\n+\t\t\t/*\n+\t\t\t * ASCE or EDAT could have changed since last icpt, or the gmap\n+\t\t\t * we're holding has been unshadowed. If the gmap is still valid,\n+\t\t\t * we can safely reuse it.\n+\t\t\t */\n+\t\t\tif (gmap_is_shadow_valid(gmap, asce, edat)) {\n+\t\t\t\tvcpu-\u003ekvm-\u003estat.gmap_shadow_reuse++;\n+\t\t\t\tgmap_get(gmap);\n+\t\t\t\treturn gmap;\n+\t\t\t}\n+\t\t\t/* release the old shadow and mark the prefix as unmapped */\n+\t\t\trelease_gmap_shadow(vsie_page);\n+\t\t}\n+\t}\n+again:\n+\tgmap = gmap_create_shadow(vcpu-\u003earch.mc, vcpu-\u003ekvm-\u003earch.gmap, asce, edat);\n+\tif (IS_ERR(gmap))\n+\t\treturn gmap;\n+\tscoped_guard(spinlock, \u0026vcpu-\u003ekvm-\u003earch.gmap-\u003echildren_lock) {\n+\t\t/* unlikely race condition, remove the previous shadow */\n+\t\tif (vsie_page-\u003egmap_cache.gmap)\n+\t\t\trelease_gmap_shadow(vsie_page);\n+\t\tif (!gmap-\u003eparent) {\n+\t\t\tgmap_put(gmap);\n+\t\t\tgoto again;\n+\t\t}\n+\t\tvcpu-\u003ekvm-\u003estat.gmap_shadow_create++;\n+\t\tlist_add(\u0026vsie_page-\u003egmap_cache.list, \u0026gmap-\u003escb_users);\n+\t\tvsie_page-\u003egmap_cache.gmap = gmap;\n+\t\tprefix_unmapped(vsie_page);\n+\t}\n+\treturn gmap;\n+}\n+\n+/*\n+ * Pin the guest page given by gpa and set hpa to the pinned host address.\n+ * Will always be pinned writable.\n+ *\n+ * Returns: - 0 on success\n+ * - -EINVAL if the gpa is not valid guest storage\n+ */\n+static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)\n+{\n+\tstruct page *page;\n+\n+\tpage = gfn_to_page(kvm, gpa_to_gfn(gpa));\n+\tif (!page)\n+\t\treturn -EINVAL;\n+\t*hpa = (hpa_t)page_to_phys(page) + (gpa \u0026 ~PAGE_MASK);\n+\treturn 0;\n+}\n+\n+/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */\n+static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)\n+{\n+\tkvm_release_page_dirty(pfn_to_page(phys_to_pfn(hpa)));\n+\t/* mark the page always as dirty for migration */\n+\tmark_page_dirty(kvm, gpa_to_gfn(gpa));\n+}\n+\n+/* unpin multiple guest pages pinned with pin_guest_pages() */\n+static void unpin_guest_pages(struct kvm *kvm, struct kvm_address_pair *addr, unsigned int nr_pages)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c nr_pages; i++) {\n+\t\tunpin_guest_page(kvm, addr[i].gpa, addr[i].hpa);\n+\t\taddr[i].gpa = 0;\n+\t\taddr[i].hpa = 0;\n+\t}\n+}\n+\n+/*\n+ * pin nr_pages consecutive guest pages\n+ */\n+static int pin_guest_pages(struct kvm *kvm, gpa_t gpa, unsigned int nr_pages,\n+\t\t\t struct kvm_address_pair *addr, unsigned int *nr_pages_pinned)\n+{\n+\tint rc = 0, i;\n+\thpa_t hpa;\n+\n+\tgpa = gpa \u0026 PAGE_MASK;\n+\n+\t/* the guest pages may not be mapped continuously, so pin each page */\n+\tfor (i = 0; i \u003c nr_pages; i++) {\n+\t\trc = pin_guest_page(kvm, gpa + PAGE_SIZE * i, \u0026hpa);\n+\t\tif (rc)\n+\t\t\tgoto err;\n+\t\taddr[i].gpa = gpa + PAGE_SIZE * i;\n+\t\taddr[i].hpa = hpa;\n+\t}\n+\t*nr_pages_pinned = i;\n+\treturn rc;\n+\n+err:\n+\tunpin_guest_pages(kvm, addr, i);\n+\t*nr_pages_pinned = 0;\n+\treturn rc;\n+}\n+\n /* copy the updated intervention request bits into the shadow scb */\n static void update_intervention_requests(struct vsie_page *vsie_page)\n {\n@@ -124,8 +368,7 @@ static void update_intervention_requests(struct vsie_page *vsie_page)\n static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n {\n \tstruct kvm_s390_sie_block *scb_s = \u0026vsie_page-\u003escb_s;\n-\tstruct kvm_s390_sie_block *scb_o = vsie_page-\u003escb_o;\n-\tint newflags, cpuflags = atomic_read(\u0026scb_o-\u003ecpuflags);\n+\tint newflags, cpuflags = atomic_read(\u0026vsie_page-\u003escb_o-\u003ecpuflags);\n \n \t/* we don't allow ESA/390 guests unless explicitly enabled */\n \tif (!(cpuflags \u0026 CPUSTAT_ZARCH) \u0026\u0026 !vcpu-\u003ekvm-\u003earch.allow_vsie_esamode)\n@@ -424,18 +667,27 @@ static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n \t}\n }\n \n-/* unshadow the scb, copying parameters back to the real scb */\n-static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n+/*\n+ * Only write the interception fields back; avoid clobbering the timer and\n+ * guest-state fields in scb_o.\n+ */\n+static inline void unshadow_intercept(struct kvm_s390_sie_block *scb_o,\n+\t\t\t\t struct kvm_s390_sie_block *scb_s)\n {\n-\tstruct kvm_s390_sie_block *scb_s = \u0026vsie_page-\u003escb_s;\n-\tstruct kvm_s390_sie_block *scb_o = vsie_page-\u003escb_o;\n-\n-\t/* interception */\n \tscb_o-\u003eicptcode = scb_s-\u003eicptcode;\n \tscb_o-\u003eicptstatus = scb_s-\u003eicptstatus;\n \tscb_o-\u003eipa = scb_s-\u003eipa;\n \tscb_o-\u003eipb = scb_s-\u003eipb;\n \tscb_o-\u003egbea = scb_s-\u003egbea;\n+}\n+\n+/* unshadow the scb, copying parameters back to the real scb */\n+static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n+{\n+\tstruct kvm_s390_sie_block *scb_s = \u0026vsie_page-\u003escb_s;\n+\tstruct kvm_s390_sie_block *scb_o = vsie_page-\u003escb_o;\n+\n+\tunshadow_intercept(scb_o, scb_s);\n \n \t/* timer */\n \tscb_o-\u003ecputm = scb_s-\u003ecputm;\n@@ -469,6 +721,39 @@ static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n \t\tscb_o-\u003eihcpu = scb_s-\u003eihcpu;\n }\n \n+static int shadow_scb_minimal(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n+{\n+\tstruct kvm_s390_sie_block *scb_s = \u0026vsie_page-\u003escb_s;\n+\tstruct kvm_s390_sie_block *scb_o = vsie_page-\u003escb_o;\n+\tint rc;\n+\n+\t/* make sure we don't have any leftovers when reusing the scb */\n+\tscb_s-\u003eicptcode = 0;\n+\tscb_s-\u003eeca = 0;\n+\tscb_s-\u003eecb = 0;\n+\tscb_s-\u003eecb2 = 0;\n+\tscb_s-\u003eecb3 = 0;\n+\tscb_s-\u003eecd = 0;\n+\tscb_s-\u003efac = 0;\n+\tscb_s-\u003efpf = 0;\n+\n+\trc = prepare_cpuflags(vcpu, vsie_page);\n+\tif (rc)\n+\t\treturn rc;\n+\n+\tscb_s-\u003eicpua = scb_o-\u003eicpua;\n+\tscb_s-\u003eecb2 |= scb_o-\u003eecb2 \u0026 ECB2_ESCA;\n+\n+\tif (vsie_page-\u003evsie_sca) {\n+\t\tscb_s-\u003eeca |= scb_o-\u003eeca \u0026 ECA_SIGPI;\n+\t\tscb_s-\u003eecb |= scb_o-\u003eecb \u0026 ECB_SRSI;\n+\t\twrite_scao(scb_s, virt_to_phys(\u0026vsie_page-\u003evsie_sca-\u003essca));\n+\t\tscb_s-\u003eosda = virt_to_phys(scb_o);\n+\t}\n+\n+\treturn 0;\n+}\n+\n /*\n * Setup the shadow scb by copying and checking the relevant parts of the g2\n * provided scb.\n@@ -488,19 +773,9 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n \tunsigned long new_mso = 0;\n \tint rc;\n \n-\t/* make sure we don't have any leftovers when reusing the scb */\n-\tscb_s-\u003eicptcode = 0;\n-\tscb_s-\u003eeca = 0;\n-\tscb_s-\u003eecb = 0;\n-\tscb_s-\u003eecb2 = 0;\n-\tscb_s-\u003eecb3 = 0;\n-\tscb_s-\u003eecd = 0;\n-\tscb_s-\u003efac = 0;\n-\tscb_s-\u003efpf = 0;\n-\n-\trc = prepare_cpuflags(vcpu, vsie_page);\n+\trc = shadow_scb_minimal(vcpu, vsie_page);\n \tif (rc)\n-\t\tgoto out;\n+\t\tgoto out_validity;\n \n \t/* timer */\n \tscb_s-\u003ecputm = scb_o-\u003ecputm;\n@@ -528,8 +803,6 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n \tif (!(atomic_read(\u0026scb_s-\u003ecpuflags) \u0026 CPUSTAT_KSS))\n \t\tscb_s-\u003eictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;\n \n-\tscb_s-\u003eicpua = scb_o-\u003eicpua;\n-\n \tif (!(atomic_read(\u0026scb_s-\u003ecpuflags) \u0026 CPUSTAT_ZARCH))\n \t\tnew_prefix \u0026= GUEST_PREFIX_MASK_ESA;\n \telse\n@@ -614,10 +887,328 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n \n \tprepare_ibc(vcpu, vsie_page);\n \trc = shadow_crycb(vcpu, vsie_page);\n-out:\n \tif (rc)\n \t\tunshadow_scb(vcpu, vsie_page);\n \treturn rc;\n+\n+out_validity:\n+\tunshadow_intercept(vsie_page-\u003escb_o, \u0026vsie_page-\u003escb_s);\n+\treturn rc;\n+}\n+\n+/* unpin the scb provided by guest 2, marking it as dirty */\n+static void unpin_scb(struct kvm *kvm, struct vsie_page *vsie_page)\n+{\n+\thpa_t hpa;\n+\n+\tif (!test_bit(VSIE_PAGE_SCB_PINNED, \u0026vsie_page-\u003eflags))\n+\t\treturn;\n+\n+\thpa = virt_to_phys(vsie_page-\u003escb_o);\n+\tif (hpa)\n+\t\tunpin_guest_page(kvm, vsie_page-\u003escb_gpa, hpa);\n+\tvsie_page-\u003escb_o = NULL;\n+\tclear_bit(VSIE_PAGE_SCB_PINNED, \u0026vsie_page-\u003eflags);\n+}\n+\n+/*\n+ * Pin the scb at gpa provided by guest 2 at vsie_page-\u003escb_o.\n+ *\n+ * Returns: - 0 if the scb was pinned.\n+ * - \u003e 0 if control has to be given to guest 2\n+ */\n+static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n+{\n+\thpa_t hpa;\n+\tint rc;\n+\n+\tif (test_bit(VSIE_PAGE_SCB_PINNED, \u0026vsie_page-\u003eflags))\n+\t\treturn 0;\n+\n+\trc = pin_guest_page(vcpu-\u003ekvm, vsie_page-\u003escb_gpa, \u0026hpa);\n+\tif (rc) {\n+\t\trc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);\n+\t\tWARN_ON_ONCE(rc);\n+\t\treturn 1;\n+\t}\n+\tvsie_page-\u003escb_o = phys_to_virt(hpa);\n+\tset_bit(VSIE_PAGE_SCB_PINNED, \u0026vsie_page-\u003eflags);\n+\treturn 0;\n+}\n+\n+/*\n+ * Unpin g2 original sca in g1 memory.\n+ *\n+ * Called with vsie_sca_lock held in write, except in the destroy path.\n+ */\n+static void unpin_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)\n+{\n+\tif (!test_bit(VSIE_SCA_SCA_PINNED, \u0026vsie_sca-\u003eflags))\n+\t\treturn;\n+\n+\tunpin_guest_pages(kvm, vsie_sca-\u003esca_o_pages, vsie_sca-\u003esca_o_nr_pages);\n+\tvsie_sca-\u003esca_o_nr_pages = 0;\n+\n+\tclear_bit(VSIE_SCA_SCA_PINNED, \u0026vsie_sca-\u003eflags);\n+}\n+\n+/*\n+ * Pin g2 original sca in g1 memory.\n+ *\n+ * Called with vsie_sca_lock held in write.\n+ */\n+static int pin_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)\n+{\n+\tbool is_esca = test_bit(VSIE_SCA_ESCA, \u0026vsie_sca-\u003eflags);\n+\tgpa_t offset = vsie_sca-\u003esca_gpa \u0026 ~PAGE_MASK;\n+\tint rc, nr_pages;\n+\n+\tif (test_bit(VSIE_SCA_SCA_PINNED, \u0026vsie_sca-\u003eflags))\n+\t\treturn 0;\n+\n+\tif (is_esca) {\n+\t\tnr_pages = 4;\n+\t\tif (offset + sizeof(struct esca_block) \u003e 4 * PAGE_SIZE)\n+\t\t\tnr_pages = 5;\n+\t} else {\n+\t\tnr_pages = 1;\n+\t\tif (offset + sizeof(struct bsca_block) \u003e PAGE_SIZE)\n+\t\t\tnr_pages = 2;\n+\t}\n+\n+\trc = pin_guest_pages(kvm, vsie_sca-\u003esca_gpa, nr_pages, vsie_sca-\u003esca_o_pages,\n+\t\t\t \u0026vsie_sca-\u003esca_o_nr_pages);\n+\tif (rc)\n+\t\treturn rc;\n+\tset_bit(VSIE_SCA_SCA_PINNED, \u0026vsie_sca-\u003eflags);\n+\n+\treturn 0;\n+}\n+\n+static int get_sca_entry_addr(struct kvm *kvm, struct vsie_sca *vsie_sca, u16 cpu_nr, gpa_t *gpa,\n+\t\t\t hpa_t *hpa)\n+{\n+\thpa_t cpu_offset, offset;\n+\tint pn;\n+\n+\t/*\n+\t * We cannot simply access the hva since the esca_block has typically\n+\t * 4 pages (arch max 5 pages) that might not be continuous in g1 memory.\n+\t * The bsca_block may also be stretched over two pages. Only the header\n+\t * is guaranteed to be on the same page.\n+\t */\n+\tif (test_bit(VSIE_SCA_ESCA, \u0026vsie_sca-\u003eflags))\n+\t\tcpu_offset = offsetof(struct esca_block, cpu[cpu_nr]);\n+\telse\n+\t\tcpu_offset = offsetof(struct bsca_block, cpu[cpu_nr]);\n+\tpn = ((vsie_sca-\u003esca_gpa \u0026 ~PAGE_MASK) + cpu_offset) \u003e\u003e PAGE_SHIFT;\n+\toffset = (vsie_sca-\u003esca_gpa + cpu_offset) \u0026 ~PAGE_MASK;\n+\tif (WARN_ON_ONCE(pn \u003e= vsie_sca-\u003esca_o_nr_pages))\n+\t\treturn -EINVAL;\n+\n+\tif (gpa)\n+\t\t*gpa = vsie_sca-\u003esca_o_pages[pn].gpa | offset;\n+\tif (hpa)\n+\t\t*hpa = vsie_sca-\u003esca_o_pages[pn].hpa | offset;\n+\treturn 0;\n+}\n+\n+static void put_vsie_sca(struct vsie_sca *vsie_sca)\n+{\n+\tif (!vsie_sca)\n+\t\treturn;\n+\n+\tWARN_ON_ONCE(atomic_dec_return(\u0026vsie_sca-\u003eref_count) \u003c 0);\n+}\n+\n+/*\n+ * Try to find a matching vsie_sca with the correct sca format.\n+ * @sca_o_gpa: original system control area address; guest-2 physical\n+ * @uses_esca: whether the guest SCB has ECB2_ESCA set\n+ *\n+ * Called with lock on vsie_sca_lock.\n+ */\n+static struct vsie_sca *get_vsie_sca_existing(struct kvm *kvm, gpa_t sca_o_gpa, bool uses_esca)\n+{\n+\tstruct vsie_sca *vsie_sca = xa_load(\u0026kvm-\u003earch.vsie.osca_to_sca,\n+\t\t\t\t\t sca_o_gpa \u003e\u003e SCA_ALIGNMENT_SHIFT);\n+\n+\tif (!vsie_sca)\n+\t\treturn NULL;\n+\tif (uses_esca != test_bit(VSIE_SCA_ESCA, \u0026vsie_sca-\u003eflags))\n+\t\treturn NULL;\n+\tWARN_ON_ONCE(atomic_inc_return(\u0026vsie_sca-\u003eref_count) \u003c 1);\n+\treturn vsie_sca;\n+}\n+\n+/* Try to find and get a currently unused vsie_sca from the vsie struct. */\n+static struct vsie_sca *get_vsie_sca_unused(struct kvm *kvm)\n+{\n+\tstruct vsie_sca *vsie_sca;\n+\tint i, ref_count;\n+\n+\tfor (i = 0; i \u003c kvm-\u003earch.vsie.sca_count; i++) {\n+\t\tvsie_sca = READ_ONCE(kvm-\u003earch.vsie.scas[kvm-\u003earch.vsie.sca_next]);\n+\t\tkvm-\u003earch.vsie.sca_next++;\n+\t\tkvm-\u003earch.vsie.sca_next %= kvm-\u003earch.vsie.sca_count;\n+\t\tref_count = atomic_inc_return(\u0026vsie_sca-\u003eref_count);\n+\t\tWARN_ON_ONCE(ref_count \u003c 1);\n+\t\tif (ref_count == 1)\n+\t\t\treturn vsie_sca;\n+\t\tput_vsie_sca(vsie_sca);\n+\t}\n+\treturn ERR_PTR(-EAGAIN);\n+}\n+\n+static void free_vsie_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)\n+{\n+\tfree_pages_exact(vsie_sca, sizeof(*vsie_sca));\n+}\n+\n+static struct vsie_sca *alloc_vsie_sca(void)\n+{\n+\tstruct vsie_sca *vsie_sca;\n+\n+\tvsie_sca = alloc_pages_exact(sizeof(*vsie_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO);\n+\tif (!vsie_sca)\n+\t\treturn NULL;\n+\n+\tmutex_init(\u0026vsie_sca-\u003emutex);\n+\tatomic_set(\u0026vsie_sca-\u003eref_count, 0);\n+\n+\treturn vsie_sca;\n+}\n+\n+/* Clear the vsie_sca struct but keep the vsie_page references, mutex and ref_count */\n+static void clear_vsie_sca(struct vsie_sca *vsie_sca)\n+{\n+\tmemset(\u0026vsie_sca-\u003ehead, 0, sizeof(vsie_sca-\u003ehead));\n+\tmemset(\u0026vsie_sca-\u003etail, 0, sizeof(vsie_sca-\u003etail));\n+}\n+\n+/* Pin and get an existing or new guest-3 system control area.*/\n+static int get_vsie_sca(struct kvm_vcpu *vcpu, struct kvm_s390_sie_block *scb_o,\n+\t\tstruct vsie_sca **vsie_sca_out)\n+{\n+\tstruct vsie_sca *vsie_sca, *vsie_sca_new = NULL;\n+\tgpa_t sca_gpa = read_scao(vcpu-\u003ekvm, scb_o);\n+\tbool is_esca = sie_uses_esca(scb_o);\n+\tstruct vsie_page *vsie_page_n;\n+\tstruct kvm *kvm = vcpu-\u003ekvm;\n+\tunsigned int max_vsie_sca;\n+\tint rc, cpu_nr;\n+\n+\t/* validate scb_o as we do not unshadow on error here */\n+\trc = validate_scao(vcpu, scb_o, sca_gpa);\n+\tif (rc)\n+\t\treturn rc;\n+\n+\tdown_read(\u0026kvm-\u003earch.vsie.vsie_sca_lock);\n+\tvsie_sca = get_vsie_sca_existing(kvm, sca_gpa, is_esca);\n+\tup_read(\u0026kvm-\u003earch.vsie.vsie_sca_lock);\n+\tif (vsie_sca) {\n+\t\t*vsie_sca_out = vsie_sca;\n+\t\treturn 0;\n+\t}\n+\n+\t/*\n+\t * Allocate new vsie_sca, it will likely be needed below.\n+\t * We want at least #online_vcpus shadows, so every VCPU can execute the\n+\t * VSIE in parallel. (Worst case all single core VMs.)\n+\t */\n+\tmax_vsie_sca = min_t(unsigned int, max_t(unsigned int, atomic_read(\u0026kvm-\u003eonline_vcpus),\n+\t\t\t vsie_shadow_sca_max), KVM_S390_MAX_VSIE_VCPUS);\n+\n+\tif (kvm-\u003earch.vsie.sca_count \u003c max_vsie_sca) {\n+\t\tvsie_sca_new = alloc_vsie_sca();\n+\t\tif (!vsie_sca_new)\n+\t\t\treturn -ENOMEM;\n+\t}\n+\n+\t/*\n+\t * Now we're taking the vsie_sca_lock in write mode so that we can manipulate\n+\t * the xarray and arch.vise.scas, etc.\n+\t *\n+\t * In the next lines we try three things to get an SCA:\n+\t * - Retry getting an existing vsie_sca\n+\t * - Using our newly allocated vsie_sca if we're under the limit\n+\t * - Reusing an vsie_sca including ssca to shadow a different osca\n+\t */\n+\tdown_write(\u0026kvm-\u003earch.vsie.vsie_sca_lock);\n+\tvsie_sca = xa_load(\u0026kvm-\u003earch.vsie.osca_to_sca, sca_gpa \u003e\u003e SCA_ALIGNMENT_SHIFT);\n+\tif (vsie_sca) {\n+\t\tWARN_ON_ONCE(atomic_inc_return(\u0026vsie_sca-\u003eref_count) \u003c 1);\n+\t\tif (is_esca == test_bit(VSIE_SCA_ESCA, \u0026vsie_sca-\u003eflags))\n+\t\t\tgoto out;\n+\t\t/* found vsie_sca with matching sca_gpa but wrong format */\n+\t\tput_vsie_sca(vsie_sca);\n+\t\txa_erase(\u0026kvm-\u003earch.vsie.osca_to_sca, sca_gpa \u003e\u003e SCA_ALIGNMENT_SHIFT);\n+\t}\n+\n+\t/* check again under write lock if we are still under our vsie_sca limit */\n+\tif (vsie_sca_new \u0026\u0026 kvm-\u003earch.vsie.sca_count \u003c max_vsie_sca) {\n+\t\t/* make use of vsie_sca just created */\n+\t\tvsie_sca = vsie_sca_new;\n+\t\tvsie_sca_new = NULL;\n+\n+\t\tkvm-\u003earch.vsie.scas[kvm-\u003earch.vsie.sca_count] = vsie_sca;\n+\t\tkvm-\u003earch.vsie.sca_count++;\n+\t\tatomic_set(\u0026vsie_sca-\u003eref_count, 1);\n+\t\tkvm-\u003estat.vsie_shadow_sca++;\n+\t} else {\n+\t\t/* reuse previously created vsie_sca allocation for different osca */\n+\t\tvsie_sca = get_vsie_sca_unused(kvm);\n+\t\t/* with nr_vcpus scas one must be reusable */\n+\t\tif (IS_ERR(vsie_sca))\n+\t\t\tgoto out;\n+\n+\t\t/* unused vsie_sca exclusive under vsie_sca_lock write lock */\n+\t\txa_erase(\u0026kvm-\u003earch.vsie.osca_to_sca, vsie_sca-\u003esca_gpa \u003e\u003e SCA_ALIGNMENT_SHIFT);\n+\t\tfor (cpu_nr = 0; cpu_nr \u003c KVM_S390_MAX_VSIE_VCPUS; cpu_nr++) {\n+\t\t\tvsie_page_n = vsie_sca-\u003epages[cpu_nr];\n+\t\t\tif (!vsie_page_n)\n+\t\t\t\tcontinue;\n+\n+\t\t\t/* unpin but keep the vsie_page for reuse */\n+\t\t\tunpin_scb(kvm, vsie_page_n);\n+\t\t\trelease_gmap_shadow_safe(kvm, vsie_page_n);\n+\t\t\tmemset(vsie_page_n, 0, sizeof(struct vsie_page));\n+\t\t\tvsie_page_n-\u003escb_gpa = ULONG_MAX;\n+\t\t}\n+\t\tunpin_sca(kvm, vsie_sca);\n+\t\tclear_vsie_sca(vsie_sca);\n+\t\tkvm-\u003estat.vsie_shadow_sca_reuse++;\n+\t}\n+\n+\tif (sie_uses_esca(scb_o))\n+\t\tset_bit(VSIE_SCA_ESCA, \u0026vsie_sca-\u003eflags);\n+\tvsie_sca-\u003esca_gpa = sca_gpa;\n+\n+\t/*\n+\t * The pinned original sca will only be unpinned lazily to limit the\n+\t * required amount of pins/unpins on each vsie entry/exit.\n+\t * The unpin is done in the reuse vsie_sca allocation path above and\n+\t * kvm_s390_vsie_destroy().\n+\t */\n+\trc = pin_sca(kvm, vsie_sca);\n+\tif (rc) {\n+\t\tvsie_sca-\u003esca_gpa = ULONG_MAX;\n+\t\tput_vsie_sca(vsie_sca);\n+\t\tgoto out;\n+\t}\n+\n+\trc = xa_insert(\u0026kvm-\u003earch.vsie.osca_to_sca, vsie_sca-\u003esca_gpa \u003e\u003e SCA_ALIGNMENT_SHIFT,\n+\t\t vsie_sca, GFP_KERNEL_ACCOUNT);\n+\tif (rc == -EBUSY)\n+\t\trc = 1;\n+\n+out:\n+\tup_write(\u0026kvm-\u003earch.vsie.vsie_sca_lock);\n+\tif (vsie_sca_new)\n+\t\tfree_vsie_sca(kvm, vsie_sca_new);\n+\tif (vsie_sca)\n+\t\t*vsie_sca_out = vsie_sca;\n+\treturn rc;\n }\n \n void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, gpa_t start, gpa_t end)\n@@ -680,44 +1271,18 @@ static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct\n \treturn rc;\n }\n \n-/*\n- * Pin the guest page given by gpa and set hpa to the pinned host address.\n- * Will always be pinned writable.\n- *\n- * Returns: - 0 on success\n- * - -EINVAL if the gpa is not valid guest storage\n- */\n-static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)\n-{\n-\tstruct page *page;\n-\n-\tpage = gfn_to_page(kvm, gpa_to_gfn(gpa));\n-\tif (!page)\n-\t\treturn -EINVAL;\n-\t*hpa = (hpa_t)page_to_phys(page) + (gpa \u0026 ~PAGE_MASK);\n-\treturn 0;\n-}\n-\n-/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */\n-static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)\n-{\n-\tkvm_release_page_dirty(pfn_to_page(phys_to_pfn(hpa)));\n-\t/* mark the page always as dirty for migration */\n-\tmark_page_dirty(kvm, gpa_to_gfn(gpa));\n-}\n-\n /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */\n static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n {\n \tstruct kvm_s390_sie_block *scb_s = \u0026vsie_page-\u003escb_s;\n \thpa_t hpa;\n \n-\thpa = (u64) scb_s-\u003escaoh \u003c\u003c 32 | scb_s-\u003escaol;\n-\tif (hpa) {\n-\t\tunpin_guest_page(vcpu-\u003ekvm, vsie_page-\u003esca_gpa, hpa);\n-\t\tvsie_page-\u003esca_gpa = 0;\n-\t\tscb_s-\u003escaol = 0;\n-\t\tscb_s-\u003escaoh = 0;\n+\tif (!vsie_page-\u003evsie_sca) {\n+\t\thpa = (u64) scb_s-\u003escaoh \u003c\u003c 32 | scb_s-\u003escaol;\n+\t\tif (hpa) {\n+\t\t\tunpin_guest_page(vcpu-\u003ekvm, vsie_page-\u003esca_gpa, hpa);\n+\t\t\twrite_scao(scb_s, 0);\n+\t\t}\n \t}\n \n \thpa = scb_s-\u003eitdba;\n@@ -756,9 +1321,6 @@ static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n * This works as long as the data lies in one page. If blocks ever exceed one\n * page, we have to fall back to shadowing.\n *\n- * As we reuse the sca, the vcpu pointers contained in it are invalid. We must\n- * therefore not enable any facilities that access these pointers (e.g. SIGPIF).\n- *\n * Returns: - 0 if all blocks were pinned.\n * - \u003e 0 if control has to be given to guest 2\n * - -ENOMEM if out of memory\n@@ -771,27 +1333,17 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n \tgpa_t gpa;\n \tint rc = 0;\n \n-\tgpa = READ_ONCE(scb_o-\u003escaol) \u0026 ~0xfUL;\n-\tif (test_kvm_cpu_feat(vcpu-\u003ekvm, KVM_S390_VM_CPU_FEAT_64BSCAO))\n-\t\tgpa |= (u64) READ_ONCE(scb_o-\u003escaoh) \u003c\u003c 32;\n-\tif (gpa) {\n-\t\tif (gpa \u003c 2 * PAGE_SIZE)\n-\t\t\trc = set_validity_icpt(scb_s, 0x0038U);\n-\t\telse if ((gpa \u0026 ~0x1fffUL) == kvm_s390_get_prefix(vcpu))\n-\t\t\trc = set_validity_icpt(scb_s, 0x0011U);\n-\t\telse if ((gpa \u0026 PAGE_MASK) !=\n-\t\t\t ((gpa + offsetof(struct bsca_block, cpu[0]) - 1) \u0026 PAGE_MASK))\n-\t\t\trc = set_validity_icpt(scb_s, 0x003bU);\n-\t\tif (!rc) {\n-\t\t\trc = pin_guest_page(vcpu-\u003ekvm, gpa, \u0026hpa);\n-\t\t\tif (rc)\n-\t\t\t\trc = set_validity_icpt(scb_s, 0x0034U);\n-\t\t}\n+\tgpa = vsie_page-\u003esca_gpa;\n+\tif (gpa \u0026\u0026 !vsie_page-\u003evsie_sca) {\n+\t\trc = validate_scao(vcpu, scb_s, gpa);\n \t\tif (rc)\n \t\t\tgoto unpin;\n-\t\tvsie_page-\u003esca_gpa = gpa;\n-\t\tscb_s-\u003escaoh = (u32)((u64)hpa \u003e\u003e 32);\n-\t\tscb_s-\u003escaol = (u32)(u64)hpa;\n+\t\trc = pin_guest_page(vcpu-\u003ekvm, gpa, \u0026hpa);\n+\t\tif (rc) {\n+\t\t\trc = set_validity_icpt(scb_s, 0x0034U);\n+\t\t\tgoto unpin;\n+\t\t}\n+\t\twrite_scao(scb_s, hpa);\n \t}\n \n \tgpa = READ_ONCE(scb_o-\u003eitdba) \u0026 ~0xffUL;\n@@ -866,51 +1418,18 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n \t\t/* Due to alignment rules (checked above) this cannot\n \t\t * cross page boundaries\n \t\t */\n-\t\trc = pin_guest_page(vcpu-\u003ekvm, gpa, \u0026hpa);\n-\t\tif (rc) {\n-\t\t\trc = set_validity_icpt(scb_s, 0x10b0U);\n-\t\t\tgoto unpin;\n-\t\t}\n-\t\tvsie_page-\u003esdnx_gpa = gpa;\n-\t\tscb_s-\u003esdnxo = hpa | sdnxc;\n-\t}\n-\treturn 0;\n-unpin:\n-\tunpin_blocks(vcpu, vsie_page);\n-\treturn rc;\n-}\n-\n-/* unpin the scb provided by guest 2, marking it as dirty */\n-static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,\n-\t\t gpa_t gpa)\n-{\n-\thpa_t hpa = virt_to_phys(vsie_page-\u003escb_o);\n-\n-\tif (hpa)\n-\t\tunpin_guest_page(vcpu-\u003ekvm, gpa, hpa);\n-\tvsie_page-\u003escb_o = NULL;\n-}\n-\n-/*\n- * Pin the scb at gpa provided by guest 2 at vsie_page-\u003escb_o.\n- *\n- * Returns: - 0 if the scb was pinned.\n- * - \u003e 0 if control has to be given to guest 2\n- */\n-static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,\n-\t\t gpa_t gpa)\n-{\n-\thpa_t hpa;\n-\tint rc;\n-\n-\trc = pin_guest_page(vcpu-\u003ekvm, gpa, \u0026hpa);\n-\tif (rc) {\n-\t\trc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);\n-\t\tWARN_ON_ONCE(rc);\n-\t\treturn 1;\n+\t\trc = pin_guest_page(vcpu-\u003ekvm, gpa, \u0026hpa);\n+\t\tif (rc) {\n+\t\t\trc = set_validity_icpt(scb_s, 0x10b0U);\n+\t\t\tgoto unpin;\n+\t\t}\n+\t\tvsie_page-\u003esdnx_gpa = gpa;\n+\t\tscb_s-\u003esdnxo = hpa | sdnxc;\n \t}\n-\tvsie_page-\u003escb_o = phys_to_virt(hpa);\n \treturn 0;\n+unpin:\n+\tunpin_blocks(vcpu, vsie_page);\n+\treturn rc;\n }\n \n /*\n@@ -1281,71 +1800,6 @@ static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struc\n \treturn rc;\n }\n \n-static void release_gmap_shadow(struct vsie_page *vsie_page)\n-{\n-\tstruct gmap *gmap = vsie_page-\u003egmap_cache.gmap;\n-\n-\tlockdep_assert_held(\u0026gmap-\u003ekvm-\u003earch.gmap-\u003echildren_lock);\n-\n-\tlist_del(\u0026vsie_page-\u003egmap_cache.list);\n-\tvsie_page-\u003egmap_cache.gmap = NULL;\n-\tprefix_unmapped(vsie_page);\n-\n-\tif (list_empty(\u0026gmap-\u003escb_users)) {\n-\t\tgmap_remove_child(gmap);\n-\t\tgmap_put(gmap);\n-\t}\n-}\n-\n-static struct gmap *acquire_gmap_shadow(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n-{\n-\tunion ctlreg0 cr0;\n-\tstruct gmap *gmap;\n-\tunion asce asce;\n-\tint edat;\n-\n-\tasce.val = vcpu-\u003earch.sie_block-\u003egcr[1];\n-\tcr0.val = vcpu-\u003earch.sie_block-\u003egcr[0];\n-\tedat = cr0.edat \u0026\u0026 test_kvm_facility(vcpu-\u003ekvm, 8);\n-\tedat += edat \u0026\u0026 test_kvm_facility(vcpu-\u003ekvm, 78);\n-\n-\tscoped_guard(spinlock, \u0026vcpu-\u003ekvm-\u003earch.gmap-\u003echildren_lock) {\n-\t\tgmap = vsie_page-\u003egmap_cache.gmap;\n-\t\tif (gmap) {\n-\t\t\t/*\n-\t\t\t * ASCE or EDAT could have changed since last icpt, or the gmap\n-\t\t\t * we're holding has been unshadowed. If the gmap is still valid,\n-\t\t\t * we can safely reuse it.\n-\t\t\t */\n-\t\t\tif (gmap_is_shadow_valid(gmap, asce, edat)) {\n-\t\t\t\tvcpu-\u003ekvm-\u003estat.gmap_shadow_reuse++;\n-\t\t\t\tgmap_get(gmap);\n-\t\t\t\treturn gmap;\n-\t\t\t}\n-\t\t\t/* release the old shadow and mark the prefix as unmapped */\n-\t\t\trelease_gmap_shadow(vsie_page);\n-\t\t}\n-\t}\n-again:\n-\tgmap = gmap_create_shadow(vcpu-\u003earch.mc, vcpu-\u003ekvm-\u003earch.gmap, asce, edat);\n-\tif (IS_ERR(gmap))\n-\t\treturn gmap;\n-\tscoped_guard(spinlock, \u0026vcpu-\u003ekvm-\u003earch.gmap-\u003echildren_lock) {\n-\t\t/* unlikely race condition, remove the previous shadow */\n-\t\tif (vsie_page-\u003egmap_cache.gmap)\n-\t\t\trelease_gmap_shadow(vsie_page);\n-\t\tif (!gmap-\u003eparent) {\n-\t\t\tgmap_put(gmap);\n-\t\t\tgoto again;\n-\t\t}\n-\t\tvcpu-\u003ekvm-\u003estat.gmap_shadow_create++;\n-\t\tlist_add(\u0026vsie_page-\u003egmap_cache.list, \u0026gmap-\u003escb_users);\n-\t\tvsie_page-\u003egmap_cache.gmap = gmap;\n-\t\tprefix_unmapped(vsie_page);\n-\t}\n-\treturn gmap;\n-}\n-\n /*\n * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.\n */\n@@ -1429,7 +1883,7 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n \t\t */\n \t\tif (kvm_s390_vcpu_has_irq(vcpu, 0) ||\n \t\t kvm_s390_vcpu_sie_inhibited(vcpu)) {\n-\t\t\tkvm_s390_rewind_psw(vcpu, 4);\n+\t\t\trc = -EAGAIN;\n \t\t\tbreak;\n \t\t}\n \t\tif (sg)\n@@ -1472,93 +1926,369 @@ static void put_vsie_page(struct vsie_page *vsie_page)\n \tclear_bit(VSIE_PAGE_IN_USE, \u0026vsie_page-\u003eflags);\n }\n \n+static void free_vsie_page(struct vsie_page *vsie_page)\n+{\n+\tfree_page((unsigned long)vsie_page);\n+}\n+\n+static struct vsie_page *alloc_vsie_page(struct kvm *kvm)\n+{\n+\tstruct vsie_page *vsie_page;\n+\n+\tvsie_page = (struct vsie_page *)__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA);\n+\tif (!vsie_page)\n+\t\treturn vsie_page;\n+\n+\t/* Mark it as invalid until it resides in the tree. */\n+\tvsie_page-\u003escb_gpa = ULONG_MAX;\n+\treturn vsie_page;\n+}\n+\n+static int init_vsie_page(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, unsigned long scb_gpa)\n+{\n+\tint rc;\n+\n+\tvsie_page-\u003escb_gpa = scb_gpa;\n+\trc = pin_scb(vcpu, vsie_page);\n+\tif (rc) {\n+\t\tvsie_page-\u003escb_gpa = ULONG_MAX;\n+\t\treturn rc;\n+\t}\n+\n+\tvsie_page-\u003esca_gpa = read_scao(vcpu-\u003ekvm, vsie_page-\u003escb_o);\n+\n+\treturn 0;\n+}\n+\n+/* Reset shadow state after a vsie_page has been (re)initialised for a new SCB. */\n+static void reset_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)\n+{\n+\tmemset(\u0026vsie_page-\u003escb_s, 0, sizeof(struct kvm_s390_sie_block));\n+\trelease_gmap_shadow_safe(kvm, vsie_page);\n+\tprefix_unmapped(vsie_page);\n+\tvsie_page-\u003efault_addr = 0;\n+\tvsie_page-\u003escb_s.ihcpu = 0xffffU;\n+}\n+\n /*\n * Get or create a vsie page for a scb address.\n *\n- * Returns: - address of a vsie page (cached or new one)\n- * - NULL if the same scb address is already used by another VCPU\n- * - ERR_PTR(-ENOMEM) if out of memory\n+ * Original control blocks are pinned when the vsie_page pointing to them is\n+ * returned.\n+ * Newly created vsie_pages only have vsie_page-\u003escb_gpa and vsie_page-\u003esca_gpa\n+ * set.\n+ *\n+ * Returns: - -EBUSY if the same scb address is already used by another VCPU\n+ * - -ENOMEM if out of memory\n */\n-static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)\n+static int get_vsie_page(struct kvm_vcpu *vcpu, unsigned long addr,\n+\t\t\t struct vsie_page **vsie_page_out)\n {\n-\tstruct vsie_page *vsie_page;\n-\tint nr_vcpus;\n-\n-\trcu_read_lock();\n-\tvsie_page = radix_tree_lookup(\u0026kvm-\u003earch.vsie.addr_to_page, addr \u003e\u003e SCB_ALIGNMENT_SHIFT);\n-\trcu_read_unlock();\n-\tif (vsie_page) {\n-\t\tif (try_get_vsie_page(vsie_page)) {\n-\t\t\tif (vsie_page-\u003escb_gpa == addr)\n-\t\t\t\treturn vsie_page;\n-\t\t\t/*\n-\t\t\t * We raced with someone reusing + putting this vsie\n-\t\t\t * page before we grabbed it.\n-\t\t\t */\n-\t\t\tput_vsie_page(vsie_page);\n+\tstruct vsie_page *vsie_page, *vsie_page_new = NULL;\n+\tstruct kvm *kvm = vcpu-\u003ekvm;\n+\tunsigned int max_vsie_page;\n+\tint rc, pages_idx;\n+\n+\tvsie_page = xa_load(\u0026kvm-\u003earch.vsie.addr_to_page, addr \u003e\u003e SCB_ALIGNMENT_SHIFT);\n+\tif (vsie_page \u0026\u0026 try_get_vsie_page(vsie_page)) {\n+\t\tif (vsie_page-\u003escb_gpa == addr) {\n+\t\t\t*vsie_page_out = vsie_page;\n+\t\t\treturn 0;\n \t\t}\n+\t\t/*\n+\t\t * We raced with someone reusing + putting this vsie\n+\t\t * page before we grabbed it.\n+\t\t */\n+\t\tput_vsie_page(vsie_page);\n \t}\n \n-\t/*\n-\t * We want at least #online_vcpus shadows, so every VCPU can execute\n-\t * the VSIE in parallel.\n-\t */\n-\tnr_vcpus = atomic_read(\u0026kvm-\u003eonline_vcpus);\n+\tmax_vsie_page = min_t(unsigned int, max_t(unsigned int, atomic_read(\u0026kvm-\u003eonline_vcpus),\n+\t\t\t vsie_shadow_scb_max), KVM_S390_MAX_VSIE_VCPUS);\n+\n+\t/* allocate new vsie_page - we will likely need it */\n+\tif (kvm-\u003earch.vsie.page_count \u003c max_vsie_page) {\n+\t\tvsie_page_new = alloc_vsie_page(kvm);\n+\t\tif (!vsie_page_new)\n+\t\t\treturn -ENOMEM;\n+\t\t__set_bit(VSIE_PAGE_IN_USE, \u0026vsie_page_new-\u003eflags);\n+\t}\n \n \tmutex_lock(\u0026kvm-\u003earch.vsie.mutex);\n-\tif (kvm-\u003earch.vsie.page_count \u003c nr_vcpus) {\n-\t\tvsie_page = (void *)__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA);\n-\t\tif (!vsie_page) {\n+\tvsie_page = xa_load(\u0026kvm-\u003earch.vsie.addr_to_page, addr \u003e\u003e SCB_ALIGNMENT_SHIFT);\n+\tif (vsie_page \u0026\u0026 try_get_vsie_page(vsie_page)) {\n+\t\tif (vsie_page-\u003escb_gpa == addr) {\n \t\t\tmutex_unlock(\u0026kvm-\u003earch.vsie.mutex);\n-\t\t\treturn ERR_PTR(-ENOMEM);\n+\t\t\tif (vsie_page_new)\n+\t\t\t\tfree_vsie_page(vsie_page_new);\n+\t\t\t*vsie_page_out = vsie_page;\n+\t\t\treturn 0;\n \t\t}\n-\t\t__set_bit(VSIE_PAGE_IN_USE, \u0026vsie_page-\u003eflags);\n-\t\tkvm-\u003earch.vsie.pages[kvm-\u003earch.vsie.page_count] = vsie_page;\n+\t\t/*\n+\t\t * We raced with someone reusing + putting this vsie\n+\t\t * page before we grabbed it.\n+\t\t */\n+\t\tput_vsie_page(vsie_page);\n+\t}\n+\n+\tif (kvm-\u003earch.vsie.page_count \u003c max_vsie_page) {\n+\t\tpages_idx = kvm-\u003earch.vsie.page_count;\n+\t\tvsie_page = vsie_page_new;\n+\t\tvsie_page_new = NULL;\n+\t\tWRITE_ONCE(kvm-\u003earch.vsie.pages[kvm-\u003earch.vsie.page_count], vsie_page);\n \t\tkvm-\u003earch.vsie.page_count++;\n+\t\tkvm-\u003estat.vsie_shadow_scb++;\n \t} else {\n \t\t/* reuse an existing entry that belongs to nobody */\n \t\twhile (true) {\n-\t\t\tvsie_page = kvm-\u003earch.vsie.pages[kvm-\u003earch.vsie.next];\n+\t\t\tpages_idx = kvm-\u003earch.vsie.next;\n+\t\t\tkvm-\u003earch.vsie.next++;\n+\t\t\tkvm-\u003earch.vsie.next %= kvm-\u003earch.vsie.page_count;\n+\t\t\tvsie_page = kvm-\u003earch.vsie.pages[pages_idx];\n \t\t\tif (try_get_vsie_page(vsie_page))\n \t\t\t\tbreak;\n-\t\t\tkvm-\u003earch.vsie.next++;\n-\t\t\tkvm-\u003earch.vsie.next %= nr_vcpus;\n \t\t}\n \t\tif (vsie_page-\u003escb_gpa != ULONG_MAX)\n-\t\t\tradix_tree_delete(\u0026kvm-\u003earch.vsie.addr_to_page,\n-\t\t\t\t\t vsie_page-\u003escb_gpa \u003e\u003e SCB_ALIGNMENT_SHIFT);\n+\t\t\txa_erase(\u0026kvm-\u003earch.vsie.addr_to_page,\n+\t\t\t\t vsie_page-\u003escb_gpa \u003e\u003e SCB_ALIGNMENT_SHIFT);\n+\t\t/* Mark it as invalid until it resides in the tree. */\n+\t\tvsie_page-\u003escb_gpa = ULONG_MAX;\n+\n+\t\tunpin_scb(kvm, vsie_page);\n+\t\tkvm-\u003estat.vsie_shadow_scb_reuse++;\n \t}\n-\t/* Mark it as invalid until it resides in the tree. */\n-\tvsie_page-\u003escb_gpa = ULONG_MAX;\n \n-\t/* Double use of the same address or allocation failure. */\n-\tif (radix_tree_insert(\u0026kvm-\u003earch.vsie.addr_to_page, addr \u003e\u003e SCB_ALIGNMENT_SHIFT,\n-\t\t\t vsie_page)) {\n-\t\tput_vsie_page(vsie_page);\n-\t\tmutex_unlock(\u0026kvm-\u003earch.vsie.mutex);\n-\t\treturn NULL;\n+\trc = init_vsie_page(vcpu, vsie_page, addr);\n+\tif (!rc) {\n+\t\trc = xa_insert(\u0026kvm-\u003earch.vsie.addr_to_page, addr \u003e\u003e SCB_ALIGNMENT_SHIFT, vsie_page,\n+\t\t\t GFP_KERNEL_ACCOUNT);\n+\t\tif (rc == -EBUSY)\n+\t\t\trc = -EAGAIN;\n \t}\n-\tvsie_page-\u003escb_gpa = addr;\n+\n \tmutex_unlock(\u0026kvm-\u003earch.vsie.mutex);\n+\tif (vsie_page_new)\n+\t\tfree_vsie_page(vsie_page_new);\n+\tif (rc) {\n+\t\tvsie_page-\u003escb_gpa = ULONG_MAX;\n+\t\tput_vsie_page(vsie_page);\n+\t\treturn rc;\n+\t}\n \n-\tmemset(\u0026vsie_page-\u003escb_s, 0, sizeof(struct kvm_s390_sie_block));\n-\tif (vsie_page-\u003egmap_cache.gmap) {\n-\t\tscoped_guard(spinlock, \u0026kvm-\u003earch.gmap-\u003echildren_lock)\n-\t\t\tif (vsie_page-\u003egmap_cache.gmap)\n-\t\t\t\trelease_gmap_shadow(vsie_page);\n+\treset_vsie_page(kvm, vsie_page);\n+\n+\t*vsie_page_out = vsie_page;\n+\treturn 0;\n+}\n+\n+static int get_vsie_page_cpu_nr(struct kvm_vcpu *vcpu, struct vsie_sca *vsie_sca, gpa_t scb_gpa,\n+\t\t\t\tu16 cpu_nr, struct vsie_page **vsie_page_out)\n+{\n+\tstruct vsie_page *vsie_page, *vsie_page_new = NULL;\n+\tbool vsie_page_is_new = true;\n+\tint rc;\n+\n+\tvsie_page = vsie_sca-\u003epages[cpu_nr];\n+\tif (!vsie_page) {\n+\t\tvsie_page_new = alloc_vsie_page(vcpu-\u003ekvm);\n+\t\tif (!vsie_page_new)\n+\t\t\treturn -ENOMEM;\n+\t\tvsie_page_new-\u003evsie_sca = vsie_sca;\n+\t\t__set_bit(VSIE_PAGE_IN_USE, \u0026vsie_page_new-\u003eflags);\n+\n+\t\t/* be careful to not loose a page here if we raced */\n+\t\tscoped_guard(mutex, \u0026vsie_sca-\u003emutex) {\n+\t\t\tvsie_page = vsie_sca-\u003epages[cpu_nr];\n+\t\t\tif (!vsie_page) {\n+\t\t\t\tWRITE_ONCE(vsie_sca-\u003epages[cpu_nr], vsie_page_new);\n+\t\t\t\tvsie_page = vsie_page_new;\n+\t\t\t}\n+\t\t}\n \t}\n-\tprefix_unmapped(vsie_page);\n-\tvsie_page-\u003efault_addr = 0;\n-\tvsie_page-\u003escb_s.ihcpu = 0xffffU;\n-\treturn vsie_page;\n+\tif (vsie_page != vsie_page_new) {\n+\t\tvsie_page_is_new = false;\n+\t\tif (vsie_page_new)\n+\t\t\tfree_vsie_page(vsie_page_new);\n+\n+\t\t/* not a new vsie_page so get it */\n+\t\tif (!try_get_vsie_page(vsie_page))\n+\t\t\treturn -EAGAIN;\n+\t\tvsie_page-\u003evsie_sca = vsie_sca;\n+\t}\n+\tif (vsie_page-\u003escb_gpa != scb_gpa || vsie_page-\u003esca_gpa != vsie_sca-\u003esca_gpa) {\n+\t\tscoped_guard(mutex, \u0026vcpu-\u003ekvm-\u003earch.vsie.mutex) {\n+\t\t\tunpin_scb(vcpu-\u003ekvm, vsie_page);\n+\t\t\trc = init_vsie_page(vcpu, vsie_page, scb_gpa);\n+\t\t}\n+\t\tif (vsie_page_is_new)\n+\t\t\tvcpu-\u003ekvm-\u003estat.vsie_shadow_scb++;\n+\t\telse\n+\t\t\tvcpu-\u003ekvm-\u003estat.vsie_shadow_scb_reuse++;\n+\t\tif (rc) {\n+\t\t\tput_vsie_page(vsie_page);\n+\t\t\treturn rc;\n+\t\t}\n+\n+\t\treset_vsie_page(vcpu-\u003ekvm, vsie_page);\n+\t}\n+\n+\t*vsie_page_out = vsie_page;\n+\treturn 0;\n }\n \n-int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)\n+/*\n+ * Copy the mcn from the osca to the vsie_sca to be able to detect mcn changes later on.\n+ *\n+ * @vsie_sca: vsie_sca to copy mcn to.\n+ * @sca: Pointer to a struct bsca_block or struct esca_block to read from.\n+ */\n+static void sca_mcn_copy(struct vsie_sca *vsie_sca, void *sca)\n {\n-\tstruct vsie_page *vsie_page;\n-\tunsigned long scb_addr;\n+\tint offset = offsetof(struct bsca_block, mcn);\n+\tint size = sizeof(unsigned long);\n+\n+\tif (test_bit(VSIE_SCA_ESCA, \u0026vsie_sca-\u003eflags)) {\n+\t\toffset = offsetof(struct esca_block, mcn);\n+\t\tsize = size * 4;\n+\t}\n+\tmemcpy(\u0026vsie_sca-\u003emcn, sca + offset, size);\n+}\n+\n+/*\n+ * Compare the mcn from the given sca to the vsie_sca to be able to detect mcn changes.\n+ *\n+ * @vsie_sca: vsie_sca to compare mcn to.\n+ * @sca: Pointer to a struct bsca_block or struct esca_block to compare to.\n+ */\n+static bool sca_mcn_equals(struct vsie_sca *vsie_sca, void *sca)\n+{\n+\tint offset = offsetof(struct bsca_block, mcn);\n+\tint size = sizeof(unsigned long);\n+\n+\tif (test_bit(VSIE_SCA_ESCA, \u0026vsie_sca-\u003eflags)) {\n+\t\tsize = size * 4;\n+\t\toffset = offsetof(struct esca_block, mcn);\n+\t}\n+\n+\treturn !memcmp(\u0026vsie_sca-\u003emcn, sca + offset, size);\n+}\n+\n+static void update_vsie_sca(struct vsie_sca *vsie_sca, unsigned int cpu_nr,\n+\t\t\t struct vsie_page *vsie_page_n, hpa_t sca_o_entry_hpa)\n+{\n+\tguard(mutex)(\u0026vsie_sca-\u003emutex);\n+\n+\tWRITE_ONCE(vsie_sca-\u003essca.cpu[cpu_nr].ssda, virt_to_phys(\u0026vsie_page_n-\u003escb_s));\n+\tWRITE_ONCE(vsie_sca-\u003essca.cpu[cpu_nr].ossea, sca_o_entry_hpa);\n+\tWRITE_ONCE(vsie_sca-\u003epages[cpu_nr], vsie_page_n);\n+}\n+\n+static int _shadow_sca_cpu(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,\n+\t\t\t struct vsie_sca *vsie_sca, hpa_t sca_o_entry_hpa,\n+\t\t\t unsigned int cpu_nr, bool is_esca)\n+{\n+\thva_t sca_o_entry_hva = (hva_t)phys_to_virt(sca_o_entry_hpa);\n+\tstruct vsie_page *vsie_page_n;\n+\tgpa_t scb_o_gpa;\n+\tint rc;\n+\n+\tif (is_esca)\n+\t\tscb_o_gpa = ((struct esca_entry *)sca_o_entry_hva)-\u003esda;\n+\telse\n+\t\tscb_o_gpa = ((struct bsca_entry *)sca_o_entry_hva)-\u003esda;\n+\tif (scb_o_gpa \u0026 0x1ffUL)\n+\t\treturn set_validity_icpt(vsie_page-\u003escb_o, 0x0001U);\n+\n+\trc = get_vsie_page_cpu_nr(vcpu, vsie_sca, scb_o_gpa, cpu_nr, \u0026vsie_page_n);\n+\tif (rc)\n+\t\treturn rc;\n+\n+\trc = shadow_scb_minimal(vcpu, vsie_page_n);\n+\tupdate_vsie_sca(vsie_sca, cpu_nr, vsie_page_n, sca_o_entry_hpa);\n+\tif (rc) {\n+\t\t/* copy intercept to primary scb_o, no unshadow_scb() on exit */\n+\t\tunshadow_intercept(vsie_page-\u003escb_o, \u0026vsie_page_n-\u003escb_s);\n+\t\trc = 1;\n+\t}\n+\tput_vsie_page(vsie_page_n);\n+\n+\treturn rc;\n+}\n+\n+/* Fill the shadow system control area used for VSIE SIGPI. */\n+static int _shadow_sca(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,\n+\t\t struct vsie_sca *vsie_sca)\n+{\n+\tbool is_esca = sie_uses_esca(vsie_page-\u003escb_o);\n+\tunsigned int cpu_nr, cpu_slots;\n+\thpa_t sca_o_entry_hpa;\n \tint rc;\n \n+\tif (is_esca)\n+\t\tset_bit(VSIE_SCA_ESCA, \u0026vsie_sca-\u003eflags);\n+\telse\n+\t\tclear_bit(VSIE_SCA_ESCA, \u0026vsie_sca-\u003eflags);\n+\tsca_mcn_copy(vsie_sca, phys_to_virt(sca_o_hpa(vsie_sca)));\n+\n+\t/* pin and make minimal shadow for ALL scb in the sca */\n+\tcpu_slots = is_esca ? KVM_S390_MAX_VSIE_VCPUS : KVM_S390_BSCA_CPU_SLOTS;\n+\tfor_each_set_bit_inv(cpu_nr, (unsigned long *)\u0026vsie_sca-\u003emcn, cpu_slots) {\n+\t\trc = get_sca_entry_addr(vcpu-\u003ekvm, vsie_sca, cpu_nr, NULL, \u0026sca_o_entry_hpa);\n+\t\tif (rc)\n+\t\t\tbreak;\n+\n+\t\tif ((vsie_page-\u003escb_o-\u003eicpua \u0026 KVM_S390_CPU_MASK) == cpu_nr) {\n+\t\t\tupdate_vsie_sca(vsie_sca, cpu_nr, vsie_page, sca_o_entry_hpa);\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\trc = _shadow_sca_cpu(vcpu, vsie_page, vsie_sca, sca_o_entry_hpa, cpu_nr, is_esca);\n+\t\tif (rc)\n+\t\t\tbreak;\n+\t}\n+\n+\tif (rc) {\n+\t\tvsie_sca-\u003essca.osca = 0;\n+\t\tfor_each_set_bit_inv(cpu_nr, (unsigned long *)\u0026vsie_sca-\u003emcn, cpu_slots) {\n+\t\t\tvsie_sca-\u003essca.cpu[cpu_nr].ssda = 0;\n+\t\t\tvsie_sca-\u003essca.cpu[cpu_nr].ossea = 0;\n+\t\t}\n+\t} else {\n+\t\tvsie_sca-\u003essca.osca = sca_o_hpa(vsie_sca);\n+\t}\n+\treturn rc;\n+}\n+\n+static bool shadow_sca_needed(struct vsie_sca *vsie_sca)\n+{\n+\tif (!vsie_sca-\u003essca.osca)\n+\t\treturn true;\n+\treturn !sca_mcn_equals(vsie_sca, phys_to_virt(sca_o_hpa(vsie_sca)));\n+}\n+\n+/* Shadow or reshadow the SCA on VSIE enter. */\n+static int shadow_sca(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct vsie_sca *vsie_sca)\n+{\n+\tscoped_guard(rwsem_read, \u0026vcpu-\u003ekvm-\u003earch.vsie.vsie_sca_lock) {\n+\t\tif (!shadow_sca_needed(vsie_sca))\n+\t\t\treturn 0;\n+\t}\n+\n+\tguard(rwsem_write)(\u0026vcpu-\u003ekvm-\u003earch.vsie.vsie_sca_lock);\n+\tif (!shadow_sca_needed(vsie_sca))\n+\t\treturn 0;\n+\n+\treturn _shadow_sca(vcpu, vsie_page, vsie_sca);\n+}\n+\n+int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)\n+{\n+\tstruct vsie_page *vsie_page = NULL;\n+\tstruct vsie_sca *vsie_sca = NULL;\n+\tstruct kvm_s390_sie_block *scb_o;\n+\tgpa_t scb_addr;\n+\thpa_t scb_hpa;\n+\tint rc = 0;\n+\n \tvcpu-\u003estat.instruction_sie++;\n \tif (!test_kvm_cpu_feat(vcpu-\u003ekvm, KVM_S390_VM_CPU_FEAT_SIEF2))\n \t\treturn -EOPNOTSUPP;\n@@ -1576,35 +2306,60 @@ int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)\n \t\treturn 0;\n \t}\n \n-\tvsie_page = get_vsie_page(vcpu-\u003ekvm, scb_addr);\n-\tif (IS_ERR(vsie_page)) {\n-\t\treturn PTR_ERR(vsie_page);\n-\t} else if (!vsie_page) {\n-\t\t/* double use of sie control block - simply do nothing */\n-\t\tkvm_s390_rewind_psw(vcpu, 4);\n-\t\treturn 0;\n+\trc = pin_guest_page(vcpu-\u003ekvm, scb_addr, \u0026scb_hpa);\n+\tif (rc)\n+\t\treturn kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);\n+\tscb_o = (struct kvm_s390_sie_block *)phys_to_virt(scb_hpa);\n+\n+\tif (!use_ssca(vcpu-\u003ekvm, scb_o)) {\n+\t\t/* get the vsie_page with pinned scb_o */\n+\t\trc = get_vsie_page(vcpu, scb_addr, \u0026vsie_page);\n+\t\tif (rc)\n+\t\t\tgoto out_unpin;\n+\t\tvsie_page-\u003evsie_sca = NULL;\n+\t} else {\n+\t\t/* get the vsie_sca with pinned original sca */\n+\t\trc = get_vsie_sca(vcpu, scb_o, \u0026vsie_sca);\n+\t\tif (rc)\n+\t\t\tgoto out_unpin;\n+\t\trc = get_vsie_page_cpu_nr(vcpu, vsie_sca, scb_addr,\n+\t\t\t\t\t scb_o-\u003eicpua \u0026 KVM_S390_CPU_MASK, \u0026vsie_page);\n+\t\tif (rc)\n+\t\t\tgoto out_put_sca;\n \t}\n \n-\trc = pin_scb(vcpu, vsie_page, scb_addr);\n-\tif (rc)\n-\t\tgoto out_put;\n \trc = shadow_scb(vcpu, vsie_page);\n \tif (rc)\n-\t\tgoto out_unpin_scb;\n+\t\tgoto out_put;\n+\tif (vsie_sca) {\n+\t\t/* pin and shadow the sca including all scb_o in the g3 conf */\n+\t\trc = shadow_sca(vcpu, vsie_page, vsie_sca);\n+\t\tif (rc)\n+\t\t\tgoto out_put;\n+\t}\n+\n \trc = pin_blocks(vcpu, vsie_page);\n \tif (rc)\n \t\tgoto out_unshadow;\n \tregister_shadow_scb(vcpu, vsie_page);\n+\n \trc = vsie_run(vcpu, vsie_page);\n+\n \tunregister_shadow_scb(vcpu);\n \tunpin_blocks(vcpu, vsie_page);\n out_unshadow:\n \tunshadow_scb(vcpu, vsie_page);\n-out_unpin_scb:\n-\tunpin_scb(vcpu, vsie_page, scb_addr);\n out_put:\n \tput_vsie_page(vsie_page);\n+out_put_sca:\n+\tput_vsie_sca(vsie_sca);\n+out_unpin:\n+\tunpin_guest_page(vcpu-\u003ekvm, scb_addr, scb_hpa);\n \n+\tif (rc == -EAGAIN) {\n+\t\tkvm_s390_rewind_psw(vcpu, 4);\n+\t\trc = 0;\n+\t}\n \treturn rc \u003c 0 ? rc : 0;\n }\n \n@@ -1612,30 +2367,55 @@ int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)\n void kvm_s390_vsie_init(struct kvm *kvm)\n {\n \tmutex_init(\u0026kvm-\u003earch.vsie.mutex);\n-\tINIT_RADIX_TREE(\u0026kvm-\u003earch.vsie.addr_to_page, GFP_KERNEL_ACCOUNT);\n+\txa_init_flags(\u0026kvm-\u003earch.vsie.addr_to_page, XA_FLAGS_ACCOUNT);\n+\tinit_rwsem(\u0026kvm-\u003earch.vsie.vsie_sca_lock);\n+\txa_init_flags(\u0026kvm-\u003earch.vsie.osca_to_sca, XA_FLAGS_ACCOUNT);\n+}\n+\n+static void kvm_s390_vsie_destroy_page(struct kvm *kvm, struct vsie_page *vsie_page)\n+{\n+\tunpin_scb(kvm, vsie_page);\n+\trelease_gmap_shadow_safe(kvm, vsie_page);\n+\tfree_vsie_page(vsie_page);\n }\n \n /* Destroy the vsie data structures. To be called when a vm is destroyed. */\n void kvm_s390_vsie_destroy(struct kvm *kvm)\n {\n \tstruct vsie_page *vsie_page;\n-\tint i;\n+\tstruct vsie_sca *vsie_sca;\n+\tint i, cpu_nr;\n+\n+\tguard(mutex)(\u0026kvm-\u003earch.vsie.mutex);\n \n-\tmutex_lock(\u0026kvm-\u003earch.vsie.mutex);\n \tfor (i = 0; i \u003c kvm-\u003earch.vsie.page_count; i++) {\n \t\tvsie_page = kvm-\u003earch.vsie.pages[i];\n-\t\tscoped_guard(spinlock, \u0026kvm-\u003earch.gmap-\u003echildren_lock)\n-\t\t\tif (vsie_page-\u003egmap_cache.gmap)\n-\t\t\t\trelease_gmap_shadow(vsie_page);\n \t\tkvm-\u003earch.vsie.pages[i] = NULL;\n-\t\t/* free the radix tree entry */\n-\t\tif (vsie_page-\u003escb_gpa != ULONG_MAX)\n-\t\t\tradix_tree_delete(\u0026kvm-\u003earch.vsie.addr_to_page,\n-\t\t\t\t\t vsie_page-\u003escb_gpa \u003e\u003e SCB_ALIGNMENT_SHIFT);\n-\t\tfree_page((unsigned long)vsie_page);\n+\t\tkvm_s390_vsie_destroy_page(kvm, vsie_page);\n \t}\n+\n \tkvm-\u003earch.vsie.page_count = 0;\n-\tmutex_unlock(\u0026kvm-\u003earch.vsie.mutex);\n+\tfor (i = 0; i \u003c kvm-\u003earch.vsie.sca_count; i++) {\n+\t\tvsie_sca = kvm-\u003earch.vsie.scas[i];\n+\t\tkvm-\u003earch.vsie.scas[i] = NULL;\n+\t\tif (!vsie_sca)\n+\t\t\tcontinue;\n+\n+\t\tfor (cpu_nr = 0; cpu_nr \u003c KVM_S390_MAX_VSIE_VCPUS; cpu_nr++) {\n+\t\t\tvsie_page = vsie_sca-\u003epages[cpu_nr];\n+\t\t\tvsie_sca-\u003epages[cpu_nr] = NULL;\n+\t\t\tif (!vsie_page)\n+\t\t\t\tcontinue;\n+\t\t\tunpin_scb(kvm, vsie_page);\n+\t\t\tkvm_s390_vsie_destroy_page(kvm, vsie_page);\n+\t\t}\n+\n+\t\tunpin_sca(kvm, vsie_sca);\n+\t\tfree_vsie_sca(kvm, vsie_sca);\n+\t}\n+\tkvm-\u003earch.vsie.sca_count = 0;\n+\txa_destroy(\u0026kvm-\u003earch.vsie.addr_to_page);\n+\txa_destroy(\u0026kvm-\u003earch.vsie.osca_to_sca);\n }\n \n void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)\ndiff --git a/drivers/s390/char/sclp_early.c b/drivers/s390/char/sclp_early.c\nindex 22dd797e62291..40e6e346ee083 100644\n--- a/drivers/s390/char/sclp_early.c\n+++ b/drivers/s390/char/sclp_early.c\n@@ -56,6 +56,7 @@ static void __init sclp_early_facilities_detect(void)\n \t\tsclp.has_diag318 = !!(sccb-\u003ebyte_134 \u0026 0x80);\n \t\tsclp.has_diag320 = !!(sccb-\u003ebyte_134 \u0026 0x04);\n \t\tsclp.has_iplcc = !!(sccb-\u003ebyte_134 \u0026 0x02);\n+\t\tsclp.has_vsie_interp_extf = !!(sccb-\u003ebyte_134 \u0026 0x01);\n \t}\n \tif (sccb-\u003ecpuoff \u003e 137) {\n \t\tsclp.has_sipl = !!(sccb-\u003ecbl \u0026 0x4000);\ndiff --git a/tools/testing/selftests/kvm/include/s390/sie.h b/tools/testing/selftests/kvm/include/s390/sie.h\nindex 160acd4a1db92..4ff1c1a354af5 100644\n--- a/tools/testing/selftests/kvm/include/s390/sie.h\n+++ b/tools/testing/selftests/kvm/include/s390/sie.h\n@@ -223,7 +223,7 @@ struct kvm_s390_sie_block {\n \t__u32\tfac;\t\t\t/* 0x01a0 */\n \t__u8\treserved1a4[20];\t/* 0x01a4 */\n \t__u64\tcbrlo;\t\t\t/* 0x01b8 */\n-\t__u8\treserved1c0[8];\t\t/* 0x01c0 */\n+\t__u64\tosda;\t\t\t/* 0x01c0 */\n #define ECD_HOSTREGMGMT\t0x20000000\n #define ECD_MEF\t\t0x08000000\n #define ECD_ETOKENF\t0x02000000\n"
} |
| 2/1 | 2026/08/27 16:16 | agent | patch-evaluator |
0mModel:best-expensive Args: null Results: {
"EnableConfigs": [
"KVM"
],
"FocusSymbols": [
"kvm_s390_handle_vsie",
"kvm_s390_vsie_gmap_notifier"
],
"Reasoning": "The patch implements shadowing of the System Control Area (SCA) for nested virtualization (vSIE) on s390x architecture. This allows nested guests to use the SSCA for SIGPI and SRSI instructions. The changes involve significant modifications to the vSIE page management, SCA shadowing logic, and memory pinning in `arch/s390/kvm/s390/vsie.c`. These are reachable core KVM features when running nested guests, making them highly relevant for fuzzing.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer. Your job is to review a provided patch series and determine if it makes functional changes to the kernel that should be fuzzed. IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively use your code access tools to examine the actual source code and confirm any assumptions. Return WorthFuzzing=false if the patch only contains: - Modifications to Documentation/, Kconfig files, or code comments. - Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints. - Changes to numeric constants or macros that do not functionally alter execution flow. - Code paths that are impossible to reach in virtualized environments like GCE or QEMU, even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim). - Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed, ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU. - Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove) that are executed only during PCI hot-unplug or sysfs driver unbind operations. If it modifies reachable core kernel logic, drivers, or architectures, use your code search tools to verify the code can be executed, then return WorthFuzzing=true. When returning WorthFuzzing=true, you MUST ALSO: 1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols. Avoid listing generic hot-path functions to prevent skewed test distributions. Prefer non-static, non-inlined API entrypoint functions over internal static helper functions (which are inlined by the compiler and do not have distinct symbol addresses). 2. Identify any specific CONFIG_ options required to properly test this new/modified feature. Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions. Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4"). Prefer calling several tools at the same time to save round-trips. Use set-results tool to provide results of the analysis. It must be called exactly once before the final reply. Ignore results of this tool. Prompt: For your convenience, here is the diff of the changes:
commit 25a55cc0e16c4ee5e1af3cd4c4c18a54af94e43c
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Aug 27 16:16:25 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/s390/include/asm/kvm_host_s390.h b/arch/s390/include/asm/kvm_host_s390.h
index cd692f8fb7643..a19d3a5f550ce 100644
--- a/arch/s390/include/asm/kvm_host_s390.h
+++ b/arch/s390/include/asm/kvm_host_s390.h
@@ -412,6 +412,10 @@ struct kvm_vm_stat {
u64 gmap_shadow_r3_entry;
u64 gmap_shadow_sg_entry;
u64 gmap_shadow_pg_entry;
+ u64 vsie_shadow_scb;
+ u64 vsie_shadow_scb_reuse;
+ u64 vsie_shadow_sca;
+ u64 vsie_shadow_sca_reuse;
};
struct kvm_arch_memory_slot {
@@ -572,13 +576,32 @@ struct sie_page2 {
};
struct vsie_page;
+struct vsie_sca;
+/*
+ * vsie_pages, scas and accompanied management vars
+ */
struct kvm_s390_vsie {
+ /*
+ * protects pages[], page_count, next, addr_to_page
+ */
struct mutex mutex;
- struct radix_tree_root addr_to_page;
+ struct xarray addr_to_page;
int page_count;
int next;
- struct vsie_page *pages[KVM_MAX_VCPUS];
+ struct vsie_page *pages[KVM_S390_MAX_VSIE_VCPUS];
+ /*
+ * The vsie_sca_lock is used to synchronize access to
+ * - the kvm_s390_vsie.scas[]
+ * - the kvm_s390_vsie.osca_to_sca map
+ * - sca_count and sca_next
+ * - new vsie_sca creation and initialization
+ */
+ struct rw_semaphore vsie_sca_lock;
+ struct xarray osca_to_sca;
+ int sca_count;
+ int sca_next;
+ struct vsie_sca *scas[KVM_S390_MAX_VSIE_VCPUS];
};
struct kvm_s390_gisa_iam {
@@ -623,6 +646,7 @@ struct kvm_arch {
int use_pfmfi;
int use_skf;
int use_zpci_interp;
+ int use_ssca;
int user_cpu_state_ctrl;
int user_sigp;
int user_stsi;
diff --git a/arch/s390/include/asm/kvm_host_s390_types.h b/arch/s390/include/asm/kvm_host_s390_types.h
index 9e348a5304217..6eef71072b4ed 100644
--- a/arch/s390/include/asm/kvm_host_s390_types.h
+++ b/arch/s390/include/asm/kvm_host_s390_types.h
@@ -6,10 +6,15 @@
#include <linux/atomic.h>
#include <linux/types.h>
+#define KVM_S390_CPU_MASK 0xff
+#define KVM_S390_MAX_VSIE_VCPUS 256
+#define KVM_S390_MAX_SCA_PAGES 5
+
#define KVM_S390_BSCA_CPU_SLOTS 64
#define KVM_S390_ESCA_CPU_SLOTS 248
#define SCB_ALIGNMENT_SHIFT 9
+#define SCA_ALIGNMENT_SHIFT 6
#define SIGP_CTRL_C 0x80
#define SIGP_CTRL_SCN_MASK 0x3f
@@ -47,6 +52,13 @@ struct bsca_entry {
__u64 reserved2[2];
};
+struct ssca_entry {
+ __u64 reserved1;
+ __u64 ssda;
+ __u64 ossea;
+ __u64 reserved2;
+};
+
union ipte_control {
unsigned long val;
struct {
@@ -88,6 +100,20 @@ struct esca_block {
struct esca_entry cpu[KVM_S390_ESCA_CPU_SLOTS];
};
+/*
+ * The shadow sca / ssca needs to cover both bsca and esca depending on what the
+ * guest uses so we allocate space for 256 entries that are defined in the
+ * architecture.
+ * The header part of the struct must not cross page boundaries.
+ */
+struct ssca_block {
+ __u64 osca;
+ __u64 reserved08[7];
+ struct ssca_entry cpu[KVM_S390_MAX_VSIE_VCPUS];
+};
+
+static_assert(offsetof(struct ssca_block, cpu) == 64);
+
/*
* This struct is used to store some machine check info from lowcore
* for machine checks that happen while the guest is running.
@@ -317,7 +343,7 @@ struct kvm_s390_sie_block {
__u32 fac; /* 0x01a0 */
__u8 reserved1a4[20]; /* 0x01a4 */
__u64 cbrlo; /* 0x01b8 */
- __u8 reserved1c0[8]; /* 0x01c0 */
+ __u64 osda; /* 0x01c0 */
#define ECD_HOSTREGMGMT 0x20000000
#define ECD_MEF 0x08000000
#define ECD_ETOKENF 0x02000000
diff --git a/arch/s390/include/asm/sclp.h b/arch/s390/include/asm/sclp.h
index 44066550b9b10..9e960c09ce0b3 100644
--- a/arch/s390/include/asm/sclp.h
+++ b/arch/s390/include/asm/sclp.h
@@ -105,6 +105,7 @@ struct sclp_info {
unsigned char has_dirq : 1;
unsigned char has_iplcc : 1;
unsigned char has_zpci_lsi : 1;
+ unsigned char has_vsie_interp_extf : 1;
unsigned char has_aisii : 1;
unsigned char has_aeni : 1;
unsigned char has_aisi : 1;
diff --git a/arch/s390/kvm/s390/s390.c b/arch/s390/kvm/s390/s390.c
index b0839e887221e..ffa5bbb8713bc 100644
--- a/arch/s390/kvm/s390/s390.c
+++ b/arch/s390/kvm/s390/s390.c
@@ -87,6 +87,10 @@ const struct kvm_stats_desc kvm_vm_stats_desc[] = {
STATS_DESC_COUNTER(VM, gmap_shadow_r3_entry),
STATS_DESC_COUNTER(VM, gmap_shadow_sg_entry),
STATS_DESC_COUNTER(VM, gmap_shadow_pg_entry),
+ STATS_DESC_COUNTER(VM, vsie_shadow_scb),
+ STATS_DESC_COUNTER(VM, vsie_shadow_scb_reuse),
+ STATS_DESC_COUNTER(VM, vsie_shadow_sca),
+ STATS_DESC_COUNTER(VM, vsie_shadow_sca_reuse),
};
const struct kvm_stats_header kvm_vm_stats_header = {
@@ -244,6 +248,11 @@ static int async_destroy = 1;
module_param(async_destroy, int, 0444);
MODULE_PARM_DESC(async_destroy, "Asynchronous destroy for protected guests");
+/* allow vsie sigp and srs interpretation if available */
+static bool ssca = true;
+module_param(ssca, bool, 0444);
+MODULE_PARM_DESC(ssca, "VSIE SIGPI and SRSI using the SSCA");
+
#define HMFAI_DWORDS 16
/*
* Base feature mask that defines default mask for facilities. Consists of the
@@ -473,6 +482,8 @@ static void __init kvm_s390_cpu_feat_init(void)
allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
if (sclp.has_astfleie2)
allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ASTFLEIE2);
+ if (sclp.has_vsie_interp_extf && ssca)
+ allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIGPIF);
/*
* KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
* all skey handling functions read/set the skey from the PGSTE
@@ -487,9 +498,6 @@ static void __init kvm_s390_cpu_feat_init(void)
* For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
* KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
* correctly shadowed. We can do that for the PGSTE but not for PTE.I.
- *
- * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
- * cannot easily shadow the SCA because of the ipte lock.
*/
}
@@ -3359,6 +3367,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
kvm->arch.use_pfmfi = sclp.has_pfmfi;
kvm->arch.use_skf = sclp.has_skey;
+ kvm->arch.use_ssca = sclp.has_vsie_interp_extf && ssca;
spin_lock_init(&kvm->arch.start_stop_lock);
kvm_s390_vsie_init(kvm);
if (use_gisa)
diff --git a/arch/s390/kvm/s390/vsie.c b/arch/s390/kvm/s390/vsie.c
index 5926689fa2267..67b68f4302b74 100644
--- a/arch/s390/kvm/s390/vsie.c
+++ b/arch/s390/kvm/s390/vsie.c
@@ -29,6 +29,7 @@
enum vsie_page_flags {
VSIE_PAGE_IN_USE = 0,
+ VSIE_PAGE_SCB_PINNED = 1,
};
struct vsie_page {
@@ -56,13 +57,13 @@ struct vsie_page {
gpa_t sdnx_gpa; /* 0x0250 */
/*
* guest address of the original SCB. Remains set for free vsie
- * pages, so we can properly look them up in our addr_to_page
- * radix tree.
+ * pages, so we can properly look them up in our addr_to_page map.
*/
gpa_t scb_gpa; /* 0x0258 */
/* the shadow gmap in use by the vsie_page */
struct gmap_cache gmap_cache; /* 0x0260 */
- __u8 reserved[0x06f8 - 0x0278]; /* 0x0278 */
+ struct vsie_sca *vsie_sca; /* 0x0278 */
+ __u8 reserved[0x06f8 - 0x0280]; /* 0x0280 */
struct kvm_s390_crypto_cb crycb; /* 0x06f8 */
__u8 fac[8 + S390_ARCH_FAC_LIST_SIZE_BYTE];/* 0x07f8 */
};
@@ -71,6 +72,88 @@ static_assert(sizeof(struct vsie_page) == PAGE_SIZE);
static_assert(offsetof(struct vsie_page, mcck_info) == offsetof(struct sie_page, mcck_info));
static_assert(IS_ALIGNED(offsetof(struct vsie_page, crycb), 8));
+struct kvm_address_pair {
+ gpa_t gpa;
+ hpa_t hpa;
+};
+
+enum vsie_sca_flags {
+ VSIE_SCA_ESCA = 0,
+ VSIE_SCA_SCA_PINNED = 1,
+};
+
+struct vsie_sca {
+ struct_group(head,
+ struct ssca_block ssca;
+ );
+ struct_group(keep,
+ struct vsie_page *pages[KVM_S390_MAX_VSIE_VCPUS];
+ /* The mutex is used to synchronize access to the pages[] */
+ struct mutex mutex;
+ atomic_t ref_count;
+ );
+ struct_group(tail,
+ gpa_t sca_gpa;
+ unsigned long flags;
+ u64 mcn[4];
+ unsigned int sca_o_nr_pages;
+ struct kvm_address_pair sca_o_pages[KVM_S390_MAX_SCA_PAGES];
+ );
+};
+
+/*
+ * SSCA needs to be 32-byte aligned and members before the cpu field need to be on the same page.
+ * Forcing it to offset 0 will always fulfill the requirements.
+ */
+static_assert(!(offsetof(struct vsie_sca, ssca)));
+
+/* maximum vsie shadow scb */
+static unsigned int vsie_shadow_scb_max = 1;
+module_param(vsie_shadow_scb_max, uint, 0644);
+MODULE_PARM_DESC(vsie_shadow_scb_max, "Maximum number of VSIE shadow control blocks to keep. Values smaller number VCPUs uses number of VCPUs; maximum 256");
+
+/* maximum vsie shadow sca */
+static unsigned int vsie_shadow_sca_max = 1;
+module_param(vsie_shadow_sca_max, uint, 0644);
+MODULE_PARM_DESC(vsie_shadow_sca_max, "Maximum number of VSIE shadow system control areas to keep. Values smaller number of VCPUs uses number of VCPUs; maximum 256");
+
+static inline hpa_t sca_o_hpa(struct vsie_sca *vsie_sca)
+{
+ return vsie_sca->sca_o_pages[0].hpa | (vsie_sca->sca_gpa & ~PAGE_MASK);
+}
+
+static inline bool sie_uses_esca(struct kvm_s390_sie_block *scb)
+{
+ return (scb->ecb2 & ECB2_ESCA);
+}
+
+static unsigned long read_scao(struct kvm *kvm, struct kvm_s390_sie_block *scb)
+{
+ unsigned long vsie_sca = READ_ONCE(scb->scaol) & ~0xfUL;
+
+ if (test_kvm_cpu_feat(kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
+ vsie_sca |= (u64)READ_ONCE(scb->scaoh) << 32;
+
+ return vsie_sca;
+}
+
+static void write_scao(struct kvm_s390_sie_block *scb, unsigned long hpa)
+{
+ scb->scaoh = (u32)((u64)hpa >> 32);
+ scb->scaol = (u32)(u64)hpa;
+}
+
+static inline bool use_ssca(struct kvm *kvm, struct kvm_s390_sie_block *scb)
+{
+ if (!kvm->arch.use_ssca)
+ return false;
+ if (!(scb->eca & ECA_SIGPI) && !(scb->ecb & ECB_SRSI))
+ return false;
+ if (!read_scao(kvm, scb))
+ return false;
+ return true;
+}
+
/* trigger a validity icpt for the given scb */
static int set_validity_icpt(struct kvm_s390_sie_block *scb,
__u16 reason_code)
@@ -81,6 +164,25 @@ static int set_validity_icpt(struct kvm_s390_sie_block *scb,
return 1;
}
+/* The sca header must not cross pages etc. */
+static int validate_scao(struct kvm_vcpu *vcpu, struct kvm_s390_sie_block *scb, gpa_t gpa)
+{
+ int offset;
+
+ if (gpa < 2 * PAGE_SIZE)
+ return set_validity_icpt(scb, 0x0038U);
+ if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
+ return set_validity_icpt(scb, 0x0011U);
+
+ if (sie_uses_esca(scb))
+ offset = offsetof(struct esca_block, cpu[0]) - 1;
+ else
+ offset = offsetof(struct bsca_block, cpu[0]) - 1;
+ if ((gpa & PAGE_MASK) != ((gpa + offset) & PAGE_MASK))
+ return set_validity_icpt(scb, 0x003bU);
+ return 0;
+}
+
/* mark the prefix as unmapped, this will block the VSIE */
static void prefix_unmapped(struct vsie_page *vsie_page)
{
@@ -109,6 +211,148 @@ static int prefix_is_mapped(struct vsie_page *vsie_page)
return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
}
+static void release_gmap_shadow(struct vsie_page *vsie_page)
+{
+ struct gmap *gmap = vsie_page->gmap_cache.gmap;
+
+ lockdep_assert_held(&gmap->kvm->arch.gmap->children_lock);
+
+ list_del(&vsie_page->gmap_cache.list);
+ vsie_page->gmap_cache.gmap = NULL;
+ prefix_unmapped(vsie_page);
+
+ if (list_empty(&gmap->scb_users)) {
+ gmap_remove_child(gmap);
+ gmap_put(gmap);
+ }
+}
+
+static void release_gmap_shadow_safe(struct kvm *kvm, struct vsie_page *vsie_page)
+{
+ if (!vsie_page->gmap_cache.gmap)
+ return;
+
+ guard(spinlock)(&kvm->arch.gmap->children_lock);
+
+ if (vsie_page->gmap_cache.gmap)
+ release_gmap_shadow(vsie_page);
+}
+
+static struct gmap *acquire_gmap_shadow(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+{
+ union ctlreg0 cr0;
+ struct gmap *gmap;
+ union asce asce;
+ int edat;
+
+ asce.val = vcpu->arch.sie_block->gcr[1];
+ cr0.val = vcpu->arch.sie_block->gcr[0];
+ edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
+ edat += edat && test_kvm_facility(vcpu->kvm, 78);
+
+ scoped_guard(spinlock, &vcpu->kvm->arch.gmap->children_lock) {
+ gmap = vsie_page->gmap_cache.gmap;
+ if (gmap) {
+ /*
+ * ASCE or EDAT could have changed since last icpt, or the gmap
+ * we're holding has been unshadowed. If the gmap is still valid,
+ * we can safely reuse it.
+ */
+ if (gmap_is_shadow_valid(gmap, asce, edat)) {
+ vcpu->kvm->stat.gmap_shadow_reuse++;
+ gmap_get(gmap);
+ return gmap;
+ }
+ /* release the old shadow and mark the prefix as unmapped */
+ release_gmap_shadow(vsie_page);
+ }
+ }
+again:
+ gmap = gmap_create_shadow(vcpu->arch.mc, vcpu->kvm->arch.gmap, asce, edat);
+ if (IS_ERR(gmap))
+ return gmap;
+ scoped_guard(spinlock, &vcpu->kvm->arch.gmap->children_lock) {
+ /* unlikely race condition, remove the previous shadow */
+ if (vsie_page->gmap_cache.gmap)
+ release_gmap_shadow(vsie_page);
+ if (!gmap->parent) {
+ gmap_put(gmap);
+ goto again;
+ }
+ vcpu->kvm->stat.gmap_shadow_create++;
+ list_add(&vsie_page->gmap_cache.list, &gmap->scb_users);
+ vsie_page->gmap_cache.gmap = gmap;
+ prefix_unmapped(vsie_page);
+ }
+ return gmap;
+}
+
+/*
+ * Pin the guest page given by gpa and set hpa to the pinned host address.
+ * Will always be pinned writable.
+ *
+ * Returns: - 0 on success
+ * - -EINVAL if the gpa is not valid guest storage
+ */
+static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
+{
+ struct page *page;
+
+ page = gfn_to_page(kvm, gpa_to_gfn(gpa));
+ if (!page)
+ return -EINVAL;
+ *hpa = (hpa_t)page_to_phys(page) + (gpa & ~PAGE_MASK);
+ return 0;
+}
+
+/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
+static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
+{
+ kvm_release_page_dirty(pfn_to_page(phys_to_pfn(hpa)));
+ /* mark the page always as dirty for migration */
+ mark_page_dirty(kvm, gpa_to_gfn(gpa));
+}
+
+/* unpin multiple guest pages pinned with pin_guest_pages() */
+static void unpin_guest_pages(struct kvm *kvm, struct kvm_address_pair *addr, unsigned int nr_pages)
+{
+ int i;
+
+ for (i = 0; i < nr_pages; i++) {
+ unpin_guest_page(kvm, addr[i].gpa, addr[i].hpa);
+ addr[i].gpa = 0;
+ addr[i].hpa = 0;
+ }
+}
+
+/*
+ * pin nr_pages consecutive guest pages
+ */
+static int pin_guest_pages(struct kvm *kvm, gpa_t gpa, unsigned int nr_pages,
+ struct kvm_address_pair *addr, unsigned int *nr_pages_pinned)
+{
+ int rc = 0, i;
+ hpa_t hpa;
+
+ gpa = gpa & PAGE_MASK;
+
+ /* the guest pages may not be mapped continuously, so pin each page */
+ for (i = 0; i < nr_pages; i++) {
+ rc = pin_guest_page(kvm, gpa + PAGE_SIZE * i, &hpa);
+ if (rc)
+ goto err;
+ addr[i].gpa = gpa + PAGE_SIZE * i;
+ addr[i].hpa = hpa;
+ }
+ *nr_pages_pinned = i;
+ return rc;
+
+err:
+ unpin_guest_pages(kvm, addr, i);
+ *nr_pages_pinned = 0;
+ return rc;
+}
+
/* copy the updated intervention request bits into the shadow scb */
static void update_intervention_requests(struct vsie_page *vsie_page)
{
@@ -124,8 +368,7 @@ static void update_intervention_requests(struct vsie_page *vsie_page)
static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
{
struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
- struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
- int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
+ int newflags, cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
/* we don't allow ESA/390 guests unless explicitly enabled */
if (!(cpuflags & CPUSTAT_ZARCH) && !vcpu->kvm->arch.allow_vsie_esamode)
@@ -424,18 +667,27 @@ static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
}
}
-/* unshadow the scb, copying parameters back to the real scb */
-static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+/*
+ * Only write the interception fields back; avoid clobbering the timer and
+ * guest-state fields in scb_o.
+ */
+static inline void unshadow_intercept(struct kvm_s390_sie_block *scb_o,
+ struct kvm_s390_sie_block *scb_s)
{
- struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
- struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
-
- /* interception */
scb_o->icptcode = scb_s->icptcode;
scb_o->icptstatus = scb_s->icptstatus;
scb_o->ipa = scb_s->ipa;
scb_o->ipb = scb_s->ipb;
scb_o->gbea = scb_s->gbea;
+}
+
+/* unshadow the scb, copying parameters back to the real scb */
+static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+{
+ struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
+ struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
+
+ unshadow_intercept(scb_o, scb_s);
/* timer */
scb_o->cputm = scb_s->cputm;
@@ -469,6 +721,39 @@ static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
scb_o->ihcpu = scb_s->ihcpu;
}
+static int shadow_scb_minimal(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+{
+ struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
+ struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
+ int rc;
+
+ /* make sure we don't have any leftovers when reusing the scb */
+ scb_s->icptcode = 0;
+ scb_s->eca = 0;
+ scb_s->ecb = 0;
+ scb_s->ecb2 = 0;
+ scb_s->ecb3 = 0;
+ scb_s->ecd = 0;
+ scb_s->fac = 0;
+ scb_s->fpf = 0;
+
+ rc = prepare_cpuflags(vcpu, vsie_page);
+ if (rc)
+ return rc;
+
+ scb_s->icpua = scb_o->icpua;
+ scb_s->ecb2 |= scb_o->ecb2 & ECB2_ESCA;
+
+ if (vsie_page->vsie_sca) {
+ scb_s->eca |= scb_o->eca & ECA_SIGPI;
+ scb_s->ecb |= scb_o->ecb & ECB_SRSI;
+ write_scao(scb_s, virt_to_phys(&vsie_page->vsie_sca->ssca));
+ scb_s->osda = virt_to_phys(scb_o);
+ }
+
+ return 0;
+}
+
/*
* Setup the shadow scb by copying and checking the relevant parts of the g2
* provided scb.
@@ -488,19 +773,9 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
unsigned long new_mso = 0;
int rc;
- /* make sure we don't have any leftovers when reusing the scb */
- scb_s->icptcode = 0;
- scb_s->eca = 0;
- scb_s->ecb = 0;
- scb_s->ecb2 = 0;
- scb_s->ecb3 = 0;
- scb_s->ecd = 0;
- scb_s->fac = 0;
- scb_s->fpf = 0;
-
- rc = prepare_cpuflags(vcpu, vsie_page);
+ rc = shadow_scb_minimal(vcpu, vsie_page);
if (rc)
- goto out;
+ goto out_validity;
/* timer */
scb_s->cputm = scb_o->cputm;
@@ -528,8 +803,6 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
- scb_s->icpua = scb_o->icpua;
-
if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_ZARCH))
new_prefix &= GUEST_PREFIX_MASK_ESA;
else
@@ -614,10 +887,328 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
prepare_ibc(vcpu, vsie_page);
rc = shadow_crycb(vcpu, vsie_page);
-out:
if (rc)
unshadow_scb(vcpu, vsie_page);
return rc;
+
+out_validity:
+ unshadow_intercept(vsie_page->scb_o, &vsie_page->scb_s);
+ return rc;
+}
+
+/* unpin the scb provided by guest 2, marking it as dirty */
+static void unpin_scb(struct kvm *kvm, struct vsie_page *vsie_page)
+{
+ hpa_t hpa;
+
+ if (!test_bit(VSIE_PAGE_SCB_PINNED, &vsie_page->flags))
+ return;
+
+ hpa = virt_to_phys(vsie_page->scb_o);
+ if (hpa)
+ unpin_guest_page(kvm, vsie_page->scb_gpa, hpa);
+ vsie_page->scb_o = NULL;
+ clear_bit(VSIE_PAGE_SCB_PINNED, &vsie_page->flags);
+}
+
+/*
+ * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
+ *
+ * Returns: - 0 if the scb was pinned.
+ * - > 0 if control has to be given to guest 2
+ */
+static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+{
+ hpa_t hpa;
+ int rc;
+
+ if (test_bit(VSIE_PAGE_SCB_PINNED, &vsie_page->flags))
+ return 0;
+
+ rc = pin_guest_page(vcpu->kvm, vsie_page->scb_gpa, &hpa);
+ if (rc) {
+ rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
+ WARN_ON_ONCE(rc);
+ return 1;
+ }
+ vsie_page->scb_o = phys_to_virt(hpa);
+ set_bit(VSIE_PAGE_SCB_PINNED, &vsie_page->flags);
+ return 0;
+}
+
+/*
+ * Unpin g2 original sca in g1 memory.
+ *
+ * Called with vsie_sca_lock held in write, except in the destroy path.
+ */
+static void unpin_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)
+{
+ if (!test_bit(VSIE_SCA_SCA_PINNED, &vsie_sca->flags))
+ return;
+
+ unpin_guest_pages(kvm, vsie_sca->sca_o_pages, vsie_sca->sca_o_nr_pages);
+ vsie_sca->sca_o_nr_pages = 0;
+
+ clear_bit(VSIE_SCA_SCA_PINNED, &vsie_sca->flags);
+}
+
+/*
+ * Pin g2 original sca in g1 memory.
+ *
+ * Called with vsie_sca_lock held in write.
+ */
+static int pin_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)
+{
+ bool is_esca = test_bit(VSIE_SCA_ESCA, &vsie_sca->flags);
+ gpa_t offset = vsie_sca->sca_gpa & ~PAGE_MASK;
+ int rc, nr_pages;
+
+ if (test_bit(VSIE_SCA_SCA_PINNED, &vsie_sca->flags))
+ return 0;
+
+ if (is_esca) {
+ nr_pages = 4;
+ if (offset + sizeof(struct esca_block) > 4 * PAGE_SIZE)
+ nr_pages = 5;
+ } else {
+ nr_pages = 1;
+ if (offset + sizeof(struct bsca_block) > PAGE_SIZE)
+ nr_pages = 2;
+ }
+
+ rc = pin_guest_pages(kvm, vsie_sca->sca_gpa, nr_pages, vsie_sca->sca_o_pages,
+ &vsie_sca->sca_o_nr_pages);
+ if (rc)
+ return rc;
+ set_bit(VSIE_SCA_SCA_PINNED, &vsie_sca->flags);
+
+ return 0;
+}
+
+static int get_sca_entry_addr(struct kvm *kvm, struct vsie_sca *vsie_sca, u16 cpu_nr, gpa_t *gpa,
+ hpa_t *hpa)
+{
+ hpa_t cpu_offset, offset;
+ int pn;
+
+ /*
+ * We cannot simply access the hva since the esca_block has typically
+ * 4 pages (arch max 5 pages) that might not be continuous in g1 memory.
+ * The bsca_block may also be stretched over two pages. Only the header
+ * is guaranteed to be on the same page.
+ */
+ if (test_bit(VSIE_SCA_ESCA, &vsie_sca->flags))
+ cpu_offset = offsetof(struct esca_block, cpu[cpu_nr]);
+ else
+ cpu_offset = offsetof(struct bsca_block, cpu[cpu_nr]);
+ pn = ((vsie_sca->sca_gpa & ~PAGE_MASK) + cpu_offset) >> PAGE_SHIFT;
+ offset = (vsie_sca->sca_gpa + cpu_offset) & ~PAGE_MASK;
+ if (WARN_ON_ONCE(pn >= vsie_sca->sca_o_nr_pages))
+ return -EINVAL;
+
+ if (gpa)
+ *gpa = vsie_sca->sca_o_pages[pn].gpa | offset;
+ if (hpa)
+ *hpa = vsie_sca->sca_o_pages[pn].hpa | offset;
+ return 0;
+}
+
+static void put_vsie_sca(struct vsie_sca *vsie_sca)
+{
+ if (!vsie_sca)
+ return;
+
+ WARN_ON_ONCE(atomic_dec_return(&vsie_sca->ref_count) < 0);
+}
+
+/*
+ * Try to find a matching vsie_sca with the correct sca format.
+ * @sca_o_gpa: original system control area address; guest-2 physical
+ * @uses_esca: whether the guest SCB has ECB2_ESCA set
+ *
+ * Called with lock on vsie_sca_lock.
+ */
+static struct vsie_sca *get_vsie_sca_existing(struct kvm *kvm, gpa_t sca_o_gpa, bool uses_esca)
+{
+ struct vsie_sca *vsie_sca = xa_load(&kvm->arch.vsie.osca_to_sca,
+ sca_o_gpa >> SCA_ALIGNMENT_SHIFT);
+
+ if (!vsie_sca)
+ return NULL;
+ if (uses_esca != test_bit(VSIE_SCA_ESCA, &vsie_sca->flags))
+ return NULL;
+ WARN_ON_ONCE(atomic_inc_return(&vsie_sca->ref_count) < 1);
+ return vsie_sca;
+}
+
+/* Try to find and get a currently unused vsie_sca from the vsie struct. */
+static struct vsie_sca *get_vsie_sca_unused(struct kvm *kvm)
+{
+ struct vsie_sca *vsie_sca;
+ int i, ref_count;
+
+ for (i = 0; i < kvm->arch.vsie.sca_count; i++) {
+ vsie_sca = READ_ONCE(kvm->arch.vsie.scas[kvm->arch.vsie.sca_next]);
+ kvm->arch.vsie.sca_next++;
+ kvm->arch.vsie.sca_next %= kvm->arch.vsie.sca_count;
+ ref_count = atomic_inc_return(&vsie_sca->ref_count);
+ WARN_ON_ONCE(ref_count < 1);
+ if (ref_count == 1)
+ return vsie_sca;
+ put_vsie_sca(vsie_sca);
+ }
+ return ERR_PTR(-EAGAIN);
+}
+
+static void free_vsie_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)
+{
+ free_pages_exact(vsie_sca, sizeof(*vsie_sca));
+}
+
+static struct vsie_sca *alloc_vsie_sca(void)
+{
+ struct vsie_sca *vsie_sca;
+
+ vsie_sca = alloc_pages_exact(sizeof(*vsie_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO);
+ if (!vsie_sca)
+ return NULL;
+
+ mutex_init(&vsie_sca->mutex);
+ atomic_set(&vsie_sca->ref_count, 0);
+
+ return vsie_sca;
+}
+
+/* Clear the vsie_sca struct but keep the vsie_page references, mutex and ref_count */
+static void clear_vsie_sca(struct vsie_sca *vsie_sca)
+{
+ memset(&vsie_sca->head, 0, sizeof(vsie_sca->head));
+ memset(&vsie_sca->tail, 0, sizeof(vsie_sca->tail));
+}
+
+/* Pin and get an existing or new guest-3 system control area.*/
+static int get_vsie_sca(struct kvm_vcpu *vcpu, struct kvm_s390_sie_block *scb_o,
+ struct vsie_sca **vsie_sca_out)
+{
+ struct vsie_sca *vsie_sca, *vsie_sca_new = NULL;
+ gpa_t sca_gpa = read_scao(vcpu->kvm, scb_o);
+ bool is_esca = sie_uses_esca(scb_o);
+ struct vsie_page *vsie_page_n;
+ struct kvm *kvm = vcpu->kvm;
+ unsigned int max_vsie_sca;
+ int rc, cpu_nr;
+
+ /* validate scb_o as we do not unshadow on error here */
+ rc = validate_scao(vcpu, scb_o, sca_gpa);
+ if (rc)
+ return rc;
+
+ down_read(&kvm->arch.vsie.vsie_sca_lock);
+ vsie_sca = get_vsie_sca_existing(kvm, sca_gpa, is_esca);
+ up_read(&kvm->arch.vsie.vsie_sca_lock);
+ if (vsie_sca) {
+ *vsie_sca_out = vsie_sca;
+ return 0;
+ }
+
+ /*
+ * Allocate new vsie_sca, it will likely be needed below.
+ * We want at least #online_vcpus shadows, so every VCPU can execute the
+ * VSIE in parallel. (Worst case all single core VMs.)
+ */
+ max_vsie_sca = min_t(unsigned int, max_t(unsigned int, atomic_read(&kvm->online_vcpus),
+ vsie_shadow_sca_max), KVM_S390_MAX_VSIE_VCPUS);
+
+ if (kvm->arch.vsie.sca_count < max_vsie_sca) {
+ vsie_sca_new = alloc_vsie_sca();
+ if (!vsie_sca_new)
+ return -ENOMEM;
+ }
+
+ /*
+ * Now we're taking the vsie_sca_lock in write mode so that we can manipulate
+ * the xarray and arch.vise.scas, etc.
+ *
+ * In the next lines we try three things to get an SCA:
+ * - Retry getting an existing vsie_sca
+ * - Using our newly allocated vsie_sca if we're under the limit
+ * - Reusing an vsie_sca including ssca to shadow a different osca
+ */
+ down_write(&kvm->arch.vsie.vsie_sca_lock);
+ vsie_sca = xa_load(&kvm->arch.vsie.osca_to_sca, sca_gpa >> SCA_ALIGNMENT_SHIFT);
+ if (vsie_sca) {
+ WARN_ON_ONCE(atomic_inc_return(&vsie_sca->ref_count) < 1);
+ if (is_esca == test_bit(VSIE_SCA_ESCA, &vsie_sca->flags))
+ goto out;
+ /* found vsie_sca with matching sca_gpa but wrong format */
+ put_vsie_sca(vsie_sca);
+ xa_erase(&kvm->arch.vsie.osca_to_sca, sca_gpa >> SCA_ALIGNMENT_SHIFT);
+ }
+
+ /* check again under write lock if we are still under our vsie_sca limit */
+ if (vsie_sca_new && kvm->arch.vsie.sca_count < max_vsie_sca) {
+ /* make use of vsie_sca just created */
+ vsie_sca = vsie_sca_new;
+ vsie_sca_new = NULL;
+
+ kvm->arch.vsie.scas[kvm->arch.vsie.sca_count] = vsie_sca;
+ kvm->arch.vsie.sca_count++;
+ atomic_set(&vsie_sca->ref_count, 1);
+ kvm->stat.vsie_shadow_sca++;
+ } else {
+ /* reuse previously created vsie_sca allocation for different osca */
+ vsie_sca = get_vsie_sca_unused(kvm);
+ /* with nr_vcpus scas one must be reusable */
+ if (IS_ERR(vsie_sca))
+ goto out;
+
+ /* unused vsie_sca exclusive under vsie_sca_lock write lock */
+ xa_erase(&kvm->arch.vsie.osca_to_sca, vsie_sca->sca_gpa >> SCA_ALIGNMENT_SHIFT);
+ for (cpu_nr = 0; cpu_nr < KVM_S390_MAX_VSIE_VCPUS; cpu_nr++) {
+ vsie_page_n = vsie_sca->pages[cpu_nr];
+ if (!vsie_page_n)
+ continue;
+
+ /* unpin but keep the vsie_page for reuse */
+ unpin_scb(kvm, vsie_page_n);
+ release_gmap_shadow_safe(kvm, vsie_page_n);
+ memset(vsie_page_n, 0, sizeof(struct vsie_page));
+ vsie_page_n->scb_gpa = ULONG_MAX;
+ }
+ unpin_sca(kvm, vsie_sca);
+ clear_vsie_sca(vsie_sca);
+ kvm->stat.vsie_shadow_sca_reuse++;
+ }
+
+ if (sie_uses_esca(scb_o))
+ set_bit(VSIE_SCA_ESCA, &vsie_sca->flags);
+ vsie_sca->sca_gpa = sca_gpa;
+
+ /*
+ * The pinned original sca will only be unpinned lazily to limit the
+ * required amount of pins/unpins on each vsie entry/exit.
+ * The unpin is done in the reuse vsie_sca allocation path above and
+ * kvm_s390_vsie_destroy().
+ */
+ rc = pin_sca(kvm, vsie_sca);
+ if (rc) {
+ vsie_sca->sca_gpa = ULONG_MAX;
+ put_vsie_sca(vsie_sca);
+ goto out;
+ }
+
+ rc = xa_insert(&kvm->arch.vsie.osca_to_sca, vsie_sca->sca_gpa >> SCA_ALIGNMENT_SHIFT,
+ vsie_sca, GFP_KERNEL_ACCOUNT);
+ if (rc == -EBUSY)
+ rc = 1;
+
+out:
+ up_write(&kvm->arch.vsie.vsie_sca_lock);
+ if (vsie_sca_new)
+ free_vsie_sca(kvm, vsie_sca_new);
+ if (vsie_sca)
+ *vsie_sca_out = vsie_sca;
+ return rc;
}
void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, gpa_t start, gpa_t end)
@@ -680,44 +1271,18 @@ static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct
return rc;
}
-/*
- * Pin the guest page given by gpa and set hpa to the pinned host address.
- * Will always be pinned writable.
- *
- * Returns: - 0 on success
- * - -EINVAL if the gpa is not valid guest storage
- */
-static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
-{
- struct page *page;
-
- page = gfn_to_page(kvm, gpa_to_gfn(gpa));
- if (!page)
- return -EINVAL;
- *hpa = (hpa_t)page_to_phys(page) + (gpa & ~PAGE_MASK);
- return 0;
-}
-
-/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
-static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
-{
- kvm_release_page_dirty(pfn_to_page(phys_to_pfn(hpa)));
- /* mark the page always as dirty for migration */
- mark_page_dirty(kvm, gpa_to_gfn(gpa));
-}
-
/* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
{
struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
hpa_t hpa;
- hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
- if (hpa) {
- unpin_guest_page(vcpu->kvm, vsie_page->sca_gpa, hpa);
- vsie_page->sca_gpa = 0;
- scb_s->scaol = 0;
- scb_s->scaoh = 0;
+ if (!vsie_page->vsie_sca) {
+ hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
+ if (hpa) {
+ unpin_guest_page(vcpu->kvm, vsie_page->sca_gpa, hpa);
+ write_scao(scb_s, 0);
+ }
}
hpa = scb_s->itdba;
@@ -756,9 +1321,6 @@ static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
* This works as long as the data lies in one page. If blocks ever exceed one
* page, we have to fall back to shadowing.
*
- * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
- * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
- *
* Returns: - 0 if all blocks were pinned.
* - > 0 if control has to be given to guest 2
* - -ENOMEM if out of memory
@@ -771,27 +1333,17 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
gpa_t gpa;
int rc = 0;
- gpa = READ_ONCE(scb_o->scaol) & ~0xfUL;
- if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
- gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32;
- if (gpa) {
- if (gpa < 2 * PAGE_SIZE)
- rc = set_validity_icpt(scb_s, 0x0038U);
- else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
- rc = set_validity_icpt(scb_s, 0x0011U);
- else if ((gpa & PAGE_MASK) !=
- ((gpa + offsetof(struct bsca_block, cpu[0]) - 1) & PAGE_MASK))
- rc = set_validity_icpt(scb_s, 0x003bU);
- if (!rc) {
- rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
- if (rc)
- rc = set_validity_icpt(scb_s, 0x0034U);
- }
+ gpa = vsie_page->sca_gpa;
+ if (gpa && !vsie_page->vsie_sca) {
+ rc = validate_scao(vcpu, scb_s, gpa);
if (rc)
goto unpin;
- vsie_page->sca_gpa = gpa;
- scb_s->scaoh = (u32)((u64)hpa >> 32);
- scb_s->scaol = (u32)(u64)hpa;
+ rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
+ if (rc) {
+ rc = set_validity_icpt(scb_s, 0x0034U);
+ goto unpin;
+ }
+ write_scao(scb_s, hpa);
}
gpa = READ_ONCE(scb_o->itdba) & ~0xffUL;
@@ -866,51 +1418,18 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
/* Due to alignment rules (checked above) this cannot
* cross page boundaries
*/
- rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
- if (rc) {
- rc = set_validity_icpt(scb_s, 0x10b0U);
- goto unpin;
- }
- vsie_page->sdnx_gpa = gpa;
- scb_s->sdnxo = hpa | sdnxc;
- }
- return 0;
-unpin:
- unpin_blocks(vcpu, vsie_page);
- return rc;
-}
-
-/* unpin the scb provided by guest 2, marking it as dirty */
-static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
- gpa_t gpa)
-{
- hpa_t hpa = virt_to_phys(vsie_page->scb_o);
-
- if (hpa)
- unpin_guest_page(vcpu->kvm, gpa, hpa);
- vsie_page->scb_o = NULL;
-}
-
-/*
- * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
- *
- * Returns: - 0 if the scb was pinned.
- * - > 0 if control has to be given to guest 2
- */
-static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
- gpa_t gpa)
-{
- hpa_t hpa;
- int rc;
-
- rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
- if (rc) {
- rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
- WARN_ON_ONCE(rc);
- return 1;
+ rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
+ if (rc) {
+ rc = set_validity_icpt(scb_s, 0x10b0U);
+ goto unpin;
+ }
+ vsie_page->sdnx_gpa = gpa;
+ scb_s->sdnxo = hpa | sdnxc;
}
- vsie_page->scb_o = phys_to_virt(hpa);
return 0;
+unpin:
+ unpin_blocks(vcpu, vsie_page);
+ return rc;
}
/*
@@ -1281,71 +1800,6 @@ static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struc
return rc;
}
-static void release_gmap_shadow(struct vsie_page *vsie_page)
-{
- struct gmap *gmap = vsie_page->gmap_cache.gmap;
-
- lockdep_assert_held(&gmap->kvm->arch.gmap->children_lock);
-
- list_del(&vsie_page->gmap_cache.list);
- vsie_page->gmap_cache.gmap = NULL;
- prefix_unmapped(vsie_page);
-
- if (list_empty(&gmap->scb_users)) {
- gmap_remove_child(gmap);
- gmap_put(gmap);
- }
-}
-
-static struct gmap *acquire_gmap_shadow(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
-{
- union ctlreg0 cr0;
- struct gmap *gmap;
- union asce asce;
- int edat;
-
- asce.val = vcpu->arch.sie_block->gcr[1];
- cr0.val = vcpu->arch.sie_block->gcr[0];
- edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
- edat += edat && test_kvm_facility(vcpu->kvm, 78);
-
- scoped_guard(spinlock, &vcpu->kvm->arch.gmap->children_lock) {
- gmap = vsie_page->gmap_cache.gmap;
- if (gmap) {
- /*
- * ASCE or EDAT could have changed since last icpt, or the gmap
- * we're holding has been unshadowed. If the gmap is still valid,
- * we can safely reuse it.
- */
- if (gmap_is_shadow_valid(gmap, asce, edat)) {
- vcpu->kvm->stat.gmap_shadow_reuse++;
- gmap_get(gmap);
- return gmap;
- }
- /* release the old shadow and mark the prefix as unmapped */
- release_gmap_shadow(vsie_page);
- }
- }
-again:
- gmap = gmap_create_shadow(vcpu->arch.mc, vcpu->kvm->arch.gmap, asce, edat);
- if (IS_ERR(gmap))
- return gmap;
- scoped_guard(spinlock, &vcpu->kvm->arch.gmap->children_lock) {
- /* unlikely race condition, remove the previous shadow */
- if (vsie_page->gmap_cache.gmap)
- release_gmap_shadow(vsie_page);
- if (!gmap->parent) {
- gmap_put(gmap);
- goto again;
- }
- vcpu->kvm->stat.gmap_shadow_create++;
- list_add(&vsie_page->gmap_cache.list, &gmap->scb_users);
- vsie_page->gmap_cache.gmap = gmap;
- prefix_unmapped(vsie_page);
- }
- return gmap;
-}
-
/*
* Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
*/
@@ -1429,7 +1883,7 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
*/
if (kvm_s390_vcpu_has_irq(vcpu, 0) ||
kvm_s390_vcpu_sie_inhibited(vcpu)) {
- kvm_s390_rewind_psw(vcpu, 4);
+ rc = -EAGAIN;
break;
}
if (sg)
@@ -1472,93 +1926,369 @@ static void put_vsie_page(struct vsie_page *vsie_page)
clear_bit(VSIE_PAGE_IN_USE, &vsie_page->flags);
}
+static void free_vsie_page(struct vsie_page *vsie_page)
+{
+ free_page((unsigned long)vsie_page);
+}
+
+static struct vsie_page *alloc_vsie_page(struct kvm *kvm)
+{
+ struct vsie_page *vsie_page;
+
+ vsie_page = (struct vsie_page *)__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA);
+ if (!vsie_page)
+ return vsie_page;
+
+ /* Mark it as invalid until it resides in the tree. */
+ vsie_page->scb_gpa = ULONG_MAX;
+ return vsie_page;
+}
+
+static int init_vsie_page(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, unsigned long scb_gpa)
+{
+ int rc;
+
+ vsie_page->scb_gpa = scb_gpa;
+ rc = pin_scb(vcpu, vsie_page);
+ if (rc) {
+ vsie_page->scb_gpa = ULONG_MAX;
+ return rc;
+ }
+
+ vsie_page->sca_gpa = read_scao(vcpu->kvm, vsie_page->scb_o);
+
+ return 0;
+}
+
+/* Reset shadow state after a vsie_page has been (re)initialised for a new SCB. */
+static void reset_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
+{
+ memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
+ release_gmap_shadow_safe(kvm, vsie_page);
+ prefix_unmapped(vsie_page);
+ vsie_page->fault_addr = 0;
+ vsie_page->scb_s.ihcpu = 0xffffU;
+}
+
/*
* Get or create a vsie page for a scb address.
*
- * Returns: - address of a vsie page (cached or new one)
- * - NULL if the same scb address is already used by another VCPU
- * - ERR_PTR(-ENOMEM) if out of memory
+ * Original control blocks are pinned when the vsie_page pointing to them is
+ * returned.
+ * Newly created vsie_pages only have vsie_page->scb_gpa and vsie_page->sca_gpa
+ * set.
+ *
+ * Returns: - -EBUSY if the same scb address is already used by another VCPU
+ * - -ENOMEM if out of memory
*/
-static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
+static int get_vsie_page(struct kvm_vcpu *vcpu, unsigned long addr,
+ struct vsie_page **vsie_page_out)
{
- struct vsie_page *vsie_page;
- int nr_vcpus;
-
- rcu_read_lock();
- vsie_page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> SCB_ALIGNMENT_SHIFT);
- rcu_read_unlock();
- if (vsie_page) {
- if (try_get_vsie_page(vsie_page)) {
- if (vsie_page->scb_gpa == addr)
- return vsie_page;
- /*
- * We raced with someone reusing + putting this vsie
- * page before we grabbed it.
- */
- put_vsie_page(vsie_page);
+ struct vsie_page *vsie_page, *vsie_page_new = NULL;
+ struct kvm *kvm = vcpu->kvm;
+ unsigned int max_vsie_page;
+ int rc, pages_idx;
+
+ vsie_page = xa_load(&kvm->arch.vsie.addr_to_page, addr >> SCB_ALIGNMENT_SHIFT);
+ if (vsie_page && try_get_vsie_page(vsie_page)) {
+ if (vsie_page->scb_gpa == addr) {
+ *vsie_page_out = vsie_page;
+ return 0;
}
+ /*
+ * We raced with someone reusing + putting this vsie
+ * page before we grabbed it.
+ */
+ put_vsie_page(vsie_page);
}
- /*
- * We want at least #online_vcpus shadows, so every VCPU can execute
- * the VSIE in parallel.
- */
- nr_vcpus = atomic_read(&kvm->online_vcpus);
+ max_vsie_page = min_t(unsigned int, max_t(unsigned int, atomic_read(&kvm->online_vcpus),
+ vsie_shadow_scb_max), KVM_S390_MAX_VSIE_VCPUS);
+
+ /* allocate new vsie_page - we will likely need it */
+ if (kvm->arch.vsie.page_count < max_vsie_page) {
+ vsie_page_new = alloc_vsie_page(kvm);
+ if (!vsie_page_new)
+ return -ENOMEM;
+ __set_bit(VSIE_PAGE_IN_USE, &vsie_page_new->flags);
+ }
mutex_lock(&kvm->arch.vsie.mutex);
- if (kvm->arch.vsie.page_count < nr_vcpus) {
- vsie_page = (void *)__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA);
- if (!vsie_page) {
+ vsie_page = xa_load(&kvm->arch.vsie.addr_to_page, addr >> SCB_ALIGNMENT_SHIFT);
+ if (vsie_page && try_get_vsie_page(vsie_page)) {
+ if (vsie_page->scb_gpa == addr) {
mutex_unlock(&kvm->arch.vsie.mutex);
- return ERR_PTR(-ENOMEM);
+ if (vsie_page_new)
+ free_vsie_page(vsie_page_new);
+ *vsie_page_out = vsie_page;
+ return 0;
}
- __set_bit(VSIE_PAGE_IN_USE, &vsie_page->flags);
- kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = vsie_page;
+ /*
+ * We raced with someone reusing + putting this vsie
+ * page before we grabbed it.
+ */
+ put_vsie_page(vsie_page);
+ }
+
+ if (kvm->arch.vsie.page_count < max_vsie_page) {
+ pages_idx = kvm->arch.vsie.page_count;
+ vsie_page = vsie_page_new;
+ vsie_page_new = NULL;
+ WRITE_ONCE(kvm->arch.vsie.pages[kvm->arch.vsie.page_count], vsie_page);
kvm->arch.vsie.page_count++;
+ kvm->stat.vsie_shadow_scb++;
} else {
/* reuse an existing entry that belongs to nobody */
while (true) {
- vsie_page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
+ pages_idx = kvm->arch.vsie.next;
+ kvm->arch.vsie.next++;
+ kvm->arch.vsie.next %= kvm->arch.vsie.page_count;
+ vsie_page = kvm->arch.vsie.pages[pages_idx];
if (try_get_vsie_page(vsie_page))
break;
- kvm->arch.vsie.next++;
- kvm->arch.vsie.next %= nr_vcpus;
}
if (vsie_page->scb_gpa != ULONG_MAX)
- radix_tree_delete(&kvm->arch.vsie.addr_to_page,
- vsie_page->scb_gpa >> SCB_ALIGNMENT_SHIFT);
+ xa_erase(&kvm->arch.vsie.addr_to_page,
+ vsie_page->scb_gpa >> SCB_ALIGNMENT_SHIFT);
+ /* Mark it as invalid until it resides in the tree. */
+ vsie_page->scb_gpa = ULONG_MAX;
+
+ unpin_scb(kvm, vsie_page);
+ kvm->stat.vsie_shadow_scb_reuse++;
}
- /* Mark it as invalid until it resides in the tree. */
- vsie_page->scb_gpa = ULONG_MAX;
- /* Double use of the same address or allocation failure. */
- if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> SCB_ALIGNMENT_SHIFT,
- vsie_page)) {
- put_vsie_page(vsie_page);
- mutex_unlock(&kvm->arch.vsie.mutex);
- return NULL;
+ rc = init_vsie_page(vcpu, vsie_page, addr);
+ if (!rc) {
+ rc = xa_insert(&kvm->arch.vsie.addr_to_page, addr >> SCB_ALIGNMENT_SHIFT, vsie_page,
+ GFP_KERNEL_ACCOUNT);
+ if (rc == -EBUSY)
+ rc = -EAGAIN;
}
- vsie_page->scb_gpa = addr;
+
mutex_unlock(&kvm->arch.vsie.mutex);
+ if (vsie_page_new)
+ free_vsie_page(vsie_page_new);
+ if (rc) {
+ vsie_page->scb_gpa = ULONG_MAX;
+ put_vsie_page(vsie_page);
+ return rc;
+ }
- memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
- if (vsie_page->gmap_cache.gmap) {
- scoped_guard(spinlock, &kvm->arch.gmap->children_lock)
- if (vsie_page->gmap_cache.gmap)
- release_gmap_shadow(vsie_page);
+ reset_vsie_page(kvm, vsie_page);
+
+ *vsie_page_out = vsie_page;
+ return 0;
+}
+
+static int get_vsie_page_cpu_nr(struct kvm_vcpu *vcpu, struct vsie_sca *vsie_sca, gpa_t scb_gpa,
+ u16 cpu_nr, struct vsie_page **vsie_page_out)
+{
+ struct vsie_page *vsie_page, *vsie_page_new = NULL;
+ bool vsie_page_is_new = true;
+ int rc;
+
+ vsie_page = vsie_sca->pages[cpu_nr];
+ if (!vsie_page) {
+ vsie_page_new = alloc_vsie_page(vcpu->kvm);
+ if (!vsie_page_new)
+ return -ENOMEM;
+ vsie_page_new->vsie_sca = vsie_sca;
+ __set_bit(VSIE_PAGE_IN_USE, &vsie_page_new->flags);
+
+ /* be careful to not loose a page here if we raced */
+ scoped_guard(mutex, &vsie_sca->mutex) {
+ vsie_page = vsie_sca->pages[cpu_nr];
+ if (!vsie_page) {
+ WRITE_ONCE(vsie_sca->pages[cpu_nr], vsie_page_new);
+ vsie_page = vsie_page_new;
+ }
+ }
}
- prefix_unmapped(vsie_page);
- vsie_page->fault_addr = 0;
- vsie_page->scb_s.ihcpu = 0xffffU;
- return vsie_page;
+ if (vsie_page != vsie_page_new) {
+ vsie_page_is_new = false;
+ if (vsie_page_new)
+ free_vsie_page(vsie_page_new);
+
+ /* not a new vsie_page so get it */
+ if (!try_get_vsie_page(vsie_page))
+ return -EAGAIN;
+ vsie_page->vsie_sca = vsie_sca;
+ }
+ if (vsie_page->scb_gpa != scb_gpa || vsie_page->sca_gpa != vsie_sca->sca_gpa) {
+ scoped_guard(mutex, &vcpu->kvm->arch.vsie.mutex) {
+ unpin_scb(vcpu->kvm, vsie_page);
+ rc = init_vsie_page(vcpu, vsie_page, scb_gpa);
+ }
+ if (vsie_page_is_new)
+ vcpu->kvm->stat.vsie_shadow_scb++;
+ else
+ vcpu->kvm->stat.vsie_shadow_scb_reuse++;
+ if (rc) {
+ put_vsie_page(vsie_page);
+ return rc;
+ }
+
+ reset_vsie_page(vcpu->kvm, vsie_page);
+ }
+
+ *vsie_page_out = vsie_page;
+ return 0;
}
-int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
+/*
+ * Copy the mcn from the osca to the vsie_sca to be able to detect mcn changes later on.
+ *
+ * @vsie_sca: vsie_sca to copy mcn to.
+ * @sca: Pointer to a struct bsca_block or struct esca_block to read from.
+ */
+static void sca_mcn_copy(struct vsie_sca *vsie_sca, void *sca)
{
- struct vsie_page *vsie_page;
- unsigned long scb_addr;
+ int offset = offsetof(struct bsca_block, mcn);
+ int size = sizeof(unsigned long);
+
+ if (test_bit(VSIE_SCA_ESCA, &vsie_sca->flags)) {
+ offset = offsetof(struct esca_block, mcn);
+ size = size * 4;
+ }
+ memcpy(&vsie_sca->mcn, sca + offset, size);
+}
+
+/*
+ * Compare the mcn from the given sca to the vsie_sca to be able to detect mcn changes.
+ *
+ * @vsie_sca: vsie_sca to compare mcn to.
+ * @sca: Pointer to a struct bsca_block or struct esca_block to compare to.
+ */
+static bool sca_mcn_equals(struct vsie_sca *vsie_sca, void *sca)
+{
+ int offset = offsetof(struct bsca_block, mcn);
+ int size = sizeof(unsigned long);
+
+ if (test_bit(VSIE_SCA_ESCA, &vsie_sca->flags)) {
+ size = size * 4;
+ offset = offsetof(struct esca_block, mcn);
+ }
+
+ return !memcmp(&vsie_sca->mcn, sca + offset, size);
+}
+
+static void update_vsie_sca(struct vsie_sca *vsie_sca, unsigned int cpu_nr,
+ struct vsie_page *vsie_page_n, hpa_t sca_o_entry_hpa)
+{
+ guard(mutex)(&vsie_sca->mutex);
+
+ WRITE_ONCE(vsie_sca->ssca.cpu[cpu_nr].ssda, virt_to_phys(&vsie_page_n->scb_s));
+ WRITE_ONCE(vsie_sca->ssca.cpu[cpu_nr].ossea, sca_o_entry_hpa);
+ WRITE_ONCE(vsie_sca->pages[cpu_nr], vsie_page_n);
+}
+
+static int _shadow_sca_cpu(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
+ struct vsie_sca *vsie_sca, hpa_t sca_o_entry_hpa,
+ unsigned int cpu_nr, bool is_esca)
+{
+ hva_t sca_o_entry_hva = (hva_t)phys_to_virt(sca_o_entry_hpa);
+ struct vsie_page *vsie_page_n;
+ gpa_t scb_o_gpa;
+ int rc;
+
+ if (is_esca)
+ scb_o_gpa = ((struct esca_entry *)sca_o_entry_hva)->sda;
+ else
+ scb_o_gpa = ((struct bsca_entry *)sca_o_entry_hva)->sda;
+ if (scb_o_gpa & 0x1ffUL)
+ return set_validity_icpt(vsie_page->scb_o, 0x0001U);
+
+ rc = get_vsie_page_cpu_nr(vcpu, vsie_sca, scb_o_gpa, cpu_nr, &vsie_page_n);
+ if (rc)
+ return rc;
+
+ rc = shadow_scb_minimal(vcpu, vsie_page_n);
+ update_vsie_sca(vsie_sca, cpu_nr, vsie_page_n, sca_o_entry_hpa);
+ if (rc) {
+ /* copy intercept to primary scb_o, no unshadow_scb() on exit */
+ unshadow_intercept(vsie_page->scb_o, &vsie_page_n->scb_s);
+ rc = 1;
+ }
+ put_vsie_page(vsie_page_n);
+
+ return rc;
+}
+
+/* Fill the shadow system control area used for VSIE SIGPI. */
+static int _shadow_sca(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
+ struct vsie_sca *vsie_sca)
+{
+ bool is_esca = sie_uses_esca(vsie_page->scb_o);
+ unsigned int cpu_nr, cpu_slots;
+ hpa_t sca_o_entry_hpa;
int rc;
+ if (is_esca)
+ set_bit(VSIE_SCA_ESCA, &vsie_sca->flags);
+ else
+ clear_bit(VSIE_SCA_ESCA, &vsie_sca->flags);
+ sca_mcn_copy(vsie_sca, phys_to_virt(sca_o_hpa(vsie_sca)));
+
+ /* pin and make minimal shadow for ALL scb in the sca */
+ cpu_slots = is_esca ? KVM_S390_MAX_VSIE_VCPUS : KVM_S390_BSCA_CPU_SLOTS;
+ for_each_set_bit_inv(cpu_nr, (unsigned long *)&vsie_sca->mcn, cpu_slots) {
+ rc = get_sca_entry_addr(vcpu->kvm, vsie_sca, cpu_nr, NULL, &sca_o_entry_hpa);
+ if (rc)
+ break;
+
+ if ((vsie_page->scb_o->icpua & KVM_S390_CPU_MASK) == cpu_nr) {
+ update_vsie_sca(vsie_sca, cpu_nr, vsie_page, sca_o_entry_hpa);
+ continue;
+ }
+
+ rc = _shadow_sca_cpu(vcpu, vsie_page, vsie_sca, sca_o_entry_hpa, cpu_nr, is_esca);
+ if (rc)
+ break;
+ }
+
+ if (rc) {
+ vsie_sca->ssca.osca = 0;
+ for_each_set_bit_inv(cpu_nr, (unsigned long *)&vsie_sca->mcn, cpu_slots) {
+ vsie_sca->ssca.cpu[cpu_nr].ssda = 0;
+ vsie_sca->ssca.cpu[cpu_nr].ossea = 0;
+ }
+ } else {
+ vsie_sca->ssca.osca = sca_o_hpa(vsie_sca);
+ }
+ return rc;
+}
+
+static bool shadow_sca_needed(struct vsie_sca *vsie_sca)
+{
+ if (!vsie_sca->ssca.osca)
+ return true;
+ return !sca_mcn_equals(vsie_sca, phys_to_virt(sca_o_hpa(vsie_sca)));
+}
+
+/* Shadow or reshadow the SCA on VSIE enter. */
+static int shadow_sca(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct vsie_sca *vsie_sca)
+{
+ scoped_guard(rwsem_read, &vcpu->kvm->arch.vsie.vsie_sca_lock) {
+ if (!shadow_sca_needed(vsie_sca))
+ return 0;
+ }
+
+ guard(rwsem_write)(&vcpu->kvm->arch.vsie.vsie_sca_lock);
+ if (!shadow_sca_needed(vsie_sca))
+ return 0;
+
+ return _shadow_sca(vcpu, vsie_page, vsie_sca);
+}
+
+int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
+{
+ struct vsie_page *vsie_page = NULL;
+ struct vsie_sca *vsie_sca = NULL;
+ struct kvm_s390_sie_block *scb_o;
+ gpa_t scb_addr;
+ hpa_t scb_hpa;
+ int rc = 0;
+
vcpu->stat.instruction_sie++;
if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
return -EOPNOTSUPP;
@@ -1576,35 +2306,60 @@ int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
return 0;
}
- vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
- if (IS_ERR(vsie_page)) {
- return PTR_ERR(vsie_page);
- } else if (!vsie_page) {
- /* double use of sie control block - simply do nothing */
- kvm_s390_rewind_psw(vcpu, 4);
- return 0;
+ rc = pin_guest_page(vcpu->kvm, scb_addr, &scb_hpa);
+ if (rc)
+ return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
+ scb_o = (struct kvm_s390_sie_block *)phys_to_virt(scb_hpa);
+
+ if (!use_ssca(vcpu->kvm, scb_o)) {
+ /* get the vsie_page with pinned scb_o */
+ rc = get_vsie_page(vcpu, scb_addr, &vsie_page);
+ if (rc)
+ goto out_unpin;
+ vsie_page->vsie_sca = NULL;
+ } else {
+ /* get the vsie_sca with pinned original sca */
+ rc = get_vsie_sca(vcpu, scb_o, &vsie_sca);
+ if (rc)
+ goto out_unpin;
+ rc = get_vsie_page_cpu_nr(vcpu, vsie_sca, scb_addr,
+ scb_o->icpua & KVM_S390_CPU_MASK, &vsie_page);
+ if (rc)
+ goto out_put_sca;
}
- rc = pin_scb(vcpu, vsie_page, scb_addr);
- if (rc)
- goto out_put;
rc = shadow_scb(vcpu, vsie_page);
if (rc)
- goto out_unpin_scb;
+ goto out_put;
+ if (vsie_sca) {
+ /* pin and shadow the sca including all scb_o in the g3 conf */
+ rc = shadow_sca(vcpu, vsie_page, vsie_sca);
+ if (rc)
+ goto out_put;
+ }
+
rc = pin_blocks(vcpu, vsie_page);
if (rc)
goto out_unshadow;
register_shadow_scb(vcpu, vsie_page);
+
rc = vsie_run(vcpu, vsie_page);
+
unregister_shadow_scb(vcpu);
unpin_blocks(vcpu, vsie_page);
out_unshadow:
unshadow_scb(vcpu, vsie_page);
-out_unpin_scb:
- unpin_scb(vcpu, vsie_page, scb_addr);
out_put:
put_vsie_page(vsie_page);
+out_put_sca:
+ put_vsie_sca(vsie_sca);
+out_unpin:
+ unpin_guest_page(vcpu->kvm, scb_addr, scb_hpa);
+ if (rc == -EAGAIN) {
+ kvm_s390_rewind_psw(vcpu, 4);
+ rc = 0;
+ }
return rc < 0 ? rc : 0;
}
@@ -1612,30 +2367,55 @@ int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
void kvm_s390_vsie_init(struct kvm *kvm)
{
mutex_init(&kvm->arch.vsie.mutex);
- INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL_ACCOUNT);
+ xa_init_flags(&kvm->arch.vsie.addr_to_page, XA_FLAGS_ACCOUNT);
+ init_rwsem(&kvm->arch.vsie.vsie_sca_lock);
+ xa_init_flags(&kvm->arch.vsie.osca_to_sca, XA_FLAGS_ACCOUNT);
+}
+
+static void kvm_s390_vsie_destroy_page(struct kvm *kvm, struct vsie_page *vsie_page)
+{
+ unpin_scb(kvm, vsie_page);
+ release_gmap_shadow_safe(kvm, vsie_page);
+ free_vsie_page(vsie_page);
}
/* Destroy the vsie data structures. To be called when a vm is destroyed. */
void kvm_s390_vsie_destroy(struct kvm *kvm)
{
struct vsie_page *vsie_page;
- int i;
+ struct vsie_sca *vsie_sca;
+ int i, cpu_nr;
+
+ guard(mutex)(&kvm->arch.vsie.mutex);
- mutex_lock(&kvm->arch.vsie.mutex);
for (i = 0; i < kvm->arch.vsie.page_count; i++) {
vsie_page = kvm->arch.vsie.pages[i];
- scoped_guard(spinlock, &kvm->arch.gmap->children_lock)
- if (vsie_page->gmap_cache.gmap)
- release_gmap_shadow(vsie_page);
kvm->arch.vsie.pages[i] = NULL;
- /* free the radix tree entry */
- if (vsie_page->scb_gpa != ULONG_MAX)
- radix_tree_delete(&kvm->arch.vsie.addr_to_page,
- vsie_page->scb_gpa >> SCB_ALIGNMENT_SHIFT);
- free_page((unsigned long)vsie_page);
+ kvm_s390_vsie_destroy_page(kvm, vsie_page);
}
+
kvm->arch.vsie.page_count = 0;
- mutex_unlock(&kvm->arch.vsie.mutex);
+ for (i = 0; i < kvm->arch.vsie.sca_count; i++) {
+ vsie_sca = kvm->arch.vsie.scas[i];
+ kvm->arch.vsie.scas[i] = NULL;
+ if (!vsie_sca)
+ continue;
+
+ for (cpu_nr = 0; cpu_nr < KVM_S390_MAX_VSIE_VCPUS; cpu_nr++) {
+ vsie_page = vsie_sca->pages[cpu_nr];
+ vsie_sca->pages[cpu_nr] = NULL;
+ if (!vsie_page)
+ continue;
+ unpin_scb(kvm, vsie_page);
+ kvm_s390_vsie_destroy_page(kvm, vsie_page);
+ }
+
+ unpin_sca(kvm, vsie_sca);
+ free_vsie_sca(kvm, vsie_sca);
+ }
+ kvm->arch.vsie.sca_count = 0;
+ xa_destroy(&kvm->arch.vsie.addr_to_page);
+ xa_destroy(&kvm->arch.vsie.osca_to_sca);
}
void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
diff --git a/drivers/s390/char/sclp_early.c b/drivers/s390/char/sclp_early.c
index 22dd797e62291..40e6e346ee083 100644
--- a/drivers/s390/char/sclp_early.c
+++ b/drivers/s390/char/sclp_early.c
@@ -56,6 +56,7 @@ static void __init sclp_early_facilities_detect(void)
sclp.has_diag318 = !!(sccb->byte_134 & 0x80);
sclp.has_diag320 = !!(sccb->byte_134 & 0x04);
sclp.has_iplcc = !!(sccb->byte_134 & 0x02);
+ sclp.has_vsie_interp_extf = !!(sccb->byte_134 & 0x01);
}
if (sccb->cpuoff > 137) {
sclp.has_sipl = !!(sccb->cbl & 0x4000);
diff --git a/tools/testing/selftests/kvm/include/s390/sie.h b/tools/testing/selftests/kvm/include/s390/sie.h
index 160acd4a1db92..4ff1c1a354af5 100644
--- a/tools/testing/selftests/kvm/include/s390/sie.h
+++ b/tools/testing/selftests/kvm/include/s390/sie.h
@@ -223,7 +223,7 @@ struct kvm_s390_sie_block {
__u32 fac; /* 0x01a0 */
__u8 reserved1a4[20]; /* 0x01a4 */
__u64 cbrlo; /* 0x01b8 */
- __u8 reserved1c0[8]; /* 0x01c0 */
+ __u64 osda; /* 0x01c0 */
#define ECD_HOSTREGMGMT 0x20000000
#define ECD_MEF 0x08000000
#define ECD_ETOKENF 0x02000000
|
| 3/2 | 2026/08/27 16:16 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 26877 output: 35 thoughts: 372 Thoughts: **My Analysis of this s390 KVM Patch** Okay, so this patch is clearly digging into the s390 KVM nested virtualization (vSIE) code – specifically, the part that handles "VSIE SIGPI and SRSI using the SSCA." That means a new `struct vsie_sca` and SCA shadowing. This is potentially interesting, so let's dig a little deeper. It's focused in `arch/s390/kvm/s390/vsie.c` and related headers. Functionally, this is a modification to the KVM subsystem for the s390 architecture. It seems this is reachable through KVM ioctls, which implies interaction with a nested guest running on s390. This adds a decent amount of complexity in the code, which makes me think it's fuzz-worthy. Before I get too far ahead of myself, let's look at the config options. `CONFIG_KVM` is, of course, a given. I'll need an s390 architecture as well. Now, let's identify some key functions that were modified or added. `kvm_s390_handle_vsie` seems to be the main entry point for vSIE, it's what I'll focus on first. It looks like `shadow_sca`, `_shadow_sca`, `get_vsie_sca`, `get_vsie_page`, `shadow_scb`, and `pin_blocks`, etc. are all important as well. I'll want to keep an eye on these. Wait a second, *kvm_s390_handle_vsie* is the main function that intercepts the SIE instruction. That's good to know. I'll check to see if there are any other entry points. I can see that `kvm_s390_handle_vsie` is called when a guest executes the SIE instruction. So, it's definitely reachable from a guest! Time to check the Kconfig for KVM on s390. |
| 4/2 | 2026/08/27 16:16 | tool | grepper |
0mArgs:{
"Expression": "config KVM",
"PathPrefix": "arch/s390/kvm/Kconfig"
}Results: {
"Output": "arch/s390/kvm/Kconfig=17=if VIRTUALIZATION\narch/s390/kvm/Kconfig-18-\narch/s390/kvm/Kconfig:19:config KVM\narch/s390/kvm/Kconfig-20-\tdef_tristate y\n--\narch/s390/kvm/Kconfig-46-\narch/s390/kvm/Kconfig:47:config KVM_S390_UCONTROL\narch/s390/kvm/Kconfig-48-\tbool \"Userspace controlled virtual machines\"\n"
} |
| 5/2 | 2026/08/27 16:16 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 27901 output: 73 thoughts: 46 Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience: **Analyzing KVM on s390: Focus on vsie.c** Alright, so the big one, the core switch to follow is definitely `CONFIG_KVM`. That's where everything hinges. Now, I need to drill down and understand the recent changes, so I'm focusing on the `arch/s390/kvm/s390/vsie.c` file. This is where the magic happens for VSIE and the s390 architecture within KVM. I'm going to carefully review the functions that have been either modified or completely introduced in this area. I'm expecting to see things that handle VSIE instructions, exception handling, and probably some crucial guest state management related to virtualization. My intuition tells me that I'll probably find key code related to the performance optimizations that are always being sought in virtualization. Let's see what they changed. |
| 6/2 | 2026/08/27 16:16 | tool | grepper |
0mArgs:{
"Expression": "^(static )?(int|void|struct [a-zA-Z0-9_]+ \\*) [a-zA-Z0-9_]+\\(",
"PathPrefix": "arch/s390/kvm/s390/vsie.c"
}Results: {
"Output": "arch/s390/kvm/s390/vsie.c=130=static unsigned long read_scao(struct kvm *kvm, struct kvm_s390_sie_block *scb)\n--\narch/s390/kvm/s390/vsie.c-139-\narch/s390/kvm/s390/vsie.c:140:static void write_scao(struct kvm_s390_sie_block *scb, unsigned long hpa)\narch/s390/kvm/s390/vsie.c-141-{\n--\narch/s390/kvm/s390/vsie.c=146=static inline bool use_ssca(struct kvm *kvm, struct kvm_s390_sie_block *scb)\n--\narch/s390/kvm/s390/vsie.c-157-/* trigger a validity icpt for the given scb */\narch/s390/kvm/s390/vsie.c:158:static int set_validity_icpt(struct kvm_s390_sie_block *scb,\narch/s390/kvm/s390/vsie.c-159-\t\t\t __u16 reason_code)\n--\narch/s390/kvm/s390/vsie.c-167-/* The sca header must not cross pages etc. */\narch/s390/kvm/s390/vsie.c:168:static int validate_scao(struct kvm_vcpu *vcpu, struct kvm_s390_sie_block *scb, gpa_t gpa)\narch/s390/kvm/s390/vsie.c-169-{\n--\narch/s390/kvm/s390/vsie.c-186-/* mark the prefix as unmapped, this will block the VSIE */\narch/s390/kvm/s390/vsie.c:187:static void prefix_unmapped(struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-188-{\n--\narch/s390/kvm/s390/vsie.c-192-/* mark the prefix as unmapped and wait until the VSIE has been left */\narch/s390/kvm/s390/vsie.c:193:static void prefix_unmapped_sync(struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-194-{\n--\narch/s390/kvm/s390/vsie.c-202-/* mark the prefix as mapped, this will allow the VSIE to run */\narch/s390/kvm/s390/vsie.c:203:static void prefix_mapped(struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-204-{\n--\narch/s390/kvm/s390/vsie.c-208-/* test if the prefix is mapped into the gmap shadow */\narch/s390/kvm/s390/vsie.c:209:static int prefix_is_mapped(struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-210-{\n--\narch/s390/kvm/s390/vsie.c-213-\narch/s390/kvm/s390/vsie.c:214:static void release_gmap_shadow(struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-215-{\n--\narch/s390/kvm/s390/vsie.c-229-\narch/s390/kvm/s390/vsie.c:230:static void release_gmap_shadow_safe(struct kvm *kvm, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-231-{\n--\narch/s390/kvm/s390/vsie.c=241=static struct gmap *acquire_gmap_shadow(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\n--\narch/s390/kvm/s390/vsie.c-296- */\narch/s390/kvm/s390/vsie.c:297:static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)\narch/s390/kvm/s390/vsie.c-298-{\n--\narch/s390/kvm/s390/vsie.c-308-/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */\narch/s390/kvm/s390/vsie.c:309:static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)\narch/s390/kvm/s390/vsie.c-310-{\n--\narch/s390/kvm/s390/vsie.c-316-/* unpin multiple guest pages pinned with pin_guest_pages() */\narch/s390/kvm/s390/vsie.c:317:static void unpin_guest_pages(struct kvm *kvm, struct kvm_address_pair *addr, unsigned int nr_pages)\narch/s390/kvm/s390/vsie.c-318-{\n--\narch/s390/kvm/s390/vsie.c-330- */\narch/s390/kvm/s390/vsie.c:331:static int pin_guest_pages(struct kvm *kvm, gpa_t gpa, unsigned int nr_pages,\narch/s390/kvm/s390/vsie.c-332-\t\t\t struct kvm_address_pair *addr, unsigned int *nr_pages_pinned)\n--\narch/s390/kvm/s390/vsie.c-356-/* copy the updated intervention request bits into the shadow scb */\narch/s390/kvm/s390/vsie.c:357:static void update_intervention_requests(struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-358-{\n--\narch/s390/kvm/s390/vsie.c-367-/* shadow (filter and validate) the cpuflags */\narch/s390/kvm/s390/vsie.c:368:static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-369-{\n--\narch/s390/kvm/s390/vsie.c-405-/* Copy to APCB FORMAT1 from APCB FORMAT0 */\narch/s390/kvm/s390/vsie.c:406:static int setup_apcb10(struct kvm_vcpu *vcpu, struct kvm_s390_apcb1 *apcb_s,\narch/s390/kvm/s390/vsie.c-407-\t\t\tunsigned long crycb_gpa, struct kvm_s390_apcb1 *apcb_h)\n--\narch/s390/kvm/s390/vsie.c-435- */\narch/s390/kvm/s390/vsie.c:436:static int setup_apcb00(struct kvm_vcpu *vcpu, unsigned long *apcb_s,\narch/s390/kvm/s390/vsie.c-437-\t\t\tunsigned long crycb_gpa, unsigned long *apcb_h)\n--\narch/s390/kvm/s390/vsie.c-461- */\narch/s390/kvm/s390/vsie.c:462:static int setup_apcb11(struct kvm_vcpu *vcpu, unsigned long *apcb_s,\narch/s390/kvm/s390/vsie.c-463-\t\t\tunsigned long crycb_gpa,\n--\narch/s390/kvm/s390/vsie.c-493- */\narch/s390/kvm/s390/vsie.c:494:static int setup_apcb(struct kvm_vcpu *vcpu, struct kvm_s390_crypto_cb *crycb_s,\narch/s390/kvm/s390/vsie.c-495-\t const u32 crycb_gpa,\n--\narch/s390/kvm/s390/vsie.c-560- */\narch/s390/kvm/s390/vsie.c:561:static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-562-{\n--\narch/s390/kvm/s390/vsie.c-635-\narch/s390/kvm/s390/vsie.c:636:static void shadow_esa(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-637-{\n--\narch/s390/kvm/s390/vsie.c-647-/* shadow (round up/down) the ibc to avoid validity icpt */\narch/s390/kvm/s390/vsie.c:648:static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-649-{\n--\narch/s390/kvm/s390/vsie.c=674=static inline void unshadow_intercept(struct kvm_s390_sie_block *scb_o,\n--\narch/s390/kvm/s390/vsie.c-684-/* unshadow the scb, copying parameters back to the real scb */\narch/s390/kvm/s390/vsie.c:685:static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-686-{\n--\narch/s390/kvm/s390/vsie.c-723-\narch/s390/kvm/s390/vsie.c:724:static int shadow_scb_minimal(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-725-{\n--\narch/s390/kvm/s390/vsie.c-763- */\narch/s390/kvm/s390/vsie.c:764:static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-765-{\n--\narch/s390/kvm/s390/vsie.c-899-/* unpin the scb provided by guest 2, marking it as dirty */\narch/s390/kvm/s390/vsie.c:900:static void unpin_scb(struct kvm *kvm, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-901-{\n--\narch/s390/kvm/s390/vsie.c-919- */\narch/s390/kvm/s390/vsie.c:920:static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-921-{\n--\narch/s390/kvm/s390/vsie.c-943- */\narch/s390/kvm/s390/vsie.c:944:static void unpin_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)\narch/s390/kvm/s390/vsie.c-945-{\n--\narch/s390/kvm/s390/vsie.c-959- */\narch/s390/kvm/s390/vsie.c:960:static int pin_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)\narch/s390/kvm/s390/vsie.c-961-{\n--\narch/s390/kvm/s390/vsie.c-987-\narch/s390/kvm/s390/vsie.c:988:static int get_sca_entry_addr(struct kvm *kvm, struct vsie_sca *vsie_sca, u16 cpu_nr, gpa_t *gpa,\narch/s390/kvm/s390/vsie.c-989-\t\t\t hpa_t *hpa)\n--\narch/s390/kvm/s390/vsie.c-1015-\narch/s390/kvm/s390/vsie.c:1016:static void put_vsie_sca(struct vsie_sca *vsie_sca)\narch/s390/kvm/s390/vsie.c-1017-{\n--\narch/s390/kvm/s390/vsie.c=1045=static struct vsie_sca *get_vsie_sca_unused(struct kvm *kvm)\n--\narch/s390/kvm/s390/vsie.c-1062-\narch/s390/kvm/s390/vsie.c:1063:static void free_vsie_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)\narch/s390/kvm/s390/vsie.c-1064-{\n--\narch/s390/kvm/s390/vsie.c=1068=static struct vsie_sca *alloc_vsie_sca(void)\n--\narch/s390/kvm/s390/vsie.c-1082-/* Clear the vsie_sca struct but keep the vsie_page references, mutex and ref_count */\narch/s390/kvm/s390/vsie.c:1083:static void clear_vsie_sca(struct vsie_sca *vsie_sca)\narch/s390/kvm/s390/vsie.c-1084-{\n--\narch/s390/kvm/s390/vsie.c-1089-/* Pin and get an existing or new guest-3 system control area.*/\narch/s390/kvm/s390/vsie.c:1090:static int get_vsie_sca(struct kvm_vcpu *vcpu, struct kvm_s390_sie_block *scb_o,\narch/s390/kvm/s390/vsie.c-1091-\t\tstruct vsie_sca **vsie_sca_out)\n--\narch/s390/kvm/s390/vsie.c-1213-\narch/s390/kvm/s390/vsie.c:1214:void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, gpa_t start, gpa_t end)\narch/s390/kvm/s390/vsie.c-1215-{\n--\narch/s390/kvm/s390/vsie.c-1244- */\narch/s390/kvm/s390/vsie.c:1245:static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct gmap *sg)\narch/s390/kvm/s390/vsie.c-1246-{\n--\narch/s390/kvm/s390/vsie.c-1274-/* unpin all blocks previously pinned by pin_blocks(), marking them dirty */\narch/s390/kvm/s390/vsie.c:1275:static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-1276-{\n--\narch/s390/kvm/s390/vsie.c-1327- */\narch/s390/kvm/s390/vsie.c:1328:static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-1329-{\n--\narch/s390/kvm/s390/vsie.c-1440- */\narch/s390/kvm/s390/vsie.c:1441:static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,\narch/s390/kvm/s390/vsie.c-1442-\t\t\tbool write_flag)\n--\narch/s390/kvm/s390/vsie.c-1470- */\narch/s390/kvm/s390/vsie.c:1471:static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct gmap *sg)\narch/s390/kvm/s390/vsie.c-1472-{\n--\narch/s390/kvm/s390/vsie.c-1496- */\narch/s390/kvm/s390/vsie.c:1497:static void handle_last_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct gmap *sg)\narch/s390/kvm/s390/vsie.c-1498-{\n--\narch/s390/kvm/s390/vsie.c=1504=static inline void clear_vsie_icpt(struct vsie_page *vsie_page)\n--\narch/s390/kvm/s390/vsie.c-1509-/* rewind the psw and clear the vsie icpt, so we can retry execution */\narch/s390/kvm/s390/vsie.c:1510:static void retry_vsie_icpt(struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-1511-{\n--\narch/s390/kvm/s390/vsie.c-1524-\narch/s390/kvm/s390/vsie.c:1525:static int handle_stfle_0(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,\narch/s390/kvm/s390/vsie.c-1526-\t\t\t u32 fac_list_origin)\n--\narch/s390/kvm/s390/vsie.c-1541-\narch/s390/kvm/s390/vsie.c:1542:static int handle_stfle_2(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, u32 fac_list_origin)\narch/s390/kvm/s390/vsie.c-1543-{\n--\narch/s390/kvm/s390/vsie.c-1570- */\narch/s390/kvm/s390/vsie.c:1571:static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-1572-{\n--\narch/s390/kvm/s390/vsie.c=1612=static u64 vsie_get_register(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, u8 reg)\n--\narch/s390/kvm/s390/vsie.c-1625-\narch/s390/kvm/s390/vsie.c:1626:static int vsie_handle_mvpg(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct gmap *sg)\narch/s390/kvm/s390/vsie.c-1627-{\n--\narch/s390/kvm/s390/vsie.c-1705- */\narch/s390/kvm/s390/vsie.c:1706:static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct gmap *sg)\narch/s390/kvm/s390/vsie.c-1707-\t__releases(vcpu-\u003ekvm-\u003esrcu)\n--\narch/s390/kvm/s390/vsie.c-1805- */\narch/s390/kvm/s390/vsie.c:1806:static void register_shadow_scb(struct kvm_vcpu *vcpu,\narch/s390/kvm/s390/vsie.c-1807-\t\t\t\tstruct vsie_page *vsie_page)\n--\narch/s390/kvm/s390/vsie.c-1835- */\narch/s390/kvm/s390/vsie.c:1836:static void unregister_shadow_scb(struct kvm_vcpu *vcpu)\narch/s390/kvm/s390/vsie.c-1837-{\n--\narch/s390/kvm/s390/vsie.c-1849- */\narch/s390/kvm/s390/vsie.c:1850:static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-1851-{\n--\narch/s390/kvm/s390/vsie.c=1916=static inline bool try_get_vsie_page(struct vsie_page *vsie_page)\n--\narch/s390/kvm/s390/vsie.c-1923-/* Put a vsie page acquired through get_vsie_page / try_get_vsie_page. */\narch/s390/kvm/s390/vsie.c:1924:static void put_vsie_page(struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-1925-{\n--\narch/s390/kvm/s390/vsie.c-1928-\narch/s390/kvm/s390/vsie.c:1929:static void free_vsie_page(struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-1930-{\n--\narch/s390/kvm/s390/vsie.c=1934=static struct vsie_page *alloc_vsie_page(struct kvm *kvm)\n--\narch/s390/kvm/s390/vsie.c-1946-\narch/s390/kvm/s390/vsie.c:1947:static int init_vsie_page(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, unsigned long scb_gpa)\narch/s390/kvm/s390/vsie.c-1948-{\n--\narch/s390/kvm/s390/vsie.c-1963-/* Reset shadow state after a vsie_page has been (re)initialised for a new SCB. */\narch/s390/kvm/s390/vsie.c:1964:static void reset_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-1965-{\n--\narch/s390/kvm/s390/vsie.c-1983- */\narch/s390/kvm/s390/vsie.c:1984:static int get_vsie_page(struct kvm_vcpu *vcpu, unsigned long addr,\narch/s390/kvm/s390/vsie.c-1985-\t\t\t struct vsie_page **vsie_page_out)\n--\narch/s390/kvm/s390/vsie.c-2082-\narch/s390/kvm/s390/vsie.c:2083:static int get_vsie_page_cpu_nr(struct kvm_vcpu *vcpu, struct vsie_sca *vsie_sca, gpa_t scb_gpa,\narch/s390/kvm/s390/vsie.c-2084-\t\t\t\tu16 cpu_nr, struct vsie_page **vsie_page_out)\n--\narch/s390/kvm/s390/vsie.c-2143- */\narch/s390/kvm/s390/vsie.c:2144:static void sca_mcn_copy(struct vsie_sca *vsie_sca, void *sca)\narch/s390/kvm/s390/vsie.c-2145-{\n--\narch/s390/kvm/s390/vsie.c=2162=static bool sca_mcn_equals(struct vsie_sca *vsie_sca, void *sca)\n--\narch/s390/kvm/s390/vsie.c-2174-\narch/s390/kvm/s390/vsie.c:2175:static void update_vsie_sca(struct vsie_sca *vsie_sca, unsigned int cpu_nr,\narch/s390/kvm/s390/vsie.c-2176-\t\t\t struct vsie_page *vsie_page_n, hpa_t sca_o_entry_hpa)\n--\narch/s390/kvm/s390/vsie.c-2184-\narch/s390/kvm/s390/vsie.c:2185:static int _shadow_sca_cpu(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,\narch/s390/kvm/s390/vsie.c-2186-\t\t\t struct vsie_sca *vsie_sca, hpa_t sca_o_entry_hpa,\n--\narch/s390/kvm/s390/vsie.c-2217-/* Fill the shadow system control area used for VSIE SIGPI. */\narch/s390/kvm/s390/vsie.c:2218:static int _shadow_sca(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,\narch/s390/kvm/s390/vsie.c-2219-\t\t struct vsie_sca *vsie_sca)\n--\narch/s390/kvm/s390/vsie.c=2261=static bool shadow_sca_needed(struct vsie_sca *vsie_sca)\n--\narch/s390/kvm/s390/vsie.c-2268-/* Shadow or reshadow the SCA on VSIE enter. */\narch/s390/kvm/s390/vsie.c:2269:static int shadow_sca(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct vsie_sca *vsie_sca)\narch/s390/kvm/s390/vsie.c-2270-{\n--\narch/s390/kvm/s390/vsie.c-2282-\narch/s390/kvm/s390/vsie.c:2283:int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)\narch/s390/kvm/s390/vsie.c-2284-{\n--\narch/s390/kvm/s390/vsie.c-2366-/* Init the vsie data structures. To be called when a vm is initialized. */\narch/s390/kvm/s390/vsie.c:2367:void kvm_s390_vsie_init(struct kvm *kvm)\narch/s390/kvm/s390/vsie.c-2368-{\n--\narch/s390/kvm/s390/vsie.c-2374-\narch/s390/kvm/s390/vsie.c:2375:static void kvm_s390_vsie_destroy_page(struct kvm *kvm, struct vsie_page *vsie_page)\narch/s390/kvm/s390/vsie.c-2376-{\n--\narch/s390/kvm/s390/vsie.c-2382-/* Destroy the vsie data structures. To be called when a vm is destroyed. */\narch/s390/kvm/s390/vsie.c:2383:void kvm_s390_vsie_destroy(struct kvm *kvm)\narch/s390/kvm/s390/vsie.c-2384-{\n--\narch/s390/kvm/s390/vsie.c-2420-\narch/s390/kvm/s390/vsie.c:2421:void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)\narch/s390/kvm/s390/vsie.c-2422-{\n"
} |
| 7/2 | 2026/08/27 16:16 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 36600 output: 34 thoughts: 625 Thoughts: **My Analysis of VSIE Functionality** Okay, let's break this down. My initial thought process is to identify the critical functions related to this Virtual Server Instruction Extension (VSIE) implementation. The listed functions, namely `kvm_s390_handle_vsie`, `get_vsie_sca`, `shadow_sca`, and a few others, are my first pass. Now, considering the prompt, I'm specifically looking for non-static, non-inlined API entrypoints. The rationale is clear: these are the functions that represent the exposed interface. I've quickly confirmed which ones are static: `get_vsie_sca`, `shadow_sca`, `_shadow_sca`, `get_vsie_page`, `shadow_scb`, and `pin_blocks` - all static, so those are out. That leaves `kvm_s390_handle_vsie`. I recall the prompt mentioning preference for entry points. Looking around, I see a few other non-static functions as well - `kvm_s390_vsie_init`, `kvm_s390_vsie_destroy`, and `kvm_s390_vsie_gmap_notifier`. Focusing back on `kvm_s390_handle_vsie`, it looks like the core entry point for VSIE processing. I'm also noticing that the patch introduces an `ssca` module parameter, which enables VSIE SIGPI and SRSI using the System Services Control Area (SSCA) through `use_ssca()`. The crucial configuration is `CONFIG_KVM`. I'll also quickly scan any other non-static functions that were modified to provide context for `kvm_s390_handle_vsie`. Specifically, `kvm_arch_init_vm` and `kvm_s390_cpu_feat_init` in `arch/s390/kvm/s390/s390.c` have been modified, along with `sclp_early_facilities_detect` in `drivers/s390/char/sclp_early.c`. Therefore, the key symbol I should focus on appears to be `kvm_s390_handle_vsie` considering all the available information. Before I go any further, I should verify the system dependencies for `CONFIG_KVM` on the s390 architecture. Usually, `CONFIG_VIRTUALIZATION` is needed, so I'll check `arch/s390/kvm/Kconfig` to be sure. |
| 8/2 | 2026/08/27 16:16 | tool | grepper |
0mArgs:{
"Expression": "depends on",
"PathPrefix": "arch/s390/kvm/Kconfig"
}Results: {
"Output": "arch/s390/kvm/Kconfig=47=config KVM_S390_UCONTROL\narch/s390/kvm/Kconfig-48-\tbool \"Userspace controlled virtual machines\"\narch/s390/kvm/Kconfig:49:\tdepends on KVM\narch/s390/kvm/Kconfig-50-\thelp\n"
} |
| 9/2 | 2026/08/27 16:16 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 37941 output: 37 thoughts: 53 Thoughts: **Analyzing the KVM Configuration** Okay, so I'm looking at this `KVM` configuration, specifically within the `arch/s390/kvm/Kconfig` file. And I'm noticing something interesting: there's no explicit `depends on` clause directly within the `KVM` configuration itself, at least that's what I'm seeing. But it *is* nested under an `if VIRTUALIZATION` block. Clearly, the availability of KVM on this architecture hinges on `VIRTUALIZATION`. Time to dig into `VIRTUALIZATION` and see what's really driving this dependency. It's the key to understanding the overall configuration flow. |
| 10/2 | 2026/08/27 16:17 | tool | grepper |
0mArgs:{
"Expression": "config VIRTUALIZATION",
"PathPrefix": "arch/s390/kvm/Kconfig"
}Results: {
"Output": "arch/s390/kvm/Kconfig=5=source \"virt/kvm/Kconfig\"\narch/s390/kvm/Kconfig-6-\narch/s390/kvm/Kconfig:7:menuconfig VIRTUALIZATION\narch/s390/kvm/Kconfig-8-\tdef_bool y\n"
} |
| 11/2 | 2026/08/27 16:17 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 38277 output: 55 thoughts: 341 Thoughts: **My Analysis of the KVM Nested Virtualization Patch** Okay, so the `VIRTUALIZATION` config is definitely set to `y`, meaning we're dealing with a KVM setup. That confirms `KVM` is indeed sufficient for the base level of this analysis. I need to make sure I haven't missed anything obvious. Now, looking at the patch, I see it's using `KVM_S390_VM_CPU_FEAT_SIGPIF`. That's a feature flag, not a base config to determine. The core of this seems to be about nested virtualization (vSIE) on s390x, specifically shadowing the System Control Area (SCA). This allows nested guests to use SSCA for SIGPI and SRSI instructions. That's a pretty low-level area, so this change will likely involve substantial modifications to page management, the SCA shadowing logic, and memory pinning within `arch/s390/kvm/s390/vsie.c`. Given that these are core KVM features when we're running nested guests, this makes it a very appealing target for fuzzing. My primary focus here will be on `kvm_s390_handle_vsie`. To confirm my approach, I need to see if there are other entry points of interest. I also need to track `kvm_s390_vsie_gmap_notifier`, `kvm_s390_vsie_init`, and `kvm_s390_vsie_destroy`. I will incorporate both `kvm_s390_handle_vsie` and `kvm_s390_vsie_gmap_notifier` in the final fuzzing targets. Finally, I will verify that `kvm_s390_vsie_gmap_notifier` is indeed non-static. |
| 12/2 | 2026/08/27 16:17 | tool | grepper |
0mArgs:{
"Expression": "void kvm_s390_vsie_gmap_notifier",
"PathPrefix": "arch/s390/kvm/s390/vsie.c"
}Results: {
"Output": "arch/s390/kvm/s390/vsie.c=1090=static int get_vsie_sca(struct kvm_vcpu *vcpu, struct kvm_s390_sie_block *scb_o,\n--\narch/s390/kvm/s390/vsie.c-1213-\narch/s390/kvm/s390/vsie.c:1214:void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, gpa_t start, gpa_t end)\narch/s390/kvm/s390/vsie.c-1215-{\n"
} |
| 13/2 | 2026/08/27 16:17 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 39276 output: 173 thoughts: 16 Thoughts: Here's your summary: **The Dynamic Nature of the Task** Okay, so I've assessed the situation, and it's definitely not a static scenario. There's a dynamic aspect to this, something that's evolving or changing. I need to account for that. Therefore, I will define and then solidify the output to the best of my ability, given the active context. The results are set and ready. |
| 14/2 | 2026/08/27 16:17 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"KVM"
],
"FocusSymbols": [
"kvm_s390_handle_vsie",
"kvm_s390_vsie_gmap_notifier"
],
"Reasoning": "The patch implements shadowing of the System Control Area (SCA) for nested virtualization (vSIE) on s390x architecture. This allows nested guests to use the SSCA for SIGPI and SRSI instructions. The changes involve significant modifications to the vSIE page management, SCA shadowing logic, and memory pinning in `arch/s390/kvm/s390/vsie.c`. These are reachable core KVM features when running nested guests, making them highly relevant for fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"KVM"
],
"FocusSymbols": [
"kvm_s390_handle_vsie",
"kvm_s390_vsie_gmap_notifier"
],
"Reasoning": "The patch implements shadowing of the System Control Area (SCA) for nested virtualization (vSIE) on s390x architecture. This allows nested guests to use the SSCA for SIGPI and SRSI instructions. The changes involve significant modifications to the vSIE page management, SCA shadowing logic, and memory pinning in `arch/s390/kvm/s390/vsie.c`. These are reachable core KVM features when running nested guests, making them highly relevant for fuzzing.",
"WorthFuzzing": true
} |
| 15/1 | 2026/08/27 16:17 | action | If |
0mArgs:{
"WorthFuzzing": true
}Results: null |
| 16/2 | 2026/08/27 16:17 | agent | kmsan-evaluator |
0mModel:best-expensive Args: null Results: {
"KMSANReasoning": "The patch introduces new structures (`vsie_sca`, `vsie_page`) and logic for shadowing the System Control Area (SCA) in s390 KVM nested virtualization (VSIE). All new memory allocations for these structures explicitly use `__GFP_ZERO` (e.g., `alloc_pages_exact(..., GFP_KERNEL_ACCOUNT | __GFP_ZERO)` and `__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA)`). When these structures are reused from caches, they are explicitly cleared using `memset` (e.g., in `clear_vsie_sca` and `reset_vsie_page`). The patch does not introduce any new data copies to userspace, and all hardware-shared shadow structures are fully zero-initialized before use. Any potential bugs introduced by this patch would be logic errors, out-of-bounds accesses, or use-after-frees, which are effectively caught by KASAN. There is no risk of uninitialized memory usage or info-leaks, making a dedicated KMSAN session unnecessary.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: For your convenience, here is the diff of the changes:
commit 25a55cc0e16c4ee5e1af3cd4c4c18a54af94e43c
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Aug 27 16:16:25 2026 +0000
syz-cluster: applied patch under review
diff --git a/arch/s390/include/asm/kvm_host_s390.h b/arch/s390/include/asm/kvm_host_s390.h
index cd692f8fb7643..a19d3a5f550ce 100644
--- a/arch/s390/include/asm/kvm_host_s390.h
+++ b/arch/s390/include/asm/kvm_host_s390.h
@@ -412,6 +412,10 @@ struct kvm_vm_stat {
u64 gmap_shadow_r3_entry;
u64 gmap_shadow_sg_entry;
u64 gmap_shadow_pg_entry;
+ u64 vsie_shadow_scb;
+ u64 vsie_shadow_scb_reuse;
+ u64 vsie_shadow_sca;
+ u64 vsie_shadow_sca_reuse;
};
struct kvm_arch_memory_slot {
@@ -572,13 +576,32 @@ struct sie_page2 {
};
struct vsie_page;
+struct vsie_sca;
+/*
+ * vsie_pages, scas and accompanied management vars
+ */
struct kvm_s390_vsie {
+ /*
+ * protects pages[], page_count, next, addr_to_page
+ */
struct mutex mutex;
- struct radix_tree_root addr_to_page;
+ struct xarray addr_to_page;
int page_count;
int next;
- struct vsie_page *pages[KVM_MAX_VCPUS];
+ struct vsie_page *pages[KVM_S390_MAX_VSIE_VCPUS];
+ /*
+ * The vsie_sca_lock is used to synchronize access to
+ * - the kvm_s390_vsie.scas[]
+ * - the kvm_s390_vsie.osca_to_sca map
+ * - sca_count and sca_next
+ * - new vsie_sca creation and initialization
+ */
+ struct rw_semaphore vsie_sca_lock;
+ struct xarray osca_to_sca;
+ int sca_count;
+ int sca_next;
+ struct vsie_sca *scas[KVM_S390_MAX_VSIE_VCPUS];
};
struct kvm_s390_gisa_iam {
@@ -623,6 +646,7 @@ struct kvm_arch {
int use_pfmfi;
int use_skf;
int use_zpci_interp;
+ int use_ssca;
int user_cpu_state_ctrl;
int user_sigp;
int user_stsi;
diff --git a/arch/s390/include/asm/kvm_host_s390_types.h b/arch/s390/include/asm/kvm_host_s390_types.h
index 9e348a5304217..6eef71072b4ed 100644
--- a/arch/s390/include/asm/kvm_host_s390_types.h
+++ b/arch/s390/include/asm/kvm_host_s390_types.h
@@ -6,10 +6,15 @@
#include <linux/atomic.h>
#include <linux/types.h>
+#define KVM_S390_CPU_MASK 0xff
+#define KVM_S390_MAX_VSIE_VCPUS 256
+#define KVM_S390_MAX_SCA_PAGES 5
+
#define KVM_S390_BSCA_CPU_SLOTS 64
#define KVM_S390_ESCA_CPU_SLOTS 248
#define SCB_ALIGNMENT_SHIFT 9
+#define SCA_ALIGNMENT_SHIFT 6
#define SIGP_CTRL_C 0x80
#define SIGP_CTRL_SCN_MASK 0x3f
@@ -47,6 +52,13 @@ struct bsca_entry {
__u64 reserved2[2];
};
+struct ssca_entry {
+ __u64 reserved1;
+ __u64 ssda;
+ __u64 ossea;
+ __u64 reserved2;
+};
+
union ipte_control {
unsigned long val;
struct {
@@ -88,6 +100,20 @@ struct esca_block {
struct esca_entry cpu[KVM_S390_ESCA_CPU_SLOTS];
};
+/*
+ * The shadow sca / ssca needs to cover both bsca and esca depending on what the
+ * guest uses so we allocate space for 256 entries that are defined in the
+ * architecture.
+ * The header part of the struct must not cross page boundaries.
+ */
+struct ssca_block {
+ __u64 osca;
+ __u64 reserved08[7];
+ struct ssca_entry cpu[KVM_S390_MAX_VSIE_VCPUS];
+};
+
+static_assert(offsetof(struct ssca_block, cpu) == 64);
+
/*
* This struct is used to store some machine check info from lowcore
* for machine checks that happen while the guest is running.
@@ -317,7 +343,7 @@ struct kvm_s390_sie_block {
__u32 fac; /* 0x01a0 */
__u8 reserved1a4[20]; /* 0x01a4 */
__u64 cbrlo; /* 0x01b8 */
- __u8 reserved1c0[8]; /* 0x01c0 */
+ __u64 osda; /* 0x01c0 */
#define ECD_HOSTREGMGMT 0x20000000
#define ECD_MEF 0x08000000
#define ECD_ETOKENF 0x02000000
diff --git a/arch/s390/include/asm/sclp.h b/arch/s390/include/asm/sclp.h
index 44066550b9b10..9e960c09ce0b3 100644
--- a/arch/s390/include/asm/sclp.h
+++ b/arch/s390/include/asm/sclp.h
@@ -105,6 +105,7 @@ struct sclp_info {
unsigned char has_dirq : 1;
unsigned char has_iplcc : 1;
unsigned char has_zpci_lsi : 1;
+ unsigned char has_vsie_interp_extf : 1;
unsigned char has_aisii : 1;
unsigned char has_aeni : 1;
unsigned char has_aisi : 1;
diff --git a/arch/s390/kvm/s390/s390.c b/arch/s390/kvm/s390/s390.c
index b0839e887221e..ffa5bbb8713bc 100644
--- a/arch/s390/kvm/s390/s390.c
+++ b/arch/s390/kvm/s390/s390.c
@@ -87,6 +87,10 @@ const struct kvm_stats_desc kvm_vm_stats_desc[] = {
STATS_DESC_COUNTER(VM, gmap_shadow_r3_entry),
STATS_DESC_COUNTER(VM, gmap_shadow_sg_entry),
STATS_DESC_COUNTER(VM, gmap_shadow_pg_entry),
+ STATS_DESC_COUNTER(VM, vsie_shadow_scb),
+ STATS_DESC_COUNTER(VM, vsie_shadow_scb_reuse),
+ STATS_DESC_COUNTER(VM, vsie_shadow_sca),
+ STATS_DESC_COUNTER(VM, vsie_shadow_sca_reuse),
};
const struct kvm_stats_header kvm_vm_stats_header = {
@@ -244,6 +248,11 @@ static int async_destroy = 1;
module_param(async_destroy, int, 0444);
MODULE_PARM_DESC(async_destroy, "Asynchronous destroy for protected guests");
+/* allow vsie sigp and srs interpretation if available */
+static bool ssca = true;
+module_param(ssca, bool, 0444);
+MODULE_PARM_DESC(ssca, "VSIE SIGPI and SRSI using the SSCA");
+
#define HMFAI_DWORDS 16
/*
* Base feature mask that defines default mask for facilities. Consists of the
@@ -473,6 +482,8 @@ static void __init kvm_s390_cpu_feat_init(void)
allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
if (sclp.has_astfleie2)
allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ASTFLEIE2);
+ if (sclp.has_vsie_interp_extf && ssca)
+ allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIGPIF);
/*
* KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
* all skey handling functions read/set the skey from the PGSTE
@@ -487,9 +498,6 @@ static void __init kvm_s390_cpu_feat_init(void)
* For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
* KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
* correctly shadowed. We can do that for the PGSTE but not for PTE.I.
- *
- * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
- * cannot easily shadow the SCA because of the ipte lock.
*/
}
@@ -3359,6 +3367,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
kvm->arch.use_pfmfi = sclp.has_pfmfi;
kvm->arch.use_skf = sclp.has_skey;
+ kvm->arch.use_ssca = sclp.has_vsie_interp_extf && ssca;
spin_lock_init(&kvm->arch.start_stop_lock);
kvm_s390_vsie_init(kvm);
if (use_gisa)
diff --git a/arch/s390/kvm/s390/vsie.c b/arch/s390/kvm/s390/vsie.c
index 5926689fa2267..67b68f4302b74 100644
--- a/arch/s390/kvm/s390/vsie.c
+++ b/arch/s390/kvm/s390/vsie.c
@@ -29,6 +29,7 @@
enum vsie_page_flags {
VSIE_PAGE_IN_USE = 0,
+ VSIE_PAGE_SCB_PINNED = 1,
};
struct vsie_page {
@@ -56,13 +57,13 @@ struct vsie_page {
gpa_t sdnx_gpa; /* 0x0250 */
/*
* guest address of the original SCB. Remains set for free vsie
- * pages, so we can properly look them up in our addr_to_page
- * radix tree.
+ * pages, so we can properly look them up in our addr_to_page map.
*/
gpa_t scb_gpa; /* 0x0258 */
/* the shadow gmap in use by the vsie_page */
struct gmap_cache gmap_cache; /* 0x0260 */
- __u8 reserved[0x06f8 - 0x0278]; /* 0x0278 */
+ struct vsie_sca *vsie_sca; /* 0x0278 */
+ __u8 reserved[0x06f8 - 0x0280]; /* 0x0280 */
struct kvm_s390_crypto_cb crycb; /* 0x06f8 */
__u8 fac[8 + S390_ARCH_FAC_LIST_SIZE_BYTE];/* 0x07f8 */
};
@@ -71,6 +72,88 @@ static_assert(sizeof(struct vsie_page) == PAGE_SIZE);
static_assert(offsetof(struct vsie_page, mcck_info) == offsetof(struct sie_page, mcck_info));
static_assert(IS_ALIGNED(offsetof(struct vsie_page, crycb), 8));
+struct kvm_address_pair {
+ gpa_t gpa;
+ hpa_t hpa;
+};
+
+enum vsie_sca_flags {
+ VSIE_SCA_ESCA = 0,
+ VSIE_SCA_SCA_PINNED = 1,
+};
+
+struct vsie_sca {
+ struct_group(head,
+ struct ssca_block ssca;
+ );
+ struct_group(keep,
+ struct vsie_page *pages[KVM_S390_MAX_VSIE_VCPUS];
+ /* The mutex is used to synchronize access to the pages[] */
+ struct mutex mutex;
+ atomic_t ref_count;
+ );
+ struct_group(tail,
+ gpa_t sca_gpa;
+ unsigned long flags;
+ u64 mcn[4];
+ unsigned int sca_o_nr_pages;
+ struct kvm_address_pair sca_o_pages[KVM_S390_MAX_SCA_PAGES];
+ );
+};
+
+/*
+ * SSCA needs to be 32-byte aligned and members before the cpu field need to be on the same page.
+ * Forcing it to offset 0 will always fulfill the requirements.
+ */
+static_assert(!(offsetof(struct vsie_sca, ssca)));
+
+/* maximum vsie shadow scb */
+static unsigned int vsie_shadow_scb_max = 1;
+module_param(vsie_shadow_scb_max, uint, 0644);
+MODULE_PARM_DESC(vsie_shadow_scb_max, "Maximum number of VSIE shadow control blocks to keep. Values smaller number VCPUs uses number of VCPUs; maximum 256");
+
+/* maximum vsie shadow sca */
+static unsigned int vsie_shadow_sca_max = 1;
+module_param(vsie_shadow_sca_max, uint, 0644);
+MODULE_PARM_DESC(vsie_shadow_sca_max, "Maximum number of VSIE shadow system control areas to keep. Values smaller number of VCPUs uses number of VCPUs; maximum 256");
+
+static inline hpa_t sca_o_hpa(struct vsie_sca *vsie_sca)
+{
+ return vsie_sca->sca_o_pages[0].hpa | (vsie_sca->sca_gpa & ~PAGE_MASK);
+}
+
+static inline bool sie_uses_esca(struct kvm_s390_sie_block *scb)
+{
+ return (scb->ecb2 & ECB2_ESCA);
+}
+
+static unsigned long read_scao(struct kvm *kvm, struct kvm_s390_sie_block *scb)
+{
+ unsigned long vsie_sca = READ_ONCE(scb->scaol) & ~0xfUL;
+
+ if (test_kvm_cpu_feat(kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
+ vsie_sca |= (u64)READ_ONCE(scb->scaoh) << 32;
+
+ return vsie_sca;
+}
+
+static void write_scao(struct kvm_s390_sie_block *scb, unsigned long hpa)
+{
+ scb->scaoh = (u32)((u64)hpa >> 32);
+ scb->scaol = (u32)(u64)hpa;
+}
+
+static inline bool use_ssca(struct kvm *kvm, struct kvm_s390_sie_block *scb)
+{
+ if (!kvm->arch.use_ssca)
+ return false;
+ if (!(scb->eca & ECA_SIGPI) && !(scb->ecb & ECB_SRSI))
+ return false;
+ if (!read_scao(kvm, scb))
+ return false;
+ return true;
+}
+
/* trigger a validity icpt for the given scb */
static int set_validity_icpt(struct kvm_s390_sie_block *scb,
__u16 reason_code)
@@ -81,6 +164,25 @@ static int set_validity_icpt(struct kvm_s390_sie_block *scb,
return 1;
}
+/* The sca header must not cross pages etc. */
+static int validate_scao(struct kvm_vcpu *vcpu, struct kvm_s390_sie_block *scb, gpa_t gpa)
+{
+ int offset;
+
+ if (gpa < 2 * PAGE_SIZE)
+ return set_validity_icpt(scb, 0x0038U);
+ if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
+ return set_validity_icpt(scb, 0x0011U);
+
+ if (sie_uses_esca(scb))
+ offset = offsetof(struct esca_block, cpu[0]) - 1;
+ else
+ offset = offsetof(struct bsca_block, cpu[0]) - 1;
+ if ((gpa & PAGE_MASK) != ((gpa + offset) & PAGE_MASK))
+ return set_validity_icpt(scb, 0x003bU);
+ return 0;
+}
+
/* mark the prefix as unmapped, this will block the VSIE */
static void prefix_unmapped(struct vsie_page *vsie_page)
{
@@ -109,6 +211,148 @@ static int prefix_is_mapped(struct vsie_page *vsie_page)
return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
}
+static void release_gmap_shadow(struct vsie_page *vsie_page)
+{
+ struct gmap *gmap = vsie_page->gmap_cache.gmap;
+
+ lockdep_assert_held(&gmap->kvm->arch.gmap->children_lock);
+
+ list_del(&vsie_page->gmap_cache.list);
+ vsie_page->gmap_cache.gmap = NULL;
+ prefix_unmapped(vsie_page);
+
+ if (list_empty(&gmap->scb_users)) {
+ gmap_remove_child(gmap);
+ gmap_put(gmap);
+ }
+}
+
+static void release_gmap_shadow_safe(struct kvm *kvm, struct vsie_page *vsie_page)
+{
+ if (!vsie_page->gmap_cache.gmap)
+ return;
+
+ guard(spinlock)(&kvm->arch.gmap->children_lock);
+
+ if (vsie_page->gmap_cache.gmap)
+ release_gmap_shadow(vsie_page);
+}
+
+static struct gmap *acquire_gmap_shadow(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+{
+ union ctlreg0 cr0;
+ struct gmap *gmap;
+ union asce asce;
+ int edat;
+
+ asce.val = vcpu->arch.sie_block->gcr[1];
+ cr0.val = vcpu->arch.sie_block->gcr[0];
+ edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
+ edat += edat && test_kvm_facility(vcpu->kvm, 78);
+
+ scoped_guard(spinlock, &vcpu->kvm->arch.gmap->children_lock) {
+ gmap = vsie_page->gmap_cache.gmap;
+ if (gmap) {
+ /*
+ * ASCE or EDAT could have changed since last icpt, or the gmap
+ * we're holding has been unshadowed. If the gmap is still valid,
+ * we can safely reuse it.
+ */
+ if (gmap_is_shadow_valid(gmap, asce, edat)) {
+ vcpu->kvm->stat.gmap_shadow_reuse++;
+ gmap_get(gmap);
+ return gmap;
+ }
+ /* release the old shadow and mark the prefix as unmapped */
+ release_gmap_shadow(vsie_page);
+ }
+ }
+again:
+ gmap = gmap_create_shadow(vcpu->arch.mc, vcpu->kvm->arch.gmap, asce, edat);
+ if (IS_ERR(gmap))
+ return gmap;
+ scoped_guard(spinlock, &vcpu->kvm->arch.gmap->children_lock) {
+ /* unlikely race condition, remove the previous shadow */
+ if (vsie_page->gmap_cache.gmap)
+ release_gmap_shadow(vsie_page);
+ if (!gmap->parent) {
+ gmap_put(gmap);
+ goto again;
+ }
+ vcpu->kvm->stat.gmap_shadow_create++;
+ list_add(&vsie_page->gmap_cache.list, &gmap->scb_users);
+ vsie_page->gmap_cache.gmap = gmap;
+ prefix_unmapped(vsie_page);
+ }
+ return gmap;
+}
+
+/*
+ * Pin the guest page given by gpa and set hpa to the pinned host address.
+ * Will always be pinned writable.
+ *
+ * Returns: - 0 on success
+ * - -EINVAL if the gpa is not valid guest storage
+ */
+static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
+{
+ struct page *page;
+
+ page = gfn_to_page(kvm, gpa_to_gfn(gpa));
+ if (!page)
+ return -EINVAL;
+ *hpa = (hpa_t)page_to_phys(page) + (gpa & ~PAGE_MASK);
+ return 0;
+}
+
+/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
+static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
+{
+ kvm_release_page_dirty(pfn_to_page(phys_to_pfn(hpa)));
+ /* mark the page always as dirty for migration */
+ mark_page_dirty(kvm, gpa_to_gfn(gpa));
+}
+
+/* unpin multiple guest pages pinned with pin_guest_pages() */
+static void unpin_guest_pages(struct kvm *kvm, struct kvm_address_pair *addr, unsigned int nr_pages)
+{
+ int i;
+
+ for (i = 0; i < nr_pages; i++) {
+ unpin_guest_page(kvm, addr[i].gpa, addr[i].hpa);
+ addr[i].gpa = 0;
+ addr[i].hpa = 0;
+ }
+}
+
+/*
+ * pin nr_pages consecutive guest pages
+ */
+static int pin_guest_pages(struct kvm *kvm, gpa_t gpa, unsigned int nr_pages,
+ struct kvm_address_pair *addr, unsigned int *nr_pages_pinned)
+{
+ int rc = 0, i;
+ hpa_t hpa;
+
+ gpa = gpa & PAGE_MASK;
+
+ /* the guest pages may not be mapped continuously, so pin each page */
+ for (i = 0; i < nr_pages; i++) {
+ rc = pin_guest_page(kvm, gpa + PAGE_SIZE * i, &hpa);
+ if (rc)
+ goto err;
+ addr[i].gpa = gpa + PAGE_SIZE * i;
+ addr[i].hpa = hpa;
+ }
+ *nr_pages_pinned = i;
+ return rc;
+
+err:
+ unpin_guest_pages(kvm, addr, i);
+ *nr_pages_pinned = 0;
+ return rc;
+}
+
/* copy the updated intervention request bits into the shadow scb */
static void update_intervention_requests(struct vsie_page *vsie_page)
{
@@ -124,8 +368,7 @@ static void update_intervention_requests(struct vsie_page *vsie_page)
static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
{
struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
- struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
- int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
+ int newflags, cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
/* we don't allow ESA/390 guests unless explicitly enabled */
if (!(cpuflags & CPUSTAT_ZARCH) && !vcpu->kvm->arch.allow_vsie_esamode)
@@ -424,18 +667,27 @@ static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
}
}
-/* unshadow the scb, copying parameters back to the real scb */
-static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+/*
+ * Only write the interception fields back; avoid clobbering the timer and
+ * guest-state fields in scb_o.
+ */
+static inline void unshadow_intercept(struct kvm_s390_sie_block *scb_o,
+ struct kvm_s390_sie_block *scb_s)
{
- struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
- struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
-
- /* interception */
scb_o->icptcode = scb_s->icptcode;
scb_o->icptstatus = scb_s->icptstatus;
scb_o->ipa = scb_s->ipa;
scb_o->ipb = scb_s->ipb;
scb_o->gbea = scb_s->gbea;
+}
+
+/* unshadow the scb, copying parameters back to the real scb */
+static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+{
+ struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
+ struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
+
+ unshadow_intercept(scb_o, scb_s);
/* timer */
scb_o->cputm = scb_s->cputm;
@@ -469,6 +721,39 @@ static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
scb_o->ihcpu = scb_s->ihcpu;
}
+static int shadow_scb_minimal(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+{
+ struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
+ struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
+ int rc;
+
+ /* make sure we don't have any leftovers when reusing the scb */
+ scb_s->icptcode = 0;
+ scb_s->eca = 0;
+ scb_s->ecb = 0;
+ scb_s->ecb2 = 0;
+ scb_s->ecb3 = 0;
+ scb_s->ecd = 0;
+ scb_s->fac = 0;
+ scb_s->fpf = 0;
+
+ rc = prepare_cpuflags(vcpu, vsie_page);
+ if (rc)
+ return rc;
+
+ scb_s->icpua = scb_o->icpua;
+ scb_s->ecb2 |= scb_o->ecb2 & ECB2_ESCA;
+
+ if (vsie_page->vsie_sca) {
+ scb_s->eca |= scb_o->eca & ECA_SIGPI;
+ scb_s->ecb |= scb_o->ecb & ECB_SRSI;
+ write_scao(scb_s, virt_to_phys(&vsie_page->vsie_sca->ssca));
+ scb_s->osda = virt_to_phys(scb_o);
+ }
+
+ return 0;
+}
+
/*
* Setup the shadow scb by copying and checking the relevant parts of the g2
* provided scb.
@@ -488,19 +773,9 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
unsigned long new_mso = 0;
int rc;
- /* make sure we don't have any leftovers when reusing the scb */
- scb_s->icptcode = 0;
- scb_s->eca = 0;
- scb_s->ecb = 0;
- scb_s->ecb2 = 0;
- scb_s->ecb3 = 0;
- scb_s->ecd = 0;
- scb_s->fac = 0;
- scb_s->fpf = 0;
-
- rc = prepare_cpuflags(vcpu, vsie_page);
+ rc = shadow_scb_minimal(vcpu, vsie_page);
if (rc)
- goto out;
+ goto out_validity;
/* timer */
scb_s->cputm = scb_o->cputm;
@@ -528,8 +803,6 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
- scb_s->icpua = scb_o->icpua;
-
if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_ZARCH))
new_prefix &= GUEST_PREFIX_MASK_ESA;
else
@@ -614,10 +887,328 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
prepare_ibc(vcpu, vsie_page);
rc = shadow_crycb(vcpu, vsie_page);
-out:
if (rc)
unshadow_scb(vcpu, vsie_page);
return rc;
+
+out_validity:
+ unshadow_intercept(vsie_page->scb_o, &vsie_page->scb_s);
+ return rc;
+}
+
+/* unpin the scb provided by guest 2, marking it as dirty */
+static void unpin_scb(struct kvm *kvm, struct vsie_page *vsie_page)
+{
+ hpa_t hpa;
+
+ if (!test_bit(VSIE_PAGE_SCB_PINNED, &vsie_page->flags))
+ return;
+
+ hpa = virt_to_phys(vsie_page->scb_o);
+ if (hpa)
+ unpin_guest_page(kvm, vsie_page->scb_gpa, hpa);
+ vsie_page->scb_o = NULL;
+ clear_bit(VSIE_PAGE_SCB_PINNED, &vsie_page->flags);
+}
+
+/*
+ * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
+ *
+ * Returns: - 0 if the scb was pinned.
+ * - > 0 if control has to be given to guest 2
+ */
+static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+{
+ hpa_t hpa;
+ int rc;
+
+ if (test_bit(VSIE_PAGE_SCB_PINNED, &vsie_page->flags))
+ return 0;
+
+ rc = pin_guest_page(vcpu->kvm, vsie_page->scb_gpa, &hpa);
+ if (rc) {
+ rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
+ WARN_ON_ONCE(rc);
+ return 1;
+ }
+ vsie_page->scb_o = phys_to_virt(hpa);
+ set_bit(VSIE_PAGE_SCB_PINNED, &vsie_page->flags);
+ return 0;
+}
+
+/*
+ * Unpin g2 original sca in g1 memory.
+ *
+ * Called with vsie_sca_lock held in write, except in the destroy path.
+ */
+static void unpin_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)
+{
+ if (!test_bit(VSIE_SCA_SCA_PINNED, &vsie_sca->flags))
+ return;
+
+ unpin_guest_pages(kvm, vsie_sca->sca_o_pages, vsie_sca->sca_o_nr_pages);
+ vsie_sca->sca_o_nr_pages = 0;
+
+ clear_bit(VSIE_SCA_SCA_PINNED, &vsie_sca->flags);
+}
+
+/*
+ * Pin g2 original sca in g1 memory.
+ *
+ * Called with vsie_sca_lock held in write.
+ */
+static int pin_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)
+{
+ bool is_esca = test_bit(VSIE_SCA_ESCA, &vsie_sca->flags);
+ gpa_t offset = vsie_sca->sca_gpa & ~PAGE_MASK;
+ int rc, nr_pages;
+
+ if (test_bit(VSIE_SCA_SCA_PINNED, &vsie_sca->flags))
+ return 0;
+
+ if (is_esca) {
+ nr_pages = 4;
+ if (offset + sizeof(struct esca_block) > 4 * PAGE_SIZE)
+ nr_pages = 5;
+ } else {
+ nr_pages = 1;
+ if (offset + sizeof(struct bsca_block) > PAGE_SIZE)
+ nr_pages = 2;
+ }
+
+ rc = pin_guest_pages(kvm, vsie_sca->sca_gpa, nr_pages, vsie_sca->sca_o_pages,
+ &vsie_sca->sca_o_nr_pages);
+ if (rc)
+ return rc;
+ set_bit(VSIE_SCA_SCA_PINNED, &vsie_sca->flags);
+
+ return 0;
+}
+
+static int get_sca_entry_addr(struct kvm *kvm, struct vsie_sca *vsie_sca, u16 cpu_nr, gpa_t *gpa,
+ hpa_t *hpa)
+{
+ hpa_t cpu_offset, offset;
+ int pn;
+
+ /*
+ * We cannot simply access the hva since the esca_block has typically
+ * 4 pages (arch max 5 pages) that might not be continuous in g1 memory.
+ * The bsca_block may also be stretched over two pages. Only the header
+ * is guaranteed to be on the same page.
+ */
+ if (test_bit(VSIE_SCA_ESCA, &vsie_sca->flags))
+ cpu_offset = offsetof(struct esca_block, cpu[cpu_nr]);
+ else
+ cpu_offset = offsetof(struct bsca_block, cpu[cpu_nr]);
+ pn = ((vsie_sca->sca_gpa & ~PAGE_MASK) + cpu_offset) >> PAGE_SHIFT;
+ offset = (vsie_sca->sca_gpa + cpu_offset) & ~PAGE_MASK;
+ if (WARN_ON_ONCE(pn >= vsie_sca->sca_o_nr_pages))
+ return -EINVAL;
+
+ if (gpa)
+ *gpa = vsie_sca->sca_o_pages[pn].gpa | offset;
+ if (hpa)
+ *hpa = vsie_sca->sca_o_pages[pn].hpa | offset;
+ return 0;
+}
+
+static void put_vsie_sca(struct vsie_sca *vsie_sca)
+{
+ if (!vsie_sca)
+ return;
+
+ WARN_ON_ONCE(atomic_dec_return(&vsie_sca->ref_count) < 0);
+}
+
+/*
+ * Try to find a matching vsie_sca with the correct sca format.
+ * @sca_o_gpa: original system control area address; guest-2 physical
+ * @uses_esca: whether the guest SCB has ECB2_ESCA set
+ *
+ * Called with lock on vsie_sca_lock.
+ */
+static struct vsie_sca *get_vsie_sca_existing(struct kvm *kvm, gpa_t sca_o_gpa, bool uses_esca)
+{
+ struct vsie_sca *vsie_sca = xa_load(&kvm->arch.vsie.osca_to_sca,
+ sca_o_gpa >> SCA_ALIGNMENT_SHIFT);
+
+ if (!vsie_sca)
+ return NULL;
+ if (uses_esca != test_bit(VSIE_SCA_ESCA, &vsie_sca->flags))
+ return NULL;
+ WARN_ON_ONCE(atomic_inc_return(&vsie_sca->ref_count) < 1);
+ return vsie_sca;
+}
+
+/* Try to find and get a currently unused vsie_sca from the vsie struct. */
+static struct vsie_sca *get_vsie_sca_unused(struct kvm *kvm)
+{
+ struct vsie_sca *vsie_sca;
+ int i, ref_count;
+
+ for (i = 0; i < kvm->arch.vsie.sca_count; i++) {
+ vsie_sca = READ_ONCE(kvm->arch.vsie.scas[kvm->arch.vsie.sca_next]);
+ kvm->arch.vsie.sca_next++;
+ kvm->arch.vsie.sca_next %= kvm->arch.vsie.sca_count;
+ ref_count = atomic_inc_return(&vsie_sca->ref_count);
+ WARN_ON_ONCE(ref_count < 1);
+ if (ref_count == 1)
+ return vsie_sca;
+ put_vsie_sca(vsie_sca);
+ }
+ return ERR_PTR(-EAGAIN);
+}
+
+static void free_vsie_sca(struct kvm *kvm, struct vsie_sca *vsie_sca)
+{
+ free_pages_exact(vsie_sca, sizeof(*vsie_sca));
+}
+
+static struct vsie_sca *alloc_vsie_sca(void)
+{
+ struct vsie_sca *vsie_sca;
+
+ vsie_sca = alloc_pages_exact(sizeof(*vsie_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO);
+ if (!vsie_sca)
+ return NULL;
+
+ mutex_init(&vsie_sca->mutex);
+ atomic_set(&vsie_sca->ref_count, 0);
+
+ return vsie_sca;
+}
+
+/* Clear the vsie_sca struct but keep the vsie_page references, mutex and ref_count */
+static void clear_vsie_sca(struct vsie_sca *vsie_sca)
+{
+ memset(&vsie_sca->head, 0, sizeof(vsie_sca->head));
+ memset(&vsie_sca->tail, 0, sizeof(vsie_sca->tail));
+}
+
+/* Pin and get an existing or new guest-3 system control area.*/
+static int get_vsie_sca(struct kvm_vcpu *vcpu, struct kvm_s390_sie_block *scb_o,
+ struct vsie_sca **vsie_sca_out)
+{
+ struct vsie_sca *vsie_sca, *vsie_sca_new = NULL;
+ gpa_t sca_gpa = read_scao(vcpu->kvm, scb_o);
+ bool is_esca = sie_uses_esca(scb_o);
+ struct vsie_page *vsie_page_n;
+ struct kvm *kvm = vcpu->kvm;
+ unsigned int max_vsie_sca;
+ int rc, cpu_nr;
+
+ /* validate scb_o as we do not unshadow on error here */
+ rc = validate_scao(vcpu, scb_o, sca_gpa);
+ if (rc)
+ return rc;
+
+ down_read(&kvm->arch.vsie.vsie_sca_lock);
+ vsie_sca = get_vsie_sca_existing(kvm, sca_gpa, is_esca);
+ up_read(&kvm->arch.vsie.vsie_sca_lock);
+ if (vsie_sca) {
+ *vsie_sca_out = vsie_sca;
+ return 0;
+ }
+
+ /*
+ * Allocate new vsie_sca, it will likely be needed below.
+ * We want at least #online_vcpus shadows, so every VCPU can execute the
+ * VSIE in parallel. (Worst case all single core VMs.)
+ */
+ max_vsie_sca = min_t(unsigned int, max_t(unsigned int, atomic_read(&kvm->online_vcpus),
+ vsie_shadow_sca_max), KVM_S390_MAX_VSIE_VCPUS);
+
+ if (kvm->arch.vsie.sca_count < max_vsie_sca) {
+ vsie_sca_new = alloc_vsie_sca();
+ if (!vsie_sca_new)
+ return -ENOMEM;
+ }
+
+ /*
+ * Now we're taking the vsie_sca_lock in write mode so that we can manipulate
+ * the xarray and arch.vise.scas, etc.
+ *
+ * In the next lines we try three things to get an SCA:
+ * - Retry getting an existing vsie_sca
+ * - Using our newly allocated vsie_sca if we're under the limit
+ * - Reusing an vsie_sca including ssca to shadow a different osca
+ */
+ down_write(&kvm->arch.vsie.vsie_sca_lock);
+ vsie_sca = xa_load(&kvm->arch.vsie.osca_to_sca, sca_gpa >> SCA_ALIGNMENT_SHIFT);
+ if (vsie_sca) {
+ WARN_ON_ONCE(atomic_inc_return(&vsie_sca->ref_count) < 1);
+ if (is_esca == test_bit(VSIE_SCA_ESCA, &vsie_sca->flags))
+ goto out;
+ /* found vsie_sca with matching sca_gpa but wrong format */
+ put_vsie_sca(vsie_sca);
+ xa_erase(&kvm->arch.vsie.osca_to_sca, sca_gpa >> SCA_ALIGNMENT_SHIFT);
+ }
+
+ /* check again under write lock if we are still under our vsie_sca limit */
+ if (vsie_sca_new && kvm->arch.vsie.sca_count < max_vsie_sca) {
+ /* make use of vsie_sca just created */
+ vsie_sca = vsie_sca_new;
+ vsie_sca_new = NULL;
+
+ kvm->arch.vsie.scas[kvm->arch.vsie.sca_count] = vsie_sca;
+ kvm->arch.vsie.sca_count++;
+ atomic_set(&vsie_sca->ref_count, 1);
+ kvm->stat.vsie_shadow_sca++;
+ } else {
+ /* reuse previously created vsie_sca allocation for different osca */
+ vsie_sca = get_vsie_sca_unused(kvm);
+ /* with nr_vcpus scas one must be reusable */
+ if (IS_ERR(vsie_sca))
+ goto out;
+
+ /* unused vsie_sca exclusive under vsie_sca_lock write lock */
+ xa_erase(&kvm->arch.vsie.osca_to_sca, vsie_sca->sca_gpa >> SCA_ALIGNMENT_SHIFT);
+ for (cpu_nr = 0; cpu_nr < KVM_S390_MAX_VSIE_VCPUS; cpu_nr++) {
+ vsie_page_n = vsie_sca->pages[cpu_nr];
+ if (!vsie_page_n)
+ continue;
+
+ /* unpin but keep the vsie_page for reuse */
+ unpin_scb(kvm, vsie_page_n);
+ release_gmap_shadow_safe(kvm, vsie_page_n);
+ memset(vsie_page_n, 0, sizeof(struct vsie_page));
+ vsie_page_n->scb_gpa = ULONG_MAX;
+ }
+ unpin_sca(kvm, vsie_sca);
+ clear_vsie_sca(vsie_sca);
+ kvm->stat.vsie_shadow_sca_reuse++;
+ }
+
+ if (sie_uses_esca(scb_o))
+ set_bit(VSIE_SCA_ESCA, &vsie_sca->flags);
+ vsie_sca->sca_gpa = sca_gpa;
+
+ /*
+ * The pinned original sca will only be unpinned lazily to limit the
+ * required amount of pins/unpins on each vsie entry/exit.
+ * The unpin is done in the reuse vsie_sca allocation path above and
+ * kvm_s390_vsie_destroy().
+ */
+ rc = pin_sca(kvm, vsie_sca);
+ if (rc) {
+ vsie_sca->sca_gpa = ULONG_MAX;
+ put_vsie_sca(vsie_sca);
+ goto out;
+ }
+
+ rc = xa_insert(&kvm->arch.vsie.osca_to_sca, vsie_sca->sca_gpa >> SCA_ALIGNMENT_SHIFT,
+ vsie_sca, GFP_KERNEL_ACCOUNT);
+ if (rc == -EBUSY)
+ rc = 1;
+
+out:
+ up_write(&kvm->arch.vsie.vsie_sca_lock);
+ if (vsie_sca_new)
+ free_vsie_sca(kvm, vsie_sca_new);
+ if (vsie_sca)
+ *vsie_sca_out = vsie_sca;
+ return rc;
}
void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, gpa_t start, gpa_t end)
@@ -680,44 +1271,18 @@ static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct
return rc;
}
-/*
- * Pin the guest page given by gpa and set hpa to the pinned host address.
- * Will always be pinned writable.
- *
- * Returns: - 0 on success
- * - -EINVAL if the gpa is not valid guest storage
- */
-static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
-{
- struct page *page;
-
- page = gfn_to_page(kvm, gpa_to_gfn(gpa));
- if (!page)
- return -EINVAL;
- *hpa = (hpa_t)page_to_phys(page) + (gpa & ~PAGE_MASK);
- return 0;
-}
-
-/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
-static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
-{
- kvm_release_page_dirty(pfn_to_page(phys_to_pfn(hpa)));
- /* mark the page always as dirty for migration */
- mark_page_dirty(kvm, gpa_to_gfn(gpa));
-}
-
/* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
{
struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
hpa_t hpa;
- hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
- if (hpa) {
- unpin_guest_page(vcpu->kvm, vsie_page->sca_gpa, hpa);
- vsie_page->sca_gpa = 0;
- scb_s->scaol = 0;
- scb_s->scaoh = 0;
+ if (!vsie_page->vsie_sca) {
+ hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
+ if (hpa) {
+ unpin_guest_page(vcpu->kvm, vsie_page->sca_gpa, hpa);
+ write_scao(scb_s, 0);
+ }
}
hpa = scb_s->itdba;
@@ -756,9 +1321,6 @@ static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
* This works as long as the data lies in one page. If blocks ever exceed one
* page, we have to fall back to shadowing.
*
- * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
- * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
- *
* Returns: - 0 if all blocks were pinned.
* - > 0 if control has to be given to guest 2
* - -ENOMEM if out of memory
@@ -771,27 +1333,17 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
gpa_t gpa;
int rc = 0;
- gpa = READ_ONCE(scb_o->scaol) & ~0xfUL;
- if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
- gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32;
- if (gpa) {
- if (gpa < 2 * PAGE_SIZE)
- rc = set_validity_icpt(scb_s, 0x0038U);
- else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
- rc = set_validity_icpt(scb_s, 0x0011U);
- else if ((gpa & PAGE_MASK) !=
- ((gpa + offsetof(struct bsca_block, cpu[0]) - 1) & PAGE_MASK))
- rc = set_validity_icpt(scb_s, 0x003bU);
- if (!rc) {
- rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
- if (rc)
- rc = set_validity_icpt(scb_s, 0x0034U);
- }
+ gpa = vsie_page->sca_gpa;
+ if (gpa && !vsie_page->vsie_sca) {
+ rc = validate_scao(vcpu, scb_s, gpa);
if (rc)
goto unpin;
- vsie_page->sca_gpa = gpa;
- scb_s->scaoh = (u32)((u64)hpa >> 32);
- scb_s->scaol = (u32)(u64)hpa;
+ rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
+ if (rc) {
+ rc = set_validity_icpt(scb_s, 0x0034U);
+ goto unpin;
+ }
+ write_scao(scb_s, hpa);
}
gpa = READ_ONCE(scb_o->itdba) & ~0xffUL;
@@ -866,51 +1418,18 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
/* Due to alignment rules (checked above) this cannot
* cross page boundaries
*/
- rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
- if (rc) {
- rc = set_validity_icpt(scb_s, 0x10b0U);
- goto unpin;
- }
- vsie_page->sdnx_gpa = gpa;
- scb_s->sdnxo = hpa | sdnxc;
- }
- return 0;
-unpin:
- unpin_blocks(vcpu, vsie_page);
- return rc;
-}
-
-/* unpin the scb provided by guest 2, marking it as dirty */
-static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
- gpa_t gpa)
-{
- hpa_t hpa = virt_to_phys(vsie_page->scb_o);
-
- if (hpa)
- unpin_guest_page(vcpu->kvm, gpa, hpa);
- vsie_page->scb_o = NULL;
-}
-
-/*
- * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
- *
- * Returns: - 0 if the scb was pinned.
- * - > 0 if control has to be given to guest 2
- */
-static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
- gpa_t gpa)
-{
- hpa_t hpa;
- int rc;
-
- rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
- if (rc) {
- rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
- WARN_ON_ONCE(rc);
- return 1;
+ rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
+ if (rc) {
+ rc = set_validity_icpt(scb_s, 0x10b0U);
+ goto unpin;
+ }
+ vsie_page->sdnx_gpa = gpa;
+ scb_s->sdnxo = hpa | sdnxc;
}
- vsie_page->scb_o = phys_to_virt(hpa);
return 0;
+unpin:
+ unpin_blocks(vcpu, vsie_page);
+ return rc;
}
/*
@@ -1281,71 +1800,6 @@ static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struc
return rc;
}
-static void release_gmap_shadow(struct vsie_page *vsie_page)
-{
- struct gmap *gmap = vsie_page->gmap_cache.gmap;
-
- lockdep_assert_held(&gmap->kvm->arch.gmap->children_lock);
-
- list_del(&vsie_page->gmap_cache.list);
- vsie_page->gmap_cache.gmap = NULL;
- prefix_unmapped(vsie_page);
-
- if (list_empty(&gmap->scb_users)) {
- gmap_remove_child(gmap);
- gmap_put(gmap);
- }
-}
-
-static struct gmap *acquire_gmap_shadow(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
-{
- union ctlreg0 cr0;
- struct gmap *gmap;
- union asce asce;
- int edat;
-
- asce.val = vcpu->arch.sie_block->gcr[1];
- cr0.val = vcpu->arch.sie_block->gcr[0];
- edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
- edat += edat && test_kvm_facility(vcpu->kvm, 78);
-
- scoped_guard(spinlock, &vcpu->kvm->arch.gmap->children_lock) {
- gmap = vsie_page->gmap_cache.gmap;
- if (gmap) {
- /*
- * ASCE or EDAT could have changed since last icpt, or the gmap
- * we're holding has been unshadowed. If the gmap is still valid,
- * we can safely reuse it.
- */
- if (gmap_is_shadow_valid(gmap, asce, edat)) {
- vcpu->kvm->stat.gmap_shadow_reuse++;
- gmap_get(gmap);
- return gmap;
- }
- /* release the old shadow and mark the prefix as unmapped */
- release_gmap_shadow(vsie_page);
- }
- }
-again:
- gmap = gmap_create_shadow(vcpu->arch.mc, vcpu->kvm->arch.gmap, asce, edat);
- if (IS_ERR(gmap))
- return gmap;
- scoped_guard(spinlock, &vcpu->kvm->arch.gmap->children_lock) {
- /* unlikely race condition, remove the previous shadow */
- if (vsie_page->gmap_cache.gmap)
- release_gmap_shadow(vsie_page);
- if (!gmap->parent) {
- gmap_put(gmap);
- goto again;
- }
- vcpu->kvm->stat.gmap_shadow_create++;
- list_add(&vsie_page->gmap_cache.list, &gmap->scb_users);
- vsie_page->gmap_cache.gmap = gmap;
- prefix_unmapped(vsie_page);
- }
- return gmap;
-}
-
/*
* Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
*/
@@ -1429,7 +1883,7 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
*/
if (kvm_s390_vcpu_has_irq(vcpu, 0) ||
kvm_s390_vcpu_sie_inhibited(vcpu)) {
- kvm_s390_rewind_psw(vcpu, 4);
+ rc = -EAGAIN;
break;
}
if (sg)
@@ -1472,93 +1926,369 @@ static void put_vsie_page(struct vsie_page *vsie_page)
clear_bit(VSIE_PAGE_IN_USE, &vsie_page->flags);
}
+static void free_vsie_page(struct vsie_page *vsie_page)
+{
+ free_page((unsigned long)vsie_page);
+}
+
+static struct vsie_page *alloc_vsie_page(struct kvm *kvm)
+{
+ struct vsie_page *vsie_page;
+
+ vsie_page = (struct vsie_page *)__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA);
+ if (!vsie_page)
+ return vsie_page;
+
+ /* Mark it as invalid until it resides in the tree. */
+ vsie_page->scb_gpa = ULONG_MAX;
+ return vsie_page;
+}
+
+static int init_vsie_page(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, unsigned long scb_gpa)
+{
+ int rc;
+
+ vsie_page->scb_gpa = scb_gpa;
+ rc = pin_scb(vcpu, vsie_page);
+ if (rc) {
+ vsie_page->scb_gpa = ULONG_MAX;
+ return rc;
+ }
+
+ vsie_page->sca_gpa = read_scao(vcpu->kvm, vsie_page->scb_o);
+
+ return 0;
+}
+
+/* Reset shadow state after a vsie_page has been (re)initialised for a new SCB. */
+static void reset_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
+{
+ memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
+ release_gmap_shadow_safe(kvm, vsie_page);
+ prefix_unmapped(vsie_page);
+ vsie_page->fault_addr = 0;
+ vsie_page->scb_s.ihcpu = 0xffffU;
+}
+
/*
* Get or create a vsie page for a scb address.
*
- * Returns: - address of a vsie page (cached or new one)
- * - NULL if the same scb address is already used by another VCPU
- * - ERR_PTR(-ENOMEM) if out of memory
+ * Original control blocks are pinned when the vsie_page pointing to them is
+ * returned.
+ * Newly created vsie_pages only have vsie_page->scb_gpa and vsie_page->sca_gpa
+ * set.
+ *
+ * Returns: - -EBUSY if the same scb address is already used by another VCPU
+ * - -ENOMEM if out of memory
*/
-static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
+static int get_vsie_page(struct kvm_vcpu *vcpu, unsigned long addr,
+ struct vsie_page **vsie_page_out)
{
- struct vsie_page *vsie_page;
- int nr_vcpus;
-
- rcu_read_lock();
- vsie_page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> SCB_ALIGNMENT_SHIFT);
- rcu_read_unlock();
- if (vsie_page) {
- if (try_get_vsie_page(vsie_page)) {
- if (vsie_page->scb_gpa == addr)
- return vsie_page;
- /*
- * We raced with someone reusing + putting this vsie
- * page before we grabbed it.
- */
- put_vsie_page(vsie_page);
+ struct vsie_page *vsie_page, *vsie_page_new = NULL;
+ struct kvm *kvm = vcpu->kvm;
+ unsigned int max_vsie_page;
+ int rc, pages_idx;
+
+ vsie_page = xa_load(&kvm->arch.vsie.addr_to_page, addr >> SCB_ALIGNMENT_SHIFT);
+ if (vsie_page && try_get_vsie_page(vsie_page)) {
+ if (vsie_page->scb_gpa == addr) {
+ *vsie_page_out = vsie_page;
+ return 0;
}
+ /*
+ * We raced with someone reusing + putting this vsie
+ * page before we grabbed it.
+ */
+ put_vsie_page(vsie_page);
}
- /*
- * We want at least #online_vcpus shadows, so every VCPU can execute
- * the VSIE in parallel.
- */
- nr_vcpus = atomic_read(&kvm->online_vcpus);
+ max_vsie_page = min_t(unsigned int, max_t(unsigned int, atomic_read(&kvm->online_vcpus),
+ vsie_shadow_scb_max), KVM_S390_MAX_VSIE_VCPUS);
+
+ /* allocate new vsie_page - we will likely need it */
+ if (kvm->arch.vsie.page_count < max_vsie_page) {
+ vsie_page_new = alloc_vsie_page(kvm);
+ if (!vsie_page_new)
+ return -ENOMEM;
+ __set_bit(VSIE_PAGE_IN_USE, &vsie_page_new->flags);
+ }
mutex_lock(&kvm->arch.vsie.mutex);
- if (kvm->arch.vsie.page_count < nr_vcpus) {
- vsie_page = (void *)__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA);
- if (!vsie_page) {
+ vsie_page = xa_load(&kvm->arch.vsie.addr_to_page, addr >> SCB_ALIGNMENT_SHIFT);
+ if (vsie_page && try_get_vsie_page(vsie_page)) {
+ if (vsie_page->scb_gpa == addr) {
mutex_unlock(&kvm->arch.vsie.mutex);
- return ERR_PTR(-ENOMEM);
+ if (vsie_page_new)
+ free_vsie_page(vsie_page_new);
+ *vsie_page_out = vsie_page;
+ return 0;
}
- __set_bit(VSIE_PAGE_IN_USE, &vsie_page->flags);
- kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = vsie_page;
+ /*
+ * We raced with someone reusing + putting this vsie
+ * page before we grabbed it.
+ */
+ put_vsie_page(vsie_page);
+ }
+
+ if (kvm->arch.vsie.page_count < max_vsie_page) {
+ pages_idx = kvm->arch.vsie.page_count;
+ vsie_page = vsie_page_new;
+ vsie_page_new = NULL;
+ WRITE_ONCE(kvm->arch.vsie.pages[kvm->arch.vsie.page_count], vsie_page);
kvm->arch.vsie.page_count++;
+ kvm->stat.vsie_shadow_scb++;
} else {
/* reuse an existing entry that belongs to nobody */
while (true) {
- vsie_page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
+ pages_idx = kvm->arch.vsie.next;
+ kvm->arch.vsie.next++;
+ kvm->arch.vsie.next %= kvm->arch.vsie.page_count;
+ vsie_page = kvm->arch.vsie.pages[pages_idx];
if (try_get_vsie_page(vsie_page))
break;
- kvm->arch.vsie.next++;
- kvm->arch.vsie.next %= nr_vcpus;
}
if (vsie_page->scb_gpa != ULONG_MAX)
- radix_tree_delete(&kvm->arch.vsie.addr_to_page,
- vsie_page->scb_gpa >> SCB_ALIGNMENT_SHIFT);
+ xa_erase(&kvm->arch.vsie.addr_to_page,
+ vsie_page->scb_gpa >> SCB_ALIGNMENT_SHIFT);
+ /* Mark it as invalid until it resides in the tree. */
+ vsie_page->scb_gpa = ULONG_MAX;
+
+ unpin_scb(kvm, vsie_page);
+ kvm->stat.vsie_shadow_scb_reuse++;
}
- /* Mark it as invalid until it resides in the tree. */
- vsie_page->scb_gpa = ULONG_MAX;
- /* Double use of the same address or allocation failure. */
- if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> SCB_ALIGNMENT_SHIFT,
- vsie_page)) {
- put_vsie_page(vsie_page);
- mutex_unlock(&kvm->arch.vsie.mutex);
- return NULL;
+ rc = init_vsie_page(vcpu, vsie_page, addr);
+ if (!rc) {
+ rc = xa_insert(&kvm->arch.vsie.addr_to_page, addr >> SCB_ALIGNMENT_SHIFT, vsie_page,
+ GFP_KERNEL_ACCOUNT);
+ if (rc == -EBUSY)
+ rc = -EAGAIN;
}
- vsie_page->scb_gpa = addr;
+
mutex_unlock(&kvm->arch.vsie.mutex);
+ if (vsie_page_new)
+ free_vsie_page(vsie_page_new);
+ if (rc) {
+ vsie_page->scb_gpa = ULONG_MAX;
+ put_vsie_page(vsie_page);
+ return rc;
+ }
- memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
- if (vsie_page->gmap_cache.gmap) {
- scoped_guard(spinlock, &kvm->arch.gmap->children_lock)
- if (vsie_page->gmap_cache.gmap)
- release_gmap_shadow(vsie_page);
+ reset_vsie_page(kvm, vsie_page);
+
+ *vsie_page_out = vsie_page;
+ return 0;
+}
+
+static int get_vsie_page_cpu_nr(struct kvm_vcpu *vcpu, struct vsie_sca *vsie_sca, gpa_t scb_gpa,
+ u16 cpu_nr, struct vsie_page **vsie_page_out)
+{
+ struct vsie_page *vsie_page, *vsie_page_new = NULL;
+ bool vsie_page_is_new = true;
+ int rc;
+
+ vsie_page = vsie_sca->pages[cpu_nr];
+ if (!vsie_page) {
+ vsie_page_new = alloc_vsie_page(vcpu->kvm);
+ if (!vsie_page_new)
+ return -ENOMEM;
+ vsie_page_new->vsie_sca = vsie_sca;
+ __set_bit(VSIE_PAGE_IN_USE, &vsie_page_new->flags);
+
+ /* be careful to not loose a page here if we raced */
+ scoped_guard(mutex, &vsie_sca->mutex) {
+ vsie_page = vsie_sca->pages[cpu_nr];
+ if (!vsie_page) {
+ WRITE_ONCE(vsie_sca->pages[cpu_nr], vsie_page_new);
+ vsie_page = vsie_page_new;
+ }
+ }
}
- prefix_unmapped(vsie_page);
- vsie_page->fault_addr = 0;
- vsie_page->scb_s.ihcpu = 0xffffU;
- return vsie_page;
+ if (vsie_page != vsie_page_new) {
+ vsie_page_is_new = false;
+ if (vsie_page_new)
+ free_vsie_page(vsie_page_new);
+
+ /* not a new vsie_page so get it */
+ if (!try_get_vsie_page(vsie_page))
+ return -EAGAIN;
+ vsie_page->vsie_sca = vsie_sca;
+ }
+ if (vsie_page->scb_gpa != scb_gpa || vsie_page->sca_gpa != vsie_sca->sca_gpa) {
+ scoped_guard(mutex, &vcpu->kvm->arch.vsie.mutex) {
+ unpin_scb(vcpu->kvm, vsie_page);
+ rc = init_vsie_page(vcpu, vsie_page, scb_gpa);
+ }
+ if (vsie_page_is_new)
+ vcpu->kvm->stat.vsie_shadow_scb++;
+ else
+ vcpu->kvm->stat.vsie_shadow_scb_reuse++;
+ if (rc) {
+ put_vsie_page(vsie_page);
+ return rc;
+ }
+
+ reset_vsie_page(vcpu->kvm, vsie_page);
+ }
+
+ *vsie_page_out = vsie_page;
+ return 0;
}
-int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
+/*
+ * Copy the mcn from the osca to the vsie_sca to be able to detect mcn changes later on.
+ *
+ * @vsie_sca: vsie_sca to copy mcn to.
+ * @sca: Pointer to a struct bsca_block or struct esca_block to read from.
+ */
+static void sca_mcn_copy(struct vsie_sca *vsie_sca, void *sca)
{
- struct vsie_page *vsie_page;
- unsigned long scb_addr;
+ int offset = offsetof(struct bsca_block, mcn);
+ int size = sizeof(unsigned long);
+
+ if (test_bit(VSIE_SCA_ESCA, &vsie_sca->flags)) {
+ offset = offsetof(struct esca_block, mcn);
+ size = size * 4;
+ }
+ memcpy(&vsie_sca->mcn, sca + offset, size);
+}
+
+/*
+ * Compare the mcn from the given sca to the vsie_sca to be able to detect mcn changes.
+ *
+ * @vsie_sca: vsie_sca to compare mcn to.
+ * @sca: Pointer to a struct bsca_block or struct esca_block to compare to.
+ */
+static bool sca_mcn_equals(struct vsie_sca *vsie_sca, void *sca)
+{
+ int offset = offsetof(struct bsca_block, mcn);
+ int size = sizeof(unsigned long);
+
+ if (test_bit(VSIE_SCA_ESCA, &vsie_sca->flags)) {
+ size = size * 4;
+ offset = offsetof(struct esca_block, mcn);
+ }
+
+ return !memcmp(&vsie_sca->mcn, sca + offset, size);
+}
+
+static void update_vsie_sca(struct vsie_sca *vsie_sca, unsigned int cpu_nr,
+ struct vsie_page *vsie_page_n, hpa_t sca_o_entry_hpa)
+{
+ guard(mutex)(&vsie_sca->mutex);
+
+ WRITE_ONCE(vsie_sca->ssca.cpu[cpu_nr].ssda, virt_to_phys(&vsie_page_n->scb_s));
+ WRITE_ONCE(vsie_sca->ssca.cpu[cpu_nr].ossea, sca_o_entry_hpa);
+ WRITE_ONCE(vsie_sca->pages[cpu_nr], vsie_page_n);
+}
+
+static int _shadow_sca_cpu(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
+ struct vsie_sca *vsie_sca, hpa_t sca_o_entry_hpa,
+ unsigned int cpu_nr, bool is_esca)
+{
+ hva_t sca_o_entry_hva = (hva_t)phys_to_virt(sca_o_entry_hpa);
+ struct vsie_page *vsie_page_n;
+ gpa_t scb_o_gpa;
+ int rc;
+
+ if (is_esca)
+ scb_o_gpa = ((struct esca_entry *)sca_o_entry_hva)->sda;
+ else
+ scb_o_gpa = ((struct bsca_entry *)sca_o_entry_hva)->sda;
+ if (scb_o_gpa & 0x1ffUL)
+ return set_validity_icpt(vsie_page->scb_o, 0x0001U);
+
+ rc = get_vsie_page_cpu_nr(vcpu, vsie_sca, scb_o_gpa, cpu_nr, &vsie_page_n);
+ if (rc)
+ return rc;
+
+ rc = shadow_scb_minimal(vcpu, vsie_page_n);
+ update_vsie_sca(vsie_sca, cpu_nr, vsie_page_n, sca_o_entry_hpa);
+ if (rc) {
+ /* copy intercept to primary scb_o, no unshadow_scb() on exit */
+ unshadow_intercept(vsie_page->scb_o, &vsie_page_n->scb_s);
+ rc = 1;
+ }
+ put_vsie_page(vsie_page_n);
+
+ return rc;
+}
+
+/* Fill the shadow system control area used for VSIE SIGPI. */
+static int _shadow_sca(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
+ struct vsie_sca *vsie_sca)
+{
+ bool is_esca = sie_uses_esca(vsie_page->scb_o);
+ unsigned int cpu_nr, cpu_slots;
+ hpa_t sca_o_entry_hpa;
int rc;
+ if (is_esca)
+ set_bit(VSIE_SCA_ESCA, &vsie_sca->flags);
+ else
+ clear_bit(VSIE_SCA_ESCA, &vsie_sca->flags);
+ sca_mcn_copy(vsie_sca, phys_to_virt(sca_o_hpa(vsie_sca)));
+
+ /* pin and make minimal shadow for ALL scb in the sca */
+ cpu_slots = is_esca ? KVM_S390_MAX_VSIE_VCPUS : KVM_S390_BSCA_CPU_SLOTS;
+ for_each_set_bit_inv(cpu_nr, (unsigned long *)&vsie_sca->mcn, cpu_slots) {
+ rc = get_sca_entry_addr(vcpu->kvm, vsie_sca, cpu_nr, NULL, &sca_o_entry_hpa);
+ if (rc)
+ break;
+
+ if ((vsie_page->scb_o->icpua & KVM_S390_CPU_MASK) == cpu_nr) {
+ update_vsie_sca(vsie_sca, cpu_nr, vsie_page, sca_o_entry_hpa);
+ continue;
+ }
+
+ rc = _shadow_sca_cpu(vcpu, vsie_page, vsie_sca, sca_o_entry_hpa, cpu_nr, is_esca);
+ if (rc)
+ break;
+ }
+
+ if (rc) {
+ vsie_sca->ssca.osca = 0;
+ for_each_set_bit_inv(cpu_nr, (unsigned long *)&vsie_sca->mcn, cpu_slots) {
+ vsie_sca->ssca.cpu[cpu_nr].ssda = 0;
+ vsie_sca->ssca.cpu[cpu_nr].ossea = 0;
+ }
+ } else {
+ vsie_sca->ssca.osca = sca_o_hpa(vsie_sca);
+ }
+ return rc;
+}
+
+static bool shadow_sca_needed(struct vsie_sca *vsie_sca)
+{
+ if (!vsie_sca->ssca.osca)
+ return true;
+ return !sca_mcn_equals(vsie_sca, phys_to_virt(sca_o_hpa(vsie_sca)));
+}
+
+/* Shadow or reshadow the SCA on VSIE enter. */
+static int shadow_sca(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct vsie_sca *vsie_sca)
+{
+ scoped_guard(rwsem_read, &vcpu->kvm->arch.vsie.vsie_sca_lock) {
+ if (!shadow_sca_needed(vsie_sca))
+ return 0;
+ }
+
+ guard(rwsem_write)(&vcpu->kvm->arch.vsie.vsie_sca_lock);
+ if (!shadow_sca_needed(vsie_sca))
+ return 0;
+
+ return _shadow_sca(vcpu, vsie_page, vsie_sca);
+}
+
+int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
+{
+ struct vsie_page *vsie_page = NULL;
+ struct vsie_sca *vsie_sca = NULL;
+ struct kvm_s390_sie_block *scb_o;
+ gpa_t scb_addr;
+ hpa_t scb_hpa;
+ int rc = 0;
+
vcpu->stat.instruction_sie++;
if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
return -EOPNOTSUPP;
@@ -1576,35 +2306,60 @@ int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
return 0;
}
- vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
- if (IS_ERR(vsie_page)) {
- return PTR_ERR(vsie_page);
- } else if (!vsie_page) {
- /* double use of sie control block - simply do nothing */
- kvm_s390_rewind_psw(vcpu, 4);
- return 0;
+ rc = pin_guest_page(vcpu->kvm, scb_addr, &scb_hpa);
+ if (rc)
+ return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
+ scb_o = (struct kvm_s390_sie_block *)phys_to_virt(scb_hpa);
+
+ if (!use_ssca(vcpu->kvm, scb_o)) {
+ /* get the vsie_page with pinned scb_o */
+ rc = get_vsie_page(vcpu, scb_addr, &vsie_page);
+ if (rc)
+ goto out_unpin;
+ vsie_page->vsie_sca = NULL;
+ } else {
+ /* get the vsie_sca with pinned original sca */
+ rc = get_vsie_sca(vcpu, scb_o, &vsie_sca);
+ if (rc)
+ goto out_unpin;
+ rc = get_vsie_page_cpu_nr(vcpu, vsie_sca, scb_addr,
+ scb_o->icpua & KVM_S390_CPU_MASK, &vsie_page);
+ if (rc)
+ goto out_put_sca;
}
- rc = pin_scb(vcpu, vsie_page, scb_addr);
- if (rc)
- goto out_put;
rc = shadow_scb(vcpu, vsie_page);
if (rc)
- goto out_unpin_scb;
+ goto out_put;
+ if (vsie_sca) {
+ /* pin and shadow the sca including all scb_o in the g3 conf */
+ rc = shadow_sca(vcpu, vsie_page, vsie_sca);
+ if (rc)
+ goto out_put;
+ }
+
rc = pin_blocks(vcpu, vsie_page);
if (rc)
goto out_unshadow;
register_shadow_scb(vcpu, vsie_page);
+
rc = vsie_run(vcpu, vsie_page);
+
unregister_shadow_scb(vcpu);
unpin_blocks(vcpu, vsie_page);
out_unshadow:
unshadow_scb(vcpu, vsie_page);
-out_unpin_scb:
- unpin_scb(vcpu, vsie_page, scb_addr);
out_put:
put_vsie_page(vsie_page);
+out_put_sca:
+ put_vsie_sca(vsie_sca);
+out_unpin:
+ unpin_guest_page(vcpu->kvm, scb_addr, scb_hpa);
+ if (rc == -EAGAIN) {
+ kvm_s390_rewind_psw(vcpu, 4);
+ rc = 0;
+ }
return rc < 0 ? rc : 0;
}
@@ -1612,30 +2367,55 @@ int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
void kvm_s390_vsie_init(struct kvm *kvm)
{
mutex_init(&kvm->arch.vsie.mutex);
- INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL_ACCOUNT);
+ xa_init_flags(&kvm->arch.vsie.addr_to_page, XA_FLAGS_ACCOUNT);
+ init_rwsem(&kvm->arch.vsie.vsie_sca_lock);
+ xa_init_flags(&kvm->arch.vsie.osca_to_sca, XA_FLAGS_ACCOUNT);
+}
+
+static void kvm_s390_vsie_destroy_page(struct kvm *kvm, struct vsie_page *vsie_page)
+{
+ unpin_scb(kvm, vsie_page);
+ release_gmap_shadow_safe(kvm, vsie_page);
+ free_vsie_page(vsie_page);
}
/* Destroy the vsie data structures. To be called when a vm is destroyed. */
void kvm_s390_vsie_destroy(struct kvm *kvm)
{
struct vsie_page *vsie_page;
- int i;
+ struct vsie_sca *vsie_sca;
+ int i, cpu_nr;
+
+ guard(mutex)(&kvm->arch.vsie.mutex);
- mutex_lock(&kvm->arch.vsie.mutex);
for (i = 0; i < kvm->arch.vsie.page_count; i++) {
vsie_page = kvm->arch.vsie.pages[i];
- scoped_guard(spinlock, &kvm->arch.gmap->children_lock)
- if (vsie_page->gmap_cache.gmap)
- release_gmap_shadow(vsie_page);
kvm->arch.vsie.pages[i] = NULL;
- /* free the radix tree entry */
- if (vsie_page->scb_gpa != ULONG_MAX)
- radix_tree_delete(&kvm->arch.vsie.addr_to_page,
- vsie_page->scb_gpa >> SCB_ALIGNMENT_SHIFT);
- free_page((unsigned long)vsie_page);
+ kvm_s390_vsie_destroy_page(kvm, vsie_page);
}
+
kvm->arch.vsie.page_count = 0;
- mutex_unlock(&kvm->arch.vsie.mutex);
+ for (i = 0; i < kvm->arch.vsie.sca_count; i++) {
+ vsie_sca = kvm->arch.vsie.scas[i];
+ kvm->arch.vsie.scas[i] = NULL;
+ if (!vsie_sca)
+ continue;
+
+ for (cpu_nr = 0; cpu_nr < KVM_S390_MAX_VSIE_VCPUS; cpu_nr++) {
+ vsie_page = vsie_sca->pages[cpu_nr];
+ vsie_sca->pages[cpu_nr] = NULL;
+ if (!vsie_page)
+ continue;
+ unpin_scb(kvm, vsie_page);
+ kvm_s390_vsie_destroy_page(kvm, vsie_page);
+ }
+
+ unpin_sca(kvm, vsie_sca);
+ free_vsie_sca(kvm, vsie_sca);
+ }
+ kvm->arch.vsie.sca_count = 0;
+ xa_destroy(&kvm->arch.vsie.addr_to_page);
+ xa_destroy(&kvm->arch.vsie.osca_to_sca);
}
void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
diff --git a/drivers/s390/char/sclp_early.c b/drivers/s390/char/sclp_early.c
index 22dd797e62291..40e6e346ee083 100644
--- a/drivers/s390/char/sclp_early.c
+++ b/drivers/s390/char/sclp_early.c
@@ -56,6 +56,7 @@ static void __init sclp_early_facilities_detect(void)
sclp.has_diag318 = !!(sccb->byte_134 & 0x80);
sclp.has_diag320 = !!(sccb->byte_134 & 0x04);
sclp.has_iplcc = !!(sccb->byte_134 & 0x02);
+ sclp.has_vsie_interp_extf = !!(sccb->byte_134 & 0x01);
}
if (sccb->cpuoff > 137) {
sclp.has_sipl = !!(sccb->cbl & 0x4000);
diff --git a/tools/testing/selftests/kvm/include/s390/sie.h b/tools/testing/selftests/kvm/include/s390/sie.h
index 160acd4a1db92..4ff1c1a354af5 100644
--- a/tools/testing/selftests/kvm/include/s390/sie.h
+++ b/tools/testing/selftests/kvm/include/s390/sie.h
@@ -223,7 +223,7 @@ struct kvm_s390_sie_block {
__u32 fac; /* 0x01a0 */
__u8 reserved1a4[20]; /* 0x01a4 */
__u64 cbrlo; /* 0x01b8 */
- __u8 reserved1c0[8]; /* 0x01c0 */
+ __u64 osda; /* 0x01c0 */
#define ECD_HOSTREGMGMT 0x20000000
#define ECD_MEF 0x08000000
#define ECD_ETOKENF 0x02000000
|
| 17/3 | 2026/08/27 16:17 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 26874 output: 257 thoughts: 5618 Thoughts: **KMSAN Assessment: s390 Nested Virtualization (VSIE) SCA Patch** Okay, let's break down this patch for the s390 KVM code. This patch introduces significant changes related to nested virtualization, specifically dealing with the Shadow System Control Area (SSCA) for VSIE SIGPI and SRSI, and I need to assess whether Kernel Memory Sanitizer (KMSAN) is necessary here. My initial thought is that since this focuses on internal KVM structures and hardware interaction, the likelihood of uninitialized memory leakage to user space or the guest is low, but I need to be *sure*. The patch introduces `struct vsie_sca` and modifies `kvm_s390_vsie` to manage an array of SCAs (`scas`) and an xarray (`osca_to_sca`). The key here is understanding how these structures are allocated, used, and, crucially, *cleared* upon reuse. I've noted the allocation strategy involves `alloc_pages_exact` and `__get_free_page`, both with `__GFP_ZERO`. This is good, ensuring new memory is zero-initialized and reducing the chance of accidental information leakage. I'm particularly interested in what's copied to userspace and how any changes impact the existing hardware SIE control block (`struct kvm_s390_sie_block`). My expectation, given the nature of VSIE, is that `kvm_s390_sie_block` is a shadow structure shared with the hardware. It isn't directly exposed to userspace, but accessed via KVM_GET_SREGS or similar interfaces, potentially making it vulnerable. However, upon reviewing, the `kvm_s390_sie_block` is, in fact, mapped to guest memory in nested virtualization. The shadow structure `vsie_page` is where the shadow SIE block is allocated with `__get_free_page`, also with the `__GFP_ZERO` flag. Crucially, the *original* SIE block is in guest memory. Next, I need to verify that when these structures are *reused*, there aren't any leftover values that could expose sensitive information to either the guest or a potential malicious actor. Specifically, I'm analyzing the zeroing and clearing logic for `vsie_sca` (using `clear_vsie_sca`) and `vsie_page` (using `reset_vsie_page`). The `clear_vsie_sca` function correctly zeros the `head` and `tail` of the `vsie_sca` struct, while retaining `keep`. Critically, `reset_vsie_page` zeros out the `scb_s` field of `vsie_page`. However, because `vsie_page` is cached and reused, I need to confirm that no data from previous executions of VSIE is left behind. Analyzing `shadow_scb` which is the code responsible for copying the shadow SIE Block is important here. `shadow_scb_minimal` appears to be the most relevant and it explicitly zeros some key fields of the SIE Block. Given that `reset_vsie_page` zeroes the *entire* `scb_s` and `vsie_page` is correctly zeroed, or that `shadow_scb_minimal` sets specific fields to zero, uninitialized memory leaks seem unlikely. I'm also paying close attention to any stack variables that might not be initialized. My investigation focused on functions such as `_shadow_sca_cpu`, `get_vsie_page_cpu_nr`, `get_vsie_sca`, and `shadow_scb`. All the variables are properly initialized, indicating that uninitialized variables on the stack are not a concern. Looking at functions like `pin_guest_pages`, `get_sca_entry_addr`, `sca_mcn_copy`, and `sca_mcn_equals`, I need to confirm whether any part of the SCA is copied to or from user space or even passed along to the hardware. All memory allocations and accesses, including within these functions, are properly handled. The crucial part here is the `ssca_block` within `vsie_sca`. This is passed to the hardware. Since `vsie_sca` is zero-allocated and updated correctly, no uninitialized memory makes its way to the hardware. In conclusion, the patch introduces new structures and logic related to VSIE for s390, specifically for shadowing the SCA. All the allocations use proper zero-initialization or explicit `memset` calls before reuse, and the patch deals exclusively with internal KVM and hardware interaction. There are no new calls to `copy_to_user` or equivalent mechanisms that would directly expose uninitialized memory to user space. Given that everything is zero |
| 18/3 | 2026/08/27 16:18 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch introduces new structures (`vsie_sca`, `vsie_page`) and logic for shadowing the System Control Area (SCA) in s390 KVM nested virtualization (VSIE). All new memory allocations for these structures explicitly use `__GFP_ZERO` (e.g., `alloc_pages_exact(..., GFP_KERNEL_ACCOUNT | __GFP_ZERO)` and `__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA)`). When these structures are reused from caches, they are explicitly cleared using `memset` (e.g., in `clear_vsie_sca` and `reset_vsie_page`). The patch does not introduce any new data copies to userspace, and all hardware-shared shadow structures are fully zero-initialized before use. Any potential bugs introduced by this patch would be logic errors, out-of-bounds accesses, or use-after-frees, which are effectively caught by KASAN. There is no risk of uninitialized memory usage or info-leaks, making a dedicated KMSAN session unnecessary.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch introduces new structures (`vsie_sca`, `vsie_page`) and logic for shadowing the System Control Area (SCA) in s390 KVM nested virtualization (VSIE). All new memory allocations for these structures explicitly use `__GFP_ZERO` (e.g., `alloc_pages_exact(..., GFP_KERNEL_ACCOUNT | __GFP_ZERO)` and `__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA)`). When these structures are reused from caches, they are explicitly cleared using `memset` (e.g., in `clear_vsie_sca` and `reset_vsie_page`). The patch does not introduce any new data copies to userspace, and all hardware-shared shadow structures are fully zero-initialized before use. Any potential bugs introduced by this patch would be logic errors, out-of-bounds accesses, or use-after-frees, which are effectively caught by KASAN. There is no risk of uninitialized memory usage or info-leaks, making a dedicated KMSAN session unnecessary.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|