From nobody Thu Sep 19 01:06:31 2024 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEDA5192B7F for ; Fri, 26 Jul 2024 23:55:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722038134; cv=none; b=Qb/idBn9vXHSRRzrQDTMtLaGBOpxbVlunQUjARW1SabzqMldzUDhw7Z7U+ZOk1+niPywi/KlG0Ywbe7GR86KCf/gLJw09dx5eGvnsUdy267XuXVmTnGPM4Kcjzg82InXFOxZYDmjw3RAWktWKo6UvdK7fvbfs/CHCF1gXuzKVw0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722038134; c=relaxed/simple; bh=Q7iFhysn4W1WfxP0+7u3aZ05Q3iDxPLY40ZQrSBaHTY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZZ0hy8WSKcnHvRn2aMmbr0P4aRwScgY7FJgkRsirO6LJDHbVzdDYbjF81aGsVs7OiRpEofPzrZaIo1+m1R476ynAgCe5oaUwrweeXJsWECOGLxcO9nM3Pb7iKUtXZjfG8NCy+YKm6U85UYtWkxIbdkoGKiuMKKO/KbcZcfWU/+4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vyrftBCa; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vyrftBCa" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-70d26115cd5so1194221b3a.1 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=vger.kernel.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=vyrftBCazLCBTorKThPCCA2XcqVV1jd+UtiELnRrdvvYJJWSY3Lex+i6jT5AwgGmZL f5MclMvki2EQF1UzYitjYBPe66FMuopo8LLCjbH99N/Iwh3ExW/2BKGJNmjRjO8HouAy /P7tvLvRGyBcHL28FfaZVMG7wWPrm9A5LzW4H+SaxRsuWbt21euPKvWfa3F1cfFFfEkp NuEkdNnRePIgXShKpxnx6DCes8gZdjn9RQf7QzqCCcx1dEgZijoDyyln/7oaC5zqDX77 yktLpqHPVFLkOP5OYp3pNqLkXYuMLKCgYY+HkXp4rOqUw7IArPyszHXqIsnEStzKcyle F24A== 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=s1Us5bmFtGfOfYeEZrY6Z4USX4f4RJVc0MwFqvEd2e8mIw4nSE6ofvu63NHEMsgEMZ bTCNU0IxfO8HJ4EH9UrIncjVPO4LyO2f5QeCKH0+F69uFT6cHdtIQIDZDEu4IAiPiJGN EMEucg0UGw6dZqRzdhNVoXrBZaxDKT6kfmF58cPbO114C3NPyKj3hQIBAhbWsvpYUlSA RZwzNlAlEeBeChVS7i8zKX9OFyDnpCBFV17fygvuY6Thbsjl57kq6orVuv2VLe/u4A9e OWFMkf4YC+PP2dciRQdGlQ25VSaxZhpy15Zx2ZQVaMssu4KK7Kq2gSp4Kao3SmRyiYn0 XsCw== X-Forwarded-Encrypted: i=1; AJvYcCV8cmv5izYxNwToETu4cjnxjz0h3bZ8tJAZrc/SKta4KaYqAOHsa4uc4o+2OKtGw8ZGvJ3nZWVvHUgI7xIhkBI7YT52FnrCpDOC3DkC X-Gm-Message-State: AOJu0Yxm6YACI/v7sFW6wbi149rIEw6WIfaH7u6HdftlZutmRll7HXL7 O6JdSkyIE3NRuVWzYBYT5w75VrB/rR0GFJf8cMFhPEsUz4qQ0+00dpMLf90FeoTzK6suXJSRg+F Lpg== 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) Reply-To: Sean Christopherson Date: Fri, 26 Jul 2024 16:52:33 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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); =20 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu); =20 -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) { } =20 -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 ty= pes - * is likely incomplete, it has been compiled purely through people wantin= g 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 =3D 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 =3D writable; =20 - /* - * 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 =3D pte_pfn(*pte); - - if (!kfp->pin) { - page =3D kvm_pfn_to_refcounted_page(pfn); - if (page && !get_page_unless_zero(page)) - return KVM_PFN_ERR_FAULT; - } - } else { + else pfn =3D page_to_pfn(page); - } =20 *kfp->refcounted_page =3D page; =20 --=20 2.46.0.rc1.232.g9752f9e123-goog