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,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 21846C49EAB for ; Tue, 5 Nov 2019 10:03:20 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C88C021928 for ; Tue, 5 Nov 2019 10:03:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="b7TEzV1y" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C88C021928 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 769F56B0003; Tue, 5 Nov 2019 05:03:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6CC236B0006; Tue, 5 Nov 2019 05:03:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56CBB6B0007; Tue, 5 Nov 2019 05:03:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0252.hostedemail.com [216.40.44.252]) by kanga.kvack.org (Postfix) with ESMTP id CEBF46B0003 for ; Tue, 5 Nov 2019 05:03:18 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with SMTP id 70F558249980 for ; Tue, 5 Nov 2019 10:03:18 +0000 (UTC) X-FDA: 76121786076.11.trail30_1cbf37949a73c X-HE-Tag: trail30_1cbf37949a73c X-Filterd-Recvd-Size: 9748 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by imf09.hostedemail.com (Postfix) with ESMTP for ; Tue, 5 Nov 2019 10:03:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1572948197; 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=+PeXIW5o4X0824gIsT37rM/mFNG9YQ7RC4BU3rblgDI=; b=b7TEzV1ymzLRJiolwYCMSMuM/UAJriCr+NPEMIHvIzablH9jMz1ui3H8ajGAdbjFOU2Rr7 Ztrfdl824aB88bShxLaQ+TISW5Kp2IxVmRHsAwmEdS0UTJUJxrToShR8g6ClSB2WMZls0P OFFz+VFO4Mu+1uUiz8zBE1BpTUVioyY= 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-385-IrNamL1SM8-MPLUk2gNZug-1; Tue, 05 Nov 2019 05:03:15 -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 3B1B3800054; Tue, 5 Nov 2019 10:03:08 +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 AEF2C5C553; Tue, 5 Nov 2019 10:02:47 +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> <01adb4cb-6092-638c-0bab-e61322be7cf5@redhat.com> Organization: Red Hat GmbH Message-ID: <613f3606-748b-0e56-a3ad-1efaffa1a67b@redhat.com> Date: Tue, 5 Nov 2019 11:02:46 +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: <01adb4cb-6092-638c-0bab-e61322be7cf5@redhat.com> Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: IrNamL1SM8-MPLUk2gNZug-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:49, David Hildenbrand wrote: > 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 wr= ote: >>>> >>>> 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 = buddy) >>>> + * as reserved - this includes ZONE_DEVICE pages and pages wit= hout >>>> + * 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@angb= and.pl/ >>> >>> I'll see if this patch set modulates or maintains that failure mode. >>> >> >> 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: >=20 > Pages that are pinned via gfn_to_pfn() and friends take a references, > however are often released via > kvm_release_pfn_clean()/kvm_release_pfn_dirty()/kvm_release_page_clean().= .. >=20 >=20 > E.g., in arch/x86/kvm/x86.c:reexecute_instruction() >=20 > ... > pfn =3D gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); > ... > kvm_release_pfn_clean(pfn); >=20 >=20 >=20 > 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)); > } >=20 > This function makes perfect sense as the counterpart for kvm_get_pfn(): >=20 > void kvm_get_pfn(kvm_pfn_t pfn) > { > =09if (!kvm_is_reserved_pfn(pfn)) > =09=09get_page(pfn_to_page(pfn)); > } >=20 >=20 > As all ZONE_DEVICE pages are currently reserved, pages pinned via > gfn_to_pfn() and friends will often not see a put_page() AFAIKS. >=20 > Now, my patch does not change that, the result of > kvm_is_reserved_pfn(pfn) will be unchanged. A proper fix for that would > probably be >=20 > a) To drop the reference to ZONE_DEVICE pages in gfn_to_pfn() and > friends, after you successfully pinned the pages. (not sure if that's > the right thing to do but you're the expert) >=20 > b) To not use kvm_release_pfn_clean() and friends on pages that were > definitely pinned. >=20 (talking to myself, sorry) Thinking again, dropping this patch from this series could effectively=20 also fix that issue. E.g., kvm_release_pfn_clean() and friends would=20 always do a put_page() if "pfn_valid() and !PageReserved()", so after=20 patch 9 also on ZONDE_DEVICE pages. But it would have side effects that might not be desired. E.g.,: 1. kvm_pfn_to_page() would also return ZONE_DEVICE pages (might even be=20 the right thing to do). 2. kvm_set_pfn_dirty() would also set ZONE_DEVICE pages dirty (might be=20 okay) --=20 Thanks, David / dhildenb