From patchwork Fri Jul 26 23:51:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743429 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C7A04C3DA4A for ; Fri, 26 Jul 2024 23:53:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=B44llYCZd4EZ9BiHApjJ5QQ8CK6GbkfpTxw4rYmbmqk=; b=teegbO6fJNnX2eAPXm30K0iUED fspcyttc/sPsNJpugcwX40XJ0vSb2hW2uRldr6ykfkehxMB4c3K3nt3ZyPXCuLEAzlZCd47EdQo+f /ah3QOQHOJaOY7GoSMyJampQ09pSIkVguuxl/TABn5frieXLKCHA3d1tiIvuOSKm/WmOY6awz7JTj lU8QD8j0vsGj0YXaRHxR9cCK55jHC4SaJX6RPCIqKnITrV27017pYvTsJWFt1t0jC3uSEN20vlk7l mBhkOfXvWaXeMil2q10A83QZvTCAs3tsc48hZBJ+be7Kwv41nirGyGrsfLgmruz/duqs4okDjEvQS 89ZJz10A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkf-00000005Pqo-35ll; Fri, 26 Jul 2024 23:53:33 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUjp-00000005P89-2FUs for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:52:43 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1fc5e61f0bbso13355085ad.1 for ; Fri, 26 Jul 2024 16:52:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037960; x=1722642760; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=B44llYCZd4EZ9BiHApjJ5QQ8CK6GbkfpTxw4rYmbmqk=; b=zm3Q8cbxYWgV/ICg/XpBQIFA5ZZrUheT5124qEoiaiXcrddyX94DdP0qUCzD45kwt+ RDlY5TVAitXGwXPxoMn4gWERtB7jj3o6wCuMtvorUh8FXdD3LZLuBHIerNymmFjUchg1 B2UugSa8CQzLCMz2F6aMXicsUkh0+Xcxf0jP2sGH5MLm8U9TlZifNMbg1YfjCkWmdmWm WIbu6BHnysY/udvO2+MxiwruCh2amZM0c3IBBl7dJT129UFrBxRyeE7pdUIf0U7+UKMb DP23v07HGXZL/WrCAcfDvW6hjnes8bdkxeQU2b9s8xm0sEA3icUbvNCUKBcl2jbmHEIt Phsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037960; x=1722642760; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=B44llYCZd4EZ9BiHApjJ5QQ8CK6GbkfpTxw4rYmbmqk=; b=qJzHnuEG5w2EovkMwQLF5gWOGRwOEDLFrpQ5J1nszf6b3ui6YHsdQ56/SeYhwti5OO VQ1UxKiCH39A+WeBHb0VmbaaONjEqYG2D8bX/W/8eY7ZZ+0Tu7GmdEzqkgsNGSc8uExS F+YllyquDpM5Fw7AJpQjIF9o13U7MEgPqgAsk0214W9MBDwSsDG4u0cls5NPhq+A1q7a /vRlgrNw/0dDq2mMYmwDxpFIbuZ7QklGFAHTSikEAS5wkk4KWSFMbXesyKSzzTf2yRf1 +/zrXepqYtym3Sfx4A+y283V9vN1hj+RUKzfS0WQ93KMv9LZNyB6RmqsViiTCs93S/iR FJVg== X-Forwarded-Encrypted: i=1; AJvYcCUxZb8A6m6B1HzhG15tP0CIFlXlxx823QpY/prBJzSrBDnOoyad/3OFTt48WbshJeLOpIMQd/WNA+uVTfXMHwiKyp7YFbPDRYnToC7+gjZ5tKiTU1U= X-Gm-Message-State: AOJu0Yxx7jnorqI+9x1rNxIT+9S2igrUUi4NTBUpTwJlturL8xutp5pN NsMATR5ehr2a0O/I6a5qIEru+lAAm9uaNh5nblYQg4fb8jcbKyOXpqcLIqqdjp4xw/aogmqYSoi q0g== X-Google-Smtp-Source: AGHT+IEjG0ZQEVlDvyCmqtjyibvaIBPxkBHQDdDjj94pZYYC/xb/lJ2iSbTktb4rvu/gOGNYLlpIc1q2Vcw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:2b0e:b0:1fd:7097:af5c with SMTP id d9443c01a7336-1ff048d8505mr410065ad.11.1722037959655; Fri, 26 Jul 2024 16:52:39 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:10 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-2-seanjc@google.com> Subject: [PATCH v12 01/84] KVM: arm64: Release pfn, i.e. put page, if copying MTE tags hits ZONE_DEVICE From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165241_599336_8F0D26EC X-CRM114-Status: UNSURE ( 9.35 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Put the page reference acquired by gfn_to_pfn_prot() if kvm_vm_ioctl_mte_copy_tags() runs into ZONE_DEVICE memory. KVM's less- than-stellar heuristics for dealing with pfn-mapped memory means that KVM can get a page reference to ZONE_DEVICE memory. Fixes: f0376edb1ddc ("KVM: arm64: Add ioctl to fetch/store tags in a guest") Signed-off-by: Sean Christopherson Reviewed-by: Catalin Marinas Reviewed-by: Steven Price --- arch/arm64/kvm/guest.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index 11098eb7eb44..e1f0ff08836a 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -1059,6 +1059,7 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, page = pfn_to_online_page(pfn); if (!page) { /* Reject ZONE_DEVICE memory */ + kvm_release_pfn_clean(pfn); ret = -EFAULT; goto out; } From patchwork Fri Jul 26 23:51:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743431 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 19620C3DA4A for ; Fri, 26 Jul 2024 23:55:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=+ACqpXiHFW0htcnePdxx7jkBXYAkQBnbtzPYGP86yy4=; b=z07N+c/ZgH9qYudoOQtiPVSOT1 mxv+ZadaFJyajmOEVgkPZOOYh3VAhfex1ZLULtKwfxoRiDNmwbwjIpDLvdms0SclnkLGoBWBqSx2u mI5wR24bfGydIv8EbNhIgTNhoIsYeOEn/02P0J8APPZZzE0uzuGKEBrHV6gqCRDBryAkXovllTbrh W/Hld+3WoX5JDKnYh7RzHmkjv6X+Xy4dFW9FWNVxj4fBDjJxkur8rR0Ie9qjlXDXsBH5Dmbig+ANM QMjgola89cuBt/7idOBRr42fkkvcpfAHmTfot0iYCFg3DOJd8Lm3WAyV9scBL6nE4lYD38aPgC0kV u+zoccDg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlu-00000005Quv-3iIQ; Fri, 26 Jul 2024 23:54:50 +0000 Received: from mail-pf1-x449.google.com ([2607:f8b0:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUjr-00000005P9S-3hta for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:52:45 +0000 Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-70ec1039600so1045943b3a.0 for ; Fri, 26 Jul 2024 16:52:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037962; x=1722642762; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=+ACqpXiHFW0htcnePdxx7jkBXYAkQBnbtzPYGP86yy4=; b=GUTcAD7G/IEqTQBDwkN0d+unfvHGOHtzepTk0k95GMmhpmyt55Pg0jbDr4Vxu9pMJz 7i/T07LhY03vJfzAA+439QiKhl8X5SShXNeN868QZIhkErIsJyT+ss/dCjKaUag5OZtQ p9rekki1UeXULTwmdrwX//WA5aLj0VL/CMQDdyWyFwNltswQbAbH6b64kCNQueyqZvtm 3FZ4a66QnzSO2wts4NY1YQVRF7HOACiCoXrkqxN/2B1v/TcsJOqXQfOEkmS3L1mZPmDU G4uuRvFHpaWCQMqoISPEklRLd3IWS9MqbUKF9qw6l9L+JOKYFkbgSD9AfnCgu81FV0wf kQwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037962; x=1722642762; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+ACqpXiHFW0htcnePdxx7jkBXYAkQBnbtzPYGP86yy4=; b=o78lbvSZi0Bxgc1MuwmyiUuFzqqgwW/+Uz5F5A+WSzHuWzAWpTwnd84eJXkKpAzAHC WwUIPHIx1hnBBxlZyzrqlU+d/6qRroRMx1AqVyeDteeNWngcKJz7YVd9TjT/FPjW/FdB C8AuV0S1nlSuem64L8rEJrpUvOkViGtWiz7jzb1rQMi/KCpLzmtvY5Dg7sxbonxotuhV TQBYiaCIyzGlxT6LlhrVKnS7PZq6aVoUVLW4EDGbJ57F+isaAqjo07qFtcoXlyZXgFhp 06CymIHWoKNhN+8if/5FyX2a0Wa9jhf3diyxqMb2Jm1t5+m/amnjfxVEtDVgdORiait8 kXqg== X-Forwarded-Encrypted: i=1; AJvYcCW+WV5YCsB5WfCABs3Ue7UnDmRR05rZHBn9hEG1gdhQnEhn/MNzSAAKI9G7NMut4gsX9j8mkWeZPsWEgZ4t10N234Zp28XAko7DZwbWSwdhcLvja/o= X-Gm-Message-State: AOJu0YzH8eOv3Jx6l3pKHhV6DfephZgCHA9kHd8jeUF8PGH5Yt/swaYk 5L+1wujxhXGA412oMc8P3oCnvQg0Dy0dM89ztWcfxA9c7KzN/8zbITOrLz0/AJL+vlnz1gYjBh8 Z3A== X-Google-Smtp-Source: AGHT+IGwCQGQqihWYa2umPunvMXl+38XfICaGVr07aRfeEr4UWXG0owvMV5oQenmn7mjdAWrLBWC2Kt2f3c= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:2d5:b0:70d:1cb3:e3bb with SMTP id d2e1a72fcca58-70ecedee1c9mr17317b3a.5.1722037961972; Fri, 26 Jul 2024 16:52:41 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:11 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-3-seanjc@google.com> Subject: [PATCH v12 02/84] KVM: arm64: Disallow copying MTE to guest memory while KVM is dirty logging From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165244_003958_7EB703A3 X-CRM114-Status: GOOD ( 11.21 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Disallow copying MTE tags to guest memory while KVM is dirty logging, as writing guest memory without marking the gfn as dirty in the memslot could result in userspace failing to migrate the updated page. Ideally (maybe?), KVM would simply mark the gfn as dirty, but there is no vCPU to work with, and presumably the only use case for copy MTE tags _to_ the guest is when restoring state on the target. Fixes: f0376edb1ddc ("KVM: arm64: Add ioctl to fetch/store tags in a guest") Signed-off-by: Sean Christopherson Reviewed-by: Catalin Marinas Reviewed-by: Steven Price --- arch/arm64/kvm/guest.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index e1f0ff08836a..962f985977c2 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -1045,6 +1045,11 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, mutex_lock(&kvm->slots_lock); + if (write && atomic_read(&kvm->nr_memslots_dirty_logging)) { + ret = -EBUSY; + goto out; + } + while (length > 0) { kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL); void *maddr; From patchwork Fri Jul 26 23:51:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743495 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C4286C3DA4A for ; Sat, 27 Jul 2024 00:11:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=oJ4Czs6Wm6GL3WD+/Q+iqutVJUfrJPNLt1yka1W8eMI=; b=akBEip54zlcWUKhBGxIYq8F5i9 jk/mJxyryOn33WOwPZACSq5JUUvrm1qRsW/TYdsVH4qjkWxn77iWciiW4GmMFIOTbq8W8rRIrTtfd cl7p1Pmbwsztn82RbGzATKr48R00t4K83Omz57Z6H+Wqh573NM78A0L8o9PkuDdriLcjU/NDkUeIX 1D/3kIkfOaywtrxMLJT20itIww+2OLPa8Or14hIchKDfMK5SxXD0GIAbxyjDXoR15wXwNCjpYASaA 7Ze22yVlsfczO1MLHPHKHm0BvpTlaAnUd4bpRfEqqbvV6TgPbQW35NBHGzjaqJnkWGcJ6cBeFEELm c1KfNIbA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV2H-00000005a2S-1Yn3; Sat, 27 Jul 2024 00:11:45 +0000 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUjt-00000005PAa-30gF for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:52:47 +0000 Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-7a12bb066aaso1079000a12.3 for ; Fri, 26 Jul 2024 16:52:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037964; x=1722642764; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=oJ4Czs6Wm6GL3WD+/Q+iqutVJUfrJPNLt1yka1W8eMI=; b=e3mlaXFq3kXHLxKh1ZEXJE7y9azzwcQTw1Ro1qT/6AJZfyXrfBCVOgfnZoaIsEFr9G NtrjUQjOZfxWBi/rk+WXajNdHpphQdycAPvmD04phGs6Bqx7asMFPGhRjnghfN4bSkaV ioa9zHcwqMan9mQ1Ueas1vEP+Uq9fZPi/BXSDEnJfmiXg36oxasVjGhYgFsXUX0Ft+Tx b4Q3rQmGDkMOAasyGPx7mJE/PfJaRSwPMiCbxTRiQ+hxxKxC0HMs+1aLwyVHGiIPNy+P SxcFnUdMnzLxM4kmObl4+c58l+rkhMujVWyJXU5WQf5hs9gxXjzfeEIow2oSACgfqxB0 5S+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037964; x=1722642764; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oJ4Czs6Wm6GL3WD+/Q+iqutVJUfrJPNLt1yka1W8eMI=; b=oSQG0xxBD3DkdN68y0PycE2mlFdXBUX1iso5mYdZO30SPaq6WXYp4EI1gvJbbXyO1E M6IpWA9pMyhPIrNsolY0hBta+LfTAKjH4UB0lGoBeVjnAalIwis+5ch12g25ZY0YYthe uVhUiCExVnNtJZ0LZuapSxsXJLYh4NkxtOmgnJRrVdZr9mq5OgDzmwvA0SyyNd9Mgiu/ xhtacqcj7mYboTbGP1Z3rWMXdcMOjSoAv4S7BfOUHKjwh747DGgBJkJ4rgl2xc9LsJG7 SrfQyZrob+D9sITJ/O3aByKCECXQFDwteK24OqpwK8epZvb1KO7ZoOZDAXsiCiDNLxk7 4/yA== X-Forwarded-Encrypted: i=1; AJvYcCW3qI+kCnv+IxOmue1APE9Pjm4eX2jSw/DaE0z7UhaNreMSL29WypeLEFX8m0J8JtjxBPs6JuHERx2j7t/aFoGFfGoGpkFDxxh+bldVNIHwLefVAMA= X-Gm-Message-State: AOJu0Yz9NXm4KUg18yxIWH9FqU7U2Etvd9I+sQdMwAQV4f4XSkR6lRYl wpKJb+GQj9nhbVIWrBu3JvrkFD8RO00saaXOd7IWBB6AWYc6ANTsGFDSPzk9ii8x38zkDJKp/6O YXQ== X-Google-Smtp-Source: AGHT+IE4QseE8FQyDppGpUcmrzEgygvCajM4BiXmkGQ8bCrs1hHMNuBsnO5u2Op4CzG6c9mnubgf1na2VnA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:f683:b0:1fc:27be:42dd with SMTP id d9443c01a7336-1ff0479bbb4mr799825ad.1.1722037964087; Fri, 26 Jul 2024 16:52:44 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:12 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-4-seanjc@google.com> Subject: [PATCH v12 03/84] KVM: Drop KVM_ERR_PTR_BAD_PAGE and instead return NULL to indicate an error From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165245_771711_E3103CA5 X-CRM114-Status: GOOD ( 13.69 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Remove KVM_ERR_PTR_BAD_PAGE and instead return NULL, as "bad page" is just a leftover bit of weirdness from days of old when KVM stuffed a "bad" page into the guest instead of actually handling missing pages. See commit cea7bb21280e ("KVM: MMU: Make gfn_to_page() always safe"). Signed-off-by: Sean Christopherson Reviewed-by: Alex Bennée --- arch/powerpc/kvm/book3s_pr.c | 2 +- arch/powerpc/kvm/book3s_xive_native.c | 2 +- arch/s390/kvm/vsie.c | 2 +- arch/x86/kvm/lapic.c | 2 +- include/linux/kvm_host.h | 7 ------- virt/kvm/kvm_main.c | 15 ++++++--------- 6 files changed, 10 insertions(+), 20 deletions(-) diff --git a/arch/powerpc/kvm/book3s_pr.c b/arch/powerpc/kvm/book3s_pr.c index a7d7137ea0c8..1bdcd4ee4813 100644 --- a/arch/powerpc/kvm/book3s_pr.c +++ b/arch/powerpc/kvm/book3s_pr.c @@ -645,7 +645,7 @@ static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte) int i; hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT); - if (is_error_page(hpage)) + if (!hpage) return; hpage_offset = pte->raddr & ~PAGE_MASK; diff --git a/arch/powerpc/kvm/book3s_xive_native.c b/arch/powerpc/kvm/book3s_xive_native.c index 6e2ebbd8aaac..d9bf1bc3ff61 100644 --- a/arch/powerpc/kvm/book3s_xive_native.c +++ b/arch/powerpc/kvm/book3s_xive_native.c @@ -654,7 +654,7 @@ static int kvmppc_xive_native_set_queue_config(struct kvmppc_xive *xive, } page = gfn_to_page(kvm, gfn); - if (is_error_page(page)) { + if (!page) { srcu_read_unlock(&kvm->srcu, srcu_idx); pr_err("Couldn't get queue page %llx!\n", kvm_eq.qaddr); return -EINVAL; diff --git a/arch/s390/kvm/vsie.c b/arch/s390/kvm/vsie.c index 54deafd0d698..566697ee37eb 100644 --- a/arch/s390/kvm/vsie.c +++ b/arch/s390/kvm/vsie.c @@ -661,7 +661,7 @@ 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 (is_error_page(page)) + if (!page) return -EINVAL; *hpa = (hpa_t)page_to_phys(page) + (gpa & ~PAGE_MASK); return 0; diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index a7172ba59ad2..6d65b36fac29 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2629,7 +2629,7 @@ int kvm_alloc_apic_access_page(struct kvm *kvm) } page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); - if (is_error_page(page)) { + if (!page) { ret = -EFAULT; goto out; } diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 689e8be873a7..3d9617d1de41 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -153,13 +153,6 @@ static inline bool kvm_is_error_gpa(gpa_t gpa) return gpa == INVALID_GPA; } -#define KVM_ERR_PTR_BAD_PAGE (ERR_PTR(-ENOENT)) - -static inline bool is_error_page(struct page *page) -{ - return IS_ERR(page); -} - #define KVM_REQUEST_MASK GENMASK(7,0) #define KVM_REQUEST_NO_WAKEUP BIT(8) #define KVM_REQUEST_WAIT BIT(9) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d0788d0a72cc..fd8c212b8de7 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3085,19 +3085,14 @@ EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic); */ struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) { - struct page *page; kvm_pfn_t pfn; pfn = gfn_to_pfn(kvm, gfn); if (is_error_noslot_pfn(pfn)) - return KVM_ERR_PTR_BAD_PAGE; + return NULL; - page = kvm_pfn_to_refcounted_page(pfn); - if (!page) - return KVM_ERR_PTR_BAD_PAGE; - - return page; + return kvm_pfn_to_refcounted_page(pfn); } EXPORT_SYMBOL_GPL(gfn_to_page); @@ -3191,7 +3186,8 @@ static void kvm_set_page_accessed(struct page *page) void kvm_release_page_clean(struct page *page) { - WARN_ON(is_error_page(page)); + if (WARN_ON(!page)) + return; kvm_set_page_accessed(page); put_page(page); @@ -3215,7 +3211,8 @@ EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); void kvm_release_page_dirty(struct page *page) { - WARN_ON(is_error_page(page)); + if (WARN_ON(!page)) + return; kvm_set_page_dirty(page); kvm_release_page_clean(page); From patchwork Fri Jul 26 23:51:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743496 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9FB7BC3DA49 for ; Sat, 27 Jul 2024 00:12:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=xiBZUTde5/yOu+VhqaXt4CYe/LeOwSdHW5ydN5gOuKQ=; b=AiOtqLr7twYR2AuctWPKbH8Hy5 tg0QaPzyDMmYCcW+QPybIyjNeQHj4Kwp7d6BAjYXmMC8/GUperE2NIFJTtKPVBsNG16fi5ydrgl1w 9JtKCqG4geXFdVr3J73jbl4nFSLUKgQQwc+ep5CTnUCVK/GRrseuNG/UTrZS2mfhWjRARhz2gD9k8 FHamGQTPbSClWx/XLTXdrylqh6t0rojMurvA7rUb1iqHJGK9zbEDvQ7GDkuJQ3dbOTonbUDmQVzVk d65E6SBft7+MPOvAQ1w6IJVyt3dALrJcY4agRd9gvNXr1MgdlfYQNlqsHTkJgnjY1pRJyT3/SBFLz V1FR/YCw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV2f-00000005aCf-3SB4; Sat, 27 Jul 2024 00:12:09 +0000 Received: from mail-pf1-x449.google.com ([2607:f8b0:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUjv-00000005PBr-11mM for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:52:48 +0000 Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-70d188c9cabso1355985b3a.0 for ; Fri, 26 Jul 2024 16:52:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037966; x=1722642766; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=xiBZUTde5/yOu+VhqaXt4CYe/LeOwSdHW5ydN5gOuKQ=; b=C/UvVZLkZ4F6Rt5/HCv5SHiklzFOUg5x++bUB4Zx/yNmtMxOWXgkZ5pdeRwFJrmMS1 tfksl1URwEirZcfc2oR6J7aEnhWuXJr8O7Us9uVLZppwoJ2gAQ1OvEyg8UEJ+DVZ0b4w lUsup/LIqazSvP+Q7T8bWuABqPCGcxNIAN8QEgvk8iJmOdZW1CshL8zWsXUz7SZdXxRn XaaKkuO0yvCZjpGCc/7pgb30A8fF6Z4EhXLcabcYekP5E7s8FmSevht1N4v/LcPKWhij 0D7BkfCMt7YMNcJ4h1KJhG1ZGoUh2E4GbivtRiCZl7SFH3Fv9e4KmCXkKaEtqAzXYRTa 7e7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037966; x=1722642766; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xiBZUTde5/yOu+VhqaXt4CYe/LeOwSdHW5ydN5gOuKQ=; b=SpyoHrAa/r88tXgpaXUYN9EWJgdSpanccWOQ2JE2RDumd121uZhfkcOEyixJaDqsg6 jUbXYP7Fk4EX/Dx2M2r+ZrUue6QQQOj6kzREpMwjUYc1mzf8ePAYbfV/CvZ5yoW/ZxDD peLYleOpD2ckLtLd5UaEURSNIMdxJ7y7/YRMBudxhB3hzoT3OYyK8pvo7+AelSYvpKCX mYO7/P+osev5CcvBtKPqh9M104QgbJa6sQMYs1BqhIWWr27sW0mEcb8zxi9ZpVQcvDlX b39vdJVk5UZ95Giee5JzqRrK5+rwjH0c6W4ZDYmWAOxvY1nP2Flrf6OivkqG/n731PoS yvcA== X-Forwarded-Encrypted: i=1; AJvYcCWvZ2+qwCu9aW9E3oepH5msJOzcIp835JO/IfmBcsDEOQUmnVfLUJJyTa3Os45OItz+qA9lkFn51guWeYN4cIIOU9xvG9I2m2YWefuUMSgZVPa9lcA= X-Gm-Message-State: AOJu0Yxx9mNPtCJ/hoPPPP1NO2RwcVmxLh144fN16una1uykMA+rlOZn RcCumtbslXCbUJ1MX8LLkP79Rig+M0LSJgk2k7C9/s4fO4R43ZIFTBdNKDQgMa/JSlSzpWDnFRq 3xA== X-Google-Smtp-Source: AGHT+IFeDzTSYc0yloJqv412y/9OHddQLGngXAZq8SjSSTQr+cl72qk3J3BVbl3XjHWFKDqTN/CvEh64V1I= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:8703:b0:70d:1285:bbcf with SMTP id d2e1a72fcca58-70ece93a09amr3050b3a.0.1722037965825; Fri, 26 Jul 2024 16:52:45 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:13 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-5-seanjc@google.com> Subject: [PATCH v12 04/84] KVM: Allow calling kvm_release_page_{clean,dirty}() on a NULL page pointer From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165247_335095_B43B1B57 X-CRM114-Status: GOOD ( 11.88 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Allow passing a NULL @page to kvm_release_page_{clean,dirty}(), there's no tangible benefit to forcing the callers to pre-check @page, and it ends up generating a lot of duplicate boilerplate code. Signed-off-by: Sean Christopherson Reviewed-by: Alex Bennée --- virt/kvm/kvm_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index fd8c212b8de7..656e931ac39e 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3186,7 +3186,7 @@ static void kvm_set_page_accessed(struct page *page) void kvm_release_page_clean(struct page *page) { - if (WARN_ON(!page)) + if (!page) return; kvm_set_page_accessed(page); @@ -3211,7 +3211,7 @@ EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); void kvm_release_page_dirty(struct page *page) { - if (WARN_ON(!page)) + if (!page) return; kvm_set_page_dirty(page); From patchwork Fri Jul 26 23:51:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743497 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 516C3C3DA4A for ; Sat, 27 Jul 2024 00:12:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=1PAT6v4tn5ltatT/kjen/UunIT1fRhHnj971W9WBYQU=; b=4N6RrjZWpy5aPwi+z8+lU5vDAu g4BHJ/4IpYvuixHgntZoW87dtVPHcYnJM3bB5QtCe6pfplk81Xnl1fwJ2R6HRgDhCL2P7OoCtmdT2 4RGRubmYkDued+ZsoiizW00M2eDBaG+uem6tuwlcP6ZWE8aR2WIp/VGD16lboITOS3pPpXlakkSH3 nCcTJB0e6qO7GG8N2xJ3RfQnSh//LCl/jS5k+0FKQs/rUt6Z78vVjJcYenQ0EW6rvShKPoqhulQwt 219teXmjPLSfKJbladRGUpYzT3TLRi3g7v+AUloUHEyAKRzPToH5J/+eD6nnbSpAcayYIJeiaM5b3 uhWAmkKA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV34-00000005aN0-17Zk; Sat, 27 Jul 2024 00:12:34 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUjx-00000005PDF-1Qkv for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:52:51 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70d3777de60so1297936b3a.0 for ; Fri, 26 Jul 2024 16:52:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037968; x=1722642768; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=1PAT6v4tn5ltatT/kjen/UunIT1fRhHnj971W9WBYQU=; b=HMSGo54v2C736ldxredd1NWPEBPQFzVNZgdPzH6XIqWfwxtJ8GUx7ZBhWEOXgyjXHn TjTbkAUX1+dFtyZjSRH1pzG9XY63Ebf5KLpDur6pBI62UyGjYgn1tUTnHcoytnzwHWLX BZ+FuWugPVJ47IS2DUnShAUApuO+3+2OW8XkhlBuXwnghlm8Xg1q1/BCoqWWNoQmyptZ QRXBmm9kMpRKl6Dd2X8TTxWC8j4id88kj3X0jgev2MfxMTHD5fGF7m8hhmEYMToSXbtd 2Itqj6AiObO0CwRy5Au4SO6kmLCiB18w7Ztn6cjOg08qIOVQuRqPAH39FdxM6eMtmZQB cAvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037968; x=1722642768; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=1PAT6v4tn5ltatT/kjen/UunIT1fRhHnj971W9WBYQU=; b=dDLwLo33zij1O6sTAzmJYOyU2Kp5zjMFZohehsFGq2XRzNOqi/ymtaCV5Y+jfzWdQX QOmM9aG3CwDnjz/upaP4raQC30Q18vK7HPqVA2gb6VqXoRrZZRzOeWcigA06XC6tZSsu aRRmQytboSz1KhhS7n4N0wqEqGrXvhUsWnujsqKp0WVLWcSCvjdgIM624+PZTc8y5ylq lqaI2gOTfULgA/ys0hSrspI3ajNzd1j6w5eRy8Q0UWt4iCyNE6sGZTRTiA7UfUYX9WhH YobBi0t9Bkz1intD/IFEPosXCMIme0anQxsuLkaEmvBWZZXVpKyIBeKqBm4ct+EeqZuF YFlQ== X-Forwarded-Encrypted: i=1; AJvYcCUQowA1zPqYpXkndisO+6UZlZoYcTh6iMbGJheaPAS6v+bYQYn1eAfmMgvOsShZziPSObmNJaz955BOjS/+qJMJex23La5ZwAG8J2aVoyTBLwkmHtM= X-Gm-Message-State: AOJu0YzXweuYgePIQPJFleu1Pl81BMmJcgro5+p75Yn9aY1JgVLAXUf7 ZWSuKMVmltTQ6o10/qzmnzuSho+mKaUeOMKAqkaTAmPF9JXyZKn2v7icmH6mX8fLN8ttLvTy4ak RFA== X-Google-Smtp-Source: AGHT+IGybwcUaaDWGaRnVlNYMzL2lybkBwi+pdiFBv1toqDzA479d7SifMoC4AbRMiwOqaRTolCzzwjRN5U= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:731c:b0:70d:13c2:1d08 with SMTP id d2e1a72fcca58-70ecedb0dfamr2514b3a.3.1722037967857; Fri, 26 Jul 2024 16:52:47 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:14 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-6-seanjc@google.com> Subject: [PATCH v12 05/84] KVM: Add kvm_release_page_unused() API to put pages that KVM never consumes From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165249_404220_39ADF0AA X-CRM114-Status: GOOD ( 10.19 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add an API to release an unused page, i.e. to put a page without marking it accessed or dirty. The API will be used when KVM faults-in a page but bails before installing the guest mapping (and other similar flows). Signed-off-by: Sean Christopherson Reviewed-by: Alex Bennée --- include/linux/kvm_host.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 3d9617d1de41..c5d39a337aa3 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1201,6 +1201,15 @@ unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable); unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn); unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, gfn_t gfn, bool *writable); + +static inline void kvm_release_page_unused(struct page *page) +{ + if (!page) + return; + + put_page(page); +} + void kvm_release_page_clean(struct page *page); void kvm_release_page_dirty(struct page *page); From patchwork Fri Jul 26 23:51:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743506 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 013C2C3DA4A for ; Sat, 27 Jul 2024 00:13:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=VnEAHRC1zG591I39OowzIjQNXTqQsRDXyh/rUn6sicQ=; b=Pq34nSLWPqEOjfCokqdb2niqbf wF1N4BkhSJ44pOUJvCNwaoHvtMomhZozRwI5FYdNbBd+fgTeoCK9+FRW2FzYI7CMu4NgGN7ZC0FCb VNkF1PspXF1J8UU7kAdrHtILzKCGe1P7mukWzSobvBvKAs1UzqSdMP2oR7XWc/PtOtzVlOULmmZfn 0TtSSUPS+bd12XmeiPQJxyeaF82EooQtKzdBzDRbgg+PzlLiqbwQePYrZj8ndULFuElAYQvvFQLag wgBlt80Ztsoc9tRswf/HWhYhmm/QkcpOS3lDj1UeiqHK7T1Y4o3qxgWf+66wfpzg/jsqWHxQ1aB+c At6feCng==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV3R-00000005aX0-2fpX; Sat, 27 Jul 2024 00:12:57 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUjz-00000005PEl-2RW5 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:52:53 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-668e964ffd9so6340627b3.3 for ; Fri, 26 Jul 2024 16:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037970; x=1722642770; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=VnEAHRC1zG591I39OowzIjQNXTqQsRDXyh/rUn6sicQ=; b=Zv2y5TGBu4jNjYjDqYMpQ7ctqA0RMqIRWZC1+t8BWrFI4iRKunriZE25YMEHTn5JN6 3Mp1PzUU2/0hJSPDCCgx4sx6PhAFzeCrSkf3hvV4Zaq78s0DPV7TJv34rhVCVpaHb62M r24KMJq7BXh0RbOeXcYCAAYdIwmgkOhouuTXfZM0vzugLZteTUFPOf1901Q17cSB2p+u qjVxn5ZS2KnY3PjEw2/oqb4IXElFT84/rvXh3DKfIDh/bwbck2zqTepCGKgY7XB6BZgJ pi85lCwbKL/Ajhlkj7OwnOyeCrqP10BMBchVSody8iZKmohVCSLhssfe9MTEC/YRdwor /Y1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037970; x=1722642770; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VnEAHRC1zG591I39OowzIjQNXTqQsRDXyh/rUn6sicQ=; b=k/NHEEMCqhAA5aS3HSwVXMUL7/13cbKMQI0GLbjKpa//l+eCFWKaRSlFh7WaT73BYO 0eU6zu5Pso/8yp+dwMHym5KWzFlGgK4TSLmgOUPnh+fdCzon+YRKzE4iOLKHyIM5ixjS EQntzRFUAH2LrIPiVbeV4ZFQxOiMnJdZlkZjW+s7yvAiTrGCehktsEZ8En4179NytlPd U1QC0wxscpMWqlq9fE1n+L5aAGyqsmKOadKyIhdE2roMgwxov27Ow4VRoGBQ1jch+woJ eGhZeZlk+UWovQuKz7T+bEgh8RYS937dgGI6hX2666/YoYNwVgaHVRDb2oYIJxprWAB4 Z/2g== X-Forwarded-Encrypted: i=1; AJvYcCUSF8zt1N1jDHNxvsFwb8wnbyQ+YL0qKcYlpVinp5bdS1Io2Q9+75wZgSTLi+CnzKy9ZyGLlnM3ZcVpQ3d6NVs52j4sK23oSSaDSgGNJTOmtyDnTy8= X-Gm-Message-State: AOJu0Yz1fhjFm8jctIiaqTrXBg0K8pjw6/pnUQZE8AKCygrkLXrsdnzt 8P9Q19xv8VHJLsEWkvQR5MlWL5X3pPdQ/HENA8quOjs7gMDVGfYFJ7hmwh2LtAUI0jDRlyiIeJW 3zQ== X-Google-Smtp-Source: AGHT+IFPOoUYsYyJmRjA25F0UYC9yeYKs61mDA/QqS/vgTcAgpqjjzQfUfGqPl0MU2c7vZElDBuLLOtKUts= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:fc9:b0:648:afcb:a7ce with SMTP id 00721157ae682-67a05a9d90bmr269367b3.3.1722037969718; Fri, 26 Jul 2024 16:52:49 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:15 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-7-seanjc@google.com> Subject: [PATCH v12 06/84] KVM: x86/mmu: Skip the "try unsync" path iff the old SPTE was a leaf SPTE From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165251_661781_648493CF X-CRM114-Status: GOOD ( 14.15 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Apply make_spte()'s optimization to skip trying to unsync shadow pages if and only if the old SPTE was a leaf SPTE, as non-leaf SPTEs in direct MMUs are always writable, i.e. could trigger a false positive and incorrectly lead to KVM creating a SPTE without write-protecting or marking shadow pages unsync. This bug only affects the TDP MMU, as the shadow MMU only overwrites a shadow-present SPTE when synchronizing SPTEs (and only 4KiB SPTEs can be unsync). Specifically, mmu_set_spte() drops any non-leaf SPTEs *before* calling make_spte(), whereas the TDP MMU can do a direct replacement of a page table with the leaf SPTE. Opportunistically update the comment to explain why skipping the unsync stuff is safe, as opposed to simply saying "it's someone else's problem". Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/spte.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c index d4527965e48c..a3baf0cadbee 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -226,12 +226,20 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, spte |= PT_WRITABLE_MASK | shadow_mmu_writable_mask; /* - * Optimization: for pte sync, if spte was writable the hash - * lookup is unnecessary (and expensive). Write protection - * is responsibility of kvm_mmu_get_page / kvm_mmu_sync_roots. - * Same reasoning can be applied to dirty page accounting. + * When overwriting an existing leaf SPTE, and the old SPTE was + * writable, skip trying to unsync shadow pages as any relevant + * shadow pages must already be unsync, i.e. the hash lookup is + * unnecessary (and expensive). + * + * The same reasoning applies to dirty page/folio accounting; + * KVM will mark the folio dirty using the old SPTE, thus + * there's no need to immediately mark the new SPTE as dirty. + * + * Note, both cases rely on KVM not changing PFNs without first + * zapping the old SPTE, which is guaranteed by both the shadow + * MMU and the TDP MMU. */ - if (is_writable_pte(old_spte)) + if (is_last_spte(old_spte, level) && is_writable_pte(old_spte)) goto out; /* From patchwork Fri Jul 26 23:51:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743507 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 731F9C3DA49 for ; Sat, 27 Jul 2024 00:13:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4JK9GTJ8OrY9PouHIVM6/FWVao5wKjRYWblDInuyPC8=; b=cVie3NfVKenrvulNuc80o18nb6 O0P8DAfGQiqRRzfdEF1RHcoQduO1zXAwqKVIuyg8I2PJ7FRvdseN4jDW4SFfm6VmDcrx0D4fx5Urr FQaJvm0jvjhkbItmYk9GsR6PA8KheQuQPb5rS1pBvBqibHqLGvpPyMd7HdaeOMxD3VkXT61HwFLFT /nqZPuU89Y+672xaO+VXTSOYc3DEZWXqe8N6pE/lW0QmPsHAa8dVNi3rAC5sVQFTQUyOX+x3oIVyU fph5SeilaVCp4Oc0XEqt/0JsbBllRCJh80WCJR6NycNFQkiOXvJZiDPKUvwYJmFPqwg4t8Be9piv2 aFnTd/ww==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV3p-00000005aff-0PYa; Sat, 27 Jul 2024 00:13:21 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUk1-00000005PGO-0ijn for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:52:56 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-66480c1a6b5so7083897b3.1 for ; Fri, 26 Jul 2024 16:52:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037972; x=1722642772; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=4JK9GTJ8OrY9PouHIVM6/FWVao5wKjRYWblDInuyPC8=; b=SLSXESt3X3wr6b2idKdqGZrU41n4S4fegws7kg0onVL6LxdEfd+P2zW69FM6TMbF08 c4D/XbLuOMtw+3tygFg4WFsV2o3aIdzs6lA7ZpnGQl4Xq7wjwwnY5SWyHMjRwF2RxYdB BU2R62sIBiiXvizJEw4no6EiAZkFQoJUn4fb0TBeKs1G3+/M0UVqiP20LdRN4D07/g5B Htw+Wbg2GpV04j2ffsWp8iO0QS2oyGBtzlNd9wtNCEvsP2pQ36ba2nXUTePr1AV1uiOp ZHMas83nGgEmVkyuObh7yUR34tcJlzVRhcvuHNc37Lh168Coz2xbK/nTaZXbClEiiR6D +FqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037972; x=1722642772; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4JK9GTJ8OrY9PouHIVM6/FWVao5wKjRYWblDInuyPC8=; b=Vv+Mxtmt9N4kE40E9QuEug2STIvUH+KPPLpjrM377Outegttfq+UXh6JjE0Y6/iJrS FPAUy2HVWVKiqXuPrfvuvZL4IX8nvPtrf4pRTvPHurQzgHZKR0rKz3dIU2WQlN7TuP2S 7GtzfGySZn30kuQRJhoY+scV4TINrPYo26PZAK5H9GPY0qYFKOaheSUk/79nizgP/iHd 2ik6pu5sXrnKH0eB4EOL3rTc/JCeem0E5TSffgJ8ASYydluu6gFb5DPbasJs1aMpQfPh t41G3xx2uFcdZIa1CEUoaCyNnFGm+UpL0FQSrj5n0uJ24EVAFPU2oXrHxmmjdqMrnoh0 Y6rQ== X-Forwarded-Encrypted: i=1; AJvYcCVNTXFfGS3Yt8eVs9uOdRwqTtfS47wPwkqRl+PmzXgWoy1OKwd6NZr+F97GtUDdshZP/oREXGx0KY6lngsePV29YLo9zVfsRyVtEE2pvcN0gzRHPsM= X-Gm-Message-State: AOJu0YyXEkRL7PgRjSVrhc7HsdzAQSTlntw0pFUBHgKiOaXLJzk82drr O8lRCAuJQpdv7MyCVUD+DWZgaYLGATx/VOkijH5cURt1NBfmnWSQtxOieozrGlPeaExxuT2P5wg UNA== X-Google-Smtp-Source: AGHT+IHPFxwOMM7hKklIc91WzRGi/y9tY1tDNjJ/P6YzbKycIVe3cl0TyRdV2w5qTWUMgv20iZ/1nODLU/M= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:fc9:b0:64b:a85:e2c5 with SMTP id 00721157ae682-67a05b92dafmr390537b3.3.1722037971723; Fri, 26 Jul 2024 16:52:51 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:16 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-8-seanjc@google.com> Subject: [PATCH v12 07/84] KVM: x86/mmu: Mark folio dirty when creating SPTE, not when zapping/modifying From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165253_393390_F30DD0FC X-CRM114-Status: GOOD ( 29.85 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark pages/folios dirty when creating SPTEs to map PFNs into the guest, not when zapping or modifying SPTEs, as marking folios dirty when zapping or modifying SPTEs can be extremely inefficient. E.g. when KVM is zapping collapsible SPTEs to reconstitute a hugepage after disbling dirty logging, KVM will mark every 4KiB pfn as dirty, even though _at least_ 512 pfns are guaranteed to be in a single folio (the SPTE couldn't potentially be huge if that weren't the case). The problem only becomes worse for 1GiB HugeTLB pages, as KVM can mark a single folio dirty 512*512 times. Marking a folio dirty when mapping is functionally safe as KVM drops all relevant SPTEs in response to an mmu_notifier invalidation, i.e. ensures that the guest can't dirty a folio after access has been removed. And because KVM already marks folios dirty when zapping/modifying SPTEs for KVM reasons, i.e. not in response to an mmu_notifier invalidation, there is no danger of "prematurely" marking a folio dirty. E.g. if a filesystems cleans a folio without first removing write access, then there already exists races where KVM could mark a folio dirty before remote TLBs are flushed, i.e. before guest writes are guaranteed to stop. Furthermore, x86 is literally the only architecture that marks folios dirty on the backend; every other KVM architecture marks folios dirty at map time. x86's unique behavior likely stems from the fact that x86's MMU predates mmu_notifiers. Long, long ago, before mmu_notifiers were added, marking pages dirty when zapping SPTEs was logical, and perhaps even necessary, as KVM held references to pages, i.e. kept a page's refcount elevated while the page was mapped into the guest. At the time, KVM's rmap_remove() simply did: if (is_writeble_pte(*spte)) kvm_release_pfn_dirty(pfn); else kvm_release_pfn_clean(pfn); i.e. dropped the refcount and marked the page dirty at the same time. After mmu_notifiers were introduced, commit acb66dd051d0 ("KVM: MMU: don't hold pagecount reference for mapped sptes pages") removed the refcount logic, but kept the dirty logic, i.e. converted the above to: if (is_writeble_pte(*spte)) kvm_release_pfn_dirty(pfn); And for KVM x86, that's essentially how things have stayed over the last ~15 years, without anyone revisiting *why* KVM marks pages/folios dirty at zap/modification time, e.g. the behavior was blindly carried forward to the TDP MMU. Practically speaking, the only downside to marking a folio dirty during mapping is that KVM could trigger writeback of memory that was never actually written. Except that can't actually happen if KVM marks folios dirty if and only if a writable SPTE is created (as done here), because KVM always marks writable SPTEs as dirty during make_spte(). See commit 9b51a63024bd ("KVM: MMU: Explicitly set D-bit for writable spte."), circa 2015. Note, KVM's access tracking logic for prefetched SPTEs is a bit odd. If a guest PTE is dirty and writable, KVM will create a writable SPTE, but then mark the SPTE for access tracking. Which isn't wrong, just a bit odd, as it results in _more_ precise dirty tracking for MMUs _without_ A/D bits. To keep things simple, mark the folio dirty before access tracking comes into play, as an access-tracked SPTE can be restored in the fast page fault path, i.e. without holding mmu_lock. While writing SPTEs and accessing memslots outside of mmu_lock is safe, marking a folio dirty is not. E.g. if the fast path gets interrupted _just_ after setting a SPTE, the primary MMU could theoretically invalidate and free a folio before KVM marks it dirty. Unlike the shadow MMU, which waits for CPUs to respond to an IPI, the TDP MMU only guarantees the page tables themselves won't be freed (via RCU). Opportunistically update a few stale comments. Cc: David Matlack Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 29 ++++------------------------- arch/x86/kvm/mmu/paging_tmpl.h | 6 +++--- arch/x86/kvm/mmu/spte.c | 20 ++++++++++++++++++-- arch/x86/kvm/mmu/tdp_mmu.c | 12 ------------ 4 files changed, 25 insertions(+), 42 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 901be9e420a4..2e6daa6d1cc0 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -547,10 +547,8 @@ static bool mmu_spte_update(u64 *sptep, u64 new_spte) kvm_set_pfn_accessed(spte_to_pfn(old_spte)); } - if (is_dirty_spte(old_spte) && !is_dirty_spte(new_spte)) { + if (is_dirty_spte(old_spte) && !is_dirty_spte(new_spte)) flush = true; - kvm_set_pfn_dirty(spte_to_pfn(old_spte)); - } return flush; } @@ -593,9 +591,6 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) if (is_accessed_spte(old_spte)) kvm_set_pfn_accessed(pfn); - if (is_dirty_spte(old_spte)) - kvm_set_pfn_dirty(pfn); - return old_spte; } @@ -626,13 +621,6 @@ static bool mmu_spte_age(u64 *sptep) clear_bit((ffs(shadow_accessed_mask) - 1), (unsigned long *)sptep); } else { - /* - * Capture the dirty status of the page, so that it doesn't get - * lost when the SPTE is marked for access tracking. - */ - if (is_writable_pte(spte)) - kvm_set_pfn_dirty(spte_to_pfn(spte)); - spte = mark_spte_for_access_track(spte); mmu_spte_update_no_track(sptep, spte); } @@ -1275,16 +1263,6 @@ static bool spte_clear_dirty(u64 *sptep) return mmu_spte_update(sptep, spte); } -static bool spte_wrprot_for_clear_dirty(u64 *sptep) -{ - bool was_writable = test_and_clear_bit(PT_WRITABLE_SHIFT, - (unsigned long *)sptep); - if (was_writable && !spte_ad_enabled(*sptep)) - kvm_set_pfn_dirty(spte_to_pfn(*sptep)); - - return was_writable; -} - /* * Gets the GFN ready for another round of dirty logging by clearing the * - D bit on ad-enabled SPTEs, and @@ -1300,7 +1278,8 @@ static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head, for_each_rmap_spte(rmap_head, &iter, sptep) if (spte_ad_need_write_protect(*sptep)) - flush |= spte_wrprot_for_clear_dirty(sptep); + flush |= test_and_clear_bit(PT_WRITABLE_SHIFT, + (unsigned long *)sptep); else flush |= spte_clear_dirty(sptep); @@ -3381,7 +3360,7 @@ static bool fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, * harm. This also avoids the TLB flush needed after setting dirty bit * so non-PML cases won't be impacted. * - * Compare with set_spte where instead shadow_dirty_mask is set. + * Compare with make_spte() where instead shadow_dirty_mask is set. */ if (!try_cmpxchg64(sptep, &old_spte, new_spte)) return false; diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index 69941cebb3a8..ef0b3b213e5b 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -891,9 +891,9 @@ static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, /* * Using the information in sp->shadowed_translation (kvm_mmu_page_get_gfn()) is - * safe because: - * - The spte has a reference to the struct page, so the pfn for a given gfn - * can't change unless all sptes pointing to it are nuked first. + * safe because SPTEs are protected by mmu_notifiers and memslot generations, so + * the pfn for a given gfn can't change unless all SPTEs pointing to the gfn are + * nuked first. * * Returns * < 0: failed to sync spte diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c index a3baf0cadbee..9b8795bd2f04 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -232,8 +232,8 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, * unnecessary (and expensive). * * The same reasoning applies to dirty page/folio accounting; - * KVM will mark the folio dirty using the old SPTE, thus - * there's no need to immediately mark the new SPTE as dirty. + * KVM marked the folio dirty when the old SPTE was created, + * thus there's no need to mark the folio dirty again. * * Note, both cases rely on KVM not changing PFNs without first * zapping the old SPTE, which is guaranteed by both the shadow @@ -266,12 +266,28 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, "spte = 0x%llx, level = %d, rsvd bits = 0x%llx", spte, level, get_rsvd_bits(&vcpu->arch.mmu->shadow_zero_check, spte, level)); + /* + * Mark the memslot dirty *after* modifying it for access tracking. + * Unlike folios, memslots can be safely marked dirty out of mmu_lock, + * i.e. in the fast page fault handler. + */ if ((spte & PT_WRITABLE_MASK) && kvm_slot_dirty_track_enabled(slot)) { /* Enforced by kvm_mmu_hugepage_adjust. */ WARN_ON_ONCE(level > PG_LEVEL_4K); mark_page_dirty_in_slot(vcpu->kvm, slot, gfn); } + /* + * If the page that KVM got from the primary MMU is writable, i.e. if + * it's host-writable, mark the page/folio dirty. As alluded to above, + * folios can't be safely marked dirty in the fast page fault handler, + * and so KVM must (somewhat) speculatively mark the folio dirty even + * though it isn't guaranteed to be written as KVM won't mark the folio + * dirty if/when the SPTE is made writable. + */ + if (host_writable) + kvm_set_pfn_dirty(pfn); + *new_spte = spte; return wrprot; } diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index c7dc49ee7388..7ac43d1ce918 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -511,10 +511,6 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, if (is_leaf != was_leaf) kvm_update_page_stats(kvm, level, is_leaf ? 1 : -1); - if (was_leaf && is_dirty_spte(old_spte) && - (!is_present || !is_dirty_spte(new_spte) || pfn_changed)) - kvm_set_pfn_dirty(spte_to_pfn(old_spte)); - /* * Recursively handle child PTs if the change removed a subtree from * the paging structure. Note the WARN on the PFN changing without the @@ -1248,13 +1244,6 @@ static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter, iter->level); new_spte = iter->old_spte & ~shadow_accessed_mask; } else { - /* - * Capture the dirty status of the page, so that it doesn't get - * lost when the SPTE is marked for access tracking. - */ - if (is_writable_pte(iter->old_spte)) - kvm_set_pfn_dirty(spte_to_pfn(iter->old_spte)); - new_spte = mark_spte_for_access_track(iter->old_spte); iter->old_spte = kvm_tdp_mmu_write_spte(iter->sptep, iter->old_spte, new_spte, @@ -1595,7 +1584,6 @@ static void clear_dirty_pt_masked(struct kvm *kvm, struct kvm_mmu_page *root, trace_kvm_tdp_mmu_spte_changed(iter.as_id, iter.gfn, iter.level, iter.old_spte, iter.old_spte & ~dbit); - kvm_set_pfn_dirty(spte_to_pfn(iter.old_spte)); } rcu_read_unlock(); From patchwork Fri Jul 26 23:51:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743432 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7E3AEC3DA49 for ; Fri, 26 Jul 2024 23:56:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Dpyi/TQvAabfHqvBiXmrWwwqtiSCC0ukXFLvczoDhHE=; b=cLjJtjStIwiWyS/mNncJSPz6wq DveI4x1B7/ZJV1NO4oGi1jRUn2XRlFUbRJ8DMBcI9j+XsGzkDkpN2mXBlqAKHQv2GZNR/AOAZ+bUV EAwNScELp/gHnSbwhRKrgsjeAw6mZy3tt9agZZ7Ogs6ZlGyk/zxukpAXh8Pi/m6pdA0wGXhhhHKgb /plLMwi1/CcqCRvIbd/rIMdWHQha89Ea0InUqK9VTaw2OrczKNDAYct/V9N97AYh5SGFELnDFCssP JsYo+3XHm+4Vx4kDC62B4kmmLXO/sE/aGL9RNvXvjfSJBMHqDAQVZ6hbgtoY4IE3N+mOr11bza4bB ppot3H0w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUnJ-00000005Rvd-3QaY; Fri, 26 Jul 2024 23:56:17 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUk3-00000005PIB-01QX for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:52:58 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-6799b9a2161so6382557b3.3 for ; Fri, 26 Jul 2024 16:52:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037974; x=1722642774; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Dpyi/TQvAabfHqvBiXmrWwwqtiSCC0ukXFLvczoDhHE=; b=KlfbSK+3gItoshuo1nYuD1xlA9HwqdpDfTcLeh5n4so7bIF0p3oDjFCYpWEu593IDl XEek7Zngo1AIQVqhxWQFUBJ64eaelkSX2O9ByNM2qSTUEdRRVyxlfCO/3HoXu0FL4gw2 qFziuUYqh7C5ZhvmkHZW8hqVVsLOnGJXk+sf6sStxQCci8ot+WgFfrX6J1p7bwEZ9LGz GAc5jqheDziz4w8oh7+Uva2GSuFvesHPkuIZSWWDKoSJ8LRBInIIKEkAd424hxjDY1CM +CNZ0hiiUL1X7UBGAMoKX/R3jzcJuGCyaa3TW/RBmTJijljJJs2vP4gUTLJzXaUObYTU ZTYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037974; x=1722642774; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Dpyi/TQvAabfHqvBiXmrWwwqtiSCC0ukXFLvczoDhHE=; b=Hjeb0Gzg9DMS1BV9YKqi8u7BqH5x1IFEIE3PN+gnlsLUoA8jM1NOUimwrw0bc9I54o +hd0E1PRdT4w25/RhxuMYrelB1l7zTkTajKJP+LMeX6kP/TYUwlKNaHw1c2JxonH2gfD pSWwi77041GEBc2tqu26tUPb/B0jHPgt30IMTRRF0PtYZ2U3NXtYwMYzJipOjNl04w3R 1A9zmjkDsikZxw/XP0bh1PkoAVVIvs9D8C1eX/tHuV0KfhxoOfoeKWy766iHXAvdcDFH gSNxes7bJ5gIWLLH2oFuE4EJkw6hn7gTBnMybdIUr+L2Y5muFBvFgQQ7AavLhS/I/c9e qkPQ== X-Forwarded-Encrypted: i=1; AJvYcCXTwGzo5F8MC52qc0uwlbHN7IUAmMWRZpZHgt+MekWJn2By4G1MNAy2G2LMv9qAuNM2cPE8QCf3qem3biAurjw4BDPE9xow1pcRRGMwzeummBCaSko= X-Gm-Message-State: AOJu0Yw2yxek5+v0zJdWum+RWpxKdBh3XYoDsajWd02b5GnJDWr01k6c +Ec5gM8Mzv1z59muKxLijIheDxjGeg06wXujtZnVZehliXeh96IOQVDc/dTllHtuf3zrr26ubKK LfA== X-Google-Smtp-Source: AGHT+IEuz+UKgzDWJGr0lPjm3UdUlqhv4uVGAVLwalCQIXTZVrMJ3vp7bbZbHMGkSPe0yP2xueKpOueyV+8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:f07:b0:64a:e220:bfb5 with SMTP id 00721157ae682-67a051e9c33mr504237b3.1.1722037973756; Fri, 26 Jul 2024 16:52:53 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:17 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-9-seanjc@google.com> Subject: [PATCH v12 08/84] KVM: x86/mmu: Mark page/folio accessed only when zapping leaf SPTEs From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165255_224070_6765F2ED X-CRM114-Status: GOOD ( 17.54 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark folios as accessed only when zapping leaf SPTEs, which is a rough heuristic for "only in response to an mmu_notifier invalidation". Page aging and LRUs are tolerant of false negatives, i.e. KVM doesn't need to be precise for correctness, and re-marking folios as accessed when zapping entire roots or when zapping collapsible SPTEs is expensive and adds very little value. E.g. when a VM is dying, all of its memory is being freed; marking folios accessed at that time provides no known value. Similarly, because KVM marks folios as accessed when creating SPTEs, marking all folios as accessed when userspace happens to delete a memslot doesn't add value. The folio was marked access when the old SPTE was created, and will be marked accessed yet again if a vCPU accesses the pfn again after reloading a new root. Zapping collapsible SPTEs is a similar story; marking folios accessed just because userspace disable dirty logging is a side effect of KVM behavior, not a deliberate goal. As an intermediate step, a.k.a. bisection point, towards *never* marking folios accessed when dropping SPTEs, mark folios accessed when the primary MMU might be invalidating mappings, as such zappings are not KVM initiated, i.e. might actually be related to page aging and LRU activity. Note, x86 is the only KVM architecture that "double dips"; every other arch marks pfns as accessed only when mapping into the guest, not when mapping into the guest _and_ when removing from the guest. Signed-off-by: Sean Christopherson --- Documentation/virt/kvm/locking.rst | 76 +++++++++++++++--------------- arch/x86/kvm/mmu/mmu.c | 4 +- arch/x86/kvm/mmu/tdp_mmu.c | 7 ++- 3 files changed, 43 insertions(+), 44 deletions(-) diff --git a/Documentation/virt/kvm/locking.rst b/Documentation/virt/kvm/locking.rst index 02880d5552d5..8b3bb9fe60bf 100644 --- a/Documentation/virt/kvm/locking.rst +++ b/Documentation/virt/kvm/locking.rst @@ -138,49 +138,51 @@ Then, we can ensure the dirty bitmaps is correctly set for a gfn. 2) Dirty bit tracking -In the origin code, the spte can be fast updated (non-atomically) if the +In the original code, the spte can be fast updated (non-atomically) if the spte is read-only and the Accessed bit has already been set since the Accessed bit and Dirty bit can not be lost. But it is not true after fast page fault since the spte can be marked writable between reading spte and updating spte. Like below case: -+------------------------------------------------------------------------+ -| At the beginning:: | -| | -| spte.W = 0 | -| spte.Accessed = 1 | -+------------------------------------+-----------------------------------+ -| CPU 0: | CPU 1: | -+------------------------------------+-----------------------------------+ -| In mmu_spte_clear_track_bits():: | | -| | | -| old_spte = *spte; | | -| | | -| | | -| /* 'if' condition is satisfied. */| | -| if (old_spte.Accessed == 1 && | | -| old_spte.W == 0) | | -| spte = 0ull; | | -+------------------------------------+-----------------------------------+ -| | on fast page fault path:: | -| | | -| | spte.W = 1 | -| | | -| | memory write on the spte:: | -| | | -| | spte.Dirty = 1 | -+------------------------------------+-----------------------------------+ -| :: | | -| | | -| else | | -| old_spte = xchg(spte, 0ull) | | -| if (old_spte.Accessed == 1) | | -| kvm_set_pfn_accessed(spte.pfn);| | -| if (old_spte.Dirty == 1) | | -| kvm_set_pfn_dirty(spte.pfn); | | -| OOPS!!! | | -+------------------------------------+-----------------------------------+ ++-------------------------------------------------------------------------+ +| At the beginning:: | +| | +| spte.W = 0 | +| spte.Accessed = 1 | ++-------------------------------------+-----------------------------------+ +| CPU 0: | CPU 1: | ++-------------------------------------+-----------------------------------+ +| In mmu_spte_update():: | | +| | | +| old_spte = *spte; | | +| | | +| | | +| /* 'if' condition is satisfied. */ | | +| if (old_spte.Accessed == 1 && | | +| old_spte.W == 0) | | +| spte = new_spte; | | ++-------------------------------------+-----------------------------------+ +| | on fast page fault path:: | +| | | +| | spte.W = 1 | +| | | +| | memory write on the spte:: | +| | | +| | spte.Dirty = 1 | ++-------------------------------------+-----------------------------------+ +| :: | | +| | | +| else | | +| old_spte = xchg(spte, new_spte);| | +| if (old_spte.Accessed && | | +| !new_spte.Accessed) | | +| flush = true; | | +| if (old_spte.Dirty && | | +| !new_spte.Dirty) | | +| flush = true; | | +| OOPS!!! | | ++-------------------------------------+-----------------------------------+ The Dirty bit is lost in this case. diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 2e6daa6d1cc0..58b70328b20c 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -542,10 +542,8 @@ static bool mmu_spte_update(u64 *sptep, u64 new_spte) * to guarantee consistency between TLB and page tables. */ - if (is_accessed_spte(old_spte) && !is_accessed_spte(new_spte)) { + if (is_accessed_spte(old_spte) && !is_accessed_spte(new_spte)) flush = true; - kvm_set_pfn_accessed(spte_to_pfn(old_spte)); - } if (is_dirty_spte(old_spte) && !is_dirty_spte(new_spte)) flush = true; diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 7ac43d1ce918..d1de5f28c445 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -520,10 +520,6 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, if (was_present && !was_leaf && (is_leaf || !is_present || WARN_ON_ONCE(pfn_changed))) handle_removed_pt(kvm, spte_to_child_pt(old_spte, level), shared); - - if (was_leaf && is_accessed_spte(old_spte) && - (!is_present || !is_accessed_spte(new_spte) || pfn_changed)) - kvm_set_pfn_accessed(spte_to_pfn(old_spte)); } static inline int __must_check __tdp_mmu_set_spte_atomic(struct tdp_iter *iter, @@ -865,6 +861,9 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, tdp_mmu_iter_set_spte(kvm, &iter, SHADOW_NONPRESENT_VALUE); + if (is_accessed_spte(iter.old_spte)) + kvm_set_pfn_accessed(spte_to_pfn(iter.old_spte)); + /* * Zappings SPTEs in invalid roots doesn't require a TLB flush, * see kvm_tdp_mmu_zap_invalidated_roots() for details. From patchwork Fri Jul 26 23:51:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743433 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E0E21C3DA49 for ; Fri, 26 Jul 2024 23:56:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=fmU/HR7p0h2R7XRsIfqUXhH6E22vlxrvSMQX4VgE/mU=; b=a5UPjkGOgIjM9JC+ulhUcwPpw9 qVY6Jrx8714DYkQ4ImQ+8blu5M9lpBdqpzrxAVrcn9FdvUPyOnbhgEcC2werWZLA2owp5BihA+eAw VQvMegy0osG3etJgLschEiwTs82iFQ8EBw3Sk2DL5+e1VxkiXa87wz4BRwPmlmdHc8bZw3hee3Ntn LJCyjkvh+LbCoabOJeTuaqOVFoMC9CbADM5cPeMW5dMAThNGtSiMCXj/VWOrg26Y7Itl4qyd7FYuE jbc3BqN7MdoZfUC1SzoOst89UUVZoZOXWCqs5OHRvraayve/+oi6z0ZOxjJGf2/3prkSSipvJlg9d QS+CknqA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUnk-00000005S9v-3ibl; Fri, 26 Jul 2024 23:56:44 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUk5-00000005PJS-0wyf for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:52:59 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-66480c1a6b5so7085547b3.1 for ; Fri, 26 Jul 2024 16:52:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037976; x=1722642776; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=fmU/HR7p0h2R7XRsIfqUXhH6E22vlxrvSMQX4VgE/mU=; b=GVn8sOEUzxgznAvb9AiWU8gjhZyCq/rJ8VnBH0A8r009nlMVPGsiw9gETcgaWmhneM qa+UDOqXZ3G8wKRM8AHmFCZU7TCXKpyljFBP6TzS9csb8Vm2pWzcDa6mKqX8RZ+0K6En Sej/kQkfdSI36yBu3UsYXAHi53bm/yC5IEhmpZhA3D3VgRYDj2VoCbLCKQ9w5OBuTWr1 xd53j6HnYECQy3oirnWS0OhU/SfEVnHx4rl2WD/Hm6tSh3Y86JIge2aC7coOk57ne+xz pDqHP+I9Gao1yPIndBVu2Wi+C9zLKL6RrMw/UddMHq5whhSvd6QecVofVNacmbrFcHsR 9u3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037976; x=1722642776; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=fmU/HR7p0h2R7XRsIfqUXhH6E22vlxrvSMQX4VgE/mU=; b=jw5bm4+J9Nps38NBlVjTg5ZiovDjWOeAZ36FXUrqCjvjaKNBfADmDbuq3WyxK4u1bR S9oTBcYOPmsHPFGAWX8EzHKZsgu8dTZwqjQEE5/gzRFgEsuVmCrKNK+eq0qFRdb2cyh9 WGn8XC/2C0vwIEg+g5zvbXvmwYASaau+S3H6/SXK7voPo1ZViE1ESFxC1s9Mm+AfKagT Dai+XWEkrElGBObWxBKTGJG0Yc3dqwrj4dyXdaT3cfi0LotoS5EdP3hClmWP3KFMT5JI Pq+X3W3hmVltkOK7LxPRAeghFXwpZGoYVH0CAdtJUFSQGkx/Uw7NGg/TCCcS1lljQ8ca NVzw== X-Forwarded-Encrypted: i=1; AJvYcCVR0LDJ9HbaFf77NefD0aGBI3P9QlwPoGXoAHApvSdNzMT80JfCn9Ekl+oLI6oP7sn75v5eta4BqAG/5pLkV2yfGx/Umb26XpwyOlbc+GtDplJFs98= X-Gm-Message-State: AOJu0Yyd+5mEP9cfdI35QKixaO1fEAZsgU93C3YXXT2e+kQPaI5bf5uf RD47l3NuLazu53xq109H58Wt0O88HQwXFMjQJWiLQD6C+ivcpTT5Hpsy64LG4j9arHlY01yPHHz Rsw== X-Google-Smtp-Source: AGHT+IEZeCODu/YeM5d/iOhxGxPVHISuly+z8I6+5BlUeuWOyJE5t2wqwhMJ9ApQGl0yAUOujG/KjtPFVv0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:ad14:0:b0:62f:f535:f41 with SMTP id 00721157ae682-67a0abd4d1fmr288247b3.9.1722037976008; Fri, 26 Jul 2024 16:52:56 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:18 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-10-seanjc@google.com> Subject: [PATCH v12 09/84] KVM: x86/mmu: Don't force flush if SPTE update clears Accessed bit From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165257_472246_1A6D1678 X-CRM114-Status: GOOD ( 15.28 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Don't force a TLB flush if mmu_spte_update() clears Accessed bit, as access tracking tolerates false negatives, as evidenced by the mmu_notifier hooks that explicit test and age SPTEs without doing a TLB flush. In practice, this is very nearly a nop. spte_write_protect() and spte_clear_dirty() never clear the Accessed bit. make_spte() always sets the Accessed bit for !prefetch scenarios. FNAME(sync_spte) only sets SPTE if the protection bits are changing, i.e. if a flush will be needed regardless of the Accessed bits. And FNAME(pte_prefetch) sets SPTE if and only if the old SPTE is !PRESENT. That leaves kvm_arch_async_page_ready() as the one path that will generate a !ACCESSED SPTE *and* overwrite a PRESENT SPTE. And that's very arguably a bug, as clobbering a valid SPTE in that case is nonsensical. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 58b70328b20c..b7642f1f993f 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -518,37 +518,24 @@ static u64 mmu_spte_update_no_track(u64 *sptep, u64 new_spte) * TLBs must be flushed. Otherwise rmap_write_protect will find a read-only * spte, even though the writable spte might be cached on a CPU's TLB. * + * Remote TLBs also need to be flushed if the Dirty bit is cleared, as false + * negatives are not acceptable, e.g. if KVM is using D-bit based PML on VMX. + * + * Don't flush if the Accessed bit is cleared, as access tracking tolerates + * false negatives, and the one path that does care about TLB flushes, + * kvm_mmu_notifier_clear_flush_young(), uses mmu_spte_update_no_track(). + * * Returns true if the TLB needs to be flushed */ static bool mmu_spte_update(u64 *sptep, u64 new_spte) { - bool flush = false; u64 old_spte = mmu_spte_update_no_track(sptep, new_spte); if (!is_shadow_present_pte(old_spte)) return false; - /* - * For the spte updated out of mmu-lock is safe, since - * we always atomically update it, see the comments in - * spte_has_volatile_bits(). - */ - if (is_mmu_writable_spte(old_spte) && - !is_writable_pte(new_spte)) - flush = true; - - /* - * Flush TLB when accessed/dirty states are changed in the page tables, - * to guarantee consistency between TLB and page tables. - */ - - if (is_accessed_spte(old_spte) && !is_accessed_spte(new_spte)) - flush = true; - - if (is_dirty_spte(old_spte) && !is_dirty_spte(new_spte)) - flush = true; - - return flush; + return (is_mmu_writable_spte(old_spte) && !is_writable_pte(new_spte)) || + (is_dirty_spte(old_spte) && !is_dirty_spte(new_spte)); } /* From patchwork Fri Jul 26 23:51:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743434 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 75DDEC3DA4A for ; Fri, 26 Jul 2024 23:57:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=e1uaEfwlbucR9PkkwcUB/hlZrD43F2tYkTCYXf9cR5U=; b=kI1M2GZHzM+i5ursuQ11fiR1mG +Dy3XLzhxJEgp7Ie9JTbiwmfaU6Afi87iuqjXvsNR6pr6u4IbGWA8iiSELZ73JaoSj478qlKnu7x7 DVHo6RUReYPFXH2aSrthrVzeD7PGP/EIe/IrPFr2M5FvQ4MMd84WVPLlVU0WNIZoDNKZ2uI3kqnaE 85jU0MbLl+AeArkwdMpK9ewndsNSIcm18m7kf49NQTy/HIJ0zGcQR7z3I4NYDDnIA3mBKQFnaJ4EH FioIdlTGhagAKdyu1hQDOehZLtDb54lYWLkQTPzSqVgOrsTHO5q4KDy7Y9aTI7fXj2sk4+VAimE/8 37nc2QSQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUoC-00000005SNX-0JCd; Fri, 26 Jul 2024 23:57:12 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUk7-00000005PLe-0vm4 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:02 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e08723e9d7cso412265276.0 for ; Fri, 26 Jul 2024 16:52:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037978; x=1722642778; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=e1uaEfwlbucR9PkkwcUB/hlZrD43F2tYkTCYXf9cR5U=; b=eA52MVxC4pkGUKu9bK7pA+8ay9aAT0XMfj2BsQEg9d5EV3eGJwrmRVsHZ+teqvd8GB BD61VVKs07SQs9UBfP06zBwMwmJrOezttpVjaoEpcjRMV+9FLRUscmPpRiWj5Fkfyb50 k637X3VzHGBXnfFHcsH2sBnZYopLGLxjAhbX+PXfbzW4gJVYqSmVZ3J01OwoTiiSOYCy K0SOcalE7K8+jKQVAbVbIim7LBtuJXmNb9p9IOGTVAqZCKhmWasYPq/HxAi7GGbEAeNi t5/8M1j5tknh3iSYMX0rfclBfyOmnQTwO1QhqzwkVb2jLsWNZHCZIYKerRyJoZ5ox2v7 lGGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037978; x=1722642778; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=e1uaEfwlbucR9PkkwcUB/hlZrD43F2tYkTCYXf9cR5U=; b=dVLohdf+5H3GuJSzqGfTVviwj4FMg+TpC5HqJU24DachSuvrzVkI15ye/tT6Rt/Hgr c5DQ133y6SoTghfl+1GjVpktC02+x1Bqz8fDo6w91LYmArn08Yj24ymy+yJiYqmlYg0i PKNU7BY5AdJ3UuED0EasV8fYMhkB5qFqF7Oos4QSeDYXY1Wlu172uKaFL9hmSWtc/P4o mR2Cz1wWEVDFFCbLcmA+tLNS9CUac2ze6hkR9dlRRZJznWZil73BpK8c1Qf769Qyc/YR QbJOmPkP/+po1XACLAN+hocHGyw0ezVFhR34QsBrZ2p+KrC+ErpiHH+5k03Q53HKjR9l I/BA== X-Forwarded-Encrypted: i=1; AJvYcCXglih+QRt9fW2ROVwfHt2NGE0/MH4Z+DxITl5NyVgNbegMdfwzyig8OuZM8tkXK3iaq2wDY/93B86/TFQonuPRIzBwUPiT+iAupqcRduJHcUuM1ys= X-Gm-Message-State: AOJu0YwOYoaf6PIRrN57kMSChB8VfZBa+YlZYy/GaInVb9yvZ2+Op3tp NZ+4oTL18iTD7PCBCrAXpTQTx+Q+yxIY6XP63Iqa77nulv3WDokN06nqPrmefE3FJ5GUFvI30ti WfA== X-Google-Smtp-Source: AGHT+IG4XdyN3FgjZWItv/Lgg5QEKJKUQifoplgps87wy/mf6q3lv4u9rcIn8zN490p0nZ8TWmWhP2C2xyU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1081:b0:e0b:1407:e357 with SMTP id 3f1490d57ef6-e0b543f0dc9mr70203276.3.1722037977844; Fri, 26 Jul 2024 16:52:57 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:19 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-11-seanjc@google.com> Subject: [PATCH v12 10/84] KVM: x86/mmu: Use gfn_to_page_many_atomic() when prefetching indirect PTEs From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165259_319224_1F01839E X-CRM114-Status: GOOD ( 11.21 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use gfn_to_page_many_atomic() instead of gfn_to_pfn_memslot_atomic() when prefetching indirect PTEs (direct_pte_prefetch_many() already uses the "to page" APIS). Functionally, the two are subtly equivalent, as the "to pfn" API short-circuits hva_to_pfn() if hva_to_pfn_fast() fails, i.e. is just a wrapper for get_user_page_fast_only()/get_user_pages_fast_only(). Switching to the "to page" API will allow dropping the @atomic parameter from the entire hva_to_pfn() callchain. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/paging_tmpl.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index ef0b3b213e5b..6b215a932158 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -535,8 +535,8 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, { struct kvm_memory_slot *slot; unsigned pte_access; + struct page *page; gfn_t gfn; - kvm_pfn_t pfn; if (FNAME(prefetch_invalid_gpte)(vcpu, sp, spte, gpte)) return false; @@ -549,12 +549,11 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, if (!slot) return false; - pfn = gfn_to_pfn_memslot_atomic(slot, gfn); - if (is_error_pfn(pfn)) + if (gfn_to_page_many_atomic(slot, gfn, &page, 1) != 1) return false; - mmu_set_spte(vcpu, slot, spte, pte_access, gfn, pfn, NULL); - kvm_release_pfn_clean(pfn); + mmu_set_spte(vcpu, slot, spte, pte_access, gfn, page_to_pfn(page), NULL); + kvm_release_page_clean(page); return true; } From patchwork Fri Jul 26 23:51:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743435 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 48676C3DA4A for ; Fri, 26 Jul 2024 23:57:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=OCfqpOOOZn7LpwpHWWHUZjndC9uE6mObDYxnDRBBpAY=; b=nABC2qR7vsbrUfS0dW1wMhdyUW /YgS5slVxfT5cp8KLAPgO3q2wIrwufGTakz+/OQkdiot07in7IStDxXdB8DCIxgRJ/SZCgRIYrUUl SB8rvIZ4g0ThiufesMrP6aHgKHjUHuNrRC/+5TJH1s0UzPo17FJwE/TPpsNc+58T2LReG8VolUFDP 0Tft/DK0z7zyJKI+uMH/5xNSN/F4eGfkLgSf6SeAKA87XLS0NXy4d+79QcfRCqEI+3437dViVQmBk Z0fbM51cd09zOv1+cbk1OxFAVmtiyZSOb8RQnWgCGp6IwyUXjYbFb6wElsDK+jmMu0QPZ0gg9EmaT CHiRcIYw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUoh-00000005SgO-2i7O; Fri, 26 Jul 2024 23:57:43 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUk9-00000005PNf-0aLT for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:05 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1fd6d695662so11959945ad.0 for ; Fri, 26 Jul 2024 16:53:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037980; x=1722642780; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=OCfqpOOOZn7LpwpHWWHUZjndC9uE6mObDYxnDRBBpAY=; b=zeQVfyBDSt5ZHvwYebEP0uro5oBdHpEzHPdBkpvI/I4x17yijVY2J2UxY2jxcAcFMt bCieqLC0T5S+zNfqauINpfVaWWl5WcaMm2+FOcGrF4DrJj9QujYpALc7ptADkxRgnvwH Bf6ok3gPx94pJ0AFJOgWsxa3/Ko1J8NSrE0PNCh178TKkV7OlCWzGp1OPQKCvA9pR1rv OlVTlZovgjBAs3eZD1epXdokQKo2A6l5w46qdopqUkHJ191TzSV7VKah51UPiqjprS5K o1El7+rwwUtPhozESv9J4pxs81utV5X6rvNSajjD5Nu/U1Mjr0NUCnS1qUsLnIrRmCnR r6Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037980; x=1722642780; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OCfqpOOOZn7LpwpHWWHUZjndC9uE6mObDYxnDRBBpAY=; b=B+1w+422h6ptmfbdzHbEHVzMuZWpdwxaa2dUAAyY19tJT+0n2p3nqpnER+GKuG8x3X 98tAFFPSTSuLzZvNuDd9LcoOGMBrkvu+weTwYesDOmvkZS+CZsXDVJQ7LdC/7zKWlWxR 4lR32IF3kExtw7QfRM9Mo+YEFpbhMfK5Pfbj422x5f8g8JFjAdnVV/UQhNYQYvh7qmUS PZ/zNifqwnA1IS53tx2et0ivsLcip/PmXrbUR0izBhVQTZhhkxVuBFmiTTWytABb5kfw 7kbQS8lvpIvTIYWXPzWNCiSGExFEjzpf1kGf26aH/T2bG0zbu6Y+3crmYsNhSrHL15i0 B3cA== X-Forwarded-Encrypted: i=1; AJvYcCVkf3o05A/CvtKvL8aR1qTNQAB7z7bLoq4mbFVV+X89i35XW8JsConsUXkJCyYYnhJp4FstchDqzo89K+gGyH6jecrlNaguOXL2HnUhoZh+OqiGkZQ= X-Gm-Message-State: AOJu0YzdZ0+VLEWrMwQDAFe9iqxFcI/jefSatKfjIs4khgud53aw3U+v juu1LOaLvF8P0A6nh9E34AJuTIItWG4UEGOjsTI342HQRaYKCC+hiMFdolPiygZUODbMnELN/KT zVA== X-Google-Smtp-Source: AGHT+IGEij7x5iOOrUuL0BYhT5Dm9YFKfO4AZ5nK1acS0T3wJ2KiuANG5aiKSF+tpPt4GoCINPA3jjFDrnM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:2341:b0:1fd:d740:b1e5 with SMTP id d9443c01a7336-1ff04850898mr447825ad.6.1722037979718; Fri, 26 Jul 2024 16:52:59 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:20 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-12-seanjc@google.com> Subject: [PATCH v12 11/84] KVM: Rename gfn_to_page_many_atomic() to kvm_prefetch_pages() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165301_253215_93CCEAED X-CRM114-Status: GOOD ( 13.61 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Rename gfn_to_page_many_atomic() to kvm_prefetch_pages() to try and communicate its true purpose, as the "atomic" aspect is essentially a side effect of the fact that x86 uses the API while holding mmu_lock. E.g. even if mmu_lock weren't held, KVM wouldn't want to fault-in pages, as the goal is to opportunistically grab surrounding pages that have already been accessed and/or dirtied by the host, and to do so quickly. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- include/linux/kvm_host.h | 4 ++-- virt/kvm/kvm_main.c | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index b7642f1f993f..c1914f02c5e1 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -2912,7 +2912,7 @@ static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu, if (!slot) return -1; - ret = gfn_to_page_many_atomic(slot, gfn, pages, end - start); + ret = kvm_prefetch_pages(slot, gfn, pages, end - start); if (ret <= 0) return -1; diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index 6b215a932158..bc801d454f41 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -549,7 +549,7 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, if (!slot) return false; - if (gfn_to_page_many_atomic(slot, gfn, &page, 1) != 1) + if (kvm_prefetch_pages(slot, gfn, &page, 1) != 1) return false; mmu_set_spte(vcpu, slot, spte, pte_access, gfn, page_to_pfn(page), NULL); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index c5d39a337aa3..79fed9fea638 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1192,8 +1192,8 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm); void kvm_arch_flush_shadow_memslot(struct kvm *kvm, struct kvm_memory_slot *slot); -int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn, - struct page **pages, int nr_pages); +int kvm_prefetch_pages(struct kvm_memory_slot *slot, gfn_t gfn, + struct page **pages, int nr_pages); struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn); unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 656e931ac39e..803299778cf8 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3060,8 +3060,8 @@ kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) } EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn); -int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn, - struct page **pages, int nr_pages) +int kvm_prefetch_pages(struct kvm_memory_slot *slot, gfn_t gfn, + struct page **pages, int nr_pages) { unsigned long addr; gfn_t entry = 0; @@ -3075,7 +3075,7 @@ int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn, return get_user_pages_fast_only(addr, nr_pages, FOLL_WRITE, pages); } -EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic); +EXPORT_SYMBOL_GPL(kvm_prefetch_pages); /* * Do not use this helper unless you are absolutely certain the gfn _must_ be From patchwork Fri Jul 26 23:51:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743436 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A3B04C3DA4A for ; Fri, 26 Jul 2024 23:58:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ADEmxv9XmYXZ8ay6LRZAqHHnUfOELcTSDZRch+cTwuw=; b=TsisQy2wB6aGzrOh1uXppQm71K EF35Zbz5ufTAaBJOG5ChqBcEKrVQsdQi3U+/mSDCSTftpLXZbfiwQJU9XPu8GnlB2X+ExzKCq7OXl xGZGFSfiA5j5u68lD4gWt3Tl3jEAK6lN15mZmtQTMHSax/rfi4+RTmfmm4joujd6lao7quYU2ysM2 l55HJ48QFlN2LFvIJ5mX2ZIVVoGbj5d877qp3AAXV3lOxXaSRiSNiuF7mmMFhBZMvZm1NhVnfBAnU 2IY9A/b4KgCR9VFbbRWHQBDMQXoWLPxtr3oglcsj3HgqpZeXJr8iUPeZNx1iayEEfePZuvaCSvEX8 H2X0Ieig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUpC-00000005SwP-0JkK; Fri, 26 Jul 2024 23:58:14 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkB-00000005PPs-2tfY for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:08 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-654d96c2bb5so6790447b3.2 for ; Fri, 26 Jul 2024 16:53:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037982; x=1722642782; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=ADEmxv9XmYXZ8ay6LRZAqHHnUfOELcTSDZRch+cTwuw=; b=3x262jKuMyrAYMOmMHYu6uFQSkjiNUmPmG5vOmipwwhNoqqEAiO/nNGF40aAyPMHdC qxXM8ctdAmwh3ymfFjSc9zgIRmnG6XBVhHxQiCjcwHkkr37tEUbw3JB/im25fMYRPwwC fUxyKzfTHAGjJhlVtgLxTyxnEzgUWyMZf1Zk8CEEshtRacSjxBCuM1w2PXUeufdo/Tmn ci+kEkPFfrykIO3LCH+LV+LUw/9DaJm7bCsXNoutArZ4TEYWYiykDBli0k05d2o2mDM1 uCG4dBIZgMDgKca+E/pjaQkCdDNeFsV+2RkKsXveXOTTlHur8zvH/Kl5uhgmmIJfusDv x4Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037982; x=1722642782; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ADEmxv9XmYXZ8ay6LRZAqHHnUfOELcTSDZRch+cTwuw=; b=JIG+oxKgXbQQ2GfZ7Iu0dzZPZu1SV1ANGcRAWYcOrRWZL6rT9gI3vybwBkOxstIjG7 XqMkLeX6UzpEtUgEWUT2oCVIKIba6ZMpZI+9ddDQf8wXaqSkD9OJFq+1LaKxkVV2fjhM YQfLY3SiG7GhMJ7a0P610l0wIYMiSDnZHUI+di9OQgyhlxNF6yTGcT9GqcgqgnHq/paZ /tNK0PHjRjGTsiEJMyfBcSgAjU36CcInkgORSIb2dqXafbCceJoO7+smdxvB+5XIWKds MM9LkLVDL37bPPpE6Z3kNbViTC2Y8Afc5q/m4LpEVrTa3yBZFDNxCI7CkUz7ja5DOEjr WRXg== X-Forwarded-Encrypted: i=1; AJvYcCXkK/CUtWjS/QiRm8rdX6LLl6l9QemRzCvIjckNmQ1vmnUB6dTHfyMXKzIIDxaOMuDYCW4UH7Gmly7yt07IGPqdvO1DDb4rlaFY0Cw5y+WC7D4Os90= X-Gm-Message-State: AOJu0YwiPSIpW/j/zPKOcLbn7Q0T/wQM1xY8/BrPxSqyRlJuwBxfeLyG Qoixx9Ttm9VDohf5Y3hqmVo4m/rTnN+z7mInhifJf4W47f4+yDQf/y6xF5NXcDrs0JE2TVwapby afg== X-Google-Smtp-Source: AGHT+IFvuKtzzbSFT7TzhVG4cD407iDdVRMZdEo+hUTgj12n6OpGkNu03b7iXctLyyeC9wijqsbDK83s/Cg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:f03:b0:665:a4a4:57c1 with SMTP id 00721157ae682-67a057b7ffdmr33777b3.2.1722037981863; Fri, 26 Jul 2024 16:53:01 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:21 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-13-seanjc@google.com> Subject: [PATCH v12 12/84] KVM: Drop @atomic param from gfn=>pfn and hva=>pfn APIs From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165303_865531_772037F5 X-CRM114-Status: GOOD ( 17.63 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Drop @atomic from the myriad "to_pfn" APIs now that all callers pass "false". No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Alex Bennée --- Documentation/virt/kvm/locking.rst | 4 +-- arch/arm64/kvm/mmu.c | 2 +- arch/powerpc/kvm/book3s_64_mmu_hv.c | 2 +- arch/powerpc/kvm/book3s_64_mmu_radix.c | 2 +- arch/x86/kvm/mmu/mmu.c | 12 ++++----- include/linux/kvm_host.h | 4 +-- virt/kvm/kvm_main.c | 36 +++++--------------------- virt/kvm/kvm_mm.h | 4 +-- virt/kvm/pfncache.c | 2 +- 9 files changed, 22 insertions(+), 46 deletions(-) diff --git a/Documentation/virt/kvm/locking.rst b/Documentation/virt/kvm/locking.rst index 8b3bb9fe60bf..9af511e7aa53 100644 --- a/Documentation/virt/kvm/locking.rst +++ b/Documentation/virt/kvm/locking.rst @@ -126,8 +126,8 @@ We dirty-log for gfn1, that means gfn2 is lost in dirty-bitmap. For direct sp, we can easily avoid it since the spte of direct sp is fixed to gfn. For indirect sp, we disabled fast page fault for simplicity. -A solution for indirect sp could be to pin the gfn, for example via -kvm_vcpu_gfn_to_pfn_atomic, before the cmpxchg. After the pinning: +A solution for indirect sp could be to pin the gfn before the cmpxchg. After +the pinning: - We have held the refcount of pfn; that means the pfn can not be freed and be reused for another gfn. diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 6981b1bc0946..30dd62f56a11 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1562,7 +1562,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, mmu_seq = vcpu->kvm->mmu_invalidate_seq; mmap_read_unlock(current->mm); - pfn = __gfn_to_pfn_memslot(memslot, gfn, false, false, NULL, + pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL, write_fault, &writable, NULL); if (pfn == KVM_PFN_ERR_HWPOISON) { kvm_send_hwpoison_signal(hva, vma_shift); diff --git a/arch/powerpc/kvm/book3s_64_mmu_hv.c b/arch/powerpc/kvm/book3s_64_mmu_hv.c index 1b51b1c4713b..8cd02ca4b1b8 100644 --- a/arch/powerpc/kvm/book3s_64_mmu_hv.c +++ b/arch/powerpc/kvm/book3s_64_mmu_hv.c @@ -613,7 +613,7 @@ int kvmppc_book3s_hv_page_fault(struct kvm_vcpu *vcpu, write_ok = true; } else { /* Call KVM generic code to do the slow-path check */ - pfn = __gfn_to_pfn_memslot(memslot, gfn, false, false, NULL, + pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL, writing, &write_ok, NULL); if (is_error_noslot_pfn(pfn)) return -EFAULT; diff --git a/arch/powerpc/kvm/book3s_64_mmu_radix.c b/arch/powerpc/kvm/book3s_64_mmu_radix.c index 408d98f8a514..26a969e935e3 100644 --- a/arch/powerpc/kvm/book3s_64_mmu_radix.c +++ b/arch/powerpc/kvm/book3s_64_mmu_radix.c @@ -852,7 +852,7 @@ int kvmppc_book3s_instantiate_page(struct kvm_vcpu *vcpu, unsigned long pfn; /* Call KVM generic code to do the slow-path check */ - pfn = __gfn_to_pfn_memslot(memslot, gfn, false, false, NULL, + pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL, writing, upgrade_p, NULL); if (is_error_noslot_pfn(pfn)) return -EFAULT; diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index c1914f02c5e1..d76390ef49b2 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4334,9 +4334,9 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault return kvm_faultin_pfn_private(vcpu, fault); async = false; - fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, false, - &async, fault->write, - &fault->map_writable, &fault->hva); + fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, &async, + fault->write, &fault->map_writable, + &fault->hva); if (!async) return RET_PF_CONTINUE; /* *pfn has correct page already */ @@ -4356,9 +4356,9 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault * to wait for IO. Note, gup always bails if it is unable to quickly * get a page and a fatal signal, i.e. SIGKILL, is pending. */ - fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, true, - NULL, fault->write, - &fault->map_writable, &fault->hva); + fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, true, NULL, + fault->write, &fault->map_writable, + &fault->hva); return RET_PF_CONTINUE; } diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 79fed9fea638..6d4503e8eabe 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1217,9 +1217,8 @@ kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn); kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, bool *writable); kvm_pfn_t gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn); -kvm_pfn_t gfn_to_pfn_memslot_atomic(const struct kvm_memory_slot *slot, gfn_t gfn); kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, - bool atomic, bool interruptible, bool *async, + bool interruptible, bool *async, bool write_fault, bool *writable, hva_t *hva); void kvm_release_pfn_clean(kvm_pfn_t pfn); @@ -1300,7 +1299,6 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn); struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu); struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn); -kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn); kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn); int kvm_vcpu_map(struct kvm_vcpu *vcpu, gpa_t gpa, struct kvm_host_map *map); void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 803299778cf8..84c73b4fc804 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2929,7 +2929,6 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma, /* * Pin guest page in memory and return its pfn. * @addr: host virtual address which maps memory to the guest - * @atomic: whether this function is forbidden from sleeping * @interruptible: whether the process can be interrupted by non-fatal signals * @async: whether this function need to wait IO complete if the * host page is not in the memory @@ -2941,22 +2940,16 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma, * 2): @write_fault = false && @writable, @writable will tell the caller * whether the mapping is writable. */ -kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool interruptible, - bool *async, bool write_fault, bool *writable) +kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool *async, + bool write_fault, bool *writable) { struct vm_area_struct *vma; kvm_pfn_t pfn; int npages, r; - /* we can do it either atomically or asynchronously, not both */ - BUG_ON(atomic && async); - if (hva_to_pfn_fast(addr, write_fault, writable, &pfn)) return pfn; - if (atomic) - return KVM_PFN_ERR_FAULT; - npages = hva_to_pfn_slow(addr, async, write_fault, interruptible, writable, &pfn); if (npages == 1) @@ -2993,7 +2986,7 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool interruptible, } kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, - bool atomic, bool interruptible, bool *async, + bool interruptible, bool *async, bool write_fault, bool *writable, hva_t *hva) { unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault); @@ -3015,39 +3008,24 @@ kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, writable = NULL; } - return hva_to_pfn(addr, atomic, interruptible, async, write_fault, - writable); + return hva_to_pfn(addr, interruptible, async, write_fault, writable); } EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot); kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, bool *writable) { - return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, false, - NULL, write_fault, writable, NULL); + return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, NULL, + write_fault, writable, NULL); } EXPORT_SYMBOL_GPL(gfn_to_pfn_prot); kvm_pfn_t gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn) { - return __gfn_to_pfn_memslot(slot, gfn, false, false, NULL, true, - NULL, NULL); + return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL, NULL); } EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot); -kvm_pfn_t gfn_to_pfn_memslot_atomic(const struct kvm_memory_slot *slot, gfn_t gfn) -{ - return __gfn_to_pfn_memslot(slot, gfn, true, false, NULL, true, - NULL, NULL); -} -EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic); - -kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn) -{ - return gfn_to_pfn_memslot_atomic(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn); -} -EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn_atomic); - kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) { return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn); diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h index 715f19669d01..a3fa86f60d6c 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -20,8 +20,8 @@ #define KVM_MMU_UNLOCK(kvm) spin_unlock(&(kvm)->mmu_lock) #endif /* KVM_HAVE_MMU_RWLOCK */ -kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool interruptible, - bool *async, bool write_fault, bool *writable); +kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool *async, + bool write_fault, bool *writable); #ifdef CONFIG_HAVE_KVM_PFNCACHE void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index f0039efb9e1e..58c706a610e5 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -198,7 +198,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) } /* We always request a writeable mapping */ - new_pfn = hva_to_pfn(gpc->uhva, false, false, NULL, true, NULL); + new_pfn = hva_to_pfn(gpc->uhva, false, NULL, true, NULL); if (is_error_noslot_pfn(new_pfn)) goto out_error; From patchwork Fri Jul 26 23:51:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743437 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 36718C3DA4A for ; Fri, 26 Jul 2024 23:58:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=G8vTlhvtYFVN9mhfz5x+cQCHJ6x1sNjhm2Vdu8bZ6Vo=; b=fFcfJJZ2E+cSQu5lUDrEC16Wmb j/8PJqE2w4e6RmOZf6/afy078imxTA0/3G3lQe5Bbm0r6xkuqNM1xNvamZoEi/FHi09ZgGNivgt8K WzVpYj5E30ZQIYfDkSYNwwi8XvcsyO1UZ4WiI7DKAMNNCwXUPt1fdgWUYlI3+8KR15iKMs8dJoYO+ 6xMK8LNvNuxiBiPCn8Xade0UARQn/2rilMANL/RylZI7mojvuYUUWdDeqpiYBFPpLYwacBoHNi0rZ qCcOMNdN8ztXjh/fPzQcQt4VljU53NNjqJj7z9ENRBydCGCod5HYnLugcI4+7HSXRLpRogLD8wRI/ tvJcDWeA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUpe-00000005TBZ-405Z; Fri, 26 Jul 2024 23:58:42 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkD-00000005PRH-1MeP for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:09 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1fc6a863751so9698365ad.2 for ; Fri, 26 Jul 2024 16:53:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037984; x=1722642784; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=G8vTlhvtYFVN9mhfz5x+cQCHJ6x1sNjhm2Vdu8bZ6Vo=; b=Nu+8mYXFrYQ5YGwtZj98aR8Bpk4QcMuLT6l7jV9IsZjg3lTutavr/cvyPs6tcwrFs8 KPWEPH8Amfj9zJmEfsrHoTcXY4sA6UPjsP0m5qqmhPjweB5/7J7uQMeLfoQtFXp3qmce a0wxJeC7vcAYd1cpEq9sri3nrvZycIeKbX3BbeQJVjVwqeA66HSueDrGq8exAFokz9mx ahpm6ZNkiRGaN34K+NkIXW997l2nYvEe1zRd7cTp2k8cACI5pbJsPNdN3R4nVsrLpk9L /bjZCyhwuppDXmcy7IV+A2B7ix5Hj+EWUZWdbOkPk+nfftcc9iq/AoN2ad7zUCqXM8Ih bPqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037984; x=1722642784; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=G8vTlhvtYFVN9mhfz5x+cQCHJ6x1sNjhm2Vdu8bZ6Vo=; b=NBeLkYjLde6Ne0oGGwylqZs7aU5Drm40/mAEf0QeH0J8IcaGMxqEOC0xCr4ZTM1gOH uUyfVoYQmlkRkFOAJx/uf6N9KycvpxDi6WinW2ZC7POIUZ6A1s7B2abdaBI4j4m3/vZo pZAy11hVWghMpsgstjQ0Mcdgw534ObEtrcn/QcV7RMGD/nwibDlu+a3LQ0rp0U99wKGz azDkTjCsF28FEJD3er4jF5eLmntAQNUJe0eo+6OT41b7uJ8fBRPOAjjtm0nr9SDJyhMY AF2PNp1HG3Csw4EijLIMzYMB8Gzt8c6Dv5HfHD9UZ+wubCjvQis9uX64G+uwMmYByNx9 b92A== X-Forwarded-Encrypted: i=1; AJvYcCVZXCp4MMUlWiGUAShZ+1A/LzarHbJ8/x+JxsJkviGKqUpbB63HKf5ktBL03semO1WHad01n82dEMvxtoVP8aTA8aAfd3xfktU61MLBhUI+zIkbaLk= X-Gm-Message-State: AOJu0YymZ8jrvDkfwphSxC8Lng3EOpWsclUgJxH6u84reECyCXJR9OuF Q7Np7hJx51rleA+fA60iPLN+LWF4WWNz5LD+mwcNJa2gWFc+gM4Qo+XUKdlVB9rWneGuqFS9UQy ELw== X-Google-Smtp-Source: AGHT+IGcvEjw3oRLgbDLJZa9I5d4JQMYDdZeA/FUbXpEzYyt5G6Xd+JdRMAKGNKsfMlLtXc62VqHMV40c8I= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:234e:b0:1f6:2964:17b8 with SMTP id d9443c01a7336-1ff0492c71fmr137005ad.10.1722037983933; Fri, 26 Jul 2024 16:53:03 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:22 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-14-seanjc@google.com> Subject: [PATCH v12 13/84] KVM: Annotate that all paths in hva_to_pfn() might sleep From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165305_535900_C8FAC2AC X-CRM114-Status: GOOD ( 11.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Now that hva_to_pfn() no longer supports being called in atomic context, move the might_sleep() annotation from hva_to_pfn_slow() to hva_to_pfn(). Signed-off-by: Sean Christopherson Reviewed-by: Alex Bennée --- virt/kvm/kvm_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 84c73b4fc804..03af1a0090b1 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2807,8 +2807,6 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, struct page *page; int npages; - might_sleep(); - if (writable) *writable = write_fault; @@ -2947,6 +2945,8 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool *async, kvm_pfn_t pfn; int npages, r; + might_sleep(); + if (hva_to_pfn_fast(addr, write_fault, writable, &pfn)) return pfn; From patchwork Fri Jul 26 23:51:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743508 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5D4E7C3DA4A for ; Sat, 27 Jul 2024 00:13:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=U9CL7WNG+bQMLxeBtbO0jb1Rey3S/NViM9NTHyPIsWo=; b=cJAQsV6zPrdY5i8g15oVOb30ih zQgvYlkMFx3vzSi+Ha8l/vTRp7SOPhl4Tgks6TbGXanXpB3ZVodZLs9y79zsVqTziTCHy7+sy1OzW nwGfWSZXWI4VHJIiZBiSFNRWT/H5RbkhXHbB3bwMUSkqHs1fW0sQHPTz8Jgqy07GMLdW5VQaSlwpz D7REzCLC9U9KGGyI/FIyWthJnYjNQHWm3VA2GnmwbfBLBBBcz+JBNTuuNKjiMV2NdzN/IYEmiQXWe 9nbUQp4TVXALS7Au9skl/nbyNx2sc1yPzB2Mhd6WeJlhMiv5tI9HNnQgUiygLBGx97fZxuralLxcL gU58f+1A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV4C-00000005aoZ-2uw2; Sat, 27 Jul 2024 00:13:44 +0000 Received: from mail-pj1-x1049.google.com ([2607:f8b0:4864:20::1049]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkF-00000005PTH-39Qd for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:12 +0000 Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2cb6c5f9810so1641257a91.2 for ; Fri, 26 Jul 2024 16:53:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037986; x=1722642786; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=U9CL7WNG+bQMLxeBtbO0jb1Rey3S/NViM9NTHyPIsWo=; b=dwEl4jOuhWtVvWSnPsuQCaefPpXFBLy8O5p/hCuU1bt6S10pkqfQbjERsZG2lNTGso oDUzrorj5rHfpiDJatLzeio21NkUgDxsGy5wtrmucfOpBffLZwaK2ghxtqM3FwP49VV+ J50JfPpkbL916r6uq2Q5b3W7zMIsij2tX660nfl74AOPymeD22jmZOHme7QJwm2KkAd+ ZwKCWoNZN8uGNF5h98UaMep8V5EsUS7cxEM8cotFnJ8JCXKBofK/K7aHBn6pXHtMWS3I 2cm5RtlVT+/R3FvqeNol9rXfNGzMzRWkcU8IdCK/ApfKKLs/VaM87Y51sGQanysZKftb +Yww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037986; x=1722642786; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=U9CL7WNG+bQMLxeBtbO0jb1Rey3S/NViM9NTHyPIsWo=; b=I07ay7w0OzvaXAJc6rDaoom5oNXg9KRaOuav8iWwJPSJwocpRRXIUlxsY/UP28kQJG cyCh90pDl55A5TVU7WD+WUB51j3HxSLUyckSE7cqnBOEHOV1a0FrYcJhGGAH2vium2kH /YtsYPmCCcKveY3zPEFHNu/y4dF7lzsHZSSFqUMxqvhlU5qbDjEC8ek3pD/Wo5PjR8Z6 APmKAnN0bQMzL829vcNdigczFRP/v38wPQRKI6zaa5tOHoGUqDQ2nhqCYWUXva3Etd2X AEjPb3iveNpFYcrZfkoxdUDoJo4cCalh0ZFwTkuGT70n06kEbDzyFlB4OMOOR0H9OlG6 UN1Q== X-Forwarded-Encrypted: i=1; AJvYcCVW7BnVOgc8Yvo0jtBNK0s7X5JE1Y5v2WBd+f4/3SKo+hR6ReptJI3bvWlm0c5XTXRN2vVf4obHrXpOCDP7i+ox7lvtuR1i9tGE3YtWA2hjGlVjrqc= X-Gm-Message-State: AOJu0Yyk7hNkiXFKR7z3SrUeLwDiTvln4ft+xHnO3oygxdZL9U9uQ3Tn /dDyxQxkolPe3SjW3LlQUVfU1X+77ifINI5xkwPBx0O4PiGbqjMfTg4P5Hc9rXRcdR+Wd44KBdg jYg== X-Google-Smtp-Source: AGHT+IHnRzETq13gV9xdTjZ59JsUWCUSm1ccdx7THxn20zb30PlPil4gPX7Xa7ZvU4LkGPt7E6DUyKfDh3Y= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:fd10:b0:2c9:26a1:3762 with SMTP id 98e67ed59e1d1-2cf7e5b209bmr21293a91.3.1722037985956; Fri, 26 Jul 2024 16:53:05 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:23 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-15-seanjc@google.com> Subject: [PATCH v12 14/84] KVM: Replace "async" pointer in gfn=>pfn with "no_wait" and error code From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165307_889390_F5037D1F X-CRM114-Status: GOOD ( 22.65 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: David Stevens Add a pfn error code to communicate that hva_to_pfn() failed because I/O was needed and disallowed, and convert @async to a constant @no_wait boolean. This will allow eliminating the @no_wait param by having callers pass in FOLL_NOWAIT along with other FOLL_* flags. Signed-off-by: David Stevens Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 18 +++++++++++------- include/linux/kvm_host.h | 3 ++- virt/kvm/kvm_main.c | 29 +++++++++++++++-------------- virt/kvm/kvm_mm.h | 2 +- virt/kvm/pfncache.c | 4 ++-- 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index d76390ef49b2..eb9ad0283fd5 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4328,17 +4328,21 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { - bool async; - if (fault->is_private) return kvm_faultin_pfn_private(vcpu, fault); - async = false; - fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, &async, + fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, true, fault->write, &fault->map_writable, &fault->hva); - if (!async) - return RET_PF_CONTINUE; /* *pfn has correct page already */ + + /* + * If resolving the page failed because I/O is needed to fault-in the + * page, then either set up an asynchronous #PF to do the I/O, or if + * doing an async #PF isn't possible, retry with I/O allowed. All + * other failures are terminal, i.e. retrying won't help. + */ + if (fault->pfn != KVM_PFN_ERR_NEEDS_IO) + return RET_PF_CONTINUE; if (!fault->prefetch && kvm_can_do_async_pf(vcpu)) { trace_kvm_try_async_get_page(fault->addr, fault->gfn); @@ -4356,7 +4360,7 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault * to wait for IO. Note, gup always bails if it is unable to quickly * get a page and a fatal signal, i.e. SIGKILL, is pending. */ - fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, true, NULL, + fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, true, true, fault->write, &fault->map_writable, &fault->hva); return RET_PF_CONTINUE; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 6d4503e8eabe..92b2922e2216 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -97,6 +97,7 @@ #define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1) #define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2) #define KVM_PFN_ERR_SIGPENDING (KVM_PFN_ERR_MASK + 3) +#define KVM_PFN_ERR_NEEDS_IO (KVM_PFN_ERR_MASK + 4) /* * error pfns indicate that the gfn is in slot but faild to @@ -1218,7 +1219,7 @@ kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, bool *writable); kvm_pfn_t gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn); kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, - bool interruptible, bool *async, + bool interruptible, bool no_wait, bool write_fault, bool *writable, hva_t *hva); void kvm_release_pfn_clean(kvm_pfn_t pfn); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 03af1a0090b1..c2efdfe26d5b 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2789,7 +2789,7 @@ static bool hva_to_pfn_fast(unsigned long addr, bool write_fault, * The slow path to get the pfn of the specified host virtual address, * 1 indicates success, -errno is returned if error is detected. */ -static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, +static int hva_to_pfn_slow(unsigned long addr, bool no_wait, bool write_fault, bool interruptible, bool *writable, kvm_pfn_t *pfn) { /* @@ -2812,7 +2812,7 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, if (write_fault) flags |= FOLL_WRITE; - if (async) + if (no_wait) flags |= FOLL_NOWAIT; if (interruptible) flags |= FOLL_INTERRUPTIBLE; @@ -2928,8 +2928,8 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma, * Pin guest page in memory and return its pfn. * @addr: host virtual address which maps memory to the guest * @interruptible: whether the process can be interrupted by non-fatal signals - * @async: whether this function need to wait IO complete if the - * host page is not in the memory + * @no_wait: whether or not this function need to wait IO complete if the + * host page is not in the memory * @write_fault: whether we should get a writable host page * @writable: whether it allows to map a writable host page for !@write_fault * @@ -2938,7 +2938,7 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma, * 2): @write_fault = false && @writable, @writable will tell the caller * whether the mapping is writable. */ -kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool *async, +kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool no_wait, bool write_fault, bool *writable) { struct vm_area_struct *vma; @@ -2950,7 +2950,7 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool *async, if (hva_to_pfn_fast(addr, write_fault, writable, &pfn)) return pfn; - npages = hva_to_pfn_slow(addr, async, write_fault, interruptible, + npages = hva_to_pfn_slow(addr, no_wait, write_fault, interruptible, writable, &pfn); if (npages == 1) return pfn; @@ -2959,7 +2959,7 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool *async, mmap_read_lock(current->mm); if (npages == -EHWPOISON || - (!async && check_user_page_hwpoison(addr))) { + (!no_wait && check_user_page_hwpoison(addr))) { pfn = KVM_PFN_ERR_HWPOISON; goto exit; } @@ -2976,9 +2976,10 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool *async, if (r < 0) pfn = KVM_PFN_ERR_FAULT; } else { - if (async && vma_is_valid(vma, write_fault)) - *async = true; - pfn = KVM_PFN_ERR_FAULT; + if (no_wait && vma_is_valid(vma, write_fault)) + pfn = KVM_PFN_ERR_NEEDS_IO; + else + pfn = KVM_PFN_ERR_FAULT; } exit: mmap_read_unlock(current->mm); @@ -2986,7 +2987,7 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool *async, } kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, - bool interruptible, bool *async, + bool interruptible, bool no_wait, bool write_fault, bool *writable, hva_t *hva) { unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault); @@ -3008,21 +3009,21 @@ kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, writable = NULL; } - return hva_to_pfn(addr, interruptible, async, write_fault, writable); + return hva_to_pfn(addr, interruptible, no_wait, write_fault, writable); } EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot); kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, bool *writable) { - return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, NULL, + return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, false, write_fault, writable, NULL); } EXPORT_SYMBOL_GPL(gfn_to_pfn_prot); kvm_pfn_t gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn) { - return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL, NULL); + return __gfn_to_pfn_memslot(slot, gfn, false, false, true, NULL, NULL); } EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot); diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h index a3fa86f60d6c..51f3fee4ca3f 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -20,7 +20,7 @@ #define KVM_MMU_UNLOCK(kvm) spin_unlock(&(kvm)->mmu_lock) #endif /* KVM_HAVE_MMU_RWLOCK */ -kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool *async, +kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool no_wait, bool write_fault, bool *writable); #ifdef CONFIG_HAVE_KVM_PFNCACHE diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 58c706a610e5..32dc61f48c81 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -197,8 +197,8 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) cond_resched(); } - /* We always request a writeable mapping */ - new_pfn = hva_to_pfn(gpc->uhva, false, NULL, true, NULL); + /* We always request a writable mapping */ + new_pfn = hva_to_pfn(gpc->uhva, false, false, true, NULL); if (is_error_noslot_pfn(new_pfn)) goto out_error; From patchwork Fri Jul 26 23:51:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743438 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2B768C3DA4A for ; Fri, 26 Jul 2024 23:59:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=XEii1g2QxUcfpOdmeEwY4J2sYQAHz7s0WQ4Z9lEf4mM=; b=x4y2UfMAgdHwCcOSWzydMX8KHi TYjVIf7PaQjrsf4SA1Y2+1LN2APjWveQ1UcXx1PqclswoRbpy3LHvafKH3HcCytCkhoUey8vbtUg1 DPse5JWHT0FLECcX6Kxe4HfzyxFVHse2VjaVfyKzfk1TOPIoXE+/oCHbPw+nupH89PNEZILCAlOg2 ka1rpwPaP+2MVVc33oYVSI+RsyTGkLQ4bu7yzE25ZOX1qjkK+l2Zh29qNC2GdmHYcv9ZuN0xP7kX9 vfjtA5miSO3cJjaj0QC3sZKLipOiI+d+5qjdiabHx46JLpd9yeRNZALe0v0JsLYYU9saVW9ce9Onp Jqbh7oCQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUqB-00000005TTK-1yyG; Fri, 26 Jul 2024 23:59:15 +0000 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkH-00000005PUn-2bUO for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:11 +0000 Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-79d95667cfaso1454855a12.2 for ; Fri, 26 Jul 2024 16:53:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037988; x=1722642788; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=XEii1g2QxUcfpOdmeEwY4J2sYQAHz7s0WQ4Z9lEf4mM=; b=Vw4SOUvRPhx9UHeT2LuztaHfmzfGs7YO8zyT6KABD4iQAWZH0+tOnItfSNk+0u5MK9 pNrLAgAIvTgF3DftiER021PIWNkBUJ7sc6ZPZTBGg4F4kGddgSEM7OaqEtqfdE1Oc7oO l5Bylu3pbAqu8wOclEXEhyARtmq9XyuVRaGS263+KiWUF/ooU3sJpO+eqn8n+9qzi7ww UwmNM9B3aekqQQ6289TRwSdOgtN8GzHR1OjoG53rZRHwr2qiADGBsmpTlh3Ala+iDiXS SqY5L/3BUWa+jgvt0s+WHnaqLVJjOQNd17nhjl/Ci0Z+Vkz2uU2qX0Pa4h5sdhYF3RRY XG8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037988; x=1722642788; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XEii1g2QxUcfpOdmeEwY4J2sYQAHz7s0WQ4Z9lEf4mM=; b=m2G73VNzBAXJLG/j0SXJYndkRY2EPMAoFjdR7rqqgnxFpxHloUFJhzILVo+bcS1nxm 1M5gWP2HhbyLcWq8Qu6lq5fqtiM274+pgLEjd49brma6K8VpaBvhOZRdKkjT3eWlxy4Q OVaJXVYqgU9UhsC/wmg+vf2bOvScE6K4BIPclBUPz+ubS0QTDsafMSHRh8tNfeHbZaKX 2Wl33RqAahz64R7kqP0y88cmXJybwnFZ7FEf8L2BmA1NGPUENsgrih+Q+R20mYvqASWg uwj6PnrdPrQG5MQ+/W1rxPwzconC/Uh1mb8UWkA3chr7DXZ2kwXNJSos6htA3GahJ8ZK LIVg== X-Forwarded-Encrypted: i=1; AJvYcCXaNUfM1EeFVpoHsZCq3xj0n+IvtGT3LlwzZxR3vBZjV2qQx7hsksBB/NNapfYScZ7KRrCP+HKID5aw7A1DeqkMAJDOOOyRV2g3fvFn418fx6S4E9U= X-Gm-Message-State: AOJu0YzOnOoW9DayOGYPBgZcUOx1i24vC9av6FDqhzeAZb5NmFL4yYb5 MO4PNKGiK7+RXkJNYk03Ne7o7uN4kfRRNQUlWWzaVLWZOhRXfQTsfDTo/8Dn68BemSBPwXyykWH GGQ== X-Google-Smtp-Source: AGHT+IHOguM9TV1Wqy8YwTb6CBUJl4Q4k/lDzAhV2epo/9u56TnU3ou5KRwZKpMCKdXGcBYB5FJy1Mcb3zg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a65:6556:0:b0:710:cc3c:3916 with SMTP id 41be03b00d2f7-7ac8e0b6796mr2159a12.3.1722037987987; Fri, 26 Jul 2024 16:53:07 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:24 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-16-seanjc@google.com> Subject: [PATCH v12 15/84] KVM: x86/mmu: Drop kvm_page_fault.hva, i.e. don't track intermediate hva From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165309_941846_4A5DD39D X-CRM114-Status: GOOD ( 13.17 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Remove kvm_page_fault.hva as it is never read, only written. This will allow removing the @hva param from __gfn_to_pfn_memslot(). Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 5 ++--- arch/x86/kvm/mmu/mmu_internal.h | 2 -- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index eb9ad0283fd5..e0bfbf95646c 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3248,7 +3248,6 @@ static int kvm_handle_noslot_fault(struct kvm_vcpu *vcpu, fault->slot = NULL; fault->pfn = KVM_PFN_NOSLOT; fault->map_writable = false; - fault->hva = KVM_HVA_ERR_BAD; /* * If MMIO caching is disabled, emulate immediately without @@ -4333,7 +4332,7 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, true, fault->write, &fault->map_writable, - &fault->hva); + NULL); /* * If resolving the page failed because I/O is needed to fault-in the @@ -4362,7 +4361,7 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault */ fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, true, true, fault->write, &fault->map_writable, - &fault->hva); + NULL); return RET_PF_CONTINUE; } diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index 1721d97743e9..f67396c435df 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -238,7 +238,6 @@ struct kvm_page_fault { /* Outputs of kvm_faultin_pfn. */ unsigned long mmu_seq; kvm_pfn_t pfn; - hva_t hva; bool map_writable; /* @@ -310,7 +309,6 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .is_private = err & PFERR_PRIVATE_ACCESS, .pfn = KVM_PFN_ERR_FAULT, - .hva = KVM_HVA_ERR_BAD, }; int r; From patchwork Fri Jul 26 23:51:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743509 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 12645C3DA49 for ; Sat, 27 Jul 2024 00:14:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=0OUoj11Bw7qJz7X/aUJltZsNV6l4pB1orjUswDi1Y5Y=; b=yJ6uvTubHcJy+OB3jHMqUGqBsc dOFUH/tivKj9tB6IFsbMB6k/lBNy65W7bAy+C+3wvxGvQbVBKxXTd64J1FFQAHUe+vpBOfY41eIHq UXkTsKU6+mL7pd6r6ldRPRumvzIMaK+DEt0Y/Lw6suRvnlPa2m/ozjVXsoQ5IO1gX4Qjlm+XOtzM0 ba/kbuBkjs9795eSRvJ7wWfIFXOT7EnAmThb9YEfSoz/cJcVMVpVRuQMSOlb0hvEUQkurtcuVtZbA L8L9jrWIEOd1izyN2PCpY4Sy1mZovTO+vsTjOKI/M19V3FvlifsuowNW2143589rHdUDhVu3Fl9dB +8XDJEnQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV4a-00000005axv-1OEc; Sat, 27 Jul 2024 00:14:08 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkJ-00000005PWL-0aTY for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:13 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-650b621f4cdso5683077b3.1 for ; Fri, 26 Jul 2024 16:53:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037990; x=1722642790; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=0OUoj11Bw7qJz7X/aUJltZsNV6l4pB1orjUswDi1Y5Y=; b=IWZEOLcerfPw8qZp5YAmj0qBF2xw6QQWAiduDMzgvj5OZ3qYex60Qmxaey1IY6bARo Sb7OKKOwjrf55Fbxgjw3B7FN553ZVwYgRc2K3nK/gxrQrjU+PKaRl+JMugmW7iRzDmn7 1kfVFavNQ/a/MJq5u+WiPFscaGdiaBWlxRjHAyWkSmNzHfgAL0W17SzBrxTivvDSroQw L5pb7Lg5dvThUDpbBfidj5vad1fbQmD99TUUf9gs2Uwb20Ie7v9Rq+5zjX7pyFCgMgmg jmZIx8SXsFQdWBQvoZ9XJZS4qx2IepjY8Jm6cmBUKgYkCMx1XVG1gGDQw9MULEca6fuw ir0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037990; x=1722642790; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0OUoj11Bw7qJz7X/aUJltZsNV6l4pB1orjUswDi1Y5Y=; b=GxIFr7hx6pG7fjI3r5txx4AA/dfdOWsDN0nJCK4TE6RtVaC6pmH1CwJbakxWBEsyyy 6qtFJtzUjL0rdyXt+zz69suUafwxahcavnvChAlMOn1vGaf4zHsZSqSWYBaYt0PNVJhn kNUIZyGa7mvV4wcfiogCXyEpxzMDcMGCQA2p9MA3oObjCm3VkcVQzOva1UVzreA7lf8k 8+7kRuw3px6SKTpRoqHjXz3FnVtburjdNs8/S/plZ4WbWjjNYiDif78VH+ER0yvOKEV3 RktDEdOU0/uCUHpxEgxe++I7Zj2frTYIwr96N76aG1fE4D1j8H/die2EDJUIbkPZAWAn OMHQ== X-Forwarded-Encrypted: i=1; AJvYcCUH/Vmrmz82O5qzEmVKvu2aGFMbcmYuoTmtuIdzwnI27cPWX98bk9q1BaZ+rj2wvjmufMaIfDToDrSWbyw/uV78zwFPZoXyOlGoVZOvrmEfPd+I3jE= X-Gm-Message-State: AOJu0YzydEIa33s098W+cXQ6fwqyYC49gyUdcQ+c411zhEyFl1Qa99aj rX7dxKPQPyKQjAIAiWvIu65tuQj7jvvyOKn2DJEP0eM8p7AW/B7cVBzGcbwPZRmRj1kNIGoLWiG NwQ== X-Google-Smtp-Source: AGHT+IHWajt4q+2GEWgaIgDXJueM9mo/vIIpZUTkzUWbzeiF8EolzfURUXE0pwxlI4gUkbCX2sqdG3/b4FA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:86:b0:650:a16c:91ac with SMTP id 00721157ae682-67a0a324dfdmr195847b3.8.1722037989755; Fri, 26 Jul 2024 16:53:09 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:25 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-17-seanjc@google.com> Subject: [PATCH v12 16/84] KVM: Drop unused "hva" pointer from __gfn_to_pfn_memslot() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165311_334885_7BEFD8E3 X-CRM114-Status: GOOD ( 13.63 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Drop @hva from __gfn_to_pfn_memslot() now that all callers pass NULL. No functional change intended. Signed-off-by: Sean Christopherson --- arch/arm64/kvm/mmu.c | 2 +- arch/powerpc/kvm/book3s_64_mmu_hv.c | 2 +- arch/powerpc/kvm/book3s_64_mmu_radix.c | 2 +- arch/x86/kvm/mmu/mmu.c | 6 ++---- include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 9 +++------ 6 files changed, 9 insertions(+), 14 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 30dd62f56a11..22ee37360c4e 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1563,7 +1563,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, mmap_read_unlock(current->mm); pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL, - write_fault, &writable, NULL); + write_fault, &writable); if (pfn == KVM_PFN_ERR_HWPOISON) { kvm_send_hwpoison_signal(hva, vma_shift); return 0; diff --git a/arch/powerpc/kvm/book3s_64_mmu_hv.c b/arch/powerpc/kvm/book3s_64_mmu_hv.c index 8cd02ca4b1b8..2f1d58984b41 100644 --- a/arch/powerpc/kvm/book3s_64_mmu_hv.c +++ b/arch/powerpc/kvm/book3s_64_mmu_hv.c @@ -614,7 +614,7 @@ int kvmppc_book3s_hv_page_fault(struct kvm_vcpu *vcpu, } else { /* Call KVM generic code to do the slow-path check */ pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL, - writing, &write_ok, NULL); + writing, &write_ok); if (is_error_noslot_pfn(pfn)) return -EFAULT; page = NULL; diff --git a/arch/powerpc/kvm/book3s_64_mmu_radix.c b/arch/powerpc/kvm/book3s_64_mmu_radix.c index 26a969e935e3..8304b6f8fe45 100644 --- a/arch/powerpc/kvm/book3s_64_mmu_radix.c +++ b/arch/powerpc/kvm/book3s_64_mmu_radix.c @@ -853,7 +853,7 @@ int kvmppc_book3s_instantiate_page(struct kvm_vcpu *vcpu, /* Call KVM generic code to do the slow-path check */ pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL, - writing, upgrade_p, NULL); + writing, upgrade_p); if (is_error_noslot_pfn(pfn)) return -EFAULT; page = NULL; diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index e0bfbf95646c..a201b56728ae 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4331,8 +4331,7 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault return kvm_faultin_pfn_private(vcpu, fault); fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, true, - fault->write, &fault->map_writable, - NULL); + fault->write, &fault->map_writable); /* * If resolving the page failed because I/O is needed to fault-in the @@ -4360,8 +4359,7 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault * get a page and a fatal signal, i.e. SIGKILL, is pending. */ fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, true, true, - fault->write, &fault->map_writable, - NULL); + fault->write, &fault->map_writable); return RET_PF_CONTINUE; } diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 92b2922e2216..f42e030f69a4 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1220,7 +1220,7 @@ kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, kvm_pfn_t gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn); kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, bool interruptible, bool no_wait, - bool write_fault, bool *writable, hva_t *hva); + bool write_fault, bool *writable); void kvm_release_pfn_clean(kvm_pfn_t pfn); void kvm_release_pfn_dirty(kvm_pfn_t pfn); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index c2efdfe26d5b..6e3bb202c1b3 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2988,13 +2988,10 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool no_wait, kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, bool interruptible, bool no_wait, - bool write_fault, bool *writable, hva_t *hva) + bool write_fault, bool *writable) { unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault); - if (hva) - *hva = addr; - if (kvm_is_error_hva(addr)) { if (writable) *writable = false; @@ -3017,13 +3014,13 @@ kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, bool *writable) { return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, false, - write_fault, writable, NULL); + write_fault, writable); } EXPORT_SYMBOL_GPL(gfn_to_pfn_prot); kvm_pfn_t gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn) { - return __gfn_to_pfn_memslot(slot, gfn, false, false, true, NULL, NULL); + return __gfn_to_pfn_memslot(slot, gfn, false, false, true, NULL); } EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot); From patchwork Fri Jul 26 23:51:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743439 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2879AC3DA49 for ; Sat, 27 Jul 2024 00:00:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=dQWjrE4jvr4hAuT3wkfi5A++VZy4br+IJ16QmEjL6no=; b=tYfvkRXxHXE1TxtT9X2Qxeg9ZU Vegy/4CWaGn3APtG+h6+4PnXaLBdEaT3ZlsCYtRMzaMNYQ95PYKfXGyfxgTZn82XaLkj1an5np1vC kE8UIuqS2SlGGX459Vlxdv0hxD650R4v2rf8Boqg6bpZh99gRY59qqLha9taXX4boOjmhyuvaTWeD O8osw4iWT+Eg5zv+RCcg0uQ/6oMejPvlEK0koVwCSOtuQwHU27Y3j9TX3zhfvA65rl/GX0M4PDZhg 3JE0uSIVziEUOyUICUorrsMuJzljqRUW3AozOM5R9kNoPd/doxN2LrOdgqT+zP8yy/Q2kebfLJyiF jp88sO+w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUr8-00000005U3x-2tCq; Sat, 27 Jul 2024 00:00:14 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkL-00000005PY9-2JoW for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:17 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70d14d11f42so1333843b3a.2 for ; Fri, 26 Jul 2024 16:53:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037992; x=1722642792; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=dQWjrE4jvr4hAuT3wkfi5A++VZy4br+IJ16QmEjL6no=; b=xs096Zpk/rxO8N78ay9J8lgHkgTFCr5BaqZo9gQzDkD//Yl0KooNZ6aQsM0rTy4xmV f1Xgc9H1F9CDeCxboL7eIX2G9yfCNMaYlhzIqws9AXVnemXfzJQtD6vxclrn3uzCi0bB yVqdCjaa5dqglh6DrQ7Yxz3skQ3afzatH/WdQQB0NR2cFMVDqZ+K0xQGh+yKXMQD5iTn JYq537TJTapv1BeGDOR+z626YgjXwPgKQ61zY8Bb6zDQVcVLi3Xt3OTyLtgz59iceyDY 5l960+ScvTUn5548lIEYO8vYGNuRVJedOPw955fQbI/2Jd8VuOKfHbY1t9yCw0pAD6/r jetA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037992; x=1722642792; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dQWjrE4jvr4hAuT3wkfi5A++VZy4br+IJ16QmEjL6no=; b=fGC0P3pVn++DJ493WLj1UF4Lp7LuNx+RYfBvmU56HB9lyEULeDNG+NITCBOuNVIVyg Z4tWcqNzTpQr8qNzCarRtfezac1CrJKzWGZ5ryaN+z2iXUfbl/NtCOqrNu7LoiNjgysS 3siAsUBZzTOIF2H3Myep9mE9MLfZ1F2/Siv0Slzwf6Co7fDj9ddTLumD44N2f6/bxRMc Diw09+D7nnNq1u/c+twG4EzuOlBd/OLEYrqkS/43Rl8UBaWoNOVq4I+3iAiVQqNDGsYq O8Z/EZLe2UXGxko/kxZ/KyLci7S12IOfptEbPMN0PVTiiu2JpteQiaX8CWI4DZB0GxDD M9aQ== X-Forwarded-Encrypted: i=1; AJvYcCWUTHHo5yWhxWb2vB4QgG8oMjCVggLU8EubGQLr8DChQjy9mcmAdoBHkjtIAODTV7GOYiGWd0kMCsTyTWIRbQkqtC+jAgMG7TMzoHgaHYxZrdo++uU= X-Gm-Message-State: AOJu0Yx4FfCTcKF6di6hRyzQE0utBV2ooIe3FyggY4nIZ2N0DFyMASn3 uXCnMr7IE3eEdjkFx891LSoH1B+4K2iVnPV/u1W9f79dApB7QOvHXF5uowDcxsKjklAqNUm0pEm +SQ== X-Google-Smtp-Source: AGHT+IF64dFMfvjIHcwYUOw27WNHnHBsH0QN8lopZ3C9XrG9iD55Iu+E93imY7RdCCfaNaEEdqDD7q46L3E= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:66e5:b0:70d:1e28:1c33 with SMTP id d2e1a72fcca58-70ece9ecd04mr9965b3a.1.1722037991679; Fri, 26 Jul 2024 16:53:11 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:26 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-18-seanjc@google.com> Subject: [PATCH v12 17/84] KVM: Introduce kvm_follow_pfn() to eventually replace "gfn_to_pfn" APIs From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165313_759739_29589F90 X-CRM114-Status: GOOD ( 26.94 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: David Stevens Introduce kvm_follow_pfn() to eventually supplant the various "gfn_to_pfn" APIs, albeit by adding more wrappers. The primary motivation of the new helper is to pass a structure instead of an ever changing set of parameters, e.g. so that tweaking the behavior, inputs, and/or outputs of the "to pfn" helpers doesn't require churning half of KVM. In the more distant future, the APIs exposed to arch code could also follow suit, e.g. by adding something akin to x86's "struct kvm_page_fault" when faulting in guest memory. But for now, the goal is purely to clean up KVM's "internal" MMU code. As part of the conversion, replace the write_fault, interruptible, and no-wait boolean flags with FOLL_WRITE, FOLL_INTERRUPTIBLE, and FOLL_NOWAIT respectively. Collecting the various FOLL_* flags into a single field will again ease the pain of passing new flags. Signed-off-by: David Stevens Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 166 +++++++++++++++++++++++--------------------- virt/kvm/kvm_mm.h | 20 +++++- virt/kvm/pfncache.c | 9 ++- 3 files changed, 111 insertions(+), 84 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6e3bb202c1b3..56c2d11761e0 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2761,8 +2761,7 @@ static inline int check_user_page_hwpoison(unsigned long addr) * true indicates success, otherwise false is returned. It's also the * only part that runs if we can in atomic context. */ -static bool hva_to_pfn_fast(unsigned long addr, bool write_fault, - bool *writable, kvm_pfn_t *pfn) +static bool hva_to_pfn_fast(struct kvm_follow_pfn *kfp, kvm_pfn_t *pfn) { struct page *page[1]; @@ -2771,14 +2770,13 @@ static bool hva_to_pfn_fast(unsigned long addr, bool write_fault, * or the caller allows to map a writable pfn for a read fault * request. */ - if (!(write_fault || writable)) + if (!((kfp->flags & FOLL_WRITE) || kfp->map_writable)) return false; - if (get_user_page_fast_only(addr, FOLL_WRITE, page)) { + if (get_user_page_fast_only(kfp->hva, FOLL_WRITE, page)) { *pfn = page_to_pfn(page[0]); - - if (writable) - *writable = true; + if (kfp->map_writable) + *kfp->map_writable = true; return true; } @@ -2789,8 +2787,7 @@ static bool hva_to_pfn_fast(unsigned long addr, bool write_fault, * The slow path to get the pfn of the specified host virtual address, * 1 indicates success, -errno is returned if error is detected. */ -static int hva_to_pfn_slow(unsigned long addr, bool no_wait, bool write_fault, - bool interruptible, bool *writable, kvm_pfn_t *pfn) +static int hva_to_pfn_slow(struct kvm_follow_pfn *kfp, kvm_pfn_t *pfn) { /* * When a VCPU accesses a page that is not mapped into the secondary @@ -2803,34 +2800,30 @@ static int hva_to_pfn_slow(unsigned long addr, bool no_wait, bool write_fault, * Note that get_user_page_fast_only() and FOLL_WRITE for now * implicitly honor NUMA hinting faults and don't need this flag. */ - unsigned int flags = FOLL_HWPOISON | FOLL_HONOR_NUMA_FAULT; - struct page *page; + unsigned int flags = FOLL_HWPOISON | FOLL_HONOR_NUMA_FAULT | kfp->flags; + struct page *page, *wpage; int npages; - if (writable) - *writable = write_fault; - - if (write_fault) - flags |= FOLL_WRITE; - if (no_wait) - flags |= FOLL_NOWAIT; - if (interruptible) - flags |= FOLL_INTERRUPTIBLE; - - npages = get_user_pages_unlocked(addr, 1, &page, flags); + npages = get_user_pages_unlocked(kfp->hva, 1, &page, flags); if (npages != 1) return npages; + if (!kfp->map_writable) + goto out; + + if (kfp->flags & FOLL_WRITE) { + *kfp->map_writable = true; + goto out; + } + /* map read fault as writable if possible */ - if (unlikely(!write_fault) && writable) { - struct page *wpage; - - if (get_user_page_fast_only(addr, FOLL_WRITE, &wpage)) { - *writable = true; - put_page(page); - page = wpage; - } + if (get_user_page_fast_only(kfp->hva, FOLL_WRITE, &wpage)) { + *kfp->map_writable = true; + put_page(page); + page = wpage; } + +out: *pfn = page_to_pfn(page); return npages; } @@ -2857,23 +2850,23 @@ static int kvm_try_get_pfn(kvm_pfn_t pfn) } static int hva_to_pfn_remapped(struct vm_area_struct *vma, - unsigned long addr, bool write_fault, - bool *writable, kvm_pfn_t *p_pfn) + struct kvm_follow_pfn *kfp, kvm_pfn_t *p_pfn) { kvm_pfn_t pfn; pte_t *ptep; pte_t pte; spinlock_t *ptl; + bool write_fault = kfp->flags & FOLL_WRITE; int r; - r = follow_pte(vma, addr, &ptep, &ptl); + r = follow_pte(vma, kfp->hva, &ptep, &ptl); if (r) { /* * get_user_pages fails for VM_IO and VM_PFNMAP vmas and does * not call the fault handler, so do it here. */ bool unlocked = false; - r = fixup_user_fault(current->mm, addr, + r = fixup_user_fault(current->mm, kfp->hva, (write_fault ? FAULT_FLAG_WRITE : 0), &unlocked); if (unlocked) @@ -2881,7 +2874,7 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma, if (r) return r; - r = follow_pte(vma, addr, &ptep, &ptl); + r = follow_pte(vma, kfp->hva, &ptep, &ptl); if (r) return r; } @@ -2893,8 +2886,8 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma, goto out; } - if (writable) - *writable = pte_write(pte); + if (kfp->map_writable) + *kfp->map_writable = pte_write(pte); pfn = pte_pfn(pte); /* @@ -2924,22 +2917,7 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma, return r; } -/* - * Pin guest page in memory and return its pfn. - * @addr: host virtual address which maps memory to the guest - * @interruptible: whether the process can be interrupted by non-fatal signals - * @no_wait: whether or not this function need to wait IO complete if the - * host page is not in the memory - * @write_fault: whether we should get a writable host page - * @writable: whether it allows to map a writable host page for !@write_fault - * - * The function will map a writable host page for these two cases: - * 1): @write_fault = true - * 2): @write_fault = false && @writable, @writable will tell the caller - * whether the mapping is writable. - */ -kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool no_wait, - bool write_fault, bool *writable) +kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp) { struct vm_area_struct *vma; kvm_pfn_t pfn; @@ -2947,11 +2925,10 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool no_wait, might_sleep(); - if (hva_to_pfn_fast(addr, write_fault, writable, &pfn)) + if (hva_to_pfn_fast(kfp, &pfn)) return pfn; - npages = hva_to_pfn_slow(addr, no_wait, write_fault, interruptible, - writable, &pfn); + npages = hva_to_pfn_slow(kfp, &pfn); if (npages == 1) return pfn; if (npages == -EINTR) @@ -2959,24 +2936,25 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool no_wait, mmap_read_lock(current->mm); if (npages == -EHWPOISON || - (!no_wait && check_user_page_hwpoison(addr))) { + (!(kfp->flags & FOLL_NOWAIT) && check_user_page_hwpoison(kfp->hva))) { pfn = KVM_PFN_ERR_HWPOISON; goto exit; } retry: - vma = vma_lookup(current->mm, addr); + vma = vma_lookup(current->mm, kfp->hva); if (vma == NULL) pfn = KVM_PFN_ERR_FAULT; else if (vma->vm_flags & (VM_IO | VM_PFNMAP)) { - r = hva_to_pfn_remapped(vma, addr, write_fault, writable, &pfn); + r = hva_to_pfn_remapped(vma, kfp, &pfn); if (r == -EAGAIN) goto retry; if (r < 0) pfn = KVM_PFN_ERR_FAULT; } else { - if (no_wait && vma_is_valid(vma, write_fault)) + if ((kfp->flags & FOLL_NOWAIT) && + vma_is_valid(vma, kfp->flags & FOLL_WRITE)) pfn = KVM_PFN_ERR_NEEDS_IO; else pfn = KVM_PFN_ERR_FAULT; @@ -2986,41 +2964,69 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool no_wait, return pfn; } +static kvm_pfn_t kvm_follow_pfn(struct kvm_follow_pfn *kfp) +{ + kfp->hva = __gfn_to_hva_many(kfp->slot, kfp->gfn, NULL, + kfp->flags & FOLL_WRITE); + + if (kfp->hva == KVM_HVA_ERR_RO_BAD) + return KVM_PFN_ERR_RO_FAULT; + + if (kvm_is_error_hva(kfp->hva)) + return KVM_PFN_NOSLOT; + + if (memslot_is_readonly(kfp->slot) && kfp->map_writable) { + *kfp->map_writable = false; + kfp->map_writable = NULL; + } + + return hva_to_pfn(kfp); +} + kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, bool interruptible, bool no_wait, bool write_fault, bool *writable) { - unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault); - - if (kvm_is_error_hva(addr)) { - if (writable) - *writable = false; - - return addr == KVM_HVA_ERR_RO_BAD ? KVM_PFN_ERR_RO_FAULT : - KVM_PFN_NOSLOT; - } - - /* Do not map writable pfn in the readonly memslot. */ - if (writable && memslot_is_readonly(slot)) { - *writable = false; - writable = NULL; - } - - return hva_to_pfn(addr, interruptible, no_wait, write_fault, writable); + struct kvm_follow_pfn kfp = { + .slot = slot, + .gfn = gfn, + .map_writable = writable, + }; + + if (write_fault) + kfp.flags |= FOLL_WRITE; + if (no_wait) + kfp.flags |= FOLL_NOWAIT; + if (interruptible) + kfp.flags |= FOLL_INTERRUPTIBLE; + + return kvm_follow_pfn(&kfp); } EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot); kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, bool *writable) { - return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, false, - write_fault, writable); + struct kvm_follow_pfn kfp = { + .slot = gfn_to_memslot(kvm, gfn), + .gfn = gfn, + .flags = write_fault ? FOLL_WRITE : 0, + .map_writable = writable, + }; + + return kvm_follow_pfn(&kfp); } EXPORT_SYMBOL_GPL(gfn_to_pfn_prot); kvm_pfn_t gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn) { - return __gfn_to_pfn_memslot(slot, gfn, false, false, true, NULL); + struct kvm_follow_pfn kfp = { + .slot = slot, + .gfn = gfn, + .flags = FOLL_WRITE, + }; + + return kvm_follow_pfn(&kfp); } EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot); diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h index 51f3fee4ca3f..d5a215958f06 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -20,8 +20,24 @@ #define KVM_MMU_UNLOCK(kvm) spin_unlock(&(kvm)->mmu_lock) #endif /* KVM_HAVE_MMU_RWLOCK */ -kvm_pfn_t hva_to_pfn(unsigned long addr, bool interruptible, bool no_wait, - bool write_fault, bool *writable); + +struct kvm_follow_pfn { + const struct kvm_memory_slot *slot; + const gfn_t gfn; + + unsigned long hva; + + /* FOLL_* flags modifying lookup behavior, e.g. FOLL_WRITE. */ + unsigned int flags; + + /* + * If non-NULL, try to get a writable mapping even for a read fault. + * Set to true if a writable mapping was obtained. + */ + bool *map_writable; +}; + +kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp); #ifdef CONFIG_HAVE_KVM_PFNCACHE void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 32dc61f48c81..067daf9ad6ef 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -159,6 +159,12 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) kvm_pfn_t new_pfn = KVM_PFN_ERR_FAULT; void *new_khva = NULL; unsigned long mmu_seq; + struct kvm_follow_pfn kfp = { + .slot = gpc->memslot, + .gfn = gpa_to_gfn(gpc->gpa), + .flags = FOLL_WRITE, + .hva = gpc->uhva, + }; lockdep_assert_held(&gpc->refresh_lock); @@ -197,8 +203,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) cond_resched(); } - /* We always request a writable mapping */ - new_pfn = hva_to_pfn(gpc->uhva, false, false, true, NULL); + new_pfn = hva_to_pfn(&kfp); if (is_error_noslot_pfn(new_pfn)) goto out_error; From patchwork Fri Jul 26 23:51:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743458 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 59147C3DA4A for ; Sat, 27 Jul 2024 00:01:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=YtgPpIjPSqL3TzmlwlpLZvvahtPd8ef2dtbW1LnZzVw=; b=fKypHW+xu8mWQL6nW4a0uTbHok gs9ucwvF8o2M73HFbwxI861V3X6KTbYKqE9M2CHxu7EN5WI5t/Zk9pTTjtv0yndS/KIjCxJ22TM61 gGpgm4MyroUec1NFMjIpgAbrzd79beIq/ncWciFJcktOpR0yEjd34pgjYMbUlT6/GhKT0mK9JFAzx 70Cje9wcfhnqYhWUTaVofjXCYha/z+y1VIsVIHJOHEAXu7oeYm6DcNMTwiq48atVlGoncY7Sq37cO 4vtChsEDemvi8VIz6iT4j4bKAHL77LjQjfJte6GR7hJDtyebaQyWOHPwIwSrzPaQKbyOOmRQQ+UYS ZJHs7iSA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUre-00000005ULA-1zMY; Sat, 27 Jul 2024 00:00:46 +0000 Received: from mail-pj1-x1049.google.com ([2607:f8b0:4864:20::1049]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkN-00000005PZv-09Gy for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:17 +0000 Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2cb77ab2074so1521041a91.1 for ; Fri, 26 Jul 2024 16:53:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037994; x=1722642794; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=YtgPpIjPSqL3TzmlwlpLZvvahtPd8ef2dtbW1LnZzVw=; b=rmiFYTIa0Wp21CB2IEf00DSh+K1YZulLvn8H57D/O8mMHWMWbbWLCe9uXzzXKAe1TR fHz404vKjtaeQveK51TlrQvvC/Pgp/by/+Qtb4gFk52kpxc0SmBRavmX4ZuMgYiNc384 VbfTiPzIkWzqq/LehQzKEkFFKnVdkXXTtIOEj8nZCERuOnQavuUcKNOJmfaBW/cw2k/Z tEe0vct9rVwAmukNXxbtaMUVZ+K0ntm11b1afQqc/kEALrmI+W6TVNOvCAo09I4CdE23 2E02eXHtbsI/yY4ohAO2hGuwqlBfgIdbUx935i6RliVG3hP0ZhB1rXIRJCFb9OZV4JDx UAHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037994; x=1722642794; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YtgPpIjPSqL3TzmlwlpLZvvahtPd8ef2dtbW1LnZzVw=; b=bxuqF07Ii1vQuuqSLzN8WTFzI27CIMZFS9HdtLNhaptf94mvoliH/1wsyPRBTQH7HW RLvkGWiUy4G0CPw0wN4KOhWYt7Ug2sKA/S1YyjuDHgFJBdiaLCMegogxUoDo+HB6lj/R XWVOCjZNhJvmGcYhrGREdpnFaVArlbeW18welZ5XohROk4jPVOSLjq5Y6+Wni0yAyM/L r/vvAo83hRMkztRlXxgvslwJtiPQWcI5TYm9Qaol6+qAYWu1aEQbJ34IsDLRszZnnMng dnFxdLSkIjqDGk9b68RRSnBz1fnWeDSOwjiNA2CvO5s9Sr9ix1eTrr5QmhTW1bPZ8m62 j2eQ== X-Forwarded-Encrypted: i=1; AJvYcCXx7O1axMHpeiQgHyBa+cf2lX/Uk3UnhIcZ3bmgA57wZ32lVzyHJV0leJqt+hM9+LnXLpubPYbm6sDYmZdTSSOShaMZmyuxQrifKcrSmS5bdyGIRiM= X-Gm-Message-State: AOJu0Yz55FPOpYjSofWYYmY49Cvqp0j/bgENBolDpKGBpK75f6Gm+9I3 FsSBE8UUVT31OTYohmMq9apNLTzvnNObfzgbOD7aiE10P0PH8NzcShmd3HDjYQE8M1mFGQslAQN Rxg== X-Google-Smtp-Source: AGHT+IHmoajaYWuB/CstqlwF+EMvaWyxqBn3H/lV1e3JQu2uS0IU9winjnCx9o/cN/zsyEDP3UE3pPbYXjw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:e744:b0:1fe:d72d:13bc with SMTP id d9443c01a7336-1ff04822069mr906395ad.5.1722037993928; Fri, 26 Jul 2024 16:53:13 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:27 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-19-seanjc@google.com> Subject: [PATCH v12 18/84] KVM: Remove pointless sanity check on @map param to kvm_vcpu_(un)map() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165315_210790_ABBFCCBD X-CRM114-Status: GOOD ( 11.45 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Drop kvm_vcpu_{,un}map()'s useless checks on @map being non-NULL. The map is 100% kernel controlled, any caller that passes a NULL pointer is broken and needs to be fixed, i.e. a crash due to a NULL pointer dereference is desirable (though obviously not as desirable as not having a bug in the first place). Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 56c2d11761e0..21ff0f4fa02c 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3092,9 +3092,6 @@ int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) void *hva = NULL; struct page *page = KVM_UNMAPPED_PAGE; - if (!map) - return -EINVAL; - pfn = gfn_to_pfn(vcpu->kvm, gfn); if (is_error_noslot_pfn(pfn)) return -EINVAL; @@ -3122,9 +3119,6 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_map); void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty) { - if (!map) - return; - if (!map->hva) return; From patchwork Fri Jul 26 23:51:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743510 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id ED1C8C3DA7F for ; Sat, 27 Jul 2024 00:14:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=oqsp5dWmLf1ceGtEyEpwn2+TSiYr9kgkgYyCIEXijOU=; b=sF4srqXOQYSOb963FZNMxA1j6w jG0CYeWSNTIJgBzfOH2/83WBAOkHXWPkzmAhs4ujfTtVbj0++lyUBj9ldrmqB1mbNBztXKdfMQQMi deXqXi9VYokh13Dp3lfH+XogjvrXGyNNVKVRIaURPVI9Yt7tP90HCRRDY4KICvjPdQxMESGZkFZMO TmXTr9gxCiLDqGAh/R29z9/+/aPqm+epdlvtSjLjqGuoLBZL/i0AlY4hewYKlmriRalc6yHMPuZnT wvnXkj5W7eYpQ3mMK5N+Ub+liesY1Kni1a9AkLfbsgndRkXbM8PMPSGEbcBaOW3wxnvgzGq9ytcSK 4SsuZzpw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV4y-00000005b7x-3N8j; Sat, 27 Jul 2024 00:14:32 +0000 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkP-00000005PcN-1TRu for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:19 +0000 Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-7a28f78c67aso1346206a12.1 for ; Fri, 26 Jul 2024 16:53:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037996; x=1722642796; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=oqsp5dWmLf1ceGtEyEpwn2+TSiYr9kgkgYyCIEXijOU=; b=WU1l/bGUgOlXergvUUEZAmStGB6e2lkbKY6g92ISVXZNY42sPSvTXwyx4umZVR8Y5v cdUylVL646oU9Igaur6rLaAq+m+GjE3yC0Yndyoif6kELo/pbSHKQBBfE9fMRmNeyzpN 2KZqn9K9lJrLjKDT8UKj9hEHZwih+bPlJAaF0VM+5J+p9R+s9kMswvBsVKQY+oni2/Eh fV0P91Corujow+DoDd7B82IT0/FlkYHfcUHmGji4+X//an9dQmlz5VM32+N7NrVOYMYB TVyZqNP50eEFVj7FNpwUg65FLJWOnTk2U4rKlABQmnwRVmSCebEzlM1kJbtiSGrQLfOM ONAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037996; x=1722642796; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oqsp5dWmLf1ceGtEyEpwn2+TSiYr9kgkgYyCIEXijOU=; b=S+GrxJJQd7g3VqCjn5gIOmBd1BkH/EBw0y7oDbKF4uVti3Buk+J0/ZXY++Kk2m+JtK doN50tVyysN0IfzRUR1nNp/cuoyZqVznVnri1IbNiAo5tyoDpHw/ffTCHZHyGTYTGNFN aHNlcFinmp2WMnSI4V/9mq7HRfhZ45VifyMCsgGrCcw4G+GOBgVn+os7SMdw4kp6xPpc 5u73n1ojbJYYRTgsmV3h2KMozM1ZctN1tUxyApBa7pwXpCILHb6Km1+06ylOc+1Cx/EV uc+vkdbxRkJcSRSFyGyBI4QsgnpxaAWM1N17/P3c89WYAZZbCLPiv0GRB25iIfiZJQVZ m4Gw== X-Forwarded-Encrypted: i=1; AJvYcCXfTRNCHzZChc1dVDorR8AMtvzFvyiP80wqFin3kTtuSCzou8+VPyveVjKKpv5c/Ur2Nin3ql2inIY+5/5BPUn/6kZtxtfXcnWZwmqNptidfgUlEkk= X-Gm-Message-State: AOJu0YxqQXNyOaN3oFPAQN45EAslhofDQSg5SBj+qp16HO6LUgTvFxTp mvm4JFXpOllrvmymXDVijgC0127yHk/U0AYbqLpTrkj5on5DgYqVia5BFqUSD9d7GFjvXNQ18c6 8Qw== X-Google-Smtp-Source: AGHT+IGPnVvSSR8GC4je1DUQj4K0eIO0wDIdrfABFjI4ae5Nt/lU6oCOfX0avnzwArksU/NKeR8FWd7ZKxM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a02:5a9:b0:6be:8aa5:bffb with SMTP id 41be03b00d2f7-7ac8e0bb8cdmr3560a12.4.1722037995764; Fri, 26 Jul 2024 16:53:15 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:28 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-20-seanjc@google.com> Subject: [PATCH v12 19/84] KVM: Explicitly initialize all fields at the start of kvm_vcpu_map() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165317_512848_F6159A0A X-CRM114-Status: GOOD ( 12.37 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Explicitly initialize the entire kvm_host_map structure when mapping a pfn, as some callers declare their struct on the stack, i.e. don't zero-initialize the struct, which makes the map->hva in kvm_vcpu_unmap() *very* suspect. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 21ff0f4fa02c..67a50b87bb87 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3088,32 +3088,24 @@ void kvm_release_pfn(kvm_pfn_t pfn, bool dirty) int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) { - kvm_pfn_t pfn; - void *hva = NULL; - struct page *page = KVM_UNMAPPED_PAGE; - - pfn = gfn_to_pfn(vcpu->kvm, gfn); - if (is_error_noslot_pfn(pfn)) - return -EINVAL; - - if (pfn_valid(pfn)) { - page = pfn_to_page(pfn); - hva = kmap(page); -#ifdef CONFIG_HAS_IOMEM - } else { - hva = memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB); -#endif - } - - if (!hva) - return -EFAULT; - - map->page = page; - map->hva = hva; - map->pfn = pfn; + map->page = KVM_UNMAPPED_PAGE; + map->hva = NULL; map->gfn = gfn; - return 0; + map->pfn = gfn_to_pfn(vcpu->kvm, gfn); + if (is_error_noslot_pfn(map->pfn)) + return -EINVAL; + + if (pfn_valid(map->pfn)) { + map->page = pfn_to_page(map->pfn); + map->hva = kmap(map->page); +#ifdef CONFIG_HAS_IOMEM + } else { + map->hva = memremap(pfn_to_hpa(map->pfn), PAGE_SIZE, MEMREMAP_WB); +#endif + } + + return map->hva ? 0 : -EFAULT; } EXPORT_SYMBOL_GPL(kvm_vcpu_map); From patchwork Fri Jul 26 23:51:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743511 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E3E8CC3DA49 for ; Sat, 27 Jul 2024 00:15:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=F7WgJlT5NNtid7p2jwFbPV8b+lTnD+3vJbPsQDaWDdo=; b=kICR7nbl3LTxfQXAfc/b6TMBz5 nGlZKNvvendWTFWMjXbYnHc8W7OdufumalkkNyVkm5dY9PLqxeC8W4sK/LI9dRxdnBEW0wH0ywBYB O/hi2iFpYGVYlaH1sGrdvonGUfnXlthqSSk+DflbBBZzOR8kj3oNx8qa3f5WuI5FmXuBMz5UZCi0y M/dXBNd3rgcpz4FoV4f40qTZl9xzyqWODop5cI3utq3pwCOKMt0aLILxnjv4w4nVajEAkwjcU6RNA HyaCpdxgmqFUv5WNrfwwPwrQkd7UP6WGzb/BdJDwoOtq6CAtHuebMkgKa3//5ZO2fTyxqo//4NEom vUVrMLSA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV5M-00000005bHF-09Eg; Sat, 27 Jul 2024 00:14:56 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkS-00000005PeV-0nGb for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:22 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e0353b731b8so462532276.2 for ; Fri, 26 Jul 2024 16:53:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037998; x=1722642798; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=F7WgJlT5NNtid7p2jwFbPV8b+lTnD+3vJbPsQDaWDdo=; b=3XOSA5RM933aJg+axcS/dYfGlr9c9Wsd4XCEY4eI/LI4a5R2AACLRHOV2Q3NMtLd8+ L/Nk+bi6Jr7LUskYlXc75vukLpLzRywEBs3oNvi9uM3EihuApuPeeuKDMXbB6IXduljo A4US1UPTvJS5f3e+CcyUggxeinwnK4yslhBw/uXLx7ATn0s1jZUP0TZrDhprz9+56Z2n Hf8v/czfOcqKPCEvISfFxPJQDDKvrM7NiplzKZOABbdjz04EMt/qzwkYQmi73B5Lq1pK KC37Rxj0dInApheZoLxp8RvMCqYIQbLs0e4kyUwCFVRsuQMNONUE1qSaG9YBk0PMIo6m wkdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037998; x=1722642798; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=F7WgJlT5NNtid7p2jwFbPV8b+lTnD+3vJbPsQDaWDdo=; b=FJ8H36KzkWQFPDL7lzevAMLgDRGLnR/ezJ46LptYvo09UeMb5gMAAtVmkpiGgwJoE6 jR99pJiy59waVdB2F96XuCUa1PdCseZAG4P7sG1gHc0EAZpIypVHQqiGO53WA0bp0M14 sDKdyJRgzvU45J8J3o0n19H9J22gIdZHTvUuX7A3Bejt9HOukMhzLpcKwRSGPssmbJSs b74pT6aBJmahlh+RaEjNIe9SMpglUHCCBh2X9W+TDg1eU/RosQx6KeFEIyKBC5z9m6vB ps7SMo9VErz2kfLkSGzAYOqMw+Pz/DOMFHXjqF5fIG8yf5/Falf1Hff9eA7ZjcUCeyuO LsEw== X-Forwarded-Encrypted: i=1; AJvYcCU8qZKN0PxlTEoPUAoFxIBknGQePDttweAxVQlPwFpi6rPIB9Qk7X85lmqr7dPssyyiuf9ppDbcG4N2dy70D5XQpfnAMp8m0RI03Ho1D0ihr8VvDVk= X-Gm-Message-State: AOJu0Yz26wAd3d/qheYPinl0tVTGBEGfJMGY/P0o8gVf2Hbu72wO89nZ 1Ef/qW8h+Ohom9fTyFabnPZO35TEUYvb/wrBVp9VCBv+2T1peP/4PRO9dd6OpZkZA3r4PVPvMyL VTQ== X-Google-Smtp-Source: AGHT+IHz6CGF/LJ/2Irg7XXoFs/UlO9MjKavvm7YGPRddocT798mRWwGF7hDBEuQel+TJyAfnYN45In/DfE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:120b:b0:e0b:4dd5:397e with SMTP id 3f1490d57ef6-e0b5455c058mr1734276.7.1722037998118; Fri, 26 Jul 2024 16:53:18 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:29 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-21-seanjc@google.com> Subject: [PATCH v12 20/84] KVM: Use NULL for struct page pointer to indicate mremapped memory From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165320_412070_B9F764A3 X-CRM114-Status: GOOD ( 16.28 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Drop yet another unnecessary magic page value from KVM, as there's zero reason to use a poisoned pointer to indicate "no page". If KVM uses a NULL page pointer, the kernel will explode just as quickly as if KVM uses a poisoned pointer. Never mind the fact that such usage would be a blatant and egregious KVM bug. Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 4 ---- virt/kvm/kvm_main.c | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f42e030f69a4..a5dcb72bab00 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -273,16 +273,12 @@ enum { READING_SHADOW_PAGE_TABLES, }; -#define KVM_UNMAPPED_PAGE ((void *) 0x500 + POISON_POINTER_DELTA) - struct kvm_host_map { /* * Only valid if the 'pfn' is managed by the host kernel (i.e. There is * a 'struct page' for it. When using mem= kernel parameter some memory * can be used as guest memory but they are not managed by host * kernel). - * If 'pfn' is not managed by the host kernel, this field is - * initialized to KVM_UNMAPPED_PAGE. */ struct page *page; void *hva; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 67a50b87bb87..3d717a131906 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3088,7 +3088,7 @@ void kvm_release_pfn(kvm_pfn_t pfn, bool dirty) int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) { - map->page = KVM_UNMAPPED_PAGE; + map->page = NULL; map->hva = NULL; map->gfn = gfn; @@ -3114,7 +3114,7 @@ void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty) if (!map->hva) return; - if (map->page != KVM_UNMAPPED_PAGE) + if (map->page) kunmap(map->page); #ifdef CONFIG_HAS_IOMEM else From patchwork Fri Jul 26 23:51:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743512 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C659FC3DA4A for ; Sat, 27 Jul 2024 00:15:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=khS3+Cji6+1mvvdZyBUq0wqzq89EmN0cfXvMph1EjiU=; b=yNpuyvfSUTUktRiUikootO/HHj b2ZzgMmLsyU2+9XuNx/U7SaM2F0Hbh/3B/4ByjX2K6Jy8vwbhLa0i0UBYFmoiVHJr92J2c22NVXjY jEqRbCDVirCWziev7PMdtz20Ou0Rx5IAb82oY2wxwXK/Jp2wnhQlCeB82QwjfNYrTH/98m4eiqHCG vh7ec9cMhTPZlHvMtvi7SEdzqNCpNdu4gWYASvGZldsG5GD1Rf09E1KoNG0oMdZcnx7bq0OUK5o5s w/8wyvhSTbo1HnY6jSSKwKC/ihWFlao1LMcnO7VgHoS3rJ4FM+YasMCmRt9VB4n0KLpeye47OhFhu Ds1BJa8w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV5l-00000005bRp-04tw; Sat, 27 Jul 2024 00:15:21 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkT-00000005Pgl-3Znq for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:24 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-66619cb2d3eso5958507b3.2 for ; Fri, 26 Jul 2024 16:53:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038000; x=1722642800; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=khS3+Cji6+1mvvdZyBUq0wqzq89EmN0cfXvMph1EjiU=; b=KvxIQGnXip473lLe0HoToInPHj5B3ZtL3zx5YTJiKrbCyzIxwLoAFRRWRAPESmPsLr Ic4RmzryO37taIULCvQrT77myehCiDqNH4UN/mpNXb3+4A/WfPsPJNSIGX7AZJ+/0oZX xbjm9CV8ZKtaG8XG4gBxuFbw7yFy9uyAJ1iL03MlwUwMFMslPIdfOrY9/UMwuZ/su+JZ ZkDtdP9Y4xtiuOC103tVxcrBbRXz1Sp+iaxo/2HO7WIigITw08/xqQmXazu7AT+bzBZW 7PqI1BLLTnVL+Q82qzaj2gpgR2D9bLfIKD7N+wYLPn1mbwT4hVt35j0l8CvBpmCzjGDI 4pxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038000; x=1722642800; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=khS3+Cji6+1mvvdZyBUq0wqzq89EmN0cfXvMph1EjiU=; b=TJGu36pmSv0ZAMml5pXPAqeVLD60aqXSohPoRLJ2HKHfkj2mTLt7/HnN7ala9+006C APrzNnthqLftIenNp2j3bQ5t0O0h5M041h7393rQ4q47RG8+TRRu7agbsGRIpfXfoOQY tSNjtsX85kFt1I0uNQ5te+nxxkCj8Cl/b0caJI0H7JAw9teL55uLqayIZ8moKkdBOmvW 8stwq125Vd9HWRk+qSCe5ugcchGcwGaf8cqAfYi4qZLyRVQZQb1Zi9EcgCvBFBlFwNPK M+ewuKb6WbpcG4fD2xDhelgyQw4QRbPhzeJwUiUuFP83dEnM6gvwnkKLhIgMUw+HQhYr JdSQ== X-Forwarded-Encrypted: i=1; AJvYcCWRS7zXJa2Yys5jvbYueJeD1dHw2lXPvrMSByGVfAVbztnfDtN5oU/Vhb01TcUtRS9tBi1kAyAeyTHmUN9icMB2Yly8G75u989YhzcSMv6aUEHMVRk= X-Gm-Message-State: AOJu0YzPROafxtz6I/oqbnqc1YT/AMSq4Wofig9CnJqzKv2NOjvs3BMj oMLDrJj4kcldaGix59TiHUR17oP9lNoDp8ooCp7SZRSPMQoTn4tub5LlSZE58uWBvog0oUHOQJs HhQ== X-Google-Smtp-Source: AGHT+IHn8ljNba0QNoBWKfaXu8Wx8VTGIoZ1RPeW8pDsltrF/Hl6Iy7/rU4VvDeHaG7eZvfgbbZBLlqPjFc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:806:b0:64a:8aec:617c with SMTP id 00721157ae682-679fffd3e35mr362617b3.0.1722038000511; Fri, 26 Jul 2024 16:53:20 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:30 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-22-seanjc@google.com> Subject: [PATCH v12 21/84] KVM: nVMX: Rely on kvm_vcpu_unmap() to track validity of eVMCS mapping From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165322_011737_9180DE2C X-CRM114-Status: GOOD ( 11.62 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Remove the explicit evmptr12 validity check when deciding whether or not to unmap the eVMCS pointer, and instead rely on kvm_vcpu_unmap() to play nice with a NULL map->hva, i.e. to do nothing if the map is invalid. Note, vmx->nested.hv_evmcs_map is zero-allocated along with the rest of vcpu_vmx, i.e. the map starts out invalid/NULL. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 2392a7ef254d..a34b49ea64b5 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -231,11 +231,8 @@ static inline void nested_release_evmcs(struct kvm_vcpu *vcpu) struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); struct vcpu_vmx *vmx = to_vmx(vcpu); - if (nested_vmx_is_evmptr12_valid(vmx)) { - kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true); - vmx->nested.hv_evmcs = NULL; - } - + kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true); + vmx->nested.hv_evmcs = NULL; vmx->nested.hv_evmcs_vmptr = EVMPTR_INVALID; if (hv_vcpu) { From patchwork Fri Jul 26 23:51:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743513 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1BB71C3DA49 for ; Sat, 27 Jul 2024 00:15:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=nY4qfmLxHYNbNwSwtMKIrsjqMbHvtDgV5qSHM3zuSO0=; b=CaDh+BXRdVGX3zgHhZ79rFmwwA MOEo5H/fBcfLHaSlukponlunS2APvX1NJbf90sQCZniQJnR0Mx0J44ihGegDjesjJpeRwNX77qqwC AOWf9ibmzGda9UBTqLFjMVnRsDKllzrxhr8Qxll1tUekjeCaPhTO3Jz5ZbME9dFGlTpyG7E3/apL2 5Jn8Ug3FXXL1lmqPFWGuGUdlfsWHImkhy/2CwIAkGOMgZlRmjczkzr1xuZ/utgmHw6KPnqNWZsOoU UCRTWpuJYbQQjk5uVMnT9bPhH9zJ5if7EI4/vfE2VlQ6oS5Af8tBtYLJy+DTyVip5ZcLfhV8ODnY4 YVG44LZg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV68-00000005bbj-2DPH; Sat, 27 Jul 2024 00:15:44 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkV-00000005Pik-3jqO for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:25 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70d1c6b7bb4so1137260b3a.1 for ; Fri, 26 Jul 2024 16:53:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038003; x=1722642803; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=nY4qfmLxHYNbNwSwtMKIrsjqMbHvtDgV5qSHM3zuSO0=; b=kveJCW1l0mmp/Bgr8Gb2pVY/cyIiAwxSIr8qnm9YSgFkrO7Fzm02WigGZNp32kkCYg C/Y56HvNNypArMypHsOJezmPv90cju9lQ/UYZ0SvMvX61hHgjo+0wdhjqeG17cmjrCov MZwDAuHNi+65tHsrFMQVhltotRAdUyBNWOS35xQa8cvPzaaH1MgdtajtG3mKcmfTocxv jpL2vGtmBR989IDSyt6sZ960OVwbqryeXdEkuaU0sRWCwCSDW5asIKPa9WYlq+0NZmIY eYYPq/gIY/XytnsRAv3kgdCJyeRqBl5DZGsKH5+oNQkaHDf5tSk77Aufzr+mOFrzE+K3 j6OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038003; x=1722642803; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nY4qfmLxHYNbNwSwtMKIrsjqMbHvtDgV5qSHM3zuSO0=; b=FEixZVW96t5w4QibAw87mV+rT68klNx7FEMkInBgnb78UuWHFKQ2GH5vNUmL/KzsGF RgCxyJCVQ5fegCp5TxHbIn++YftM1XUwNpQt4V0mQGlGo+juhpN6Ek/SOmzux8EZtFKQ RTvPQRsAGvfGIbB7chEkyUlwAx3kBd9qM4zODrx+MuoJ8VXxZq7v2Wl1FqIrc3CAMvDE uw8FNJ+2/jrDHgJyg+tBHkd2sUuRGJIzp30L+gWjLabR//8vBqEWXauBr6wQcaPbPAPY 86o1xsi9KFj1aP2NZMSQiYVpakef04CBvyt048ds+UwOYS7Bg0po8k5moQZvB/u149Ym uTWw== X-Forwarded-Encrypted: i=1; AJvYcCXcf3jB6fPYCwo4UchCRpRS1TQOF6m57Nj9K1ba2DZ2zjoo6iXdrPA3I8+p0HvIqQ9lSorWHu4tojeu+M1Bw8zZLzCxfc6kaYCrvvNwRDMlJSAl2H8= X-Gm-Message-State: AOJu0YyawR6lJlu/iskfkJuThGC/PAZJRYBYEhH5O8evJ1TISMNGmQYQ EB497j4ITTxrFTWKM5r4GXvxBx42FvQBeT7K7P3CnfEM+s9nHls6XdHMDbqtPCGxb+zH1xKGW7W qQw== X-Google-Smtp-Source: AGHT+IH3GR1gDSsaIiyxz/X29dplakZOpWNN6FppUWgXRDWsCZD6dFhJbJXFL2hO7f6rIpjA8QVwSQZzxjA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:6f1c:b0:70d:138a:bee8 with SMTP id d2e1a72fcca58-70ece533146mr8919b3a.0.1722038002674; Fri, 26 Jul 2024 16:53:22 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:31 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-23-seanjc@google.com> Subject: [PATCH v12 22/84] KVM: nVMX: Drop pointless msr_bitmap_map field from struct nested_vmx From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165323_954052_B92D19C5 X-CRM114-Status: GOOD ( 13.56 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Remove vcpu_vmx.msr_bitmap_map and instead use an on-stack structure in the one function that uses the map, nested_vmx_prepare_msr_bitmap(). Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 8 ++++---- arch/x86/kvm/vmx/vmx.h | 2 -- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index a34b49ea64b5..372d005e09e7 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -621,7 +621,7 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, int msr; unsigned long *msr_bitmap_l1; unsigned long *msr_bitmap_l0 = vmx->nested.vmcs02.msr_bitmap; - struct kvm_host_map *map = &vmx->nested.msr_bitmap_map; + struct kvm_host_map msr_bitmap_map; /* Nothing to do if the MSR bitmap is not in use. */ if (!cpu_has_vmx_msr_bitmap() || @@ -644,10 +644,10 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, return true; } - if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map)) + if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), &msr_bitmap_map)) return false; - msr_bitmap_l1 = (unsigned long *)map->hva; + msr_bitmap_l1 = (unsigned long *)msr_bitmap_map.hva; /* * To keep the control flow simple, pay eight 8-byte writes (sixteen @@ -711,7 +711,7 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, MSR_IA32_FLUSH_CMD, MSR_TYPE_W); - kvm_vcpu_unmap(vcpu, &vmx->nested.msr_bitmap_map, false); + kvm_vcpu_unmap(vcpu, &msr_bitmap_map, false); vmx->nested.force_msr_bitmap_recalc = false; diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 42498fa63abb..889c6c42ee27 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -204,8 +204,6 @@ struct nested_vmx { struct kvm_host_map virtual_apic_map; struct kvm_host_map pi_desc_map; - struct kvm_host_map msr_bitmap_map; - struct pi_desc *pi_desc; bool pi_pending; u16 posted_intr_nv; From patchwork Fri Jul 26 23:51:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743459 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F2BFDC3DA4A for ; Sat, 27 Jul 2024 00:02:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=xlV4Qu7sFx9TmbqoV45yeLGjyM3cIO8KzptAxLEtfNk=; b=j6xJTGwtRdifks+AxTi7Ntfxre Rp7qlEbmhuAjrg8cKHysBgJmTDyz3U0rvd3k5y6FhdO8LkV7MrEdd6TT8uB0yHH3EOgNWpcVi+Il4 zu5MEn7dM+gbyuZvPm85emx0UZ/auWBqx8BHbUw3WwsAaKvIRhlhAZv2QBzNoTZrj7aAOEg2Kgaa9 ZQdnlrjM7XIR2rT3QwIkj+CfcRv8rnpZ1KN8GDs4T/Xmj9F3MEdg+hsz7M78fhEDEIhMp2RuNgDje ulTYRK9fWT7LjmBDHSelr0WVzo8rU2raXMkm+4C01poxZrqSAjPPuamrdcZrtRovYsw2gG8LAVx7s a73OJQRg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUtA-00000005V7w-2L8t; Sat, 27 Jul 2024 00:02:20 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkY-00000005Pkg-1TQ2 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:27 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-66c0f57549fso7695987b3.0 for ; Fri, 26 Jul 2024 16:53:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038005; x=1722642805; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=xlV4Qu7sFx9TmbqoV45yeLGjyM3cIO8KzptAxLEtfNk=; b=E04bICl1HNMPH2Fal1W1r33/BM5sE8j6hp2FR6P9ZcLqCOca6v6PclinKzCE58peW4 EmHVgWfJIdltJOCsb7wcjMSi9x+q07JbSGCmbZJRwsPPHSQhmMOpur+7Bjgpuv7zJGYE PDVji0dYews91RzvPzzeBya80B0yPkHxbIxHVfgsaY4obdtVglbfyOiML30UDilx9sMn h/QxnamTx1JkNE1uyjEZg3uQuZtcnA8XqbH8fszqSj91mhxvBl3F/P9GfvPTg/nagapR sPRZgFb4iP6z62XhsSnxpugQoBt5m/lZnv0ZVYJ2dbiDb4lZvV/AZAihcpX1zXU/OIlr D+gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038005; x=1722642805; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xlV4Qu7sFx9TmbqoV45yeLGjyM3cIO8KzptAxLEtfNk=; b=qPzGVM1D0F8Uk6H1mdB/byWj9pnjik2gL8k5B8cIym3C4yf4H/RhB5Yy/bGKzAmeNl oFX1f3edxM5Hf5M6OxIcbHENVLnsLgSmePPS42ASFBJkARcsov5QF/8EtkbN6asQH/xF ilfOsiTkK/bna7S82AOM5GcRRnDT10gcYtjwuFmYRb0Zaxi2Qbclnfx7QILp5X//0/RO eoxKGzUzo1/AwO5pudQoYF/DAwt5YGObsWYjFl5hH6UJzqR5D13tYFTZovZvnBskdU7W zx9zfPncrul1iMPfx6+BfHw9gUgz7vrC19P5Ayfju7FEnUoV7rJ8VrC7bsJyX3TWXPqL fUGg== X-Forwarded-Encrypted: i=1; AJvYcCVHHC18QdmnK374wqbyHjfsPeRFs82P2ZUtkK2GHqfHqaNijNBWEuRjnSjeJpHgElU8uuekJbZyI45lih5HwqkJwgmteZcCe4nbW5t8w22dpJnV90g= X-Gm-Message-State: AOJu0YweEWuqHA1syCT5OIgB04NUOYNJmzMtt+D2qxr2nM9Xoa/q018a AAWqT/gP9Uz+hqvZGfwN4TQCZ79EmMuIpKtws3AQ3Zb0tGu9AIfCe6LaY3DRiDcYcJhAM9+Anb4 aTQ== X-Google-Smtp-Source: AGHT+IGzjee8SmO1Sa/b53YWKn3FsBJYizAY4tObHxd1ZNVi3RptE5IxRlqmItgGIGNjjqk15j+5giHmBC8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a5b:8d2:0:b0:e03:53a4:1a7 with SMTP id 3f1490d57ef6-e0b5454c840mr15153276.10.1722038004800; Fri, 26 Jul 2024 16:53:24 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:32 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-24-seanjc@google.com> Subject: [PATCH v12 23/84] KVM: nVMX: Add helper to put (unmap) vmcs12 pages From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165326_486194_E5E4F5B1 X-CRM114-Status: GOOD ( 13.97 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a helper to dedup unmapping the vmcs12 pages. This will reduce the amount of churn when a future patch refactors the kvm_vcpu_unmap() API. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 372d005e09e7..8d05d1d9f544 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -314,6 +314,21 @@ static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) vcpu->arch.regs_dirty = 0; } +static void nested_put_vmcs12_pages(struct kvm_vcpu *vcpu) +{ + struct vcpu_vmx *vmx = to_vmx(vcpu); + + /* + * Unpin physical memory we referred to in the vmcs02. The APIC access + * page's backing page (yeah, confusing) shouldn't actually be accessed, + * and if it is written, the contents are irrelevant. + */ + kvm_vcpu_unmap(vcpu, &vmx->nested.apic_access_page_map, false); + kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); + kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); + vmx->nested.pi_desc = NULL; +} + /* * Free whatever needs to be freed from vmx->nested when L1 goes down, or * just stops using VMX. @@ -346,15 +361,8 @@ static void free_nested(struct kvm_vcpu *vcpu) vmx->nested.cached_vmcs12 = NULL; kfree(vmx->nested.cached_shadow_vmcs12); vmx->nested.cached_shadow_vmcs12 = NULL; - /* - * Unpin physical memory we referred to in the vmcs02. The APIC access - * page's backing page (yeah, confusing) shouldn't actually be accessed, - * and if it is written, the contents are irrelevant. - */ - kvm_vcpu_unmap(vcpu, &vmx->nested.apic_access_page_map, false); - kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); - kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); - vmx->nested.pi_desc = NULL; + + nested_put_vmcs12_pages(vcpu); kvm_mmu_free_roots(vcpu->kvm, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); @@ -4942,11 +4950,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, vmx_update_cpu_dirty_logging(vcpu); } - /* Unpin physical memory we referred to in vmcs02 */ - kvm_vcpu_unmap(vcpu, &vmx->nested.apic_access_page_map, false); - kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); - kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); - vmx->nested.pi_desc = NULL; + nested_put_vmcs12_pages(vcpu); if (vmx->nested.reload_vmcs01_apic_access_page) { vmx->nested.reload_vmcs01_apic_access_page = false; From patchwork Fri Jul 26 23:51:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743465 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3D660C3DA49 for ; Sat, 27 Jul 2024 00:03:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Cx2uwWro3wQxHPvpUbBfNMOIaJj2wri0zL/P/0Je2BI=; b=Ew16/cWpTufjmiKqr66gnC16Gr yzuDHyEx8Ulq4OySGrr+3iIGn5kcr1ezwb+8W5Vke1xay4hOXdNaL3CX6kzSb8OkFbcWoZq3EbtXU 65YsSjISxJhBC0cy+scIFjvT6I6a5UcoDzm7A0203wFeBNKrUDA6dSWibRWI8jJMeWIGF0Sv3V0I1 5+V7ogQl0YNu6oMD8p53qk/he22A1uIhmq6gNYA+DojZLqq+TmWYTOE4quEGFhXWRTpf9mVC2TUiK r9eOf2iOPkWSOA+DS6NTI5ugrDducuDecXfnTIJGSCZAUfGk1c6upX5JsrR0/xmquXhm2qZDYnRw6 zhWs0ZjA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUti-00000005VN3-3PWm; Sat, 27 Jul 2024 00:02:54 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUka-00000005PmP-2eIF for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:30 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1fd8677380dso10144305ad.2 for ; Fri, 26 Jul 2024 16:53:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038007; x=1722642807; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Cx2uwWro3wQxHPvpUbBfNMOIaJj2wri0zL/P/0Je2BI=; b=tZZkRN6JgG5XvJu8tFToFjXh13MDbQ2r0pmOp57B4dOJhYdLXe3RQPpSHktuUbs0R+ mb8AgTz5P/cJVN1yGys15N9ACbNv2bAlnm7SudXKofX9TAm8iVPQzu9HIT8xlOAGiuER 562xbI8qZX4SPvt8eqma+xohKLoFtPhXIVohOK3SkGZ/DAque4D7+WVyYxqgUksYuA7V F8q+eule6udySzhN0tDcnsJAX2ItKW46+YoqHoJ4NMnlDML2ISNifpO5UojOrA/lkBCO mMrD94/rwgZsLQ9EH5l8UnWuTg5ATCfYulfd+/1sVe5MS1BCUUOQcNGjamRXf2uSSmZH zmzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038007; x=1722642807; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Cx2uwWro3wQxHPvpUbBfNMOIaJj2wri0zL/P/0Je2BI=; b=JwYq8RYRFF33S4ibACAMAKoCDkT8TVVe0dXnm9BwNIDJuShWlncalrOcpSDx9XYHGh /LPqG6P8FcRkX8xlM1bF/6hJW3VJtWRGjwtA9TOYVj6WGVZOkhpghtWpuuN3BxjsPvG+ y40a9cULlY7/fiCClzDSrO4Pa0pK2yVH4PREWPPjQ2DlyZcGi91A3AbeqKwSyUpubG7q aEOGtsPFWWGimJvrBPqrzawilE3ylzDlpolOKcJ58dqDLMmCrChYEkfa1KgvBMhMmEtH SsK59o9uZVytcWGLvancLGPyvM9tTmpJIgEMDZZIAOfbueVKF/q9YfTVDsrpQtfR8a/6 Ukww== X-Forwarded-Encrypted: i=1; AJvYcCVSU9Bs/M70Kbs+IA77dN/hnUstOJVbi9Xohe25Pi1wDSwxC9wgfATYYvnum+AmNOJejmPYqNkxlMCzS+LSA9SqAWropX790rseVIE9A2GiLS0sGgE= X-Gm-Message-State: AOJu0YzqkwHUg4IpQlfgxBkXfp0KkByMnFAULcHDWF62vNx593mGa0PY Np38VInHY5QtHTcDcUsna4R6no1pHsFyYZ0mxsrUsADA0ypFh4Tafjjh2uG+p9OsfZ9c/ctJD0y EVA== X-Google-Smtp-Source: AGHT+IFyTmAdCKGjl4Efd/ur3Lgpr5WGUo8BVtVPeI74QsMUPNsZhPnU9qRmgrxg08u1lYU5BCnREsvFCzc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:41ce:b0:1fa:acf0:72d6 with SMTP id d9443c01a7336-1ff0483372emr764645ad.3.1722038007284; Fri, 26 Jul 2024 16:53:27 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:33 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-25-seanjc@google.com> Subject: [PATCH v12 24/84] KVM: Use plain "struct page" pointer instead of single-entry array From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165328_770262_BB3A72FB X-CRM114-Status: GOOD ( 13.08 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use a single pointer instead of a single-entry array for the struct page pointer in hva_to_pfn_fast(). Using an array makes the code unnecessarily annoying to read and update. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Alex Bennée --- virt/kvm/kvm_main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 3d717a131906..8e83d3f043f1 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2763,7 +2763,7 @@ static inline int check_user_page_hwpoison(unsigned long addr) */ static bool hva_to_pfn_fast(struct kvm_follow_pfn *kfp, kvm_pfn_t *pfn) { - struct page *page[1]; + struct page *page; /* * Fast pin a writable pfn only if it is a write fault request @@ -2773,8 +2773,8 @@ static bool hva_to_pfn_fast(struct kvm_follow_pfn *kfp, kvm_pfn_t *pfn) if (!((kfp->flags & FOLL_WRITE) || kfp->map_writable)) return false; - if (get_user_page_fast_only(kfp->hva, FOLL_WRITE, page)) { - *pfn = page_to_pfn(page[0]); + if (get_user_page_fast_only(kfp->hva, FOLL_WRITE, &page)) { + *pfn = page_to_pfn(page); if (kfp->map_writable) *kfp->map_writable = true; return true; From patchwork Fri Jul 26 23:51:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743466 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EE871C3DA4A for ; Sat, 27 Jul 2024 00:03:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=tUI1d5PvM6nQSFLRYt7w7bGZCStWjtac1YnX2jIYbCg=; b=JqzzGG9bp4i8r9N0VMEW+U1XOg XzS4yCqz4czGvdH5AvdiCOtEAjmZddsGZ8AB77lUDKtATPFmUkO/x4/HJKWH/gGggMBSmMhiJfAbE ugOYVFajFwwvqKDkEE4tWULVnAitGgis09Cp8ppnMFCPmjdiRKBo3jjLO3B6O2KvDMEaTdvPKrMtg BX9HhGnw3vMKd8oI87hps+rfbNCLKsbswz1OQ7Ih7mAsDGFgjLLgeoswy8KA3USSS6DyI+0ED6vYx pyMWPFc0oPRyI2VKos551qsYNq0PKPEb4Nz8VrUDCQkkvQdSTH6A22SDoIMTsRiPwEBsaQ3Houjc2 jYv7nC7g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUu7-00000005VXo-2gjl; Sat, 27 Jul 2024 00:03:19 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkc-00000005Pnn-3u5f for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:32 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70d19a4137dso1422862b3a.1 for ; Fri, 26 Jul 2024 16:53:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038010; x=1722642810; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=tUI1d5PvM6nQSFLRYt7w7bGZCStWjtac1YnX2jIYbCg=; b=oIIsEW897Ml3oEqV1ukXS5s/BrFGFitpm7/uhyxWAtaNaqAOmNAa7L/mkMl5ljqKcj b522IN0r6O0dTcmOkCOnYgd2qMoS2gwb/YWjfKu7JcfZMeBxefC8C/oVuZ4vDBhxnTj5 pUbZL0Dr2BUii5s3STiGXu0cx+4u4BCbUx19v//DJKz4747L1fWwdRPMorl4mIB4oTUW egdGYsv5PqbeaV4toAebuKnW9qNx0b9kRxax2FNS0DS6xcRwBej4z6doDpxB+TtsW+BQ rlJMlj0R1/B6mQ/b1NxZL8YOuQtLvhrF2rx4w+eGk/ZQuMKdlrLGvMY7PedROfNgZC7S Rl4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038010; x=1722642810; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tUI1d5PvM6nQSFLRYt7w7bGZCStWjtac1YnX2jIYbCg=; b=u1vlgbq2BWwqlfx2/yJwSrXXFUdtqa4ogpfxSG91gWvfQwekCN2/9jz3FSxSCVqiyl hZ2ZdoIcAD9zI6POjoa/qCW+GKmRENlymy3FVEcuRlbN6yEApBuTaEF0HXFr8bRYSp51 ufaLyBwC0Stk93u96cE/xHlTlhIjTrYGFAdCrLsZd+jFZdMpyWJ7GD89XIkULqTYCCeY bVeOCQZN9KC7Ag7DRIeJ4d5+xrJTSPEx9HbKPB7cYHUjhrq1Th2mAjYeIc6OvW3c+1aw 0sOwcOjkPWGR9aOxQQvs97z3GjZKemQgP9VDnTtruZXrp2ED6oK8jiJ+ZOE39sdxUqtw 4bVA== X-Forwarded-Encrypted: i=1; AJvYcCWFu4NUZ1x9a+9VvrQ2SdowGoEus96Zi/a7+uXD2nQ+LJ+En1UA12gmk2tTCkeIzbL+N6YEaOcsRlBzUhpoYdD7qsMyBxbfY2q8pSQADcvf024UGKQ= X-Gm-Message-State: AOJu0YxLZ71yxLT02FUFeFH4oyonwNgRGldOvUgAJlIO72j598KGKnhU 8/qkabgLXnau7cThrcLLPue/nL7TP3s0Rij9qEUzNnYbjssJgJL7PIdA2R3u8xr2fQIhH81NWy2 3yg== X-Google-Smtp-Source: AGHT+IGIDN5ccGIB3cs8xyjoNBA33aBwY8OuaAQU+QnM7LAxDHF+KLZuhqGJARF7orYR9wXwPpBL0fgCScs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:8593:b0:70d:3466:2f1a with SMTP id d2e1a72fcca58-70ece63fd2emr7784b3a.1.1722038009466; Fri, 26 Jul 2024 16:53:29 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:34 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-26-seanjc@google.com> Subject: [PATCH v12 25/84] KVM: Provide refcounted page as output field in struct kvm_follow_pfn From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165331_090308_981CDDB3 X-CRM114-Status: GOOD ( 24.26 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add kvm_follow_pfn.refcounted_page as an output for the "to pfn" APIs to "return" the struct page that is associated with the returned pfn (if KVM acquired a reference to the page). This will eventually allow removing KVM's hacky kvm_pfn_to_refcounted_page() code, which is error prone and can't detect pfns that are valid, but aren't (currently) refcounted. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 100 +++++++++++++++++++++----------------------- virt/kvm/kvm_mm.h | 9 ++++ 2 files changed, 56 insertions(+), 53 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 8e83d3f043f1..31570c5627e3 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2756,6 +2756,46 @@ static inline int check_user_page_hwpoison(unsigned long addr) return rc == -EHWPOISON; } +static kvm_pfn_t kvm_resolve_pfn(struct kvm_follow_pfn *kfp, struct page *page, + pte_t *pte, bool writable) +{ + kvm_pfn_t pfn; + + WARN_ON_ONCE(!!page == !!pte); + + if (kfp->map_writable) + *kfp->map_writable = writable; + + /* + * FIXME: Remove this once KVM no longer blindly calls put_page() on + * every pfn that points at a struct page. + * + * Get a reference for follow_pte() pfns if they happen to point at a + * struct page, as KVM will ultimately call kvm_release_pfn_clean() on + * the returned pfn, i.e. KVM expects to have a reference. + * + * Certain IO or PFNMAP mappings can be backed with valid struct pages, + * but be allocated without refcounting, e.g. tail pages of + * non-compound higher order allocations. Grabbing and putting a + * reference to such pages would cause KVM to prematurely free a page + * it doesn't own (KVM gets and puts the one and only reference). + * Don't allow those pages until the FIXME is resolved. + */ + if (pte) { + pfn = pte_pfn(*pte); + page = kvm_pfn_to_refcounted_page(pfn); + if (page && !get_page_unless_zero(page)) + return KVM_PFN_ERR_FAULT; + } else { + pfn = page_to_pfn(page); + } + + if (kfp->refcounted_page) + *kfp->refcounted_page = page; + + return pfn; +} + /* * The fast path to get the writable pfn which will be stored in @pfn, * true indicates success, otherwise false is returned. It's also the @@ -2774,9 +2814,7 @@ static bool hva_to_pfn_fast(struct kvm_follow_pfn *kfp, kvm_pfn_t *pfn) return false; if (get_user_page_fast_only(kfp->hva, FOLL_WRITE, &page)) { - *pfn = page_to_pfn(page); - if (kfp->map_writable) - *kfp->map_writable = true; + *pfn = kvm_resolve_pfn(kfp, page, NULL, true); return true; } @@ -2808,23 +2846,15 @@ static int hva_to_pfn_slow(struct kvm_follow_pfn *kfp, kvm_pfn_t *pfn) if (npages != 1) return npages; - if (!kfp->map_writable) - goto out; - - if (kfp->flags & FOLL_WRITE) { - *kfp->map_writable = true; - goto out; - } - /* map read fault as writable if possible */ - if (get_user_page_fast_only(kfp->hva, FOLL_WRITE, &wpage)) { - *kfp->map_writable = true; + if (!(flags & FOLL_WRITE) && kfp->map_writable && + get_user_page_fast_only(kfp->hva, FOLL_WRITE, &wpage)) { put_page(page); page = wpage; + flags |= FOLL_WRITE; } -out: - *pfn = page_to_pfn(page); + *pfn = kvm_resolve_pfn(kfp, page, NULL, flags & FOLL_WRITE); return npages; } @@ -2839,20 +2869,9 @@ static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault) return true; } -static int kvm_try_get_pfn(kvm_pfn_t pfn) -{ - struct page *page = kvm_pfn_to_refcounted_page(pfn); - - if (!page) - return 1; - - return get_page_unless_zero(page); -} - static int hva_to_pfn_remapped(struct vm_area_struct *vma, struct kvm_follow_pfn *kfp, kvm_pfn_t *p_pfn) { - kvm_pfn_t pfn; pte_t *ptep; pte_t pte; spinlock_t *ptl; @@ -2882,38 +2901,13 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma, pte = ptep_get(ptep); if (write_fault && !pte_write(pte)) { - pfn = KVM_PFN_ERR_RO_FAULT; + *p_pfn = KVM_PFN_ERR_RO_FAULT; goto out; } - if (kfp->map_writable) - *kfp->map_writable = pte_write(pte); - pfn = pte_pfn(pte); - - /* - * Get a reference here because callers of *hva_to_pfn* and - * *gfn_to_pfn* ultimately call kvm_release_pfn_clean on the - * returned pfn. This is only needed if the VMA has VM_MIXEDMAP - * set, but the kvm_try_get_pfn/kvm_release_pfn_clean pair will - * simply do nothing for reserved pfns. - * - * Whoever called remap_pfn_range is also going to call e.g. - * unmap_mapping_range before the underlying pages are freed, - * causing a call to our MMU notifier. - * - * Certain IO or PFNMAP mappings can be backed with valid - * struct pages, but be allocated without refcounting e.g., - * tail pages of non-compound higher order allocations, which - * would then underflow the refcount when the caller does the - * required put_page. Don't allow those pages here. - */ - if (!kvm_try_get_pfn(pfn)) - r = -EFAULT; - + *p_pfn = kvm_resolve_pfn(kfp, NULL, &pte, pte_write(pte)); out: pte_unmap_unlock(ptep, ptl); - *p_pfn = pfn; - return r; } diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h index d5a215958f06..d3ac1ba8ba66 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -35,6 +35,15 @@ struct kvm_follow_pfn { * Set to true if a writable mapping was obtained. */ bool *map_writable; + + /* + * Optional output. Set to a valid "struct page" if the returned pfn + * is for a refcounted or pinned struct page, NULL if the returned pfn + * has no struct page or if the struct page is not being refcounted + * (e.g. tail pages of non-compound higher order allocations from + * IO/PFNMAP mappings). + */ + struct page **refcounted_page; }; kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp); From patchwork Fri Jul 26 23:51:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743467 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AE97DC3DA49 for ; Sat, 27 Jul 2024 00:04:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=R2I4/1amdIANY6ZAXPe7SG9ch9BlLbSqRy3xqYz3ZKo=; b=yFRwGxF9wk5nI7Kpc1xJBCBKvR wUSrJ/WPJuJunqBXRnUYNbBrPtFEkyu9gr2m+OE4D5sZQFe0ti8wHkfCQx9WZVqiRsCyTv/VbqHXC WFdxE/BCZ5SPINL0oUhtRFQ9Q9P3yGxlUyNrRkHEb7CXyZJaxHWOEomfRsIqQ8x7RUx1GCfrk/8+i eGaaZFuTPIWwF5ADhc41yKgUTUeI1lE/9IuPTIGswqole/cXeby3wGmDz7WUalp2coqsMvCAwZTHz AU3q34sJaG3hRCDiOfU+w3LEWXSBPxlM/jfpv9LjQBRTryhSj5dsh9WyrvTp05qePGbgl9h0BHrAE P6kGuLdw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUuc-00000005Vpb-1zps; Sat, 27 Jul 2024 00:03:50 +0000 Received: from mail-pj1-x104a.google.com ([2607:f8b0:4864:20::104a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUke-00000005PpG-3QxE for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:34 +0000 Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-2cd72aa5328so1511971a91.2 for ; Fri, 26 Jul 2024 16:53:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038011; x=1722642811; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=R2I4/1amdIANY6ZAXPe7SG9ch9BlLbSqRy3xqYz3ZKo=; b=usE+cDUWTYA9NxUNXAKflQvcZWMbnzlvgsbtIY9kIIPY14ngsjqdMlQgYrtrtZJMV3 3aAgew+dPMKRL+8GcVCwpYTwngmkHGC1A2XpvdUl8NjZwDq+zY56/QtUKZNAXboM7vV+ ZO4Y1FzHnsB4xrVD0PbLRCi+dBGtALRWIG4O1bcJU956l/gWIMs2VY3e6hnEtTvhy7gB 5PI8oUeat1ydranbOfGQlPCBhX3zfZ9mE8/6LxrFxCoAQMmDRrbWZc/pcCdLu8fK08pR ifjP9ykxBtBzhO+fbDqQDTZm3Mzobt5Y5ihbHER8PuXRjsnW21d9CkCWK+Xauc+RGMdc oJaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038011; x=1722642811; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=R2I4/1amdIANY6ZAXPe7SG9ch9BlLbSqRy3xqYz3ZKo=; b=nO3EY1s8hCO1fMjQXz38EhJDKvdd55MzroqpHtHjrqxXSWrHyZ62HAvskW1C1OjPV4 pauLWhtcSbhgrYwjWy8nRmGTdS4JVjbGKFbgA0pm+BZj/RAEPZ5FtzBWcNnFdp1QO48Z NjrTvfCtSvW87oRUnonjh7BOEcgE/3NEe1E0baNzHX/nqsoMf0aRhF53vk4ZT3Fqt4Jj yI5yuDdl3lccMu6cYFFh6ACI3KOR0FZw7jG9XUhnkpouoTlTYkCRCcH0o+OTnzy9iVaB zZvFGNdWeeYtWnZERBvjBhZo1aYazPUL9rRFAJLQ839R1j2NCPqk5UocyfyJ1GOaxK28 EoRw== X-Forwarded-Encrypted: i=1; AJvYcCXschGZkWnJdrDePfa9NbAoJkk/5ti7ydXFhoO815zZngK3bNRGnKST9VDfGP57OZDGAQqffU2itE2QkTYy5Jz0zlaptqMh/ZdW7RcsnUBEdVXs0hA= X-Gm-Message-State: AOJu0YyBzNssY1G9LFap1mSTX/H7EKP27zKlVOlhvQ22v+m8Tq1Y4LXW 0oz9h/jOLfLhkF5o8S8HrTeTt/FtMB10KwUGPDDb7UzCp36DQ5pCwFqrMVu9Cj6cspBq1jNN+4U baw== X-Google-Smtp-Source: AGHT+IHKOaCbBS38DB3+XV8s1t7JOWc+aANKzUQK49zxzSlWFwYOsfXU8rtGTN4QUDwCdEEeB4LXs+kD0Xc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:890d:b0:2c8:637:7390 with SMTP id 98e67ed59e1d1-2cf7e97d380mr2244a91.6.1722038011380; Fri, 26 Jul 2024 16:53:31 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:35 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-27-seanjc@google.com> Subject: [PATCH v12 26/84] KVM: Move kvm_{set,release}_page_{clean,dirty}() helpers up in kvm_main.c From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165332_975222_BC2B419D X-CRM114-Status: GOOD ( 11.51 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hoist the kvm_{set,release}_page_{clean,dirty}() APIs further up in kvm_main.c so that they can be used by the kvm_follow_pfn family of APIs. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Alex Bennée --- virt/kvm/kvm_main.c | 82 ++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 31570c5627e3..48b626f1b5f3 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2748,6 +2748,47 @@ unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *w return gfn_to_hva_memslot_prot(slot, gfn, writable); } +static bool kvm_is_ad_tracked_page(struct page *page) +{ + /* + * Per page-flags.h, pages tagged PG_reserved "should in general not be + * touched (e.g. set dirty) except by its owner". + */ + return !PageReserved(page); +} + +static void kvm_set_page_dirty(struct page *page) +{ + if (kvm_is_ad_tracked_page(page)) + SetPageDirty(page); +} + +static void kvm_set_page_accessed(struct page *page) +{ + if (kvm_is_ad_tracked_page(page)) + mark_page_accessed(page); +} + +void kvm_release_page_clean(struct page *page) +{ + if (!page) + return; + + kvm_set_page_accessed(page); + put_page(page); +} +EXPORT_SYMBOL_GPL(kvm_release_page_clean); + +void kvm_release_page_dirty(struct page *page) +{ + if (!page) + return; + + kvm_set_page_dirty(page); + kvm_release_page_clean(page); +} +EXPORT_SYMBOL_GPL(kvm_release_page_dirty); + static inline int check_user_page_hwpoison(unsigned long addr) { int rc, flags = FOLL_HWPOISON | FOLL_WRITE; @@ -3125,37 +3166,6 @@ void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty) } EXPORT_SYMBOL_GPL(kvm_vcpu_unmap); -static bool kvm_is_ad_tracked_page(struct page *page) -{ - /* - * Per page-flags.h, pages tagged PG_reserved "should in general not be - * touched (e.g. set dirty) except by its owner". - */ - return !PageReserved(page); -} - -static void kvm_set_page_dirty(struct page *page) -{ - if (kvm_is_ad_tracked_page(page)) - SetPageDirty(page); -} - -static void kvm_set_page_accessed(struct page *page) -{ - if (kvm_is_ad_tracked_page(page)) - mark_page_accessed(page); -} - -void kvm_release_page_clean(struct page *page) -{ - if (!page) - return; - - kvm_set_page_accessed(page); - put_page(page); -} -EXPORT_SYMBOL_GPL(kvm_release_page_clean); - void kvm_release_pfn_clean(kvm_pfn_t pfn) { struct page *page; @@ -3171,16 +3181,6 @@ void kvm_release_pfn_clean(kvm_pfn_t pfn) } EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); -void kvm_release_page_dirty(struct page *page) -{ - if (!page) - return; - - kvm_set_page_dirty(page); - kvm_release_page_clean(page); -} -EXPORT_SYMBOL_GPL(kvm_release_page_dirty); - void kvm_release_pfn_dirty(kvm_pfn_t pfn) { struct page *page; From patchwork Fri Jul 26 23:51:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743468 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B2CDAC3DA4A for ; Sat, 27 Jul 2024 00:04:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=o1vHCUb6CZ680r+o/spGyfXDOmfamym0Nonz9UARGME=; b=GbQSA72jLZkj/OdSRubBCKOXQF BsNS68nCusyjMuTGeYL8oZU8NlafKix8Tk2aFa8E3/iBzyJfq9wpSfEoSW4euPE+QfbqULkcxO2R6 E63DRLWhvCpxAe41lfoMrkcPw6j5IE6Z+M/YcF5vxkMWA+eXXVRhjAgUr+wdxHp1qAlm807ZAz21V 6QFfUybxSqcRB7dsCIPOe0cZOo6+3sR4GXRaB63hbo6br2p9qB8q/X7oIXB2UX0NMzja3bFoInBrS m49GDpyEtTrMTvndsEleQG7M7RPHGzZXRnMsswbHjFqeGruLCh8NNwXIV6VzblU2nfB09RZmAYlDI CXiDiSIA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUv6-00000005W5C-1PJN; Sat, 27 Jul 2024 00:04:20 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkh-00000005Pr3-0IM4 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:38 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1fc5b60f416so10713505ad.3 for ; Fri, 26 Jul 2024 16:53:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038014; x=1722642814; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=o1vHCUb6CZ680r+o/spGyfXDOmfamym0Nonz9UARGME=; b=ASID56uOqS7A7nICzxxM8Jr7+MVvEX/51hN7vQA6Xhg61XIKknC+txjvtbHaUfFW61 3c/JJq/vhCzAKRyuAfS5HHbfawnj9weOWtch1m4zSDYnkhSKmCf93+3wjsEOfg9oSPKl SjspJrJEzeQXeVoFdNQjt/fzq1KbdU6sidEMuLSqXnK+lUlZWUj2a7pge5JnwSBFz/ry Z77BNMmk1U0mpl22cojlcERTTOKyezdMFiWgxqYTtdDYT5gLWvVUjKx5yWW8Z62bGxDp BQzRnot6gZHOc0GqOkiUhIpBQ5QledMXprzeidPIplFLGd4aOGpwXNCtn9XwN2pGtWLU bs0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038014; x=1722642814; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=o1vHCUb6CZ680r+o/spGyfXDOmfamym0Nonz9UARGME=; b=Qyopz6RV4ZiktkvPQUvbJ20X7WdFBmocE34I+PDOfwQ2xlsT5nDb42+brhuctsL6z/ r2iaetHRuW4Zu1ViUWoWtWvuvAweCC8ri4+4f1PiFAej4ingh5bN7ma9aKD1yj7F/OyT XytvPZTk6peB01xpelAHxlU67ZocFDJOICLZZI+2TAfR3xZlDNuUhwwTDw+NcdF21EeL +u2EJhNZrR34pgD38zx4Y52s8PXmF+v7bhpZIs0uUEynfQYtE2rbKqGVsxsspFzSIEaw SXHjb3wXXwlQhyFtDV8wAc5fZffFE9jJC2j7q996kkzY1rQWU9hFQsgii4tfn2VsEiic ukLQ== X-Forwarded-Encrypted: i=1; AJvYcCWp4Ky0CsK4yBf+msY9iztDQA+f6dgXnxnX7OeAQRcZV+AlPGvsdIZpe7JW3FHXtdphMk7dGeHME5VdtB5TZ32xHekwoSKJogxLCbzPw9Qx66YLQaU= X-Gm-Message-State: AOJu0YwODA07KZFToXuOtJmBp2w2/NDhHBDTdE7UoDlsCZcTVWr6e3rJ 6Fp30J/iDd0LdugkOrWddcBz+/47eVdsS/SUQbzegK30LPOvjOnlOzZgRDQ8AB9dQtKLBj+hxHV b3g== X-Google-Smtp-Source: AGHT+IEBaNygerYKF4xG2tivsYC4I+4KiNqX7pDiMNbM1xexlfLGsv92ykpFMstIuclC7BO9j6NqEQXsK1A= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:d2c5:b0:1f9:b35f:a2b6 with SMTP id d9443c01a7336-1ff047dce33mr22915ad.1.1722038013079; Fri, 26 Jul 2024 16:53:33 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:36 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-28-seanjc@google.com> Subject: [PATCH v12 27/84] KVM: pfncache: Precisely track refcounted pages From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165335_179298_A84A40BE X-CRM114-Status: GOOD ( 12.89 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Track refcounted struct page memory using kvm_follow_pfn.refcounted_page instead of relying on kvm_release_pfn_clean() to correctly detect that the pfn is associated with a struct page. Signed-off-by: Sean Christopherson --- virt/kvm/pfncache.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 067daf9ad6ef..728d2c1b488a 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -159,11 +159,14 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) kvm_pfn_t new_pfn = KVM_PFN_ERR_FAULT; void *new_khva = NULL; unsigned long mmu_seq; + struct page *page; + struct kvm_follow_pfn kfp = { .slot = gpc->memslot, .gfn = gpa_to_gfn(gpc->gpa), .flags = FOLL_WRITE, .hva = gpc->uhva, + .refcounted_page = &page, }; lockdep_assert_held(&gpc->refresh_lock); @@ -198,7 +201,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) if (new_khva != old_khva) gpc_unmap(new_pfn, new_khva); - kvm_release_pfn_clean(new_pfn); + kvm_release_page_unused(page); cond_resched(); } @@ -218,7 +221,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) new_khva = gpc_map(new_pfn); if (!new_khva) { - kvm_release_pfn_clean(new_pfn); + kvm_release_page_unused(page); goto out_error; } @@ -236,11 +239,11 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) gpc->khva = new_khva + offset_in_page(gpc->uhva); /* - * Put the reference to the _new_ pfn. The pfn is now tracked by the + * Put the reference to the _new_ page. The page is now tracked by the * cache and can be safely migrated, swapped, etc... as the cache will * invalidate any mappings in response to relevant mmu_notifier events. */ - kvm_release_pfn_clean(new_pfn); + kvm_release_page_clean(page); return 0; From patchwork Fri Jul 26 23:51:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743469 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 964D4C3DA49 for ; Sat, 27 Jul 2024 00:05:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=cQ8Ke99Gz78IcD45RWrPNlFRmlyo3xxXp4XhOasYhk0=; b=l9uuwaMa0nYTR5dXo3376Xi3lT ix18tG6JHeU+j34ry8c6OxS5qgSFaLjyfc/IoiajixmGZkXNP8soQiZCC+1MjCaHh4RjWuYFL4W1p ozqPpvnAdAloKgdyA/gaAr41F3W9UiEXWyfc+Sryl+d2D5yVELL2nLkOd22sxiYREdOwqoCjjEVfS RdmhLyFXcBlO0yiUIzOG5b6CMBjLCZM3YCFIkPO6uV/GBq+xDmOsQXoS4dZFD5/VFCk1EQH+bvn5O UoDc0+dOcszwohbzxgNUlqlyghWtYSjqb4x1Nxd08tJhdtc01856hkMIs601yLSP84Cd8EkHgKU82 pu3IeNfg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUvY-00000005WKx-44a8; Sat, 27 Jul 2024 00:04:49 +0000 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUki-00000005Ptj-36q5 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:39 +0000 Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1fc5651e888so10451425ad.0 for ; Fri, 26 Jul 2024 16:53:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038016; x=1722642816; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=cQ8Ke99Gz78IcD45RWrPNlFRmlyo3xxXp4XhOasYhk0=; b=pf/JNHBi2Jz/rx4qLf7VFjisoAs9HqzHYLIRBuDqTIYBwVtVxLE4+QTZuQDahHpKn7 cftFOdz16favzPJOeJrSAYrPYdma0RSo+tngtSw1ONlNo1EHmhAL/eWLPDz+ISgsbyI8 l9wyixbXkVlusbRR/kX3IA9/XUr+6/t6NoOD0di7thFyUDtzzM2j4qhoG5WXqhA5GL/h KmM5nOOuvke4KTyIXdH1AXiRATwrF2mbVP0ZR32MYlhyFB0QoaXkI0LFd44rHgzVn+aL nnMJ1THDEtCYWNHddAj9E1hYdR9FFhC2gnO92YdpWiCmCu0qllUE+1kyjLzulzONYjYK vsfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038016; x=1722642816; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=cQ8Ke99Gz78IcD45RWrPNlFRmlyo3xxXp4XhOasYhk0=; b=bqA8bZ7N4A6SoqE3CnX57GuHFVTjwznXIgBkQ1Rp0GEDTosDXT8yH6nwVL5/O1Njzo WLWyltkSqiNoDQc8StsJNFwGoS/wRqOfMIT1kEZZGVzSAOL1oG+cx74J47b0G2/H6xal WKMGsOQmS3qfum+9Nq8Ps4fUilZumO4UkwQj5o5swbjM4SbZi3nph6YNn+saxABe0VxL NUTyK/kDC10LDQ2oqE066Iw7KYRPsnXa16R8v5c3Uy/Ee3IBt3Ux5z5T6U5/TEH7x4tu VaT6RUCabIfurxuP1JLA+C2dyrFcuKnsxenkVpaXNpYCX3U0EQTRGKpj8ozQ7Pk9iXSx 7xhg== X-Forwarded-Encrypted: i=1; AJvYcCVGjurjthomj/BHlwV29OHFQOHUPjUMPP+DmRs12u++nVFoPrWYi45ScJsCk/NW6lYdr5F5rxoBv3z1XWP20DE0tuPTX+KfxVAGYrJ+SkYKONAAJ/w= X-Gm-Message-State: AOJu0Yw3sqSBF6Q7BeSjqU9aQrjmIboqblgrooM/eEmsY3VwogfXxNNg LKh0lhvTfHmAdHsUAXNCa2IYbK6VT+rVVw0pDfS357Rt7Cs128otuiVZ6T5NGQtS1CA8nlWOY0n I1w== X-Google-Smtp-Source: AGHT+IEYmoKPicLFNTjAfXc3rW7QIOSCWQtpI+t2ZFjfSCVkTMXBL3j4sNtxlzg5xGSYJWPU8sNID0rjz8Y= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:d481:b0:1fb:5f82:6a61 with SMTP id d9443c01a7336-1ff047b8ea6mr1156745ad.5.1722038015496; Fri, 26 Jul 2024 16:53:35 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:37 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-29-seanjc@google.com> Subject: [PATCH v12 28/84] KVM: Migrate kvm_vcpu_map() to kvm_follow_pfn() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165337_118998_D8305799 X-CRM114-Status: GOOD ( 16.94 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: David Stevens Migrate kvm_vcpu_map() to kvm_follow_pfn(), and have it track whether or not the map holds a refcounted struct page. Precisely tracking struct page references will eventually allow removing kvm_pfn_to_refcounted_page() and its various wrappers. Signed-off-by: David Stevens [sean: use a pointer instead of a boolean] Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index a5dcb72bab00..8b5ac3305b05 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -280,6 +280,7 @@ struct kvm_host_map { * can be used as guest memory but they are not managed by host * kernel). */ + struct page *refcounted_page; struct page *page; void *hva; kvm_pfn_t pfn; @@ -1223,7 +1224,6 @@ void kvm_release_pfn_dirty(kvm_pfn_t pfn); void kvm_set_pfn_dirty(kvm_pfn_t pfn); void kvm_set_pfn_accessed(kvm_pfn_t pfn); -void kvm_release_pfn(kvm_pfn_t pfn, bool dirty); int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, int len); int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 48b626f1b5f3..255cbed83b40 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3113,21 +3113,21 @@ struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) } EXPORT_SYMBOL_GPL(gfn_to_page); -void kvm_release_pfn(kvm_pfn_t pfn, bool dirty) -{ - if (dirty) - kvm_release_pfn_dirty(pfn); - else - kvm_release_pfn_clean(pfn); -} - int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) { + struct kvm_follow_pfn kfp = { + .slot = gfn_to_memslot(vcpu->kvm, gfn), + .gfn = gfn, + .flags = FOLL_WRITE, + .refcounted_page = &map->refcounted_page, + }; + + map->refcounted_page = NULL; map->page = NULL; map->hva = NULL; map->gfn = gfn; - map->pfn = gfn_to_pfn(vcpu->kvm, gfn); + map->pfn = kvm_follow_pfn(&kfp); if (is_error_noslot_pfn(map->pfn)) return -EINVAL; @@ -3159,10 +3159,16 @@ void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty) if (dirty) kvm_vcpu_mark_page_dirty(vcpu, map->gfn); - kvm_release_pfn(map->pfn, dirty); + if (map->refcounted_page) { + if (dirty) + kvm_release_page_dirty(map->refcounted_page); + else + kvm_release_page_clean(map->refcounted_page); + } map->hva = NULL; map->page = NULL; + map->refcounted_page = NULL; } EXPORT_SYMBOL_GPL(kvm_vcpu_unmap); From patchwork Fri Jul 26 23:51:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743514 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7675FC3DA4A for ; Sat, 27 Jul 2024 00:16:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=hlhq7We3LF/Ul7sqre1CZocM6x6KNUXlB6LyC83BMPc=; b=LCJrhj1EVzCz7EohXGJdwbHpnZ Hk9+fKvYd5ReuV5x/gp1pmJVnqRhLST7bDYuATvb6bs7Ylx3qa83p/MaV8d1dxoLBsksS5o6wQ3Vi PPa7ogyaSXbSftUpfQhtF5iXbY6TCYf+d9Vd5etqupYk0QXtypKClSVoM3w4L4dUeil5CifaNQrC4 q8e3HaHTcnApK7Uujw5YacWp7QQHKvlNog/mUBnpqPu4F4GowHyB0siG/QkKolXYUHuUMDiQHwmf+ EQ/nLlj8HCJykWcsdN49zi0mZ2yW1TKr+JV6LX05qgVaRsVFgEiUkC1etBYxFtaW8S2ohQVJPp6OW ZYjIfEZQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV6W-00000005bll-0ETP; Sat, 27 Jul 2024 00:16:08 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkr-00000005Q0v-1ISi for linux-arm-kernel@bombadil.infradead.org; Fri, 26 Jul 2024 23:53:45 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To:Sender: Content-Transfer-Encoding:Content-ID:Content-Description; bh=hlhq7We3LF/Ul7sqre1CZocM6x6KNUXlB6LyC83BMPc=; b=J2tosjvQqIc4DWj6/rXdDiPi+N uyHFbMUjFtxEuiw7frxbCs3sIv0MLs83p4IJDQn6TY65bHUXhPdsTKzyvdftm5qp6KK/HnkyPqTGM L5zq1nIiJRp1vV8Li9E47978nFl0ed1h9Zyirx0Ax7xHmVnmRR/qba3Mf0a+IOo6qFQJgT6Yp3CpS YHD3wOh6DGxjDuwfaonEtouXS/wJwK878iYL4U7oD9dgQvaktsVHCP1qrBv863wsWdvxNOgMRMI1T q3765eAMDTHJ5MspTD/18LBC9JmhzAtmOzvTu9oUA6A8tculiDu5TWUdJ71Zps1Biv0WFFx2H16sN yeF9pzkg==; Received: from mail-pf1-x449.google.com ([2607:f8b0:4864:20::449]) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkn-00000004Jyx-0QPW for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:43 +0000 Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-70e9e3dd963so1395572b3a.1 for ; Fri, 26 Jul 2024 16:53:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038018; x=1722642818; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=hlhq7We3LF/Ul7sqre1CZocM6x6KNUXlB6LyC83BMPc=; b=Rimon+EmJLdmFAU/YCFWwPAynZDrSIiIUDPOfVWli3+6vmBXbpmxN1aF/tS7Z+yKfe FZ/OE66ljZHOW7flSTxNWWvXhWPz7iuDs0m5dPCYDHV9GFZvawreVCuSStXWJH5tHnb2 krXpkkMyQsBL57JpsHGm9l4CuVY7gL17WI/K2oeg3F12kTdaRZLi2Se5w1If6OHFOzCx vOeFUoKk1uthZGP/BEcQyLhCqxrq20kWaqfixxgWZP1WG896sTJULtxFctJpd/rYZC0x 67CyS3QyG615/tOK7fEsmeE2MBYDxNFCq3FynXpTDb31GGo9vc2p6rAgxt9MpEE3JjiR +dKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038018; x=1722642818; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hlhq7We3LF/Ul7sqre1CZocM6x6KNUXlB6LyC83BMPc=; b=YexJAk3AW+kvmuqL65tplrUD8X+xwb/DV8b+H0ctCAPNCLiTKPdgLcpBdaqrwmjCbM 8nrunmVkovhRoIFg2cGsBUlnERFJ9O4uWcm1h2XmBjoUQJeFaj4TUqWpNAOItdMKfFpw a4gfMjS4PaXJCwDARYDt1tWkfyCRzCy2YqmFx1stLtThdWg3389L4Qc5UZ8VqUr2avL5 389Nzikg9k3W1x7/mJ4kaHiJUkMTgJJ5T6ea5CG0Z424Ns8ZQa1dzKPcYpm9zofoLlDl oKGRztFsHbEScMRRryqU41kboXrFzES72DBGgvpCdmGEF94nwweLhzmoLDGfl9dERdhC id/g== X-Forwarded-Encrypted: i=1; AJvYcCXtIOJPzOWQqhzD9gfd35KtmzV9j4pNHrPpRK2bdv2nBfbWWkPEux8jsWqPpFXO50sBSns6MdtcWoegASzbUzz0Y35Pidk0QZ4LgubrxqhyYqu179Q= X-Gm-Message-State: AOJu0YzVYcuVXp6NB/broXHEuH7C+tjdu5l+cVaMxuUxVmxqp08pOSIn 8uLAtAuF2eB4ULIfBNIbxnuuI+JUjNmRFtPXQ6rVGF6D3rC6wv6wpnyZnYIpvXJ8jiI3e+8WuNC 4aQ== X-Google-Smtp-Source: AGHT+IHh1dyWiT0um9sPElrkv3mBIH4IG/eZ2ITHeKZHWjYM95v4ERaP3jgKxmY1KlRL2J6TSkYz8SuqfJg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:2f10:b0:70e:9de1:9edf with SMTP id d2e1a72fcca58-70ece9fc2c1mr8667b3a.1.1722038017517; Fri, 26 Jul 2024 16:53:37 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:38 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-30-seanjc@google.com> Subject: [PATCH v12 29/84] KVM: Pin (as in FOLL_PIN) pages during kvm_vcpu_map() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240727_005341_371692_1B1D0BDA X-CRM114-Status: GOOD ( 24.88 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Pin, as in FOLL_PIN, pages when mapping them for direct access by KVM. As per Documentation/core-api/pin_user_pages.rst, writing to a page that was gotten via FOLL_GET is explicitly disallowed. Correct (uses FOLL_PIN calls): pin_user_pages() write to the data within the pages unpin_user_pages() INCORRECT (uses FOLL_GET calls): get_user_pages() write to the data within the pages put_page() Unfortunately, FOLL_PIN is a "private" flag, and so kvm_follow_pfn must use a one-off bool instead of being able to piggyback the "flags" field. Link: https://lwn.net/Articles/930667 Link: https://lore.kernel.org/all/cover.1683044162.git.lstoakes@gmail.com Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 54 +++++++++++++++++++++++++++++----------- virt/kvm/kvm_mm.h | 7 ++++++ 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 8b5ac3305b05..3d4094ece479 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -280,7 +280,7 @@ struct kvm_host_map { * can be used as guest memory but they are not managed by host * kernel). */ - struct page *refcounted_page; + struct page *pinned_page; struct page *page; void *hva; kvm_pfn_t pfn; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 255cbed83b40..4a9b99c11355 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2824,9 +2824,12 @@ static kvm_pfn_t kvm_resolve_pfn(struct kvm_follow_pfn *kfp, struct page *page, */ if (pte) { pfn = pte_pfn(*pte); - page = kvm_pfn_to_refcounted_page(pfn); - if (page && !get_page_unless_zero(page)) - return KVM_PFN_ERR_FAULT; + + if (!kfp->pin) { + page = kvm_pfn_to_refcounted_page(pfn); + if (page && !get_page_unless_zero(page)) + return KVM_PFN_ERR_FAULT; + } } else { pfn = page_to_pfn(page); } @@ -2845,16 +2848,24 @@ static kvm_pfn_t kvm_resolve_pfn(struct kvm_follow_pfn *kfp, struct page *page, static bool hva_to_pfn_fast(struct kvm_follow_pfn *kfp, kvm_pfn_t *pfn) { struct page *page; + bool r; /* - * Fast pin a writable pfn only if it is a write fault request - * or the caller allows to map a writable pfn for a read fault - * request. + * Try the fast-only path when the caller wants to pin/get the page for + * writing. If the caller only wants to read the page, KVM must go + * down the full, slow path in order to avoid racing an operation that + * breaks Copy-on-Write (CoW), e.g. so that KVM doesn't end up pointing + * at the old, read-only page while mm/ points at a new, writable page. */ if (!((kfp->flags & FOLL_WRITE) || kfp->map_writable)) return false; - if (get_user_page_fast_only(kfp->hva, FOLL_WRITE, &page)) { + if (kfp->pin) + r = pin_user_pages_fast(kfp->hva, 1, FOLL_WRITE, &page) == 1; + else + r = get_user_page_fast_only(kfp->hva, FOLL_WRITE, &page); + + if (r) { *pfn = kvm_resolve_pfn(kfp, page, NULL, true); return true; } @@ -2883,10 +2894,21 @@ static int hva_to_pfn_slow(struct kvm_follow_pfn *kfp, kvm_pfn_t *pfn) struct page *page, *wpage; int npages; - npages = get_user_pages_unlocked(kfp->hva, 1, &page, flags); + if (kfp->pin) + npages = pin_user_pages_unlocked(kfp->hva, 1, &page, flags); + else + npages = get_user_pages_unlocked(kfp->hva, 1, &page, flags); if (npages != 1) return npages; + /* + * Pinning is mutually exclusive with opportunistically mapping a read + * fault as writable, as KVM should never pin pages when mapping memory + * into the guest (pinning is only for direct accesses from KVM). + */ + if (WARN_ON_ONCE(kfp->map_writable && kfp->pin)) + goto out; + /* map read fault as writable if possible */ if (!(flags & FOLL_WRITE) && kfp->map_writable && get_user_page_fast_only(kfp->hva, FOLL_WRITE, &wpage)) { @@ -2895,6 +2917,7 @@ static int hva_to_pfn_slow(struct kvm_follow_pfn *kfp, kvm_pfn_t *pfn) flags |= FOLL_WRITE; } +out: *pfn = kvm_resolve_pfn(kfp, page, NULL, flags & FOLL_WRITE); return npages; } @@ -3119,10 +3142,11 @@ int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) .slot = gfn_to_memslot(vcpu->kvm, gfn), .gfn = gfn, .flags = FOLL_WRITE, - .refcounted_page = &map->refcounted_page, + .refcounted_page = &map->pinned_page, + .pin = true, }; - map->refcounted_page = NULL; + map->pinned_page = NULL; map->page = NULL; map->hva = NULL; map->gfn = gfn; @@ -3159,16 +3183,16 @@ void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty) if (dirty) kvm_vcpu_mark_page_dirty(vcpu, map->gfn); - if (map->refcounted_page) { + if (map->pinned_page) { if (dirty) - kvm_release_page_dirty(map->refcounted_page); - else - kvm_release_page_clean(map->refcounted_page); + kvm_set_page_dirty(map->pinned_page); + kvm_set_page_accessed(map->pinned_page); + unpin_user_page(map->pinned_page); } map->hva = NULL; map->page = NULL; - map->refcounted_page = NULL; + map->pinned_page = NULL; } EXPORT_SYMBOL_GPL(kvm_vcpu_unmap); diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h index d3ac1ba8ba66..acef3f5c582a 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -30,6 +30,13 @@ struct kvm_follow_pfn { /* FOLL_* flags modifying lookup behavior, e.g. FOLL_WRITE. */ unsigned int flags; + /* + * Pin the page (effectively FOLL_PIN, which is an mm/ internal flag). + * The page *must* be pinned if KVM will write to the page via a kernel + * mapping, e.g. via kmap(), mremap(), etc. + */ + bool pin; + /* * If non-NULL, try to get a writable mapping even for a read fault. * Set to true if a writable mapping was obtained. From patchwork Fri Jul 26 23:51:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743515 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 59312C3DA4A for ; Sat, 27 Jul 2024 00:16:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ABkrzr1cGSxzpfOXTkcnfxpngM7W7Byv3yxUL9/Xpog=; b=ES3RmTegjfag0bl761qw8Ts3f/ TiLFL0hHRAbmIpz83DHL2k6iNmzWbn6ehPfm9wbo1Iw7UGbHmIDI7CYjCHyTtTDG8SOny8PE3tWDn sDJFTfNNG9gqSokGhoJ3FHZzRFaZ7Di81qaKgq1qrX8RP40wW5kjTKBV0HXGX8rGuscP0fEUG/Q6/ yklwU33kgUBkMuDsbj4THeY5O3QeVDnsuQiL8HtYPnyGqtt/0WdaxCNe4ouxEHG6IwAlBIF3538n9 1/4XAX0Avp1b4HQmlwMdxKGEzCpJbLhZ/j+FOQuU5YUweMEdJix5FtQKX1rho9EUeJEqXwDHKqKt1 21e9UUEA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV6u-00000005bwf-2gpd; Sat, 27 Jul 2024 00:16:32 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkn-00000005PxX-2rKm for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:45 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-e03b3f48c65so446177276.0 for ; Fri, 26 Jul 2024 16:53:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038020; x=1722642820; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=ABkrzr1cGSxzpfOXTkcnfxpngM7W7Byv3yxUL9/Xpog=; b=wyE64ImBNbMdo6E0DTERUJzgCvzHeAYGZGutnCKneiUwLYcNVm53aTqAu7fHClgsrS GtYbnE6jxLpB+a/yDh6y5oY+X5TSAzRqVlJUTyiNv/lWcm0l7qaxCSXq2X9F+EgLiLlu ymJ5mjfnqQHj8ayV1r2PTJfm4bei+VYut6COJDF562CwznPkhFGDqv/ah+CCoMhanb4k 4nyOP483zqSHkGR4fo5wvw2mCH8w4e5/VKc96KEMIWmZQhmnQ2M3/xRANkQHkyBfnUnH 38yBsU4Jukdz30a18ZEChMd2XZKmuK2egQvA7AJ/3glgbd8jdBEs5Os9wZk3M2MqTG91 8S8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038020; x=1722642820; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ABkrzr1cGSxzpfOXTkcnfxpngM7W7Byv3yxUL9/Xpog=; b=kFvukiaQCYjL4VF7Yo8PPwm7gCl/bLrZ4186XVyERP9fTOov7XKEqzurFnTWi3CbbO chvuvRIvRsT/HlRRWFPIk+7UwkgUOYWi3nr3bjGpPiosBu0VAzMokgLR+4KSGf+8L0vd WDbn9Yrri19we3EiMdzGos6HVUpHVk03p7zWNWSJRW4Oh2oN+QkUJxSEQEwscgfEzdKM 4sf7zsDDMBf2ZVGctDHuuNZjgUwzjZC5ncT8LBq1qVPUjKle6bchr7xMwiYZQbXxRbRw iN4+OaSBUUFpKU4B8cf6rBR4qhqsxBVgonBoJBXM1yUPrxl/D+unWX2aQAGHgeBeoTwz VFqA== X-Forwarded-Encrypted: i=1; AJvYcCXqTYEqJH/bsUDop9j1xHIOga/mpGCBByOEiYivFvCqy7K11bijfgMdYB1lzkv+NwwfTKxKmjQlH+JqOg7lozdSD9zh8Sjsxrhn6rIEFjYca7GLM6g= X-Gm-Message-State: AOJu0YzP7TAgrS/xxrrGCZOFJri+yB0+r1ghx6uqm5azG4TXQCT3MM2K OPhuykp0OG9M6I2Krl5lH0gQIGK0/kO/YvbMmYmpCA3UXUAQXKZ8TFoZXgInAVlAUTb2hHTkpwf UkA== X-Google-Smtp-Source: AGHT+IF9e10ucuu5PEDYEm2t1VHH57SvniduNcr3PDVCEbnd2uQ/527xnsEGixiu/Fx7KhH32OKutuDbjT0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:9004:0:b0:e03:b9df:aa13 with SMTP id 3f1490d57ef6-e0b5455eafcmr36206276.8.1722038019655; Fri, 26 Jul 2024 16:53:39 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:39 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-31-seanjc@google.com> Subject: [PATCH v12 30/84] KVM: nVMX: Mark vmcs12's APIC access page dirty when unmapping From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165341_909731_022DAFC3 X-CRM114-Status: GOOD ( 13.08 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark the APIC access page as dirty when unmapping it from KVM. The fact that the page _shouldn't_ be written doesn't guarantee the page _won't_ be written. And while the contents are likely irrelevant, the values _are_ visible to the guest, i.e. dropping writes would be visible to the guest (though obviously highly unlikely to be problematic in practice). Marking the map dirty will allow specifying the write vs. read-only when *mapping* the memory, which in turn will allow creating read-only maps. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 8d05d1d9f544..3096f6f5ecdb 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -318,12 +318,7 @@ static void nested_put_vmcs12_pages(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); - /* - * Unpin physical memory we referred to in the vmcs02. The APIC access - * page's backing page (yeah, confusing) shouldn't actually be accessed, - * and if it is written, the contents are irrelevant. - */ - kvm_vcpu_unmap(vcpu, &vmx->nested.apic_access_page_map, false); + kvm_vcpu_unmap(vcpu, &vmx->nested.apic_access_page_map, true); kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); vmx->nested.pi_desc = NULL; From patchwork Fri Jul 26 23:51:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743470 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DD51CC3DA4A for ; Sat, 27 Jul 2024 00:05:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=XPLhffmLB4ZFWgxHZuTKpVpsLNVQ+RH00jdWHKAirBA=; b=pvuuC5nslod2xbnYHYSRAQvbQl v0azjP4aykxyXDEUQIej8JFfkPX2Fbi1Eal04LGXW99zuihtr4JvidtaP6fKvVPSu3vyh78c2ELAI JesZHYTbWXzADxFpvfGcgyciUBSMQcOV8cH1vi0SeWc8NcJrswpnRD9RwusYzw5AyEyDuy0kunz0n gh4L/sQD/VYkeTpesaLj34z9XaiIE9IyqYY+8KU0WyL9Eda38ZP1y0gtg+bMknLJ2HJfLxLAjLNz5 t0FHLQUaAVUY3mmJkr9Ep0YBEoxkGP/VmaRduW46/WiAE/iRfZelsC4dA3IK1i44S5URNRLG0DJbh ZyM51p1Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUwU-00000005Wqv-3u6f; Sat, 27 Jul 2024 00:05:46 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkp-00000005PzZ-2nx8 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:47 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e0b2ac67a53so433835276.0 for ; Fri, 26 Jul 2024 16:53:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038022; x=1722642822; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=XPLhffmLB4ZFWgxHZuTKpVpsLNVQ+RH00jdWHKAirBA=; b=oLzS03F1aLggBVp3loR2zFGp1J1AeN4JV+4XlD3sk4s/g+U85d4i1D40+B/h2VZFmt YJpnR9HexVXb5KBFZEgwfSLKWwGc17qQG+NabFuwebSA07Ga8sqXBcRIhJbxJaGUKCFH HQscrJdqsS+/dSyUm67gb2AdztJDqmhjkmQIgroIuX2pSRyPPoAqiUkuDD2+rH5qIXGe q1MAAyTPInWs5QYVfCG1yl1AqM94rBQwPRFhuoPF/nZxT5MYF2CnoR3xbMgnVIKcQg32 D3A8gq/7SK9RkvqhVaOjMQH8Ov0yEn8kSXMxlcNU9OCzfO780EKhbUTaD8+tQtq6bcG1 rMFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038022; x=1722642822; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XPLhffmLB4ZFWgxHZuTKpVpsLNVQ+RH00jdWHKAirBA=; b=Uu0N85JXrdVWuz8uzTo/Y34m1LVQISOKnnH7bMNSsQzZuyzkdY96IFZotS2XYXFCZF OmdYx4BYZ6eXHcdzqpuGRoZwbgcAKuGPkHm0IHb31om9+mz1hqZ8tv12lVOSkuTblTtE HgbICujkuWJuNmr1Y4bMztpYATsp3GdbxSxDx+mR0JWaS/PCz+w5wZPug4y7IGF9Oktj uZ1cQNoHCuTOB5IWehZTjeM/vYm20xzssYO7+kKSd8o0s3IseJwZmQYLO24Zjj1pfZh1 1q5jEN3+fbiB6cnACi6b5Hg3Dlay+uX48WGo+1v4GBI7He5fDHCrdBbgAJT6qKV1QzDU TQQQ== X-Forwarded-Encrypted: i=1; AJvYcCXivrs85brlLb5Nta99x9mzO7kMDr5Vcd2k4WspeSErgBOkUnt2X3ZcnNwr2MvdQwHM50OjneGZEz4IJVOv5dPEy2EwW9btKyIpZ1kwql/h1J0hCKI= X-Gm-Message-State: AOJu0YxXp4C//2WO8oxfc1rHrN+aiiZwNX8Njn1ngO37gkEYtau2otZ3 bu0Wn/kL9fe8oQBxlZxu/y5LhXQnW3AvGiV4DirmTfM/gkQXKG5+sRTxUfCmy4XKvaFuPyzds7n U8Q== X-Google-Smtp-Source: AGHT+IGBhzeuzUiyouvcvFTiXgb/HBxmg2MAS0wOhJT2px7pnvcCfHG92BiTIsAz4jQy3aPwc+Eekl1Ce9c= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:6844:0:b0:e0b:3432:73d4 with SMTP id 3f1490d57ef6-e0b5560e6b3mr22860276.1.1722038021685; Fri, 26 Jul 2024 16:53:41 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:40 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-32-seanjc@google.com> Subject: [PATCH v12 31/84] KVM: Pass in write/dirty to kvm_vcpu_map(), not kvm_vcpu_unmap() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165343_859824_9F0D8E46 X-CRM114-Status: GOOD ( 17.89 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Now that all kvm_vcpu_{,un}map() users pass "true" for @dirty, have them pass "true" as a @writable param to kvm_vcpu_map(), and thus create a read-only mapping when possible. Note, creating read-only mappings can be theoretically slower, as they don't play nice with fast GUP due to the need to break CoW before mapping the underlying PFN. But practically speaking, creating a mapping isn't a super hot path, and getting a writable mapping for reading is weird and confusing. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/nested.c | 4 ++-- arch/x86/kvm/svm/sev.c | 2 +- arch/x86/kvm/svm/svm.c | 8 ++++---- arch/x86/kvm/vmx/nested.c | 16 ++++++++-------- include/linux/kvm_host.h | 20 ++++++++++++++++++-- virt/kvm/kvm_main.c | 12 +++++++----- 6 files changed, 40 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 6f704c1037e5..23b3a228cd0a 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -922,7 +922,7 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) nested_svm_vmexit(svm); out: - kvm_vcpu_unmap(vcpu, &map, true); + kvm_vcpu_unmap(vcpu, &map); return ret; } @@ -1126,7 +1126,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm) vmcb12->control.exit_int_info_err, KVM_ISA_SVM); - kvm_vcpu_unmap(vcpu, &map, true); + kvm_vcpu_unmap(vcpu, &map); nested_svm_transition_tlb_flush(vcpu); diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index a16c873b3232..62f63fd714df 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3466,7 +3466,7 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm) sev_es_sync_to_ghcb(svm); - kvm_vcpu_unmap(&svm->vcpu, &svm->sev_es.ghcb_map, true); + kvm_vcpu_unmap(&svm->vcpu, &svm->sev_es.ghcb_map); svm->sev_es.ghcb = NULL; } diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index c115d26844f7..742a2cec04ce 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2299,7 +2299,7 @@ static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload) svm_copy_vmloadsave_state(vmcb12, svm->vmcb); } - kvm_vcpu_unmap(vcpu, &map, true); + kvm_vcpu_unmap(vcpu, &map); return ret; } @@ -4690,7 +4690,7 @@ static int svm_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram) svm_copy_vmrun_state(map_save.hva + 0x400, &svm->vmcb01.ptr->save); - kvm_vcpu_unmap(vcpu, &map_save, true); + kvm_vcpu_unmap(vcpu, &map_save); return 0; } @@ -4750,9 +4750,9 @@ static int svm_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram) svm->nested.nested_run_pending = 1; unmap_save: - kvm_vcpu_unmap(vcpu, &map_save, true); + kvm_vcpu_unmap(vcpu, &map_save); unmap_map: - kvm_vcpu_unmap(vcpu, &map, true); + kvm_vcpu_unmap(vcpu, &map); return ret; } diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 3096f6f5ecdb..f7dde74ff565 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -231,7 +231,7 @@ static inline void nested_release_evmcs(struct kvm_vcpu *vcpu) struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); struct vcpu_vmx *vmx = to_vmx(vcpu); - kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true); + kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map); vmx->nested.hv_evmcs = NULL; vmx->nested.hv_evmcs_vmptr = EVMPTR_INVALID; @@ -318,9 +318,9 @@ static void nested_put_vmcs12_pages(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); - kvm_vcpu_unmap(vcpu, &vmx->nested.apic_access_page_map, true); - kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); - kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); + kvm_vcpu_unmap(vcpu, &vmx->nested.apic_access_page_map); + kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map); + kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map); vmx->nested.pi_desc = NULL; } @@ -624,7 +624,7 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, int msr; unsigned long *msr_bitmap_l1; unsigned long *msr_bitmap_l0 = vmx->nested.vmcs02.msr_bitmap; - struct kvm_host_map msr_bitmap_map; + struct kvm_host_map map; /* Nothing to do if the MSR bitmap is not in use. */ if (!cpu_has_vmx_msr_bitmap() || @@ -647,10 +647,10 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, return true; } - if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), &msr_bitmap_map)) + if (kvm_vcpu_map_readonly(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), &map)) return false; - msr_bitmap_l1 = (unsigned long *)msr_bitmap_map.hva; + msr_bitmap_l1 = (unsigned long *)map.hva; /* * To keep the control flow simple, pay eight 8-byte writes (sixteen @@ -714,7 +714,7 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, MSR_IA32_FLUSH_CMD, MSR_TYPE_W); - kvm_vcpu_unmap(vcpu, &msr_bitmap_map, false); + kvm_vcpu_unmap(vcpu, &map); vmx->nested.force_msr_bitmap_recalc = false; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 3d4094ece479..82ca0971c156 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -285,6 +285,7 @@ struct kvm_host_map { void *hva; kvm_pfn_t pfn; kvm_pfn_t gfn; + bool writable; }; /* @@ -1297,8 +1298,23 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn); struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu); struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn); kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn); -int kvm_vcpu_map(struct kvm_vcpu *vcpu, gpa_t gpa, struct kvm_host_map *map); -void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty); + +int __kvm_vcpu_map(struct kvm_vcpu *vcpu, gpa_t gpa, struct kvm_host_map *map, + bool writable); +void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map); + +static inline int kvm_vcpu_map(struct kvm_vcpu *vcpu, gpa_t gpa, + struct kvm_host_map *map) +{ + return __kvm_vcpu_map(vcpu, gpa, map, true); +} + +static inline int kvm_vcpu_map_readonly(struct kvm_vcpu *vcpu, gpa_t gpa, + struct kvm_host_map *map) +{ + return __kvm_vcpu_map(vcpu, gpa, map, false); +} + unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn); unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable); int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, int offset, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 4a9b99c11355..a46c7bf1f902 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3136,7 +3136,8 @@ struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) } EXPORT_SYMBOL_GPL(gfn_to_page); -int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) +int __kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map, + bool writable) { struct kvm_follow_pfn kfp = { .slot = gfn_to_memslot(vcpu->kvm, gfn), @@ -3150,6 +3151,7 @@ int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) map->page = NULL; map->hva = NULL; map->gfn = gfn; + map->writable = writable; map->pfn = kvm_follow_pfn(&kfp); if (is_error_noslot_pfn(map->pfn)) @@ -3166,9 +3168,9 @@ int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) return map->hva ? 0 : -EFAULT; } -EXPORT_SYMBOL_GPL(kvm_vcpu_map); +EXPORT_SYMBOL_GPL(__kvm_vcpu_map); -void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty) +void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map) { if (!map->hva) return; @@ -3180,11 +3182,11 @@ void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty) memunmap(map->hva); #endif - if (dirty) + if (map->writable) kvm_vcpu_mark_page_dirty(vcpu, map->gfn); if (map->pinned_page) { - if (dirty) + if (map->writable) kvm_set_page_dirty(map->pinned_page); kvm_set_page_accessed(map->pinned_page); unpin_user_page(map->pinned_page); From patchwork Fri Jul 26 23:51:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743471 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3740EC3DA49 for ; Sat, 27 Jul 2024 00:06:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=bC28fbRseb42Ch2zMqBFWEix5TOk22ZdgToO+ip0K/0=; b=tqJ9kBi+go5kCavMAeXEQKg8u0 vRGIO18dChypRIglYme6Fgs7COiqtfVu/VxI20BN1xOIXlGmnPncVsC4v57cGfyOB1MYhHONqYDiT rCxkboSNj49zDddbkUW8w1DzwS5s2hBqXI5mF+VqksEFfi9Blgq3ud+JnMAkvZZTSw2RoalB8ccOU b75PKUKLirYCmyfhO8y8Mz1W1cPURXZ4IxPNd/S9Z4ZleUUpbFuHIKbDsj1SuKDbw3PQ07Jiezecr 1zqZ6InR3LlyEWOikGxSfGYL/Mb657icDvWDVq144QM0FmkrqMZyVZTalCmDU4OFiz1aQMunitP4P XJgOLuLw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUwu-00000005X3J-31Q9; Sat, 27 Jul 2024 00:06:12 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkr-00000005Q0p-1pbd for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:47 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e0879957a99so416342276.1 for ; Fri, 26 Jul 2024 16:53:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038024; x=1722642824; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=bC28fbRseb42Ch2zMqBFWEix5TOk22ZdgToO+ip0K/0=; b=wlhR9UHWbRCcQsCk7Qyniuas+4OWywK1BeSXLA4zE78tE6GChzNJdviYi1HSoc6ax+ d5InN80QZ7NVPje2u+/kGiRgzLMjqD1/uw3IT/pVfIDbPxxubwiIuaMwhEzhhZ1gm5ME ADd7f7qw747nrFDjEnX4nRrpHj9GwWm6YegJe/3i7siQiydGWbUr1LRCjrix/y5dpmYP WcS/UQHpztxSTruAEyOiyrTIxz6BGeITkWO/cPIRYxpTWSdAryT+ZkD81tYpAxlpUnN9 IxV6UFPt8AYBHzjxmOkSwTHuE2YVus3/UZPegFqoW6Y7Jmw4lERsEdBZXSVQDm5CTnyg lkIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038024; x=1722642824; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bC28fbRseb42Ch2zMqBFWEix5TOk22ZdgToO+ip0K/0=; b=h3GjkL2RJ+MfT5pOKRTeTJRfVlWhZW2cgSorq+c0QyDw3//4doo+CEQc1butS2QZxg PpE7p2SS/hTlBqmTv1yMF9PNFQTJSeerv7MfcF+8PhtiRc+qhFvjncX+YhiD3chcAGf0 a4wuLzg6aW9jTmLoo4F+sqqou9qXSH+rIPWuevcItKg69UNfEJ4b21hhjYgHP+ttScG4 S88iMvr4fOoM0DVTvwT8f1RG/LONcuizqIpRkELgPG0TMu3EGoLfiIAMOrffDouGgYqw QPYyZnjrtqqSMM5X8ELfkNokZGE8u5ubWv7DTmfcwD0/A8R3YnH1pPysoCsZ6B1GAY3t 8xyw== X-Forwarded-Encrypted: i=1; AJvYcCWlvXd7fHfDyHMibBbbK1DvZferYBXC+FnYmZHb27QA3pJHH0zdkPikQRv/09+YYOzFMY3+XTllpRpg5MIOkYkNE8ZC421EZJHrKemElZ0ZiaF5wgA= X-Gm-Message-State: AOJu0Yy4xqHq8PMs6YBbKNOjRjVtMSxNQCNcss3gWz7ScIvnfHzN29zq 7gAFTHg/F6/rpBU8Rxr/LEzWnFS/FpVJmBeQ04lO0fFyUnPnjFMaF/N581Vh2Rk9DyEnzAQ0fo2 +6g== X-Google-Smtp-Source: AGHT+IFe6CawsiHghHJz6goDA5MI5OiaYSIvtgnV/oY+y/tLOOW/H4O66kyosfEcqs3UQ2nQeG3uHH/jimU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:181d:b0:e03:5144:1d48 with SMTP id 3f1490d57ef6-e0b5452490amr2050276.11.1722038023813; Fri, 26 Jul 2024 16:53:43 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:41 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-33-seanjc@google.com> Subject: [PATCH v12 32/84] KVM: Get writable mapping for __kvm_vcpu_map() only when necessary From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165345_686176_76CB8149 X-CRM114-Status: GOOD ( 12.29 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org When creating a memory map for read, don't request a writable pfn from the primary MMU. While creating read-only mappings can be theoretically slower, as they don't play nice with fast GUP due to the need to break CoW before mapping the underlying PFN, practically speaking, creating a mapping isn't a super hot path, and getting a writable mapping for reading is weird and confusing. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index a46c7bf1f902..a28479629488 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3142,7 +3142,7 @@ int __kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map, struct kvm_follow_pfn kfp = { .slot = gfn_to_memslot(vcpu->kvm, gfn), .gfn = gfn, - .flags = FOLL_WRITE, + .flags = writable ? FOLL_WRITE : 0, .refcounted_page = &map->pinned_page, .pin = true, }; From patchwork Fri Jul 26 23:51:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743472 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 964BAC3DA4A for ; Sat, 27 Jul 2024 00:06:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=TLCx2qDCvkD9gyhhD5S2JLlOeuoeLAn7xH7FruJz3kQ=; b=tFqX6KhT8d9uNmQ97D5khj8V5Y 39HWrCjltkS7jT4LYLzzTiHS2NFz5a/gFH40NsoneSEJMg4+GpCpxTQ5WnHJgzQAE9ndA5ApuGsy3 TnA290L/MSQ4BpZgNwdBF68QqTH6vFQj8SLiiXk0txYb4DKW1GvhrRGh1I1rT3RzFlv0YpsoFZ1hc Dq6kNyeZcuNb9wep15NxzLta3+e38DyXOoe2e3rM8+szLwaP1eUIMqsS4wpeN3DUZV/n5mvUtg14s xpLvdWLuIXDKNqa8cjCMpLMgVLZNo5IrqaFeDjLMhrlR1hG18igVsaCgWFpBF+ZSc94yhGEBtWX9/ 8VTVeSpA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUxJ-00000005XFy-33jY; Sat, 27 Jul 2024 00:06:37 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkt-00000005Q2a-01jP for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:50 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-e0b2ca961ebso498688276.0 for ; Fri, 26 Jul 2024 16:53:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038026; x=1722642826; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=TLCx2qDCvkD9gyhhD5S2JLlOeuoeLAn7xH7FruJz3kQ=; b=ViVkUxx3zfWX1yD6qzk5hPWzIEjkXj8anpcFGSg2HFKjPFXWLz28h5jJT+y9/W+XSq Mj1mgTdh1bOa8RPk001AHy0jep4nsL6sCKis+wtlc+BI5youohH3VEMS4UMccor+0bIO RxdaAMHAoD0lwMuhL9cKJuzj+CCtXFjttHgqCHqVQ63RE4jX3Y17mhEQX5ldahrN/Wf0 W1F/xI/h2fR1P9Wa3MFNhJXK7qOK0hAjG89lVyjqf9SBoBy8TaHB8EuBUmL0vVZde6qu B4y1UG7EsYwM2labztv9XN+dXN5giJ9SyLySkrXHmTn4YBxkHms7mvskXKVIEsjXTlmC zzew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038026; x=1722642826; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TLCx2qDCvkD9gyhhD5S2JLlOeuoeLAn7xH7FruJz3kQ=; b=Q3w1ngSRN+oJbTJWLXSJ80+OFlkFReFE/xld5/M1DfaEUxStOEeZcz+HKDNtr+hibz 1UspcsVZs1AdjZ7e7QVgjWSEFe4xf5rYYy/kigxpFtLBHuumIlf+xLVSqJtx3J+i7MmY ndNawvzI9HItHoRaBPVQPjM3eDC0MeXv/lmaEmqZYvXPe5tyP+gLUwbeUTc6vms2Ryuv clE2a9hMDJ1PO/5c4psCG0CEyxrWBVukkdeTZJE7095mDPlwwXBfVAlB/uPN04EVqR8Q k4rdSnKCdZELdTfylpWPSbrcHxRgRwDK6XpzswEaAA8Rl4twJzqBsGNY9BDdGoEPCzFx o4FA== X-Forwarded-Encrypted: i=1; AJvYcCUOHpkfs7XjUC8vKXihFs7lMzZKU4fuNmarEFj2Lz9I45IohV4GJFGIlJP3aSSHzBFKob4Wq3p76DpSGB1PvtUeuRert47uYcJx76iDk/L1ffpG6wo= X-Gm-Message-State: AOJu0YzFaZ9cUw28xwEschwcsZHLPShDxYNc5oEe1IT55ofL2gF9UUJy u1iURcmzbdBY6Zzj9DYvYo40zuFBquH2+P7kF9a2cRb2ofQxpjrNcP8Pu2DHJnFw34yTLuhmth3 2lA== X-Google-Smtp-Source: AGHT+IFuGqnDYAUSJ5FmVd85CGAsbS7YYsLTPQN/3Ul7QhkRMd8+dVyQHeeD8wiMpZyrbrthnX4Q+b9wtNM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1005:b0:e0b:f93:fe8c with SMTP id 3f1490d57ef6-e0b5427fa67mr79832276.0.1722038025699; Fri, 26 Jul 2024 16:53:45 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:42 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-34-seanjc@google.com> Subject: [PATCH v12 33/84] KVM: Disallow direct access (w/o mmu_notifier) to unpinned pfn by default From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165347_267175_AB743AB0 X-CRM114-Status: GOOD ( 16.87 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add an off-by-default module param to control whether or not KVM is allowed to map memory that isn't pinned, i.e. that KVM can't guarantee won't be freed while it is mapped into KVM and/or the guest. Don't remove the functionality entirely, as there are use cases where mapping unpinned memory is safe (as defined by the platform owner), e.g. when memory is hidden from the kernel and managed by userspace, in which case userspace is already fully trusted to not muck with guest memory mappings. But for more typical setups, mapping unpinned memory is wildly unsafe, and unnecessary. The APIs are used exclusively by x86's nested virtualization support, and there is no known (or sane) use case for mapping PFN-mapped memory a KVM guest _and_ letting the guest use it for virtualization structures. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index a28479629488..0b3c0bddaa07 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -94,6 +94,13 @@ unsigned int halt_poll_ns_shrink = 2; module_param(halt_poll_ns_shrink, uint, 0644); EXPORT_SYMBOL_GPL(halt_poll_ns_shrink); +/* + * Allow direct access (from KVM or the CPU) without MMU notifier protection + * to unpinned pages. + */ +static bool allow_unsafe_mappings; +module_param(allow_unsafe_mappings, bool, 0444); + /* * Ordering of locks: * @@ -2821,6 +2828,9 @@ static kvm_pfn_t kvm_resolve_pfn(struct kvm_follow_pfn *kfp, struct page *page, * reference to such pages would cause KVM to prematurely free a page * it doesn't own (KVM gets and puts the one and only reference). * Don't allow those pages until the FIXME is resolved. + * + * Don't grab a reference for pins, callers that pin pages are required + * to check refcounted_page, i.e. must not blindly release the pfn. */ if (pte) { pfn = pte_pfn(*pte); @@ -2942,6 +2952,14 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma, bool write_fault = kfp->flags & FOLL_WRITE; int r; + /* + * Remapped memory cannot be pinned in any meaningful sense. Bail if + * the caller wants to pin the page, i.e. access the page outside of + * MMU notifier protection, and unsafe umappings are disallowed. + */ + if (kfp->pin && !allow_unsafe_mappings) + return -EINVAL; + r = follow_pte(vma, kfp->hva, &ptep, &ptl); if (r) { /* From patchwork Fri Jul 26 23:51:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743473 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C538BC3DA4A for ; Sat, 27 Jul 2024 00:07:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=f0UOaTihDpRJ7PP+Ej2doGcOk+AHWo0BkuOsHzozLVI=; b=wJx8W2GqimW00xPqRsDlBRppP1 xle3yK1t8YMrR4gvVPgkcmLuwtYlWLIsk/nqm63Plhi38xmOwul5PkarZY9U9wezXDPPUZn7cp5At NsfB6Cr2o5qOaPTQWQmWOsoACIcMigyFWmG1dUL7oMnPbxboS1n9CB0bak+uYaRhWN4xsrYGBBfNF L9Cx0rfGzrxPx7uf/P4Xo6R2rxFQ3hNODxmFWugkKbpsx2ynOT4mgxzWMFy4MqwqaFEvHeYd4vBxb 0pE0NI1gQx9UHn9c4yahsCULZdtYvHDAS9EK+5PwGQtfLD137wBmRvaFt0ng5Nk+1I4pUy2yOOJDm nbAmqUlQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUxt-00000005Xe9-2EOK; Sat, 27 Jul 2024 00:07:13 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUku-00000005Q4M-3wJz for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:52 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1fc54c57a92so10626555ad.3 for ; Fri, 26 Jul 2024 16:53:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038028; x=1722642828; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=f0UOaTihDpRJ7PP+Ej2doGcOk+AHWo0BkuOsHzozLVI=; b=euoXExK9GWAFuMpIdFQ7q8HBipFFrXmajA3RSJ0IUws9NyIaLOSByOXhNl74zIRdvE VSE/px8NDztYc0LeivyQjS8KKxIEKYMjKDpgGXeCdKensDYIUXnwhS3EoCWtoTrs8Ukr yXI+wZ2PSQMVVgDD3HhXLkin7p13ft0zh6hWSGOVbkj6N1yQOP/h8LSIy11Hm69GTpp3 izYwkBJu2zF+H98+30h/NBmgLub4s4DLNPQso4Acl47SqXicQnUNZV0HSzCxp4jGzHn3 3q1GfmGJ/S/s8x3l8rpP4cQ5LKwGpy+gL0QZscNe3m3L2tHcSGLf7/7FtKrWLxSwo4GE VmOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038028; x=1722642828; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=f0UOaTihDpRJ7PP+Ej2doGcOk+AHWo0BkuOsHzozLVI=; b=iEoYY9tal0/oo7VbtCdr+M4gX/aIQd9AZ+2IbB+SiDzTXXoQ1tKD0w+w0jbRl7AQ75 t2gnJSTArOorg7sq0xQkogpJmwvN4tD52ApET7eIk/dSHfpvCudOIxgHxdz2sSkdQoCg 6262FR+sceCk6viIf0rnj6Lq22m5xdEIt3oExdUSJxRz7Iyrs+fwWWfSGl9pd+lhANU3 OYMm4eWbxs80yT6qSKJx3/5OBcsNdasBIMBS6EZuq3JEU4ShS4KZuihp6e5kjVWDd55l STVHQBBcTrxy1+Ut+kx+ENAtNBRJoZXKxoJrmbA6oGtqPHj3EujjsP0Mzc1zRSRXZWUP FzuQ== X-Forwarded-Encrypted: i=1; AJvYcCUacQk+N4OzG1VJy5edxGAqMd9aa8QGmtA0VHl/aqFML8Vz/vU6zMnhAWdIxNLNPYvWgOsRjPbgPaW6JpoLq6IP@lists.infradead.org X-Gm-Message-State: AOJu0YzpbgolPtt6w0TmcgG1lQ/405b/fXXHtPRzkuzpPwJ/OrERZzLE dT4HeMf87dll/W77Pywnl+XSLeqAehmuLyjOvnDr2bBwSbFLCGFnLSugsvy6JsyHIYRBpVukBlB 0Ow== X-Google-Smtp-Source: AGHT+IGYUMRc1uUxr912DKcgHH4gUUnvf0wLEKnF8dkY5ruUFOJLgoqFTBS1LD3evC3JUrfTD0ZWGbhz678= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:cecd:b0:1fd:6529:7443 with SMTP id d9443c01a7336-1ff0486a65amr545105ad.11.1722038027663; Fri, 26 Jul 2024 16:53:47 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:43 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-35-seanjc@google.com> Subject: [PATCH v12 34/84] KVM: Add a helper to lookup a pfn without grabbing a reference From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165349_206069_1250D7CF X-CRM114-Status: GOOD ( 13.13 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a kvm_follow_pfn() wrapper, kvm_lookup_pfn(), to allow looking up a gfn=>pfn mapping without the caller getting a reference to any underlying page. The API will be used in flows that want to know if a gfn points at a valid pfn, but don't actually need to do anything with the pfn. Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 82ca0971c156..5a572cef4adc 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1212,6 +1212,8 @@ static inline void kvm_release_page_unused(struct page *page) void kvm_release_page_clean(struct page *page); void kvm_release_page_dirty(struct page *page); +kvm_pfn_t kvm_lookup_pfn(struct kvm *kvm, gfn_t gfn); + kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn); kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, bool *writable); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0b3c0bddaa07..ad84dab8c5dc 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3118,6 +3118,22 @@ kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) } EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn); +kvm_pfn_t kvm_lookup_pfn(struct kvm *kvm, gfn_t gfn) +{ + struct page *refcounted_page = NULL; + struct kvm_follow_pfn kfp = { + .slot = gfn_to_memslot(kvm, gfn), + .gfn = gfn, + .flags = FOLL_WRITE, + .refcounted_page = &refcounted_page, + }; + kvm_pfn_t pfn; + + pfn = kvm_follow_pfn(&kfp); + kvm_release_page_unused(refcounted_page); + return pfn; +} + int kvm_prefetch_pages(struct kvm_memory_slot *slot, gfn_t gfn, struct page **pages, int nr_pages) { From patchwork Fri Jul 26 23:51:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743516 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0131BC3DA4A for ; Sat, 27 Jul 2024 00:17:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Xse5B7AOfcqAOlY7bR5VB/olShqjuvoHnQg+aILML2Y=; b=bVkWn5E5P26/La5afb49/SmUJ0 OoVOBmDpQg04iDc/lHJ18ezCxrf6YeqY49ZdSRgFH4B+nfX9tL0KhZh3P3U7xiCQKdd4VNHZySzm0 Dm+Rmn1vLIj3/FWm9vfgfiaBqMjF9qJ2WOzpPn6msYUP0U3qFuwf24KNvrFN1M4SvuM+ScBWIaxVS 1g1vWORTW61F2QUWb69n1vV7SuNrTnv/INIEvcfllU9a1xPoa2SUzKu7m5GnfiOs68Hnb4qLH0xMI YJG3XYQrO5MAa4sXCGI/tgUiHC/AkiCxQBd83s3zRWry6BKNirZcxrkKAiExPnndOMV03aApsEgkt C/g+Bwgg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV7K-00000005c7S-03Sa; Sat, 27 Jul 2024 00:16:58 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkx-00000005Q6T-1H3A for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:53 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70d19a4137dso1423100b3a.1 for ; Fri, 26 Jul 2024 16:53:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038030; x=1722642830; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Xse5B7AOfcqAOlY7bR5VB/olShqjuvoHnQg+aILML2Y=; b=h7J9kfzruyv2/4kKVm58ZGH4y0IlNudqVeWE6/BepGyMXbF/2OSz2HQIaYFgzPANsE xUcLEO9JpXjPzZzr74/E8L1Q7IEpvD6ANmdk5UbfbbWAlZLUCJTkYhWqczOc1RhQYJA9 PBi0olLVJERvd5wQ8y0FyJwNCQMU3T1+RQkknHc+nNwgu1eVXXzaqI6UhfcTzzULaelf vaX+ktifoVrRrEM9eYyCjfvMpgch7a9lXURCHm1I3L40OJDTuBXOc3HroasjaJB0Cor6 PWJaGmwNbDAykEgtJcOhcLdg43TWWBCwmBxZ0xnKLx1lz/VB0XEzkiEasQyUFJ2b0VLx p09g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038030; x=1722642830; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Xse5B7AOfcqAOlY7bR5VB/olShqjuvoHnQg+aILML2Y=; b=nhm4mn9uRkC/DRdeuikgZ0VTaypWkuDoMySWsJtPj8g7f0vyDTNWo+3u65sVZh1WZh LS/BYI86y4wExf5/es3JyCuSesl4tv19527+0snHCr5lc2fwbxQnD/fxMOoYG2p9L4/c m4jPt2wTT2PQsyQIAGW98HDFEZ5Pjmb5gayodKcpKPSzf1RTNLituxLvwIDMASOY8EYw RbqEOSuS/FBf1hsQYZJYj8isHWt1Elux9UWPx4kRz19rab5v8QBqRabtyYLXvKSupop3 SFUawFhq6WlAH0YFBMoLhJoJZNExXIlslZotV9HBnX4rDRL4j5sqizI48qBIrzJeJITL +pkg== X-Forwarded-Encrypted: i=1; AJvYcCVxspb7Knaf5ZYmQI6S+O9f7AmfV7HyMKt8J6v75zY3bv0NCa2pzTxg3dqdQhwgXwFr1ZpZ0ceSJw3WffQWNTtzBrb7+vsfwXAaAKdq3eC359yGals= X-Gm-Message-State: AOJu0YyYPWh76DbygcN9Nm1zVHNNmPl1IMCYslWgwaBgiBRJtoA1W5ij zx/TU6uK8mwtfzIz+sUPyTi+LPKM06w6tqbd/PNXoZUBjYbyUg+jkRazC9tWWzXs7VqWYOvUPiR vQA== X-Google-Smtp-Source: AGHT+IEFcNwoZPtxzQ32cLJ1vxa7gkQJVcgltXDXIuLlGv+WV+Ss10kIc9nwtzcNKm3zIIgT6vAGf5Ndk9o= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:2e2a:b0:70d:9a0e:c13b with SMTP id d2e1a72fcca58-70ece7f0562mr55820b3a.3.1722038029490; Fri, 26 Jul 2024 16:53:49 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:44 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-36-seanjc@google.com> Subject: [PATCH v12 35/84] KVM: x86: Use kvm_lookup_pfn() to check if retrying #PF is useful From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165351_580533_873CDB3F X-CRM114-Status: GOOD ( 16.03 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use kvm_lookup_pfn() instead of an open coded equivalent when checking to see if KVM should exit to userspace or re-enter the guest after failed instruction emulation triggered by a guest page fault. Note, there is a small functional change as kvm_lookup_pfn() doesn't mark the page as accessed, whereas kvm_release_pfn_clean() does mark the page accessed (if the pfn is backed by a refcounted struct page). Neither behavior is wrong per se, e.g. querying the gfn=>pfn mapping doesn't actually access the page, but the guest _did_ access the gfn, otherwise the fault wouldn't have occurred. That said, either KVM will exit to userspace and the guest will likely be terminated, or KVM will re-enter the guest and, barring weirdness in the guest, the guest will re-access the gfn, and KVM will fault-in the pfn and mark it accessed. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index af6c8cf6a37a..59501ad6e7f5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8867,7 +8867,6 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, int emulation_type) { gpa_t gpa = cr2_or_gpa; - kvm_pfn_t pfn; if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) return false; @@ -8892,22 +8891,15 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, } /* - * Do not retry the unhandleable instruction if it faults on the - * readonly host memory, otherwise it will goto a infinite loop: + * Do not retry the unhandleable instruction if emulation was triggered + * for emulated MMIO, e.g. by a readonly memslot or lack of a memslot, + * otherwise KVM will send the vCPU into an infinite loop: * retry instruction -> write #PF -> emulation fail -> retry * instruction -> ... */ - pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); - - /* - * If the instruction failed on the error pfn, it can not be fixed, - * report the error to userspace. - */ - if (is_error_noslot_pfn(pfn)) + if (is_error_noslot_pfn(kvm_lookup_pfn(vcpu->kvm, gpa_to_gfn(gpa)))) return false; - kvm_release_pfn_clean(pfn); - /* * If emulation may have been triggered by a write to a shadowed page * table, unprotect the gfn (zap any relevant SPTEs) and re-enter the From patchwork Fri Jul 26 23:51:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743487 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 14177C3DA49 for ; Sat, 27 Jul 2024 00:08:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4+b7zhduK4lVkLFwoy0UCXuK8f7LQragdXHyxQERLss=; b=SyhEtXqbexOSNQKERP/RTD+tp0 co2fNU9eArEJoILnA3sC+hNJxaoXBOXXwpMCpHK0TCbG8WZaka1mwhhFV02lV4FmC8asQ4mBJLWQo XY3xYvT6apQydfc1H4ZsIQl7m19HDzIj+gfe3B2IVyNdf0dfFiVEL5yPzs6Gr7KHXindG/oVyF9Vr gH+lmMcLYkXXz455VLBYuyIwaLzOB4Em8Wku2DxqqBZsQwDArcT23fjSejnR9oGlQutAlJf4VN4iC Lo5CEhTQBikMH4LvuGwVRdrH0gW/mMg8FKDHHNsx3kPzKbzFjLoW7Twe/TJFyOSMcql20Kb0d9Qx0 8Pe+9b1A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUyd-00000005Y4y-3909; Sat, 27 Jul 2024 00:07:59 +0000 Received: from mail-pj1-x1049.google.com ([2607:f8b0:4864:20::1049]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUky-00000005Q8g-49AA for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:54 +0000 Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2cb5ab2f274so1669829a91.3 for ; Fri, 26 Jul 2024 16:53:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038032; x=1722642832; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=4+b7zhduK4lVkLFwoy0UCXuK8f7LQragdXHyxQERLss=; b=jOgmo1Kvu0Il2tltdTiW7hgTLp/6boECR2qoxz2C3M6DuqGqfEvD6rxMFJao7ANMJC ZKDrQfZMIQpU+VtyB1uuiHiPTeSJoGwLLZ3sYBZPnn2CMsBXgYChW1xF71yGUKT3x1sR Fdd309XxfMlP2vsu4SALW0c/gnZ/uCJTG9067EVKmbothMcXsC3ccHwB1HSqsl+XiWaD 02RZs8Qmc15ZgFoAjOhqo0V2ViMrHvq6ejrNIMpGm9hDxQUhJgl4YP2uWic+JAGlZTTh 0LtJe6rpMqLGG0UJVQPYcJCvjTAvAx6Dgtuiy2zlP8PLmN7eNM/QhAr/wS+gV0QkhpDB JJRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038032; x=1722642832; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4+b7zhduK4lVkLFwoy0UCXuK8f7LQragdXHyxQERLss=; b=X7td5bHsoe06PrRgClczCzUmcAuo8ANhdl4739c7BxGPdKQXPvf2vplcWXgPjUHx90 OwdZz/Rob/HUD4HcIGn2gIDti87U7hF2M4U7poYQcVDqU15bKQj3tDaK/pVrc8EiY607 FteBBpxEPuIWglRQ2FKxeUgYWHgt1GXy24GKurpHsMDhJAT9IZtruPWuZXq5cnIBuCKE y25h4F/PQO48IoFw2MEI0FVqnauyK39jDvanKHkL/BGBR4+LPHZBPcbfI0Fwekxj2QGs Ccosojc1Ee0Gijw9froyUYIpEKOAqhvR2vkuEw2fbulOt1OEpmsqOywIi7Wn9/FUHRf9 r07A== X-Forwarded-Encrypted: i=1; AJvYcCXFkZpr06Oz1lujLyWQxqp4K9s/VQ4JuWxokeoC8hydgmM16SaOwB7TlCSFZkEe5XqSApayq4S6VLRXPExhg4S3RFO6gykOLp6CueC39E7j9Lz29/E= X-Gm-Message-State: AOJu0YzrquhOvARkyKurohBGwIorl4234HNOSsmn+9TF6IQC7fQlV/iw bOcIQSbgmRNjhVb1wAcIUkdQybykXMphB/A5CTyq7nXlBAUxhcWcMnFrYqJgYIDOzvXMVDJKUkh BFA== X-Google-Smtp-Source: AGHT+IH83fBTCbgjCdiYRsrBHzQDSmPEXzxw/dBUECcFBZL6/864S5lAdfsy5Zb31ECaUXnMa239gb1WZDE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:4f8b:b0:2c9:759f:a47d with SMTP id 98e67ed59e1d1-2cf7e84e558mr8570a91.4.1722038031713; Fri, 26 Jul 2024 16:53:51 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:45 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-37-seanjc@google.com> Subject: [PATCH v12 36/84] KVM: x86: Use kvm_lookup_pfn() to check if APIC access page was installed From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165353_081530_EF2ACC28 X-CRM114-Status: GOOD ( 13.16 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use kvm_lookup_pfn() to verify that the APIC access page was allocated and installed as expected. The mapping is controlled by KVM, i.e. it's guaranteed to be backed by struct page, the purpose of the check is purely to ensure the page is allocated, i.e. that KVM doesn't point the guest at garbage. Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 6d65b36fac29..88dc43660d23 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2612,8 +2612,8 @@ void kvm_apic_update_apicv(struct kvm_vcpu *vcpu) int kvm_alloc_apic_access_page(struct kvm *kvm) { - struct page *page; void __user *hva; + kvm_pfn_t pfn; int ret = 0; mutex_lock(&kvm->slots_lock); @@ -2628,17 +2628,16 @@ int kvm_alloc_apic_access_page(struct kvm *kvm) goto out; } - page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); - if (!page) { - ret = -EFAULT; - goto out; - } - /* * Do not pin the page in memory, so that memory hot-unplug * is able to migrate it. */ - put_page(page); + pfn = kvm_lookup_pfn(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); + if (is_error_noslot_pfn(pfn)) { + ret = -EFAULT; + goto out; + } + kvm->arch.apic_access_memslot_enabled = true; out: mutex_unlock(&kvm->slots_lock); From patchwork Fri Jul 26 23:51:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743488 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 12164C3DA4A for ; Sat, 27 Jul 2024 00:08:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=WZrNaP2gtmwMUuypG9xN5YPW8gmxxvZG3Q4zEjuRFyk=; b=eoixFy9RtjbgRkoxiUhYuF0w5z bSPJNXN/z5vZplSgi1sSEtFt7h1YxpOJIlJuvYKc4ndaZ0881vcY0NFbiKXjKPfdwLLmvTtjQ98jM 1hIyrLSulPUOudaGTZYFZK3yRiQ2aj7m59Jkgbr0VOxqIHSXfHfeErQXPrhPPY04KGLmJWCC0MECT /u2bdl8J59GwiByNSRZr44qEmd1A+DAOQuLSKGOS5m0CqNsgv7yVoFH3dtPfrHb6yaiIQw+TMkcXe lGS7vgx/t9rsIpHfor4rrdkLxSmhp9TinxVvC0T7bGUJc15dBbVA3AEB9TOsKm6QXaW8uugkD3XMI Uky9vvBA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUz2-00000005YGY-1DDD; Sat, 27 Jul 2024 00:08:24 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUl1-00000005QAS-0FIg for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:56 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70d34fa1726so1503124b3a.1 for ; Fri, 26 Jul 2024 16:53:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038034; x=1722642834; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=WZrNaP2gtmwMUuypG9xN5YPW8gmxxvZG3Q4zEjuRFyk=; b=SLxWDAOjcO+YeiId+O4qMU3uqrENr8CqVMy/4z08UDYW1WaxrwLJI7lTTpTlWejR40 7ofD7MCOfSl49G0eqbRB//ETO9pMCx4U8Rs2lFivy0DNlNcEVmyi8da0h8tt04xdhpyY Gj4RAyld31uCidFXKb3KEmgBCJ8UdW3gWA6HVqM8olCyQwNLJc7eLdaM4zu2OKPBF5v/ ZaeCkc4Rg026xHgNaEcwkBsnP4r6AUWYI2e4em/JR5yeo0nZQUlHAinwoBXro12T/0wi Qje6jjZQsi+jeIqL19JtWkQxdgZ2Fa+xr2Ruh0PGxvixj0iEKl65MZBqwvc+14AgzyYq LTjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038034; x=1722642834; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WZrNaP2gtmwMUuypG9xN5YPW8gmxxvZG3Q4zEjuRFyk=; b=WpYr/Lr5vI/3TpMgW+kEmkhaELwCLku75H8c0B5z8twrBdee4PJGce6ebOtsDUTzgp Cu7T4Onq3o+kKua8VPvlLnClBKjvGBHr6kJyqBX/LMaQ2VJF8BgteAqm/Cvn64K8qvne PD86ncgB+bKXpvEm9DqlPrFleiqarfep5b2LajBZlib1DC0kAXVGtuFsrR3tjl+eRNMl S/J6Hb1JEHRDeKQ+koZPIXii2h22Nk0vaI06gC3WLMIZpZWCVfliU5z9XVFVK45UbQ7d W1My41h3HKIvi2R/WF0aWk8B4dTjegSuoNdkwFmfATbf48MN4Kl6yvseq/4Ud4k7YdkJ LbDw== X-Forwarded-Encrypted: i=1; AJvYcCVjDRbummVUHCzi7vd//fnDbiPC7gDkVXzK/KKirbiQp0OdnCRjqoK3cBGCX/K/mOLoGbXKT4/Ehb/UNlFFpBSWu7m1jjSvPhYyfWPs7QTaYdaEDHE= X-Gm-Message-State: AOJu0Yyp2He8+2Rh3eNBAG7cWLrnJ90g/CJiTCWbiiaoUEDZlmYgN6I4 Tli7eV/FQC4D3LScT2kumINn5Vo6QfWq6fEXuf9sEixensj6102+FEonc53EOHU/qKVN+y7o8Wr 9dA== X-Google-Smtp-Source: AGHT+IGBlhv2aegJ385ep1cUBQA5c+rLA7WdLDI/xGSka7RxNxI5KP3IWpNdT5/lkHtS9+xMECoFkdebDOk= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1829:b0:70d:1bf2:2878 with SMTP id d2e1a72fcca58-70ecee0a0ebmr24898b3a.5.1722038033787; Fri, 26 Jul 2024 16:53:53 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:46 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-38-seanjc@google.com> Subject: [PATCH v12 37/84] KVM: x86/mmu: Add "mmu" prefix fault-in helpers to free up generic names From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165355_111519_083085E8 X-CRM114-Status: GOOD ( 13.92 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Prefix x86's faultin_pfn helpers with "mmu" so that the mmu-less names can be used by common KVM for similar APIs. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 19 ++++++++++--------- arch/x86/kvm/mmu/mmu_internal.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index a201b56728ae..4d30920f653d 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4301,8 +4301,8 @@ static u8 kvm_max_private_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, return req_max_level; } -static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, - struct kvm_page_fault *fault) +static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault) { int max_order, r; @@ -4325,10 +4325,11 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, return RET_PF_CONTINUE; } -static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) +static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault) { if (fault->is_private) - return kvm_faultin_pfn_private(vcpu, fault); + return kvm_mmu_faultin_pfn_private(vcpu, fault); fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, true, fault->write, &fault->map_writable); @@ -4363,8 +4364,8 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault return RET_PF_CONTINUE; } -static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, - unsigned int access) +static int kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault, unsigned int access) { struct kvm_memory_slot *slot = fault->slot; int ret; @@ -4447,7 +4448,7 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, if (mmu_invalidate_retry_gfn_unsafe(vcpu->kvm, fault->mmu_seq, fault->gfn)) return RET_PF_RETRY; - ret = __kvm_faultin_pfn(vcpu, fault); + ret = __kvm_mmu_faultin_pfn(vcpu, fault); if (ret != RET_PF_CONTINUE) return ret; @@ -4524,7 +4525,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault if (r) return r; - r = kvm_faultin_pfn(vcpu, fault, ACC_ALL); + r = kvm_mmu_faultin_pfn(vcpu, fault, ACC_ALL); if (r != RET_PF_CONTINUE) return r; @@ -4617,7 +4618,7 @@ static int kvm_tdp_mmu_page_fault(struct kvm_vcpu *vcpu, if (r) return r; - r = kvm_faultin_pfn(vcpu, fault, ACC_ALL); + r = kvm_mmu_faultin_pfn(vcpu, fault, ACC_ALL); if (r != RET_PF_CONTINUE) return r; diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index f67396c435df..a5113347bb12 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -235,7 +235,7 @@ struct kvm_page_fault { /* The memslot containing gfn. May be NULL. */ struct kvm_memory_slot *slot; - /* Outputs of kvm_faultin_pfn. */ + /* Outputs of kvm_mmu_faultin_pfn(). */ unsigned long mmu_seq; kvm_pfn_t pfn; bool map_writable; diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index bc801d454f41..b02d0abfca68 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -811,7 +811,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault if (r) return r; - r = kvm_faultin_pfn(vcpu, fault, walker.pte_access); + r = kvm_mmu_faultin_pfn(vcpu, fault, walker.pte_access); if (r != RET_PF_CONTINUE) return r; From patchwork Fri Jul 26 23:51:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743489 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 33BB9C3DA49 for ; Sat, 27 Jul 2024 00:09:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=G6XdYYnK6SmaIVsSbjLWLiXTD6vQEkdodh0PTaLJXbU=; b=wPdC8o638Y0nPzF/mKiYX6D1E0 KDMEuSBXgvW/gE4CM02GpLWwG3L1VCu47kEFgeXc14GI2AXF57YDKNYZxpe6wCEafPsm8NUyF2w1j EFIb+RRh4Z6orscv+rYdO8m3xRW+/ysFEV6ZvAiigWZTLMGbIY3IilA4I3JZgogVVD/fqR6OM7aL6 g6w9nddkEte4YDBr6jVXpiKEr55T/PtPxWbfMor7QVpacXiz6dgyWsfG4SCg0f8srEL8nJKYiCkJi yZKaAWcpPVtYxNowTJju9Yl494+a51+13T8G4UXK+1hdoI8pNAc1A9+rUmntoupHZdgqlmVEKeicg 6PqJl7kw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUzS-00000005YT8-1hd0; Sat, 27 Jul 2024 00:08:50 +0000 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUl3-00000005QC4-0dO1 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:58 +0000 Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1fc5e61f0bbso13361955ad.1 for ; Fri, 26 Jul 2024 16:53:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038036; x=1722642836; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=G6XdYYnK6SmaIVsSbjLWLiXTD6vQEkdodh0PTaLJXbU=; b=iBeXJb+g0qQJ2gVCuL5L2WREBrai8rYzW1tegUMgyrC7f3tNKZDnPvY/+FY8h+tu19 FAqx7jTpFWs9HRJ6j/ID2yP2hNE8wxPrSM6dccHVoAbKu9xHto+hmRmkQeNknM8FH+93 PjA8Vl/d+Nc8bzKM5W/YqVUX99+Pwi5i79aSjpvijZVuHl3Mdu60ojnMfanBg6mqDSbY nCQZkJ17kRAg3nqszLlAldZknGUqg4qtc2UArT6hE2kaYLUaBR5MfwEGyBdy3R06U5nL n8mqgD78SYPTCVzf12e8EfNLyVHmNxquEAdVOtZ+JJgsdG7m4RDRlWDuVZBqfCQ7CZUg OmlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038036; x=1722642836; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=G6XdYYnK6SmaIVsSbjLWLiXTD6vQEkdodh0PTaLJXbU=; b=qgf3k2jSXa5Fbjhgt3dgn3oT/VpTVM6XCNQziwTrM+rx6HwiKuB1KKkGF7n7m9gKEt dZD7c2jgQtyO4sf/yjjpW9HD7RLyvNg2agHUGsZIpjHzDr/4cNUKTFMfs/RneirS4zSG h4hPpdYkEh298OOHH0R4zFZudb5oZ2WOeH7zI59ufVLvV5A3ZfMVN+cri6ULTjOdJbP6 1SdGU7hccPp1bzOni2BWSut3OeEQ0rzXxWtema9+gozzYk0pcOvkfk3i3DN51T3SSydX HAcJdKirA22dMtoihayVN5DNBjDTHuOMa78lWVhOfYQRGXQRD387wmNX9DpiAhydXKC8 XTkQ== X-Forwarded-Encrypted: i=1; AJvYcCXQdx6WQ+cvfrmBtuygVAAdCOE2yCGqlupiYyLxQA6fotT42GzMs4I92IWIeq/vqf18p12HF8u3thfAb+pUUZnnwRk4c3ZXxGIbTgH5/NGRZlfE6O0= X-Gm-Message-State: AOJu0YxZ0pL1SxkLJL3fyHbVCW+9vEZMVecjTO84drDvwfeKlu5Hn4WD 7TbFnBXmAXketXOPvbKK6r5YKAZ56pRETv4/44NVW+QN0ymRp1KQ+/0H55uQR7e1eK+9W+mG0PZ 1bw== X-Google-Smtp-Source: AGHT+IFqcJX/9FY6hmZYd8EnC/sSURY7mS/rFneQMUHDSr3ovaE0Tb6dbx+RwJc5DGDg7FZUxHqlUK5Q8cY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:c945:b0:1fc:733d:8465 with SMTP id d9443c01a7336-1ff0488cadamr598725ad.8.1722038035986; Fri, 26 Jul 2024 16:53:55 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:47 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-39-seanjc@google.com> Subject: [PATCH v12 38/84] KVM: x86/mmu: Put direct prefetched pages via kvm_release_page_clean() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165357_291662_CDD57B46 X-CRM114-Status: GOOD ( 12.83 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use kvm_release_page_clean() to put prefeteched pages instead of calling put_page() directly. This will allow de-duplicating the prefetch code between indirect and direct MMUs. Note, there's a small functional change as kvm_release_page_clean() marks the page/folio as accessed. While it's not strictly guaranteed that the guest will access the page, KVM won't intercept guest accesses, i.e. won't mark the page accessed if it _is_ accessed by the guest (unless A/D bits are disabled, but running without A/D bits is effectively limited to pre-HSW Intel CPUs). Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 4d30920f653d..0def1444c01c 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -2919,7 +2919,7 @@ static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu, for (i = 0; i < ret; i++, gfn++, start++) { mmu_set_spte(vcpu, slot, start, access, gfn, page_to_pfn(pages[i]), NULL); - put_page(pages[i]); + kvm_release_page_clean(pages[i]); } return 0; From patchwork Fri Jul 26 23:51:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743490 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 30260C3DA4A for ; Sat, 27 Jul 2024 00:09:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Qh6T21uN8n1VHf7Uf0xlt62GR+52F2/YXe/M8yozbBs=; b=UsWjURPqnjOnEG6xB5nzZLL8os PQ4jmOCQ7tGeyk+O+vPVzhrq7/DqZxN7C1vNd5pDTRazuReqmVlbS5qg/tifYF83nY8QpTSDODaBG jeWgkCjviigZ3xyTyBr7CXK8YtyLrYs8r5jrALTheIbmpPWQM6N6PD6lLdd4VsO4nrcb52Snw6c8R 6Q2jbVeTetcgS58Iba08j8KpNOJ2NUb4uVd6dXGoP+jPnQH2lPieWr8k8WdU2eHBLpyA0kaCJDAzx XXdiDjsCDzX5UWEpjgbo14KNd1mxoWqvoyGOcxshLZ2jkazjNL2t10W43AHa7MusRVmwltx15fhbw YIt2DIEA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUzx-00000005YmQ-3Hj8; Sat, 27 Jul 2024 00:09:22 +0000 Received: from mail-pj1-x104a.google.com ([2607:f8b0:4864:20::104a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUl5-00000005QDb-2P3G for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:02 +0000 Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-2cb4c2276b6so1596345a91.1 for ; Fri, 26 Jul 2024 16:53:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038038; x=1722642838; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Qh6T21uN8n1VHf7Uf0xlt62GR+52F2/YXe/M8yozbBs=; b=u3EyyRjHaFaF8Nb8+8YiK4ZvPNigzDP90Tl1ktBUsj7zVw67kY3rt+2AUEN3AZQfYZ ysdYnUreZk9GgJwVtXZr/JN5kkop2ZvuVCcvDUcc6aRiwY5/LNYMeaNaq9Q1KjBl9ZG5 0OxU5/sYcdwZYyw+D+6S8mOPqB72D60IM6Z51iCUZFeQYP8sGzUfRXgG/Negc/3svKxi Iw6kGnUm1fo9VUKXwI/tb+3z6iay+Wad6dGRjam1eiwi+lAVq9e5TIwjhxKOPwbEZXU4 wpMYiWMcewO1XzDQMw76QnjuJ84ATV6FsPiUzXdPsjQKMECWJYa3vYUkfW6RHmoLdhv5 YrUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038038; x=1722642838; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Qh6T21uN8n1VHf7Uf0xlt62GR+52F2/YXe/M8yozbBs=; b=MU/GdGFDGbbSkFReCddCNsuTm5PLLWanlzjD4K9okyTE3YdYb5jBBOSLTzlnxNe+6Q ryfjc357G6p/lSCUqXW003qlfxp/tCuuu5OrYwK/V14VB7Yvgxpfyq2Ji59i+YVxMQHo 31z4/FAAr5FSWCKVl76/ih6fzmhwPQZEObAuHApfZGca994VRpDZKhSU7QE8VWPiIq23 MxMIZU5xVdXhvkavBSCweuMrK6oCsxuH6P3oKR7ACekk311PzT5u60wZ7DzOs4oarMFT j6LWN9gIjIpZgasimZakNwLtC7r3Rbwzn8g9JTLIv/P3AgIRv2sGQ41IZaAxc7x4swTZ 1tww== X-Forwarded-Encrypted: i=1; AJvYcCXVcduvqGqIWvFreknIXzl0m1OzQMNWv0GAQfosxhHUlJIxTi/3d0gJmeT8sYUupBxELEa4gn/aP5toEWCjOql13sBae207KatWRgfVYAPwYDoFkXQ= X-Gm-Message-State: AOJu0Yw2OxmgwmfAPNr5FlIyvBAqy9XHxEtTGTGe54tKEgq73WuR6jmr EFrTf5qrA4mBr2Sv3jfzWAA9aos565pv0jQB5Mhey9hIkM1Cw47ScOz2AKIGRDH1Rcm8AKp1boM 6tA== X-Google-Smtp-Source: AGHT+IHJ/uNdZAPdurru3IzCEMezhYQEvGsVQTc+qIu+wSY9S108IorpvBbDocczOA6aFsvKzrBA+JBrX4Q= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:4495:b0:2c9:61e2:ce26 with SMTP id 98e67ed59e1d1-2cf7e1c639emr12819a91.2.1722038038053; Fri, 26 Jul 2024 16:53:58 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:48 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-40-seanjc@google.com> Subject: [PATCH v12 39/84] KVM: x86/mmu: Add common helper to handle prefetching SPTEs From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165359_663613_17AF4F69 X-CRM114-Status: GOOD ( 15.01 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Deduplicate the prefetching code for indirect and direct MMUs. The core logic is the same, the only difference is that indirect MMUs need to prefetch SPTEs one-at-a-time, as contiguous guest virtual addresses aren't guaranteed to yield contiguous guest physical addresses. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 40 +++++++++++++++++++++------------- arch/x86/kvm/mmu/paging_tmpl.h | 13 +---------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 0def1444c01c..e76f64f55c4a 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -2897,32 +2897,41 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot, return ret; } -static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu, - struct kvm_mmu_page *sp, - u64 *start, u64 *end) +static bool kvm_mmu_prefetch_sptes(struct kvm_vcpu *vcpu, gfn_t gfn, u64 *sptep, + int nr_pages, unsigned int access) { struct page *pages[PTE_PREFETCH_NUM]; struct kvm_memory_slot *slot; - unsigned int access = sp->role.access; - int i, ret; - gfn_t gfn; + int i; + + if (WARN_ON_ONCE(nr_pages > PTE_PREFETCH_NUM)) + return false; - gfn = kvm_mmu_page_get_gfn(sp, spte_index(start)); slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK); if (!slot) - return -1; + return false; - ret = kvm_prefetch_pages(slot, gfn, pages, end - start); - if (ret <= 0) - return -1; + nr_pages = kvm_prefetch_pages(slot, gfn, pages, nr_pages); + if (nr_pages <= 0) + return false; - for (i = 0; i < ret; i++, gfn++, start++) { - mmu_set_spte(vcpu, slot, start, access, gfn, + for (i = 0; i < nr_pages; i++, gfn++, sptep++) { + mmu_set_spte(vcpu, slot, sptep, access, gfn, page_to_pfn(pages[i]), NULL); kvm_release_page_clean(pages[i]); } - return 0; + return true; +} + +static bool direct_pte_prefetch_many(struct kvm_vcpu *vcpu, + struct kvm_mmu_page *sp, + u64 *start, u64 *end) +{ + gfn_t gfn = kvm_mmu_page_get_gfn(sp, spte_index(start)); + unsigned int access = sp->role.access; + + return kvm_mmu_prefetch_sptes(vcpu, gfn, start, end - start, access); } static void __direct_pte_prefetch(struct kvm_vcpu *vcpu, @@ -2940,8 +2949,9 @@ static void __direct_pte_prefetch(struct kvm_vcpu *vcpu, if (is_shadow_present_pte(*spte) || spte == sptep) { if (!start) continue; - if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0) + if (!direct_pte_prefetch_many(vcpu, sp, start, spte)) return; + start = NULL; } else if (!start) start = spte; diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index b02d0abfca68..e1c2f098d9d5 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -533,9 +533,7 @@ static bool FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, u64 *spte, pt_element_t gpte) { - struct kvm_memory_slot *slot; unsigned pte_access; - struct page *page; gfn_t gfn; if (FNAME(prefetch_invalid_gpte)(vcpu, sp, spte, gpte)) @@ -545,16 +543,7 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, pte_access = sp->role.access & FNAME(gpte_access)(gpte); FNAME(protect_clean_gpte)(vcpu->arch.mmu, &pte_access, gpte); - slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, pte_access & ACC_WRITE_MASK); - if (!slot) - return false; - - if (kvm_prefetch_pages(slot, gfn, &page, 1) != 1) - return false; - - mmu_set_spte(vcpu, slot, spte, pte_access, gfn, page_to_pfn(page), NULL); - kvm_release_page_clean(page); - return true; + return kvm_mmu_prefetch_sptes(vcpu, gfn, spte, 1, pte_access); } static bool FNAME(gpte_changed)(struct kvm_vcpu *vcpu, From patchwork Fri Jul 26 23:51:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743491 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9392FC3DA4A for ; Sat, 27 Jul 2024 00:10:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=kjY6k542G0oo6Yjo2zUiSEPku2YJftlYNe05DeRMCZs=; b=KHd1f0RFv0zEE4RrJpCH5dNskN lAntgsxS6zZyVA26s8TIB0xnoe5mt1Yp5gitmLRNvHpCYP+rNkOhShmBVSnUKNZCjQOysvATLgoWL PlshK89A5c3XXH5SFJOueympcMhzyPXPaJjK7QZtA0lcf5Q0HwCZLvdQG49BOJSyWxh5Rg8ZEYWSp dyo76SCEERgOuHlPwgdbtT/wpagcBaHDMuFTGp+JSOE9PJhs9/oUz2c8UC71xwqW6YzNO+6AImBEo zCsxaTIrnBrIzm+4wou0zNmNUP468gatJ3AZlvwG+W1cFuTLrvq4WPcIrztBk8n2OQwu+LNp9BOR5 k/CzIC3Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV0T-00000005Z4v-3Cgg; Sat, 27 Jul 2024 00:09:53 +0000 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUl7-00000005QEt-1HJF for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:03 +0000 Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1fc658a161bso8412885ad.0 for ; Fri, 26 Jul 2024 16:54:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038040; x=1722642840; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=kjY6k542G0oo6Yjo2zUiSEPku2YJftlYNe05DeRMCZs=; b=bRqKG9Wa+MUKW1vCG0J73XcCzMXMHemQIBdaDy1XerJJT1Nij4D3pVb1IRMYADK4ZR 6Udh+HI0jeMTyGj6eJCw46zuK88teH34OVBVlRvV84TTRpZIE95jwmUD/61b+O5xuzCF gxpkllcoDYIVpZKoHBclWNdFGdubgE+Ie3dApqxsz4vESIc+Q3893trXFkF0+gJnrBKZ 9mKxiR9XdOikVg+BiQb9P/QoIBN0rUuJS1LSOJN0z5VgIK+AZ/XQnGSiqsrjxLdIY9M+ JAGwdLJEF5qlaoT+UkLpiSW4pUUPSRNfLfDKJzaKh7Qn48IzEvFSVNNqDDxXnWt9zEk2 yuqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038040; x=1722642840; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=kjY6k542G0oo6Yjo2zUiSEPku2YJftlYNe05DeRMCZs=; b=cA7OjpkrxWLmByN4T750/wMEmIXaVqd5rT1OxlOI9yUt8seEBjQqucWHwIPEUoUx4/ eh3RcLKd2vdfylwyxI3fy1C9+YNyivisD+3Tkv3Tybf17Oze5sfjkwhGQkHfAY3fyDbQ 2/vcv3nHYJE974DqTi74qzsIdnpg4wMgx7TXo/KDQZv8YV2Gu76xRCTG4WeLpzI6rEtZ XoakNATyr2TWcv3U6clfSXcePLq5AAYcIbRmZL76Xt0NmFrqFnTXV/xAn/9ivckIzfjK FkkxLQGyYgtNOW9HKPUi8R1KMp/TcOfrw5hAB/hkk09WRfIM7SP38wlMj93aTIk/+RCC hQPA== X-Forwarded-Encrypted: i=1; AJvYcCUemP2ZqMyjIDaIADvlvcdspANb9Y1S2jQWmq1GzKK8J5QPc6tmIHBgOt6oCr4KUEKYgslX6xzWoWYp6NL3pFSUGKH05trYJWs3Q0CbdnjxccQ4eTs= X-Gm-Message-State: AOJu0Yy2RVrZNtCOK83RWa7Gsmd4t10SaN0KFSoBLRx8St68eIn+qCax kTiisQZUF8skTdis7heZvYCSrzseB3coxp4rX2mk/vfIMcbWM8q9IpnoIhsKDXsMJCRBsSYLyOw U7Q== X-Google-Smtp-Source: AGHT+IHzv0Ay1s7/D5RlCraUOAPppGpx+9DOe5Wc2TnAqa2oJae5LaRq2mY8TBHhwH7zU/okRdoTzatzS5o= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:f60c:b0:1f8:6c64:3575 with SMTP id d9443c01a7336-1ff046dcdffmr290425ad.0.1722038040140; Fri, 26 Jul 2024 16:54:00 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:49 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-41-seanjc@google.com> Subject: [PATCH v12 40/84] KVM: x86/mmu: Add helper to "finish" handling a guest page fault From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165401_386449_27050374 X-CRM114-Status: GOOD ( 12.21 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a helper to finish/complete the handling of a guest page, e.g. to mark the pages accessed and put any held references. In the near future, this will allow improving the logic without having to copy+paste changes into all page fault paths. And in the less near future, will allow sharing the "finish" API across all architectures. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 12 +++++++++--- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index e76f64f55c4a..1cdd67707461 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4311,6 +4311,12 @@ static u8 kvm_max_private_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, return req_max_level; } +static void kvm_mmu_finish_page_fault(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault, int r) +{ + kvm_release_pfn_clean(fault->pfn); +} + static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { @@ -4476,7 +4482,7 @@ static int kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, * mmu_lock is acquired. */ if (mmu_invalidate_retry_gfn_unsafe(vcpu->kvm, fault->mmu_seq, fault->gfn)) { - kvm_release_pfn_clean(fault->pfn); + kvm_mmu_finish_page_fault(vcpu, fault, RET_PF_RETRY); return RET_PF_RETRY; } @@ -4552,8 +4558,8 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault r = direct_map(vcpu, fault); out_unlock: + kvm_mmu_finish_page_fault(vcpu, fault, r); write_unlock(&vcpu->kvm->mmu_lock); - kvm_release_pfn_clean(fault->pfn); return r; } @@ -4641,8 +4647,8 @@ static int kvm_tdp_mmu_page_fault(struct kvm_vcpu *vcpu, r = kvm_tdp_mmu_map(vcpu, fault); out_unlock: + kvm_mmu_finish_page_fault(vcpu, fault, r); read_unlock(&vcpu->kvm->mmu_lock); - kvm_release_pfn_clean(fault->pfn); return r; } #endif diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index e1c2f098d9d5..b6897916c76b 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -835,8 +835,8 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault r = FNAME(fetch)(vcpu, fault, &walker); out_unlock: + kvm_mmu_finish_page_fault(vcpu, fault, r); write_unlock(&vcpu->kvm->mmu_lock); - kvm_release_pfn_clean(fault->pfn); return r; } From patchwork Fri Jul 26 23:51:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743492 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 07AE2C3DA7F for ; Sat, 27 Jul 2024 00:10:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=9zY2YEu1HUtjdcDr5plCoNlYuLGLbh4OhGnITp611eA=; b=z8l20+AoD2qhZtxkVvmL9R/WLx beXUm3ivQ8SIKBY+/pATb6zhiZsGHXLyZdT0NXrZZ74cUsNzUbcphmwUCgK3jHnAvb9SKtan/46Hk /BvOExpNi0PNHdXSynNd3AOqcj0U3AoqRq7YpHNGC8V54M4nGbO8jBevqT/HY9yLu9+c+Vr6c9E25 8m7xA778f0iQDDBpgz5D6XoivdYPhoGLhbAx0PM4jVGmr+jhaaCggtj4A7AGnf4nA682+mce+gcee 8fv96d//bIJfiT+a0oZY3237LDJH/nIjYyGe56KIPYT+tXDFe08HblPBuZs8UPYHhl3HjT4KJSGkv cGkp0WzA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV0x-00000005ZLh-3MEx; Sat, 27 Jul 2024 00:10:23 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUl9-00000005QGR-2s5x for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:05 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70ec1039600so1046613b3a.0 for ; Fri, 26 Jul 2024 16:54:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038042; x=1722642842; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=9zY2YEu1HUtjdcDr5plCoNlYuLGLbh4OhGnITp611eA=; b=lCii9c1JIOtwqfdmo8tdvPuGAIAdWHVvMRLwHoBu4XIiWl/gSKD5LI9zcvNQUZ23eQ Ty0jDPMFDtI6pZOwd2F3igE4Ct/6xE7BwTqVd5jl6YNahxjUBZu7Vojpsh6pRVfhe6ux 5jAw+C+hakxjACPzjC8TkRNAT97l/ROSYMXa6zGjbtxg5mf2t1iDskvNPLIc6A9RkwTq x5xjbS6n6JEEXYlM5D7XWLbFF+eYvgizIHiCopONIQ6e+d2aKmQ5pmNIF2GZlsLdFlWl d9Au4uucGQievHr2guERMEnwtvrGbnNx1SSo223a+hUWXfSS+xQ94/Xh8F/DpmPhgm8b lJXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038042; x=1722642842; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9zY2YEu1HUtjdcDr5plCoNlYuLGLbh4OhGnITp611eA=; b=s4uCiVftq0+gyGJh8mB3f4jRi+DrZXfXEQTHl/XkfIndPgD1YKMHx6zJZRNkzsg2mm ruQ2W4ob5OnW1uT8P3wfuLL/sekxmMYfz9lB3w2SHremYp6VnMlTfB82JHD9rRTwEdjS uu3j7BSXcSzdrqPzIjRMk9K9bE8Lzw2/qVAVdX4chVlkvF28Ss2FiNtDvxpuqCERQ1se aGpvqY2hBauHCFT4e4nvTxGvLsJ6Ac07H2lj0L2rpRyt7RFuL2T3AXcXnBzpdRyAgica MBkNZ7dXk17Gcr/aYs7QIg07JQ4xdXYv0OkgmVi1F0DBExh3CtzyyqZHnExZaz3TG8il qSDg== X-Forwarded-Encrypted: i=1; AJvYcCVqA22WFgQazFrByZo2BvY8m9XO25JYLXrb7bNKeL/56BCJjnC142uTmKwanoblpxeqkH0e/K1D6eisx5VJwXt3M2ROZng24xmz8dThWhJtVbYDOO8= X-Gm-Message-State: AOJu0YxW0pLLQ6+lS8VjF0Dm1gvbyY5JwVmQQ3jr5tcbZZ17bldmnWC0 iuxXNwVHk+DPwH3Ko3wBh6a2FOzl+LLvr2fOrduSNMKBw9OxyZm5KTig5ausVARmGrKlczUZmkQ wUw== X-Google-Smtp-Source: AGHT+IFj2ro53tB8A9NyZ1GYVrgqdx/ajbf6jMsbjIk3q7R62qDhpHqlToArJZlcHrNz2kHOoBaEKLt0+7c= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:66e5:b0:70d:27ca:96b8 with SMTP id d2e1a72fcca58-70ece926ad1mr25418b3a.0.1722038041966; Fri, 26 Jul 2024 16:54:01 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:50 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-42-seanjc@google.com> Subject: [PATCH v12 41/84] KVM: x86/mmu: Mark pages/folios dirty at the origin of make_spte() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165403_862807_277FD148 X-CRM114-Status: GOOD ( 18.38 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Move the marking of folios dirty from make_spte() out to its callers, which have access to the _struct page_, not just the underlying pfn. Once all architectures follow suit, this will allow removing KVM's ugly hack where KVM elevates the refcount of VM_MIXEDMAP pfns that happen to be struct page memory. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 29 +++++++++++++++++++++++++++-- arch/x86/kvm/mmu/paging_tmpl.h | 5 +++++ arch/x86/kvm/mmu/spte.c | 11 ----------- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 1cdd67707461..7e7b855ce1e1 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -2918,7 +2918,16 @@ static bool kvm_mmu_prefetch_sptes(struct kvm_vcpu *vcpu, gfn_t gfn, u64 *sptep, for (i = 0; i < nr_pages; i++, gfn++, sptep++) { mmu_set_spte(vcpu, slot, sptep, access, gfn, page_to_pfn(pages[i]), NULL); - kvm_release_page_clean(pages[i]); + + /* + * KVM always prefetches writable pages from the primary MMU, + * and KVM can make its SPTE writable in the fast page, without + * notifying the primary MMU. Mark pages/folios dirty now to + * ensure file data is written back if it ends up being written + * by the guest. Because KVM's prefetching GUPs writable PTEs, + * the probability of unnecessary writeback is extremely low. + */ + kvm_release_page_dirty(pages[i]); } return true; @@ -4314,7 +4323,23 @@ static u8 kvm_max_private_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, static void kvm_mmu_finish_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, int r) { - kvm_release_pfn_clean(fault->pfn); + lockdep_assert_once(lockdep_is_held(&vcpu->kvm->mmu_lock) || + r == RET_PF_RETRY); + + /* + * If the page that KVM got from the *primary MMU* is writable, and KVM + * installed or reused a SPTE, mark the page/folio dirty. Note, this + * may mark a folio dirty even if KVM created a read-only SPTE, e.g. if + * the GFN is write-protected. Folios can't be safely marked dirty + * outside of mmu_lock as doing so could race with writeback on the + * folio. As a result, KVM can't mark folios dirty in the fast page + * fault handler, and so KVM must (somewhat) speculatively mark the + * folio dirty if KVM could locklessly make the SPTE writable. + */ + if (!fault->map_writable || r == RET_PF_RETRY) + kvm_release_pfn_clean(fault->pfn); + else + kvm_release_pfn_dirty(fault->pfn); } static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu, diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index b6897916c76b..2e2d87a925ac 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -953,6 +953,11 @@ static int FNAME(sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int spte_to_pfn(spte), spte, true, false, host_writable, &spte); + /* + * There is no need to mark the pfn dirty, as the new protections must + * be a subset of the old protections, i.e. synchronizing a SPTE cannot + * change the SPTE from read-only to writable. + */ return mmu_spte_update(sptep, spte); } diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c index 9b8795bd2f04..2c5650390d3b 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -277,17 +277,6 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, mark_page_dirty_in_slot(vcpu->kvm, slot, gfn); } - /* - * If the page that KVM got from the primary MMU is writable, i.e. if - * it's host-writable, mark the page/folio dirty. As alluded to above, - * folios can't be safely marked dirty in the fast page fault handler, - * and so KVM must (somewhat) speculatively mark the folio dirty even - * though it isn't guaranteed to be written as KVM won't mark the folio - * dirty if/when the SPTE is made writable. - */ - if (host_writable) - kvm_set_pfn_dirty(pfn); - *new_spte = spte; return wrprot; } From patchwork Fri Jul 26 23:51:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743493 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3C94FC3DA49 for ; Sat, 27 Jul 2024 00:11:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=66tkjZePX8REZ4BHWwuGWSSOzSubgrfSmhtIFHX3PRM=; b=qaEHQSUdExEPHG76SfOd10NGhw xn3cSddN0XLP/2Yq1iNkoBgBA/37jotF0RXm+lDRVzXjTxiBDpy02ibWnwSPuR51p0nNYhZpRIK7L 3LppHOWudqPb+TN/io4a37kY5hxvseaak78lHb1aKBI+HGpNv/J6GtIf5b8E/LIC0FxpjOAfJWGPp v2jJaAPAtPe1LsIsLvn0zFF6TAG1kNlR17RgofsGdwy3ZbG3oV8a8qfMugqKhqEgRwCErNpU4JOEK bfmK7uuUbd701vAh5cn6F6SQuwMl+auBH/CD+yZIP1x4q1tuAI0nraWBCFv04DMIRfhURIaRSWs+5 HRgG2PWw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV1Q-00000005Zbg-2qsG; Sat, 27 Jul 2024 00:10:52 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlB-00000005QHz-1nlL for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:07 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-672bea19bedso6263307b3.3 for ; Fri, 26 Jul 2024 16:54:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038044; x=1722642844; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=66tkjZePX8REZ4BHWwuGWSSOzSubgrfSmhtIFHX3PRM=; b=ju9p12X6+p/wDCj4zrSzx9/mhrb1zDVyOW9r7wxMqRb8NYmjAwtIFiJGGexQL5m4j0 ZIOPgjki2wu4af6yEX8NIcndPzvQE9VDNW9Z7f4BY2LZGpqhsZme6kf4Gona6LJeX7qU uVX3qdmF6zRPTkQ3ylh0O22ITk9LaixbXYH1xMAwQrYRaiRNhnhtT4CO49iIqChF184p aPFXcyQgmoG36OkGV2SU75GHbu9oSLMM7BM6YrMSdflBZSoxR8TwQLLOWv3l116AblTw giH3fB2yzx5rBumzR11mFuF60VCtpPI5VK53DdBSpDjPc898qju+AvC90enREb1a76pt IQKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038044; x=1722642844; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=66tkjZePX8REZ4BHWwuGWSSOzSubgrfSmhtIFHX3PRM=; b=XGwZyGTR/MWFCNBaHWBJzludcNz1mdhO8Z+R+7lPDZbgfyjPN4AqPwZBNK2heJlV5Q uv7ODGi/o+k4LMcjx1nyNBV7VKkBsWikbmaQ/A7ZLp0J8YRpe74stE0ThsaPiyAZkHPi z5XTXs9jmiPrkvzDb2YjUmeES6jZ+De3CP5x6ehMKCdN3lD/5DczmlZYJBI/braS+zQL aIyZeP56rNeAQhbo4X8MpHCrbB34hM4ygcKQNjAbgYVhIVXJrGEZADpCRIQnEniY7NpE XpkwyQ/qYC1lMJEs2I88/tIePT5qdCyYgqx82jxRLgfeRsuInlVvVdkaMDfm1oJvYqBR zVdA== X-Forwarded-Encrypted: i=1; AJvYcCVak5idlG32MAJn2aTvv41PKlyGbBTUBTJKX+dz80euAlNyXeAobJzwLiOOy1SGZDpN6TeWzMw58E3K+g4qvqLc4N45jPUGsATo9id3nNhZvgSPBPw= X-Gm-Message-State: AOJu0Yz87oyn3cA1pW6/T41cnFh6AHGwEppp+iz/oInRAvvhwM0ByWZ9 TyOZNUuC/qjH5ylZ+6fEVDxduUtXqU25hgbN8l6szwMRuLphF7fSDY6140NZ6t15RDDBoI1dBCc Asg== X-Google-Smtp-Source: AGHT+IEXCHhy0parcxZWaUKhAkA9z5ds6aScUvbMwsWWyatCr0tT87bBO0XasR4cX5mU+wgB/x+9BeJQnC8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:f92:b0:673:b39a:92f2 with SMTP id 00721157ae682-67a0a7fd778mr244297b3.7.1722038044118; Fri, 26 Jul 2024 16:54:04 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:51 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-43-seanjc@google.com> Subject: [PATCH v12 42/84] KVM: Move declarations of memslot accessors up in kvm_host.h From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165405_623785_9FDCD03E X-CRM114-Status: UNSURE ( 9.91 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Move the memslot lookup helpers further up in kvm_host.h so that they can be used by inlined "to pfn" wrappers. No functional change intended. Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 5a572cef4adc..ef0277b77375 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1153,6 +1153,10 @@ static inline bool kvm_memslot_iter_is_valid(struct kvm_memslot_iter *iter, gfn_ kvm_memslot_iter_is_valid(iter, end); \ kvm_memslot_iter_next(iter)) +struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn); +struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu); +struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn); + /* * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations: * - create a new memory slot @@ -1290,15 +1294,13 @@ int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc, }) int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len); -struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn); bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn); bool kvm_vcpu_is_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn); unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn); void mark_page_dirty_in_slot(struct kvm *kvm, const struct kvm_memory_slot *memslot, gfn_t gfn); void mark_page_dirty(struct kvm *kvm, gfn_t gfn); -struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu); -struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn); + kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn); int __kvm_vcpu_map(struct kvm_vcpu *vcpu, gpa_t gpa, struct kvm_host_map *map, From patchwork Fri Jul 26 23:51:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743494 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 066B3C3DA4A for ; Sat, 27 Jul 2024 00:11:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=wxuzXAlWAmTLEMJkRQnxVQGU0NLqznOMJ0954FoR3Ro=; b=ZlYbP5eCvJRsJ81bJO5vUR7HGm O/LdkpKbN83vgJtCqSRTtRNZFNl33SKCp6mm4ZT/iT+tQ1R7hgr2XJgWghfaSs8RS2iJK7R4iaZek JeWKFz7fagz9Lzk8nTG/oYl2qzhyiiK8kFgakQGP2PRxYnY0ZsjwL4CLNU65YV+XqX8mBQte9ki/T woPiKpB31Fn9fh41D4IVNM8L/SF1k/8RZHWBnsiEoqR9aY2GXKmqNLbhdJn80JqQ7WLkpOkbPgWW5 iaHsqAYC2BkIaPJRXCueg/gARsW8qJDN/sIAqsf7448bMWr7ztixuxP98ad79Iqzp2UofZdwq6T8k 1GagaTDw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV1s-00000005Zqq-0aDB; Sat, 27 Jul 2024 00:11:20 +0000 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlE-00000005QKi-2K5h for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:09 +0000 Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-7a1188b3bc2so1412752a12.2 for ; Fri, 26 Jul 2024 16:54:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038047; x=1722642847; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=wxuzXAlWAmTLEMJkRQnxVQGU0NLqznOMJ0954FoR3Ro=; b=K3AZZMXbKStUlpEqnDA7Yiq3IrBAUw5pVtr5jVRsmGK4kEdHN/4REBaAbhTiuUc1cu hfUzFB8ky43XTmSxk1A8bCWtTdof58i29wVaFXQlUL7VZEFiVUfEIYBdPS1adFQxVza5 keAyTpXut2Y5IKgJTatpz1m8PjW44DnpoQzdnb3gRvGuEaJh6lteHEEwHlNIs3LxrNVV L98zuBnVI+i4Lm+bKDRw8gO+Mm1deHKGSU4G1I2EpJgoxoOF0fZYPg38cc9ycnFD7peP Hzs7HkIu5oOsR23yh0f1Jm0datpauiY7TRD9jHu65ag5x0fwRvrdsjU/TcIiAgwuTTE2 Jc6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038047; x=1722642847; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=wxuzXAlWAmTLEMJkRQnxVQGU0NLqznOMJ0954FoR3Ro=; b=o3yzAt+litsBxspbECcLr9hgPwelkW2hlyYEaiReHlyhsZtO6vN3u3WgxbwGzLnPNf 3Q2pYKSpIiBM+NnITyn31hlcBjoTjnUSlpqhNrfGNl48tjV4kl9/cbtkOkL0nwLdE7is lHi9erCAJg+9J6twFyc8n2O0EwUNemt3eQOFEUBkIU1T5vUC4+nkST8k3jf6VXtqHIA/ 56sGN56S7hoGGrxiyj+dqFJfrsmz/vrKEItN9te8PTuwgWaMmxEgZSfc7I6wwZ3D0LPx R5nBupFlkz/bDYy1C5sKFi9igHNmAILp/5ZMyGQ37numt2HNHezWoOgwu0TytCbTMIa+ 1nHw== X-Forwarded-Encrypted: i=1; AJvYcCWmlsvHw1n3itp/6PVC1m4Ol41zr6/kYh5HwYWYPKOEhShgqqo0zGpuQHEYa7C8WDpPQQeh7l13T96E2gon0g885ryLzGj7XMMRDRkoLkB/Jo/Yc4w= X-Gm-Message-State: AOJu0YyRmN4ViuqTqXau1j7oVfCBHhAzEgxtdlAEQcjJ4YeLvKDJ3IKy itq9NjiD3a5Qmraamuffck27+G3cL5HF8+q761UrEXJzlC+8K93BmYPYd2SwSnAOWR++ZmZqF6+ 7QA== X-Google-Smtp-Source: AGHT+IHHkDF9jDFipyHQqfPDWUmaQYRR7lKWyDIChzQG4y5VknOXhGJqf1L9qSRAYqKwiuxxpki7u8BCvjE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:141f:0:b0:6e5:ef07:5922 with SMTP id 41be03b00d2f7-7ac8d9d818bmr4158a12.1.1722038046008; Fri, 26 Jul 2024 16:54:06 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:52 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-44-seanjc@google.com> Subject: [PATCH v12 43/84] KVM: Add kvm_faultin_pfn() to specifically service guest page faults From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165408_608854_6AA6DB7E X-CRM114-Status: GOOD ( 13.59 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a new dedicated API, kvm_faultin_pfn(), for servicing guest page faults, i.e. for getting pages/pfns that will be mapped into the guest via an mmu_notifier-protected KVM MMU. Keep struct kvm_follow_pfn buried in internal code, as having __kvm_faultin_pfn() take "out" params is actually cleaner for several architectures, e.g. it allows the caller to have its own "page fault" structure without having to marshal data to/from kvm_follow_pfn. Long term, common KVM would ideally provide a kvm_page_fault structure, a la x86's struct of the same name. But all architectures need to be converted to a common API before that can happen. Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 11 +++++++++++ virt/kvm/kvm_main.c | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index ef0277b77375..e0548ae92659 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1217,6 +1217,17 @@ void kvm_release_page_clean(struct page *page); void kvm_release_page_dirty(struct page *page); kvm_pfn_t kvm_lookup_pfn(struct kvm *kvm, gfn_t gfn); +kvm_pfn_t __kvm_faultin_pfn(const struct kvm_memory_slot *slot, gfn_t gfn, + unsigned int foll, bool *writable, + struct page **refcounted_page); + +static inline kvm_pfn_t kvm_faultin_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, + bool write, bool *writable, + struct page **refcounted_page) +{ + return __kvm_faultin_pfn(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, + write ? FOLL_WRITE : 0, writable, refcounted_page); +} kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn); kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ad84dab8c5dc..6dc448602751 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3134,6 +3134,28 @@ kvm_pfn_t kvm_lookup_pfn(struct kvm *kvm, gfn_t gfn) return pfn; } +kvm_pfn_t __kvm_faultin_pfn(const struct kvm_memory_slot *slot, gfn_t gfn, + unsigned int foll, bool *writable, + struct page **refcounted_page) +{ + struct kvm_follow_pfn kfp = { + .slot = slot, + .gfn = gfn, + .flags = foll, + .map_writable = writable, + .refcounted_page = refcounted_page, + }; + + if (WARN_ON_ONCE(!writable || !refcounted_page)) + return KVM_PFN_ERR_FAULT; + + *writable = false; + *refcounted_page = NULL; + + return kvm_follow_pfn(&kfp); +} +EXPORT_SYMBOL_GPL(__kvm_faultin_pfn); + int kvm_prefetch_pages(struct kvm_memory_slot *slot, gfn_t gfn, struct page **pages, int nr_pages) { From patchwork Fri Jul 26 23:51:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743517 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9CDAAC3DA49 for ; Sat, 27 Jul 2024 00:17:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=kZSI1KNdqk//fOFhZYsFCszCldNWPUNapLOmZmlQ+ic=; b=P7dXHG04NEF/4xYAt8eL/9YJwg cLZHtdWzos84ghCO5kmjqI1yTKtlv+idqvDdHJm1UXI9gM0cdkVH3emAUXKzVGGU5OZUabU5vy+e1 8zd853m14JtGRMorcuTJLVki87uofl26qKmGsgnedr6Kt8LhlGN09HxuIRZWYkfj0TqrdMUWCT3Jl avWwPVZomNyLD9KVCV6skgwsGGCP9SzV0kBHWrln8xQUzQsY+0aJF/8Y224xQmstoWVO5uXTAAFS9 zzXezHSR+xoXuZG39tKmuBUpmfJab/OBbAgMlH+BFjW0/YsVqSMkIgMVpL8VJpOVr/+g6kXyNsSyk WVYNAenw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV7g-00000005cH0-2j7m; Sat, 27 Jul 2024 00:17:20 +0000 Received: from mail-pg1-x549.google.com ([2607:f8b0:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlG-00000005QMN-1WGe for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:12 +0000 Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-7a134fd9261so1530999a12.3 for ; Fri, 26 Jul 2024 16:54:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038049; x=1722642849; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=kZSI1KNdqk//fOFhZYsFCszCldNWPUNapLOmZmlQ+ic=; b=s4wASBRKd6BS3GoU1RdqDdxBDalyKO12lNqDuIYh4MVTNjAuLy36JY84ZLVgdcEGMb YvI4e0Ofx6jKjpuqwTowN8nJ/Ab2zvOh4hk8KjGaCt2ZHMUwHYVbpBxwM3dhOgVPuC/a oai1U9/6QbhjLtBFtQVVhKp9YroEuFbePchwAp+3FWJmVytWvPBVkBTXePTVhwo7PFq7 85qPm5S8VQbcp+nizVjn79vVSXmv3qKwpcOG5APokn/f22Lr8/VryJ7Qfse7pxAWF7LJ 8F+dbCjoZw9VX/Uh5JFVzoFS5DlLpOo9VGO/UwJ67EszDXRqY39U3gd1vhmYJYQEPraG BhfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038049; x=1722642849; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=kZSI1KNdqk//fOFhZYsFCszCldNWPUNapLOmZmlQ+ic=; b=D5VCF6+NaHRdaUIYIhKXwtbtjsu76JFIR9jUWl8y1PFihu75/As2qUX2Gtc5DmTsPp Vz8rN9KaM1jlvZuZj1JguU8SEWqnmZJ1XwPfFdrzdviSd2/5N+eHgraJUYXRyR2KiGbR 63lwVnAszKkbNmvv07pdC/OcLrWoHiMHIaQk8dJtE5v5j0y3YUO0QIc14NzhpQuT4zeT jYomLFWUQ0izvnmmtqDeBebt1OYk/lICpm5pONYnFN1kWV8wY6nfirIh2m54IGjozJTE NL90vqjxiDGAGUVjRWv0GebznW7dCQ1akl90xi2XDx8x3DRmBjvsg4KBfDzXT1ZmPp3o oKkA== X-Forwarded-Encrypted: i=1; AJvYcCUS6xK7aSDUJbezq0Rx/atnxOlxQaOpMgIQlRoiyCdf4EXv10p7jtcdr9/B6pF9mL2F/6lNu8Kgfrs8ZK6F2vcxgkg4USuZkCrq2gFcRwOHVBNEcCM= X-Gm-Message-State: AOJu0YwSjIs1rEVrBAoz5++6xg40V66N78q4m8QohZrb57r49KJqJH+m 6ea6to/TRvpdlZQTOARW686JKBqB27fkEKzMeveyjcIcf7zTn4h+3iSMFqCovAWuajv4RUvCFpk /JA== X-Google-Smtp-Source: AGHT+IHwPJ/UM6h8kXfIVs09j5bz4zlDvOcU2rq/epWSbhMGMPOGMhlwxlAMNu8ktgd9HGr+mJ5fl7Shz2c= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:360a:0:b0:7a1:4462:412e with SMTP id 41be03b00d2f7-7ac8fd30864mr2218a12.9.1722038048851; Fri, 26 Jul 2024 16:54:08 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:53 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-45-seanjc@google.com> Subject: [PATCH v12 44/84] KVM: x86/mmu: Convert page fault paths to kvm_faultin_pfn() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165410_456707_FCD3515E X-CRM114-Status: GOOD ( 14.58 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Convert KVM x86 to use the recently introduced __kvm_faultin_pfn(). Opportunstically capture the refcounted_page grabbed by KVM for use in future changes. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 14 ++++++++++---- arch/x86/kvm/mmu/mmu_internal.h | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 7e7b855ce1e1..53555ea5e5bb 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4369,11 +4369,14 @@ static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu, static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { + unsigned int foll = fault->write ? FOLL_WRITE : 0; + if (fault->is_private) return kvm_mmu_faultin_pfn_private(vcpu, fault); - fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, false, true, - fault->write, &fault->map_writable); + foll |= FOLL_NOWAIT; + fault->pfn = __kvm_faultin_pfn(fault->slot, fault->gfn, foll, + &fault->map_writable, &fault->refcounted_page); /* * If resolving the page failed because I/O is needed to fault-in the @@ -4400,8 +4403,11 @@ static int __kvm_mmu_faultin_pfn(struct kvm_vcpu *vcpu, * to wait for IO. Note, gup always bails if it is unable to quickly * get a page and a fatal signal, i.e. SIGKILL, is pending. */ - fault->pfn = __gfn_to_pfn_memslot(fault->slot, fault->gfn, true, true, - fault->write, &fault->map_writable); + foll |= FOLL_INTERRUPTIBLE; + foll &= ~FOLL_NOWAIT; + fault->pfn = __kvm_faultin_pfn(fault->slot, fault->gfn, foll, + &fault->map_writable, &fault->refcounted_page); + return RET_PF_CONTINUE; } diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index a5113347bb12..e1f8385105a5 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -238,6 +238,7 @@ struct kvm_page_fault { /* Outputs of kvm_mmu_faultin_pfn(). */ unsigned long mmu_seq; kvm_pfn_t pfn; + struct page *refcounted_page; bool map_writable; /* From patchwork Fri Jul 26 23:51:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743523 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B16E8C3DA4A for ; Sat, 27 Jul 2024 00:18:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=RjeQeusXYf+NFoksbIMp6SJLG4xl7UVW4NR1eYCui2c=; b=xYwCwwDDBhdPyyqlsOy+4VOkHr bxDldTTe3CP8ZwLE1RGC7ejpmlKuvvB6yW3lK8MFc7HB2JBs+MmeEyccXrR/acCNDIGaB6RhCkQvg fkwZnH9UM828pqwgiuiG7vSdVl16ScwgqnprtjidU6CcsB9DIAExA1ZQ6QVefXiUNEJLyREXWh1uL ZgnZY8iJV5Fira5XYYnc+N0mUuEqJampFB5xRUEXoONtM62r0Fg8ofxAvu0mbPcRt98fdAc8W0v22 n+dWQ3wKnhJugXUrhKUeeBxUvLmB50iUlFZU7tLLtWm0/1slmkdRlc+8/e8Ox9x0AOK86DSDmotLa TYYIIBBA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV89-00000005cYH-2h7Z; Sat, 27 Jul 2024 00:17:49 +0000 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlI-00000005QNl-0wrl for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:17 +0000 Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1fc52d3c76eso12299615ad.3 for ; Fri, 26 Jul 2024 16:54:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038051; x=1722642851; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=RjeQeusXYf+NFoksbIMp6SJLG4xl7UVW4NR1eYCui2c=; b=D6x+/kPwbuj6kN3vq1dXXmy8agRQNfx+O9qk9E+4qTWtpoivA9Q1xKx7nnQIa3kr4W hrtFWzyAa6gXIwGGsoNlFDsbEQJg0y7D7Pl8kYwWfOb7Ab0r3T1s/Mmt5O0YWSdYCcgo a6f6qobdasfL/bbhlgdinfGsrxplkIWqEz0Plz/ksxhiijZ6SwM11NQfnpT4lxFjl0b2 Xbfj/gNNeNNNhHkHvKzad41zgmmFg/pfLSbD8C+8hB9+jiJPycjIyOZfxddsRSdONRxS kKghEvpyTERLU2UTSePykaJhXls3B3jOFUOhg351U6kWx60bu9DEvSx9B/aKwHfSMG84 TeJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038051; x=1722642851; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=RjeQeusXYf+NFoksbIMp6SJLG4xl7UVW4NR1eYCui2c=; b=AOc9XekubweWJ4MXRf+GVhx5CPACTnQ3KGm5z0k/v1Ms7H7lqj09oj58N+lJ8HlYhG w0mRbOsgNMivZPv3i4gBsBeq56vp/48RUMissPZrwirLtQtNce9IrTxHXAA6RkvK9cbx JhgeU9w4pb7zlJWdCrVn09BBi5tPlaBZU/QHtJYXK5xEaofO7tS7fgQfHD3yl726eDN8 x9ZzrPpp8haiHFIu40WPCJt8bi7UtvoWMLad4P+TQviF4CUyHZsOr645Ro8JqkJQzZq7 7rYkexO+tbgtoSKszx7mbfQrFr9o8gzGQGqwYxIAGcD1K/pDmdsbqPpWJYCTjSUD84lh I5Vw== X-Forwarded-Encrypted: i=1; AJvYcCVhNhS7cEPvyEVqPFbRlMZChtyMwvTvXUq0CC3iviHq56bMQV5xTxtYAUikEWeKTdGrXm+TBot80CAIc3z+1+lQoGa6/YonFB67nvC7M+c8BlTP2yI= X-Gm-Message-State: AOJu0YxjGrEhh778ZRXIbqqpI4ubD+aIKWXX4YlzFtXmKAYAaxDvD4d6 WiCbRxNODrQxh2ZxCW/Jqq7E4rtBE2QLaTEB0C51z5jXKpJxO3t+5NcMBDMvUNHA6DLGbMj2Rmq EYA== X-Google-Smtp-Source: AGHT+IH9Sleax7HjGR/xKBhc5+sBB7ouHNPkUkQToWM2on12s7r2/JIy8m8cUdV6C+LgY7aIUstVbYLK+Ts= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:244b:b0:1fb:82f5:6631 with SMTP id d9443c01a7336-1ff04898c7dmr609255ad.9.1722038050968; Fri, 26 Jul 2024 16:54:10 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:54 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-46-seanjc@google.com> Subject: [PATCH v12 45/84] KVM: guest_memfd: Provide "struct page" as output from kvm_gmem_get_pfn() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165412_406475_BD444BCD X-CRM114-Status: GOOD ( 16.69 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Provide the "struct page" associated with a guest_memfd pfn as an output from __kvm_gmem_get_pfn() so that KVM guest page fault handlers can directly put the page instead of having to rely on kvm_pfn_to_refcounted_page(). Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/svm/sev.c | 10 ++++++---- include/linux/kvm_host.h | 6 ++++-- virt/kvm/guest_memfd.c | 19 +++++++++++-------- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 53555ea5e5bb..146e57c9c86d 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4353,7 +4353,7 @@ static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu, } r = kvm_gmem_get_pfn(vcpu->kvm, fault->slot, fault->gfn, &fault->pfn, - &max_order); + &fault->refcounted_page, &max_order); if (r) { kvm_mmu_prepare_memory_fault_exit(vcpu, fault); return r; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 62f63fd714df..5c125e4c1096 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3847,6 +3847,7 @@ static int __sev_snp_update_protected_guest_state(struct kvm_vcpu *vcpu) if (VALID_PAGE(svm->sev_es.snp_vmsa_gpa)) { gfn_t gfn = gpa_to_gfn(svm->sev_es.snp_vmsa_gpa); struct kvm_memory_slot *slot; + struct page *page; kvm_pfn_t pfn; slot = gfn_to_memslot(vcpu->kvm, gfn); @@ -3857,7 +3858,7 @@ static int __sev_snp_update_protected_guest_state(struct kvm_vcpu *vcpu) * The new VMSA will be private memory guest memory, so * retrieve the PFN from the gmem backend. */ - if (kvm_gmem_get_pfn(vcpu->kvm, slot, gfn, &pfn, NULL)) + if (kvm_gmem_get_pfn(vcpu->kvm, slot, gfn, &pfn, &page, NULL)) return -EINVAL; /* @@ -3886,7 +3887,7 @@ static int __sev_snp_update_protected_guest_state(struct kvm_vcpu *vcpu) * changes then care should be taken to ensure * svm->sev_es.vmsa is pinned through some other means. */ - kvm_release_pfn_clean(pfn); + kvm_release_page_clean(page); } /* @@ -4686,6 +4687,7 @@ void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) struct kvm_memory_slot *slot; struct kvm *kvm = vcpu->kvm; int order, rmp_level, ret; + struct page *page; bool assigned; kvm_pfn_t pfn; gfn_t gfn; @@ -4712,7 +4714,7 @@ void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) return; } - ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &order); + ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &page, &order); if (ret) { pr_warn_ratelimited("SEV: Unexpected RMP fault, no backing page for private GPA 0x%llx\n", gpa); @@ -4770,7 +4772,7 @@ void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) out: trace_kvm_rmp_fault(vcpu, gpa, pfn, error_code, rmp_level, ret); out_no_trace: - put_page(pfn_to_page(pfn)); + kvm_release_page_unused(page); } static bool is_pfn_range_shared(kvm_pfn_t start, kvm_pfn_t end) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index e0548ae92659..9d2a97eb30e4 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2462,11 +2462,13 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) #ifdef CONFIG_KVM_PRIVATE_MEM int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, - gfn_t gfn, kvm_pfn_t *pfn, int *max_order); + gfn_t gfn, kvm_pfn_t *pfn, struct page **page, + int *max_order); #else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, - kvm_pfn_t *pfn, int *max_order) + kvm_pfn_t *pfn, struct page **page, + int *max_order) { KVM_BUG_ON(1, kvm); return -EIO; diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 1c509c351261..ad1f9e73cd13 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -542,12 +542,12 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) } static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, - gfn_t gfn, kvm_pfn_t *pfn, int *max_order, bool prepare) + gfn_t gfn, kvm_pfn_t *pfn, struct page **page, + int *max_order, bool prepare) { pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; struct kvm_gmem *gmem = file->private_data; struct folio *folio; - struct page *page; int r; if (file != slot->gmem.file) { @@ -571,9 +571,9 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, return -EHWPOISON; } - page = folio_file_page(folio, index); + *page = folio_file_page(folio, index); - *pfn = page_to_pfn(page); + *pfn = page_to_pfn(*page); if (max_order) *max_order = 0; @@ -585,7 +585,8 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, } int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, - gfn_t gfn, kvm_pfn_t *pfn, int *max_order) + gfn_t gfn, kvm_pfn_t *pfn, struct page **page, + int *max_order) { struct file *file = kvm_gmem_get_file(slot); int r; @@ -593,7 +594,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, if (!file) return -EFAULT; - r = __kvm_gmem_get_pfn(file, slot, gfn, pfn, max_order, true); + r = __kvm_gmem_get_pfn(file, slot, gfn, pfn, page, max_order, true); fput(file); return r; } @@ -604,6 +605,7 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long { struct file *file; struct kvm_memory_slot *slot; + struct page *page; void __user *p; int ret = 0, max_order; @@ -633,7 +635,8 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long break; } - ret = __kvm_gmem_get_pfn(file, slot, gfn, &pfn, &max_order, false); + ret = __kvm_gmem_get_pfn(file, slot, gfn, &pfn, &page, + &max_order, false); if (ret) break; @@ -644,7 +647,7 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long p = src ? src + i * PAGE_SIZE : NULL; ret = post_populate(kvm, gfn, pfn, p, max_order, opaque); - put_page(pfn_to_page(pfn)); + put_page(page); if (ret) break; } From patchwork Fri Jul 26 23:51:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743524 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C7B35C3DA49 for ; Sat, 27 Jul 2024 00:18:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=IbuNIltU7tfSlkccHv78LPGh6NGiPV3XEW5ZkU/rXdY=; b=0uix67ZxjBUEBlrOO3iioAZ//x wCPlZ3KzrE6QW6EWwCMY7tf8N2KcWHzAdBLQBY7+z0pZ67c+ESjQ0ciT5xiugKRpBmTG8U/0JbCi7 CoCkvOmueYABXOrKQk1tgqT/BFheII82vwforvTCrWMiAA+1b6EDgqNAJhPKjuiFUibNAasJfmkDA L2Imh9Kue6SMAmYBwxNBKhpQgPKxW5opX+flRHM0hdYeq44QM0n8dhYB+InqID2x9grkgcSDaceDk 0/BmVP64E2I3mLT9mniLYfBShYnR2lkNi6V+ijZ6GXvdHApeaC8NXynh1S/AXkQHPrY9aNZj7dnkg tXkBhbFg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV8c-00000005cnl-1iYz; Sat, 27 Jul 2024 00:18:18 +0000 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlK-00000005QPM-1sBl for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:19 +0000 Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1fc5652f7d4so11482875ad.2 for ; Fri, 26 Jul 2024 16:54:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038053; x=1722642853; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=IbuNIltU7tfSlkccHv78LPGh6NGiPV3XEW5ZkU/rXdY=; b=AAQ+rehMPwP1OaP8ajf7vbU3PO1kZAl2spdFSlBxoYr3LbQ+iICjpK3yIoyEGAv8DH q0yDVZVH+xEc92179z3XgUH0JkPGl63jfUxXw9dsr7cCgALDDIjBOVXchYiy5l78JlbH GFAD0HUBjd1GdkVC8bLkvb0kX0hPhAngRamZMTTHs2D2fWeXePqDtbu5Eqy45Yqmi+Lm jee4Fh/8LY3u705ZgLQbPeAe3wUOU/eBgu1hn+qF2gmBKVP9a7/TOha+WPTZ6Nm3QM5m XpbNY0lYrJclSXY4HCrfnAO+B4Op7A/LkRboWsfxxmSj+XYt09W3wfpgTfRnch1YWGAb zLZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038053; x=1722642853; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=IbuNIltU7tfSlkccHv78LPGh6NGiPV3XEW5ZkU/rXdY=; b=vjQkC82dUIxdZ+3G5aowlkUilQ23oH4fJ8YvsWZ0iNHEgdPQz2dFJjXxyXVT/PQBtp kXu2n58eU1ywosoHIxXJKRF91Im3goqNmTwhtAGs4LLzJ47No0dZJ/gFxEimV5PtruYc 1bK/PcIzKcNKD645eMZ9sAR8Weryo0BO7V2ZE3MMste8EJiCv0OOxTrLHbZ4CkxE6zTo 4h+a2iTvzGHzVsePPTZ8niE0HbyvjwPWJtSmnQ/5ib4/v7MZmAHozRBujMKcOglruHOw Aqp29e880W6C9msYxCjlLtZOamn3TlrGR+gQWpiKP6pX5KCVP0eveQ5Cxscu1BPALhaQ MN5Q== X-Forwarded-Encrypted: i=1; AJvYcCU/IHdu93PeIH/YmKgeonPd8s85KwWxJbccfL0dY6YcnFrW1iUjrN4ZMzL4GcynfHq3cpgTYDF/pxzk5HZlJpMY84quPR+qlXKijPBe34zS9hgQhpk= X-Gm-Message-State: AOJu0YzoYm4wmcJ9NFl2HOjE7qNeCjG8K4XF1YOZvvsZ5JdhQt5rtTXN kofEvIMjtZ83POc6T2N606bC3kwTfjE+i5spS69PqAhnFtN6ozMfE4amdQS3Tmcrm3y7R3ZZbvQ LaQ== X-Google-Smtp-Source: AGHT+IF1WA3mVZXk9S93pEq2eXLardKUPi+yfLeicmT6epShCKMtCcCE54tvymeWkFnvdj0Qbejb/rqSUKM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:41cc:b0:1fd:87a7:1445 with SMTP id d9443c01a7336-1ff0489344bmr935735ad.9.1722038053119; Fri, 26 Jul 2024 16:54:13 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:55 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-47-seanjc@google.com> Subject: [PATCH v12 46/84] KVM: x86/mmu: Put refcounted pages instead of blindly releasing pfns From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165414_677701_BE477FC0 X-CRM114-Status: GOOD ( 11.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Now that all x86 page fault paths precisely track refcounted pages, use Use kvm_page_fault.refcounted_page to put references to struct page memory when finishing page faults. This is a baby step towards eliminating kvm_pfn_to_refcounted_page(). Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 146e57c9c86d..3cdb1bd80823 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4326,6 +4326,9 @@ static void kvm_mmu_finish_page_fault(struct kvm_vcpu *vcpu, lockdep_assert_once(lockdep_is_held(&vcpu->kvm->mmu_lock) || r == RET_PF_RETRY); + if (!fault->refcounted_page) + return; + /* * If the page that KVM got from the *primary MMU* is writable, and KVM * installed or reused a SPTE, mark the page/folio dirty. Note, this @@ -4337,9 +4340,9 @@ static void kvm_mmu_finish_page_fault(struct kvm_vcpu *vcpu, * folio dirty if KVM could locklessly make the SPTE writable. */ if (!fault->map_writable || r == RET_PF_RETRY) - kvm_release_pfn_clean(fault->pfn); + kvm_release_page_clean(fault->refcounted_page); else - kvm_release_pfn_dirty(fault->pfn); + kvm_release_page_dirty(fault->refcounted_page); } static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu, From patchwork Fri Jul 26 23:51:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743525 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CE4ADC3DA49 for ; Sat, 27 Jul 2024 00:18:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=gyzdwZRgQhOlsIfYTCemvzcmHdUAJy3wvQe/vDjl5Fg=; b=E7bXL45bxX/Wq56An5J/o3WPov FUJpHqmww7DzhHSzELnUj46We5VginvfvSguiFndyV7rRRlPTlnesgMA01eLaAothnfmPQqItGBam ih/7xvdBrgT9Kn/JoCicwXiXYGuduATyTwxs0SjPUXN2fM2hbRjXfRZ/9WLcstntLQCaf/UyD40K/ YHpN/EKBmvyL7RgWDY9NqlVEUBhXwmIHDnspQnRyZEeadVo0XreSslpDwPZfyIfEOsNHZ4DYFsXEh o9SIhOvICfg86qXV+h383YbIKb73Fv9bVxyfKLZFGU/fB0P9p7EvxpkR1YPxQdkHivJnEk05ZhPZt aXd5aQNQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV94-00000005d2c-40Dx; Sat, 27 Jul 2024 00:18:46 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlM-00000005QR1-2Rpn for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:19 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-66a2aee82a0so6613917b3.0 for ; Fri, 26 Jul 2024 16:54:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038055; x=1722642855; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=gyzdwZRgQhOlsIfYTCemvzcmHdUAJy3wvQe/vDjl5Fg=; b=UP4nL2qMNSlVLcitmVs1V3U24gAiYO0x4JX+StE1g7LxiiQdHwe60Xc5k5VUGMwFaA uHxFMZ8Q6iL1XiGm9jD459N5/BkpyJhfGT92VqHyKk0rOkSPaoA38Xmxengi0wqOmXVd 36ngHFiz8yGFgJdHE4gq+icHoioEq8f/KvijZ2LXDW35nqUsWbndmtpTI+ww6BurOsLv mlqgzdX1XIFfMDqmfXfZ5fVyCaqMZi+V4tYajebxnsAjgyztXxYJwUC4+YNX9tDcUrAU WnV72f16B6k/KKqum/1b0Y9XSMAnd0dbCEH6E4J8Wz0/5xw2Ya8IaMdCKSeIzLjDAKKT bxKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038055; x=1722642855; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gyzdwZRgQhOlsIfYTCemvzcmHdUAJy3wvQe/vDjl5Fg=; b=cRy+8I2Ted6rWYH7j7LJwTAR84ZP940HdAXB0dMTF/niJvi62zMZlcGkouXqw6Gs9T QzjvSgp5x2jB2/YXGL+0HPOTYhB3hdvHuqCws7Faza1Q5J56rnTgs9izUGC21+gjphgL gyJUmo1sj0NwycOE0+fbTCsNX9sC8sxIoVffpYI17up8tH46sEcHhn1pCNmNchVVR7pv OKZF35BJ+fYWmAOaTA7eJ09QK1TMGDFj31J+LWZYZPvIFb7UFvptArRUWZZQwaE4DdQH DhVPhcmKd1DynPUdXP14AIv/0vn7+CE5avY9R1kXexyeC3lK09KcB0o/0n8bIFp1oyg4 /AlQ== X-Forwarded-Encrypted: i=1; AJvYcCVTG2pdeYLyLimwZLENY676t92hWEwAm8Ts5hQflG4a+Z+Z2hKq24SX8opKSVF7XT7diPXWhwBBKj2ylDd31N8+NQ9KyoAvRESq1+GregvS+xUpx38= X-Gm-Message-State: AOJu0Ywi9YPLR4gkRscwExD9ryBLnuD40oMvBAar6GC1znQUBCDfGH0h fSgoRcrirY0WLmMWzzdq5Hwcnn5eMglM5FIOKOEiACHxMwwQuL89bT+rRlQBdoRYaSb0D+U9Lz3 Nsw== X-Google-Smtp-Source: AGHT+IFpeG9OHD4NFko0QV+s2bLKC/mOPhRZ2kdDMQUp9/Z6e7qKWnbHym6UcCLJKsXB8YuZDTrWfSN5tHI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:14:b0:66a:764f:e57f with SMTP id 00721157ae682-67a0abd50e9mr49107b3.7.1722038055008; Fri, 26 Jul 2024 16:54:15 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:56 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-48-seanjc@google.com> Subject: [PATCH v12 47/84] KVM: x86/mmu: Don't mark unused faultin pages as accessed From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165417_003670_D04FB927 X-CRM114-Status: GOOD ( 13.47 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org When finishing guest page faults, don't mark pages as accessed if KVM is resuming the guest _without_ installing a mapping, i.e. if the page isn't being used. While it's possible that marking the page accessed could avoid minor thrashing due to reclaiming a page that the guest is about to access, it's far more likely that the gfn=>pfn mapping was was invalidated, e.g. due a memslot change, or because the corresponding VMA is being modified. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 3cdb1bd80823..95beb50748fc 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4339,7 +4339,9 @@ static void kvm_mmu_finish_page_fault(struct kvm_vcpu *vcpu, * fault handler, and so KVM must (somewhat) speculatively mark the * folio dirty if KVM could locklessly make the SPTE writable. */ - if (!fault->map_writable || r == RET_PF_RETRY) + if (r == RET_PF_RETRY) + kvm_release_page_unused(fault->refcounted_page); + else if (!fault->map_writable) kvm_release_page_clean(fault->refcounted_page); else kvm_release_page_dirty(fault->refcounted_page); From patchwork Fri Jul 26 23:51:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743526 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E2CBEC3DA49 for ; Sat, 27 Jul 2024 00:19:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=l9BW/dK5c/4ryaOpC0RxFiUBYVnD4MPO1UqGokFlyv8=; b=rcov5HuXjMNjCVlavImFJ2PbNw 08aDljVHm1j8xnnG5Mierexi5ZxM2t54Oy0O2ZWLruhHWvgytENMwP6Ifv+1OM7N02ovKvtInIUYG oYqC2FzgRzy8aDfF2LPVUc5Bg/SeHvuGDEYRZlxayo7MzRIs8PPXZVDr7hW5Xy/Hi1lDYRNSN5Eby 7lXgx2W/2WayLVXCAdv5Rc+nKI6WU3+be/fJvR0lkmc045qIVljlds1D7M7SWiE2SmJrwjRe215h6 5Am3oBmIQfSDinZV6HtExuTNIVCbEEHbmQxolV6OJo8LoIzz/m2py02atRA3cO+87gtyem+iMTjoy Hcu5p1vQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV9U-00000005dG6-3y9T; Sat, 27 Jul 2024 00:19:13 +0000 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlO-00000005QTT-21k1 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:23 +0000 Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1fd9a0efe4eso11319185ad.0 for ; Fri, 26 Jul 2024 16:54:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038057; x=1722642857; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=l9BW/dK5c/4ryaOpC0RxFiUBYVnD4MPO1UqGokFlyv8=; b=rwltHS3Fz9Iauzuo3pJpgwu/pOrEGW1j/bhMwzhBdfd15DWg3bSOarD5elcMoJuls7 22FUF4qXnsJKLDFzOVNue7CQXZKpD6rmQdhGpDUrLe3GrM3gVgT7xEtPT9YteNuuJHNm JkrYtNXVXnyyz5d6N9i+HrKT3lgZCVGQe1m1TzGHbbF5ZHEJAc6PqOHxoygAE55vRN6u J27B/M8Ud30wFON9izgtx9XJWYSArcqVnx1hmzqg1pXhJKIr31hcDLcASguw4Ug1kiFZ ynpWpOZ4QPREdiSsXVfQKMEvjuxWsWupPA0u5ceimx7eqkiNqCNn/Ze5UljiXjsg9Uyh NgiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038057; x=1722642857; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=l9BW/dK5c/4ryaOpC0RxFiUBYVnD4MPO1UqGokFlyv8=; b=R6jjY+A/el6rMZX1ELmDRLKeMXjyxFXuKuYfS650Kjkqc3Q+vgY/1WiiEl5t55l4oi 22Zezw60QHWTYPnxvFcKLgrZGvo+WUx1qZGzDRgwRxF33jRl/WChW35JtXMPu8fKYsbN PhAdfyAP9H24M20mer1djMu/adzXhk9DBxhK75DpY0DYq1vYbjopuswSlQO6UfbyYO2P iDyhq7cV3hXjRtCuUW2LDq5oJUSBlD4TOqIUdTBbi2tWdgbmKBdNYxhmZa5Mwh7JPEwG qbmEnkdwbaSAx8YbJxO1uzZhtocwfOnDK6w7u/F1wUw5oFpPj5mZ5Gcze7djKQFFhOSO jGVw== X-Forwarded-Encrypted: i=1; AJvYcCWoCxm0B/60uOzINZvvZqto07kn9Isafi0Q/u9FTPzQZjKCK/mpEZ4Aq9zBuGcWApdOnJNAEbaCsDfo60Jbf2a6G7HA2Xt06ypvcp59wl+Ri2yyPwQ= X-Gm-Message-State: AOJu0YxymdOw/fXr+nxv5LixvFPZo/w/v7I/xXXVSIb/lErIFO6BqHcp wYcMVPSLI467tG71qv2Q3t7oFefJnl/HTedeKwOjwv8n8Gb2Qixf0KeEiBp6/+FiKOGrfzy7vu8 apg== X-Google-Smtp-Source: AGHT+IH3COIaUp2rXSDrmiVRXNOzjzCRiof8RV2PU1lQ3pUgA2jXVKIIsmrd9aJoYDTjtMdA3IOsVNqBhcc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ea06:b0:1f9:cbe5:e422 with SMTP id d9443c01a7336-1ff0488dbf2mr744395ad.8.1722038056933; Fri, 26 Jul 2024 16:54:16 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:57 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-49-seanjc@google.com> Subject: [PATCH v12 48/84] KVM: Move x86's API to release a faultin page to common KVM From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165418_659035_413B9119 X-CRM114-Status: GOOD ( 16.61 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Move KVM x86's helper that "finishes" the faultin process to common KVM so that the logic can be shared across all architectures. Note, not all architectures implement a fast page fault path, but the gist of the comment applies to all architectures. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 24 ++---------------------- include/linux/kvm_host.h | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 95beb50748fc..2a0cfa225c8d 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4323,28 +4323,8 @@ static u8 kvm_max_private_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, static void kvm_mmu_finish_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, int r) { - lockdep_assert_once(lockdep_is_held(&vcpu->kvm->mmu_lock) || - r == RET_PF_RETRY); - - if (!fault->refcounted_page) - return; - - /* - * If the page that KVM got from the *primary MMU* is writable, and KVM - * installed or reused a SPTE, mark the page/folio dirty. Note, this - * may mark a folio dirty even if KVM created a read-only SPTE, e.g. if - * the GFN is write-protected. Folios can't be safely marked dirty - * outside of mmu_lock as doing so could race with writeback on the - * folio. As a result, KVM can't mark folios dirty in the fast page - * fault handler, and so KVM must (somewhat) speculatively mark the - * folio dirty if KVM could locklessly make the SPTE writable. - */ - if (r == RET_PF_RETRY) - kvm_release_page_unused(fault->refcounted_page); - else if (!fault->map_writable) - kvm_release_page_clean(fault->refcounted_page); - else - kvm_release_page_dirty(fault->refcounted_page); + kvm_release_faultin_page(vcpu->kvm, fault->refcounted_page, + r == RET_PF_RETRY, fault->map_writable); } static int kvm_mmu_faultin_pfn_private(struct kvm_vcpu *vcpu, diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 9d2a97eb30e4..91341cdc6562 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1216,6 +1216,32 @@ static inline void kvm_release_page_unused(struct page *page) void kvm_release_page_clean(struct page *page); void kvm_release_page_dirty(struct page *page); +static inline void kvm_release_faultin_page(struct kvm *kvm, struct page *page, + bool unused, bool dirty) +{ + lockdep_assert_once(lockdep_is_held(&kvm->mmu_lock) || unused); + + if (!page) + return; + + /* + * If the page that KVM got from the *primary MMU* is writable, and KVM + * installed or reused a SPTE, mark the page/folio dirty. Note, this + * may mark a folio dirty even if KVM created a read-only SPTE, e.g. if + * the GFN is write-protected. Folios can't be safely marked dirty + * outside of mmu_lock as doing so could race with writeback on the + * folio. As a result, KVM can't mark folios dirty in the fast page + * fault handler, and so KVM must (somewhat) speculatively mark the + * folio dirty if KVM could locklessly make the SPTE writable. + */ + if (unused) + kvm_release_page_unused(page); + else if (dirty) + kvm_release_page_dirty(page); + else + kvm_release_page_clean(page); +} + kvm_pfn_t kvm_lookup_pfn(struct kvm *kvm, gfn_t gfn); kvm_pfn_t __kvm_faultin_pfn(const struct kvm_memory_slot *slot, gfn_t gfn, unsigned int foll, bool *writable, From patchwork Fri Jul 26 23:51:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743527 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 74BA3C3DA49 for ; Sat, 27 Jul 2024 00:19:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=TqUPaGEc7V7k122pX7jZuWDsKBXmCX3Y3KCoLsEzljY=; b=jF90Zczp8Ooz0C9kDjFJpp7L/j yimT4B0xEWHgoaX4fl1Orh6QSEE10xOJIRvBFRhsVSV7lGFdWdXjRqYKiJvzBFCxZJlcOBtbKdk4f Z9/qgUNp/pIn/Hze8ZAFWr7YLjhIpKrcrgjst5WJVDGmIb2G46f7NP6fm9nm4zpZ4ZBumxTiO/u/Z GXRtNFbypL3whBQG6ENJS1DYXuzu2RLDkZgcAXo3zOJgNYpTcfozObBWem/hu75OEKtCK4q1HW/lJ ECUMXDH9Q3hddSS2uYRui8Ncu9pcB6diahc1MRJs2q8kVonp90C+Jkr/M+TZZ29Iyh9vX3b71XPby AKVOtqjw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV9w-00000005dUi-2Q37; Sat, 27 Jul 2024 00:19:40 +0000 Received: from mail-pj1-x1049.google.com ([2607:f8b0:4864:20::1049]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlP-00000005QVH-3Z8p for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:25 +0000 Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2cb696be198so1638101a91.3 for ; Fri, 26 Jul 2024 16:54:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038059; x=1722642859; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=TqUPaGEc7V7k122pX7jZuWDsKBXmCX3Y3KCoLsEzljY=; b=xqt6MwrjwiG6Jl+edJpYIKVxwh2eVe43CG086lQKI580H90HRUv0lj8vBbu5bbCCZq 9+vN4U5/5t+X5VCZMfImh5UwCCCOFP80XxNkXd9MD03+LfrM381W855UWTJ3ec7e5cNN BpSVT8UXDflXmVB3dZm03eSmhFTrM7I8X9azctBc90W4TA/rFhkgsN1rNmBpFFAwujon 4PFXd63IAym6fyPKqbr6i/sBxeKjPJMxkwKNZw40fyl1EplZ/Uhh3SINAlG/StjMTt+c sGE/IETuFarpv0/oVtyzkiFiGQyJCS3pgDXVPlYrHm0Gw/ZqUli1j4Bs+oAFx4FQvXpi cATw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038059; x=1722642859; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TqUPaGEc7V7k122pX7jZuWDsKBXmCX3Y3KCoLsEzljY=; b=bPcQmbU1ckMsz8YKAKLt2A/CAJUUgt24ZlVjP5r6h6TAoeWRJcXcglnnHr1dv3KygH HIo4MwktSHjj/iKvKb2DbrR2KOf8+G0YYQCXpd+G8q/hMgfoFw7jigsK5IpUY2a9jGS+ nMN+Z2VfxVHDr2LNjy6SCPvOChIZsLo8YHs4I3Hhy7uPKSIKghllcVoBKxUSUnmX3W1n ga3cW2q1VE0jggmIm9/edGS2Kl1t3RPelmk8qR2uJox921lMO7vlAVz07Y6R7BthGI0n F98Y9x9kypTW9Sz1NSPuoM379sFegim56VYYCLfni76zxjmmX1eRT5WFrSTQ16dJ5uGX i5QQ== X-Forwarded-Encrypted: i=1; AJvYcCVVPop3EYgjh21HYMThzOo2gDeSvKTioz98aHk1Cn5o3HR7CTOiylPQzpDLJ2U5W4prT4eCy/xDn7FnJKdztbd8veB018a7jdDqrWTLHcMfBigRnFI= X-Gm-Message-State: AOJu0YzMmC/R2lMrYo8wBjhE73C5U0gCVivF83oxm23uuJ4+ZW/7T+Sh +1kG923PJS4WJn9CxxY+Tej3OVHc6fZXHBz7h+UUKrpMVVMNK0YqlP4wVeoWPSMZ7nE99310ZUQ 9lA== X-Google-Smtp-Source: AGHT+IH7R2Sq6ws3raxo/M+HjUZh8HrSGcUFKZNBVKBew2cgXvpkeEwYajfUFO1jgdCm33mambqvR7vew2A= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:19c4:b0:2c8:8288:1f3c with SMTP id 98e67ed59e1d1-2cf7e08defcmr21966a91.1.1722038058548; Fri, 26 Jul 2024 16:54:18 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:58 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-50-seanjc@google.com> Subject: [PATCH v12 49/84] KVM: VMX: Hold mmu_lock until page is released when updating APIC access page From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165420_087426_6A049A92 X-CRM114-Status: GOOD ( 14.11 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hold mmu_lock across kvm_release_pfn_clean() when refreshing the APIC access page address to ensure that KVM doesn't mark a page/folio as accessed after it has been unmapped. Practically speaking marking a folio accesses is benign in this scenario, as KVM does hold a reference (it's really just marking folios dirty that is problematic), but there's no reason not to be paranoid (moving the APIC access page isn't a hot path), and no reason to be different from other mmu_notifier-protected flows in KVM. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index f18c2d8c7476..30032585f7dc 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6828,25 +6828,22 @@ void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) return; read_lock(&vcpu->kvm->mmu_lock); - if (mmu_invalidate_retry_gfn(kvm, mmu_seq, gfn)) { + if (mmu_invalidate_retry_gfn(kvm, mmu_seq, gfn)) kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); - read_unlock(&vcpu->kvm->mmu_lock); - goto out; - } + else + vmcs_write64(APIC_ACCESS_ADDR, pfn_to_hpa(pfn)); - vmcs_write64(APIC_ACCESS_ADDR, pfn_to_hpa(pfn)); - read_unlock(&vcpu->kvm->mmu_lock); - - /* - * No need for a manual TLB flush at this point, KVM has already done a - * flush if there were SPTEs pointing at the previous page. - */ -out: /* * Do not pin apic access page in memory, the MMU notifier * will call us again if it is migrated or swapped out. */ kvm_release_pfn_clean(pfn); + + /* + * No need for a manual TLB flush at this point, KVM has already done a + * flush if there were SPTEs pointing at the previous page. + */ + read_unlock(&vcpu->kvm->mmu_lock); } void vmx_hwapic_isr_update(int max_isr) From patchwork Fri Jul 26 23:51:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743528 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6DF23C3DA4A for ; Sat, 27 Jul 2024 00:20:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=495sZLd3sCBTMIrtoKFKEohUXAwQ30LA4hbLQQBllaM=; b=O+OgdQpGDaENHqhYdg74pojjvo i1B9hZtpWJpAtIXgWAgX5eDjx0oPyskcRRy4EunANwQ4kIL27OCFHry6MDWrt/FZpN1c6d5EIdie5 lt2msJFNR+FQYYWMr8V5KdYxljHyp3uSiIq+lmYMbHF3uWM73CohQ7AZQolx9cJpJmxKyAfJW0iKT 9Oo+R4SQ/uhwO7rspI0qKDw/Dtly79eONeZe4VbvzT+QJdTcU/E5pn++BI9CmqL18kZWdhP9NNzaz pSTy3IIdJuy348YJ5Iw4fpvqGc+EQoEmE3R2/TDffkVy7zLuGvNdeGXCQgol0VZlPUojgjEd+6773 9orNlHqg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVAO-00000005dmI-46go; Sat, 27 Jul 2024 00:20:08 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlX-00000005QbE-0a19 for linux-arm-kernel@bombadil.infradead.org; Fri, 26 Jul 2024 23:54:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To:Sender: Content-Transfer-Encoding:Content-ID:Content-Description; bh=495sZLd3sCBTMIrtoKFKEohUXAwQ30LA4hbLQQBllaM=; b=gvYT2LB3UECpq7W4zY5U558chG WfnbxrpjInMbqQwikYD7UDWw3WUC7Un884MG/Q4OEkQjTmYiryp7daAHNkG1QExV5CNFOW1emn1aw 43RGYRq/HocTL/X+X5WxSImTdlrt/qnPVmsxdIQoO+MOoIthrasifyvcup84jVO9/V7T5mzmOFARH 2AG+Wf/XaDZBUXiq5aI9fPLQv3NgrP0hZ1ysBRbCGT+Uvm+W4iG/6d2UflBtvOQy8rOPz4Ti025v0 ++UGDHrQc4MRBQyhNuIl9s8tC2e0LTgN5e0lGO+jxYDE6Rl0a12fYn4CqQFxmfnpmZiHD1naI0Fdp 6qxdYuWg==; Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlU-00000004K1B-0FSq for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:25 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70d19a4137dso1423328b3a.1 for ; Fri, 26 Jul 2024 16:54:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038061; x=1722642861; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=495sZLd3sCBTMIrtoKFKEohUXAwQ30LA4hbLQQBllaM=; b=sDvOePFAvFgC8uzSLk4jiOO2dWqP7lcelY38TFcY8u3eC3K0BWfeJXcSmrJAelD/ps scCgnPTVhWfXLxOE9Hwb5N1TA/J3gkKhceTw7e0/HfcsLcbhGNn4Sul3t8IfH2G1Uq69 WmK2EqQLL9KfYwSi4MxTSBNWL+jpuuE9so1a5EAokko1O/4zLb6L15Ro2TskUbUN8TMh npGUP8ESxShD3krMjJp2X7IS+69JtgGK3KgCzhXmw2eMM1OIcKtEsiMGuszpz3qQmwlg eqTZBipk7LWfJj8tNWknQkJzqdJzwX2OEJI7lO1BbprFFQwmqWRSackU2zvIoPw/XIFg 63/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038061; x=1722642861; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=495sZLd3sCBTMIrtoKFKEohUXAwQ30LA4hbLQQBllaM=; b=Q1S3/aqTBZNjAUuDLsJ26lk3EBsy5KCsY04jNbVgzurN6Ox9VDyRtO6/aHQ/63r1ly 0F5SFz/RCELxMLRXgNHrfBR3vx3i7dq6cWCKtH6QXZNr6JqnzGysY2FyBgmU8ovX1Qex 4XsKRYFgpr89J5qV94XwAKVLyDx2iHHWYWqejiyxA7LUjTQ0rBWonVkEgnKvTKHfLu/P 6ImJdISNr318qjDzvr7zjPPQkD3zaN6zQfV9PH+mhUGiReyl4bz/OFH6s+I5IzjcTFl9 vBMzreRKlu5wIBFU0JP4RkWTb7He+JcRsDkfzI1dz9xhUHrWBm/VNjnRg1HdkLL74P1+ ZDbA== X-Forwarded-Encrypted: i=1; AJvYcCUtYXJL25Zq5C8Lc9cq47mxR/7F2HGq/Y2xosRVaV3owX52E4f3CwR9elMHvvH1wO4s2ZUJBiofyJScNpExL1Q0rxVzxrXYe6fESEUm2jLW6/QoCNM= X-Gm-Message-State: AOJu0YxLiuNGKf5Gziapk5mbASvyju/fILThTA46c4lPnmDu349bZNvS aucA4rqr6H32hoo2PZWC3dXfBb1nAP0pGBLX0GG+jQwLQSQld0aLW3gd8BKLPqQfSxivs+EXPFn zGw== X-Google-Smtp-Source: AGHT+IEX18wdLlkp+2aMf2anoWcNTpOtSr1c0ECGrHfjhmBCNZQh1f0vS16HR3FEGEctU7qtPLZbGkdGSSc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:6f1c:b0:70d:138a:bee8 with SMTP id d2e1a72fcca58-70ece533146mr8925b3a.0.1722038060383; Fri, 26 Jul 2024 16:54:20 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:59 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-51-seanjc@google.com> Subject: [PATCH v12 50/84] KVM: VMX: Use __kvm_faultin_page() to get APIC access page/pfn From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240727_005424_259991_D2836DDC X-CRM114-Status: GOOD ( 16.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use __kvm_faultin_page() get the APIC access page so that KVM can precisely release the refcounted page, i.e. to remove yet another user of kvm_pfn_to_refcounted_page(). While the path isn't handling a guest page fault, the semantics are effectively the same; KVM just happens to be mapping the pfn into a VMCS field instead of a secondary MMU. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 30032585f7dc..b109bd282a52 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6786,8 +6786,10 @@ void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) struct kvm *kvm = vcpu->kvm; struct kvm_memslots *slots = kvm_memslots(kvm); struct kvm_memory_slot *slot; + struct page *refcounted_page; unsigned long mmu_seq; kvm_pfn_t pfn; + bool ign; /* Defer reload until vmcs01 is the current VMCS. */ if (is_guest_mode(vcpu)) { @@ -6823,7 +6825,7 @@ void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) * controls the APIC-access page memslot, and only deletes the memslot * if APICv is permanently inhibited, i.e. the memslot won't reappear. */ - pfn = gfn_to_pfn_memslot(slot, gfn); + pfn = __kvm_faultin_pfn(slot, gfn, FOLL_WRITE, &ign, &refcounted_page); if (is_error_noslot_pfn(pfn)) return; @@ -6834,10 +6836,13 @@ void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) vmcs_write64(APIC_ACCESS_ADDR, pfn_to_hpa(pfn)); /* - * Do not pin apic access page in memory, the MMU notifier - * will call us again if it is migrated or swapped out. + * Do not pin the APIC access page in memory so that it can be freely + * migrated, the MMU notifier will call us again if it is migrated or + * swapped out. KVM backs the memslot with anonymous memory, the pfn + * should always point at a refcounted page (if the pfn is valid). */ - kvm_release_pfn_clean(pfn); + if (!WARN_ON_ONCE(!refcounted_page)) + kvm_release_page_clean(refcounted_page); /* * No need for a manual TLB flush at this point, KVM has already done a From patchwork Fri Jul 26 23:52:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743530 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 48A09C3DA4A for ; Sat, 27 Jul 2024 00:21:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=cHTm12hU98ihrn/4YhoQtTNxFr6qD1GrOX0sqy5dmrg=; b=G637FdTNRVcIdVjMYmlxVI0Mwe M+wjpM8FPEUlLy571eepEqDUBwJyqHxgCVzMvX3C4jDAoyGcg1SsriSvpf628Bgw6Da3N94xMVZv+ GH5w9svL/4s3LQ623biGv6q+NpI3k9bRI8MXezPKR0TsOhOMluR5MbO63fFOd5pOT30S69Be7lxJF jux/VZ3kG7T6dFoGLXKdrxuOawpY6mG/00fYbY7+xWcBnQ5e16cUWCb7IX4pkOoz2ZqJC8RRrgwiu lDF6rNEO3dUqBrxHnX6ZqvxsCsqkll2t37yQVH1vLAZwb+UfEwJ5HRQPsWyJc72JZNSp9J121S0R9 NMoFhfBg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVBb-00000005eWw-0nOG; Sat, 27 Jul 2024 00:21:23 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlb-00000005QfJ-2OKg for linux-arm-kernel@bombadil.infradead.org; Fri, 26 Jul 2024 23:54:31 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To:Sender: Content-Transfer-Encoding:Content-ID:Content-Description; bh=cHTm12hU98ihrn/4YhoQtTNxFr6qD1GrOX0sqy5dmrg=; b=CZTRi8jLfDlpvna2dsvIBASjA5 vLJ3trbL9QseQ4wLkjYN1pyM66OtnHyU0zpvgtbO+j0h/9yaciYUyEbBAl16Dv2kXfb7aC0NqvrNe /3me0zorBaAynuuoogjhs5QudVdcpMbcbDwu6kdGZ4dPFgJ/tX1pKPUshaOE0RNKWLrLh5SlAH22b zZMSpNHSSbSohbQ5xjEULqj/QZwtCi0/01BcUcvAs0K5OL2q2GVpqlISjxXPkvWfjGZUSjhRlhOAl zkib5qpwHSrFdsvGlF3NV+gSKi37+G/qt24XFP965OO3AQurDO0EGY4i2xmpdo8GwbffKtrE2FljO sJiqgTPw==; Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlU-00000004K1G-20zW for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:30 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-66b8faa2a4aso7071887b3.0 for ; Fri, 26 Jul 2024 16:54:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038062; x=1722642862; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=cHTm12hU98ihrn/4YhoQtTNxFr6qD1GrOX0sqy5dmrg=; b=1b1yYmKP7mnXxMD8tnd+bW+btmybxXMpXB9ZXxcbBPnk+y4Xq9L0jBGrXdeHOBJaEd uC+H+nUCYNpyEQdXJ+nfU3WnD90l2xR7m6qttWhkfpTr9nbPF8hyg6bFDel1hBhA09Zh W7s0yYoht5LCR3cLfE53AP+OBr0k9fFZuH7KoAmBCJ+/zmcPeuu8FHEcAca3Y+9cnw9E LqoF0LHpsAIbV7LI27bxZ3EDfXCAtVRXou/8AkGPVA9M5IKZv4XuTsUBH7LbVJI3mvLc tdeUZ8Tq2/MfUfEFY67Tet/kbztVAei3X6LYK1t45ciMHH9UmdCg+dP2KDlWAG8o0+Q5 22uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038062; x=1722642862; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=cHTm12hU98ihrn/4YhoQtTNxFr6qD1GrOX0sqy5dmrg=; b=Irqt6+TPdcTkqJM3rpW7DCAyyCtaZabgmRgQFZNeAOGxJdOZq/i2++b+7f1BT/pB0W //9jSR92Tq2pBfYYKOrIUZxzrNLcZdnSclmmlG8btxgCcyyjX2Cv8yYZP6ouud+fg/ft hlYPozy99T9KozpgleH4MSk9EnaC+Ru+ixG73xkihFDsUcLCbRM56zvpSECbPu+M/M0P XZjUm7Q4FB6+s4otGf7c04bqRQ8QR9JJPVO1vM+YC39p5WtL0zIy217JKnRqhO3xs3D3 bnbrjtjlKKgADw1JT26ggrgvAyYtV/pHSrNzOH8fcyRvpxGO8rruX5CKbi5JuYqjLkSe g4Lg== X-Forwarded-Encrypted: i=1; AJvYcCUFDUSMBVnM6RmYncNJ6S4TUfF15OQ7lZuh45wtkalkbGhBMBaqWMpCMuG34105F9uvP/94iwnUJ6Kg/ozAtVFz4zEHWMmvjHZWzowB+p8L58BrUJg= X-Gm-Message-State: AOJu0YwIaGGcWxUQhU30qwG9qYC7c9fMKo/jsMGmn0eHr1ASZdjh102U BP4viVtq5J8bV1Ohn3KJgAnB2/fAcAksc76HnMvS5/7qsc3yFe7cJ8yX5WWXRRVzQa7rSQQ2MtR 8JQ== X-Google-Smtp-Source: AGHT+IE591qeJqHACb7CkRANjpP2e5907DfMapi1wU/6IAMP+NJSB5aPgL5hiS9fj7k9JDiJNu7gQfM60rI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1005:b0:e0b:f93:fe8c with SMTP id 3f1490d57ef6-e0b5427fa67mr79886276.0.1722038062601; Fri, 26 Jul 2024 16:54:22 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:00 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-52-seanjc@google.com> Subject: [PATCH v12 51/84] KVM: PPC: e500: Mark "struct page" dirty in kvmppc_e500_shadow_map() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240727_005428_814985_82BA0B12 X-CRM114-Status: GOOD ( 13.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark the underlying page as dirty in kvmppc_e500_ref_setup()'s sole caller, kvmppc_e500_shadow_map(), which will allow converting e500 to __kvm_faultin_pfn() + kvm_release_faultin_page() without having to do a weird dance between ref_setup() and shadow_map(). Opportunistically drop the redundant kvm_set_pfn_accessed(), as shadow_map() puts the page via kvm_release_pfn_clean(). Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/e500_mmu_host.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/arch/powerpc/kvm/e500_mmu_host.c b/arch/powerpc/kvm/e500_mmu_host.c index c664fdec75b1..5c2adfd19e12 100644 --- a/arch/powerpc/kvm/e500_mmu_host.c +++ b/arch/powerpc/kvm/e500_mmu_host.c @@ -242,7 +242,7 @@ static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe) return tlbe->mas7_3 & (MAS3_SW|MAS3_UW); } -static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref, +static inline bool kvmppc_e500_ref_setup(struct tlbe_ref *ref, struct kvm_book3e_206_tlb_entry *gtlbe, kvm_pfn_t pfn, unsigned int wimg) { @@ -252,11 +252,7 @@ static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref, /* Use guest supplied MAS2_G and MAS2_E */ ref->flags |= (gtlbe->mas2 & MAS2_ATTRIB_MASK) | wimg; - /* Mark the page accessed */ - kvm_set_pfn_accessed(pfn); - - if (tlbe_is_writable(gtlbe)) - kvm_set_pfn_dirty(pfn); + return tlbe_is_writable(gtlbe); } static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref) @@ -337,6 +333,7 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, unsigned int wimg = 0; pgd_t *pgdir; unsigned long flags; + bool writable = false; /* used to check for invalidations in progress */ mmu_seq = kvm->mmu_invalidate_seq; @@ -490,7 +487,9 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, goto out; } } - kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg); + writable = kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg); + if (writable) + kvm_set_pfn_dirty(pfn); kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize, ref, gvaddr, stlbe); From patchwork Fri Jul 26 23:52:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743529 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2A631C3DA49 for ; Sat, 27 Jul 2024 00:21:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=WWjK/C8AJGiWvc7o48+tH+q+w9m6pJN61mgGRKL0h6o=; b=cs9zEhKA4l1UAlGLsF3U7KlxfE rQkwMXvO5IcEqvFU8AvOcfShzY+EtQELuwUQwXdQGPGRVjRj9UMuCtajX+JhjDXngx0TBtUgzxSH8 Ye4/N0K37pDmxYGTmxu05ZY6mX3eB2Jt6xT87AmExcz7oia1Il87/8Ed7Jn5Y/n+4z2OfoXRh3XLR zC66IgxGRIFZW140xpAPNNIOCe3jpGxQfCLMv3h8rVOo+HAaVIpynQVs/9sZ9QxseBhCv/OdF6LRW yaqjG88MuhsPRCOeZEzylGOXBysdF5fSIREdpeMe53436+51I7s7nGQIZSM2/QlPpBBAA9rvldV2g ctRhKGxQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVB5-00000005eGa-2Sze; Sat, 27 Jul 2024 00:20:51 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlW-00000005QaS-1BJM for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:30 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1fee7c9e4a4so10499745ad.2 for ; Fri, 26 Jul 2024 16:54:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038065; x=1722642865; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=WWjK/C8AJGiWvc7o48+tH+q+w9m6pJN61mgGRKL0h6o=; b=qUU4OHgbOw2M0kRlim6KlT4W+dp5p8w6E3f8xq1UppxM8UA1RcMojCUFYNfdsRFxCl aOO+824kw1jD8RjFbLKGuxUxkjF2YLzrhMS5VoJCJJ92di/sz/2baNLTk+GAIYLWvcdK RYHrZ4np0WDEcjOMLI042UccPIwPAJCKiJ9P+xxqQcghLAIJZpAPtRps0pY34ssPPR+K uxspnpnFpO2/inQ7+s3ZQHp/Bt4P4Van57fp/t7DNcFscgYoeKTty8H23R3LOwRgqTVY 7wt/QEGVMg+vBotmBoXqrqnCyp9Pwie8rkKWeJqd2KS0WOzP/BhfAoBbTIyOksGes05g DuxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038065; x=1722642865; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WWjK/C8AJGiWvc7o48+tH+q+w9m6pJN61mgGRKL0h6o=; b=DjiMu7nJLWrtEDXyQaIVXaSU2N9qlMCfQP8YQm6kbv0XwBN08Piz/EEj9hxWIEZe98 1I/vRuCY7hzEMJUcP/GcWGVQ+J70qLLCJVADWhg8Jr8oYeDPjrde5saUU+T+wb10YX9f V4Wt0E2QjR7eDvLLhVZXtAj04KxCL3hXCTS5CZ+f5E0KJrJCVkxR4o54pKt2aWmqOoGR cBwMkIl9GVFZP5bp5i3wrAFvDrZUQod1jNmI+KN53+/zSHQ97dABieLReQJs+fbGtq9W Lc13Tk7BdaMCFfJA1IEcxWHucIkBx3zTHhRaBL+MKa0mCHj29dLuMZjRGoabf2UPbnKe dZtw== X-Forwarded-Encrypted: i=1; AJvYcCWEpSw68OWEHJ/21oIWl/CJyE2iTPcC3jarADqde/OF2QmLK0Y0NnQvIe/mDrLitdmaXuQB3GGaAIccLQMveBo/r0UXHQbbMR67WbLhQb3zlu5MvpM= X-Gm-Message-State: AOJu0Yz6IxJkyF+SbRADltuEJ6hbj649/3dhITMsyP5Eb9RLddlK/YD2 745GtEHgtVfWk7Lc/y5N7DdEA5p5u3EkrGDbxYySa7s2GY7oCQ89YZJkMnySX/1bLobiVl1jh2X 0/g== X-Google-Smtp-Source: AGHT+IHjueGi4XHTR80VLKYNSmezTNqicqTjy1hjWkoeqfVxP7xL/6dlCTgQugjOLvFflviFEVZzvXY7ZzM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:41c6:b0:1fb:80c5:ce5d with SMTP id d9443c01a7336-1ff047b2dd6mr400915ad.4.1722038064523; Fri, 26 Jul 2024 16:54:24 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:01 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-53-seanjc@google.com> Subject: [PATCH v12 52/84] KVM: PPC: e500: Mark "struct page" pfn accessed before dropping mmu_lock From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165426_588293_E5324DDE X-CRM114-Status: UNSURE ( 9.82 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark pages accessed before dropping mmu_lock when faulting in guest memory so that shadow_map() can convert to kvm_release_faultin_page() without tripping its lockdep assertion on mmu_lock being held. Marking pages accessed outside of mmu_lock is ok (not great, but safe), but marking pages _dirty_ outside of mmu_lock can make filesystems unhappy. Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/e500_mmu_host.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/arch/powerpc/kvm/e500_mmu_host.c b/arch/powerpc/kvm/e500_mmu_host.c index 5c2adfd19e12..334dd96f8081 100644 --- a/arch/powerpc/kvm/e500_mmu_host.c +++ b/arch/powerpc/kvm/e500_mmu_host.c @@ -498,11 +498,9 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, kvmppc_mmu_flush_icache(pfn); out: - spin_unlock(&kvm->mmu_lock); - /* Drop refcount on page, so that mmu notifiers can clear it */ kvm_release_pfn_clean(pfn); - + spin_unlock(&kvm->mmu_lock); return ret; } From patchwork Fri Jul 26 23:52:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743531 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4E100C3DA49 for ; Sat, 27 Jul 2024 00:21:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=zSr/S7cQW+jg6+5Yur5d69GSxQ0D+3E/XB18qDPP3eA=; b=Zco3tLUuSrMOEumUlx1uqYYlvB Q+FW8aYXEr1wgvcIllzBTLbqFs8n645+1fpL3Gje5rXfa4CIZkTrCX2u/ATLu1o3JwIevioJJV+1N +2H485QqUUNyuVhdIfnUrhvVu1QV/z61E8RIpIwp79xqq2PYy40/eVOXy5+IJmt5BmU7GZm+a2dXe HbwPfh3DUV/YovBR0ft1b+mOOqDp/UJ4SirYAmweeJuShkX0fEyX5coYRkXmYdwg+GkpA2Fwv5fIh 9RfnTIVd2Xdl1LB4KPyXkwIIXZHfK1q/VjFE9JcB541VJ1TwrmxVni/f4jBuuzcLYhjKh0mSRP4lQ 5UZTc90w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVC2-00000005elE-1Z6s; Sat, 27 Jul 2024 00:21:50 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlY-00000005Qc0-1VFf for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:32 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70d1a9bad5dso1374482b3a.0 for ; Fri, 26 Jul 2024 16:54:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038067; x=1722642867; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=zSr/S7cQW+jg6+5Yur5d69GSxQ0D+3E/XB18qDPP3eA=; b=Yn2ggu6BQKcMFB9DloD1NFvFA9WT1o9AlWGknyXfGwG66zrtNAvRhYE2xZMC0UhBNd Hfdz0YWI9rVcQXoycJxQmWwG8+QUP5MvwQTECGM0HuaAYE2ZWQ3cfgjV+iKj4NSwp4gc ng2LYUanNE8VfqgI3cJkwp14zLswAmvuRvpupZkkZO463l8z5Fg0unzetH1RzAOe02rQ H8rYQl9WExMEn+zdhXxOQTnIfBnpRBgm3HXmXSMNs7IECkK5jHNRv0hHamL+4SXPlYlc AdV4NxIUVIpTd4NF5g+jOmVmzA8JYHkUWPQgQR/3GqZI6soPGtrozOd0sMWpRgvGm+6D Ydlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038067; x=1722642867; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zSr/S7cQW+jg6+5Yur5d69GSxQ0D+3E/XB18qDPP3eA=; b=TfzbVfpEFywEF08CKLmASwyUcKV5quqau5kJvdDYR3G1JmwArQDwKGFy6QAWULEStb El+pjIhVZ+O/g5gYrKVWneEp9pmWl7SlYDfYE7pYBp91Xgi2jdMKMiT6jb3cB1jPZ0Zc xdsn2LvNtGO6Av5t0OIACHOyJPUVxnYwrS8IgYtryQFAWwAD0dIerdlyugaQiIz/2TDT 0+sNH50BziKpKDFaacWHUzEGMTBkNy25BQHZjYRltO/siyKe4JGn0ixBsE9L3bj7+Y+v uPki88zRJ2e+EmwePVysdE2GH+1l/IpB6PfhtIPm1SJ8Xcw9KDAJuSpTGVLlpEjDxfnk eAyA== X-Forwarded-Encrypted: i=1; AJvYcCWy64zJL/dLwvnSjw93rAIv6KaPVegXAJuIxfq8PAXt8aL6p9oTYS2KsDGyuBU3k8w/o8Wq1uOOaLFLSlJox2sp0I3k072XbETCw2RUeiE+bkNpvaE= X-Gm-Message-State: AOJu0YynzcJvg7U2+z4jYpefNSadSG5KD51nGnUju4wxdJgiG0uFDd9l 3trOCEm59qgTSuIXA16GgXHs7VEP5bVim0jn8PEfkGgbWyvxOpt52qQIKmA0x8I9hEliSAEWM0n fjg== X-Google-Smtp-Source: AGHT+IEvvOLEi17q5BnKGFU80vyNCkxL+K4ME2VW/zA0UgmW3Jn60esm+Gg/1wQhRB2++SmF9ZjJ4yJ/TXU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:238d:b0:70e:98e2:fdae with SMTP id d2e1a72fcca58-70ecee6db73mr19809b3a.0.1722038066705; Fri, 26 Jul 2024 16:54:26 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:02 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-54-seanjc@google.com> Subject: [PATCH v12 53/84] KVM: PPC: e500: Use __kvm_faultin_pfn() to handle page faults From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165428_577350_0427765F X-CRM114-Status: GOOD ( 13.37 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Convert PPC e500 to use __kvm_faultin_pfn()+kvm_release_faultin_page(), and continue the inexorable march towards the demise of kvm_pfn_to_refcounted_page(). Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/e500_mmu_host.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/arch/powerpc/kvm/e500_mmu_host.c b/arch/powerpc/kvm/e500_mmu_host.c index 334dd96f8081..e5a145b578a4 100644 --- a/arch/powerpc/kvm/e500_mmu_host.c +++ b/arch/powerpc/kvm/e500_mmu_host.c @@ -322,6 +322,7 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, { struct kvm_memory_slot *slot; unsigned long pfn = 0; /* silence GCC warning */ + struct page *page = NULL; unsigned long hva; int pfnmap = 0; int tsize = BOOK3E_PAGESZ_4K; @@ -443,7 +444,7 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, if (likely(!pfnmap)) { tsize_pages = 1UL << (tsize + 10 - PAGE_SHIFT); - pfn = gfn_to_pfn_memslot(slot, gfn); + pfn = __kvm_faultin_pfn(slot, gfn, FOLL_WRITE, NULL, &page); if (is_error_noslot_pfn(pfn)) { if (printk_ratelimit()) pr_err("%s: real page not found for gfn %lx\n", @@ -488,8 +489,6 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, } } writable = kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg); - if (writable) - kvm_set_pfn_dirty(pfn); kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize, ref, gvaddr, stlbe); @@ -498,8 +497,7 @@ static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, kvmppc_mmu_flush_icache(pfn); out: - /* Drop refcount on page, so that mmu notifiers can clear it */ - kvm_release_pfn_clean(pfn); + kvm_release_faultin_page(kvm, page, !!ret, writable); spin_unlock(&kvm->mmu_lock); return ret; } From patchwork Fri Jul 26 23:52:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743532 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7D663C3DA49 for ; Sat, 27 Jul 2024 00:22:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Tp8vWxWuEfoTun4nd23rTIy8nVqGGlQS/g8lo8vNdQg=; b=AEP5VXb5wrWvMemoUujgQjqxFj zDmFQmDZrQJBlnvNcs6ws9VXr4nrEeT3fG/0I47bxhCe5WvA+i5A4j/o6zX6PvpsN/RS/PmjhvWAK SCZzTmjLxZ6gVbT0mRYKLu/ZTdaJhemYis5tePTj9TwGbWNe6Fj7Y2I2/OQVUby+X7J51xGrTX+ua 6MQMAy4mBgmb5w+KLAuufDi1aiVmiMTAKqF5mQ1anR0S/0mCQWnfJShy/tK8MvNceq0rDW02udNG9 eN5QuLVZTxZizienoQgobItlZxgXPaudzJjFA9YcPap+71yn08KLJ5joaiJz+JdmmDGOiusQoGdap ItIL2w2w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVCT-00000005f34-3y7j; Sat, 27 Jul 2024 00:22:17 +0000 Received: from mail-pf1-f201.google.com ([209.85.210.201]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUla-00000005QeK-2KuH for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:33 +0000 Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-70d23f0a3d7so1451317b3a.1 for ; Fri, 26 Jul 2024 16:54:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038069; x=1722642869; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Tp8vWxWuEfoTun4nd23rTIy8nVqGGlQS/g8lo8vNdQg=; b=a1Lhshv8scCGz4BW/AhCsEkLRsaRE03TXxu79ACYfTgekGUogBCvMSdGmgyhHZOtFV M5/Y3F0oraYdEq2hipg7fJb6pX222KltgONMQcluQxaskkHxMb+TwImU+Z6cgCzipXUS 97v+5JEJlIEUh9/pPVsIw3GeKmbtcPutToCS4lgJVAzaPwm3JEArcedc4zy9dP7h1bDH /hky0GMwSrc5viTH5liLZAWG7T+SY3RdN2B9mjqtDfIIrESwzQws6ZGsekrs2pT4cFmn n2uZXIGgMJ2lcjAEfipWXqTDWdkYlaA+3KveekqQb7ZRlc+/knjrPxRFLKThdYPKjxny wi2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038069; x=1722642869; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Tp8vWxWuEfoTun4nd23rTIy8nVqGGlQS/g8lo8vNdQg=; b=G3kLGkT8/M1r1TqX+4n0nQtoUPNnWhhzBwkZQXpr3PU4ZpwWnWZktPgdeXa8GWoPZM NirnxbnqDMV+YmfvochClhF79To2o7YScGmv8ye832YmDsfBWhT6i+efGWI5ssaAOZgK lRP3cXyEDO2DRsSnZ+qADzB0pU/E6J4eAbhs9EoO/rcyTLyC5s6gld28/MayEv5h8Fmn +6H/B/rQruZMDSrtO3NOFHEea9FB7TdyPi5ACPYsWIOZxnWLeK2r7qhFrJlAw9b/z41s qFjFcdZJ+fuxuFFLQ2NEDC1ZbIUvi3YmkfWrb54cW28aIf8d82Gh29SwI1gbJasJva65 7cLg== X-Forwarded-Encrypted: i=1; AJvYcCUXHI6SRU6GFbzpdJ7B2mDWRN8ZglgicwI/GiC1sST9IIynbvuTDofZMPBpDIPp0KZQu/AS+71tkntvznS4JOTA3aGJ3G4BTNrAt2C4CSEtzEQvWpg= X-Gm-Message-State: AOJu0YzjjhEpUm9sNIm7LE38M1gTgGqMmuLhhXMm9pdY7qwbCftUgUK0 66A0LN7mPoOVv4wK5crZ5EQT2zx6JxlyetkXLhcWpCzt813iMhHEFwHMCyL6dRzEtwOsIKZoSxr rFg== X-Google-Smtp-Source: AGHT+IGdEcu09Hin88e0jkC1IJzP1gp2m8bzTdpaUdhQIiML2lxsNf415Y84GaeMaBcBrlwhHWl3MjBIRUQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:6f1c:b0:706:71b3:d7cf with SMTP id d2e1a72fcca58-70ece926b55mr9310b3a.0.1722038068878; Fri, 26 Jul 2024 16:54:28 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:03 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-55-seanjc@google.com> Subject: [PATCH v12 54/84] KVM: arm64: Mark "struct page" pfns accessed/dirty before dropping mmu_lock From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165430_787653_A3130E9C X-CRM114-Status: GOOD ( 12.40 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark pages/folios accessed+dirty prior to dropping mmu_lock, as marking a page/folio dirty after it has been written back can make some filesystems unhappy (backing KVM guests will such filesystem files is uncommon, and the race is minuscule, hence the lack of complaints). See the link below for details. This will also allow converting arm64 to kvm_release_faultin_page(), which requires that mmu_lock be held (for the aforementioned reason). Link: https://lore.kernel.org/all/cover.1683044162.git.lstoakes@gmail.com Signed-off-by: Sean Christopherson --- arch/arm64/kvm/mmu.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 22ee37360c4e..ce13c3d884d5 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1685,15 +1685,17 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, } out_unlock: + if (writable && !ret) + kvm_set_pfn_dirty(pfn); + else + kvm_release_pfn_clean(pfn); + read_unlock(&kvm->mmu_lock); /* Mark the page dirty only if the fault is handled successfully */ - if (writable && !ret) { - kvm_set_pfn_dirty(pfn); + if (writable && !ret) mark_page_dirty_in_slot(kvm, memslot, gfn); - } - kvm_release_pfn_clean(pfn); return ret != -EAGAIN ? ret : 0; } From patchwork Fri Jul 26 23:52:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743543 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6ECDBC3DA4A for ; Sat, 27 Jul 2024 00:22:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=caIg6eZHDZieCZ2y8TnIGsflH+xvFOT1i8qMSVM8X2U=; b=aDS8VF7e+52HqWYcvPUyDFAlIn GpLKHU0icibPTODnAPuo9qEo3xgQDcGr5FxxiJ57glEIYa3bJNLhIj6dPyvfK7CJIdysdfUdCcCu9 RZr5PMllbpwJVf/LE0VdAZkxOS0uIaN5UrXPgrYpxOdhlbJObrU22wa9eZc7nUo81EfYiHesODe0e 6eCeVa2HmDIFNzNI3kTm5+12PI5bcY+Gzm2ryLAe+eBQM569N+04DjaZmnAgYVCNd57KbpG8VIuFm xgvpRk/G/ebDbHK/RogrofhHZ6znvobJmle9EsLCxwa5x7muvPQTxmK9lprPYQWY35I3mOXTU992t qXccbohg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVCx-00000005fKW-2Huc; Sat, 27 Jul 2024 00:22:47 +0000 Received: from mail-pf1-x449.google.com ([2607:f8b0:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlc-00000005QgS-1Zwr for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:35 +0000 Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-70ec1039600so1046823b3a.0 for ; Fri, 26 Jul 2024 16:54:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038071; x=1722642871; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=caIg6eZHDZieCZ2y8TnIGsflH+xvFOT1i8qMSVM8X2U=; b=L6Vh7A+BONb2dq+by+MhTfWBD5KMq4ZcbNgL1qRWH/8nRuuhZk7mFKHCjuL6pdpIg7 UKTcSULbYwg294JRCwDcMDw3Wt4T7qawJZCJ2ZJVqkow//bFaAud1wxv3eBCvLpy/rSc tbLPJWPHv8CYL/6r2LMX6xKCS+NG+QF+UbYp4OeJUHuGIrYIvii2PcYtuYmv8dH0hA8u HYf2YWdrPREUt61XQyEnKLmWcnqnHG8b8nqGnRJHuK56INLZ3TaOMCMlPE2cGEpZqO98 p4S8C7J4UYrMM+AhNyRrTll8edZIKnIdnnTvYSWIrfqiJ1sZ1zlE4q7X5gj+oPceBGlg HWDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038071; x=1722642871; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=caIg6eZHDZieCZ2y8TnIGsflH+xvFOT1i8qMSVM8X2U=; b=PwO07SG6bVloOYsIsEUTmgXi6CnF6+YVSFjyqRtWL3yJTHq6Pts+Maq7qksw/pfHe8 C7PjYJfTMLCv1tTkkcn325vbG9dLVNXou2dVEOS0WhKD7Ix1JgaVfg4ECR4ijWXJfU84 EhOJow3nh1myT57+orMojPEKYPOdTlteH9D75eb+NNUA4uxTWo8GLBsJHeoN06P+0tMR V/XNacWmulQuNp5eI8YybbciaGmir+Rz4zSfWKwC/LXJcq7PfZ6kiBvK3Ljg4M7DHigC YhOISXup38XABgLks26UTX+cw/CWVvf7JabUX3L8mC15hQuHHJiQqjNGDdT5cCQVZgkl hkhw== X-Forwarded-Encrypted: i=1; AJvYcCUECFY6/naw4tDhPBU7CNSeeX5sfs01WxB0TWl1tr2zdEzpwn62SKtvrmJhABbaPJl0yCfaXubXXxT5TIrYL+gyAoaJzSLtJQboIT+NvobwvL9wIGY= X-Gm-Message-State: AOJu0YwVM/zTRRufOeHzyfoioRnCI1qw7pNqBDGEo/AJZTm6WO26FzL5 nDwPXqrIMUgvbQvzhgiBzuGyafodjd2jFYhKamolRS6VyAGXn/+8JsFVnGgAqCR+ZP23RInziaY Yww== X-Google-Smtp-Source: AGHT+IFCpL56eroYNOyLiVhUcT5fyuFzA98b59p62FwWY/A7pZmePwqTF4FXfEto2ywMz9XC9lExu2Xr4CM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:9445:b0:706:3421:740d with SMTP id d2e1a72fcca58-70ece9ecf02mr25759b3a.1.1722038071004; Fri, 26 Jul 2024 16:54:31 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:04 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-56-seanjc@google.com> Subject: [PATCH v12 55/84] KVM: arm64: Use __kvm_faultin_pfn() to handle memory aborts From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165432_597712_7FC4A6AA X-CRM114-Status: GOOD ( 13.88 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Convert arm64 to use __kvm_faultin_pfn()+kvm_release_faultin_page(). Three down, six to go. Signed-off-by: Sean Christopherson --- arch/arm64/kvm/mmu.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index ce13c3d884d5..756fc856ab44 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1439,6 +1439,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, long vma_pagesize, fault_granule; enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; struct kvm_pgtable *pgt; + struct page *page; if (fault_is_perm) fault_granule = kvm_vcpu_trap_get_perm_fault_granule(vcpu); @@ -1553,7 +1554,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, /* * Read mmu_invalidate_seq so that KVM can detect if the results of - * vma_lookup() or __gfn_to_pfn_memslot() become stale prior to + * vma_lookup() or __kvm_faultin_pfn() become stale prior to * acquiring kvm->mmu_lock. * * Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs @@ -1562,8 +1563,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, mmu_seq = vcpu->kvm->mmu_invalidate_seq; mmap_read_unlock(current->mm); - pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL, - write_fault, &writable); + pfn = __kvm_faultin_pfn(memslot, gfn, write_fault ? FOLL_WRITE : 0, + &writable, &page); if (pfn == KVM_PFN_ERR_HWPOISON) { kvm_send_hwpoison_signal(hva, vma_shift); return 0; @@ -1576,7 +1577,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, * If the page was identified as device early by looking at * the VMA flags, vma_pagesize is already representing the * largest quantity we can map. If instead it was mapped - * via gfn_to_pfn_prot(), vma_pagesize is set to PAGE_SIZE + * via __kvm_faultin_pfn(), vma_pagesize is set to PAGE_SIZE * and must not be upgraded. * * In both cases, we don't let transparent_hugepage_adjust() @@ -1685,11 +1686,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, } out_unlock: - if (writable && !ret) - kvm_set_pfn_dirty(pfn); - else - kvm_release_pfn_clean(pfn); - + kvm_release_faultin_page(kvm, page, !!ret, writable); read_unlock(&kvm->mmu_lock); /* Mark the page dirty only if the fault is handled successfully */ From patchwork Fri Jul 26 23:52:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743544 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E6B1CC3DA4A for ; Sat, 27 Jul 2024 00:23:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=EmJI7mCwitfWH+6LwHkJyXugWKEFSS3lkF1NSC2iA8Y=; b=SMNgEiEfreTVUYlua+pcoueVTX 5jfU5DxelvaLTGme9z1+3noiQqqPKa8TCUeHAtgG0SsRap+Ppe9Yd4Oz8C4YbD8nrZslH0hlYXLox 0H8T5Yk4U6UezONueYJXnLlx1K8ssOkkgkSqupYi+i6i+/eiVFMMcLwsUVnsyN1K/46CnY9cQpyla dTpvQ5mvKXkd7k0vydkg5fQd0G0w8ATSNXZzubA0cx/7SeP96h/vVsiuiU9PPt/ORIRqEPHD0jeDr Ide4zLkeZ4XQJ5V0hvijchKetJP4ppdE/kMm6iHjEnaAxuUEfojaMfi9garpF82g1dBzXnUPpeyy6 0CaprTsg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVDT-00000005fcq-2jWD; Sat, 27 Jul 2024 00:23:19 +0000 Received: from mail-pj1-x104a.google.com ([2607:f8b0:4864:20::104a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlf-00000005QjG-2ey1 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:37 +0000 Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-2cb7364bac9so1380487a91.0 for ; Fri, 26 Jul 2024 16:54:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038074; x=1722642874; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=EmJI7mCwitfWH+6LwHkJyXugWKEFSS3lkF1NSC2iA8Y=; b=z/sWPq8X0VYqLhknX9wnJ8pXC+1A7eAW2aJnlwjy8GR1ksgusnC8d3Yb1Huny3aFac AcUaSzDAR7VC+YPqYT3xPg789wnEtxX5ItNPiIFAU4F/tnWH1dBId8uvmpXq31g29WHf 95aAzf1GbIJkdyBKFP3XLYLig6eH9JMoweZ5GwVq3kw+ibUKp9L7wb3ts/13u73m4IeQ klaLUmKW3xsswtoFMkd/I728xnRThLuJIIEdpVSFfbezLZ+IStWfiBB+5kuV9ejmxVcg Fo1UV230/5WOg+ScK+qEBEjhOWG1qclEXxyvMaAW8IFYQmo900uqR5SyuF42EviuNDmO jDdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038074; x=1722642874; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=EmJI7mCwitfWH+6LwHkJyXugWKEFSS3lkF1NSC2iA8Y=; b=JB+/7EQkvVj+f1ueEBJse39dReVaa0VbZ33gqXUVj48p6X35RXmJWgx4+iZxTKhr7K D2VY9OZLJe55ZVrYnX2S8TltMipqjVgn4xs3lO16qZ0zk2RbY9RrTAGhGbaS53HgpJKY UDXUd/ByC8DOyG5BZ0vGrhTTuAovrFZU4Fgmkr70CZXzYEUIByzhXT/mVsw8EwlLpCgA CxB99V6CM3/Qab4Q0E/L6dfAfUaz8+vA62P2hjVoa/BPTHKgoVsWmoxyygWAzPuCDEEh lhMGT5/CeZqwNtbXc2n6p121JtM5UU9SNSJzD//jk04Fb42jV6WJCKqsE7k1d6bWK7wO vtbQ== X-Forwarded-Encrypted: i=1; AJvYcCWkEGIGKrbErqMhpvB6QZhbyFsSBYmj2Y6A1eb3yyt/3m0qp880iT8JK+FcImz/qnGNGgZsWJLruvqbOem/+F6R7TuSLoycqcQncMS0/W0vnFE03DQ= X-Gm-Message-State: AOJu0YwUjAOdg50r9gXd1nWhcrBl2ePz73IvECRWsPrAXH3bjn1Z392u zeQzJv1lhVDsCknfwQqjgEeynGBQCCLfetV+Zif7lRipHPXd6kioHyIyP4GoJrr1Yh6jtmJ229Y xAQ== X-Google-Smtp-Source: AGHT+IG4DskQJpICSO/7ZhzoCwUBvTBXkkJhFyAvydTVcsfBboV7ccXE3z2KESMy5r+D4xCglFGTF0FLhYg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:470e:b0:2c8:632:7efe with SMTP id 98e67ed59e1d1-2cf7d1673aamr10630a91.4.1722038073062; Fri, 26 Jul 2024 16:54:33 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:05 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-57-seanjc@google.com> Subject: [PATCH v12 56/84] KVM: RISC-V: Mark "struct page" pfns dirty iff a stage-2 PTE is installed From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165435_710603_E5721D23 X-CRM114-Status: GOOD ( 11.92 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Don't mark pages dirty if KVM bails from the page fault handler without installing a stage-2 mapping, i.e. if the page is guaranteed to not be written by the guest. In addition to being a (very) minor fix, this paves the way for converting RISC-V to use kvm_release_faultin_page(). Signed-off-by: Sean Christopherson Reviewed-by: Andrew Jones Acked-by: Anup Patel --- arch/riscv/kvm/mmu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index b63650f9b966..06aa5a0d056d 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -669,7 +669,6 @@ int kvm_riscv_gstage_map(struct kvm_vcpu *vcpu, goto out_unlock; if (writable) { - kvm_set_pfn_dirty(hfn); mark_page_dirty(kvm, gfn); ret = gstage_map_page(kvm, pcache, gpa, hfn << PAGE_SHIFT, vma_pagesize, false, true); @@ -682,6 +681,9 @@ int kvm_riscv_gstage_map(struct kvm_vcpu *vcpu, kvm_err("Failed to map in G-stage\n"); out_unlock: + if ((!ret || ret == -EEXIST) && writable) + kvm_set_pfn_dirty(hfn); + spin_unlock(&kvm->mmu_lock); kvm_set_pfn_accessed(hfn); kvm_release_pfn_clean(hfn); From patchwork Fri Jul 26 23:52:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743545 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6217EC3DA49 for ; Sat, 27 Jul 2024 00:23:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=8WWxSeEhwRY8Kz5lKN5dweAMeNFVe6itBQweyIKsQVg=; b=4kt+/d8E3JgY9qq+/mhiUkqGUx oHS0658Q8BsnqSQN7aE+HEnaITrT3skkE4q1yMUdJRhRQIAD7AFnrjZvR2Df4Or18Csq9vCFGeFoT Ky347/S83vO/D82X2WRSZiGZIjxp6l7gmlYU8abIJ0DgEZMRWt33CA6qFm3J1/N6zT2Uw5vHNq6z5 nxX+M/Y5VKPnqCEbJDQwlu9MV1FLPwy/yCMJeWB6PR7h8AsIwLZut9R4GtajhJip0FQ8YoW9yh3xn 6s9LsJUMWeW8s9LApRcsFdN+HOgdoDTSyEaugAdxC1lOOb5tOmXUMBRFGFWKlX3NR2LZOoDIPA9E1 qD0tueHQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVDs-00000005flV-0ZO3; Sat, 27 Jul 2024 00:23:44 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlh-00000005Qkp-2scc for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:38 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70d14d11f42so1334369b3a.2 for ; Fri, 26 Jul 2024 16:54:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038077; x=1722642877; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=8WWxSeEhwRY8Kz5lKN5dweAMeNFVe6itBQweyIKsQVg=; b=gUINzmv3ccwPURuliTih7NZtM154wXvsdSsrcafg+DMUmCf4ubpEHFwzZ05zre7bTh hrS5r72dQVe4ry6XCpkWWN2tEcgyx1OADXToRRHJUfR8qXlmq3iN31aRq+taolnPt5gW uL1ZlmyxVfCLD014XsrVbKzEjXs3Xjb6VKo1vV1q1fEtfLbRUdufKsHYHACmV3Ipp0kC UQWjJS8enQt7lX5Sh1bNa/dygf/L8ljU5U8tvLht0KfAiQoA++7cAHJfU2M7g93az7nz ibGjXPVRk4tFkZVXDCLsOGyJbLhlzcOhXbm0lk5GVtsUOQiWUlU9SdjcpKDY5dMTu6U6 9e0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038077; x=1722642877; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8WWxSeEhwRY8Kz5lKN5dweAMeNFVe6itBQweyIKsQVg=; b=YZSLU78Ra1I+uDK29HmHpFY4AtNzCraFYuOrZioKkmsFkwM/7TpJfcDDazEuEfq2HX mFSxI2oEOlX8UD9rtC0SPK7AJ59EDMJJ2nfskXQ1j/nQzG64UjQjmqszRrSWaClFoKXU AQ0BJG060nmwMw3rx10tvZtg+PLP62XmKQ4CXk2SggVgIGE0fAwfm69nQ7bjpy7gNOkq JFertuCC8bNwjX+nZv2FsSienDfx93DzdhdVk7kKKUn321lrCOvVCAphfrjfbUxtVoa2 b/U6Sfpz0gcq2FLVM5wsgNbTenNlzqeJdvoRDa8AFC0EOK/2hZi5Wq5hTMZCe017H+st Bhhw== X-Forwarded-Encrypted: i=1; AJvYcCWTXbECd7H4ptBrMPK+q8kIpMFBCYTgfz4H6kiS63ho3LNATRTbjn2praAi2Qs+GVLXNtNOqW80opOwRTzRvke1Ml/mkbZzns9l3ngfqd9mkUlEu3c= X-Gm-Message-State: AOJu0YzoO+MaSrG+blGNiqOeTGezJjD6NIB1uvSFApGaIQ3GS1UL/I9n Emcv5QsKUNQJRc7f7DsToeTBWxUnv4a8KfWdOW+Oue3fkajNL8n6eZqM2bUvRSsni4itEy5+PKb gCA== X-Google-Smtp-Source: AGHT+IFil497nfsH36PcPgD5gbB02jEmsX2CuGy2fe2s5ytKElT5nFsSQdr9eHlR2IIEavfJT4DgKyMZ3iM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:66e5:b0:70d:1e28:1c33 with SMTP id d2e1a72fcca58-70ece9ecd04mr10022b3a.1.1722038076443; Fri, 26 Jul 2024 16:54:36 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:06 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-58-seanjc@google.com> Subject: [PATCH v12 57/84] KVM: RISC-V: Mark "struct page" pfns accessed before dropping mmu_lock From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165437_745962_D97D4A04 X-CRM114-Status: GOOD ( 10.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark pages accessed before dropping mmu_lock when faulting in guest memory so that RISC-V can convert to kvm_release_faultin_page() without tripping its lockdep assertion on mmu_lock being held. Marking pages accessed outside of mmu_lock is ok (not great, but safe), but marking pages _dirty_ outside of mmu_lock can make filesystems unhappy. Signed-off-by: Sean Christopherson Reviewed-by: Andrew Jones Acked-by: Anup Patel --- arch/riscv/kvm/mmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 06aa5a0d056d..806f68e70642 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -683,10 +683,10 @@ int kvm_riscv_gstage_map(struct kvm_vcpu *vcpu, out_unlock: if ((!ret || ret == -EEXIST) && writable) kvm_set_pfn_dirty(hfn); + else + kvm_release_pfn_clean(hfn); spin_unlock(&kvm->mmu_lock); - kvm_set_pfn_accessed(hfn); - kvm_release_pfn_clean(hfn); return ret; } From patchwork Fri Jul 26 23:52:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743546 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5EEACC3DA49 for ; Sat, 27 Jul 2024 00:24:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=lKpu7mXAQaRbYxbEDOwpqeV9V2sn6fCg4Pm2+RclqqE=; b=e7gXohlvDySm57z0+OIZw0pnSL 1/azrq636RR/qgcDzwug/Myld25gkZJqiy0fbqb5zzq2Q1uaflvVYel5egE9t8F8vwzhauWaRN+cY /syiVzTVDecj/bwxvXsJ+/aRIXqCsuL9HmiMjpGkBpIp/EgCICh4G7tMT3xFNIkUUyPIGbFkW5OvA P/6e263H6Bqu76okHutWuYeCxR83KlNYn49rn1Pgb638nxA09cC/eP8bcxirHIC8XtI6F8UBlVXEH NF/C4K4f8FW7JnkEIvZOGfVMjYGXdqFKrSd90RPGFJv9FU8Tekt8XNI/sdURlfesyxa7Pwp4d8Z8q KjLuut5A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVEK-00000005g0Q-0dbG; Sat, 27 Jul 2024 00:24:12 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlk-00000005QmC-1IVX for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:41 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-650ab31aabdso5339097b3.3 for ; Fri, 26 Jul 2024 16:54:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038079; x=1722642879; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=lKpu7mXAQaRbYxbEDOwpqeV9V2sn6fCg4Pm2+RclqqE=; b=amvRF73sojBrVVsixwqf2/BTc3bqumQhPW+sDePsVGJusquJ7TcLb6QEfGXt9RJFYN HWcfA9/LtYq8FSlBNnWwDNCNw8NkHuLUQQAO+ycvurxUJiBhvQ2s3nZ0FxbZH7+Iet4q 1VYkHMq4I6d200hyXPNbapazBzkOwBpAoUL4ePzap0J1zvyN7aqMkXV3gqg7TPA3ubyf 7blcbUoEhVJmuTiqdO1jLNTpW59smLZoHWjLSuDwARZZTFIraUcYGAUjRcBLGVM6jOrc KOU5v+CP0CbCvBIv4i7W0n9rWSrJwlY6RccvQIvvYl/xij+lXugvB1//9odswzNQtLM3 BIRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038079; x=1722642879; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lKpu7mXAQaRbYxbEDOwpqeV9V2sn6fCg4Pm2+RclqqE=; b=UcEkV0A3hCRRVj2N4FswjjtYtMuQP73nWE/U/NGwKP+6rAmETwXodf1GS3+bPUqnso u+grGjbd/dME7b5UCvEC7XEGAX57LGo9lSoL9Awcoy/58k0uzBa12E8Gx/H6TackO7Xz ELmZL3NpLK7lxR8N6lvEhCI6r/SGB5O0qm5NYGHBgmI+UTbCr3v1YfG/5bJRsqI8X2ck C0WYLcv4zUYqTirDaopNL/amMXkp/pEeXQilfABjTIljsKkhLe11dM7v72T64TjAT8XK 6tICz+kLne0z3ivxF89heonLywajheBfHcKPdS65sY2kX4Jf8M7rY+8cKjMoKCMzo/Fs 5/MQ== X-Forwarded-Encrypted: i=1; AJvYcCWX1dFjxwOeg/BsaO4kj35m2Rg5FKaiIP06kn+nEuU/4Vp47oCEh5GA4GaNTcvN2RwskwCHWTuuPv0fJrkIUwixa+qzy6ubGrje3JN2Igy4c5RbFsc= X-Gm-Message-State: AOJu0YxjAynt3ScGgqmKoVbhfAMXZhWQNly6JZ7LRAIauuv+VdiBMGm9 KfWWsSuZcqsJM01HorI4PDP2P2pZ27QyfnOtwjNytULIm5WWaEPU/jAYVVwKm1N77gH4Dy6jzqi XXQ== X-Google-Smtp-Source: AGHT+IHhzhjU3Z65Ec9zcL96WqENi83QMIcwPrprR++iZTSrPH87Hnrc6/v3lgb0Tgsr4AkAwll8yu22uIk= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:ec3:b0:663:ddc1:eab8 with SMTP id 00721157ae682-67a088f07bfmr790257b3.4.1722038078515; Fri, 26 Jul 2024 16:54:38 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:07 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-59-seanjc@google.com> Subject: [PATCH v12 58/84] KVM: RISC-V: Use kvm_faultin_pfn() when mapping pfns into the guest From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165440_374986_1A2C6B08 X-CRM114-Status: GOOD ( 11.06 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Convert RISC-V to __kvm_faultin_pfn()+kvm_release_faultin_page(), which are new APIs to consolidate arch code and provide consistent behavior across all KVM architectures. Signed-off-by: Sean Christopherson Reviewed-by: Andrew Jones Acked-by: Anup Patel --- arch/riscv/kvm/mmu.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 806f68e70642..f73d6a79a78c 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -601,6 +601,7 @@ int kvm_riscv_gstage_map(struct kvm_vcpu *vcpu, bool logging = (memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY)) ? true : false; unsigned long vma_pagesize, mmu_seq; + struct page *page; /* We need minimum second+third level pages */ ret = kvm_mmu_topup_memory_cache(pcache, gstage_pgd_levels); @@ -631,7 +632,7 @@ int kvm_riscv_gstage_map(struct kvm_vcpu *vcpu, /* * Read mmu_invalidate_seq so that KVM can detect if the results of - * vma_lookup() or gfn_to_pfn_prot() become stale priort to acquiring + * vma_lookup() or __kvm_faultin_pfn() become stale priort to acquiring * kvm->mmu_lock. * * Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs @@ -647,7 +648,7 @@ int kvm_riscv_gstage_map(struct kvm_vcpu *vcpu, return -EFAULT; } - hfn = gfn_to_pfn_prot(kvm, gfn, is_write, &writable); + hfn = kvm_faultin_pfn(vcpu, gfn, is_write, &writable, &page); if (hfn == KVM_PFN_ERR_HWPOISON) { send_sig_mceerr(BUS_MCEERR_AR, (void __user *)hva, vma_pageshift, current); @@ -681,11 +682,7 @@ int kvm_riscv_gstage_map(struct kvm_vcpu *vcpu, kvm_err("Failed to map in G-stage\n"); out_unlock: - if ((!ret || ret == -EEXIST) && writable) - kvm_set_pfn_dirty(hfn); - else - kvm_release_pfn_clean(hfn); - + kvm_release_faultin_page(kvm, page, ret && ret != -EEXIST, writable); spin_unlock(&kvm->mmu_lock); return ret; } From patchwork Fri Jul 26 23:52:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743547 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 692C6C3DA4A for ; Sat, 27 Jul 2024 00:24:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=eM04pE2oItbps9DzmNWUdptdfO1yLAvOtXUsQdNxOmg=; b=14WRp+SWolwDSqujhas/Wp7Vr1 wKF+Oi9Bel454hjHQLivbSPsZQALaoje1qCfzA/S1lNEvGtDD3fTg3DdQ8XibyKcrVgAO2vvEgUah Qi9wDviWVwrhptneUPoCNdr8YdT2fy2yYfPXmHsry7DIOobptm4K9fe0BNzKAQeW2dGmNZWcs+IXf UTYrcEcxbCldGlhN3YRXLr3SYjI4T/HcudO9ldQD/0LRLybwE/LhUOO2KqLukmOzQPEiTMeTJDgHY rwfrL0DqZpOQ2f9Kbx3UZ49px426+RP9ZPRuN2uCmtCmFb4v/AMSz9wPGbnD7/pObvoweHdv8CMbc iytlNXqQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVEj-00000005gBi-005R; Sat, 27 Jul 2024 00:24:37 +0000 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlm-00000005QnR-0Dy9 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:44 +0000 Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1fc52d3c76eso12300865ad.3 for ; Fri, 26 Jul 2024 16:54:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038081; x=1722642881; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=eM04pE2oItbps9DzmNWUdptdfO1yLAvOtXUsQdNxOmg=; b=uZNIDEchj1DUpSbAExGQ2gxdUyZ5XEFg43Ir2gS9nJJQQQaFG3FUUDejjwRrhZDKFE bvRpyKj7seBMkPDNMbMVx8P80bvD/UzxH3fjCGf9NMGaoeKaE2Fhaj5Nv17Je21cXySH JyXY/9ANtL3fGhU3TqrOADJoc90XTprKVm6nVZ19gvLwymJWrhLooIEb8MiB4NDpUyrO Hxhn3wq3tPahUd7ZsFA7GIhyVDy8XQ/S8BHfZrQOOMtCSnN4toEkNftRm7Y0wtmWacDP Z3DpuLx6MZ+LzSgaeJUCXLH9VRtQXPo+257A2WZzC3X5JY8bIo0vehH6kPdIiYLXTqA1 dIfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038081; x=1722642881; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=eM04pE2oItbps9DzmNWUdptdfO1yLAvOtXUsQdNxOmg=; b=Np/9fcfmV/LNO50q0E9Vof4UZlhR74tQGqvKv43zXk4gcqzM+efvNT9StT0yYj/N04 /u/CGK1vKPTjJG06JQrN97bezRcC5L9UxhuoljstSrDzqyiVn2L+cwTLzks9JLu6rpUQ UK0os/scCO4J4+ej7rFV0pzB75tSKgqoUEl1Db93xvYhDQD8tS1slg2A/gyELyv3eRSH uR6F7F4isLDKcIiE7/HUg0TT7Jxr1rpu/rwWRMqR/qoVqhnH50DBaA2nEd2G8988ZDYp gADGYPV//ZkkDxgM1iECOsNW46SHVZBICt4KPhnb7oTeXDcI74Xjk4oaypRAxc4VvqHA Zbdw== X-Forwarded-Encrypted: i=1; AJvYcCWjVi51OYJ/+f9VpLVNaeCi36c0829MvS9fZTDvZl9M3evQaWSzuLeBjam4a3HbfSeEfeGLRJ9HES8Vrg1b2EPTOYfa+1Wc0NBhjxcXzXDlveJQr7k= X-Gm-Message-State: AOJu0YzAI4DSUhpuuQJERNGLPDhRId7hopTjNFcpKqE0hAF84L0qrYr7 K/w4Yi5yDFI3gcJhFEiru31LBNWyYH+13vNDlbKK7PS1ZdPZUXFM65MtZ9qgz0gOxo1drN4OOsb FGw== X-Google-Smtp-Source: AGHT+IHO8zzuGkjlMSTl8Rqd7iW1pR8+tgiNg2ojfeAF3X7ugPbzXNjRAQyOFKQ91J/aSe/NwOtEVPH7LtU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:db11:b0:1f8:44f4:efd9 with SMTP id d9443c01a7336-1ff047d0af0mr1044175ad.2.1722038080667; Fri, 26 Jul 2024 16:54:40 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:08 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-60-seanjc@google.com> Subject: [PATCH v12 59/84] KVM: PPC: Use __kvm_faultin_pfn() to handle page faults on Book3s HV From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165442_139935_97580C9F X-CRM114-Status: GOOD ( 13.90 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Replace Book3s HV's homebrewed fault-in logic with __kvm_faultin_pfn(), which functionally does pretty much the exact same thing. Note, when the code was written, KVM indeed didn't do fast GUP without "!atomic && !async", but that has long since changed (KVM tries fast GUP for all writable mappings). Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/book3s_64_mmu_hv.c | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/arch/powerpc/kvm/book3s_64_mmu_hv.c b/arch/powerpc/kvm/book3s_64_mmu_hv.c index 2f1d58984b41..f305395cf26e 100644 --- a/arch/powerpc/kvm/book3s_64_mmu_hv.c +++ b/arch/powerpc/kvm/book3s_64_mmu_hv.c @@ -603,27 +603,10 @@ int kvmppc_book3s_hv_page_fault(struct kvm_vcpu *vcpu, write_ok = writing; hva = gfn_to_hva_memslot(memslot, gfn); - /* - * Do a fast check first, since __gfn_to_pfn_memslot doesn't - * do it with !atomic && !async, which is how we call it. - * We always ask for write permission since the common case - * is that the page is writable. - */ - if (get_user_page_fast_only(hva, FOLL_WRITE, &page)) { - write_ok = true; - } else { - /* Call KVM generic code to do the slow-path check */ - pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL, - writing, &write_ok); - if (is_error_noslot_pfn(pfn)) - return -EFAULT; - page = NULL; - if (pfn_valid(pfn)) { - page = pfn_to_page(pfn); - if (PageReserved(page)) - page = NULL; - } - } + pfn = __kvm_faultin_pfn(memslot, gfn, writing ? FOLL_WRITE : 0, + &write_ok, &page); + if (is_error_noslot_pfn(pfn)) + return -EFAULT; /* * Read the PTE from the process' radix tree and use that From patchwork Fri Jul 26 23:52:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743548 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E14D8C3DA4A for ; Sat, 27 Jul 2024 00:25:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=6xYXr8Us14FXXKODtWRB0Ht9lxWZb28t4tAXkZ5fXOk=; b=HJoAv+odV9PG4wauHgA2r6UW7h GyrkySXmUP+IibtKvUpzy00viYLqZYHBbr1dPbLbmOlKOP9eM7K/vHoOcl7X719Cf8XyZxTQ1bAVJ Iq/UZNoOAa1d7MQaOALxJsIl43Hgt3o6P9YzIhvuvL9jJQhUMoH9Vc//vpG8WBcbwHRcuyfBrsx1A 6MKEYy9nIJ1mOJPqNb7kurT8HzaAKVZFdtH+TkvwNiQBqg0ymlUEJBpnCnYDcgcRd2nICJ5hKyoR7 m4yW1WMFynX2MrhNmdlBXFUhXSLa1B4BdhB+zgA2wFNXiHJbFMiRsKhGpeRhq8wPdVqslFBNyTbJO xMx/AhCw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVFD-00000005gRr-3tie; Sat, 27 Jul 2024 00:25:08 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlo-00000005Qp0-4C0p for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:46 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-e02a4de4f4eso434383276.1 for ; Fri, 26 Jul 2024 16:54:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038083; x=1722642883; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=6xYXr8Us14FXXKODtWRB0Ht9lxWZb28t4tAXkZ5fXOk=; b=WzmDaC20mfV/JEaz5zEA97NXbkZnXiX9MHnJ2J1GXX2Pur0iJha5Tc7/fNVKvGzQ/7 QZpSn4v38PEJUZWLeiQVyAhgWA9kNpJWfCkcVmZ+Tx5+YpsIxpF9zdCMkV+jlNFwQep9 zDHQ3t5ebywROlZ3waUBUzh/uj2ORPajdyRb75AL5u+i3y5mg4eYqHclXwMMV6Mv120g BoRWbn55aN0MxsAT/bhup79INbZGvCvkNm2zfAYgMB5tkXU0/tgSzPZJuEYY9wrNGpHF UgvknPaOG9vPznhJBS+OfQIzD7tMWE2hwV2HZ6kEMVomGHrDwaYdXg25iPdRCF0j/ric kfpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038083; x=1722642883; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6xYXr8Us14FXXKODtWRB0Ht9lxWZb28t4tAXkZ5fXOk=; b=wlhhlXRhwX8xxECq4SR4da2iB6vYZqLWC69Kbkf3b7VbiJb3KheeI3PPSCXd6QaVYO aKFfQEO2Jz2U+Qx7SJL4WlX/Uo7JDAtD25q6S3QQZC8gDoTObZgTPPEPRg86CwiJhfRq 33gMg1EEBy+WWooZlbvsiEcaEc+ZvYZPzWKj4kTYCquZL15OoAPLAHEtm1BB03x+HKKu 0neo7bkaJ0K6EGfUl/kYt97GFs8eE2PQxcu/UY5A3qTOb5uc2An8B/7qrAPhyrXpPO6C +wRnwADTtqOJesKqt17wFwLGHGfIOotkiH/xpmuVrxHd6irCQKf/zOLJJSJdgm3HSeAo 4Z9A== X-Forwarded-Encrypted: i=1; AJvYcCVesN7f3+ca8BCrIuMOCHQG3xNDVfjSMJWeKBIT65GS8nBwr1PlWd6gByRBrP8gCLldyBQ0pQ+BhfXiydV7d6zgaW7Cr7q20Rlz8LZzHIz8Ut7htRw= X-Gm-Message-State: AOJu0YyVSYApUCgiTN9y+yzvjAT7NQdS9gIy4NLWkAQ2xaDVOQ7cOTQ+ aSQCe6G1tDoUc/kT2UAXQKyLB5yjJkozVjYyQkjx8Rab3RjTI9MrtGOwnQvxElX93hnOlyEhXPF THQ== X-Google-Smtp-Source: AGHT+IE8wacjAGm1xSlUK/1K8X0GxeW+L1fXTQ/Y1AQI/5HhDGzzQTYAcJk3V2f1As7zQmLQNuyAyAYHMio= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:8c02:0:b0:e05:f1ad:a139 with SMTP id 3f1490d57ef6-e0b545c6203mr2068276.11.1722038082801; Fri, 26 Jul 2024 16:54:42 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:09 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-61-seanjc@google.com> Subject: [PATCH v12 60/84] KVM: PPC: Use __kvm_faultin_pfn() to handle page faults on Book3s Radix From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165445_127640_486261FF X-CRM114-Status: GOOD ( 14.55 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Replace Book3s Radix's homebrewed (read: copy+pasted) fault-in logic with __kvm_faultin_pfn(), which functionally does pretty much the exact same thing. Note, when the code was written, KVM indeed didn't do fast GUP without "!atomic && !async", but that has long since changed (KVM tries fast GUP for all writable mappings). Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/book3s_64_mmu_radix.c | 29 +++++--------------------- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/arch/powerpc/kvm/book3s_64_mmu_radix.c b/arch/powerpc/kvm/book3s_64_mmu_radix.c index 8304b6f8fe45..14891d0a3b73 100644 --- a/arch/powerpc/kvm/book3s_64_mmu_radix.c +++ b/arch/powerpc/kvm/book3s_64_mmu_radix.c @@ -829,40 +829,21 @@ int kvmppc_book3s_instantiate_page(struct kvm_vcpu *vcpu, unsigned long mmu_seq; unsigned long hva, gfn = gpa >> PAGE_SHIFT; bool upgrade_write = false; - bool *upgrade_p = &upgrade_write; pte_t pte, *ptep; unsigned int shift, level; int ret; bool large_enable; + kvm_pfn_t pfn; /* used to check for invalidations in progress */ mmu_seq = kvm->mmu_invalidate_seq; smp_rmb(); - /* - * Do a fast check first, since __gfn_to_pfn_memslot doesn't - * do it with !atomic && !async, which is how we call it. - * We always ask for write permission since the common case - * is that the page is writable. - */ hva = gfn_to_hva_memslot(memslot, gfn); - if (!kvm_ro && get_user_page_fast_only(hva, FOLL_WRITE, &page)) { - upgrade_write = true; - } else { - unsigned long pfn; - - /* Call KVM generic code to do the slow-path check */ - pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL, - writing, upgrade_p); - if (is_error_noslot_pfn(pfn)) - return -EFAULT; - page = NULL; - if (pfn_valid(pfn)) { - page = pfn_to_page(pfn); - if (PageReserved(page)) - page = NULL; - } - } + pfn = __kvm_faultin_pfn(memslot, gfn, writing ? FOLL_WRITE : 0, + &upgrade_write, &page); + if (is_error_noslot_pfn(pfn)) + return -EFAULT; /* * Read the PTE from the process' radix tree and use that From patchwork Fri Jul 26 23:52:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743549 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 23B67C3DA49 for ; Sat, 27 Jul 2024 00:25:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=fnzANaCry6VTBaaBvIzLI2Urcy+eNpGQjPUes1ZSEvM=; b=PIVeNOmd8UKFgnpjWiXyNgpoXD CPgQu50THwzwMPsPJfTAAwpvhM0JGBhUAts0WFT0IeUQiv7rwF8gZGExTmdTXa4NL2c5KoOJCbpLn ntRHzetptivHc/MRukMOIEOQ0LdEgIpUC0+D0jLokgK2nd2VbbRD+786sAaYvFGTon7pbp9LqFvyr QNaiFgLwlykEMoQubc0MwVYFOb/qxl8O1HjRdVU/hyKvF4Gz9JUemNUG4CRHmaGzZqpJVGTqXk5P2 X2SHEqWgyqmNvZMkZjFK/p1t4m+mBzFgBCmy7+RHbhU8OQb3+L2uzwxuq/NUnyBq08lgRTb2KpEs7 VxFksGyw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVFf-00000005geg-3eIM; Sat, 27 Jul 2024 00:25:35 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlq-00000005Qqa-1JaL for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:48 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1fd5fe96cfeso11130645ad.0 for ; Fri, 26 Jul 2024 16:54:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038085; x=1722642885; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=fnzANaCry6VTBaaBvIzLI2Urcy+eNpGQjPUes1ZSEvM=; b=gDdumSYISEt5BOnZYxIyX7s2c6Qd5hL0KA7Uxo/1LdrtTWYbg1+YL8du2a2IIKcQcE 83TESYnHWTqSGwhrd+wnqX8zrJOoB1d9lbXMZI6CUFnK+/irKbJcJkG86HDUbluD3asz nfM1jpL8zydmmfk3ix9vqwFqvmmzs5C/ZZjIwynOHLcTCqxfMtdbAoN0PBeLfTMPRp+4 HbgpILMDdoh/rOLUBCkLEX/mRJ/LfmLzQcPlxi9v86QK6jiV5SKCzuiXhG/qSWrOAkB3 WdckdmOyaUqrcN4tPNcFXyx7YEHepH33HY7kQkaqizvVqctYPwMS6lG5cUGi267xUj2p 5+Xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038085; x=1722642885; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=fnzANaCry6VTBaaBvIzLI2Urcy+eNpGQjPUes1ZSEvM=; b=bihAPNggjufvKQ1083MSZe0HJPGRE2a70ffTUtm/AB0iBPYANTZSuxyz/jB1hHPsa0 i2GJPcIYCZuCnqshKkORsI3GHl6L1Ae5QueXuh87r6HCSUhFQxmMbrkb+m6n/8Wqes7d dO7JjiDEy1s0VNBoZv/UVA+/SHHMaQ7NXrXDz9zvpYs+FzP5LfA9vyFBHfz/2/ITEh0c R6IPpu9Wx69la2bqoJqhJnMMElQI4fZxC4I1h3EV73qsRAp+c7nxY3NWri/LSE6BFRBE Q9KUfprp8hYHuMUi6hl8ArnjuU+T59KfWvPVbLp4JSZ9UBuJWDZ4ZHIagL2H6jpP0wHW O00A== X-Forwarded-Encrypted: i=1; AJvYcCXX1mc24KgnzPoBtT7x5G16BJKuB36lyxPDajy4FWv7a9KKButFVQnkmLD6676Dc72HttdqFS35G8Slm37YCd699IHSfuQOtFhhLWBK3T4Riys1qn0= X-Gm-Message-State: AOJu0Yx0KqA6reHdTk39nA579L0jyMbvI2jVkqKMR4z3JL6wMYHpVbFS 4EorSzeYqvnElAtP/mrhGN0Poss4ICpNQKJuRANlBzPPfFhnx7I4pfvLOBLRI836Z6+6JNOL80G W8Q== X-Google-Smtp-Source: AGHT+IHmHH2jIucYiOpERpDS4ASgx5TvMHlaOIj1nvo/BSmO2/veDdrUl5Q72dqDHu1wFellCWvE9VI+k4Q= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:11d0:b0:1fb:6b70:8f6a with SMTP id d9443c01a7336-1ff04917c5dmr841815ad.11.1722038084875; Fri, 26 Jul 2024 16:54:44 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:10 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-62-seanjc@google.com> Subject: [PATCH v12 61/84] KVM: PPC: Drop unused @kvm_ro param from kvmppc_book3s_instantiate_page() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165446_396578_397DF42F X-CRM114-Status: GOOD ( 14.52 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Drop @kvm_ro from kvmppc_book3s_instantiate_page() as it is now only written, and never read. No functional change intended. Signed-off-by: Sean Christopherson --- arch/powerpc/include/asm/kvm_book3s.h | 2 +- arch/powerpc/kvm/book3s_64_mmu_radix.c | 6 ++---- arch/powerpc/kvm/book3s_hv_nested.c | 4 +--- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/arch/powerpc/include/asm/kvm_book3s.h b/arch/powerpc/include/asm/kvm_book3s.h index 3e1e2a698c9e..34e8f0b7b345 100644 --- a/arch/powerpc/include/asm/kvm_book3s.h +++ b/arch/powerpc/include/asm/kvm_book3s.h @@ -203,7 +203,7 @@ extern bool kvmppc_hv_handle_set_rc(struct kvm *kvm, bool nested, extern int kvmppc_book3s_instantiate_page(struct kvm_vcpu *vcpu, unsigned long gpa, struct kvm_memory_slot *memslot, - bool writing, bool kvm_ro, + bool writing, pte_t *inserted_pte, unsigned int *levelp); extern int kvmppc_init_vm_radix(struct kvm *kvm); extern void kvmppc_free_radix(struct kvm *kvm); diff --git a/arch/powerpc/kvm/book3s_64_mmu_radix.c b/arch/powerpc/kvm/book3s_64_mmu_radix.c index 14891d0a3b73..b3e6e73d6a08 100644 --- a/arch/powerpc/kvm/book3s_64_mmu_radix.c +++ b/arch/powerpc/kvm/book3s_64_mmu_radix.c @@ -821,7 +821,7 @@ bool kvmppc_hv_handle_set_rc(struct kvm *kvm, bool nested, bool writing, int kvmppc_book3s_instantiate_page(struct kvm_vcpu *vcpu, unsigned long gpa, struct kvm_memory_slot *memslot, - bool writing, bool kvm_ro, + bool writing, pte_t *inserted_pte, unsigned int *levelp) { struct kvm *kvm = vcpu->kvm; @@ -931,7 +931,6 @@ int kvmppc_book3s_radix_page_fault(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot; long ret; bool writing = !!(dsisr & DSISR_ISSTORE); - bool kvm_ro = false; /* Check for unusual errors */ if (dsisr & DSISR_UNSUPP_MMU) { @@ -984,7 +983,6 @@ int kvmppc_book3s_radix_page_fault(struct kvm_vcpu *vcpu, ea, DSISR_ISSTORE | DSISR_PROTFAULT); return RESUME_GUEST; } - kvm_ro = true; } /* Failed to set the reference/change bits */ @@ -1002,7 +1000,7 @@ int kvmppc_book3s_radix_page_fault(struct kvm_vcpu *vcpu, /* Try to insert a pte */ ret = kvmppc_book3s_instantiate_page(vcpu, gpa, memslot, writing, - kvm_ro, NULL, NULL); + NULL, NULL); if (ret == 0 || ret == -EAGAIN) ret = RESUME_GUEST; diff --git a/arch/powerpc/kvm/book3s_hv_nested.c b/arch/powerpc/kvm/book3s_hv_nested.c index 05f5220960c6..771173509617 100644 --- a/arch/powerpc/kvm/book3s_hv_nested.c +++ b/arch/powerpc/kvm/book3s_hv_nested.c @@ -1527,7 +1527,6 @@ static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu, unsigned long n_gpa, gpa, gfn, perm = 0UL; unsigned int shift, l1_shift, level; bool writing = !!(dsisr & DSISR_ISSTORE); - bool kvm_ro = false; long int ret; if (!gp->l1_gr_to_hr) { @@ -1607,7 +1606,6 @@ static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu, ea, DSISR_ISSTORE | DSISR_PROTFAULT); return RESUME_GUEST; } - kvm_ro = true; } /* 2. Find the host pte for this L1 guest real address */ @@ -1629,7 +1627,7 @@ static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu, if (!pte_present(pte) || (writing && !(pte_val(pte) & _PAGE_WRITE))) { /* No suitable pte found -> try to insert a mapping */ ret = kvmppc_book3s_instantiate_page(vcpu, gpa, memslot, - writing, kvm_ro, &pte, &level); + writing, &pte, &level); if (ret == -EAGAIN) return RESUME_GUEST; else if (ret) From patchwork Fri Jul 26 23:52:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743550 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C4872C3DA49 for ; Sat, 27 Jul 2024 00:26:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=oFlnnuKhNT9l8QiAImtf2ecgGSNucZgQMIB0EjCSaJA=; b=kMnMWrY4kEU8i7srKKUDs52bdD mcZkv28/UBkohbNdNamgwWIyx4atX3hMyLMpknnLZeZuzKgu5XaVxPtU0mQa+IjiMThWIRQVWbLiO yzxffzk0lUQLYYLA5XkDF4YWNsAG4ww9jQ0fJ/1bUrC5c4SSrXA+TeAqy5zrUw6vTibmA8QEaT5cS 3t0Awc+GHHqvtMxBMenFdl5aMlluP2Hh6Uw12f2+X9ebACjOKlH5vLDV4FRB1hgCkhy/fd4z3pJ0f jpl4UIRqLfUF0rx7Z4ucPVir0FJZd+oBGNwhCf9ZPQKv/7J1s8CD+pmo34OHqIJFmFFBG3WNU8wXn SYPCMptg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVG3-00000005glj-1CNh; Sat, 27 Jul 2024 00:25:59 +0000 Received: from mail-pj1-x104a.google.com ([2607:f8b0:4864:20::104a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUls-00000005QsH-0D8Y for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:49 +0000 Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-2cb81c562edso1499011a91.2 for ; Fri, 26 Jul 2024 16:54:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038087; x=1722642887; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=oFlnnuKhNT9l8QiAImtf2ecgGSNucZgQMIB0EjCSaJA=; b=EMwhLJunDrk3NB2hn/Boeh5Fn/qHAb9ITI39S+dEzdcmoT+THiAX+15JlSddAh1Sd9 si5tj1z58M7KGA/vcZrvF6X0zigGCNdHPbfxApmzsUZnPrkUPSaglDQ8xB7uPA3sR8OI vtEJkWDpMUO3aBnxG9w7/ROooekgtXv0jZkaoO8olC5HHqElBX4aeb/RNYc+q5iy4CP1 vmquzp2u+LI1ZrmA66xXzACZAtgcOvc2aBO/6JHSARjiQBiLPTd8nWm9/EqfPXQntXp+ bwFWvzy8Cs8ApdSa3Dt3rEp2sovh6NWoDhcS/jDeBSNxe1KgmgykLUkjsGVtG+tCda0n f7pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038087; x=1722642887; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oFlnnuKhNT9l8QiAImtf2ecgGSNucZgQMIB0EjCSaJA=; b=LHJ8n+1CARCetZw8nsyMb6UG+JrMv7mWcBtbO3SqxvGkxMpQVMAgibUi5yMpBHYSbi y1tJae8YHjcIC4uLLfwcFx3Ek+avbOJk7TbrXfHmSYhJHtaWlUE37gwQjIPDmJIk6e6B ipEidbLTwOziBT5O6HzfBSoNbn3+kqvkyBDpgoZ9/tHDCBgLcGUAlcmZRFLth7f7l+Xx CgnRGaTw3SzIkRKDtPbaAwsFHfoDG6UGP9uLX9vsAVVC4ul2m8VS2ROF2W3OMXecU5pM nBBRYUE8brJzcVvdZKD2FYBdNGWB8EQa0bjN9h98mOZp1pasMseGf+iFUCGIaAbZ4EG4 3tTA== X-Forwarded-Encrypted: i=1; AJvYcCX8MZnDjXRAIu2hSLtXdY+hbYe8JPTocFcEoefjlDCAoty3JkSPJTvtFL0FHckqM/8djT0zLHy/GoEEWoCaKa+79PZdBQGs4iC7U9EQVKMulJb28tU= X-Gm-Message-State: AOJu0YzT0rtHuDmWNX0cxr8uDPWUvPJMdX7Zc1SCIK5AIP3hzVV7QtJ+ B5WV5gNmmtV6cdLxv5S1EBWME9xHWJ+G6Za0U9aXtRHyDIWQxN22Sf2ilUMGpsx93N5x5UceDYH fVg== X-Google-Smtp-Source: AGHT+IHpYPU/r0yyvqEOzDrNXs+L4GUiZCI45y4AH9hPwSk8nxMjnSbjsFMJ0nzRNCC8+VosuScTVNX7UfA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:4f8e:b0:2c9:9a89:a2ae with SMTP id 98e67ed59e1d1-2cf7d4317d2mr2552a91.0.1722038086790; Fri, 26 Jul 2024 16:54:46 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:11 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-63-seanjc@google.com> Subject: [PATCH v12 62/84] KVM: PPC: Book3S: Mark "struct page" pfns dirty/accessed after installing PTE From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165448_115746_1770F2E2 X-CRM114-Status: GOOD ( 11.29 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark pages/folios dirty/accessed after installing a PTE, and more specifically after acquiring mmu_lock and checking for an mmu_notifier invalidation. Marking a page/folio dirty after it has been written back can make some filesystems unhappy (backing KVM guests will such filesystem files is uncommon, and the race is minuscule, hence the lack of complaints). See the link below for details. This will also allow converting Book3S to kvm_release_faultin_page(), which requires that mmu_lock be held (for the aforementioned reason). Link: https://lore.kernel.org/all/cover.1683044162.git.lstoakes@gmail.com Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/book3s_64_mmu_host.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/powerpc/kvm/book3s_64_mmu_host.c b/arch/powerpc/kvm/book3s_64_mmu_host.c index bc6a381b5346..d0e4f7bbdc3d 100644 --- a/arch/powerpc/kvm/book3s_64_mmu_host.c +++ b/arch/powerpc/kvm/book3s_64_mmu_host.c @@ -121,13 +121,10 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte, vpn = hpt_vpn(orig_pte->eaddr, map->host_vsid, MMU_SEGSIZE_256M); - kvm_set_pfn_accessed(pfn); if (!orig_pte->may_write || !writable) rflags |= PP_RXRX; - else { + else mark_page_dirty(vcpu->kvm, gfn); - kvm_set_pfn_dirty(pfn); - } if (!orig_pte->may_execute) rflags |= HPTE_R_N; @@ -202,8 +199,11 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte, } out_unlock: + if (!orig_pte->may_write || !writable) + kvm_release_pfn_clean(pfn); + else + kvm_release_pfn_dirty(pfn); spin_unlock(&kvm->mmu_lock); - kvm_release_pfn_clean(pfn); if (cpte) kvmppc_mmu_hpte_cache_free(cpte); From patchwork Fri Jul 26 23:52:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743551 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D2AB6C3DA4A for ; Sat, 27 Jul 2024 00:26:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4jqep9wI+WruRy0z2TZYbUEBq8RbIq5j1OfKKvbW4+Y=; b=AfEzp0B5AHl8Qku26+vG59wGX7 IQeeJENqpueBzuzZTbKZOBEGGOpDUK5KarUvNxQEAu1r6U+00zX+mIXQGbNK9FIPoESTCZPn5Il+E I1oBLEoTHKx+TadG7F7TLvEo04tWqGenKpER+MlcTQ9ZBpaDhMPWOvSLTpNp4u4OKOXp46R5E32Y7 +d0V4Ol2W+UW/iY6EioSCPom3JS+b+24Qs5fR9g/aUXmkvn3xY7e0k09N/jLIsSltYdaZfPgaB+i4 1SoqBM/fHViWw+/nqgrKytFFjG6Ip69NPWdXGh+8rYQxFcNOlCs1ILUMkzmN324eaCuM5Ax2PlORf CkETyBZg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVGT-00000005gxB-0BbH; Sat, 27 Jul 2024 00:26:25 +0000 Received: from mail-pj1-x1049.google.com ([2607:f8b0:4864:20::1049]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlu-00000005QtN-1TL8 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:52 +0000 Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2cb656e4d97so1766271a91.2 for ; Fri, 26 Jul 2024 16:54:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038089; x=1722642889; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=4jqep9wI+WruRy0z2TZYbUEBq8RbIq5j1OfKKvbW4+Y=; b=VCKZxIMyozw/M1ysU0bWtw0NrDRR/bhmjKd89m8jqq5OD3+7cW/UoSW3U/4GFWMv1Q V04ak3KNqFSWoqtHdzQM+38BcI8oDOeLZhmor9tC4RMk2sWaCXFmewAf4ilPFmHlNqZU bvJHcdh586b8/spNuaCK7vbRBXYZW7FAol6YRkKUR86ol3Fs8xfwfEJkF2eq5ufnGyUU JmsTsTSurgmFOq0ETih+SMijJl8ll3IYXI3r6Q0bmxXs5yuYu4IpTxJiNv0IlnJemKgX iYtoxh3r6UoKXr67vpB0vDZQABx8iaCMWJxftBl0hD64/VnN3VwXPS4z6isuBO8AnDL4 DvDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038089; x=1722642889; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4jqep9wI+WruRy0z2TZYbUEBq8RbIq5j1OfKKvbW4+Y=; b=toD+Kx+QA2pzlgz1CG8Z2L6L3PjgJ0PihvgPGziiHmTAFv15vSSmXkzl4wrY+LQW2z JhgaVtPoLE+DTtl6G8kJcJYxKkOtPk8Te6cE36xjsKByGgdrvRzH2YkU+6YOUC5fuqwn HdubVtaFLqMMzzfPk6njLd2bCgYuINzOdnpriMFcI2xURy/Y5jZoDrasmwbA+R2Pgf/8 6P7AA+CWiIu1pSY+lRu9zxOUNBGfB1q2RxYmuDiFPRrmNjlS1dyJ2drZvKPUDtwleiN/ cKDmiqx/0rF3QgDv64Vjh/inY+90lJTYYuNgQxDOoNLWtgLc8bkFu/jMLAKnTXPtB0Pb yrHw== X-Forwarded-Encrypted: i=1; AJvYcCWBbIBGQQHUr+gXx/ENeb0I7h4CqibeB4w4NvEDGdO5YZyYGyptCKjrQ8JgSmfj1hp1cMcGeuJED2Lt4wH/q6mYIU6IeWyBBMS0yJ4WssaoIYztNbA= X-Gm-Message-State: AOJu0Ywc1EN1XxnWVaS2/LtZpzdWImIMB/PFRG27ZpCv8sSvZuDCJM05 fBUFbJu1NWBCpgatjltWITwh4IPOFSoVYjMfG7Gb5QRwycMoRzi3vyYz75CvWEGyUyr7mKPOp38 EKA== X-Google-Smtp-Source: AGHT+IFasJAr04PTZrf6XUkQxxUmiACppU16Z34UUuO+d3NYseQpUQCzHrJDN41FkLsUjVBlqZCxQJJvuE8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:6fe5:b0:2c9:98bc:3584 with SMTP id 98e67ed59e1d1-2cf7e83a1famr42409a91.6.1722038088823; Fri, 26 Jul 2024 16:54:48 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:12 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-64-seanjc@google.com> Subject: [PATCH v12 63/84] KVM: PPC: Use kvm_faultin_pfn() to handle page faults on Book3s PR From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165450_479489_9FDC0388 X-CRM114-Status: GOOD ( 14.60 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Convert Book3S PR to __kvm_faultin_pfn()+kvm_release_faultin_page(), which are new APIs to consolidate arch code and provide consistent behavior across all KVM architectures. Signed-off-by: Sean Christopherson --- arch/powerpc/include/asm/kvm_book3s.h | 2 +- arch/powerpc/kvm/book3s.c | 7 ++++--- arch/powerpc/kvm/book3s_32_mmu_host.c | 7 ++++--- arch/powerpc/kvm/book3s_64_mmu_host.c | 10 +++++----- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/arch/powerpc/include/asm/kvm_book3s.h b/arch/powerpc/include/asm/kvm_book3s.h index 34e8f0b7b345..343c10dda80f 100644 --- a/arch/powerpc/include/asm/kvm_book3s.h +++ b/arch/powerpc/include/asm/kvm_book3s.h @@ -235,7 +235,7 @@ extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat, extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr); extern int kvmppc_emulate_paired_single(struct kvm_vcpu *vcpu); extern kvm_pfn_t kvmppc_gpa_to_pfn(struct kvm_vcpu *vcpu, gpa_t gpa, - bool writing, bool *writable); + bool writing, bool *writable, struct page **page); extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev, unsigned long *rmap, long pte_index, int realmode); extern void kvmppc_update_dirty_map(const struct kvm_memory_slot *memslot, diff --git a/arch/powerpc/kvm/book3s.c b/arch/powerpc/kvm/book3s.c index ff6c38373957..d79c5d1098c0 100644 --- a/arch/powerpc/kvm/book3s.c +++ b/arch/powerpc/kvm/book3s.c @@ -422,7 +422,7 @@ int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu) EXPORT_SYMBOL_GPL(kvmppc_core_prepare_to_enter); kvm_pfn_t kvmppc_gpa_to_pfn(struct kvm_vcpu *vcpu, gpa_t gpa, bool writing, - bool *writable) + bool *writable, struct page **page) { ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM; gfn_t gfn = gpa >> PAGE_SHIFT; @@ -437,13 +437,14 @@ kvm_pfn_t kvmppc_gpa_to_pfn(struct kvm_vcpu *vcpu, gpa_t gpa, bool writing, kvm_pfn_t pfn; pfn = (kvm_pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT; - get_page(pfn_to_page(pfn)); + *page = pfn_to_page(pfn); + get_page(*page); if (writable) *writable = true; return pfn; } - return gfn_to_pfn_prot(vcpu->kvm, gfn, writing, writable); + return kvm_faultin_pfn(vcpu, gfn, writing, writable, page); } EXPORT_SYMBOL_GPL(kvmppc_gpa_to_pfn); diff --git a/arch/powerpc/kvm/book3s_32_mmu_host.c b/arch/powerpc/kvm/book3s_32_mmu_host.c index 4b3a8d80cfa3..5b7212edbb13 100644 --- a/arch/powerpc/kvm/book3s_32_mmu_host.c +++ b/arch/powerpc/kvm/book3s_32_mmu_host.c @@ -130,6 +130,7 @@ extern char etext[]; int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte, bool iswrite) { + struct page *page; kvm_pfn_t hpaddr; u64 vpn; u64 vsid; @@ -145,7 +146,7 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte, bool writable; /* Get host physical address for gpa */ - hpaddr = kvmppc_gpa_to_pfn(vcpu, orig_pte->raddr, iswrite, &writable); + hpaddr = kvmppc_gpa_to_pfn(vcpu, orig_pte->raddr, iswrite, &writable, &page); if (is_error_noslot_pfn(hpaddr)) { printk(KERN_INFO "Couldn't get guest page for gpa %lx!\n", orig_pte->raddr); @@ -232,7 +233,7 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte, pte = kvmppc_mmu_hpte_cache_next(vcpu); if (!pte) { - kvm_release_pfn_clean(hpaddr >> PAGE_SHIFT); + kvm_release_page_unused(page); r = -EAGAIN; goto out; } @@ -250,7 +251,7 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte, kvmppc_mmu_hpte_cache_map(vcpu, pte); - kvm_release_pfn_clean(hpaddr >> PAGE_SHIFT); + kvm_release_page_clean(page); out: return r; } diff --git a/arch/powerpc/kvm/book3s_64_mmu_host.c b/arch/powerpc/kvm/book3s_64_mmu_host.c index d0e4f7bbdc3d..be20aee6fd7d 100644 --- a/arch/powerpc/kvm/book3s_64_mmu_host.c +++ b/arch/powerpc/kvm/book3s_64_mmu_host.c @@ -88,13 +88,14 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte, struct hpte_cache *cpte; unsigned long gfn = orig_pte->raddr >> PAGE_SHIFT; unsigned long pfn; + struct page *page; /* used to check for invalidations in progress */ mmu_seq = kvm->mmu_invalidate_seq; smp_rmb(); /* Get host physical address for gpa */ - pfn = kvmppc_gpa_to_pfn(vcpu, orig_pte->raddr, iswrite, &writable); + pfn = kvmppc_gpa_to_pfn(vcpu, orig_pte->raddr, iswrite, &writable, &page); if (is_error_noslot_pfn(pfn)) { printk(KERN_INFO "Couldn't get guest page for gpa %lx!\n", orig_pte->raddr); @@ -199,10 +200,9 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte, } out_unlock: - if (!orig_pte->may_write || !writable) - kvm_release_pfn_clean(pfn); - else - kvm_release_pfn_dirty(pfn); + /* FIXME: Don't unconditionally pass unused=false. */ + kvm_release_faultin_page(kvm, page, false, + orig_pte->may_write && writable); spin_unlock(&kvm->mmu_lock); if (cpte) kvmppc_mmu_hpte_cache_free(cpte); From patchwork Fri Jul 26 23:52:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743552 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EB865C3DA4A for ; Sat, 27 Jul 2024 00:26:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=XEv6FisOGr6vpV2eziQMsIEoEavMf3RHPa1v90mbDdU=; b=ITmlg6PYC5QSm4sCAiamrDTrjU QvHwnhC2zIdyyCnAVOwQTApKMCMt0LYvX8K0KSHAN9DcHqYXVB6aSUxQz5cwe3WDn6P/AXYSRN0HX 9vyZ+QtQyWSepKqDPQcV4H0SEC6R5AKGAX+fIzaKRwDkC3quum6FqaplWssTmXQQFZkvwNg6OFtTE iSFBSOLDhM/NNZilJk6wFzoh4fmkzQe4Qvh26Og7c5tsFwVzq/y76qMNcW5Grp69R5ryc71wTya8v tM7ZnO42MLPr0MjYMQihrw1wjDnpKmd374b3UDs5enxrBEICJxwnAQjJePmspMdk+df1JbCL0RYfS cX7bu7Yg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVGr-00000005h6H-21Vo; Sat, 27 Jul 2024 00:26:49 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUlv-00000005QvF-3Xg3 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:53 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dfa73db88dcso444062276.0 for ; Fri, 26 Jul 2024 16:54:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038090; x=1722642890; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=XEv6FisOGr6vpV2eziQMsIEoEavMf3RHPa1v90mbDdU=; b=EeCsWPS9kIjQeoy2YhVAeX8xthgjWzE22IQrjisj/hzoLhWw/cZNvxdgXKZd+ivJ5B dXklDq9YktuJK2q/IsJ231CG8P8ZBxPVuhoS1eaK+6KlYLxfWVAGEkmxL/akQFweDMvX CtagECM/ocgqShIgyoYH8oIEC1YyRUQZj2EI93Uot2LFbK3q0r8/mrr3IpuXro0JYRLc z4G7HavOIyyHLQt2fAaN0KbNm2VcHX1vpvP5D9/LRgSa7FYyrRYGEsBJgsHZUuQSYmOO L8ZTvt0xGAcJjM7n2ZX+hm5vY9MtMwG6e2IMwg/sDbtg7N0TaCZGdYImjosRDo18eZ+i mMRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038090; x=1722642890; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XEv6FisOGr6vpV2eziQMsIEoEavMf3RHPa1v90mbDdU=; b=UiT4w7WgNDmP9Nz7XVHsn5Jk+nQF95BV8LdD3nIMqsIzSq1vNUKPTguEJ11b/Nbj4X 7k72k3RHSbK38d5xj7LHYLcgpURco0BqLIP5HsYkOh2tKgPma1O624kunNImfiZ85ne4 dcTRQS8J61j7yqRDjwS8lBMnTh4+nTckjM1PE3qHA56KUTEMzB7Vs2WmtKr7EF7DAib0 86/KdxHAl9Wx08AMkpUuVMDBue8S68cIUA4SO3nUVEO7m/JSy7tXS+vNbEmGteEnkKcV jOP+hARQurJ1eNXZ/m2G23MYI7fQw+cc7rCTIMAg14OusfyHAPmh1Tfz6faaL8yK5mk2 o5MQ== X-Forwarded-Encrypted: i=1; AJvYcCVMkVtn6J4TtIt9vm1rTYlopia5QCgdODDa1CuNfg/SIWgJGEIMOiJiRVr98YwypnGpU3WwUEhPU18Oj/9ssIYma3tpFanjRPCZckaqVhMKFNkbdCI= X-Gm-Message-State: AOJu0YyTum4vf5DMlA2k3as9HnhYZWANXZGtffaZSQweT67nYRvwQtXK +YtdT15DcBafEb8njRUfyrQUfG1raeZK1EkhWAcWd65B0fOUQK3bpxCI0R+PdWn9Ar/UEzQLOsM fLA== X-Google-Smtp-Source: AGHT+IEFsToK4Mtfkt/isyLtzSbxuiJaEYiedQjj5sFCo8o3/vbFtfyZ9L8therJfArRWUvkwGk/K/B8SPA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1002:b0:e05:6961:6db3 with SMTP id 3f1490d57ef6-e0b54503b59mr2236276.9.1722038090539; Fri, 26 Jul 2024 16:54:50 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:13 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-65-seanjc@google.com> Subject: [PATCH v12 64/84] KVM: LoongArch: Mark "struct page" pfns dirty only in "slow" page fault path From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165452_056331_F0F792B7 X-CRM114-Status: GOOD ( 11.19 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark pages/folios dirty only the slow page fault path, i.e. only when mmu_lock is held and the operation is mmu_notifier-protected, as marking a page/folio dirty after it has been written back can make some filesystems unhappy (backing KVM guests will such filesystem files is uncommon, and the race is minuscule, hence the lack of complaints). See the link below for details. Link: https://lore.kernel.org/all/cover.1683044162.git.lstoakes@gmail.com Signed-off-by: Sean Christopherson Reviewed-by: Bibo Mao --- arch/loongarch/kvm/mmu.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/arch/loongarch/kvm/mmu.c b/arch/loongarch/kvm/mmu.c index 2634a9e8d82c..364dd35e0557 100644 --- a/arch/loongarch/kvm/mmu.c +++ b/arch/loongarch/kvm/mmu.c @@ -608,13 +608,13 @@ static int kvm_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa, bool writ if (kvm_pte_young(changed)) kvm_set_pfn_accessed(pfn); - if (kvm_pte_dirty(changed)) { - mark_page_dirty(kvm, gfn); - kvm_set_pfn_dirty(pfn); - } if (page) put_page(page); } + + if (kvm_pte_dirty(changed)) + mark_page_dirty(kvm, gfn); + return ret; out: spin_unlock(&kvm->mmu_lock); @@ -915,12 +915,14 @@ static int kvm_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, bool write) else ++kvm->stat.pages; kvm_set_pte(ptep, new_pte); - spin_unlock(&kvm->mmu_lock); - if (prot_bits & _PAGE_DIRTY) { - mark_page_dirty_in_slot(kvm, memslot, gfn); + if (writeable) kvm_set_pfn_dirty(pfn); - } + + spin_unlock(&kvm->mmu_lock); + + if (prot_bits & _PAGE_DIRTY) + mark_page_dirty_in_slot(kvm, memslot, gfn); kvm_release_pfn_clean(pfn); out: From patchwork Fri Jul 26 23:52:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743553 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6B047C3DA49 for ; Sat, 27 Jul 2024 00:27:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=rpwXsNxFJqP0CEr/yEQ2h0r+lN6KBTuvETCSKMoe8FQ=; b=rleox2V5aItkSs6Kvk9iBFqE5D JKMxm2WtyvqZNPtn46Um4MzNgU/5h9L/Xx1lH9zxQ3EromgIMCL6o4rwAMv3ISDXXKaDnKgA3MToR Yt0qShzoNpZXo958F6+BLqqLMDB6N6aXWoZ2a1X/qIZeF56iIEgmozLfGkG9ZkjB4ANJSRtA6Ofma Qm5rM+so7DDRM54PtbScUyA+yg+fHb0ENHv2r4dqLb731XAwtCYgQuuxMdrwBZ0o1HhWLNMhC/c0a rIUfvswRILu1Sytq2+OTMIXDVsab32d+G7lt1HuBM0237Hg4P7IcGDJZvpVIupM/ktaCTTMg084BU Zz1pInrg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVHK-00000005hNb-1a13; Sat, 27 Jul 2024 00:27:18 +0000 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUly-00000005Qx4-1zfv for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:56 +0000 Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1fed6fe8002so10219975ad.2 for ; Fri, 26 Jul 2024 16:54:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038093; x=1722642893; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=rpwXsNxFJqP0CEr/yEQ2h0r+lN6KBTuvETCSKMoe8FQ=; b=zGcKry2mcen4CZCTkukKs+Nhk9bKhk+SJbteeryNgOFo0gyHXiHTxSTM6ZutJQX8a+ wbkVzTV6013wnb3qCfljUXVSo0MGKnJBug97IFf/qIuj73/edTdpt6XyQK1pVuVNruu7 vFaMY7lXdOF4zgtKGrBhlCqbkRZgS7+oaE0KY3dqNZAmWcI68rz0tyWjGh1GLzdVTET8 0d2gW0Eb6+CDTyD9FqMIGZqCH5pa5BdjnJqtkG9mt9R9706NgU2BqS0QopRpEEykp+Wa T7IuV5H6inMrQLUD+mR/v7iRJ+WZrtb0y8QEznkeIzIu5jfH8V1mcdIXbvmRqx0fTQ78 51eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038093; x=1722642893; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rpwXsNxFJqP0CEr/yEQ2h0r+lN6KBTuvETCSKMoe8FQ=; b=l0OS8fnFara7WYiXhil3wIc1Kjj2OyQyfg10YoLZ/2G7tUxhFQZQKGCKzSHPHvm4Xu C9uBc0Hx+V9inHIgvcX1s3aTRR+c/1+BrKtuViYtgbYxmIC/p3vZcfOVF97ZFS+RLXP5 w7zGFU084tee9BcVGMDQBieaSRbfTAwATv66VipOU9TJ+S3MAZ/cDxpGS+XaZgZV2M0+ 38hQUAKKpHYLQdXo9wfiMWjF4xgxEv8OkGHi1RqJI+V+pAMu5xgNN1TRZHu5WozAJM8Z dOchpF8wX/TFLyaYVAnfOLz8KmBC8F88m5jQtml/NxHk2EDTpxxvz+J5Fn0SXJ01ZKHE QvEQ== X-Forwarded-Encrypted: i=1; AJvYcCWvA2RNKSLm3zpiN+Z8VybRxFEWm8p0OV1KOzXXDkytEH8oTJ/BOiJh5zu6ThIyvJW0MZZtzW1gy+GAF7sz00gKd6WEWHM6yOAw+AJLGxC8vnGgcuM= X-Gm-Message-State: AOJu0YzCHOFOOwnQuUn+Gia0Qnsyh4Ez1psnqiqcNEoxTRUBjaV3fUor hbAvzqxZBONyzytVB+iWciDJ0FNJhrChYBxo0AJ8qoa9Z0DYtyGivOJ109OqsN8cStnHgn7JnjZ lOQ== X-Google-Smtp-Source: AGHT+IEz1qMb/9xSOR6pziERGmR6RlcO2d1FpOxR3ZCse98fOqCGJ5ia3ZBUMMNkI3e0ioyh2oD8FsbOxx8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:2341:b0:1fd:63d7:5d34 with SMTP id d9443c01a7336-1ff04803eeamr27405ad.5.1722038092373; Fri, 26 Jul 2024 16:54:52 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:14 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-66-seanjc@google.com> Subject: [PATCH v12 65/84] KVM: LoongArch: Mark "struct page" pfns accessed only in "slow" page fault path From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165454_764998_96C03C79 X-CRM114-Status: GOOD ( 13.46 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark pages accessed only in the slow path, before dropping mmu_lock when faulting in guest memory so that LoongArch can convert to kvm_release_faultin_page() without tripping its lockdep assertion on mmu_lock being held. Signed-off-by: Sean Christopherson Reviewed-by: Bibo Mao --- arch/loongarch/kvm/mmu.c | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/arch/loongarch/kvm/mmu.c b/arch/loongarch/kvm/mmu.c index 364dd35e0557..52b5c16cf250 100644 --- a/arch/loongarch/kvm/mmu.c +++ b/arch/loongarch/kvm/mmu.c @@ -552,12 +552,10 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) static int kvm_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa, bool write) { int ret = 0; - kvm_pfn_t pfn = 0; kvm_pte_t *ptep, changed, new; gfn_t gfn = gpa >> PAGE_SHIFT; struct kvm *kvm = vcpu->kvm; struct kvm_memory_slot *slot; - struct page *page; spin_lock(&kvm->mmu_lock); @@ -570,8 +568,6 @@ static int kvm_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa, bool writ /* Track access to pages marked old */ new = kvm_pte_mkyoung(*ptep); - /* call kvm_set_pfn_accessed() after unlock */ - if (write && !kvm_pte_dirty(new)) { if (!kvm_pte_write(new)) { ret = -EFAULT; @@ -595,23 +591,11 @@ static int kvm_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa, bool writ } changed = new ^ (*ptep); - if (changed) { + if (changed) kvm_set_pte(ptep, new); - pfn = kvm_pte_pfn(new); - page = kvm_pfn_to_refcounted_page(pfn); - if (page) - get_page(page); - } + spin_unlock(&kvm->mmu_lock); - if (changed) { - if (kvm_pte_young(changed)) - kvm_set_pfn_accessed(pfn); - - if (page) - put_page(page); - } - if (kvm_pte_dirty(changed)) mark_page_dirty(kvm, gfn); From patchwork Fri Jul 26 23:52:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743564 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D7827C3DA4A for ; Sat, 27 Jul 2024 00:27:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=r/0kdPmZ+ClWir3sWShoQK9y6l+vrSl7wHPcCrpa8dU=; b=xQ2kc9oCeH3nred2TumyIOB32A T1AjXaoOaPuoArypQ/Xc9YDNNkvfRs4gUUMVlMjF2R0nb6UyiGDgoYQYcSA54uoniHnKZHZEma81y Zaios9zWxuOc9SN51LY5qN/rFMGKn9bVKJ555tCKfN96wZkZ/SvuMFlBxMbs/mwzjFg2ZP89lQgZA n7NFvizcKEUhQTbjcb1fPGox9J8rtYEjV8oJEVu2I1NDhEAnuoaNbsQqUUXOuE2FQSeSXRRTgoT8I ZQjws3gwDP2Lzfx/JsjMN4bbLyyuZVP6s9TqQrrJ2K8fzd7SadzABzaMMzicIA6ApOqofKHGD3uMy 0Xo3+pHA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVHn-00000005hdW-3rKX; Sat, 27 Jul 2024 00:27:47 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUm0-00000005Qya-0FYI for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:58 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-665a6dd38c8so7226467b3.1 for ; Fri, 26 Jul 2024 16:54:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038094; x=1722642894; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=r/0kdPmZ+ClWir3sWShoQK9y6l+vrSl7wHPcCrpa8dU=; b=ppTrVMrqQJ6FMlV9ZBvfqCcD53spTKFXDhRoolesutIT2ET4fAwSbh5bbA+G9kzNB2 MdrYrwb3TXPr86pCi74VSUoY+mSwR651m/AW1Matp5Ul41EkGRvkE+tFsa8JEWEXgDjN zohX8hOWz0Z/f9iT8mzs64XLWHuFIyeMK2mEIHKhS2+MOK0Jcmw38tYT8Dhtl6aEpbhI 9YqvXBK+9CEcOJGNC0RoaGqeZMKGes5X9FPjrJ5OUbw7E2yKQ+pnQlexCfcfwq+Y/alR QsPrEn/EReLp2dBUML/6PFVoAjXu9DR+A2MrNpLGpdL9hUhgaIqw5VPAk1wnuWDynQ2Z MN2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038094; x=1722642894; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=r/0kdPmZ+ClWir3sWShoQK9y6l+vrSl7wHPcCrpa8dU=; b=Mw/bN5cNwb1kStdrdsGgtGzhX0NKgmph/UF/CHQ9YgOmNgp2kZaKlbEY5TFWGIn3BF hbCIVDOX8oj+dPV9tIvdNsVD9HAE7DNBoQ60UfF1vn0UG6czLdbrCAWvcpHxrlgDbcaC b0N8MPK02gABz4uEfrRi8uTUBH/DVN5BOD5ZGng0a0gF6o8pOK/sKdymiRW3A5Rn7UQE C473GtMdYHVvL45uLpOLPj8TE9AuJaKOe3Qq3wrfDTQu5sdtEJqK2cL1Nn9OLCy8kS3J qnzGfyOQZF0vGkyhx7GWCA+3r0LaKvAb47y2Fv7znVa3f2qSae9AHPmDujjeMGiOZpFX lj3Q== X-Forwarded-Encrypted: i=1; AJvYcCUhcpqwLR5qT/hWnZHM19xGAD2nFhDX7OQlrBAQVAT3B4JJLyaeDxJXICNvtPUqBVPuQ73m5D1ObK1jv1Y4KvmZei99t9o3oPbTAhtwLqComLFZBbo= X-Gm-Message-State: AOJu0Yy7e+fAPCj0+29JjDA7Ddj/fFtMbGiQHtN6IepSJeOQmYr/vh9Q qdhjtvPIPNe7ydKwQvZQVzfFkM2w/Aej2q6bPd9RX0OmLGw9YB9apKiSVg3/9eIwCxnAugg77bx w/A== X-Google-Smtp-Source: AGHT+IFfaCc2nH86ghdgf4dpmC6KBZ4LBEB5Lf4TmkgG+fOYVVXJfjBbiJJRrvW3lQJXVsfLzyeGSOH6WxY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:830:b0:61b:e103:804d with SMTP id 00721157ae682-67a004a2a4amr37847b3.0.1722038094439; Fri, 26 Jul 2024 16:54:54 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:15 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-67-seanjc@google.com> Subject: [PATCH v12 66/84] KVM: LoongArch: Mark "struct page" pfn accessed before dropping mmu_lock From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165456_212736_9784730B X-CRM114-Status: GOOD ( 11.46 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark pages accessed before dropping mmu_lock when faulting in guest memory so that LoongArch can convert to kvm_release_faultin_page() without tripping its lockdep assertion on mmu_lock being held. Signed-off-by: Sean Christopherson Reviewed-by: Bibo Mao --- arch/loongarch/kvm/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/loongarch/kvm/mmu.c b/arch/loongarch/kvm/mmu.c index 52b5c16cf250..230cafa178d7 100644 --- a/arch/loongarch/kvm/mmu.c +++ b/arch/loongarch/kvm/mmu.c @@ -902,13 +902,13 @@ static int kvm_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, bool write) if (writeable) kvm_set_pfn_dirty(pfn); + kvm_release_pfn_clean(pfn); spin_unlock(&kvm->mmu_lock); if (prot_bits & _PAGE_DIRTY) mark_page_dirty_in_slot(kvm, memslot, gfn); - kvm_release_pfn_clean(pfn); out: srcu_read_unlock(&kvm->srcu, srcu_idx); return err; From patchwork Fri Jul 26 23:52:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743565 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8584FC3DA4A for ; Sat, 27 Jul 2024 00:28:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=T3Tq4gPotkzFIpSFFawcUtCGWBlTwHcibbE0IfTBXb4=; b=MMaA8dBoxe442SL/YLuOsJAGt9 6LYHqGJ90T+7qRdX8GEVoeBobnCrT4o/Ihiv4YAucFlP7OUdGvENn9i9VgFWJK/uC9vhKIbQ4lhtG gz4lRtdsYY6Huc8NzHPiXf2+rEMJITk3YI89yXnCoWuuCVCY4dIfa4RH1bKv9z60NjXRbGPEUPUQa OuOz9YD9HeMnQN3GeQ2B3Mur+8WER66fNjZhiaNrWuttF35paO5nlIlcElWYYsXcaZLCF6qzfVp76 BtiCS7OVL3Nl0rEiW0mDSBcRgk6eGbn6hBas1gAuBDHIgEtjkGIq8Br8V3CId4XVDyxX6CPNtsJQ8 L9T1xMhA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVIC-00000005hoW-3ICW; Sat, 27 Jul 2024 00:28:12 +0000 Received: from mail-pf1-x449.google.com ([2607:f8b0:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUm1-00000005R0w-3nUf for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:54:59 +0000 Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-70d14fc3317so1434781b3a.1 for ; Fri, 26 Jul 2024 16:54:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038096; x=1722642896; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=T3Tq4gPotkzFIpSFFawcUtCGWBlTwHcibbE0IfTBXb4=; b=BhozBO0kEAfCrIflS0Go8lh9e/ZB2mWScYCllqepSY29Ygi9IwPBSE7Go//e4lAwUU mX28kBjKeJK8fHod/iMvcLezmzW3+aHE83hYJ5u4W8AbuTbatNysH+4wBVR1lHx+4YfC ubXiFv8w3D9XeWYXz6xCBhByBf5kbBSVO3ZaNNhJrIs7pv1NVY2bjNDSU42JwJZ+sZPE pAWJ5khc6WOuQNNpRnD0dZ4i+fUpsXJDwiYpZF+2mz1lK6M6PFNkzOY1Rm6VG0tuxpg2 P6skP2qkjFW/K5T682nNAXt+8Sq1oZNBUrnbRPcaIxARwjEtXzyHOUiM9oal9T5MSyvg gwzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038096; x=1722642896; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=T3Tq4gPotkzFIpSFFawcUtCGWBlTwHcibbE0IfTBXb4=; b=VkFftHFCSqspbnw9l6F4uOHSnsXpJWG8E8h94of2OoqKM2MzEXWVuX39Kigpk83TGu UJNfQUv35EOk3bLzackLoCwlAEhKdiHHH1pzZWz0bf2tSEmLLfYj/gcZWqZnFh9d2NzH wyMh9yl87U2g7iOXaDmd9iQaU4hNGXzpcFhbwfcm0IsNKcSjWxsqo9wS04DSABu4r/pH m2oNRv6Cz0UbqDSWddBZNt48THWL7zwjZr02hRG4HB8RUWjdSEquBJFIiExdidWNDzoz cSehjEMtGWqy22JmjWzhyDMd0xXAL5IheW1INXhYBxo2IDOJJqAp5Ixr6wW702Y+0Upp 1t9Q== X-Forwarded-Encrypted: i=1; AJvYcCXMjnCqmuZxtQOiYMDl06B81uHCYsAJjFTqLGOz883rpKQ/0P4yK8CogonmFtvbs3msiZjMOWmFJYMVRjpVAS1U58oQh53AUs/Mn8eksfkvdT17Cmg= X-Gm-Message-State: AOJu0Ywma+0eC86CunrxiEAVDuFC57nQl7iayrSijtdWDJmupLZlb/3X Tdi831cn1deJurmPyw0iO3QmB94jAkfMKCOYugaaCDQWZmwN7zq0vEuqEogOiuUuQKowW3AtQFH p7w== X-Google-Smtp-Source: AGHT+IExCpdv69na3gU93uhAEfGJO0DgmCPrxQU8TmhxNm9ObFOOTchJQQycuTaWmcpCMOvBszJjylffDDY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:2d99:b0:70d:2b2a:60f7 with SMTP id d2e1a72fcca58-70ece928763mr9066b3a.0.1722038096309; Fri, 26 Jul 2024 16:54:56 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:16 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-68-seanjc@google.com> Subject: [PATCH v12 67/84] KVM: LoongArch: Use kvm_faultin_pfn() to map pfns into the guest From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165458_291663_32BEADDD X-CRM114-Status: GOOD ( 14.21 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Convert LoongArch to kvm_faultin_pfn()+kvm_release_faultin_page(), which are new APIs to consolidate arch code and provide consistent behavior across all KVM architectures. Signed-off-by: Sean Christopherson --- arch/loongarch/kvm/mmu.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/arch/loongarch/kvm/mmu.c b/arch/loongarch/kvm/mmu.c index 230cafa178d7..83e4376deabb 100644 --- a/arch/loongarch/kvm/mmu.c +++ b/arch/loongarch/kvm/mmu.c @@ -780,6 +780,7 @@ static int kvm_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, bool write) struct kvm *kvm = vcpu->kvm; struct kvm_memory_slot *memslot; struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; + struct page *page; /* Try the fast path to handle old / clean pages */ srcu_idx = srcu_read_lock(&kvm->srcu); @@ -807,7 +808,7 @@ static int kvm_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, bool write) mmu_seq = kvm->mmu_invalidate_seq; /* * Ensure the read of mmu_invalidate_seq isn't reordered with PTE reads in - * gfn_to_pfn_prot() (which calls get_user_pages()), so that we don't + * kvm_faultin_pfn() (which calls get_user_pages()), so that we don't * risk the page we get a reference to getting unmapped before we have a * chance to grab the mmu_lock without mmu_invalidate_retry() noticing. * @@ -819,7 +820,7 @@ static int kvm_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, bool write) smp_rmb(); /* Slow path - ask KVM core whether we can access this GPA */ - pfn = gfn_to_pfn_prot(kvm, gfn, write, &writeable); + pfn = kvm_faultin_pfn(vcpu, gfn, write, &writeable, &page); if (is_error_noslot_pfn(pfn)) { err = -EFAULT; goto out; @@ -831,10 +832,10 @@ static int kvm_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, bool write) /* * This can happen when mappings are changed asynchronously, but * also synchronously if a COW is triggered by - * gfn_to_pfn_prot(). + * kvm_faultin_pfn(). */ spin_unlock(&kvm->mmu_lock); - kvm_release_pfn_clean(pfn); + kvm_release_page_unused(page); if (retry_no > 100) { retry_no = 0; schedule(); @@ -900,10 +901,7 @@ static int kvm_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, bool write) ++kvm->stat.pages; kvm_set_pte(ptep, new_pte); - if (writeable) - kvm_set_pfn_dirty(pfn); - kvm_release_pfn_clean(pfn); - + kvm_release_faultin_page(kvm, page, false, writeable); spin_unlock(&kvm->mmu_lock); if (prot_bits & _PAGE_DIRTY) From patchwork Fri Jul 26 23:52:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743566 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B11E5C3DA4A for ; Sat, 27 Jul 2024 00:28:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=EmsAuvL8HQXwYqsGGesvXxhYre2Gu8OeDMQE+hBNppI=; b=EmLOdjhDS0YQ3GhfMHJRVaIIOQ 6NkkD6OB7vjZeaMYCss/LxsGljXfRwj0AUVo/zRPW6EFMFBmTEYYm6d/ZlV/DjUUTtfL3KabERY65 fpyIRiD0J3eZDfr3EJpRf4zeXZL2uFPUOJJBWGGuxdrJaHOw62VG4XBlJYZIRww6QXWGu7F80JGsz zARsF3FIQkk67vasanFSAh35GqPjNrmxuC6oxgz03POjls+diJyEm3vIOt4zN1ieu6aR+r31pEWT8 nTZLUx5UWEct5QUQ9OMItmrk7NQ4sI/AlVM5uEhC/1K7iytEAbPdrJEjgAxyZG9jJpOraXiuBsk+U gXuxq6bQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVIc-00000005i0O-2Rqo; Sat, 27 Jul 2024 00:28:38 +0000 Received: from mail-pg1-x549.google.com ([2607:f8b0:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUm5-00000005R3f-0AiT for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:03 +0000 Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-649731dd35bso1375103a12.0 for ; Fri, 26 Jul 2024 16:55:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038100; x=1722642900; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=EmsAuvL8HQXwYqsGGesvXxhYre2Gu8OeDMQE+hBNppI=; b=UfGQyyD3vq6j6kdku9TuM0lVETgP2Gx9PLJcBddmimczlR/92NTCV8JBanvDkn0VyW tkUpNxwhs2rqDe9AeYtCkFi0c9BL6/wQs41+x5hTOCgr0KWgTgIKdyQThTDilvWtt66o 7Q+HXsjzpMWObpUqE5lZe6fsL9CNIBcWF1GhSVq1nLvIwQccrvx97vl4bK5xV3/66k8Y +OiHB/tIDt+vyeCWcRMXAr2U4uf5k0adbru5mqt94PmgJknDkxDDt3V+SNO39kgJngOM 0UyNKEEgZsJ7Zxzqlj2pZpmoRWn/+OjCncoW/6LbaovHQaxSec0ANSLpI39TZLIlzv9R NquA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038100; x=1722642900; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=EmsAuvL8HQXwYqsGGesvXxhYre2Gu8OeDMQE+hBNppI=; b=N0lQZTDHc4y1+JIPEUfbbr8u7uvKz8ZP2VbBIErhcyxVSJwDqlkBuoYIXcSbttwm4q U020nnYt9wn2NVuaVDApKJk5nrNYVv0PgEV/F47oH9kHPSGD7YDt3yL3EdkVU6ph6avR uxx2axHEAfK44rb1UO4FqEgum2uz07er7lWkdP6WF5k0cjiJSvs4eC/qMgImPlV/9gnq RruRQKAntKXunXkMHXrgYRwqBa55SsiSQr9H8AGh7Rg7eLSwjC5bC6AxSAWws2Xq/Hpr OCV+GliHJs+se8RihSlongq7rlPgOJP+vwI2gdGTO0kSMHOT32jjN/pqvK2TjmA8yNSO MwQA== X-Forwarded-Encrypted: i=1; AJvYcCXkQNviHiLmbgT6hknIxTKpwyMF27zMKZ8+HOnYCni0RVpbUFfeduF+7qP8XYExpdq1J3+Dxgwt/Wqu3pHm0K/ELu9oliinQookbQVcok/D8Z4gM6k= X-Gm-Message-State: AOJu0Yxgw3qlEEhqWWnBpUSkuK92OgaoHrRg2LRHbWnaNtj6Wt7YHzfT njJNE1shyc1XuQBTqYjZEcPspEHzquNT4mQgCWaIB4798akkFF8kZ4pc+aSUyZTmh0scn4atArf SNg== X-Google-Smtp-Source: AGHT+IFixvL6M1c2YXLcj1uim9WdRxJ+9f/3q289x5sMH5Ueo8TBf7rPuOmHjx69h4l0El/Ee2ditwF800M= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a02:fca:b0:72b:5113:ec05 with SMTP id 41be03b00d2f7-7ac8e39fc8dmr2096a12.5.1722038098497; Fri, 26 Jul 2024 16:54:58 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:17 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-69-seanjc@google.com> Subject: [PATCH v12 68/84] KVM: MIPS: Mark "struct page" pfns dirty only in "slow" page fault path From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165501_277452_DE000CC3 X-CRM114-Status: GOOD ( 11.17 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark pages/folios dirty only the slow page fault path, i.e. only when mmu_lock is held and the operation is mmu_notifier-protected, as marking a page/folio dirty after it has been written back can make some filesystems unhappy (backing KVM guests will such filesystem files is uncommon, and the race is minuscule, hence the lack of complaints). See the link below for details. Link: https://lore.kernel.org/all/cover.1683044162.git.lstoakes@gmail.com Signed-off-by: Sean Christopherson --- arch/mips/kvm/mmu.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/mips/kvm/mmu.c b/arch/mips/kvm/mmu.c index c17157e700c0..4da9ce4eb54d 100644 --- a/arch/mips/kvm/mmu.c +++ b/arch/mips/kvm/mmu.c @@ -514,7 +514,6 @@ static int _kvm_mips_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa, set_pte(ptep, pte_mkdirty(*ptep)); pfn = pte_pfn(*ptep); mark_page_dirty(kvm, gfn); - kvm_set_pfn_dirty(pfn); } if (out_entry) @@ -628,7 +627,6 @@ static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, if (write_fault) { prot_bits |= __WRITEABLE; mark_page_dirty(kvm, gfn); - kvm_set_pfn_dirty(pfn); } } entry = pfn_pte(pfn, __pgprot(prot_bits)); @@ -642,6 +640,9 @@ static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, if (out_buddy) *out_buddy = *ptep_buddy(ptep); + if (writeable) + kvm_set_pfn_dirty(pfn); + spin_unlock(&kvm->mmu_lock); kvm_release_pfn_clean(pfn); kvm_set_pfn_accessed(pfn); From patchwork Fri Jul 26 23:52:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743567 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BB5E0C3DA4A for ; Sat, 27 Jul 2024 00:29:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=xPTfQtMbnGzpFXQRYDpT9MKKQAxP5DVy80mpAp/3DnI=; b=yDCQC6UhrMyjMFI1jj94ZHvsi5 52JCOuVuV/6BX6ItYJYwolb8sZlLQN2N7n3SHpjB4mIwDhSGl0b5IeuWvbnAF95qZGZGLLJM+GpJc xQ0/raXQ0wRI/aE/wrD0Ev6ZagGjw4mwniwznzJpfBRaZTuiEqjdVzavBKzvTLHQw+wbC9lhyM3Fp qKmXuJ2wgpZNQsaXLVp5819jNyQh5+nFogIYx+u7fzyQwq1kT+80rcDvNeKuEap8s82fDNlxG5M7p ApVcd/PFHQFsu9/PI1Uy+IUoq1vqHWbi6pKqFqrtILpDSOzT4nkjj9c6Ckgorx6tlWKz7HaNRNv6O oAtJCJpw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVJ4-00000005iGX-1dds; Sat, 27 Jul 2024 00:29:06 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUm7-00000005R5Z-0Z0u for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:08 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e035f7b5976so760599276.0 for ; Fri, 26 Jul 2024 16:55:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038102; x=1722642902; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=xPTfQtMbnGzpFXQRYDpT9MKKQAxP5DVy80mpAp/3DnI=; b=c2EmoLdn6qWUnwkOI6Es5Bckdmi+xotmqTg2HTzLZNNNX8PcxIrfx3F3x3Gg2ZMiMg mhj5TJzavZrgCw7PYLzcxEl83nISxS5/HJfs9HRJck6YuxX2YgvNczqNrU1gLvQbGEHh HQHxrzXAfGeo84l/fs0VIAl4E+qbKlrHHj3s6vnooQ+9m/TKG7aieSPgRSsP5m4ZLdtI fD04ZHLyN8HK8R3tu9EYmBj+mIgxQ5dmpnQ+jAALXe96+m9nNUfD1+zgyx91K5BQd0pU z2ZrtNQP7fxu8DWnlG4RvMXgBtgMN0xzUn9bkjJW9/079rCv1BT4EP5tZArWL+Mbwhf+ GcLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038102; x=1722642902; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xPTfQtMbnGzpFXQRYDpT9MKKQAxP5DVy80mpAp/3DnI=; b=thWELODmYdg1xcECphe2YcWZpsJOqP4gF1moP0lHEXjcfbg0xNee3kqsPUM5Nh7Row Qi6GIt/gZCsezqriJK7ETrp6VVYK3l8r9JyLoxpCZy/su8eAGphzBZgsaRqqjFkKe23z 8rm5e7T8P5OCPnFLA/uhadYM7MkINOfSI+muXXrr2AxGkAl7zA5E/fjvDMPkxzLz1U5S a8XWWjOtnZZTjNX05jfJdeJVA3etpNuPQSU2ULMSe/VUPhv6LMf3N2PVauhhanF3YsT9 VggE8vQrXqy/OLnQwtfynnOR8zswQQOPBxj3UA/ZdFXx4JEC+l9CgPCmLplVJ39AxVTI GRZg== X-Forwarded-Encrypted: i=1; AJvYcCVljHg1xXwpei60feXdqubW9zIttlZksrTCk5Sn+no0GUbQfFh89kznJomEM9PiUzgTWVUNxuvpGNVUf+lejd+fch+CxhJnSe7c/lRoIyjG8/v0XlU= X-Gm-Message-State: AOJu0Yydh3IjD0OxWenjJYUmcXjtJbSw5r6+dzcyGrUSQ0Tr4/azDg79 chcvtKXgckJtuhKEI9w5cOWdWo/3hxOToauWbLkpRxbALKhIGDDJlz1mAqpDYGwLhCuTIDqcFR6 YTw== X-Google-Smtp-Source: AGHT+IEEWasWSJ9byHCocPT0rn5wdP9A8yl8cF0t65kc5fqEIfuaFnuVC5T09t/hlGHF+EVf0JUp5RZPIP4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:c0a:b0:e05:a890:5aaa with SMTP id 3f1490d57ef6-e0b555474d7mr20378276.1.1722038101719; Fri, 26 Jul 2024 16:55:01 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:18 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-70-seanjc@google.com> Subject: [PATCH v12 69/84] KVM: MIPS: Mark "struct page" pfns accessed only in "slow" page fault path From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165503_637203_C8F948D8 X-CRM114-Status: GOOD ( 13.73 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark pages accessed only in the slow page fault path in order to remove an unnecessary user of kvm_pfn_to_refcounted_page(). Marking pages accessed in the primary MMU during KVM page fault handling isn't harmful, but it's largely pointless and likely a waste of a cycles since the primary MMU will call into KVM via mmu_notifiers when aging pages. I.e. KVM participates in a "pull" model, so there's no need to also "push" updates. Signed-off-by: Sean Christopherson --- arch/mips/kvm/mmu.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/arch/mips/kvm/mmu.c b/arch/mips/kvm/mmu.c index 4da9ce4eb54d..f1e4b618ec6d 100644 --- a/arch/mips/kvm/mmu.c +++ b/arch/mips/kvm/mmu.c @@ -484,8 +484,6 @@ static int _kvm_mips_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa, struct kvm *kvm = vcpu->kvm; gfn_t gfn = gpa >> PAGE_SHIFT; pte_t *ptep; - kvm_pfn_t pfn = 0; /* silence bogus GCC warning */ - bool pfn_valid = false; int ret = 0; spin_lock(&kvm->mmu_lock); @@ -498,12 +496,9 @@ static int _kvm_mips_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa, } /* Track access to pages marked old */ - if (!pte_young(*ptep)) { + if (!pte_young(*ptep)) set_pte(ptep, pte_mkyoung(*ptep)); - pfn = pte_pfn(*ptep); - pfn_valid = true; - /* call kvm_set_pfn_accessed() after unlock */ - } + if (write_fault && !pte_dirty(*ptep)) { if (!pte_write(*ptep)) { ret = -EFAULT; @@ -512,7 +507,6 @@ static int _kvm_mips_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa, /* Track dirtying of writeable pages */ set_pte(ptep, pte_mkdirty(*ptep)); - pfn = pte_pfn(*ptep); mark_page_dirty(kvm, gfn); } @@ -523,8 +517,6 @@ static int _kvm_mips_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa, out: spin_unlock(&kvm->mmu_lock); - if (pfn_valid) - kvm_set_pfn_accessed(pfn); return ret; } From patchwork Fri Jul 26 23:52:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743568 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AC896C3DA4A for ; Sat, 27 Jul 2024 00:29:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=y+Ar7T6On16alzy9M8c5sPTmukkBuZuZUdMs6MViY54=; b=htVbGAD1WBGqYrafxqAJHDYReR XPC71e1XQcZaVqP6l7AYMnzA+3oCVmogl9SzseDON5RIwE2PyOCILuxtfeWYjBKOO5glFzlnS8KOY x7oUgIH196f9kzYp0YfEnDDAN+wxJy+ksiYRFSMqQpbJUoYVC49Db4vfYhIt0ePO9CWlVlaFmZ6r4 +oln1XzZ41JyKTOI0j+3gRmMYCK6ghtTLkIIx7vOY17ien9Uy7uPYyu0DSkVKFkS6zZgkObCx4vMH IOKM3JgRfa1d01qmzIW7W9ZIUofmCQjwzAIUspgnOdccAptLZh++Drd+q4W/kzTAqeqco+LeKX/+v ay448iog==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVJW-00000005iRV-1rLS; Sat, 27 Jul 2024 00:29:34 +0000 Received: from mail-pg1-x549.google.com ([2607:f8b0:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUm8-00000005R8R-3ab9 for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:09 +0000 Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-778702b9f8fso980516a12.1 for ; Fri, 26 Jul 2024 16:55:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038104; x=1722642904; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=y+Ar7T6On16alzy9M8c5sPTmukkBuZuZUdMs6MViY54=; b=Xp+oKq+tgSaakiE6sOy9CxG4QRN4uS+QNzJ7kjMpUAoSZEvNxAIqL3C7BpprX+d2uC RFINH9qvnLQKAqjISIMjX2bTo6sNEJvt/qbmcOaEWtUeBjgf41gNttkPymXMZfdblf3x 1szZThDQwNIBLjqYRRKu7mfuxeqPEwGVcVjB0W1BiwDkOXDAIMtQA+Rx59uiAR+Vvm6w ag5YebQynXSLGIE4LnXxB2gaIbqDPu1m4qIHGAzeYVCip9/Vx5GD/EzUg6OU0fYULCbS aPXaPCuGzbB7Z0rcTvCbb3cbi9pWE95kgnSf/Z73WYEv7no++f2TF3g0awk3EpAojYx1 xj3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038104; x=1722642904; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=y+Ar7T6On16alzy9M8c5sPTmukkBuZuZUdMs6MViY54=; b=AuQbpaJzZ9459adHSVj9N9EEx+nG0OAl7CzqLl8A3mCkEyTnrwc29AHf/usLadFGKe iu4un6KsXdh8PoT3J85Uaqo1ZQfkDRXPiGM1JVOz+L9hzCkFn/hRYhdWG+U30n9bzrfU +q6cs7C/UL368qZb8uu8qdZ5EP9EXYDflCiWjQLuRI0KmFhVvspexxpIDR4xXP90Sy3D jRkYguVC8dCYR/Da2zzaoW5R35A0Oq4Odlf1gW78RQ9rcmvZsKEO2zTVq2bvUyICZ9+0 hh4RWx/uca8ZZSeqLfNGhkdu83wlh1/UJVbLzvOo+rS9hu+QtEUOkEmZkrxcI3xt3Xhl wzzg== X-Forwarded-Encrypted: i=1; AJvYcCXAWmehj0nmMAB5XBy9tLi/dfQwQRpqXrs0tdl91HZi2CMuPXF72RaiBxQAJ77KJxNE+srX+e6gYspQsurLhicGlw7M4Nm1EFe+N8eqc8rS1sA00OQ= X-Gm-Message-State: AOJu0Ywml88SJqu4doZ8F67NWVcCut5s9XFZ2cwtQfN/byjZVcrGBpPU noQ0zzrT5ZoWk6/slee6FpTSejJ5fs4biWvG1sf8PLLmjflVM7venX2564Cd583+UI0QVXLhDpy +tA== X-Google-Smtp-Source: AGHT+IFnr9ua4KoyN+kf9dgXRrvt4SVsbQldBEs2PtVQEhle6Kqivs/NJk7PeSFySHT1lFiCWuBu3lKqAis= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a02:50a:b0:740:2b12:8780 with SMTP id 41be03b00d2f7-7ac8fd30684mr3158a12.5.1722038103410; Fri, 26 Jul 2024 16:55:03 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:19 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-71-seanjc@google.com> Subject: [PATCH v12 70/84] KVM: MIPS: Mark "struct page" pfns accessed prior to dropping mmu_lock From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165505_208848_92A93254 X-CRM114-Status: GOOD ( 10.20 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mark pages accessed before dropping mmu_lock when faulting in guest memory so that MIPS can convert to kvm_release_faultin_page() without tripping its lockdep assertion on mmu_lock being held. Signed-off-by: Sean Christopherson --- arch/mips/kvm/mmu.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/arch/mips/kvm/mmu.c b/arch/mips/kvm/mmu.c index f1e4b618ec6d..69463ab24d97 100644 --- a/arch/mips/kvm/mmu.c +++ b/arch/mips/kvm/mmu.c @@ -634,10 +634,9 @@ static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, if (writeable) kvm_set_pfn_dirty(pfn); - - spin_unlock(&kvm->mmu_lock); kvm_release_pfn_clean(pfn); - kvm_set_pfn_accessed(pfn); + + spin_unlock(&kvm->mmu_lock); out: srcu_read_unlock(&kvm->srcu, srcu_idx); return err; From patchwork Fri Jul 26 23:52:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743571 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CF7E9C3DA4A for ; Sat, 27 Jul 2024 00:31:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=M9eYn37tam32k4Zpbs9VqSvLY6wlhZ916YFlgyF6peA=; b=12UZKvraL6WRqIG1B3TbbOBGvl 451/mYaIJBoGF6Ml2yla6MeTmEbW4jNd0TJ8XKKc7YoQwY5bLmEQ8vfTdEEfgL5T/55LKNOx+IYSO 07miOo88JgqHQFxZNa/f64lp3g5S2Ip37C7WHeVKzZ17/n71zCrZqLcZNFOAFzJy+RnN8vQRzKPOG /bcuX8g9PybdCPAkpPRWxhkH76OxowI8QTEeOplmuvSbkNQJcGW+OLvndH1bGiB75+P4yz+G2wjrU IfmI99tNE5YiNaYUoFjNYFPs17xhB2k40rAZKSrekWd+xuYFQNPTAPYu1P8mSZ2GUtJCUEz2L0QKm xTDXmCXA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVL1-00000005jDi-3kj5; Sat, 27 Jul 2024 00:31:07 +0000 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmE-00000005RDZ-2gzf for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:16 +0000 Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-7a134fd9261so1531589a12.3 for ; Fri, 26 Jul 2024 16:55:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038109; x=1722642909; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=M9eYn37tam32k4Zpbs9VqSvLY6wlhZ916YFlgyF6peA=; b=c0WShnEqI9zybJMiCXTEfvNq8PbWYMebI4kgiVVwpmOZ0PklDW2FgMa5BegC7dCDVK q3fvPXR5Mp0S2Uk0lS2eDICFnhKEY/sSMVMUpptwy+j7/v+d0Czuuv9rOY7W8o/pR30P pDvwDOBaWprvTc31K55pdNv8UpzGNDLUNnpSPrwK6lFJYvclZmK9YwIW+BXyl37GOdG5 RDtvAQSYqZq4YblJKUwRdZIFrPyb1bKkz9/SSytJWZnRjbcLIA94AL2JSQjsRhFPv972 KmcnvJ375ftGwTqp0f2htzpPJeOf6A5CtExNMTOXUjI+YQAvdNc6xb4YiNOu9eIToI9k KXiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038109; x=1722642909; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=M9eYn37tam32k4Zpbs9VqSvLY6wlhZ916YFlgyF6peA=; b=ttAkbmsZGBCJ+9M3ZJYhm96n9BaRt8lx2pi9tfeUAF/q74rM8hHfux7H/BLanTUu/l OUtBsiE9GVmjvMgxuvPUBZUP6m6YYnJT9PcbBNyFNfSl7rmTPOb18p6LLkGL1xMbAW9e 7pJu1zthCGAYD+gGFkJQVkdv+4dnjcbA5wM/oVyGWIT18Ucs2nhToLySJz3fNJMfskkJ W5pl1eQwE2Dy4no1TzqNbcDDCO5DKudUTCd1REVNxjrTKQD70cY3tofstwvFbg6ixGh4 UEwv+q4RJUYFVmU6FXrTVM/3MRkhpbRMYXGFQhprLVjKty5tmMY+XAEY4oA+8c1zaSmw uTvA== X-Forwarded-Encrypted: i=1; AJvYcCWDaiimHfT0nbfhMBxvwrT27jjRQlb8MrAsGiocEFYrr7YX0CEIsmhuSMxgf+UBfzWQlSAFuD/mm8diiVxsxj85SJjqhrJHAStm1LTvS/IoG1yMfBw= X-Gm-Message-State: AOJu0YwRAT5HpbTQ1y9p3m1pDTlsexj73DW1vSgb81X2CpylNh51OlrG L55joOQZngqsgx6AmooTkb2VOgMO6uPq8J0suj67Q8+JbhslazQTf4XfaZR5wa1XEhWrXH+FASO 98Q== X-Google-Smtp-Source: AGHT+IG8TH3qPrjAadwsmfbGVL51l5vDfwJrWL4CCM6knLIepOGyFeiwzAiBROJulbOiIHONd5UuXBO9hTI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a02:526:b0:6e3:e0bc:a332 with SMTP id 41be03b00d2f7-7ac8dbc497emr2439a12.2.1722038105187; Fri, 26 Jul 2024 16:55:05 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:20 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-72-seanjc@google.com> Subject: [PATCH v12 71/84] KVM: MIPS: Use kvm_faultin_pfn() to map pfns into the guest From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165510_918553_FFF2900D X-CRM114-Status: GOOD ( 13.64 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Convert MIPS to kvm_faultin_pfn()+kvm_release_faultin_page(), which are new APIs to consolidate arch code and provide consistent behavior across all KVM architectures. Signed-off-by: Sean Christopherson --- arch/mips/kvm/mmu.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/arch/mips/kvm/mmu.c b/arch/mips/kvm/mmu.c index 69463ab24d97..d2c3b6b41f18 100644 --- a/arch/mips/kvm/mmu.c +++ b/arch/mips/kvm/mmu.c @@ -557,6 +557,7 @@ static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, bool writeable; unsigned long prot_bits; unsigned long mmu_seq; + struct page *page; /* Try the fast path to handle old / clean pages */ srcu_idx = srcu_read_lock(&kvm->srcu); @@ -578,7 +579,7 @@ static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, mmu_seq = kvm->mmu_invalidate_seq; /* * Ensure the read of mmu_invalidate_seq isn't reordered with PTE reads - * in gfn_to_pfn_prot() (which calls get_user_pages()), so that we don't + * in kvm_faultin_pfn() (which calls get_user_pages()), so that we don't * risk the page we get a reference to getting unmapped before we have a * chance to grab the mmu_lock without mmu_invalidate_retry() noticing. * @@ -590,7 +591,7 @@ static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, smp_rmb(); /* Slow path - ask KVM core whether we can access this GPA */ - pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writeable); + pfn = kvm_faultin_pfn(vcpu, gfn, write_fault, &writeable, &page); if (is_error_noslot_pfn(pfn)) { err = -EFAULT; goto out; @@ -602,10 +603,10 @@ static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, /* * This can happen when mappings are changed asynchronously, but * also synchronously if a COW is triggered by - * gfn_to_pfn_prot(). + * kvm_faultin_pfn(). */ spin_unlock(&kvm->mmu_lock); - kvm_release_pfn_clean(pfn); + kvm_release_page_unused(page); goto retry; } @@ -632,10 +633,7 @@ static int kvm_mips_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, if (out_buddy) *out_buddy = *ptep_buddy(ptep); - if (writeable) - kvm_set_pfn_dirty(pfn); - kvm_release_pfn_clean(pfn); - + kvm_release_faultin_page(kvm, page, false, writeable); spin_unlock(&kvm->mmu_lock); out: srcu_read_unlock(&kvm->srcu, srcu_idx); From patchwork Fri Jul 26 23:52:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743569 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7E9C1C3DA4A for ; Sat, 27 Jul 2024 00:30:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Agw1NDopTbJHSwXqEMTAoK1oQKqohLcT4CyuwhV7hPY=; b=sNDo12ZtyyaHRupYzGmuDUmtOY HBfjQMhu8Jrxhd+wuKk4fMFPuAy+u+p2OPhlVBAoIK15R9lsK9ZuRiN9kBCupq+rbTdUXiwSKIZA0 WU10v9JFvCJ8pVQC7UEjzitL/Q5OZkAwL8rVYxYS2bXrDJODmv8ghxpC8M/JxV15acAwjiRSaRa/7 8M18odXyKOKip9e5mgg1n/NljlZhFfG+7i6lkYXMqzWdnxDdUBbxL1AQrIUNPpsbC0+kp/9FJNBye NCKwjlMDYw9uvlG3phJ2yIfJ/4bp6e7yqTYiP5R2c7ocX8yVez8ih8PgtG+sXXxPm34Bx0gusXiHL VMglcwDw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVK9-00000005iq2-2T0P; Sat, 27 Jul 2024 00:30:13 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmD-00000005RBX-0A4d for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:13 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1fc5f04f356so10599555ad.1 for ; Fri, 26 Jul 2024 16:55:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038107; x=1722642907; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Agw1NDopTbJHSwXqEMTAoK1oQKqohLcT4CyuwhV7hPY=; b=e5nexWDcr74v5Ti+MHQB4qjptT5ei7vIlrzAr/Qats5fQWJ6muj6YMibrXTH1JuHLA CilAF1jvFFBK601Uj17+v4Hop5RaH3v+5fezi1IDydkh6M40E6jAp9W7XW/37ify7Ku/ 4vfnxbHPWWQcWE6gDXYEm8C/qAxl5N7AvN0JPkrjlDtT8LzFXm8uFQVXMYbR04g13rpB QgHju7F254xreGdDdluFS2IotbeSJj2+usRAviI+RsPVGgBNQFgqxyolgp2blecAdpMO qn7diiJtaqtUaUvskluWkfj9Cjzc4TBeas8RtEQ9OY/gXS1qPYFZ/mz404XrtnrLoB1r DqbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038107; x=1722642907; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Agw1NDopTbJHSwXqEMTAoK1oQKqohLcT4CyuwhV7hPY=; b=rMaADb5q5lK16XX0xyQmE8JK8mxbmZzCgQ67tZBObFvSr0lnkfXj9ws3DkPmnUC/Re OWSboreoTKdrd+qmGdQqnS5ITjFHq3DqvyQ4DxPP0ouLzPHY8lw60j004C0nOnzzVy7W PQ/Vks6H1PUHn9KVBiP62bsbLIC4kEs4YPzeGCZ+HRGfcRWjHUydOYmY9/qvD0jJD3m2 2PnjmLf9gLmraTD8IOuQ4HFoJnJQ0AMVSrO12XkWaM5Rv1mWZrtKY+3g99bn1wagv0tO VQhAVHsd6+AIhj9+sh7ZnrnXmtZ8UKciD6TA9j3R0Vblg8QF1HHt0YY9dWbHynbK9gjA yjUQ== X-Forwarded-Encrypted: i=1; AJvYcCVGu3KU4qR7xqROGe9V8gtBwGOms5Cjpi56ieN+VKg2qTsRInNQo8GnxcmPeQd1Jgw6IqwcCt4uVwxvn72+0qwUolDcdlfRZWQ9NR6PhYvzdhCnCdg= X-Gm-Message-State: AOJu0Yy5Y2fKldzLr7y3fUooCrIXGsrJTlsEcPvvVAyosJatAvpcN02z mzasy5ilgqG0Ds5+cG0QhzMqJ/VztFrUl4ZNaxungrU1nlPjQNbI94R5O43xTzO0zB7I/PnzlME OOw== X-Google-Smtp-Source: AGHT+IHxZ9LAKt5vIHU7V05HDfJ12gC6skwH/WeW9XOXHpR7B1c0ubRWlOUtq2UiALskA4rClXraM4lFVDE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:b18d:b0:1fd:8ebf:672c with SMTP id d9443c01a7336-1ff047d8f61mr25415ad.3.1722038107293; Fri, 26 Jul 2024 16:55:07 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:21 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-73-seanjc@google.com> Subject: [PATCH v12 72/84] KVM: PPC: Remove extra get_page() to fix page refcount leak From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165509_304226_835D0FF5 X-CRM114-Status: GOOD ( 10.20 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Don't manually do get_page() when patching dcbz, as gfn_to_page() gifts the caller a reference. I.e. doing get_page() will leak the page due to not putting all references. Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/book3s_pr.c | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/powerpc/kvm/book3s_pr.c b/arch/powerpc/kvm/book3s_pr.c index 1bdcd4ee4813..ae4757ac0848 100644 --- a/arch/powerpc/kvm/book3s_pr.c +++ b/arch/powerpc/kvm/book3s_pr.c @@ -652,7 +652,6 @@ static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte) hpage_offset &= ~0xFFFULL; hpage_offset /= 4; - get_page(hpage); page = kmap_atomic(hpage); /* patch dcbz into reserved instruction, so we trap */ From patchwork Fri Jul 26 23:52:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743570 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4E9BAC3DA49 for ; Sat, 27 Jul 2024 00:30:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=FGNMZjC6zrq4QPk0aaY9W+lI1TKYWYG7APrsjifVvZM=; b=qOJXNxsIloTbEuGPcGeHReXR5X q8Koy4/8N7lu7QnxwlC+eS4opIQ2OTc1aMbqkSv5ZuaZyBhRvyXGdd/tSpXVxSq3CNsz1eOJAIo2n /aQoWgu50nnexvguZIT/uKPwDwMsWF/A6iv0DL+Kx3r6M9Es9jC7nnaT3rfoegzZf1WwK6lMRfEAj o7Shp6qG8KQ07CovKy0vSC5ObrC3x5GVGmfO/Yvh6XTSjSQrN1ANHooTzkoDqgzk1yU+Qn0Woqbfd FI64J+qs2zRlaiFtUpab716hlwMeFJHjZ14nckJRJxhGr2L449bSY+apZa31emJS/0uy1Bfzkwbnx Qlf5eYiA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVKe-00000005j4K-2akL; Sat, 27 Jul 2024 00:30:44 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmF-00000005REP-1WPj for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:16 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-6648363b329so5175957b3.3 for ; Fri, 26 Jul 2024 16:55:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038110; x=1722642910; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=FGNMZjC6zrq4QPk0aaY9W+lI1TKYWYG7APrsjifVvZM=; b=ZLp5txfxYH6q2vmavmG997qwjBolQPC+YcsmUXhWlVmZ9Qtc9WeyBrgRwFoGpBRUqN A51TRObFHCPZux/6+OeZWCDVR74M2+zX97YgUSY5HkSdJN2KuSCALfx/8uhLtIx/PNm9 04wdf3t/H+Py3bLaECsLQikDqOzFiE1n5NBElBNvSpgTHhmB5PLV2YB90pbSiLuRV0Db Zi3NbkAr6GfD7asOEJBKfvQSCSYtpPypzvvq9sA0c/28R1q8DMrDhOBZjeFN3Fo8d9X+ wyKHVY785vOeY63wLXv+8EqRnORHy7maTE2XpetWWUFEi9IR5qooRG5IDZEIhugWtDQA /Q4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038110; x=1722642910; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FGNMZjC6zrq4QPk0aaY9W+lI1TKYWYG7APrsjifVvZM=; b=SPlNWZkB6oE2LhaRpKtv4ZlcHeYMvAZMM4YDCuqUuhxMJbdv2vH2bBV38aXObUji2p c6CsIM9fATAVuBXFE96NkarNtSRmaqEN4J3cbIDHogIGqjvHDm5L92alWZZbk8pPCaEW K/MVM0w9lvbXn2JVgXn1FDRKfL30dP0s9cHTvKJWT0de/C1Ca2NiGvWKohHpkvLRm/nS 3JbuufozZTJtLXoNIj9rBMN0Ku+yUzdcwwQakNsLUxOhVNpXkcIGJwwWcuMiuaOOFq16 pbZu/cqCrJvtJQw3L8RA9R9DO1Zx4XG33IZF9n6d58YmWaKH7IIOxuzKUesg4P4sBnD1 sNSg== X-Forwarded-Encrypted: i=1; AJvYcCVZm2PBMcs27waXj7ErFisAJuTa1c/JRQhhkomvqevGsOarGR6w6nY1KFYCFpHZry2xSN7+1/srcwrinBhBpl77+Fq1RrECxmhgcLVAXObIK6EXePc= X-Gm-Message-State: AOJu0YzYZjJneE3J70XeqaDIsLTXlte4LuzgYsMaDBLArlKaP2/Bt8wj tlzYIbf2Vo26sSERAky/VZheFYqEoUcgQw+pkfQaHMaULVJYVE+GvEJth8los3tin5B1O8ThyuF Lcw== X-Google-Smtp-Source: AGHT+IFLZWO6XOpdLpjl0c1N0cYikN5d5kXLguU/WRS6HZfMhk4D9x8u+2tsxxP6CN0/8qHX8YjuuRQeC7A= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:830:b0:61b:e103:804d with SMTP id 00721157ae682-67a004a2a4amr37877b3.0.1722038109325; Fri, 26 Jul 2024 16:55:09 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:22 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-74-seanjc@google.com> Subject: [PATCH v12 73/84] KVM: PPC: Use kvm_vcpu_map() to map guest memory to patch dcbz instructions From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165511_677503_B76AED67 X-CRM114-Status: GOOD ( 13.09 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use kvm_vcpu_map() when patching dcbz in guest memory, as a regular GUP isn't technically sufficient when writing to data in the target pages. As per Documentation/core-api/pin_user_pages.rst: Correct (uses FOLL_PIN calls): pin_user_pages() write to the data within the pages unpin_user_pages() INCORRECT (uses FOLL_GET calls): get_user_pages() write to the data within the pages put_page() As a happy bonus, using kvm_vcpu_{,un}map() takes care of creating a mapping and marking the page dirty. Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/book3s_pr.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/arch/powerpc/kvm/book3s_pr.c b/arch/powerpc/kvm/book3s_pr.c index ae4757ac0848..393c18958a5b 100644 --- a/arch/powerpc/kvm/book3s_pr.c +++ b/arch/powerpc/kvm/book3s_pr.c @@ -639,28 +639,27 @@ static void kvmppc_set_pvr_pr(struct kvm_vcpu *vcpu, u32 pvr) */ static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte) { - struct page *hpage; + struct kvm_host_map map; u64 hpage_offset; u32 *page; - int i; + int i, r; - hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT); - if (!hpage) + r = kvm_vcpu_map(vcpu, pte->raddr >> PAGE_SHIFT, &map); + if (r) return; hpage_offset = pte->raddr & ~PAGE_MASK; hpage_offset &= ~0xFFFULL; hpage_offset /= 4; - page = kmap_atomic(hpage); + page = map.hva; /* patch dcbz into reserved instruction, so we trap */ for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++) if ((be32_to_cpu(page[i]) & 0xff0007ff) == INS_DCBZ) page[i] &= cpu_to_be32(0xfffffff7); - kunmap_atomic(page); - put_page(hpage); + kvm_vcpu_unmap(vcpu, &map); } static bool kvmppc_visible_gpa(struct kvm_vcpu *vcpu, gpa_t gpa) From patchwork Fri Jul 26 23:52:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743572 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D9225C3DA49 for ; Sat, 27 Jul 2024 00:31:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4Hy7goaarUlwaWZyx1m4yCokRE7UoQ8eGBMKWJ4Hgr8=; b=VvqFWUal7Q5YXacu6GBuKABm/z PpO6LfzvzRhry3A4wNnDbTBwccvlXQprhtmM6Q8EliEAUMdi0GDOlXMA+D1ioh1Jxyoy2lawzdep0 VYi9SLe682VCbcsSpDhYuLlrPve3E/shr8PM0Bs4n/LGcJSRiTF067qqxFqLdeO7qDSbKYuhnkjGo wuKETUrUCtXXrkph/1v290C7dxAskEnyivbua2+lGR0ywjMWEFGifICBADiqhxjP1Oj87t9YEiUZn gQy8IkqCW2tAp8BopODRqzUrjdlbRGpxTZSaQUMBYLV8YhfVmFobUPt/r/lQMabZUC9AJOlrSTkxz AFM2l18g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVLP-00000005jNT-1k4o; Sat, 27 Jul 2024 00:31:31 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmH-00000005RGh-0lEC for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:18 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e05e9e4dfbeso401925276.1 for ; Fri, 26 Jul 2024 16:55:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038112; x=1722642912; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=4Hy7goaarUlwaWZyx1m4yCokRE7UoQ8eGBMKWJ4Hgr8=; b=cLUA9EbG95Oxs7i79dWaFSc7DX1k10O3OLzs5TiVpQ2clmyEphzz0i4Un6YYdsPfQd UsWHj8pIg23z1hcBtV0RtscLF3qkIqaN9/MixGtoCe23uybTf6vZeFdKNOwkHART1UkK wGt49cJMSG5q6u9/CkE/COhpgGvSG/RtG/6o48TspBawhLGgkNKBFDXXPMPU0tmgfFug pfsej6XjkHes5ngKSzQMR9H7UR9dPL4yontZ3G4bwsDCDPWhGXTazHk4BFxQ2DP8skGS dzRcdQzl4c4ZYqApKHEIZjmFUqpYC392XYddcRtXpSxFW9DD0YND1u6kt0VELgfh9uWV 4h4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038112; x=1722642912; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4Hy7goaarUlwaWZyx1m4yCokRE7UoQ8eGBMKWJ4Hgr8=; b=FAZEJAWf8ZznK5Ku1vMW+UOAkPHt6/VhVERRZzHJ2o6fKESq+WcsVtOCUIOh/6PJ5h oK/HkUWZJKaBAABeozvyBHUFfFgMRdryfXJrKiJt7ENxV+uz9nYfeWp2leTEW3o6D6jr opZcpPMeCoHsH7K20SC2shzkoKaSq36fkILarccIwTGRyJIksubJE00zpoLTxEOwBJcu JI8dQ02MrObyoYBXoMaKWmHuRhzlg4MosnV4H/EiUP0umamA+VfjB7dxMrQQSZ8SZ3tb FLQz1CJ4e7hWvtwOVMGmUYkUY97fHLv6a404LexCLeHchv7vOrggU0Rn/4Z0klzjRD7r vzTg== X-Forwarded-Encrypted: i=1; AJvYcCW7SuQqeRlK22QvPQJ3nCK+HoyrosauuvvR60yC13JhSoHFjFEZQyDotoIuiwGx7IpJB/v5Efzd2ckZNCCREPRyoVvnvDbENeEhJKpvzBN352H3CVo= X-Gm-Message-State: AOJu0YyNzgL7bT9wX0bHExwsxwiCldJhPWpcqYpIW3BrwNqS6QBtJfts jvwkGTyb+rJFjryMMgV1QWuKrEYX9LEfnpMG7YI1wH9BNufrmHpy5kOuw8HTOoNRwUmJiA6sU2l Nkw== X-Google-Smtp-Source: AGHT+IH925MpsiY9ahug77TjXa70fcvLwqDVLsDqA/3N0vd70sZBQGqqpgNL91Lx+sGl/PSva06bFRo6+6A= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:2b8c:b0:e03:b0b4:9456 with SMTP id 3f1490d57ef6-e0b5454ca99mr43273276.7.1722038111873; Fri, 26 Jul 2024 16:55:11 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:23 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-75-seanjc@google.com> Subject: [PATCH v12 74/84] KVM: Convert gfn_to_page() to use kvm_follow_pfn() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165513_353126_E1667750 X-CRM114-Status: GOOD ( 10.69 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Convert gfn_to_page() to the new kvm_follow_pfn() internal API, which will eventually allow removing gfn_to_pfn() and kvm_pfn_to_refcounted_page(). Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6dc448602751..d0f55a6ecb31 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3181,14 +3181,16 @@ EXPORT_SYMBOL_GPL(kvm_prefetch_pages); */ struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) { - kvm_pfn_t pfn; + struct page *refcounted_page = NULL; + struct kvm_follow_pfn kfp = { + .slot = gfn_to_memslot(kvm, gfn), + .gfn = gfn, + .flags = FOLL_WRITE, + .refcounted_page = &refcounted_page, + }; - pfn = gfn_to_pfn(kvm, gfn); - - if (is_error_noslot_pfn(pfn)) - return NULL; - - return kvm_pfn_to_refcounted_page(pfn); + (void)kvm_follow_pfn(&kfp); + return refcounted_page; } EXPORT_SYMBOL_GPL(gfn_to_page); From patchwork Fri Jul 26 23:52:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743590 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 13784C3DA4A for ; Sat, 27 Jul 2024 00:34:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=zSysp7FPh6wn+DolRHFxx8ZAZJRhLuISfWWp3RFo0OA=; b=vDkdwPso8g3S6swS/1hG44mHok t/yluuaBOyGJcuT+dr2cvUSFZcVL4G0j8yMir1anw7Fc3P+eIdr+9g10H0h6z8n4I0U2VqXFjOO+1 BZPJnRSblabHv9lsd8OvMzX/EqJQQ0mdg2s7zJ7OCNxrb0HBJd0ciTDlqR+pcpF2mUqahjZBTOUN4 c9DtCuO738plV2aUvoYD6ixIHl2BSkUwLBc0vhJAkzqQJuHiE5VHfuvFAuhvqey79inwW7eUkNJix irg77OGpFLmJmqg0n1xB0ib4zSgq/phSbVFYMP8ZfrzVaHDFIoAoMsafONcC3YtYVRbDBeh1ppMPP S1c+copw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVO4-00000005keb-0vE7; Sat, 27 Jul 2024 00:34:16 +0000 Received: from mail-yw1-f202.google.com ([209.85.128.202]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmJ-00000005RIg-3JSj for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:20 +0000 Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-64c3f08ca52so6085917b3.3 for ; Fri, 26 Jul 2024 16:55:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038114; x=1722642914; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=zSysp7FPh6wn+DolRHFxx8ZAZJRhLuISfWWp3RFo0OA=; b=bFBdxJbz8zF+GJFvTDYh9eVPDUVSda/GfCCST9csdNxQQxzBdwbva5JYAKcVSrTMnI ufZ73/DYJreWAXng3swmgSIOH5v1Uw6NTrn+5w64UzAFXTZZCQjA+QlNrNMj4odse/A4 oNPWXr1IUvQls2f3EGd1ZBBwMSuPzp3s47ID9xcb0rRzaQpgIVQvJBlZ/de8qVdJ63hx cJk1ExiybQiRFQByG/v0vzSfiEg5lnPw34EcKWFkkcKL+ws77ZYzxAfM8cPm1RkzYLt8 iQHu3fcAVmw6Ksv+NS/ShB2qxN4AN0sCEIaP7e0Sq39MTefp24nF0RyvPlkGM5eEYDfW u10w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038114; x=1722642914; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zSysp7FPh6wn+DolRHFxx8ZAZJRhLuISfWWp3RFo0OA=; b=es+K7bEvqA/PjlhgaFTTfjz/qiKEuKA7KaM0eMUHSF3zpYbQTLCjzOhsPVDq7gdu/R ZcLz9kvq4qodPWbSnzheYHolBz7Irzj16mLUhx73JPBYfF5yMvD7kz+OTopln5T4g5NB 9lepf+a32EEzf8FUEhuxYM0tzFfHgTrDZchIY0n4Fq+fl2tnkFmk1eCIz4rJERMPuYSB BhrLA5F2WwJdLrmE3ZniVCRQan69EyL4glhv1exifimZfjDueajy54HWAp2tMN9bn0Xw hMR8yntiSzRne85AtGIR5kdFnHoasjRgkL8xVcHE3PMWKKW/Oc8leZnCeZLetfKQtg40 L3OQ== X-Forwarded-Encrypted: i=1; AJvYcCUBr4wy5XTUvYVpH/iVUIJU+XGzh2qBRHipEHM4wD4GOGBr6rVmL4X4D8EkPnaotGb3u6t8y03uVCH6QAeZLbmDj18xrzBzXrPs+HBLBECA6PLMPxc= X-Gm-Message-State: AOJu0YxFqB/OfhbkDQGKZsVcSz64MtYdJQY39DaGteTcKfOzgtioaePC gFiMi/t10yL3vALfiTCylSkzT1h6yvNrBegBEYSL5eSthj45U9M6bOn+3TfADMlapNyApK0Ni5C 8nA== X-Google-Smtp-Source: AGHT+IHtdYLzf8VxCow9ze9t5PB7b90MwNFbCURT1XZzWgwcWEIftZd8Pqph9MK+hSFYPsX4JKQKNB1AEe8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1889:b0:dfb:1147:cbaa with SMTP id 3f1490d57ef6-e0b5459b0b9mr34694276.10.1722038113838; Fri, 26 Jul 2024 16:55:13 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:24 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-76-seanjc@google.com> Subject: [PATCH v12 75/84] KVM: Add support for read-only usage of gfn_to_page() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165515_975917_3ADE89C5 X-CRM114-Status: GOOD ( 15.72 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Rework gfn_to_page() to support read-only accesses so that it can be used by arm64 to get MTE tags out of guest memory. Opportunistically rewrite the comment to be even more stern about using gfn_to_page(), as there are very few scenarios where requiring a struct page is actually the right thing to do (though there are such scenarios). Add a FIXME to call out that KVM probably should be pinning pages, not just getting pages. Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 7 ++++++- virt/kvm/kvm_main.c | 15 ++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 91341cdc6562..f2d3c3c436cc 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1198,7 +1198,12 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm, int kvm_prefetch_pages(struct kvm_memory_slot *slot, gfn_t gfn, struct page **pages, int nr_pages); -struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn); +struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn, bool write); +static inline struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) +{ + return __gfn_to_page(kvm, gfn, true); +} + unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn); unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable); unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d0f55a6ecb31..16bc3ac3ff84 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3174,25 +3174,26 @@ int kvm_prefetch_pages(struct kvm_memory_slot *slot, gfn_t gfn, EXPORT_SYMBOL_GPL(kvm_prefetch_pages); /* - * Do not use this helper unless you are absolutely certain the gfn _must_ be - * backed by 'struct page'. A valid example is if the backing memslot is - * controlled by KVM. Note, if the returned page is valid, it's refcount has - * been elevated by gfn_to_pfn(). + * Don't use this API unless you are absolutely, positively certain that KVM + * needs to get a struct page, e.g. to pin the page for firmware DMA. + * + * FIXME: Users of this API likely need to FOLL_PIN the page, not just elevate + * its refcount. */ -struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) +struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn, bool write) { struct page *refcounted_page = NULL; struct kvm_follow_pfn kfp = { .slot = gfn_to_memslot(kvm, gfn), .gfn = gfn, - .flags = FOLL_WRITE, + .flags = write ? FOLL_WRITE : 0, .refcounted_page = &refcounted_page, }; (void)kvm_follow_pfn(&kfp); return refcounted_page; } -EXPORT_SYMBOL_GPL(gfn_to_page); +EXPORT_SYMBOL_GPL(__gfn_to_page); int __kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map, bool writable) From patchwork Fri Jul 26 23:52:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743591 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C3D20C3DA49 for ; Sat, 27 Jul 2024 00:34:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=gSJgbeqW0EjFVjI/rvx0LQaQxVZn1dW4kzDKJHwZpGY=; b=L9W3MqxqtoHtHduUkI/hs269AU xWZUvWtUI8gb3Aqr+ePBe7qI+TwteeGFygbKahwKhJ0KfgNp2SqQnUf9M9JjWd7USeDNhddvbW7sS rAr2iZTUiKp99ZGdtvciDWdpEJr7zvDRXLy12kQzPZazSd/VDsfW770J6QKh7GWTaD+y2CP6RGUib hTyJdHe+s5Zr77BhJ/0oq6IpSFmcMBG6n44tpI+PmGULLxuURjpTxhD+DuG8Aq2q2BgEMSTAfF98W IRYhvLzzJpAJ0YyGlglGsjCN64Wv9vHOJk4l8wgXqdEjKKQHu9/dAn+znMcVZ24NhR5c56R7PExAb jQ1imr2w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVOR-00000005knS-3bgI; Sat, 27 Jul 2024 00:34:39 +0000 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmL-00000005RKI-0OKt for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:21 +0000 Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1fc5e61f0bbso13367865ad.1 for ; Fri, 26 Jul 2024 16:55:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038116; x=1722642916; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=gSJgbeqW0EjFVjI/rvx0LQaQxVZn1dW4kzDKJHwZpGY=; b=dKLp4urTYxrMqWnh5jJzqr/fuqAAiRZmLGEYFfqMSjamPxxYI/rYGNYvxyFrPOfisz AFBinzC0qq1IayWZHdNsSTTRlU47sfBlyOb65kgFIAAAUR0dF+6ZjQHJZO4hbcEa7vdE It0VKyTq2bCDLg8Qj/LjbQyx/G9rcUHA2ZDOLP1GhivjReHm734NvWYNT/J4WKLBHCok 1xaPTSvMEDkHqOfC5FxlXcTPlzjfhKCwxeQJ/KvcPQrO1i+Xc2QSUt0nvkdqp+qkQNHl ytEUxKgq6APH6ehph8mxslmoJn4srt8DHQ3NejdesJriaNo/nKumleAAlQSLkbTRZLqF WsrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038116; x=1722642916; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gSJgbeqW0EjFVjI/rvx0LQaQxVZn1dW4kzDKJHwZpGY=; b=iRo1nCOwJDVsfdLzbpdcMiS0F2++n3ekPrNml5mzdDDmBEhZPiLXdhvrI+F5inPsJD 1CmJudBoVUeu20ayCC6qyB5UJ0C2c31WYOuAN5A18MGsRsXwbAz+ekvLkOS05dqRS9Lt NYIjYTJ1ROJ/PkEEYz2SEy0yLoM9ImoU+Y74P+k6hQQHmduItYWlXMuO1ohCq+rfnbJh awzsEMC4gaRbU2Q2acQi0Sy2dIizcq3HHkyly57BgYMD7zOLyXk9X6lOI9qF5GFcrnPv K9pqjWzBk4pcTup9djBII5v/dy90Blo5vDcBhM33NY8no8UZhpQD4GkhQJxu9hDp+qJH iVcQ== X-Forwarded-Encrypted: i=1; AJvYcCVGFpveQB7cKo5VkyYOtsZmiVA1ruSbs+IvJ7k9qxwaP+9fyM8I/pBFSdaKqJDLK7OC6bPOYymewzVcgpXYUbyqpohd2eyvvxKxaod7NRzKL9+LczM= X-Gm-Message-State: AOJu0YyMSgUUoPzdsfAAGE+wcjpaElnK3zQR9EuDHBKF/Se8atpmHUxa JmpW+WXHi8HWTHebZURYL11ABMT0cPyVyMAfXnr/rNLiGhWfF3/arPss10Q/eO7AXvTA6IReTHY uiw== X-Google-Smtp-Source: AGHT+IHxrkKUWCeUEpiaW8aWqxkJkE+eVd4nhH8cGKa9NS3y3LrlRDBQyz/B78NptpquvhhjWf0asoH4ivY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:c945:b0:1fc:733d:8465 with SMTP id d9443c01a7336-1ff0488cadamr599925ad.8.1722038115596; Fri, 26 Jul 2024 16:55:15 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:25 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-77-seanjc@google.com> Subject: [PATCH v12 76/84] KVM: arm64: Use __gfn_to_page() when copying MTE tags to/from userspace From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165517_382840_AC016A03 X-CRM114-Status: GOOD ( 12.73 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use __gfn_to_page() instead when copying MTE tags between guest and userspace. This will eventually allow removing gfn_to_pfn_prot(), gfn_to_pfn(), kvm_pfn_to_refcounted_page(), and related APIs. Signed-off-by: Sean Christopherson --- arch/arm64/kvm/guest.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index 962f985977c2..4cd7ffa76794 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -1051,20 +1051,18 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, } while (length > 0) { - kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL); + struct page *page = __gfn_to_page(kvm, gfn, write); void *maddr; unsigned long num_tags; - struct page *page; - if (is_error_noslot_pfn(pfn)) { - ret = -EFAULT; - goto out; - } - - page = pfn_to_online_page(pfn); if (!page) { + ret = -EFAULT; + goto out; + } + + if (!pfn_to_online_page(page_to_pfn(page))) { /* Reject ZONE_DEVICE memory */ - kvm_release_pfn_clean(pfn); + kvm_release_page_unused(page); ret = -EFAULT; goto out; } @@ -1078,7 +1076,7 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, /* No tags in memory, so write zeros */ num_tags = MTE_GRANULES_PER_PAGE - clear_user(tags, MTE_GRANULES_PER_PAGE); - kvm_release_pfn_clean(pfn); + kvm_release_page_clean(page); } else { /* * Only locking to serialise with a concurrent @@ -1093,8 +1091,7 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, if (num_tags != MTE_GRANULES_PER_PAGE) mte_clear_page_tags(maddr); set_page_mte_tagged(page); - - kvm_release_pfn_dirty(pfn); + kvm_release_page_dirty(page); } if (num_tags != MTE_GRANULES_PER_PAGE) { From patchwork Fri Jul 26 23:52:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743592 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 278C0C3DA4A for ; Sat, 27 Jul 2024 00:35:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=6h2fNXv3Lnt/1e1wvaAdv81wiOKkGDK2sT+OsnVLOZA=; b=2UTZCUEkYluhO7MaFcbKPNQQHM LsvPBce/JQNUn4+M/JJY5a/82ZMT2pklkA90JfYzCrPSOPPRmb51OTkBCf/qgs6BJ8hpjTMo+gVXr iyZMjVqP/tZLCnNY1bL0CGtdJ0DhAhyivMS8H0klxfpTFoQiVRPIMZv289Bgr1EUiojHQaPoTOYPS +3wQ5DNseGaKEohpz84ZQa81f8Z2rlzzTrckduXepePu9pZkTtc5X6haQV9VDz6/phPOEfDCu5wL8 fyzi2g8LfIGD7/Q7yXT3uL8ZvzpsMsZzMdFpg8/w4jGacV/q2KwOs/LFkNtF/gZETpHycv9lnYhrg 47MjQm7g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVOq-00000005kvZ-1jNF; Sat, 27 Jul 2024 00:35:04 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmN-00000005RMB-3Bar for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:23 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1fc5acc1b96so12431285ad.3 for ; Fri, 26 Jul 2024 16:55:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038118; x=1722642918; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=6h2fNXv3Lnt/1e1wvaAdv81wiOKkGDK2sT+OsnVLOZA=; b=bv6x8e7OzatgWEM16HtzYx9P+gZE7PcN+IJjInGpYx8HBYfvejXKBlTdfiXW+T+1hF DOMaMF6AivchVERIVt3iw1O1hk17BS5qwimdf2dc91rSqXlUAiWZKQMzv0jBwbEZnWCW gBfTdwQPmncfUlLNA7Mwe8X0Zz52tFczeLXekeUP3aMR63YIlv63KTDnJFiSvM1N7rXe gxyQSjzpztUkkYIKd0hWwG97nj3aDfxpYghsBnUGyoZyMD3HslXy5oQJKAEIfZMi9cgf bz1/XI3D85JPaOLOEGxWUxbSU9BpG9zYyVZnXAgHlL64p1pFuV5sbVPuwX9PmVza/87O N0Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038118; x=1722642918; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6h2fNXv3Lnt/1e1wvaAdv81wiOKkGDK2sT+OsnVLOZA=; b=eaoyLDfyWQlpcYjXD9KAOUetrS7adm64R3LUo6fwwiGVWR19zc5Ho392z7xxA6+mpD Jq9J/LW6pYEMVKKeqmXHRGInR6Waz3u4B2it6LTmX5s/HI+6OsqgSYbme3HuyY+Z13dc 0CQBw2ErRciXKMPT8D9BbYZD6443/6X2tEAZIf88t9tfDvsI/L5UWC9gr137bJDYMHk3 9Aot2sF2Wat6u8GTyITxb1k7J3RLG2s45Eo9LcU7XW1bqJozXnuvdD8grEXKDYoTG5CZ VaXa6PH1NP4qI7ic61S5+Yp6xXsjMk8cv/7Ht8AxdUHRhYAj21E2taYHOSXh8ZUnxFY6 6hqg== X-Forwarded-Encrypted: i=1; AJvYcCXgzYpAJnm3Ceb2fP8zKZ7xVjqDHRgMmP6ONppcdGmJsQlW9nwXX1vYe59WU5zIOIVk5xaN9VjVdY+T/V65HQ2lBmPrGV53O6RR61jEx9cj1O3cEzA= X-Gm-Message-State: AOJu0YyNcVUPaN41hf/42jkj0nyp4TlsG/lUq87cX9MZcZWnyPncJWe5 mn8Wm6/MsWDgGZrTJ9+wZ2+aRlG86eJf+5yCX7xxue7t7LpEY6QXwC+RimWYjBT8av2amIY+FHW w/A== X-Google-Smtp-Source: AGHT+IEudPW7JEwYI6UrgE0ex8JC6EzLtvCIl4+qVoUW1lZq2cn7k6X5ytnrRNqYuzfOTIfNZ2CfrPbG7k4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:f693:b0:1fb:b3f:b9bf with SMTP id d9443c01a7336-1ff046e1217mr520495ad.0.1722038117916; Fri, 26 Jul 2024 16:55:17 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:26 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-78-seanjc@google.com> Subject: [PATCH v12 77/84] KVM: PPC: Explicitly require struct page memory for Ultravisor sharing From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165520_286354_F58D821B X-CRM114-Status: GOOD ( 11.89 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Explicitly require "struct page" memory when sharing memory between guest and host via an Ultravisor. Given the number of pfn_to_page() calls in the code, it's safe to assume that KVM already requires that the pfn returned by gfn_to_pfn() is backed by struct page, i.e. this is likely a bug fix, not a reduction in KVM capabilities. Switching to gfn_to_page() will eventually allow removing gfn_to_pfn() and kvm_pfn_to_refcounted_page(). Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/book3s_hv_uvmem.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/arch/powerpc/kvm/book3s_hv_uvmem.c b/arch/powerpc/kvm/book3s_hv_uvmem.c index 92f33115144b..3a6592a31a10 100644 --- a/arch/powerpc/kvm/book3s_hv_uvmem.c +++ b/arch/powerpc/kvm/book3s_hv_uvmem.c @@ -879,9 +879,8 @@ static unsigned long kvmppc_share_page(struct kvm *kvm, unsigned long gpa, { int ret = H_PARAMETER; - struct page *uvmem_page; + struct page *page, *uvmem_page; struct kvmppc_uvmem_page_pvt *pvt; - unsigned long pfn; unsigned long gfn = gpa >> page_shift; int srcu_idx; unsigned long uvmem_pfn; @@ -901,8 +900,8 @@ static unsigned long kvmppc_share_page(struct kvm *kvm, unsigned long gpa, retry: mutex_unlock(&kvm->arch.uvmem_lock); - pfn = gfn_to_pfn(kvm, gfn); - if (is_error_noslot_pfn(pfn)) + page = gfn_to_page(kvm, gfn); + if (!page) goto out; mutex_lock(&kvm->arch.uvmem_lock); @@ -911,16 +910,16 @@ static unsigned long kvmppc_share_page(struct kvm *kvm, unsigned long gpa, pvt = uvmem_page->zone_device_data; pvt->skip_page_out = true; pvt->remove_gfn = false; /* it continues to be a valid GFN */ - kvm_release_pfn_clean(pfn); + kvm_release_page_unused(page); goto retry; } - if (!uv_page_in(kvm->arch.lpid, pfn << page_shift, gpa, 0, + if (!uv_page_in(kvm->arch.lpid, page_to_pfn(page) << page_shift, gpa, 0, page_shift)) { kvmppc_gfn_shared(gfn, kvm); ret = H_SUCCESS; } - kvm_release_pfn_clean(pfn); + kvm_release_page_clean(page); mutex_unlock(&kvm->arch.uvmem_lock); out: srcu_read_unlock(&kvm->srcu, srcu_idx); @@ -1083,21 +1082,21 @@ kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long gpa, int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gfn) { - unsigned long pfn; + struct page *page; int ret = U_SUCCESS; - pfn = gfn_to_pfn(kvm, gfn); - if (is_error_noslot_pfn(pfn)) + page = gfn_to_page(kvm, gfn); + if (!page) return -EFAULT; mutex_lock(&kvm->arch.uvmem_lock); if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, NULL)) goto out; - ret = uv_page_in(kvm->arch.lpid, pfn << PAGE_SHIFT, gfn << PAGE_SHIFT, - 0, PAGE_SHIFT); + ret = uv_page_in(kvm->arch.lpid, page_to_pfn(page) << PAGE_SHIFT, + gfn << PAGE_SHIFT, 0, PAGE_SHIFT); out: - kvm_release_pfn_clean(pfn); + kvm_release_page_clean(page); mutex_unlock(&kvm->arch.uvmem_lock); return (ret == U_SUCCESS) ? RESUME_GUEST : -EFAULT; } From patchwork Fri Jul 26 23:52:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743593 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 13EC8C3DA49 for ; Sat, 27 Jul 2024 00:35:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=B5cS3fQCyroPznI2D76vYSWht1aEO90tb+OiOa938C4=; b=m3TPA7xw89WjPTYXk/uH8wh29/ lbjDbnUNp5wRpxuqv2LntcIszNt1+oe8akACkuMl6uNuVg9ebMjzRERh8gXGwZ13FhVibF9ivz2fT 5KKdjXFkURHexKT7IBT9Oe54DIBseZ/+fxN8EYaB+NfwSQ/H84Hz8zwMkTYK37Z67GoXjYR+51r/F hRaxP6D5jrT1ZhxsuQqaa1rLIAUhxRH+7oPWfXzIyoyiP2/42fSo+cK96nfhgCQ4QlYzuRNCuL+th icxllqwWMYrjpXDPkktZK/eA6L1W4QEJP4GgMM5Ed3LTJzxqF4aZr3th2pMx9db9jQWyPVEGPJYs3 B3N8p2fQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVPD-00000005l1c-2YeI; Sat, 27 Jul 2024 00:35:27 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmP-00000005RNW-30IT for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:27 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-664bc570740so4647547b3.0 for ; Fri, 26 Jul 2024 16:55:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038120; x=1722642920; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=B5cS3fQCyroPznI2D76vYSWht1aEO90tb+OiOa938C4=; b=IBsDS6Weschrfr/Y8bjtKGCfkJKvEgOR0klko/q5tcsUNRZEdF0j8jjHz0gSgRdeQX EYVYT2ei2pPK+ar54+5intb3VhJfjDgfLPmLeg+ja3Sg73mIxnIGkwAhtxhFZAEAMwVQ Tg+KhTBFyI0pJclVZHFg3yp/K1Ev6skm/7du92jK3tPFxOqhTtCb62P5U8LLxBBsENhz 46ZW6fbkKlGDiqls6wwB5gT6wR5w4bDosvoqDyoneDzLMfBpgd0gKdmE5N/iY21Er0FE zNs7O8DElN2bzSBpPuT1dkf6aavBvaAeg0Bn14IJO9wV+VajhaQ1PZrNgr5LVP/EIjWA Kodg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038120; x=1722642920; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=B5cS3fQCyroPznI2D76vYSWht1aEO90tb+OiOa938C4=; b=GCLGHEwjqqFjeJW3lyz+YM8rcEzwDHErf5GpK/dhTIQu+QqX4/E7pXgzPjSQRIJJll WoLWYWPkTWY8hZOfE21IXkfv7KCTigRwaSFZBKS9jfx3rLiB+IYXBIOKnsWdvJ5Yr/dj 0ajQtE4hNMLxg9L0bDmzqVYtLZnVW3VZ7mOk7h22I5Tp6uYx4gZ8XjOV+FeZTXSU3vrG qrJ68krJfHRU1Tq+zi1Saw0FVci7iyqMXtEeO5/AJuwvF0+RqcjCEbnUyDhIgDJMZbV6 wqOXrnA5X3TRrxdMVUF8hOOU7ngQMPQIQVmIuXbHhL53gxWch5wj6pKl4HryWJYRy5ct Uvpg== X-Forwarded-Encrypted: i=1; AJvYcCWhpNSjXVTCmuKovmgFIF47+Vxefd6nc4jmdMB1M9V9clHQocGdo5pinfwLIlpUSw+EfMMz3heXLYrZD7Xr9cvclTkXArRCYqj54PGROpJP7uLZjb0= X-Gm-Message-State: AOJu0YwEjpeZI6tagmRxwm3SvXeS8/mzM76R0xUslupGb7MDSThdZ2Bd jSr2veEX1yO9F4FyW4SmN65PiOkKiEy9y2hycTpA3zpuhwhBvPgwCPc5d97H958PDpwb4K+oxso IHQ== X-Google-Smtp-Source: AGHT+IFMnVGgr4kpL4f9GKaKFvGezuIshpJ8eZPypNhpoA7Mjxc5gf+mvj1jThnEcafAjrCOymr+lPlnWyQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a5b:606:0:b0:e0b:3139:7ce5 with SMTP id 3f1490d57ef6-e0b55d079camr27265276.5.1722038119814; Fri, 26 Jul 2024 16:55:19 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:27 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-79-seanjc@google.com> Subject: [PATCH v12 78/84] KVM: Drop gfn_to_pfn() APIs now that all users are gone From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165522_093945_3F995AA4 X-CRM114-Status: UNSURE ( 9.51 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Drop gfn_to_pfn() and all its variants now that all users are gone. No functional change intended. Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 11 -------- virt/kvm/kvm_main.c | 59 ---------------------------------------- 2 files changed, 70 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f2d3c3c436cc..34a1cadb1b80 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1260,14 +1260,6 @@ static inline kvm_pfn_t kvm_faultin_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, write ? FOLL_WRITE : 0, writable, refcounted_page); } -kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn); -kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, - bool *writable); -kvm_pfn_t gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn); -kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, - bool interruptible, bool no_wait, - bool write_fault, bool *writable); - void kvm_release_pfn_clean(kvm_pfn_t pfn); void kvm_release_pfn_dirty(kvm_pfn_t pfn); void kvm_set_pfn_dirty(kvm_pfn_t pfn); @@ -1342,9 +1334,6 @@ unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn); void mark_page_dirty_in_slot(struct kvm *kvm, const struct kvm_memory_slot *memslot, gfn_t gfn); void mark_page_dirty(struct kvm *kvm, gfn_t gfn); - -kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn); - int __kvm_vcpu_map(struct kvm_vcpu *vcpu, gpa_t gpa, struct kvm_host_map *map, bool writable); void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 16bc3ac3ff84..5dcf3561b829 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3059,65 +3059,6 @@ static kvm_pfn_t kvm_follow_pfn(struct kvm_follow_pfn *kfp) return hva_to_pfn(kfp); } -kvm_pfn_t __gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn, - bool interruptible, bool no_wait, - bool write_fault, bool *writable) -{ - struct kvm_follow_pfn kfp = { - .slot = slot, - .gfn = gfn, - .map_writable = writable, - }; - - if (write_fault) - kfp.flags |= FOLL_WRITE; - if (no_wait) - kfp.flags |= FOLL_NOWAIT; - if (interruptible) - kfp.flags |= FOLL_INTERRUPTIBLE; - - return kvm_follow_pfn(&kfp); -} -EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot); - -kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, - bool *writable) -{ - struct kvm_follow_pfn kfp = { - .slot = gfn_to_memslot(kvm, gfn), - .gfn = gfn, - .flags = write_fault ? FOLL_WRITE : 0, - .map_writable = writable, - }; - - return kvm_follow_pfn(&kfp); -} -EXPORT_SYMBOL_GPL(gfn_to_pfn_prot); - -kvm_pfn_t gfn_to_pfn_memslot(const struct kvm_memory_slot *slot, gfn_t gfn) -{ - struct kvm_follow_pfn kfp = { - .slot = slot, - .gfn = gfn, - .flags = FOLL_WRITE, - }; - - return kvm_follow_pfn(&kfp); -} -EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot); - -kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) -{ - return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn); -} -EXPORT_SYMBOL_GPL(gfn_to_pfn); - -kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) -{ - return gfn_to_pfn_memslot(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn); -} -EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn); - kvm_pfn_t kvm_lookup_pfn(struct kvm *kvm, gfn_t gfn) { struct page *refcounted_page = NULL; From patchwork Fri Jul 26 23:52:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743594 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 89A62C3DA4A for ; Sat, 27 Jul 2024 00:35:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=dTOlfH+mhloY9mYg+/p0IQai80b+6RXS/WhBEV7av3U=; b=fxV5jJvYTZp+3cE2e9G8/I0cUw Ht/R45aESjG6Dk8Ncmm17nrIunRoL9u82l6k5PTBvTpF3w+EapoitoafFIdFtVGJKQ3aQsid0cZny EHc800dknkxAEdjugNztHXSSYkOg4BO7tgVer2ANtCEAL6kKef4Xv67ZhzvoknWNp25CWl5bx0XPa YabyYNcBypuiGdaz++51PkxnJhERBdIvyaKZUo3yGMR9lKQrgBYMnNAz8v/LRPLtRgFZ3LxZ0tOj1 V9ZSERiTu8D87eU7epmda6kdmxGnmsctltLB/fe40pWX/cTLLvFyiD643xlpha4o4PIPsekv3GZaH uLA/YBxA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVPa-00000005l7Q-0PGP; Sat, 27 Jul 2024 00:35:50 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmR-00000005RPs-1xNn for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:28 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-66c0f57549fso7730627b3.0 for ; Fri, 26 Jul 2024 16:55:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038122; x=1722642922; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=dTOlfH+mhloY9mYg+/p0IQai80b+6RXS/WhBEV7av3U=; b=dlz2WeJdMDVSn0QF2JM5EqHFTTi3/lVh8XhLstjFhRRg6JglfIlf2kMFLDQ3AM9ewI L8n3w4EA57DkccusDFhkxvp8TUOcV78Pw1oTScwprBhyluesmBvULgAhYXmS0mhd36Sg pmJPVAta9EmSFUcaLAaRwvpPJPHuLkQCKpCQtFqnSux5gUyFQuiNdOfupZ4OAjDVlCoa 8WNK/eotizs3VYgMuNZEvubQiCB9+Eqa2zFZJ8euD0DHf189k2p3VPLTlGVIZnKdlhq5 XmG2zIRE3reeo5AcObRl/+eH61qd44wFO9H0k4letPuzhqqxrfy/ffO69JEddMWjZOYd S0Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038122; x=1722642922; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dTOlfH+mhloY9mYg+/p0IQai80b+6RXS/WhBEV7av3U=; b=byrv7xm5zwSQ9m8h/POQjkBhsRQ85Uwg5iwREbsjmfIlHNGJZIvlfW1ifnT5SxKBzP uTqrt1ejPlPAohJ0spIQAj/7RvLn+CDxTMR6qh436D5YCrIJR8YDZOaTTddrWuaqTEU2 +EeyEl9Ka3mY/BZWOBotEYbVP5bYY8FxUbWdcGTdpamVCrDjkWZ1fmP+JJUXZufhecAB fqJj4dYdwtTz/LxIw7o7FJC2K9JpU0vnGslGwLsf5NW2aMxk3mxsfx0KhlYnJEsHbKv8 MB8jApFqglkHAhpuX14d59tuPCKejsb8TBlcKfywyLWeshbLwyeOwoUKuqlikjooiDKx 1ztg== X-Forwarded-Encrypted: i=1; AJvYcCXpFm21ICCFyaF5/+R5b9P0ysDkDj830cwUYVqwGjja2FbPTBl4RvMYXZ+zqQJ2kqynUv3Cc4A2/WzqyDxmvIZiWx6JenB4ttutzRb23j2xRXBYAVc= X-Gm-Message-State: AOJu0YxT3S9AKrvNgsn8W6TT4uoXnZvQdL26G8Er6Czq6eIb3hiKVkHN z+xUgPqYkIixkPmH/sINq1nfjUY0LL8NaIHlSAUS1c4PeXWsTMhBdQ+DWfgYxBTQ2LhfHNzXurx xSg== X-Google-Smtp-Source: AGHT+IGgifSFTGz9fVU5jdzVocA5wMp2MITnk/v5MkueXs++Udesz0kCsEkHm8VEDJpbtR+Hv7uv12YKgg8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:f0a:b0:62f:1f63:ae4f with SMTP id 00721157ae682-67a052e5dccmr429907b3.1.1722038121721; Fri, 26 Jul 2024 16:55:21 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:28 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-80-seanjc@google.com> Subject: [PATCH v12 79/84] KVM: s390: Use kvm_release_page_dirty() to unpin "struct page" memory From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165523_687987_D96CDB67 X-CRM114-Status: GOOD ( 12.81 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use kvm_release_page_dirty() when unpinning guest pages, as the pfn was retrieved via pin_guest_page(), i.e. is guaranteed to be backed by struct page memory. This will allow dropping kvm_release_pfn_dirty() and friends. Signed-off-by: Sean Christopherson --- arch/s390/kvm/vsie.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/s390/kvm/vsie.c b/arch/s390/kvm/vsie.c index 566697ee37eb..f6f1569be1cc 100644 --- a/arch/s390/kvm/vsie.c +++ b/arch/s390/kvm/vsie.c @@ -670,7 +670,7 @@ static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa) /* 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_pfn_dirty(hpa >> PAGE_SHIFT); + kvm_release_page_dirty(pfn_to_page(hpa >> PAGE_SHIFT)); /* mark the page always as dirty for migration */ mark_page_dirty(kvm, gpa_to_gfn(gpa)); } From patchwork Fri Jul 26 23:52:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743595 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 83AC8C3DA49 for ; Sat, 27 Jul 2024 00:36:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4JkhtJX/rWB1cBzt9zuFqY6FqfPwx7EIf6/sG5QRcB4=; b=CsbqniWIwu/D0/T15rYUqVcHrK enSqlUtGZqoSklsiHYSLW82DGZYkcXLXkj3eEpZ7iOUNZ5YiwhC0d5fJWFpfcLwuisDof607vksxt TWAKlthr+tsUPL0sY+K6cKSKn2wFt7egZn1RpufFIb/Yi8AMiscN+HcVPUQirRCiQ635t8ij1F92l EMsHSKvbmOh2k/0G71tn/1BDBRvou7ls9YSF58V6E4KzUFHpx4dEBGG9ohQvE9qnb9MQ76XTdzdAl o/5BlhDo8QhWzYK43lgszJIMIWkqAzKcy6V75ca+6Fi7dKG6Lru/c95mTigpqdWU+n2hzU7c5fY5l OMiwW3wg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVPx-00000005lD9-2KHN; Sat, 27 Jul 2024 00:36:13 +0000 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmT-00000005RRU-2ppR for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:33 +0000 Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-7a1188b3bc2so1413228a12.2 for ; Fri, 26 Jul 2024 16:55:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038124; x=1722642924; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=4JkhtJX/rWB1cBzt9zuFqY6FqfPwx7EIf6/sG5QRcB4=; b=MLKVdw5V+Whed1vnGPvWRKhJPSsV57TBDnpeYP8PC9LQgpLuMBAO8jAtp+vCaptgWw FUignf03pGkW06O7PhqKJtlEFW79hLOmj2HvYKwKJX4sxu4eCPZDsSYaZ/E7ErwbIVY4 G6dO58cvROtEduyZPtTBsVpeLDSR//Xdxg7b8rx7Xfai0EHbS3e9XOLOwdJDQczvwo3z W0GCT70N6Be0NCUt91M+x82o7oHiq6YV00WiHAB4uk7B3FpR6+j1diWxfT1k5bQft54T MTlDJriqVmv8FxOIKR3c4e6FRnW0dY/+m3Jbk7D45wsh91LGN3D+r7AS4SK31rFajAHt yQww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038124; x=1722642924; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4JkhtJX/rWB1cBzt9zuFqY6FqfPwx7EIf6/sG5QRcB4=; b=klMF6nK+61TVY/xOMTt2nxRasEwaZvENJyOG9ymtDeHZI1ZYOomIOlMZFj6HCNFUHr +P+eatUSGmpWtLABiODRZzzs1j8Oqu8KAKhu7kQP57l81M2a+GCCOaYjkPD7gnAruc/j uLKh4Fttfqp7jUq88d9TfNfENysklqMeM078TfsU+RxmwrXk475vtnx6ce9znQWA0RFQ tP8nb5x5hO8p5QiiOP3YezW4K4JM2cNJ8NGjSYGOoLJRfkJ3M4waUOURDEk1oQrNi640 Vpz6C6i16iKksqXAQqzpYmq8jVhbVxb1FbmVdDX7zmnGR1xWk/gAzTDMdVDEOG+Ke2RH R9Bg== X-Forwarded-Encrypted: i=1; AJvYcCXyVmCVcozhYZlk1w8Ajr89YU/pSsTwctJJib7p47V7tz57YpPdYm8SYefyybkM3cBmWX1pdUCuNyGTF2l7aJRJ9//fwSdbCnCJvyi3S597VTnhsgQ= X-Gm-Message-State: AOJu0YxucIxIwIOwCFlbTVEDr8wPz+1JehnSETLWUfrPT90exSj+RJjl 8fg46V3b6dmU8qIqgahUbxmeTYGHEgFtk77kP1MJ9Y8rQ3ikRKaBkEtf7OebdH0uJSRcZTXILnQ bUQ== X-Google-Smtp-Source: AGHT+IFPQa1UjlYj7+Kv/jUhsXC3P8YtU5HtBxuA0lRTgUmwYvAf/3a7bhqWFsOX8QFlAkrWz2XoripRfzc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ea03:b0:1fb:7f2c:5652 with SMTP id d9443c01a7336-1ff04893401mr182655ad.7.1722038123783; Fri, 26 Jul 2024 16:55:23 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:29 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-81-seanjc@google.com> Subject: [PATCH v12 80/84] KVM: Make kvm_follow_pfn.refcounted_page a required field From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165525_847375_6E161A5C X-CRM114-Status: GOOD ( 11.33 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Now that the legacy gfn_to_pfn() APIs are gone, and all callers of hva_to_pfn() pass in a refcounted_page pointer, make it a required field to ensure all future usage in KVM plays nice. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 5dcf3561b829..030a08d4b21d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2844,8 +2844,7 @@ static kvm_pfn_t kvm_resolve_pfn(struct kvm_follow_pfn *kfp, struct page *page, pfn = page_to_pfn(page); } - if (kfp->refcounted_page) - *kfp->refcounted_page = page; + *kfp->refcounted_page = page; return pfn; } @@ -3001,6 +3000,9 @@ kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp) might_sleep(); + if (WARN_ON_ONCE(!kfp->refcounted_page)) + return KVM_PFN_ERR_FAULT; + if (hva_to_pfn_fast(kfp, &pfn)) return pfn; From patchwork Fri Jul 26 23:52:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743596 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AE951C3DA49 for ; Sat, 27 Jul 2024 00:36:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4pm2gWEOx5QJNc32HyFmIStymfHzlyUTPI/gy9LPzsU=; b=Lpb8XO7gN2bAZwXlTqWPGBbJVn MAC/LHy9pXN9m45TO/Yp9wAgWksnE9ouM8YqZovP7h0/P5hGPGbIOov0ak8saWjl3bbKU8OSqEtIJ SeDgg1g+EsGfMxmxyaXbGLCE/cUo3bSYR6sZpDBmxzqhyCkpTUDbYkVpc0TP8DIzh1AuUCDsGW5Kc TZrVbUUxfVlWrObj8J15VkHyzhMxZt/MFhyJ7fSj2NffhNeboeZPi+5c5CrGL6z6EDlliTf5dNGFZ oTsGQ8B5afV9glNbpia9h+7BTxx6iSgttxAyNdr9vWVj00YGWNiIoEH06HoIN5VxFDSR57J0oCVlQ ZIYZ3RWg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVQL-00000005lJp-1FA2; Sat, 27 Jul 2024 00:36:37 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmV-00000005RTL-1mDK for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:33 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e03a694ba5aso409881276.3 for ; Fri, 26 Jul 2024 16:55:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038126; x=1722642926; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=4pm2gWEOx5QJNc32HyFmIStymfHzlyUTPI/gy9LPzsU=; b=Kb97cyn3ZSPd1Kme2ztN+oJnnKW9PTfV51qVVmubcKxsXEVk/aCxJGVCCeijYAJci1 GIf4j1sGIh4t/D2ZK2r+amSALvGUoy5GiVgJJLFcOI3ZLDwBRfwBuJosYx82UUN8XHLb ETwHs8ZNmHHn1r9imNoku+g9muOrSjqGrPrqI39ZnXFAXq5qarJczhwrz4IZJZX7dMaM xXz4AzVdj1/pMm/WbTvIQerFErRbwOqzetGvjzygmFhGd/0ezXI8HEI0k9YiiLjCgady LedNf0PdwU2KhxFyyA3dSirN1+OhY4DE82IJ1AQ3s5QwqptP7pnoVKUaOE+NSmydRASO E9tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038126; x=1722642926; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4pm2gWEOx5QJNc32HyFmIStymfHzlyUTPI/gy9LPzsU=; b=HvC1YQVoEIxjIrdicoFEpa+HQMOTtTuChfVvkG4p/vsp0ABlfkot/jeQuPsoCCKa+t Ke6WBKdgfHtC2ZxLHHtnU5se/4GgFr+yk2wBE2a8Isg6J4KhqxaGXn7SBxsTQRwLN1AZ BEVBR4cXyrlDmkPmN5AeBAU4A0k08vj2U0JVpfE3DY5BcsPm5RAX7lyTGRP8pyZ94KKt R4K9bef+YnHphJuxaYfVynXu/JnUIQIn6+8WJT0ESrR79AbrD54RBqaBCkk50u52PD9a RRNlrQeQ1rKy+ayKvu7Q86aTOTx81oz8wr37OuX49jUJ+Ie+wX2Lmnh9Bi/jGk3XN9i3 AkCw== X-Forwarded-Encrypted: i=1; AJvYcCW4SY+TMBTwimrGYz+zWOVdkVSlxzuoJ4TRRIfNP/YABgwO65HhGeT0Uco5Vlwxs+gmdlLmdGMfNZomlRHgTlMFzBjdSemj1SbFnHqizXtWkfAfkCY= X-Gm-Message-State: AOJu0Yzop7h1mUDogA6sc4NvTjwURWgvgghoLleA26AlTXo/+MSOwhc0 lnMbkCZjcokHYwJsFwOXrXvsqUA5kyhXvlBajGb29yOj++agbG1/j4Y5Qnc6b2pRweu/hKDHYCE rLw== X-Google-Smtp-Source: AGHT+IGgyahrOCg0Ix+MrKo8QCm20LiC8Zd9dP6asKSX3o/8So/b0NT46HfwZKTclpC/RfFjW09NkNDMj3A= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1029:b0:e0b:9b5:8647 with SMTP id 3f1490d57ef6-e0b544ec4ddmr2334276.8.1722038125883; Fri, 26 Jul 2024 16:55:25 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:30 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-82-seanjc@google.com> Subject: [PATCH v12 81/84] KVM: x86/mmu: Don't mark "struct page" accessed when zapping SPTEs From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165527_624839_69E09EAB X-CRM114-Status: GOOD ( 14.83 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Don't mark pages/folios as accessed in the primary MMU when zapping SPTEs, as doing so relies on kvm_pfn_to_refcounted_page(), and generally speaking is unnecessary and wasteful. KVM participates in page aging via mmu_notifiers, so there's no need to push "accessed" updates to the primary MMU. And if KVM zaps a SPTe in response to an mmu_notifier, marking it accessed _after_ the primary MMU has decided to zap the page is likely to go unnoticed, i.e. odds are good that, if the page is being zapped for reclaim, the page will be swapped out regardless of whether or not KVM marks the page accessed. Dropping x86's use of kvm_set_pfn_accessed() also paves the way for removing kvm_pfn_to_refcounted_page() and all its users. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 17 ----------------- arch/x86/kvm/mmu/tdp_mmu.c | 3 --- 2 files changed, 20 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 2a0cfa225c8d..5979eeb916cd 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -546,10 +546,8 @@ static bool mmu_spte_update(u64 *sptep, u64 new_spte) */ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) { - kvm_pfn_t pfn; u64 old_spte = *sptep; int level = sptep_to_sp(sptep)->role.level; - struct page *page; if (!is_shadow_present_pte(old_spte) || !spte_has_volatile_bits(old_spte)) @@ -561,21 +559,6 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) return old_spte; kvm_update_page_stats(kvm, level, -1); - - pfn = spte_to_pfn(old_spte); - - /* - * KVM doesn't hold a reference to any pages mapped into the guest, and - * instead uses the mmu_notifier to ensure that KVM unmaps any pages - * before they are reclaimed. Sanity check that, if the pfn is backed - * by a refcounted page, the refcount is elevated. - */ - page = kvm_pfn_to_refcounted_page(pfn); - WARN_ON_ONCE(page && !page_count(page)); - - if (is_accessed_spte(old_spte)) - kvm_set_pfn_accessed(pfn); - return old_spte; } diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index d1de5f28c445..dc153cf92a40 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -861,9 +861,6 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, tdp_mmu_iter_set_spte(kvm, &iter, SHADOW_NONPRESENT_VALUE); - if (is_accessed_spte(iter.old_spte)) - kvm_set_pfn_accessed(spte_to_pfn(iter.old_spte)); - /* * Zappings SPTEs in invalid roots doesn't require a TLB flush, * see kvm_tdp_mmu_zap_invalidated_roots() for details. From patchwork Fri Jul 26 23:52:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743597 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 31D5EC3DA4A for ; Sat, 27 Jul 2024 00:37:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Truk8wLwk4Y9wyHD0oQlYUKlIPfs2FyGYSCSiM9XszU=; b=QHz+0q6WMfsn91G7m9EdDSn3fQ ft6paFXefWjqCEWzqgpYowX5onKmMi6GJG2H24xtRi4WdfYanUslIeaV4HgsZ6osgBfEelFMfXyyl D500zTAh3MVO1LlwIm6COgBuFc7FNViLLuS+52472s6VQTHcIcDIzA0tOlLHro7N9aDjes2u+XpKl BTqw6aQqBzTfsPyUqf4w5OZrjcasEzJpmca2ORtslo7/wONXKd/7N/yv1llocl1MFzGhq33IdvwMn MSjIz07vJj2n9V6o+/p2klyRB1Wz2+Kq9LTRZCgPFhk8TXYinrNU2iLIfyGax3Wb1qCDJw1GyPox6 YijKR+xw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVQi-00000005lPS-2XXJ; Sat, 27 Jul 2024 00:37:00 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmY-00000005RUo-1lxL for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:34 +0000 Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-70d392d311cso1417696b3a.0 for ; Fri, 26 Jul 2024 16:55:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038128; x=1722642928; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Truk8wLwk4Y9wyHD0oQlYUKlIPfs2FyGYSCSiM9XszU=; b=RNz0nDzIpxoTBUh4tdcsi4kFAHXeULeFBfBh4I4ytUquS1dSF7VyeKAdsORCpaXeT8 wFz5yyLM1Bx6PNr9q665YpoIjNaLksaBYn36blC6vJFWF9flc2CLT86+wXJn9DZ3tbam fzwtsXq/i+bC+2iLKk3RsM8I3Et0AcDMCLiE4sJywkVU0PfgoIAE7RpLYZ7bjevGkdhm 6CA3QDtuwuZ100OzKDvkyDoJ1EIIRCI9f9UnpbBGufQZHV8+ZFeu1KPJj8G6wJLO56mW bYBqw1FS/iE+EoievT8N2ZktbVtxbzw3hw2b8tuZwO3ZPDU3MSueEqb5dL3gBjULn4Am IaYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038128; x=1722642928; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Truk8wLwk4Y9wyHD0oQlYUKlIPfs2FyGYSCSiM9XszU=; b=VVrOYuU1RARJZs2JB3TlQgtbYF3NpTpiQRifmW0T09vdLj+ZKmignD7sveMfO6pqUQ mBTqVsZEJ2rB9AoikNK864GVbMHxlQq6gN1QE79AqFpKbbeQCBX4E6mS3YYKNToGyjP6 ODTFQyseiDFYQzLZmmDmNojx6/Pvob74d7Qwkx16e/J2wcElXF95K3ETkl79Yq2p2yZ2 u7sfD1K9+Cpd0xH1gxF7C54YZgfECrYTEBvFdHd5EvigHxZb2I79pIRktlQFkJc7FmcO +5UIhRrmSB+92SWjOfahfKPzsPXiDp7opPxkpbTkCCfnFCheCTj95lI5nJVpGiOb+NLr gxmw== X-Forwarded-Encrypted: i=1; AJvYcCWG/PMf92sv/o/84v+SyMD8mfAHi//ASRDbSoe2xWQE24oXWt1eYUd6E/qLF6uYCJwm1pH91CmntDF/tlfOv/ehm/mfQEaScoX/azAmYLq9MK4FudI= X-Gm-Message-State: AOJu0Yx4pHOATs8HGXlrPp12VHphD1RKmpnbXKRB9DmAqCRde5hQPfKi km76eU5guTWinWVsPkO7zOyqFKfzr8gtyQd/1XTgqgKWnwwAXCspRRIkAJPurIvQC+zM2t4s/X2 7Rw== X-Google-Smtp-Source: AGHT+IESez/zxTWutjtLJAUJhIiTiAtRpWSvjJWmuvSpH774la09rZR91czqzlEpBwGUZ+0osFi4L0/wScE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:8591:b0:704:3140:5a94 with SMTP id d2e1a72fcca58-70ecea0e7ffmr15968b3a.2.1722038127870; Fri, 26 Jul 2024 16:55:27 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:31 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-83-seanjc@google.com> Subject: [PATCH v12 82/84] KVM: arm64: Don't mark "struct page" accessed when making SPTE young From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165530_671471_31E58BA0 X-CRM114-Status: GOOD ( 13.78 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Don't mark pages/folios as accessed in the primary MMU when making a SPTE young in KVM's secondary MMU, as doing so relies on kvm_pfn_to_refcounted_page(), and generally speaking is unnecessary and wasteful. KVM participates in page aging via mmu_notifiers, so there's no need to push "accessed" updates to the primary MMU. Dropping use of kvm_set_pfn_accessed() also paves the way for removing kvm_pfn_to_refcounted_page() and all its users. Signed-off-by: Sean Christopherson --- arch/arm64/include/asm/kvm_pgtable.h | 4 +--- arch/arm64/kvm/hyp/pgtable.c | 7 ++----- arch/arm64/kvm/mmu.c | 6 +----- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index 19278dfe7978..676d80723c38 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -632,10 +632,8 @@ int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size); * * If there is a valid, leaf page-table entry used to translate @addr, then * set the access flag in that entry. - * - * Return: The old page-table entry prior to setting the flag, 0 on failure. */ -kvm_pte_t kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr); +void kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr); /** * kvm_pgtable_stage2_test_clear_young() - Test and optionally clear the access diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 9e2bbee77491..6679e02a02c4 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -1287,19 +1287,16 @@ int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size) NULL, NULL, 0); } -kvm_pte_t kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr) +void kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr) { - kvm_pte_t pte = 0; int ret; ret = stage2_update_leaf_attrs(pgt, addr, 1, KVM_PTE_LEAF_ATTR_LO_S2_AF, 0, - &pte, NULL, + NULL, NULL, KVM_PGTABLE_WALK_HANDLE_FAULT | KVM_PGTABLE_WALK_SHARED); if (!ret) dsb(ishst); - - return pte; } struct stage2_age_data { diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 756fc856ab44..8fd8ea5b5795 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1699,18 +1699,14 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, /* Resolve the access fault by making the page young again. */ static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa) { - kvm_pte_t pte; struct kvm_s2_mmu *mmu; trace_kvm_access_fault(fault_ipa); read_lock(&vcpu->kvm->mmu_lock); mmu = vcpu->arch.hw_mmu; - pte = kvm_pgtable_stage2_mkyoung(mmu->pgt, fault_ipa); + kvm_pgtable_stage2_mkyoung(mmu->pgt, fault_ipa); read_unlock(&vcpu->kvm->mmu_lock); - - if (kvm_pte_valid(pte)) - kvm_set_pfn_accessed(kvm_pte_to_pfn(pte)); } /** From patchwork Fri Jul 26 23:52:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743589 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C88F2C3DA49 for ; Sat, 27 Jul 2024 00:34:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=jpI7lTRxFmJ8IejB0Qdg95emli3A+yh4X5lHdX1qdPI=; b=wT54y9Ho+zoiskTXVPLmmgPRV9 c9VYGQFxLBb4GzTRypmCrZkk5XpbavEp3zMfJxFDG2pMSqE48Tiz9gkY3yJp5xj+tbOReYjwl+UDm 6veJMQo4nRmL2QLOQIwrMCCfS6fad5Ij43Yqheimtx4Y53R70z8g2nAbHW4DfEwUKX1vYIT2jqE9F IWsKajanqYc9N4UZGottI4krrfvCXxfgWSAxR0n4inlAxxtcY+6vbPDkttdoRU+VvqFKtSKPvgIYO jVjVT/xuKHn8nY7cVkN8U5vD9DKAZoBkVmCLmKeqrW7aBEYxfOxAD1xP3Xe1JILDuvOaUuAX34eDQ SSLnxQnA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVNe-00000005kTL-1kpq; Sat, 27 Jul 2024 00:33:50 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUme-00000005RaJ-2UrG for linux-arm-kernel@bombadil.infradead.org; Fri, 26 Jul 2024 23:55:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To:Sender: Content-Transfer-Encoding:Content-ID:Content-Description; bh=jpI7lTRxFmJ8IejB0Qdg95emli3A+yh4X5lHdX1qdPI=; b=SFbEzVdz/Z3Eehqfh/wTNKzrKj qSes4bY5AvKcd8FbJDzUCgxOiXb/ADouHm6zGs49lUAoq71Jz93vbzgyWSFbGCzDJqnj/ag3I0ICT x+m+1qNOvhowosyLUax1VyPH4a688MyH6R+g308nAIkuH1N4U6tlrW9FXTlODh8tG0BXv/7XnyvUo VzM9bhUjeoDj4wQhlcus1l5CQfv4ri5ooQA+mEPrgBAGxIOXDJAp4PFmHIhPwhmkVBVGplSJcQtnJ rYUxO+VrRAlNN+BK64NszPlK4lSHcntS9GzkPBC8SfB9lCO2KUXxiCBZjNg5xpmLr1BcLpLpafrh7 yxZDhDqQ==; Received: from mail-pj1-x104a.google.com ([2607:f8b0:4864:20::104a]) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmb-00000004K63-0pFK for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:35 +0000 Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-2cb696be198so1638693a91.3 for ; Fri, 26 Jul 2024 16:55:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038130; x=1722642930; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=jpI7lTRxFmJ8IejB0Qdg95emli3A+yh4X5lHdX1qdPI=; b=ekjcoN5WbTzN6fVlgke7IKaXdIbLufxQundkgaUu9xBMfqR3dAwAAI6RnWyG4gr8WH zM9Cf8iIBWQkPTazAW9eUfsegaqEVyP6bF4mxlA7Ew3kXO9wpoOejVIp5uRG000bQpp5 x6B3sYIQx6iwE7bzmuKd8YKotf1ZMjRZNyb05ompSdOhOBa6G6IEvMlqCMG6CewBCOrG AADVm4xBLGDaVAyRGd/HiKPp4VWf21/czjWdQWMKV+/uO09MXK4NLcBbR88HaeAfUIXh vZmjpSReuZnpjmcJD541EQ69G9jlxvgu065gnMMgrOdhe+ofaK42PVkfdjWbSuJjFgdz iL2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038130; x=1722642930; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jpI7lTRxFmJ8IejB0Qdg95emli3A+yh4X5lHdX1qdPI=; b=uMV/GaHuZm7n/Gf7wHIfJStYKSZ7Wb2Q8rh29D/usRIwhwevA7TdV51xMiY+EZ9Veh jKsZjeCGrDP98yff8wM6TnTFOKAR7czlyXBO5Gsayebk2VVVx/M5r7oJig2MnhSY8vjk Vbq2YmzE05t4fjPp39OmuN3zKR/LKPW8JSmBWGRUad0/0nrxlNXHls6JLWGwVphJ5eyI dDsj5AGxJmNNOX0aNRQjNH2NlcemjLzAoy44fWsBuo2NdkV+bFn9luZR1heTE2a20OBK PWZ8PZm6gx18l/sTilaMxtcVZtaqZm0cfNZdgDqqR/83ixX0E4gTDDWQCczlRFx/CNCw eF0Q== X-Forwarded-Encrypted: i=1; AJvYcCVXT/uiFUwmzxqE10VZ2BIdUGW2j3FJjJJnBH0QO4zIavw4siLNAVjki4LxW7xOZUGdqlZsv6p8x/WrnRTnpZm6kwdQpCFMk91XkxVqu5a0oY9hQ2o= X-Gm-Message-State: AOJu0YxwfVfnw/e5FZhxexaleB8OcZq4/ENfrVEsJF35qcyUTswTj2sP YpEDUJS+TydrwcWYu6vcStP86OWwvqo23pJL2JP+N7xTFSeZFfaj0jek/+H1yLMnWUZlOxSDEtD frg== X-Google-Smtp-Source: AGHT+IFv8CQNcqhsspCtIqnm+Y+lStR0ot3kSIK9X3XaAk7UtrG+09935pql8gySUvK94zWnbXq5q/tu644= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:fb47:b0:2c9:61f9:9aea with SMTP id 98e67ed59e1d1-2cf7e618b7dmr21168a91.5.1722038129745; Fri, 26 Jul 2024 16:55:29 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:32 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-84-seanjc@google.com> Subject: [PATCH v12 83/84] KVM: Drop APIs that manipulate "struct page" via pfns From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240727_005533_638941_797E476E X-CRM114-Status: GOOD ( 11.39 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Remove all kvm_{release,set}_pfn_*() APIs not that all users are gone. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Alex Bennée --- include/linux/kvm_host.h | 5 ---- virt/kvm/kvm_main.c | 55 ---------------------------------------- 2 files changed, 60 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 34a1cadb1b80..87d61f16a449 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1260,11 +1260,6 @@ static inline kvm_pfn_t kvm_faultin_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, write ? FOLL_WRITE : 0, writable, refcounted_page); } -void kvm_release_pfn_clean(kvm_pfn_t pfn); -void kvm_release_pfn_dirty(kvm_pfn_t pfn); -void kvm_set_pfn_dirty(kvm_pfn_t pfn); -void kvm_set_pfn_accessed(kvm_pfn_t pfn); - int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, int len); int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 030a08d4b21d..8b85e1130a63 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3200,61 +3200,6 @@ void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map) } EXPORT_SYMBOL_GPL(kvm_vcpu_unmap); -void kvm_release_pfn_clean(kvm_pfn_t pfn) -{ - struct page *page; - - if (is_error_noslot_pfn(pfn)) - return; - - page = kvm_pfn_to_refcounted_page(pfn); - if (!page) - return; - - kvm_release_page_clean(page); -} -EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); - -void kvm_release_pfn_dirty(kvm_pfn_t pfn) -{ - struct page *page; - - if (is_error_noslot_pfn(pfn)) - return; - - page = kvm_pfn_to_refcounted_page(pfn); - if (!page) - return; - - kvm_release_page_dirty(page); -} -EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty); - -/* - * Note, checking for an error/noslot pfn is the caller's responsibility when - * directly marking a page dirty/accessed. Unlike the "release" helpers, the - * "set" helpers are not to be used when the pfn might point at garbage. - */ -void kvm_set_pfn_dirty(kvm_pfn_t pfn) -{ - if (WARN_ON(is_error_noslot_pfn(pfn))) - return; - - if (pfn_valid(pfn)) - kvm_set_page_dirty(pfn_to_page(pfn)); -} -EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); - -void kvm_set_pfn_accessed(kvm_pfn_t pfn) -{ - if (WARN_ON(is_error_noslot_pfn(pfn))) - return; - - if (pfn_valid(pfn)) - kvm_set_page_accessed(pfn_to_page(pfn)); -} -EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); - static int next_segment(unsigned long len, int offset) { if (len > PAGE_SIZE - offset) From patchwork Fri Jul 26 23:52:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743588 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 83AA3C3DA4A for ; Sat, 27 Jul 2024 00:33:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=diuq/5OTzA7KvKsX4AEile2HlGAF4SgNWi1szy67iaM=; b=fnu0Xn+gDG8Ivmk0CCU7oLvHWf uCgTT1eebllc/ItbNNtXaY5C+Qyz3PoR+JZk+xhrO5fFENpY25mvHS+hmO0Bcey4IA2RPpe0nOBE6 enkcEM3/UQm/qDJOxiRF2AiksEs0gQRiJ+6GMy40GfFCPvGemEu9/GAHCGRSjRA6aQXOxWg/pgzq5 dhNWunrJwhjQ8WrOTnYsD0oAT/wPi5IPv3BY1W8Uji/ygrsTOFQwHOnb+z+KLB563m9es5NL3iGEp rCz4CJmcE9HTZcT7fYxNCaVfd0WA8jS5mrhSb39Pyik67WP3gCJTvbq2BoZBe+V5mHLLryESzF0UA QPLiVXMw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVNE-00000005kKS-34XF; Sat, 27 Jul 2024 00:33:24 +0000 Received: from mail-pf1-f202.google.com ([209.85.210.202]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUmb-00000005RXv-31QI for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:55:37 +0000 Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-70ecd589debso374031b3a.2 for ; Fri, 26 Jul 2024 16:55:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722038132; x=1722642932; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=diuq/5OTzA7KvKsX4AEile2HlGAF4SgNWi1szy67iaM=; b=uNv/hXtg8S753DpdEml61zsDUsMp1LcW/lR5JnxqJO7/Tusb0OAi1+C6yO8KXYsyq4 R2mJ4zC2EBM5dEjitRsr6UxrywSiJJVS48i647XcBc7jUlj99hXL6iLuBzoS9AKnTsY7 k9waw7JyXbP16iDKQOjToiSxm7glIqDN82YIjbZ7lQ+6hAlx2Q/lMSpErCVX1L0THGlX cxZbN6Xxpji5Hm8NtN0cpDeeK5g5Vw1Fq76o54C/BNvtghIs7RiENbxIzFhrVz4sZLYz ni87+jdyiOmNddHLQ5ZRqJ6tOBNSiGCSh/0lwm5aWJP3FwnoBOKPCUQ9gOUa+NicvqKF S/Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722038132; x=1722642932; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=diuq/5OTzA7KvKsX4AEile2HlGAF4SgNWi1szy67iaM=; b=eq+/fLfPUioiEhRmLXrSuP31d7PaKquy/413VtOo5pdxbmUVodZ4nygTUC+ReTaZQd fKWK63nD1Fg06VOU31NLODCZENmw+ZpLILoTFOwkyQbKAta08ZXVzAzbZfKVuA1+Ql5J 1sjqj0l8oE699KF11So43YAWk/fZ9HwlJDMUp1DFI3cFav2LQNM6XwIouLr9xRE9yPpW Ks5WP5HEfWwSP5aB4GZybgWlYH9RozUcA4eSXMzpgyplaGxuqTVgHZ+ToHRRS1gbj1qw pCVoq4vk/yoNHr/h+4ctmmYQo80jFmehgGU4MWycTIAg5+Nr5VhWsjZ9XTuSXEiTXkM7 OoWw== X-Forwarded-Encrypted: i=1; AJvYcCVYpG8kpw/sZJbDUngXadVKabjlblisXOGe1bxENCCJnXAlLUCl25eFG42UTPurPdp1QhlrW1xUWIcMkEcCcIwNxIjSkePHcTQ5C6M+MWvkJgTRXRQ= X-Gm-Message-State: AOJu0Ywfv9lN3pfy78NZNflJFXtyUCDtMAmPp7ukKZYpHgcF/Dj09PMT 2hj11RMsF3GBMTpcxLGVsMcgwYogc39E4lZ+7pdmo0ylPRnDCLEzzA0sZq36/Ytpwfz53VFkNU8 qGg== X-Google-Smtp-Source: AGHT+IHhZHDOMBimQWMolk5T4YvfOEDeWobwSpVEA59XfGWHoVcD5oY/zbLuHzkbg8Phg0CxGQdxohIyYms= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:66e5:b0:70d:27ca:96b8 with SMTP id d2e1a72fcca58-70ece926ad1mr25428b3a.0.1722038131828; Fri, 26 Jul 2024 16:55:31 -0700 (PDT) Date: Fri, 26 Jul 2024 16:52:33 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-85-seanjc@google.com> Subject: [PATCH v12 84/84] KVM: Don't grab reference on VM_MIXEDMAP pfns that have a "struct page" From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165533_962902_1B286348 X-CRM114-Status: GOOD ( 22.07 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Now that KVM no longer relies on an ugly heuristic to find its struct page references, i.e. now that KVM can't get false positives on VM_MIXEDMAP pfns, remove KVM's hack to elevate the refcount for pfns that happen to have a valid struct page. In addition to removing a long-standing wart in KVM, this allows KVM to map non-refcounted struct page memory into the guest, e.g. for exposing GPU TTM buffers to KVM guests. Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 3 -- virt/kvm/kvm_main.c | 75 ++-------------------------------------- 2 files changed, 2 insertions(+), 76 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 87d61f16a449..d4513ffaf2e1 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1702,9 +1702,6 @@ void kvm_arch_sync_events(struct kvm *kvm); int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu); -struct page *kvm_pfn_to_refcounted_page(kvm_pfn_t pfn); -bool kvm_is_zone_device_page(struct page *page); - struct kvm_irq_ack_notifier { struct hlist_node link; unsigned gsi; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 8b85e1130a63..e279140f2425 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -160,52 +160,6 @@ __weak void kvm_arch_guest_memory_reclaimed(struct kvm *kvm) { } -bool kvm_is_zone_device_page(struct page *page) -{ - /* - * The metadata used by is_zone_device_page() to determine whether or - * not a page is ZONE_DEVICE is guaranteed to be valid if and only if - * the device has been pinned, e.g. by get_user_pages(). WARN if the - * page_count() is zero to help detect bad usage of this helper. - */ - if (WARN_ON_ONCE(!page_count(page))) - return false; - - return is_zone_device_page(page); -} - -/* - * Returns a 'struct page' if the pfn is "valid" and backed by a refcounted - * page, NULL otherwise. Note, the list of refcounted PG_reserved page types - * is likely incomplete, it has been compiled purely through people wanting to - * back guest with a certain type of memory and encountering issues. - */ -struct page *kvm_pfn_to_refcounted_page(kvm_pfn_t pfn) -{ - struct page *page; - - if (!pfn_valid(pfn)) - return NULL; - - page = pfn_to_page(pfn); - if (!PageReserved(page)) - return page; - - /* The ZERO_PAGE(s) is marked PG_reserved, but is refcounted. */ - if (is_zero_pfn(pfn)) - return page; - - /* - * ZONE_DEVICE pages currently set PG_reserved, but from a refcounting - * perspective they are "normal" pages, albeit with slightly different - * usage rules. - */ - if (kvm_is_zone_device_page(page)) - return page; - - return NULL; -} - /* * Switches to specified vcpu, until a matching vcpu_put() */ @@ -2814,35 +2768,10 @@ static kvm_pfn_t kvm_resolve_pfn(struct kvm_follow_pfn *kfp, struct page *page, if (kfp->map_writable) *kfp->map_writable = writable; - /* - * FIXME: Remove this once KVM no longer blindly calls put_page() on - * every pfn that points at a struct page. - * - * Get a reference for follow_pte() pfns if they happen to point at a - * struct page, as KVM will ultimately call kvm_release_pfn_clean() on - * the returned pfn, i.e. KVM expects to have a reference. - * - * Certain IO or PFNMAP mappings can be backed with valid struct pages, - * but be allocated without refcounting, e.g. tail pages of - * non-compound higher order allocations. Grabbing and putting a - * reference to such pages would cause KVM to prematurely free a page - * it doesn't own (KVM gets and puts the one and only reference). - * Don't allow those pages until the FIXME is resolved. - * - * Don't grab a reference for pins, callers that pin pages are required - * to check refcounted_page, i.e. must not blindly release the pfn. - */ - if (pte) { + if (pte) pfn = pte_pfn(*pte); - - if (!kfp->pin) { - page = kvm_pfn_to_refcounted_page(pfn); - if (page && !get_page_unless_zero(page)) - return KVM_PFN_ERR_FAULT; - } - } else { + else pfn = page_to_pfn(page); - } *kfp->refcounted_page = page;