From nobody Fri Dec 19 20:52:51 2025 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 EB2F91E1C3B 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=1736264635; cv=none; b=c6JLcLq5O13H/fVhixhZ8Z6gJ1m3b7qbhj+X8l0I4dNUVKsNqYe/JtAbYFKH8/XCYx6mgrLK7bPdtHmdx8IzBIafJboRLBAl1676e4bQblavzCZCvxQexpwCiJDtm4/5/r5uucQl7uceplW7W1rD7kp0V/KR/m1aOGkQJtCC/Yw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736264635; c=relaxed/simple; bh=kff/5MuiUSshICdL0Pz3Kwaact1TDCjVUg2HjOr3gSg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ek8oLQl9W8WFxagogmwFHn+Y2QPcguCIqCK3SK6lYjSDv4Qin6w8DvyhUg34luMwNkdVLpPlsWnxwAu9lABu0Pk4dpNAgN6Up0nIqtVLoPVoXQoG6GzTukBGV/ecgzys5fIRn8kbr7mBLv8HPf4xT4vMapleSuZWGiZkScLXTbE= 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-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-342-Z0OgrVEKP1yaUdjVd0RyBw-1; Tue, 07 Jan 2025 10:43:50 -0500 X-MC-Unique: Z0OgrVEKP1yaUdjVd0RyBw-1 X-Mimecast-MFC-AGG-ID: Z0OgrVEKP1yaUdjVd0RyBw Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-385ded5e92aso2969777f8f.3 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=LiXwsNloJIVD/03FFLfdAzquZ/rSR3jssBU093h/6RPeFpB8A4E4t1byZ8FkKNN1mg gnvY89dnyliMg9GhOSj7sds64CfGthNW2wXQIPUKRPG9G86zqCLEHwiE0CRGBL9LwvBg fcwaBSMuelYr+TngHM/oxaapIVJ6bTlZFFsxxgSYtI0u1gr8kJ1P9zlkyekeBvvSj7rv PvQHAzr1KZPNIdvJQ8+MIY9awpB7j6BbtAsuDXjY9NueOhsa7pxbR+Ij7u1DbWcHa9Bn YaSvstb47992zVcSfDYRfSi5QFVxUWmmpnxhVhjDSW8l+2/5Fy8ZWQdGK5dlo8X510vD jlqQ== X-Gm-Message-State: AOJu0YxiV3rCQC+YifdJ88fyBu1KgotqAELR6sK3Qg6AQr0Xa5Wyt4Gj DRXmY+C2yQxKG+rZVh+uHOC4YxDsosyJrnmzMw6Z1weGViFlNBnrMspACvvITmKSNTLl/JIr6OM 13wjLqchzAtAsyX1HCsdT1MRwfbJfMPh/M2Keu0LXWt/pTOcpJ6dL/Gby9qdhWsTaBNQ2+1Cufl Ih2paeME23oq/3GQOrVhCZufbmLZmgyEj5dNY3+gPksA== X-Gm-Gg: ASbGncvu8zp9VYZkfb5yvCAUHa25mPjna2Rmeuoa0j4pxJX1cmVvQevhyHe/kF/r0TD xeZSdW5BUebI97p+co8h56fiHrq8A5pyTEupVcbSvqOR2lGabUUonSa3u7PqbbYxKtB5HXH4gbY DA76725aKXyUy70lW0yeHvGCsoMXiAW3WoWyLBfSbA98dKTSR+S72CRQX9/53AS7PhyQRtjW8MA bXBBkuJJkROTcWpK5mm//JApLbpGad2PXnms7FYQRgk8n6amCxLQWumo3F49P12m/cWlFM4+Q6f DGDdHErbUq0/gC+Hu+aKC8TRhiRdssCuqe+GGFPA4g== X-Received: by 2002:a05:6000:1f88:b0:386:4034:f9a0 with SMTP id ffacd0b85a97d-38a22408cbemr49437417f8f.52.1736264629583; 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: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3D0xffff 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 virtualiz= ation") Cc: stable@vger.kernel.org Signed-off-by: David Hildenbrand Reviewed-by: Christoph Schlameuss Reviewed-by: Claudio Imbrenda Tested-by: Christoph Schlameuss --- 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 *k= vm, unsigned long addr) page =3D radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9); rcu_read_unlock(); if (page) { - if (page_ref_inc_return(page) =3D=3D 2) - return page_to_virt(page); + if (page_ref_inc_return(page) =3D=3D 2) { + if (page->index =3D=3D addr) + return page_to_virt(page); + /* + * We raced with someone reusing + putting this vsie + * page before we grabbed it. + */ + } page_ref_dec(page); } =20 @@ -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 %=3D nr_vcpus; } - radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9); + if (page->index !=3D ULONG_MAX) + radix_tree_delete(&kvm->arch.vsie.addr_to_page, + page->index >> 9); } - page->index =3D addr; - /* double use of the same address */ + /* Mark it as invalid until it resides in the tree. */ + page->index =3D 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 =3D addr; mutex_unlock(&kvm->arch.vsie.mutex); =20 vsie_page =3D page_to_virt(page); @@ -1496,7 +1507,9 @@ void kvm_s390_vsie_destroy(struct kvm *kvm) vsie_page =3D 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 !=3D ULONG_MAX) + radix_tree_delete(&kvm->arch.vsie.addr_to_page, + page->index >> 9); __free_page(page); } kvm->arch.vsie.page_count =3D 0; --=20 2.47.1 From nobody Fri Dec 19 20:52:51 2025 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 BE3E01F37B6 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-607-W6puQYK8M9iPeiXLkNBVgA-1; Tue, 07 Jan 2025 10:43:52 -0500 X-MC-Unique: W6puQYK8M9iPeiXLkNBVgA-1 X-Mimecast-MFC-AGG-ID: W6puQYK8M9iPeiXLkNBVgA Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-385d51ba2f5so6658298f8f.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=QbnapXWpsT7Zx0rQQApa7kWpfmWd1HNzaGTiMnUqd277xr06wp8MLPwEhXgiJFfRRN U0f7g5C94jCF8t9b6nvNhdik0F8kpnRYZPOsLzwUMAB3BDPhS5JUiVuN0jNQ9+p8i06h B2mbfGeDbh6UDJwMflzpn1nRu+V/L5H08K94i37ozqSXnq/4TwqL1ugdIRl7cBWtcpVZ M8ZUuBfwBhPsH9w3KDl/VzZYz2vTujwkV6ZIhfcHW5aB2U1oAT2x1xp897jU10QVRF3a H2YKMM1aA9USz1zl1YyxOlYemNCpQ9+9ZEeCmZpDdfMTYbSylwDDunW/D11VKaAvLU+4 VEew== X-Gm-Message-State: AOJu0YxVQ9axPk4IGgQjoLhPZUS7wChzHYWjjVV+BYvs5n5MvfnMNiSb hV8sAs3eKmCuei76G8ZlLn4Sg1WzYfra/j78BA6hztyumvosr2K/rSgoij/5PfHk7ZSDSkgi/eA 6XRCpiRkQCZXboxg3qk39wMz4iSnssWTy6eBw/kU2mIhCR9qfLjHmOHqCeVEdI/5Ji74Esf0qht 7bOlMeOnqL+sGN/uFnzm5TD53qZNirNMHYtQFJ2ox/Mg== X-Gm-Gg: ASbGnctDvp1K19Mr95muhs9BpMBso1JrBFeNsvR3cNeefEy/6xi8+d3nk0SAoISxyY9 /IrpMkNMzxzBq9dlXSYf4gA1V0Q6Jduxamr/E3qKrl1K/Y1ZBnDJHP3m8FrbrtUki9eeAfX4D/g kyKyRBc5ijDW8fXEOO4jJ2ByAMv7Quky4C7wywYrfQIVS7sS+AjeQJA7CuSses0FYj5pW5/zNIs ptD6MqC7evqs5bfGzUO1eDefuOnA/OnJg5WwyV0cD5qVYxeV4Xat5092oJ4dSjjHBCyEBuwOpMg sXbKEegnag10R7TiilDKRdeI01luNsGhguaKET+szg== X-Received: by 2002:a5d:64c9:0:b0:385:f17b:de54 with SMTP id ffacd0b85a97d-38a221f11f9mr58228937f8f.5.1736264631131; 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: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Christoph Schlameuss Reviewed-by: Claudio Imbrenda Tested-by: Christoph Schlameuss --- 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 *k= vm, unsigned long addr) page =3D radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9); rcu_read_unlock(); if (page) { + vsie_page =3D page_to_virt(page); if (page_ref_inc_return(page) =3D=3D 2) { - if (page->index =3D=3D addr) - return page_to_virt(page); + if (vsie_page->scb_gpa =3D=3D 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 *kv= m, unsigned long addr) mutex_unlock(&kvm->arch.vsie.mutex); return ERR_PTR(-ENOMEM); } + vsie_page =3D page_to_virt(page); page_ref_inc(page); kvm->arch.vsie.pages[kvm->arch.vsie.page_count] =3D 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 =3D kvm->arch.vsie.pages[kvm->arch.vsie.next]; + vsie_page =3D page_to_virt(page); if (page_ref_inc_return(page) =3D=3D 2) break; page_ref_dec(page); kvm->arch.vsie.next++; kvm->arch.vsie.next %=3D nr_vcpus; } - if (page->index !=3D ULONG_MAX) + if (vsie_page->scb_gpa !=3D 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 =3D ULONG_MAX; + vsie_page->scb_gpa =3D ULONG_MAX; =20 /* 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 *k= vm, unsigned long addr) mutex_unlock(&kvm->arch.vsie.mutex); return NULL; } - page->index =3D addr; + vsie_page->scb_gpa =3D addr; mutex_unlock(&kvm->arch.vsie.mutex); =20 - vsie_page =3D 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 =3D 0; @@ -1507,9 +1515,9 @@ void kvm_s390_vsie_destroy(struct kvm *kvm) vsie_page =3D page_to_virt(page); release_gmap_shadow(vsie_page); /* free the radix tree entry */ - if (page->index !=3D ULONG_MAX) + if (vsie_page->scb_gpa !=3D 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 =3D 0; --=20 2.47.1 From nobody Fri Dec 19 20:52:51 2025 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 523881F4708 for ; Tue, 7 Jan 2025 15:43:59 +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=1736264641; cv=none; b=pdQUXw8w5JkVVAlLsfixRo0+qHVfpxvQFAKH/VZuIEY81ZWXDqdiDG4YSw+YcLskZtgYNnbk2raAhQnQbA0PlYG3iuLDajvLIY0uElC039Rph4Ppl/lZJFVh9HuF4w6QYqafI9+AWgvg3PxpV/0tuUKxrnzpcygWPrqASQWv2kg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736264641; c=relaxed/simple; bh=L+CPO+OXcYMowj2nsp4a3RdcFfWPLoQzNe/QJrtHqCA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y9lpKPUgr/wPUsxjQigp8Vy9Tjji/ZqAGC+2QcbCplci9Ro9s5dg9BuL9ktHXKVx2cgUSt79xij19FI0/qEZx0m9QNGI0nPDSjmt9y1xYlz4utsZ9dgLUWqRqsdiy9mhypJocRsRSC18xVwJNjO66uUKADf52Ga+xfSw3MgZrXQ= 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=RsHtS/dD; 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="RsHtS/dD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736264638; 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=RsHtS/dDWs5pPgBx+Z3zRkycqS2cO3zron57483E4VlotD+qJ9EHDLx9eZepOottRmdBE/ BpmhWuscAzD7K+JcxWlPrRBu0+cf+i6/8majo7RQ/tanaVcQyf8nkxWSuDpSZ42d7hPJuY atEtNCLqRcwdVyfZ/PEy/JIK7mgy9/s= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-253-hax2gtSKNo-tdx9q1iv97w-1; Tue, 07 Jan 2025 10:43:57 -0500 X-MC-Unique: hax2gtSKNo-tdx9q1iv97w-1 X-Mimecast-MFC-AGG-ID: hax2gtSKNo-tdx9q1iv97w Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4362153dcd6so44022095e9.2 for ; Tue, 07 Jan 2025 07:43:56 -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=bv/0QBSjWhuDYLd+bhBKz1st5V2FAA9/fbL1AmDgLOzBxA1jPey3y8xMxI3v+mXZRe K4urBohcRZ5QlxskGdtKVh5vqQevtbOqNKveFg4dJvFi9DbOhA+8Wur4pSlsbfNQwupn 4h/UlRvi5w8hpbMV74hRba88Dyxg3J9egp9A2awM5yF7QNGu+yaM4i6gQl2XBydnwgGe tYAQn0fzdsdIC+qw5FV3sPy7mZ21JncCQsDWFtYF4OdCpW677HS5aDKd5sRiPt4nOURU 5Yh68jyAlsqsdjjRw/cetQQUWwV+sNvdkCSv91B9Bi7KhgzzlKTcO76y87x+1XguTN/X valw== X-Gm-Message-State: AOJu0YyCe4Mv+dMVMVEnylRPSg/iE75uRyhBNpnSZD9km6OP+7Pcnfj+ lFPQOw79OtK/i2BBWHPGhs5Dz5VeAvimMWJmpbfYkshYAQimOIGZWHVaeMbkfYXwfRNiBAXS3Ts 1H6yVMIeTHT9/NQRMdSO+/XbDtcudAa/U7t7jqnY0rm6jWVUjzdpqbFXLDywJc0kojdybM4JPUi 296mjBCLqePXu8AbmKsxDC1wrI2UDrk+0pbi0JB6cD6A== X-Gm-Gg: ASbGnctopdeJMPbscAjAmkN9+2NPS4v32s8QgKyCRp26n+hbwSunVNoF34ce9HCSQFr qno3NDbCy3YH2kV7bpNfrXiDY1e5fZj88NU+It8Vp1knDMvNRK1GOLCsU99eb9zUn9aHU8kskpU DZR2g10ADDJoWW24n/d2FlHLH98/76DIacmYoqMbcj758Md94CgNDN/BZU8DeWOwqyIjZDf8V+5 tIlRDqt4W+B0cTJOEg0aY3uU/40twE5VTZk5jHt5e+4N2n1VKx18YMBMQkmc97o4PWX/8eQlOIe 40tPfcodOjH26WcinUJ2cIAe5l+yrOjcOghLqv7MRg== X-Received: by 2002:a05:600c:4f92:b0:436:488f:50a with SMTP id 5b1f17b1804b1-43668646896mr573017765e9.17.1736264633516; 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: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Christoph Schlameuss Reviewed-by: Claudio Imbrenda Tested-by: Christoph Schlameuss --- 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" =20 +enum vsie_page_flags { + VSIE_PAGE_IN_USE =3D 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 vs= ie_page *vsie_page) return rc; } =20 +/* 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 =3D page_to_virt(page); - if (page_ref_inc_return(page) =3D=3D 2) { + if (try_get_vsie_page(vsie_page)) { if (vsie_page->scb_gpa =3D=3D 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); } =20 /* @@ -1394,7 +1417,7 @@ static struct vsie_page *get_vsie_page(struct kvm *kv= m, unsigned long addr) return ERR_PTR(-ENOMEM); } vsie_page =3D 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] =3D page; kvm->arch.vsie.page_count++; } else { @@ -1402,9 +1425,8 @@ static struct vsie_page *get_vsie_page(struct kvm *kv= m, unsigned long addr) while (true) { page =3D kvm->arch.vsie.pages[kvm->arch.vsie.next]; vsie_page =3D page_to_virt(page); - if (page_ref_inc_return(page) =3D=3D 2) + if (try_get_vsie_page(vsie_page)) break; - page_ref_dec(page); kvm->arch.vsie.next++; kvm->arch.vsie.next %=3D nr_vcpus; } @@ -1417,7 +1439,7 @@ static struct vsie_page *get_vsie_page(struct kvm *kv= m, unsigned long addr) =20 /* 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 *k= vm, unsigned long addr) return vsie_page; } =20 -/* 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 =3D 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); =20 return rc < 0 ? rc : 0; } --=20 2.47.1 From nobody Fri Dec 19 20:52:51 2025 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 5E2831F4279 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-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-688-ucTNJ5XYP-6TT8mewwjznw-1; Tue, 07 Jan 2025 10:43:56 -0500 X-MC-Unique: ucTNJ5XYP-6TT8mewwjznw-1 X-Mimecast-MFC-AGG-ID: ucTNJ5XYP-6TT8mewwjznw Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-385e49efd59so3198209f8f.0 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=UZshuG2HWmb7xOi6GBZ+Gb1D5xxIcgv9YAywwsH13Q5/p0+rpMvAbbA2JCsV+jta6t H2j17qcS7j/vTw20r2nB0A+a70t1zqAamifCdZUjODcyHP2Jaipmji5SeSQ4iRhTrAFR d0HTtMU1BidW8klXG8iSh5EN88EnMQeu4bVRCTnZbswqgtYEoqJFXLkkMf6aQlA/OBvd nk+dqPi5DeF1mQUa8S+yVnL9lHfDqUJ61joQz1e3hDPyyVsZ5NXphxSRpqsIVx2CgK4Q jlcxMNS7jhTzXrJs23UvFLfO84fB/8t53HNc680XZmwos12UfW67xJwY/MpfHt+LwVh6 +qBw== X-Gm-Message-State: AOJu0Yx7q1d0TBqnq3fkvbZ0T6HSTaePGBAVGuLbmqKVAhhR0zuK5aXr vTjYrF59t6hChlauqBrEfUVvUqURnPhfU3+8p8kg5E598PlQnyOf4D/HlpoPXtsPfGlqwttw1qL nrs1vkyN+ggNnl34Dx92KZ1Wo4qySwPFrz0FzM75lR9Ftz7diJ7q8q3Zqp1xraPCfpOQjhl4mSo pFMJVBRtAiv8Lcnuo17gW9AQm3Zu3z1j0OWTHcX6sYuA== X-Gm-Gg: ASbGnctB1yyWbIfBtC1vD75EtRnCc00GQc6If8/VVSS2bKBexgZVgPsCgCMMXFMIj9p eizK7QOGwKX6Qx1PChD7a9JiOGIVCKosyLCL4TUsGqU6rQakQVq8SVAUatwocqDC/avn7JpoXjO 5k0ChW/MhEY19hno47FqUmF7mfiKUlJN54es/n3PiT0k0MmcZOb8RaCVkLrj48Oq+tBmNCjy9vI EUwpgssxwBXPYTJqHmCCs8bKi6hRnUcMAPORgH8hOm3G5R/o2tSMBx+VS3czoA3MOFHEuP9RPvy FudE02iBezE8CtNgO2oZFtCbpprKKAIwr7vo1Hk8Uw== X-Received: by 2002:a5d:6486:0:b0:386:5b2:a9d9 with SMTP id ffacd0b85a97d-38a22408e42mr60678379f8f.53.1736264634954; 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: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Christoph Schlameuss Reviewed-by: Claudio Imbrenda Tested-by: Christoph Schlameuss --- 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_h= ost.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 */ }; =20 +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]; }; =20 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, uns= igned long start, struct kvm *kvm =3D gmap->private; struct vsie_page *cur; unsigned long prefix; - struct page *page; int i; =20 if (!gmap_is_shadow(gmap)) @@ -609,10 +608,9 @@ void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, un= signed long start, * therefore we can safely reference them all the time. */ for (i =3D 0; i < kvm->arch.vsie.page_count; i++) { - page =3D READ_ONCE(kvm->arch.vsie.pages[i]); - if (!page) + cur =3D READ_ONCE(kvm->arch.vsie.pages[i]); + if (!cur) continue; - cur =3D page_to_virt(page); if (READ_ONCE(cur->gmap) !=3D gmap) continue; prefix =3D cur->scb_s.prefix << GUEST_PREFIX_SHIFT; @@ -1384,14 +1382,12 @@ static void put_vsie_page(struct vsie_page *vsie_pa= ge) static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) { struct vsie_page *vsie_page; - struct page *page; int nr_vcpus; =20 rcu_read_lock(); - page =3D radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9); + vsie_page =3D radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9); rcu_read_unlock(); - if (page) { - vsie_page =3D page_to_virt(page); + if (vsie_page) { if (try_get_vsie_page(vsie_page)) { if (vsie_page->scb_gpa =3D=3D addr) return vsie_page; @@ -1411,20 +1407,18 @@ static struct vsie_page *get_vsie_page(struct kvm *= kvm, unsigned long addr) =20 mutex_lock(&kvm->arch.vsie.mutex); if (kvm->arch.vsie.page_count < nr_vcpus) { - page =3D alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA); - if (!page) { + vsie_page =3D (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 =3D page_to_virt(page); __set_bit(VSIE_PAGE_IN_USE, &vsie_page->flags); - kvm->arch.vsie.pages[kvm->arch.vsie.page_count] =3D page; + kvm->arch.vsie.pages[kvm->arch.vsie.page_count] =3D vsie_page; kvm->arch.vsie.page_count++; } else { /* reuse an existing entry that belongs to nobody */ while (true) { - page =3D kvm->arch.vsie.pages[kvm->arch.vsie.next]; - vsie_page =3D page_to_virt(page); + vsie_page =3D 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 *kv= m, unsigned long addr) vsie_page->scb_gpa =3D ULONG_MAX; =20 /* 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; =20 mutex_lock(&kvm->arch.vsie.mutex); for (i =3D 0; i < kvm->arch.vsie.page_count; i++) { - page =3D kvm->arch.vsie.pages[i]; + vsie_page =3D kvm->arch.vsie.pages[i]; kvm->arch.vsie.pages[i] =3D NULL; - vsie_page =3D page_to_virt(page); release_gmap_shadow(vsie_page); /* free the radix tree entry */ if (vsie_page->scb_gpa !=3D 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 =3D 0; mutex_unlock(&kvm->arch.vsie.mutex); --=20 2.47.1