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: 1965572 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=XoYF++8I; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=KuKgJ2ud; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20230601 header.b=uNv/hXtg; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WW5G94rKXz20FH for ; Sat, 27 Jul 2024 10:33:53 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Reply-To:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID :References:Mime-Version:In-Reply-To:Date:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=/m8eFVYvtcdZcnfSgt5H8WzBrxilJsihPIWeVLwljRs=; b=XoYF++8IRTa2fr 7R6LqwA4bcS+08RTgCzJGXqRQQ+0wfM4LZF77+BQfYVY6WSoOynhYPnZ9iBxp9CCVwr+BdGqGsGuk E59l+5AAe+LSlAcQNC0gLNsGyQ8OMAtKpvdfz1ozn7PmoDQf+pl9FdIH2AU6jP/9dCM0viTVamjva gXZTBnxlZEduJ5CfNd1s0+cKpoaOdFGW83B42FC8dnZ1mSfXPRI8OMTy5OE1MfRkhaUR+RoEXl/4b Ogjx/5L/sGKPm5FvvPPS4O5RpkLU85xeXubSO1RU78eou9FOW7N8MDnDt3xyRagWnQRCxIFsRgNoQ dNFgmW5mnKNFChuLbQ9Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXVNg-00000005kUc-1c48; Sat, 27 Jul 2024 00:33:52 +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 1sXUmg-00000005Rbi-1Fg5 for kvm-riscv@bombadil.infradead.org; Fri, 26 Jul 2024 23:55:38 +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=diuq/5OTzA7KvKsX4AEile2HlGAF4SgNWi1szy67iaM=; b=KuKgJ2udL3Y1QealZobthOh+bF Fq0cgzP4YEw30vdv7GRlsYaSs/HXfftXMnmKpclwvehzdF0UdnVtSP8T0h7WOiXLuprSQg3PANyGo 2iPiOVVa0VyE6w0UMnAIwjzMl7hElN2ms1clgX4MpxK10q5U8D3UK+402qrKuLpqZxdvXFsdPH0n0 9qfca/FDS3SXl06HKuO00WIcQ34J7Oh3cMU2idzvIvnuu904+bK3BC+XAV+RaParerPbdxXeB3J/5 skqAkTuQkicWCiSTa4eC7nd7l/5JWGL56b0XfCLBZWalO7mvZmvNQ5ajrsCUXuKIEvdaTwwnDcoXM /LkRBSsg==; 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 1sXUmc-00000004K6F-0UWZ for kvm-riscv@lists.infradead.org; Fri, 26 Jul 2024 23:55:37 +0000 Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-70e9ea89b42so1344885b3a.3 for ; Fri, 26 Jul 2024 16:55:33 -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=As6pedgRttYnjBrYvpmIkYuJBURqz1drO0zJX8zNig8wpUZBaQXxE4Dg6GRgaqsl3J gYFmtbxjpRkaqzhNVsBEvk3ejd3usU6rR1qVRhQR0QUAr9nbV8MDNMcC8bGGxU65B0YA FHuTxxz7IHU+3i87ik3y/kZCl2hI65xpJUeMIpTWpiMnVns+M/ShBlT4jN+QyS4fLIMK bd621mmVXgtPlKQIEtQbjdFoy0YQKMX6N2f/4DwcQGNqaUwJ84NtIYIOgyHQ9Tyg/0PP ESRvWTEJltatZNhKwGDQMWJT7gMPnTVX1t3L/ur2bfSoEN8w42Fd7gDTunhUBS3+lovW 3sKg== X-Forwarded-Encrypted: i=1; AJvYcCWDHm3sck5+MKhICqzAj6bo+vRKpXpdbuWM/kug4SstpxaTFLYsjeiAmm+D4GdH8W9bVVuVNLon13O2XEu4f3INi03s27+6cvAzcuEWJg== X-Gm-Message-State: AOJu0Ywlgzb6rRq58eAvXZ5uhx7Mvwv7TAHSy39s1YNx87mZmt3+lXbd +uHCnLTv5I5fJJ5eXyFon1aJ/1jUSvvd3WCWYTAFGZphrOS3YT/4MbQT0/S03EF4IN4F273FU9s EnA== 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-20240727_005534_435064_8D83B450 X-CRM114-Status: GOOD ( 20.52 ) X-Spam-Score: -7.6 (-------) X-Spam-Report: Spam detection software, running on the system "desiato.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: 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 pf [...] Content analysis details: (-7.6 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:449 listed in] [list.dnswl.org] -7.5 USER_IN_DEF_DKIM_WL From: address is in the default DKIM welcome-list 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.0 DKIMWL_WL_MED DKIMwl.org - Medium trust sender X-BeenThere: kvm-riscv@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: "kvm-riscv" Errors-To: kvm-riscv-bounces+incoming=patchwork.ozlabs.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;