Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1435164pxm; Thu, 3 Mar 2022 18:08:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJyEm2bpZOloWhqJLtXJE7k2cJN5SQmdwm39wcME052J3Vxb1BBNkE5/W/vjSYVKBdr1kpyr X-Received: by 2002:a17:906:358e:b0:6cf:61dd:5a1f with SMTP id o14-20020a170906358e00b006cf61dd5a1fmr30116736ejb.416.1646359720539; Thu, 03 Mar 2022 18:08:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646359720; cv=none; d=google.com; s=arc-20160816; b=DM4WNgzXV/Izt+aaQFq2x8iVeagAr7XIO/NmKWQcla8tFVs0OsswojnLC6JerR1U+q ZA0JIJuLaKy47t8uud6SP2g7v3sfc1tpvGfmqORcetaYDlPQP3gQmj0pSzEOyCFLvWEQ DB0R/QYTzXHWKY8IO2FwivyyuIATwXGeehvVeVA5GXQMVGbDNaZ2VyQpkvtM+Oa+wMgW bsmP2wUhZcO1fa+sDxEQOnIoONE23LVOqepbFnA5JYrkY1Hhgtc+ou3vIdXP5LZKmOwW 7UoukC0z+Ul9FHprnx2fVugPuHtzgbHTzxUVr/+qbCROHK7+e3+HrfiykyA9RPE21aSy 2xsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=XQpQ3m9Rtf9LR3kz2qxftzIEj8ln513NcB495ceelT4=; b=ygCgtcqmKmfRYP0W22iBOntCDX/twIdbeo6pzQ1ulzjb1l/aT+G21cvMJlcYrqtqWn mzHvMVwsw9/JJMVeIjk6nCTqvowsXLV8aogne/D2krFchLo4FsbWDS67tWkOXjoypnlp nklj8oNsiWyf4RAbF1wQoR4eJtGU2DROYLIzuiSViUs2QQkBbgwYdhCwr9tA5sZ0b589 W17BcyzxB1WQFNYpcmxUbh07d4McDnKuOC5rcn5z3UwyBBgrePwk+dPk/TqLKAQ0rpbP b9sa3bzm+fm3V/4opevj/NQyxd3QTQxGYv9UuPjbiGbgRJJuVOlwY591CZJHcrtYi0mg b18A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=M5BhchOG; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cw1-20020a056402228100b004132fa9bb35si2327410edb.4.2022.03.03.18.08.17; Thu, 03 Mar 2022 18:08:40 -0800 (PST) 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=M5BhchOG; 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 S235068AbiCCSfT (ORCPT + 99 others); Thu, 3 Mar 2022 13:35:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234176AbiCCSfR (ORCPT ); Thu, 3 Mar 2022 13:35:17 -0500 Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40CE6198ED3 for ; Thu, 3 Mar 2022 10:34:31 -0800 (PST) Received: by mail-pg1-x52d.google.com with SMTP id o8so5296377pgf.9 for ; Thu, 03 Mar 2022 10:34:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=XQpQ3m9Rtf9LR3kz2qxftzIEj8ln513NcB495ceelT4=; b=M5BhchOGalZGDRNQ28oPcuwRqgRGSqglUZdoWjpLy3sC+QzcDMziJuStaAuWnKqEte VfGea+ht08trhrgvxja+7R93F6wCLTlRKf031phcl8xVdGQxcl10cL6Gbc933gkxEJ0g 5CIl+mfyE+C3w5C+XqH3jArZpySK8h8+G298Sf3QDOL53I2+O8QFHGiH5MrLwviWYs+l rMP1HlY7C0FNGovaC5YtuxQlyP/dTApMgKYxg6Uyinc7WODYETqOOqj7EOJ5GJ/+a5KY LCD6Q2+2/9M3ynLRpG30/UtpZjAPr6QzfNyYRvwxDlqcS74KgF2PXciuTmeKrBZeNoNB Gs9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=XQpQ3m9Rtf9LR3kz2qxftzIEj8ln513NcB495ceelT4=; b=YNl0eJFy0B8oN0xJR7pHew9bAS1qK0IiVQQjLDVfOcHZk2vnRpMca70O6b2aCO51cH 84CJjtkK2J1QAu+IPNmyf9kIFoEqjC3bowwouQ6XoFMCyFfhHM9mX9XKfXob3dcsQTrv /76AmVqJWOq0Av4TMVOzUsHKe6aVCiqN0ME57QsamfT5nfptPn+pUIIOgRCGETewcMfY uTW0klxDtFSPlSI2Jt11yVen5Gl7uYn/hZmLt1SOqZuiwJMvp34rDAMX73ta/1TF1wTO 7gshWBI2fCMTzVrEnZZ1+f2BB+OtZJmkUefD97u7A9jCwW0k/MBrzwxickWoKRkTFyLM EjvA== X-Gm-Message-State: AOAM532G8/Jdu9m8PSSdwxbXmyjP3w8jLCtas3U9FkAN6aWwkgpkF9+/ MC2gld02aDR/0UO5VBpa41htEA== X-Received: by 2002:a05:6a00:7c6:b0:4e1:799:7a2 with SMTP id n6-20020a056a0007c600b004e1079907a2mr39059440pfu.25.1646332470420; Thu, 03 Mar 2022 10:34:30 -0800 (PST) Received: from google.com (226.75.127.34.bc.googleusercontent.com. [34.127.75.226]) by smtp.gmail.com with ESMTPSA id t41-20020a056a0013a900b004e167af0c0dsm3210601pfg.89.2022.03.03.10.34.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Mar 2022 10:34:29 -0800 (PST) Date: Thu, 3 Mar 2022 18:34:26 +0000 From: Mingwei Zhang To: Sean Christopherson Cc: Paolo Bonzini , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , David Hildenbrand , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Ben Gardon Subject: Re: [PATCH v3 10/28] KVM: x86/mmu: Add helpers to read/write TDP MMU SPTEs and document RCU Message-ID: References: <20220226001546.360188-1-seanjc@google.com> <20220226001546.360188-11-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220226001546.360188-11-seanjc@google.com> X-Spam-Status: No, score=-18.1 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable 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: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Feb 26, 2022, Sean Christopherson wrote: > Add helpers to read and write TDP MMU SPTEs instead of open coding > rcu_dereference() all over the place, and to provide a convenient > location to document why KVM doesn't exempt holding mmu_lock for write > from having to hold RCU (and any future changes to the rules). > > No functional change intended. > > Signed-off-by: Sean Christopherson > Reviewed-by: Ben Gardon Reviewed-by: Mingwei Zhang > --- > arch/x86/kvm/mmu/tdp_iter.c | 6 +++--- > arch/x86/kvm/mmu/tdp_iter.h | 16 ++++++++++++++++ > arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- > 3 files changed, 22 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/kvm/mmu/tdp_iter.c b/arch/x86/kvm/mmu/tdp_iter.c > index be3f096db2eb..6d3b3e5a5533 100644 > --- a/arch/x86/kvm/mmu/tdp_iter.c > +++ b/arch/x86/kvm/mmu/tdp_iter.c > @@ -12,7 +12,7 @@ static void tdp_iter_refresh_sptep(struct tdp_iter *iter) > { > iter->sptep = iter->pt_path[iter->level - 1] + > SHADOW_PT_INDEX(iter->gfn << PAGE_SHIFT, iter->level); > - iter->old_spte = READ_ONCE(*rcu_dereference(iter->sptep)); > + iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep); > } > > static gfn_t round_gfn_for_level(gfn_t gfn, int level) > @@ -89,7 +89,7 @@ static bool try_step_down(struct tdp_iter *iter) > * Reread the SPTE before stepping down to avoid traversing into page > * tables that are no longer linked from this entry. > */ > - iter->old_spte = READ_ONCE(*rcu_dereference(iter->sptep)); > + iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep); > > child_pt = spte_to_child_pt(iter->old_spte, iter->level); > if (!child_pt) > @@ -123,7 +123,7 @@ static bool try_step_side(struct tdp_iter *iter) > iter->gfn += KVM_PAGES_PER_HPAGE(iter->level); > iter->next_last_level_gfn = iter->gfn; > iter->sptep++; > - iter->old_spte = READ_ONCE(*rcu_dereference(iter->sptep)); > + iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep); > > return true; > } > diff --git a/arch/x86/kvm/mmu/tdp_iter.h b/arch/x86/kvm/mmu/tdp_iter.h > index 216ebbe76ddd..bb9b581f1ee4 100644 > --- a/arch/x86/kvm/mmu/tdp_iter.h > +++ b/arch/x86/kvm/mmu/tdp_iter.h > @@ -9,6 +9,22 @@ > > typedef u64 __rcu *tdp_ptep_t; > > +/* > + * TDP MMU SPTEs are RCU protected to allow paging structures (non-leaf SPTEs) > + * to be zapped while holding mmu_lock for read. Holding RCU isn't required for > + * correctness if mmu_lock is held for write, but plumbing "struct kvm" down to > + * the lower depths of the TDP MMU just to make lockdep happy is a nightmare, so > + * all accesses to SPTEs are done under RCU protection. > + */ > +static inline u64 kvm_tdp_mmu_read_spte(tdp_ptep_t sptep) > +{ > + return READ_ONCE(*rcu_dereference(sptep)); > +} > +static inline void kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 val) > +{ > + WRITE_ONCE(*rcu_dereference(sptep), val); > +} > + > /* > * A TDP iterator performs a pre-order walk over a TDP paging structure. > */ > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > index 4f460782a848..8fbf3364f116 100644 > --- a/arch/x86/kvm/mmu/tdp_mmu.c > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > @@ -609,7 +609,7 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm, > * here since the SPTE is going from non-present > * to non-present. > */ > - WRITE_ONCE(*rcu_dereference(iter->sptep), 0); > + kvm_tdp_mmu_write_spte(iter->sptep, 0); > > return 0; > } > @@ -648,7 +648,7 @@ static inline void __tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter, > */ > WARN_ON(is_removed_spte(iter->old_spte)); > > - WRITE_ONCE(*rcu_dereference(iter->sptep), new_spte); > + kvm_tdp_mmu_write_spte(iter->sptep, new_spte); > > __handle_changed_spte(kvm, iter->as_id, iter->gfn, iter->old_spte, > new_spte, iter->level, false); > @@ -1046,7 +1046,7 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) > * because the new value informs the !present > * path below. > */ > - iter.old_spte = READ_ONCE(*rcu_dereference(iter.sptep)); > + iter.old_spte = kvm_tdp_mmu_read_spte(iter.sptep); > } > > if (!is_shadow_present_pte(iter.old_spte)) { > -- > 2.35.1.574.g5d30c73bfb-goog >