From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.3 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57BFFC49EAB for ; Tue, 5 Nov 2019 09:49:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1775620869 for ; Tue, 5 Nov 2019 09:49:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ays4fire" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1775620869 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A5A446B0006; Tue, 5 Nov 2019 04:49:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A317C6B0007; Tue, 5 Nov 2019 04:49:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 920AA6B0008; Tue, 5 Nov 2019 04:49:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0229.hostedemail.com [216.40.44.229]) by kanga.kvack.org (Postfix) with ESMTP id 7BCE46B0006 for ; Tue, 5 Nov 2019 04:49:40 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with SMTP id 1F28BB9EB for ; Tue, 5 Nov 2019 09:49:40 +0000 (UTC) X-FDA: 76121751720.18.roof77_373c11f7fdb62 X-HE-Tag: roof77_373c11f7fdb62 X-Filterd-Recvd-Size: 8931 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by imf03.hostedemail.com (Postfix) with ESMTP for ; Tue, 5 Nov 2019 09:49:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1572947379; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kHZKkj9R7Yfgi4epLzXOt7O5NFLJrXYVRc9BFpKTvw4=; b=ays4fire+s7EruRcynGe+zVSxdhaOhcCuSr+Lu055/Gpznrs7yUk2ZG545vM1pm5Nv4V/F C4UphstR6FxeIJx8TGRSngpmpkmoVTu8Gu7dFBYDlfurJ2li0njj+Ac8DGa6O9RPWFAZjN eUzDG54jz9hdifA3rWuFPe6uIni5Rm8= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-51-3-UT9hZ4OuOURxDou1h6oQ-1; Tue, 05 Nov 2019 04:49:37 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8EDBA800C73; Tue, 5 Nov 2019 09:49:30 +0000 (UTC) Received: from [10.36.117.253] (ovpn-117-253.ams2.redhat.com [10.36.117.253]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0EEEB5C1B2; Tue, 5 Nov 2019 09:49:10 +0000 (UTC) Subject: Re: [PATCH v1 03/10] KVM: Prepare kvm_is_reserved_pfn() for PG_reserved changes From: David Hildenbrand To: Dan Williams Cc: Linux Kernel Mailing List , Linux MM , Michal Hocko , Andrew Morton , kvm-ppc@vger.kernel.org, linuxppc-dev , KVM list , linux-hyperv@vger.kernel.org, devel@driverdev.osuosl.org, xen-devel , X86 ML , Alexander Duyck , Alexander Duyck , Alex Williamson , Allison Randal , Andy Lutomirski , "Aneesh Kumar K.V" , Anshuman Khandual , Anthony Yznaga , Benjamin Herrenschmidt , Borislav Petkov , Boris Ostrovsky , Christophe Leroy , Cornelia Huck , Dave Hansen , Haiyang Zhang , "H. Peter Anvin" , Ingo Molnar , "Isaac J. Manjarres" , Jim Mattson , Joerg Roedel , Johannes Weiner , Juergen Gross , KarimAllah Ahmed , Kees Cook , "K. Y. Srinivasan" , "Matthew Wilcox (Oracle)" , Matt Sickler , Mel Gorman , Michael Ellerman , Michal Hocko , Mike Rapoport , Mike Rapoport , Nicholas Piggin , Oscar Salvador , Paolo Bonzini , Paul Mackerras , Paul Mackerras , Pavel Tatashin , Pavel Tatashin , Peter Zijlstra , Qian Cai , =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , Sasha Levin , Sean Christopherson , Stefano Stabellini , Stephen Hemminger , Thomas Gleixner , Vitaly Kuznetsov , Vlastimil Babka , Wanpeng Li , YueHaibing , Adam Borowski References: <20191024120938.11237-1-david@redhat.com> <20191024120938.11237-4-david@redhat.com> Organization: Red Hat GmbH Message-ID: <01adb4cb-6092-638c-0bab-e61322be7cf5@redhat.com> Date: Tue, 5 Nov 2019 10:49:10 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.1.1 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: 3-UT9hZ4OuOURxDou1h6oQ-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 05.11.19 10:17, David Hildenbrand wrote: > On 05.11.19 05:38, Dan Williams wrote: >> On Thu, Oct 24, 2019 at 5:11 AM David Hildenbrand wro= te: >>> >>> Right now, ZONE_DEVICE memory is always set PG_reserved. We want to >>> change that. >>> >>> KVM has this weird use case that you can map anything from /dev/mem >>> into the guest. pfn_valid() is not a reliable check whether the memmap >>> was initialized and can be touched. pfn_to_online_page() makes sure >>> that we have an initialized memmap (and don't have ZONE_DEVICE memory). >>> >>> Rewrite kvm_is_reserved_pfn() to make sure the function produces the >>> same result once we stop setting ZONE_DEVICE pages PG_reserved. >>> >>> Cc: Paolo Bonzini >>> Cc: "Radim Kr=C4=8Dm=C3=A1=C5=99" >>> Cc: Michal Hocko >>> Cc: Dan Williams >>> Cc: KarimAllah Ahmed >>> Signed-off-by: David Hildenbrand >>> --- >>> virt/kvm/kvm_main.c | 10 ++++++++-- >>> 1 file changed, 8 insertions(+), 2 deletions(-) >>> >>> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c >>> index e9eb666eb6e8..9d18cc67d124 100644 >>> --- a/virt/kvm/kvm_main.c >>> +++ b/virt/kvm/kvm_main.c >>> @@ -151,9 +151,15 @@ __weak int kvm_arch_mmu_notifier_invalidate_range(= struct kvm *kvm, >>> >>> bool kvm_is_reserved_pfn(kvm_pfn_t pfn) >>> { >>> - if (pfn_valid(pfn)) >>> - return PageReserved(pfn_to_page(pfn)); >>> + struct page *page =3D pfn_to_online_page(pfn); >>> >>> + /* >>> + * We treat any pages that are not online (not managed by the b= uddy) >>> + * as reserved - this includes ZONE_DEVICE pages and pages with= out >>> + * a memmap (e.g., mapped via /dev/mem). >>> + */ >>> + if (page) >>> + return PageReserved(page); >>> return true; >>> } >> >> So after this all the pfn_valid() usage in kvm_main.c is replaced with >> pfn_to_online_page()? Looks correct to me. >> >> However, I'm worried that kvm is taking reference on ZONE_DEVICE pages >> through some other path resulting in this: >> >> https://lore.kernel.org/linux-nvdimm/20190919154708.GA24650@angban= d.pl/ >> >> I'll see if this patch set modulates or maintains that failure mode. >> >=20 > I'd assume that the behavior is unchanged. Ithink we get a reference to > these ZONE_DEVICE pages via __get_user_pages_fast() and friends in > hva_to_pfn_fast() and friends in virt/kvm/kvm_main.c >=20 I think I know what's going wrong: Pages that are pinned via gfn_to_pfn() and friends take a references,=20 however are often released via=20 kvm_release_pfn_clean()/kvm_release_pfn_dirty()/kvm_release_page_clean()... E.g., in arch/x86/kvm/x86.c:reexecute_instruction() ... pfn =3D gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); ... kvm_release_pfn_clean(pfn); void kvm_release_pfn_clean(kvm_pfn_t pfn) { =09if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) =09=09put_page(pfn_to_page(pfn)); } This function makes perfect sense as the counterpart for kvm_get_pfn(): void kvm_get_pfn(kvm_pfn_t pfn) { =09if (!kvm_is_reserved_pfn(pfn)) =09=09get_page(pfn_to_page(pfn)); } As all ZONE_DEVICE pages are currently reserved, pages pinned via=20 gfn_to_pfn() and friends will often not see a put_page() AFAIKS. Now, my patch does not change that, the result of=20 kvm_is_reserved_pfn(pfn) will be unchanged. A proper fix for that would=20 probably be a) To drop the reference to ZONE_DEVICE pages in gfn_to_pfn() and=20 friends, after you successfully pinned the pages. (not sure if that's=20 the right thing to do but you're the expert) b) To not use kvm_release_pfn_clean() and friends on pages that were=20 definitely pinned. --=20 Thanks, David / dhildenb