Message ID | 20221013211234.1318131-6-seanjc@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp483859wrs; Thu, 13 Oct 2022 14:14:34 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Uz8mWBwNVXwodcZurSw2+48UhTyGCCxTui56tnUCFdz1+8R4Bf5Kn/DgWl/usrQ0oTO0b X-Received: by 2002:a63:5a61:0:b0:41b:b021:f916 with SMTP id k33-20020a635a61000000b0041bb021f916mr1538884pgm.387.1665695673885; Thu, 13 Oct 2022 14:14:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665695673; cv=none; d=google.com; s=arc-20160816; b=azxxJptg07TQxvGiofGJazQq5OeoYqG2vY0s7oPV4/WA4TRkC7sOGEk82rnk9gpGmO XgVGcQdUH9S52NEoxHKb4MzEYhyGcZivw3I3QbyuZ6M3fhHpTvd3qHs7xRnSi2oee8Yo HyEm7SBxvnrSPyXYCEqryVH3TWfk74nUr5JyppZAw/FcbEMO8UoIrPfDBbqmZSrBtfiP CFNRzUGu292gCVl+2vVqydkbjagzg20sarEhHpaNeYZlp28AIIGwbzqvDdbFnh3z1khI grQB8oPrh5bE44HiWwimFNul0ZL46CbO8dQU9ntjgLrSaH8Y2ykXZ/oxI2wTM/eGa5wO a6/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=I+Js4rwjsW6iIwbX3VOZw+Ue+OSoWJ6FRkZ2WqW5Mnw=; b=QoyM0n/vOOVzS3mKMKdhe49KhjquioGJ/ue5PmwjWVdz9zWt0mrtDVKiWXLB7vWZpu k0jmPR1Fd6CSYCVatIRXf73/1/xDyMoMwH2TScXoigjk1DWv/tlojuNLGQR/ZxFVmH86 Q0kuCrtqxRmTGKzGOYRoCJ3wITtX+g8mDdPAlo0j/hw416PsvY/Ic948Xf44cIdrlxrl whs3WVCI0Wh6aAraCScbtCDtaBw8QRA61ArZLsGs+Q20hSZps4oaTOEIAhIpIzzXNbxv bCvbof6m6wxpB9gkja6YXqTE8NoYyhA/vafQuyf4PZ72Stj3EJo6OLMJ66P/QfvWQAJC UZig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=YFtv89DA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o12-20020a17090ac70c00b0020ac3463dd3si644610pjt.84.2022.10.13.14.14.21; Thu, 13 Oct 2022 14:14:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=YFtv89DA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230130AbiJMVNk (ORCPT <rfc822;ouuuleilei@gmail.com> + 99 others); Thu, 13 Oct 2022 17:13:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbiJMVNV (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 13 Oct 2022 17:13:21 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53ADE192B9A for <linux-kernel@vger.kernel.org>; Thu, 13 Oct 2022 14:13:10 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id h19-20020a63e153000000b00434dfee8dbaso1575352pgk.18 for <linux-kernel@vger.kernel.org>; Thu, 13 Oct 2022 14:13:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=I+Js4rwjsW6iIwbX3VOZw+Ue+OSoWJ6FRkZ2WqW5Mnw=; b=YFtv89DAnR5aI0XcjusnNfre1iUUFeX+FG5+Sjlu+cBR5Br5DMTjfEA/fWvvSWjOe9 qleW6wvYLwBC3TnG0PiE6Yfr9LOJ3zOOv5R+SwNTyFqmzegCCnRMDfuj5mSkgLqdvlpR tpFCZ8Efs75QDGh0h///oq7iGCXmgtWaNQ8D89JKktSH1uGHYgzs/UVYpXvydxyHSOEu ZVlyQnj2SYH+rrJo+dWEzzhs4F6Qq/Cq21G4tJPZaqAiELtteZgN4syVtkjWWyd78HTt dNQoMbkw+DdnCI070hDk6W8CEt9C2u2Vi63DbpALlIHoVJe8avwk/sBteEnAlNZRYADW Yn9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=I+Js4rwjsW6iIwbX3VOZw+Ue+OSoWJ6FRkZ2WqW5Mnw=; b=cRC9qhsv0H61Q8aMIRmhhriBEtdLPtYrzLT5kBWqEoPAF6cFSm2n9bZZocc6WK+ZZT 304NHBh72Wl00es76lroMDNPZNJDauuuJzzusyPnheo6Iqe9K43vNyj8+iVapzKSn5wN 0S1s/AIhMOo2ZUbUoDgXOBCV+YzGUW4j2LJccEu7GzobXK7Cd71EiQh8b3JOZ72W/KA4 ykQUcLX73eomUCqhofyKjy8LuYOD6Ihp0HGZCPM7L8I9prKM562vELJIQ4gyeADrk5p0 HshZr9vn/2iilnptyZ+hFIu3jWS/T4vtZWpDMyXYR0+xp0Szgv084A8qKoBqphTG0Cak VMhw== X-Gm-Message-State: ACrzQf2xc9oj4N3ogRrG/I7DIsemSV45udl/gDIXkCA6yftL4Y1cfs9M 0e0gBesO4S++ZTrHPmwYycE0g0Qjr9M= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:d990:b0:20d:9c20:90c with SMTP id d16-20020a17090ad99000b0020d9c20090cmr1895243pjv.203.1665695567569; Thu, 13 Oct 2022 14:12:47 -0700 (PDT) Reply-To: Sean Christopherson <seanjc@google.com> Date: Thu, 13 Oct 2022 21:12:23 +0000 In-Reply-To: <20221013211234.1318131-1-seanjc@google.com> Mime-Version: 1.0 References: <20221013211234.1318131-1-seanjc@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Message-ID: <20221013211234.1318131-6-seanjc@google.com> Subject: [PATCH v2 05/16] KVM: x86: Remove unused argument in gpc_unmap_khva() From: Sean Christopherson <seanjc@google.com> To: Sean Christopherson <seanjc@google.com>, Paolo Bonzini <pbonzini@redhat.com> Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Michal Luczaj <mhal@rbox.co>, David Woodhouse <dwmw2@infradead.org> Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746608507109978871?= X-GMAIL-MSGID: =?utf-8?q?1746608507109978871?= |
Series |
KVM: x86: gfn_to_pfn_cache fixes and cleanups
|
|
Commit Message
Sean Christopherson
Oct. 13, 2022, 9:12 p.m. UTC
From: Michal Luczaj <mhal@rbox.co> Remove the unused @kvm argument from gpc_unmap_khva(). Signed-off-by: Michal Luczaj <mhal@rbox.co> Signed-off-by: Sean Christopherson <seanjc@google.com> --- virt/kvm/pfncache.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
Comments
On 14/10/2022 5:12 am, Sean Christopherson wrote:
> Remove the unused @kvm argument from gpc_unmap_khva().
Nit: the caller kvm_gpc_unmap() can also get rid of the unused @kvm argument.
On 12/2/22 10:28, Like Xu wrote: > On 14/10/2022 5:12 am, Sean Christopherson wrote: >> Remove the unused @kvm argument from gpc_unmap_khva(). > > Nit: the caller kvm_gpc_unmap() can also get rid of the unused @kvm argument. Right, the initial series cleaned up kvm_gpc_unmap() in a separate patch. Current iteration removes kvm_gpc_unmap() later in the series: https://lore.kernel.org/kvm/20221013211234.1318131-12-seanjc@google.com/ Michal
On Fri, 2022-12-02 at 11:57 +0100, Michal Luczaj wrote: > On 12/2/22 10:28, Like Xu wrote: > > On 14/10/2022 5:12 am, Sean Christopherson wrote: > > > Remove the unused @kvm argument from gpc_unmap_khva(). > > > > Nit: the caller kvm_gpc_unmap() can also get rid of the unused @kvm argument. > > Right, the initial series cleaned up kvm_gpc_unmap() in a separate patch. > Current iteration removes kvm_gpc_unmap() later in the series: > https://lore.kernel.org/kvm/20221013211234.1318131-12-seanjc@google.com/ I have been keeping that series up to date in https://git.infradead.org/users/dwmw2/linux.git/shortlog/refs/heads/gpc-fixes Now that the dust has settled on the Xen runstate area, I may post it as v3 of the series. Or I may attempt to resolve the gpc->len immutability thing first. I'm still not really convinced Sean has won me round on that; I'm still quite attached to the TOCTOU benefit of checking the length right there at the moment you're going to use the pointer — especially given that it *doesn't* have bounds checks like get_user() does, as Sean points out.
On Fri, Dec 02, 2022, David Woodhouse wrote: > On Fri, 2022-12-02 at 11:57 +0100, Michal Luczaj wrote: > > On 12/2/22 10:28, Like Xu wrote: > > > On 14/10/2022 5:12 am, Sean Christopherson wrote: > > > > Remove the unused @kvm argument from gpc_unmap_khva(). > > > > > > Nit: the caller kvm_gpc_unmap() can also get rid of the unused @kvm argument. > > > > Right, the initial series cleaned up kvm_gpc_unmap() in a separate patch. > > Current iteration removes kvm_gpc_unmap() later in the series: > > https://lore.kernel.org/kvm/20221013211234.1318131-12-seanjc@google.com/ > > I have been keeping that series up to date in > https://git.infradead.org/users/dwmw2/linux.git/shortlog/refs/heads/gpc-fixes > > Now that the dust has settled on the Xen runstate area, I may post it > as v3 of the series. > > Or I may attempt to resolve the gpc->len immutability thing first. I'm > still not really convinced Sean has won me round on that; Ya, I agree that storing "len" is undesirable, but so is storing "gpa" instead of "gfn". > I'm still quite attached to the TOCTOU benefit of checking the length right > there at the moment you're going to use the pointer — especially given that > it *doesn't* have bounds checks like get_user() does, as Sean points out. I'm in favor of keeping the length checks if we modify the cache to store the gfn, not the gpa, and require the gpa (or maybe just offset?) in the "get a kernel pointer" API. So, how about this for a set of APIs? Obviously not tested whatsoever, but I think they address the Xen use cases, and will fit the nested virt cases too (which want to stuff a pfn into a VMCS/VMCB). void *kvm_gpc_get_kmap(struct gfn_to_pfn_cache *gpc, gpa_t offset, unsigned long len, bool atomic) { <lock + refresh> return gpc->khva + offset; } EXPORT_SYMBOL_GPL(kvm_gpc_refresh); kvm_pfn_t kvm_gpc_get_pfn(struct gfn_to_pfn_cache *gpc, bool atomic) { <lock + refresh of full page> return gpc->pfn; } EXPORT_SYMBOL_GPL(kvm_gpc_refresh); void kvm_gpc_put(struct gfn_to_pfn_cache *gpc) { <unlock> } EXPORT_SYMBOL_GPL(kvm_gpc_refresh); int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc) { return __kvm_gpc_refresh(gpc, gfn_to_gpa(gpc->gfn), PAGE_SIZE); } EXPORT_SYMBOL_GPL(kvm_gpc_refresh); And then __kvm_gpc_refresh() would do something like: diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 2d6aba677830..b2dd2eda4b56 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -236,22 +236,19 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) return -EFAULT; } -static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, +static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpt_t gpa, unsigned long len) { struct kvm_memslots *slots = kvm_memslots(gpc->kvm); - unsigned long page_offset = gpa & ~PAGE_MASK; bool unmap_old = false; unsigned long old_uhva; kvm_pfn_t old_pfn; void *old_khva; + gfn_t gfn; int ret; - /* - * If must fit within a single page. The 'len' argument is - * only to enforce that. - */ - if (page_offset + len > PAGE_SIZE) + /* An individual cache doesn't support page splits. */ + if ((gpa & ~PAGE_MASK) + len > PAGE_SIZE) return -EINVAL; /* @@ -268,16 +265,16 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, goto out_unlock; } + gfn = gpa_to_gfn(gpa); + old_pfn = gpc->pfn; - old_khva = gpc->khva - offset_in_page(gpc->khva); + old_khva = gpc->khva; old_uhva = gpc->uhva; /* If the userspace HVA is invalid, refresh that first */ - if (gpc->gpa != gpa || gpc->generation != slots->generation || + if (gpc->gfn != gfn || gpc->generation != slots->generation || kvm_is_error_hva(gpc->uhva)) { - gfn_t gfn = gpa_to_gfn(gpa); - - gpc->gpa = gpa; + gpc->gfn = gfn; gpc->generation = slots->generation; gpc->memslot = __gfn_to_memslot(slots, gfn); gpc->uhva = gfn_to_hva_memslot(gpc->memslot, gfn); @@ -295,12 +292,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, if (!gpc->valid || old_uhva != gpc->uhva) { ret = hva_to_pfn_retry(gpc); } else { - /* - * If the HVA→PFN mapping was already valid, don't unmap it. - * But do update gpc->khva because the offset within the page - * may have changed. - */ - gpc->khva = old_khva + page_offset; + /* If the HVA→PFN mapping was already valid, don't unmap it. */ ret = 0; goto out_unlock; }
diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 23180f1d9c1c..32ccf168361b 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -98,7 +98,7 @@ bool kvm_gpc_check(struct kvm *kvm, struct gfn_to_pfn_cache *gpc, gpa_t gpa, } EXPORT_SYMBOL_GPL(kvm_gpc_check); -static void gpc_unmap_khva(struct kvm *kvm, kvm_pfn_t pfn, void *khva) +static void gpc_unmap_khva(kvm_pfn_t pfn, void *khva) { /* Unmap the old pfn/page if it was mapped before. */ if (!is_error_noslot_pfn(pfn) && khva) { @@ -177,7 +177,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct kvm *kvm, struct gfn_to_pfn_cache *gpc) * the existing mapping and didn't create a new one. */ if (new_khva != old_khva) - gpc_unmap_khva(kvm, new_pfn, new_khva); + gpc_unmap_khva(new_pfn, new_khva); kvm_release_pfn_clean(new_pfn); @@ -324,7 +324,7 @@ int kvm_gpc_refresh(struct kvm *kvm, struct gfn_to_pfn_cache *gpc, gpa_t gpa, mutex_unlock(&gpc->refresh_lock); if (unmap_old) - gpc_unmap_khva(kvm, old_pfn, old_khva); + gpc_unmap_khva(old_pfn, old_khva); return ret; } @@ -353,7 +353,7 @@ void kvm_gpc_unmap(struct kvm *kvm, struct gfn_to_pfn_cache *gpc) write_unlock_irq(&gpc->lock); mutex_unlock(&gpc->refresh_lock); - gpc_unmap_khva(kvm, old_pfn, old_khva); + gpc_unmap_khva(old_pfn, old_khva); } EXPORT_SYMBOL_GPL(kvm_gpc_unmap);