From patchwork Tue Jan 7 15:43:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13929211 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB3531EB9FA for ; Tue, 7 Jan 2025 15:43:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736264634; cv=none; b=Nz6OATcwLr0v1odW5hL3I8rc/3tnmoofJ5ye06nXKvlDOdQPGx7Aw3eWRLHheoDRJqVW8ZjXEwZe3iNzuxL3IOltSXMiM4aDa4QzNynzFPJnbgujS+uH4KsTR8L7kYbMGWjSoEclubphlaWgoPWjbJTV7ZNtCY9WsjIlAN1WJXs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736264634; c=relaxed/simple; bh=kff/5MuiUSshICdL0Pz3Kwaact1TDCjVUg2HjOr3gSg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=f1L/xBjVXgDQOgYuMIb8kGSodt5LHGP7QR/b2ZDooLE0DdDU1UEeLkU+umNy4vqtGVP2+pY3XZaswzXfAc3IVp5QyC6AbKg3EpyhhFmpruOADHJEIa/As5M3PgiqbwLUr26Scz9mfuHi1TVXWXf0x+ScxVTzFAzqnQvvbiQ5Vdw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=SQnv+BdO; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="SQnv+BdO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736264632; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=be/xDpPjneQ7+z88xrOgXMm6ql8CC9vSv4tySFvSNk8=; b=SQnv+BdOjsYYMfAU2LTzbh9oU5RB3hydAxe09kyLYetiOA2zEWfjltjxRpParD5da54MFa PIDQow1CAtNTIJa9cnJK6EWFHIvcBAUXBaOKK91/foy7+TBFYAlyaYme6Me/N25II4esRs gC56lSS6B5yivZYLBmKTVvN+ZMxsvlA= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-342-Pg2IWnzgNLCmUUmTJjKLZw-1; Tue, 07 Jan 2025 10:43:50 -0500 X-MC-Unique: Pg2IWnzgNLCmUUmTJjKLZw-1 X-Mimecast-MFC-AGG-ID: Pg2IWnzgNLCmUUmTJjKLZw Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-38a684a096eso1709415f8f.2 for ; Tue, 07 Jan 2025 07:43:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736264629; x=1736869429; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=be/xDpPjneQ7+z88xrOgXMm6ql8CC9vSv4tySFvSNk8=; b=W0morg6fPOzjS93+yKHy17PlGee26vW5s2+2FZ7M7t9PJtiXA6W4epWNgF0kCpF4FU 8Q+04p4VFJvbKSUj67RuuJNo7vSYOenA5dAB70x7jc25Au5ujntw5CEV/pHAZ01DhnqZ E4/c/gUezueBi84prjBqdelqf6MrhxzVjI+Fl/wXiwRh3bIrdocQ2co5xG5g/M7QG5nV mQ1gHhCuz4kUNhpU88skMU02P3hjFkAQ3IbX42mVFbJ3dh7nIfKeuFRZbZB1s8aCf+Ln e69+pc9VFHPqjletrZRCANq5NOMkyZPjO0a5AZJW7DK2uM1FlGvQr0Xg8B7y1Rx5f6QY ORsQ== X-Gm-Message-State: AOJu0Yzq9RBm9SD5k55CxJQuZyWBjerm6ZDAZMBHV4xJwiLMYmzJP9Qh wbojo7fkfaWxqqdjIOzgb1+rvKVPcqR1tGufzKxZlafJHdTfwq1wwrnysTD7DyPGaa8mASpGlVm 5qYyGyYzleIQUt+h5fj2CUuMtUXv+Rb1Elc5Sr0OVYYAQlVNpXg== X-Gm-Gg: ASbGncuiRcGxR+H1L+tIYNPBGjm0MKT9pyvP3wNL8WYsgcB6vS6AUVEzebBLmlSXWYU Vfo61D7vq+91P5rIgB2fO3II1SMPPKvabyiyY41FOkjbbYeQ2YQfJ2/IA2o2VHOWcEAGdZIYjGt ZxEFZpALCvPa9QoEfyfO6BaBdLF1ELiQZhHUdDwA+6r8Q5qME/1CPSV0hl4IxOUHI1J6kwVWCxu pBWQIJQvrTEo0EIXVA9M9+0z751GHUbKOGZjYh/UDFNghSrzgBHbMvnUJzDQI3L00cmPpBdwOQg 7RpafbKZK5U6DMrqNrc3MM4aVR2Ra4rvqryV7eKOmA== X-Received: by 2002:a05:6000:1f88:b0:386:4034:f9a0 with SMTP id ffacd0b85a97d-38a22408cbemr49437409f8f.52.1736264629542; Tue, 07 Jan 2025 07:43:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IECHU3QXEd9DSMtt+gL3jnHOKISExr8ZSLHane3TsnWNkBR9T163eXOTOHhVFPDdEe315KvNw== X-Received: by 2002:a05:6000:1f88:b0:386:4034:f9a0 with SMTP id ffacd0b85a97d-38a22408cbemr49437388f8f.52.1736264629175; Tue, 07 Jan 2025 07:43:49 -0800 (PST) Received: from localhost (p200300cbc719170056dc6a88b509d3f3.dip0.t-ipconnect.de. [2003:cb:c719:1700:56dc:6a88:b509:d3f3]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38a1c828d39sm52310207f8f.9.2025.01.07.07.43.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Jan 2025 07:43:48 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, linux-s390@vger.kernel.org, David Hildenbrand , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Sven Schnelle , Thomas Huth , "Matthew Wilcox (Oracle)" , stable@vger.kernel.org Subject: [PATCH v1 1/4] KVM: s390: vsie: fix some corner-cases when grabbing vsie pages Date: Tue, 7 Jan 2025 16:43:41 +0100 Message-ID: <20250107154344.1003072-2-david@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250107154344.1003072-1-david@redhat.com> References: <20250107154344.1003072-1-david@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 We try to reuse the same vsie page when re-executing the vsie with a given SCB address. The result is that we use the same shadow SCB -- residing in the vsie page -- and can avoid flushing the TLB when re-running the vsie on a CPU. So, when we allocate a fresh vsie page, or when we reuse a vsie page for a different SCB address -- reusing the shadow SCB in different context -- we set ihcpu=0xffff to trigger the flush. However, after we looked up the SCB address in the radix tree, but before we grabbed the vsie page by raising the refcount to 2, someone could reuse the vsie page for a different SCB address, adjusting page->index and the radix tree. In that case, we would be reusing the vsie page with a wrong page->index. Another corner case is that we might set the SCB address for a vsie page, but fail the insertion into the radix tree. Whoever would reuse that page would remove the corresponding radix tree entry -- which might now be a valid entry pointing at another page, resulting in the wrong vsie page getting removed from the radix tree. Let's handle such races better, by validating that the SCB address of a vsie page didn't change after we grabbed it (not reuse for a different SCB; the alternative would be performing another tree lookup), and by setting the SCB address to invalid until the insertion in the tree succeeded (SCB addresses are aligned to 512, so ULONG_MAX is invalid). These scenarios are rare, the effects a bit unclear, and these issues were only found by code inspection. Let's CC stable to be safe. Fixes: a3508fbe9dc6 ("KVM: s390: vsie: initial support for nested virtualization") Cc: stable@vger.kernel.org Signed-off-by: David Hildenbrand --- arch/s390/kvm/vsie.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/arch/s390/kvm/vsie.c b/arch/s390/kvm/vsie.c index 150b9387860ad..0fb527b33734c 100644 --- a/arch/s390/kvm/vsie.c +++ b/arch/s390/kvm/vsie.c @@ -1362,8 +1362,14 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9); rcu_read_unlock(); if (page) { - if (page_ref_inc_return(page) == 2) - return page_to_virt(page); + if (page_ref_inc_return(page) == 2) { + if (page->index == addr) + return page_to_virt(page); + /* + * We raced with someone reusing + putting this vsie + * page before we grabbed it. + */ + } page_ref_dec(page); } @@ -1393,15 +1399,20 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) kvm->arch.vsie.next++; kvm->arch.vsie.next %= nr_vcpus; } - radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9); + if (page->index != ULONG_MAX) + radix_tree_delete(&kvm->arch.vsie.addr_to_page, + page->index >> 9); } - page->index = addr; - /* double use of the same address */ + /* Mark it as invalid until it resides in the tree. */ + page->index = ULONG_MAX; + + /* Double use of the same address or allocation failure. */ if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) { page_ref_dec(page); mutex_unlock(&kvm->arch.vsie.mutex); return NULL; } + page->index = addr; mutex_unlock(&kvm->arch.vsie.mutex); vsie_page = page_to_virt(page); @@ -1496,7 +1507,9 @@ void kvm_s390_vsie_destroy(struct kvm *kvm) vsie_page = page_to_virt(page); release_gmap_shadow(vsie_page); /* free the radix tree entry */ - radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9); + if (page->index != ULONG_MAX) + radix_tree_delete(&kvm->arch.vsie.addr_to_page, + page->index >> 9); __free_page(page); } kvm->arch.vsie.page_count = 0; From patchwork Tue Jan 7 15:43:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13929213 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE3881F37AF for ; Tue, 7 Jan 2025 15:43:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736264637; cv=none; b=o2tTwAJs7WH9JDLlk7DkeFsaEcQSYdCoKtjMuBeO8kVctcrv1R26BJtuEeBb7KLXxqnmXU6WnjjUZa+1cJ/qnBZuBFF/DIbnWo2WwX1vc4zzbfhVcQ95HZAn831LmEMGSeCZUQrvrK3VrZqingHCLrhseW2Itv0ktsnEUaENxRE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736264637; c=relaxed/simple; bh=us/gybqfWlRp+ZsJNbaLvVR85yeT4xyjGChYxOQOMFw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RZO8gpDra1wuBRY17nOmwgMVCkTJ6Dn/yusytEWEjEFpURQaOAZqblToaiaczosgHMg835pGjmPwg3SEsYmwWBqKQWxYFlFXnyTmo87YyqrlvgcRkt6NuPO926Zy+qDOE3RuXTe+R55UDaR2Pz1wyvXmJDd0jqKJ9byAntgboCc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=HYZ33jT3; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="HYZ33jT3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736264634; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1wRqChhOxLKlA65I6C1ssgvb1d066+xZqhJzJYv/kMc=; b=HYZ33jT3YtKuWE5GFjAWOqniWXM590XGA0yHePaPXlWci4P2xIzjvrHjEflgcF84l2Eed7 vHIRGnVMDHgacVEzY9bOFaAg2lOlgozI2M4vXg8Eu0IfFURf4ZaxjmEE+H1A1XD+4Kthwf RBVHOn9RkCqlcbr+at2xxS8t8nivLB8= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-615-mHVJKdGdMvK7PYcooSEyjA-1; Tue, 07 Jan 2025 10:43:52 -0500 X-MC-Unique: mHVJKdGdMvK7PYcooSEyjA-1 X-Mimecast-MFC-AGG-ID: mHVJKdGdMvK7PYcooSEyjA Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-385d51ba2f5so6658294f8f.2 for ; Tue, 07 Jan 2025 07:43:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736264631; x=1736869431; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1wRqChhOxLKlA65I6C1ssgvb1d066+xZqhJzJYv/kMc=; b=nsR5qsmZs//2G8uXQw2TDIQlxfYnwHTAUxGIISamsEuyhhryfmpdTD4DJcOXPoNmK0 i3fC9JUE4Za0na3+wq9UTfYs+vEMnJyIwvDZojr3oTAv9Cth4PUMC8BTwlbGtS91hyRt S2M/hnR4wluZgeL7WS2dReQFOoS8ukPXeya1e8bCGrmBcS04y43JWL4p1FCVM3AIRXvz eorhZShSOL4gJeVSAsQfyoF4lgCMcS4KOFQtEnx4jpSd/pTaN4o410VNLXxNW3E1s8y+ ra4FSpmRRvjCCTNng3Gm93FG5gPkEpgWe/Ze5tVtgqtyMFdUF03lZOIEJOnBDO8hfcrm a7Ww== X-Gm-Message-State: AOJu0YwY2H1LkkDtC4J04CgRFTq5DbcWYD4cSfrjA9zSGIPPunFs2jpt d50Kw6Ib1go5jLA1Mlk8kJpo5SieeEcS4iOmFD1bdajd7vKoPHcFQgBs80CBQg0thhysTx/SLdU fo82bsqiyXnpEELBO82XiqqdLm6Hmv50KBzAc6C0kwpzqnYHyPQ== X-Gm-Gg: ASbGncsNQQGIyMnLPo3/9NEpf6Hq1VYWWa0c9KCWZQq5RR8A65nitFT2QxMDroB8WMZ spdJdD+Y7IfzcVKnhuvknDrEgB4+DVz+YDj5pXw8ySgcG0NN3srPAdLwamhmktoib8BuVffcY+h NG6tJkSsTJ1POMjRotqCHyco9XyHDDQEq5RPMmglG3ODKCCvUq3KJnmgaeMLbeBwcucBSOxTC+6 7M7MonXKdASFrF1LnzqlFxwCclB7N3tsyGwTy9Um7jJOCJRpo0ZeMP3qEQ6+I8yofThP+sxXkw3 geJJ+9j83nqak4T78OTeJQMxKqV9kUWfXAAtzCmkbg== X-Received: by 2002:a5d:64c9:0:b0:385:f17b:de54 with SMTP id ffacd0b85a97d-38a221f11f9mr58228928f8f.5.1736264631019; Tue, 07 Jan 2025 07:43:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IHLoECKEzTyvVm/DKjeX8M+3c6FmHlgXx3op9ZadVYNTen3ZEo0J6qV8cTI63InkqFC8SLJEA== X-Received: by 2002:a5d:64c9:0:b0:385:f17b:de54 with SMTP id ffacd0b85a97d-38a221f11f9mr58228911f8f.5.1736264630656; Tue, 07 Jan 2025 07:43:50 -0800 (PST) Received: from localhost (p200300cbc719170056dc6a88b509d3f3.dip0.t-ipconnect.de. [2003:cb:c719:1700:56dc:6a88:b509:d3f3]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38a1c89e140sm51822558f8f.79.2025.01.07.07.43.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Jan 2025 07:43:50 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, linux-s390@vger.kernel.org, David Hildenbrand , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Sven Schnelle , Thomas Huth , "Matthew Wilcox (Oracle)" Subject: [PATCH v1 2/4] KVM: s390: vsie: stop using page->index Date: Tue, 7 Jan 2025 16:43:42 +0100 Message-ID: <20250107154344.1003072-3-david@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250107154344.1003072-1-david@redhat.com> References: <20250107154344.1003072-1-david@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Let's stop using page->index, and instead use a field inside "struct vsie_page" to hold that value. We have plenty of space left in there. This is one part of stopping using "struct page" when working with vsie pages. We place the "page_to_virt(page)" strategically, so the next cleanups requires less churn. Signed-off-by: David Hildenbrand --- arch/s390/kvm/vsie.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/arch/s390/kvm/vsie.c b/arch/s390/kvm/vsie.c index 0fb527b33734c..00cd9a27fd8fc 100644 --- a/arch/s390/kvm/vsie.c +++ b/arch/s390/kvm/vsie.c @@ -46,7 +46,13 @@ struct vsie_page { gpa_t gvrd_gpa; /* 0x0240 */ gpa_t riccbd_gpa; /* 0x0248 */ gpa_t sdnx_gpa; /* 0x0250 */ - __u8 reserved[0x0700 - 0x0258]; /* 0x0258 */ + /* + * 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. + */ + gpa_t scb_gpa; /* 0x0258 */ + __u8 reserved[0x0700 - 0x0260]; /* 0x0260 */ struct kvm_s390_crypto_cb crycb; /* 0x0700 */ __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */ }; @@ -1362,9 +1368,10 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9); rcu_read_unlock(); if (page) { + vsie_page = page_to_virt(page); if (page_ref_inc_return(page) == 2) { - if (page->index == addr) - return page_to_virt(page); + if (vsie_page->scb_gpa == addr) + return vsie_page; /* * We raced with someone reusing + putting this vsie * page before we grabbed it. @@ -1386,6 +1393,7 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) mutex_unlock(&kvm->arch.vsie.mutex); return ERR_PTR(-ENOMEM); } + vsie_page = page_to_virt(page); page_ref_inc(page); kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page; kvm->arch.vsie.page_count++; @@ -1393,18 +1401,19 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) /* reuse an existing entry that belongs to nobody */ while (true) { page = kvm->arch.vsie.pages[kvm->arch.vsie.next]; + vsie_page = page_to_virt(page); if (page_ref_inc_return(page) == 2) break; page_ref_dec(page); kvm->arch.vsie.next++; kvm->arch.vsie.next %= nr_vcpus; } - if (page->index != ULONG_MAX) + if (vsie_page->scb_gpa != ULONG_MAX) radix_tree_delete(&kvm->arch.vsie.addr_to_page, - page->index >> 9); + vsie_page->scb_gpa >> 9); } /* Mark it as invalid until it resides in the tree. */ - page->index = ULONG_MAX; + 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 >> 9, page)) { @@ -1412,10 +1421,9 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) mutex_unlock(&kvm->arch.vsie.mutex); return NULL; } - page->index = addr; + vsie_page->scb_gpa = addr; mutex_unlock(&kvm->arch.vsie.mutex); - vsie_page = page_to_virt(page); memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block)); release_gmap_shadow(vsie_page); vsie_page->fault_addr = 0; @@ -1507,9 +1515,9 @@ void kvm_s390_vsie_destroy(struct kvm *kvm) vsie_page = page_to_virt(page); release_gmap_shadow(vsie_page); /* free the radix tree entry */ - if (page->index != ULONG_MAX) + if (vsie_page->scb_gpa != ULONG_MAX) radix_tree_delete(&kvm->arch.vsie.addr_to_page, - page->index >> 9); + vsie_page->scb_gpa >> 9); __free_page(page); } kvm->arch.vsie.page_count = 0; From patchwork Tue Jan 7 15:43:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13929214 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB6191F3D2C for ; Tue, 7 Jan 2025 15:43:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736264638; cv=none; b=knt36YiQMS1+RSnzznEJDIoGNLYjkdc6PmsB37YXMsL3Te2lLRLnGvO+Ek7z6IVJu7fYS7L7HAZm61tb/JdsjeLECdnUF0K4mcQFX+EQ1cJPDM13iQDQCGQvJ/pkda7OooT7+2I3I1aT3TRlGjXka9aTy1oFcsMEaCXajPGuwOk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736264638; c=relaxed/simple; bh=L+CPO+OXcYMowj2nsp4a3RdcFfWPLoQzNe/QJrtHqCA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IKTfcCUL6X8LeK2EKxABEIDbvwXFzOthImgE4Bxo3okSYMeIWZ043SwhVVv1XO8AtDNDAQ4+GORKOwnBQ/g14sS8aojdiLpJcP6vcFJ6ZzjJCoSSxIC+5+fGJ4ok9Tk8oaq08CTdJZQ8RyIvLDVwdQHGpDm99jRiTr2Ea4Jxp7k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=LGGy7j+2; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="LGGy7j+2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736264635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SuA9xO+YQCmgV3vJM4yzlrCIirQZOm8j6HpoB+CK45c=; b=LGGy7j+2NHTxiXI5KJc4KCzHuvYbpGoPebrc/FtyhdL6CKZ+0rSSkd5JxYndSYqasL+s5u FLFGmLnoi1BTLxHgRF7mULpNCvwI4ILzlxiKeglv6ng0RtAxI7ErLXhEgqW1kHpnz5nFte c8L+qVVnJlwdBAtUAsIFPSzlvynl1Ic= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-677-C94_rJvfNlWvxgqQcm3T-g-1; Tue, 07 Jan 2025 10:43:54 -0500 X-MC-Unique: C94_rJvfNlWvxgqQcm3T-g-1 X-Mimecast-MFC-AGG-ID: C94_rJvfNlWvxgqQcm3T-g Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4362153dcd6so44022105e9.2 for ; Tue, 07 Jan 2025 07:43:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736264633; x=1736869433; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SuA9xO+YQCmgV3vJM4yzlrCIirQZOm8j6HpoB+CK45c=; b=wdsJf380L1+sFv9coERjpVPTkOEG+0kVfe6B1VBrXQnBeavYDyVlRBECx1kcOLkF6u d+UDqsYjhx4ALz283LTa4GY6W5YzWyDLvFmho/REGg+9bD/RUzPmEvmEfRovHj1TSjrg cp/LWaZBIIlG9jTZY1RfhCFCVOlZzWxJIcop5ZYxV2GmMrBpbIyxxqjIcLO07wFw7/rq YrDQZiWYZXwtxM3MJN3TbJ+qsGrt2/+cUSTPHg/sirR6Mztg7VVdl8S9/9LDSi//jVHq mykMWfZAva0sMVydfiM+E92qh2BIGwL/umInPG7/fL5ZX8jrtAfC00+DsPTPPNVqfE63 V0lQ== X-Gm-Message-State: AOJu0YyEH43B+wTezl0NhnJGtgWwsjYSxgGJMjGpuyeoLpEB+VzfNxSs E62j8DI0bNfVmNXmdwMyG4a3XLAyEvLfdv2PKRGZGfFXQ8WkJq8AYtV0YCuWcHSSPffge0vpltJ KGCIqnzkj3hQdXaMcPqmsURtJVVb2I/Z7DYjAzmGyjLfkZH7veQ== X-Gm-Gg: ASbGncvmvCi54VdLD8PC2tZU41/OY0FZ0e8l5i1Mjf1NzKTo91HWaAGRHwAgcsVLfC+ 5aINM8c20PyQMR1sQK1MHaCX/94f8SzMlx9hSQqI+0YTfpPHhqd7fxvkT8+GHR92c727izKQNSX oBOOCx5D8kcQRiwagNZQznfo7tSgiOOvUN3NiVuaOlCfVcbdK07PmIgpbwLBhzJQgpxjKw3ljt3 Jd6Y+g1LmRgIpM+tNV2BE5TshEsnli8j5T6FxDvOZojXKXXufhf5XhDWsHrqmdNCz1MDtdUrvlo Vkk4b1uMy95MabtN3REpy4Bd+DcPEAIr/kW1lobfgA== X-Received: by 2002:a05:600c:4f92:b0:436:488f:50a with SMTP id 5b1f17b1804b1-43668646896mr573017645e9.17.1736264633480; Tue, 07 Jan 2025 07:43:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IEh8YfTHFHS4ct6XJT1582QW2xCuoEvw06dxSlo9D6j1V3t/nUHTzhQ92uwlzN3QPUXJktBRA== X-Received: by 2002:a05:600c:4f92:b0:436:488f:50a with SMTP id 5b1f17b1804b1-43668646896mr573017315e9.17.1736264632960; Tue, 07 Jan 2025 07:43:52 -0800 (PST) Received: from localhost (p200300cbc719170056dc6a88b509d3f3.dip0.t-ipconnect.de. [2003:cb:c719:1700:56dc:6a88:b509:d3f3]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-43656b013ecsm635054145e9.16.2025.01.07.07.43.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Jan 2025 07:43:51 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, linux-s390@vger.kernel.org, David Hildenbrand , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Sven Schnelle , Thomas Huth , "Matthew Wilcox (Oracle)" Subject: [PATCH v1 3/4] KVM: s390: vsie: stop messing with page refcount Date: Tue, 7 Jan 2025 16:43:43 +0100 Message-ID: <20250107154344.1003072-4-david@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250107154344.1003072-1-david@redhat.com> References: <20250107154344.1003072-1-david@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Let's stop messing with the page refcount, and use a flag that is set / cleared atomically to remember whether a vsie page is currently in use. Note that we could use a page flag, or a lower bit of the scb_gpa. Let's keep it simple for now, we have sufficient space. While at it, stop passing "struct kvm *" to put_vsie_page(), it's unused. Signed-off-by: David Hildenbrand --- arch/s390/kvm/vsie.c | 46 +++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/arch/s390/kvm/vsie.c b/arch/s390/kvm/vsie.c index 00cd9a27fd8fc..29fdffeab635d 100644 --- a/arch/s390/kvm/vsie.c +++ b/arch/s390/kvm/vsie.c @@ -23,6 +23,10 @@ #include "kvm-s390.h" #include "gaccess.h" +enum vsie_page_flags { + VSIE_PAGE_IN_USE = 0, +}; + struct vsie_page { struct kvm_s390_sie_block scb_s; /* 0x0000 */ /* @@ -52,7 +56,12 @@ struct vsie_page { * radix tree. */ gpa_t scb_gpa; /* 0x0258 */ - __u8 reserved[0x0700 - 0x0260]; /* 0x0260 */ + /* + * Flags: must be set/cleared atomically after the vsie page can be + * looked up by other CPUs. + */ + unsigned long flags; /* 0x0260 */ + __u8 reserved[0x0700 - 0x0268]; /* 0x0268 */ struct kvm_s390_crypto_cb crycb; /* 0x0700 */ __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */ }; @@ -1351,6 +1360,20 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) return rc; } +/* Try getting a given vsie page, returning "true" on success. */ +static inline bool try_get_vsie_page(struct vsie_page *vsie_page) +{ + if (test_bit(VSIE_PAGE_IN_USE, &vsie_page->flags)) + return false; + return !test_and_set_bit(VSIE_PAGE_IN_USE, &vsie_page->flags); +} + +/* Put a vsie page acquired through get_vsie_page / try_get_vsie_page. */ +static void put_vsie_page(struct vsie_page *vsie_page) +{ + clear_bit(VSIE_PAGE_IN_USE, &vsie_page->flags); +} + /* * Get or create a vsie page for a scb address. * @@ -1369,15 +1392,15 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) rcu_read_unlock(); if (page) { vsie_page = page_to_virt(page); - if (page_ref_inc_return(page) == 2) { + 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); } - page_ref_dec(page); } /* @@ -1394,7 +1417,7 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) return ERR_PTR(-ENOMEM); } vsie_page = page_to_virt(page); - page_ref_inc(page); + __set_bit(VSIE_PAGE_IN_USE, &vsie_page->flags); kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page; kvm->arch.vsie.page_count++; } else { @@ -1402,9 +1425,8 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) while (true) { page = kvm->arch.vsie.pages[kvm->arch.vsie.next]; vsie_page = page_to_virt(page); - if (page_ref_inc_return(page) == 2) + if (try_get_vsie_page(vsie_page)) break; - page_ref_dec(page); kvm->arch.vsie.next++; kvm->arch.vsie.next %= nr_vcpus; } @@ -1417,7 +1439,7 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) /* Double use of the same address or allocation failure. */ if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) { - page_ref_dec(page); + put_vsie_page(vsie_page); mutex_unlock(&kvm->arch.vsie.mutex); return NULL; } @@ -1431,14 +1453,6 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) return vsie_page; } -/* put a vsie page acquired via get_vsie_page */ -static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page) -{ - struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT); - - page_ref_dec(page); -} - int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu) { struct vsie_page *vsie_page; @@ -1489,7 +1503,7 @@ int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu) out_unpin_scb: unpin_scb(vcpu, vsie_page, scb_addr); out_put: - put_vsie_page(vcpu->kvm, vsie_page); + put_vsie_page(vsie_page); return rc < 0 ? rc : 0; } From patchwork Tue Jan 7 15:43:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13929215 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E2DC1F427A for ; Tue, 7 Jan 2025 15:43:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736264640; cv=none; b=RdSgiRQBXgsCoYEQTqmA4Vuu9WA7rPaWpdvd5El0/XFXwAhQd5ZL2F5hvR4rAt4UgNWDGKCgADuiRGCpQUQBdLqEFNbofHjtlMscnWmoYYs3ct6dgLXc67YfkgmYwU6lNkJB7cxMdXR5eoLs9haserqax1qqkiX8TfNEHePghDg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736264640; c=relaxed/simple; bh=0QXPE+eXFXyIB0uillW17Lfw0Fpu3Su9RoFjQWDKyq0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Aqs1ea+WNl7z8GocBAc0FaD71Ac3miWDIP6Rbh1uIPWhBNIuXqplplyAs1Rkld9vsG/nRlvTGRxjZCAazSVfveYS7PIoqsUWXq743aTBEVswZv+T+MfLUHlieeqacPn0lNRy86PzQtvc3fAUBjR02Ave7dr/y+WnSg2CcgXAnOM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=gh2HxaTb; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gh2HxaTb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736264637; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ksT98VQdUhSv0luDz3yS7GKqTDZHnvkmGgkAuI3OBoQ=; b=gh2HxaTbvX2RmcOD0cGRsm99yTJR+wFWr79bjfkjI0Xitop66qz5zBnLjAXYnbbfQpEk9o UMQa94fk0lNIkiLScfQrTyaFSwklIj6PcDidAenQX1ZfZH4jliGKZuyKp2KWY6Kluy6hE+ qqz6BiOrIMTveJocEnWdb6dnIetXnvk= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-220-1Ku-wcibMKKrlkbd2WND_A-1; Tue, 07 Jan 2025 10:43:56 -0500 X-MC-Unique: 1Ku-wcibMKKrlkbd2WND_A-1 X-Mimecast-MFC-AGG-ID: 1Ku-wcibMKKrlkbd2WND_A Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-385ded5e92aso2969801f8f.3 for ; Tue, 07 Jan 2025 07:43:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736264635; x=1736869435; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ksT98VQdUhSv0luDz3yS7GKqTDZHnvkmGgkAuI3OBoQ=; b=Db4aKVejYEXAuK12DZMdJspXbd1teT6gTUeX9EOYV+WLHi891cqiR9rGroOTcd55/i BQzcZ5faFABMmxST+ac3NtyXJdBkA6B4JQbD+TQTjHLM0uHGIday2TMcewaAitjpSv4d JLE4TXE+0JmMuDTlyPAEak750m7xiLsxKQ1fyNwdtNmLyadw+SBDN7l2XeNniZfrFuaw Akv+P7bqYIbcwhyv0egk78sBuA7pfcRb8+pAdd2WXFQNO3BbswNTV5i+/uLuPdOLyqE0 zzOK5kmkniPCyTNQJVxk9dFx6r82b57EiJ+0vdTp1HHwltvTLXX2rqXXTjvbdEW1D4uj jXaA== X-Gm-Message-State: AOJu0YwFtZ8wCsKAnZdIEZpabrjFcWGOEEgo5YhmdPf03mXa6jMcnLqD blUro3w/wXHH6ue00SrJaX31hTEqyNk2wJTqKQW99XSNzXmrNG/wzk2f0PbXUJix99QlobZG96d IHV/N0oXTpT0YkeV7YXxAmk9vFthOnfmFGP/UVeLhpfbo49OFJQ== X-Gm-Gg: ASbGncvxPdNyN2gcLF/t7/3IbOj8ArJS0C6+ZHBEE+xPjcSGVTILCXPvSSOmFNggFPI 1iNziuel1RyXHAqvhxOZe1PtRgr57z4sC3IJROA708SVTd+3ZXossWKPZzBurqmkNTBxYlCBU2Q csngSDMx9JZywgDMMqdQqnevHDgqtWGqmQPs9vOl0b/brcdtnfK25FyAfdWUIi+b0N/bBC2s4z5 GY1LM+PW9rRvK5M8irLrLv3h0r1aHRF/H/Uvv8gzukPL/hxTOj738lEV+8neX7mUKmGdO/iXuVK 9CNs6oEPZFQ6PrDQLo93MFwxfnNa/t8xh0mxRhuZ3A== X-Received: by 2002:a5d:6486:0:b0:386:5b2:a9d9 with SMTP id ffacd0b85a97d-38a22408e42mr60678370f8f.53.1736264634920; Tue, 07 Jan 2025 07:43:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IHsk0AvacnGYBFNBBsM2kiGWVAG8AyisJBhvtUsieGfaJb05XpWAWuK2N6//5cFbq49Bs8eag== X-Received: by 2002:a5d:6486:0:b0:386:5b2:a9d9 with SMTP id ffacd0b85a97d-38a22408e42mr60678347f8f.53.1736264634544; Tue, 07 Jan 2025 07:43:54 -0800 (PST) Received: from localhost (p200300cbc719170056dc6a88b509d3f3.dip0.t-ipconnect.de. [2003:cb:c719:1700:56dc:6a88:b509:d3f3]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38a2432e587sm46830028f8f.95.2025.01.07.07.43.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Jan 2025 07:43:54 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, linux-s390@vger.kernel.org, David Hildenbrand , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Sven Schnelle , Thomas Huth , "Matthew Wilcox (Oracle)" Subject: [PATCH v1 4/4] KVM: s390: vsie: stop using "struct page" for vsie page Date: Tue, 7 Jan 2025 16:43:44 +0100 Message-ID: <20250107154344.1003072-5-david@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250107154344.1003072-1-david@redhat.com> References: <20250107154344.1003072-1-david@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Now that we no longer use page->index and the page refcount explicitly, let's avoid messing with "struct page" completely. Signed-off-by: David Hildenbrand --- arch/s390/include/asm/kvm_host.h | 4 +++- arch/s390/kvm/vsie.c | 31 ++++++++++++------------------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/arch/s390/include/asm/kvm_host.h b/arch/s390/include/asm/kvm_host.h index 97c7c81275434..4581388411b71 100644 --- a/arch/s390/include/asm/kvm_host.h +++ b/arch/s390/include/asm/kvm_host.h @@ -931,12 +931,14 @@ struct sie_page2 { u8 reserved928[0x1000 - 0x928]; /* 0x0928 */ }; +struct vsie_page; + struct kvm_s390_vsie { struct mutex mutex; struct radix_tree_root addr_to_page; int page_count; int next; - struct page *pages[KVM_MAX_VCPUS]; + struct vsie_page *pages[KVM_MAX_VCPUS]; }; struct kvm_s390_gisa_iam { diff --git a/arch/s390/kvm/vsie.c b/arch/s390/kvm/vsie.c index 29fdffeab635d..22643f4ae4455 100644 --- a/arch/s390/kvm/vsie.c +++ b/arch/s390/kvm/vsie.c @@ -599,7 +599,6 @@ void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start, struct kvm *kvm = gmap->private; struct vsie_page *cur; unsigned long prefix; - struct page *page; int i; if (!gmap_is_shadow(gmap)) @@ -609,10 +608,9 @@ void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start, * therefore we can safely reference them all the time. */ for (i = 0; i < kvm->arch.vsie.page_count; i++) { - page = READ_ONCE(kvm->arch.vsie.pages[i]); - if (!page) + cur = READ_ONCE(kvm->arch.vsie.pages[i]); + if (!cur) continue; - cur = page_to_virt(page); if (READ_ONCE(cur->gmap) != gmap) continue; prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT; @@ -1384,14 +1382,12 @@ static void put_vsie_page(struct vsie_page *vsie_page) static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) { struct vsie_page *vsie_page; - struct page *page; int nr_vcpus; rcu_read_lock(); - page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9); + vsie_page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9); rcu_read_unlock(); - if (page) { - vsie_page = page_to_virt(page); + if (vsie_page) { if (try_get_vsie_page(vsie_page)) { if (vsie_page->scb_gpa == addr) return vsie_page; @@ -1411,20 +1407,18 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) mutex_lock(&kvm->arch.vsie.mutex); if (kvm->arch.vsie.page_count < nr_vcpus) { - page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA); - if (!page) { + vsie_page = (void *)__get_free_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA); + if (!vsie_page) { mutex_unlock(&kvm->arch.vsie.mutex); return ERR_PTR(-ENOMEM); } - vsie_page = page_to_virt(page); __set_bit(VSIE_PAGE_IN_USE, &vsie_page->flags); - kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page; + kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = vsie_page; kvm->arch.vsie.page_count++; } else { /* reuse an existing entry that belongs to nobody */ while (true) { - page = kvm->arch.vsie.pages[kvm->arch.vsie.next]; - vsie_page = page_to_virt(page); + vsie_page = kvm->arch.vsie.pages[kvm->arch.vsie.next]; if (try_get_vsie_page(vsie_page)) break; kvm->arch.vsie.next++; @@ -1438,7 +1432,8 @@ static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) 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 >> 9, page)) { + if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, + vsie_page)) { put_vsie_page(vsie_page); mutex_unlock(&kvm->arch.vsie.mutex); return NULL; @@ -1519,20 +1514,18 @@ void kvm_s390_vsie_init(struct kvm *kvm) void kvm_s390_vsie_destroy(struct kvm *kvm) { struct vsie_page *vsie_page; - struct page *page; int i; mutex_lock(&kvm->arch.vsie.mutex); for (i = 0; i < kvm->arch.vsie.page_count; i++) { - page = kvm->arch.vsie.pages[i]; + vsie_page = kvm->arch.vsie.pages[i]; kvm->arch.vsie.pages[i] = NULL; - vsie_page = page_to_virt(page); release_gmap_shadow(vsie_page); /* 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 >> 9); - __free_page(page); + free_page((unsigned long)vsie_page); } kvm->arch.vsie.page_count = 0; mutex_unlock(&kvm->arch.vsie.mutex);