Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2577437ybl; Thu, 29 Aug 2019 10:01:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqzfgtug0iZAuEcW1yCdWuoUCYbPd+Fu5SJf/lp1fI5Jp/PZwJBxqfRrs1UYEO/fB/9MD/Ek X-Received: by 2002:a17:902:4545:: with SMTP id m63mr9629433pld.45.1567098066333; Thu, 29 Aug 2019 10:01:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567098066; cv=none; d=google.com; s=arc-20160816; b=lg2S/ztseGs/AejHcSJhv8RTTKPqEbQWLY97WoKfJNO++nUCFYuUwrcrcxEKifw/8v aj0AiOIaeNIR+c5bJ4TKAw55mRK4ERJldPOEo7uUZVmpwFiF3Alb9lFnuTPNnsscnl5d QPvNu2Rc4lFrrh+NxWJyJo0hIWR81C7lBWcjjzkshykRwhdYN9en+t2AIIw5SKfQdTJz +0EP34FrayniMn1xzHIgK7YoH2QKXOAenrgZHe0OAeMmGSDhBHa2q6keVy4MhdvjCkNs q7NqOPlQkIZUwe5eT7xxynTtBaDDA2JGDbPAJupS5CyCWOc+s47NSGFezIv9Ofd70vbB 90zA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=A+yUquBQTlO4YT7nc5QxX+L6ZY5D4Oebn/uy0KHtSIk=; b=GkL4/V1Jjs719Hsmi6R9AhkiuhchdIqvCqF3hf8jJqYAQG2zEJXMaGd66rk9Kaieff WVYVyXq2Os6mz+cdgSU49GCR9xLXuqMpZ2n5XYZf9sXNULpJVkC71eyLuCNegaeXtOnK b3/sypasusK1H5sa1XRqqbQte4F5luyGG5wEoxK4Bcy+k3ysGC709sSwwo3yIDwWkDh9 VyeBAIAZMYVPugXdjEDoEG+geBxQzhPd8WnXXFAvi3igv9yI/1u/2PYf8VK/EwnDCLCT xDgkFnwyzJIdELKTHXKDMW8sKgPbBpvKnd/fm5yNbu4eUanzpLP5pzo+A3GHKvPWJD+V 1yCg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x5si2442851pln.274.2019.08.29.10.00.50; Thu, 29 Aug 2019 10:01:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727673AbfH2Q62 (ORCPT + 99 others); Thu, 29 Aug 2019 12:58:28 -0400 Received: from out30-132.freemail.mail.aliyun.com ([115.124.30.132]:38548 "EHLO out30-132.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727087AbfH2Q62 (ORCPT ); Thu, 29 Aug 2019 12:58:28 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R901e4;CH=green;DM=||false|;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e01422;MF=luoben@linux.alibaba.com;NM=1;PH=DS;RN=4;SR=0;TI=SMTPD_---0TanJx0D_1567097902; Received: from bn0418deMacBook-Pro.local(mailfrom:luoben@linux.alibaba.com fp:SMTPD_---0TanJx0D_1567097902) by smtp.aliyun-inc.com(127.0.0.1); Fri, 30 Aug 2019 00:58:23 +0800 Subject: Re: [PATCH v2] vfio/type1: avoid redundant PageReserved checking To: Alex Williamson Cc: cohuck@redhat.com, linux-kernel@vger.kernel.org, Andrea Arcangeli References: <20190827124041.4f986005@x1.home> <3517844d6371794cff59b13bf9c2baf1dcbe571c.1566966365.git.luoben@linux.alibaba.com> <20190828095501.12e71bd3@x1.home> From: Ben Luo Message-ID: <6c234632-b7e9-45c7-3d70-51a4c83161f6@linux.alibaba.com> Date: Fri, 30 Aug 2019 00:58:22 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <20190828095501.12e71bd3@x1.home> Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ?? 2019/8/28 ????11:55, Alex Williamson ะด??: > On Wed, 28 Aug 2019 12:28:04 +0800 > Ben Luo wrote: > >> currently, if the page is not a tail of compound page, it will be >> checked twice for the same thing. >> >> Signed-off-by: Ben Luo >> --- >> drivers/vfio/vfio_iommu_type1.c | 3 +-- >> 1 file changed, 1 insertion(+), 2 deletions(-) >> >> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c >> index 054391f..d0f7346 100644 >> --- a/drivers/vfio/vfio_iommu_type1.c >> +++ b/drivers/vfio/vfio_iommu_type1.c >> @@ -291,11 +291,10 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async) >> static bool is_invalid_reserved_pfn(unsigned long pfn) >> { >> if (pfn_valid(pfn)) { >> - bool reserved; >> struct page *tail = pfn_to_page(pfn); >> struct page *head = compound_head(tail); >> - reserved = !!(PageReserved(head)); >> if (head != tail) { >> + bool reserved = PageReserved(head); >> /* >> * "head" is not a dangling pointer >> * (compound_head takes care of that) > Thinking more about this, the code here was originally just a copy of > kvm_is_mmio_pfn() which was simplified in v3.12 with the commit below. > Should we instead do the same thing here? Thanks, > > Alex ok, and kvm_is_mmio_pfn() has also been updated since then, I will take a look at that and compose a new patch > > commit 11feeb498086a3a5907b8148bdf1786a9b18fc55 > Author: Andrea Arcangeli > Date: Thu Jul 25 03:04:38 2013 +0200 > > kvm: optimize away THP checks in kvm_is_mmio_pfn() > > The checks on PG_reserved in the page structure on head and tail pages > aren't necessary because split_huge_page wouldn't transfer the > PG_reserved bit from head to tail anyway. > > This was a forward-thinking check done in the case PageReserved was > set by a driver-owned page mapped in userland with something like > remap_pfn_range in a VM_PFNMAP region, but using hugepmds (not > possible right now). It was meant to be very safe, but it's overkill > as it's unlikely split_huge_page could ever run without the driver > noticing and tearing down the hugepage itself. > > And if a driver in the future will really want to map a reserved > hugepage in userland using an huge pmd it should simply take care of > marking all subpages reserved too to keep KVM safe. This of course > would require such a hypothetical driver to tear down the huge pmd > itself and splitting the hugepage itself, instead of relaying on > split_huge_page, but that sounds very reasonable, especially > considering split_huge_page wouldn't currently transfer the reserved > bit anyway. > > Signed-off-by: Andrea Arcangeli > Signed-off-by: Gleb Natapov > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index d2836788561e..0fc25aed79a8 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -102,28 +102,8 @@ static bool largepages_enabled = true; > > bool kvm_is_mmio_pfn(pfn_t pfn) > { > - if (pfn_valid(pfn)) { > - int reserved; > - struct page *tail = pfn_to_page(pfn); > - struct page *head = compound_trans_head(tail); > - reserved = PageReserved(head); > - if (head != tail) { > - /* > - * "head" is not a dangling pointer > - * (compound_trans_head takes care of that) > - * but the hugepage may have been splitted > - * from under us (and we may not hold a > - * reference count on the head page so it can > - * be reused before we run PageReferenced), so > - * we've to check PageTail before returning > - * what we just read. > - */ > - smp_rmb(); > - if (PageTail(tail)) > - return reserved; > - } > - return PageReserved(tail); > - } > + if (pfn_valid(pfn)) > + return PageReserved(pfn_to_page(pfn)); > > return true; > }