Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp481547pxu; Thu, 7 Jan 2021 09:43:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJyg3eNP+CsYUbaw/tk27UO2vxZcLEjg1suRVjeE6s/VN6UoGaIT3hhJJFVefj578fomc3kb X-Received: by 2002:aa7:d494:: with SMTP id b20mr2595440edr.330.1610041405099; Thu, 07 Jan 2021 09:43:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610041405; cv=none; d=google.com; s=arc-20160816; b=qEBcSxztwFqR3F7/f3AjczupC8Va3i1BAcX6BcDMWdqawJyXGozQyZIrM38azlRghe K5jgCxPUqh608WGPNi11JBEeTPgJzGgrrLwQ3ZfMGiAGBnaG0vq9Dn9uXXSLYcXanyk/ 6Ha6q23rW36ztUDDvEfw9s/eDoTawrdYiJefMnFZKO2JjNDf+c0E44nJvcQ+T1KL4wBD ox02UYHdWDl4nRV4bIn1bmnS6wYct9DbGi5jbF9UzB24z5teVn5V3vfIIx60le3QWoH6 Fbx9eD8YJlc5VyaotuUy8pjY5uFiyNqnAVbh9VzDDnMXpp5Ac0a6/+WAQi9YOcD+tS9/ E33g== 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=ILMPwCwZrBJ1HMfYPpc9UDsKQwFZc17yoGrjxBe8NiI=; b=vPzBN0NFHRYwlKK7P7k3oCPlNOxlPhrmwqWslI1j+/hEpA87OZ4QzyfaHxAWpfT3aW 0lC4tlz3FN/Gc0Vt+lRWwmnxAg5nvOru6cfhAa6r+3Rgx352lQ1gsMwrGzslbKR1NbiW cOQL1sh1re8izyggk8rfErryNKCth7qqaKlQGEyhqosVfCxFIGwQ6+rf+yX+v2YUb6+I ZZ8q1n+wSQG01O1bfFl2qTZIJ1UieeBtJze8a8rjWGa3SHFsmY1Se6e6mAEUcjHau/UT +7E9WXnzcUeOYQEAbI3VW1j/0oFbUvqh+3ZYZpPnzoTRvUScN1Uy2Avk7lGoQlzqu/I7 gxlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=K9lAta31; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e16si2399781eja.462.2021.01.07.09.43.02; Thu, 07 Jan 2021 09:43:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=K9lAta31; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1728808AbhAGRll (ORCPT + 99 others); Thu, 7 Jan 2021 12:41:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726165AbhAGRll (ORCPT ); Thu, 7 Jan 2021 12:41:41 -0500 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7C08C0612F6 for ; Thu, 7 Jan 2021 09:41:00 -0800 (PST) Received: by mail-pf1-x436.google.com with SMTP id s21so4280312pfu.13 for ; Thu, 07 Jan 2021 09:41:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ILMPwCwZrBJ1HMfYPpc9UDsKQwFZc17yoGrjxBe8NiI=; b=K9lAta31bHz+XI4lQJtLUqRu5RcufMnNjXJo+/bdjD0p9WrHHbUYeZQGmSZ1fAZz8B nvWIKhZnKvYhNujvKH6UslFurON7uXp5dU1M+l/QmGFyYlOMpjTpGytFXN9P2fKMgRel uyyjS7Y5Km6xgNzEZqqtIREQyVXfHGbVBOhODyO+Ym9Xq2hHXNci9WYpUShSWI1LyW0D u0AcR1XmNrj+xNIyE2rMUf8/SJ+L+LhHGCIRxqHpI++IQ3f0WjeIK/dQK1YkXfWIBT0q /gAU/0jlm+S+wsX0KeTdTxAWlRSOhVK/4n+2zvx2Ja62VUURI69aPsMrTTNB28VgV4aD luQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ILMPwCwZrBJ1HMfYPpc9UDsKQwFZc17yoGrjxBe8NiI=; b=YYp9WGtvTQQsMeovnotyxaMPjCYPlWnvVc0bejw+86TPF4Lf0Z9OwFsEqqb4HjB3sM /lqj3Bel4SoLFzh8B4TSbAREQH1n/H8zSBXgc6QS3zSAGkqwOhr3lcZz+bUdpxd5rF1D WfdwFB0ewhflLuv4iwJbBiu4VK0XDHDgksZKs8IUJ0s8RIg+V+qqUT3Qv+q146sq16yD j31vYHxzrHIeOHKufjtPNjEFwub1l2KFRT/VUdKlFkw4vTAxH8zZYtZ3/3+BflXM8g0s SRipPLkqHE/A7R0WWtST1+ZUc1yjYjZ8jWa8Yq69JOjCFZniJY1XskyGHZmvwEHvngAd LTqQ== X-Gm-Message-State: AOAM532Jskom8eqlKXf6T7Rq2W13nEmIS1uY3YcwHzuRx2rstocEs3kd 1elHS2iXjI1bzDQAEf1BFz331Q== X-Received: by 2002:a65:5209:: with SMTP id o9mr2934295pgp.34.1610041260269; Thu, 07 Jan 2021 09:41:00 -0800 (PST) Received: from google.com ([2620:15c:f:10:1ea0:b8ff:fe73:50f5]) by smtp.gmail.com with ESMTPSA id a12sm7145470pgq.5.2021.01.07.09.40.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jan 2021 09:40:59 -0800 (PST) Date: Thu, 7 Jan 2021 09:40:53 -0800 From: Sean Christopherson To: Ben Gardon Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Peter Shier , "Maciej S . Szmigiero" , Leo Hou Subject: Re: [PATCH v3 1/2] KVM: x86/mmu: Ensure TDP MMU roots are freed after yield Message-ID: References: <20210107001935.3732070-1-bgardon@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210107001935.3732070-1-bgardon@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the future, please document the changes in each revision, e.g. in a cover letter or in the ignored part of the diff. On Wed, Jan 06, 2021, Ben Gardon wrote: > Many TDP MMU functions which need to perform some action on all TDP MMU > roots hold a reference on that root so that they can safely drop the MMU > lock in order to yield to other threads. However, when releasing the > reference on the root, there is a bug: the root will not be freed even > if its reference count (root_count) is reduced to 0. > > To simplify acquiring and releasing references on TDP MMU root pages, and > to ensure that these roots are properly freed, move the get/put operations > into another TDP MMU root iterator macro. > > Moving the get/put operations into an iterator macro also helps > simplify control flow when a root does need to be freed. Note that using > the list_for_each_entry_safe macro would not have been appropriate in > this situation because it could keep a pointer to the next root across > an MMU lock release + reacquire, during which time that root could be > freed. > > Reported-by: Maciej S. Szmigiero > Suggested-by: Paolo Bonzini > Fixes: faaf05b00aec ("kvm: x86/mmu: Support zapping SPTEs in the TDP MMU") > Fixes: 063afacd8730 ("kvm: x86/mmu: Support invalidate range MMU notifier for TDP MMU") > Fixes: a6a0b05da9f3 ("kvm: x86/mmu: Support dirty logging for the TDP MMU") > Fixes: 14881998566d ("kvm: x86/mmu: Support disabling dirty logging for the tdp MMU") > Signed-off-by: Ben Gardon Reviewed-by: Sean Christopherson > --- > arch/x86/kvm/mmu/tdp_mmu.c | 104 +++++++++++++++++-------------------- > 1 file changed, 48 insertions(+), 56 deletions(-) > > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > index 75db27fda8f3..d4191ed193cd 100644 > --- a/arch/x86/kvm/mmu/tdp_mmu.c > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > @@ -44,7 +44,48 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm) > WARN_ON(!list_empty(&kvm->arch.tdp_mmu_roots)); > } > > -#define for_each_tdp_mmu_root(_kvm, _root) \ > +static void tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root) > +{ > + if (kvm_mmu_put_root(kvm, root)) > + kvm_tdp_mmu_free_root(kvm, root); > +} > + > +static inline bool tdp_mmu_next_root_valid(struct kvm *kvm, > + struct kvm_mmu_page *root) > +{ > + lockdep_assert_held(&kvm->mmu_lock); > + > + if (list_entry_is_head(root, &kvm->arch.tdp_mmu_roots, link)) > + return false; > + > + kvm_mmu_get_root(kvm, root); > + return true; > + > +} > + > +static inline struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm, > + struct kvm_mmu_page *root) > +{ > + struct kvm_mmu_page *next_root; > + > + next_root = list_next_entry(root, link); > + tdp_mmu_put_root(kvm, root); > + return next_root; > +} > + > +/* > + * Note: this iterator gets and puts references to the roots it iterates over. Maybe refer to it as "the yield_safe() variant" instead of "this" so that the comment makes sense with minimal context? > + * This makes it safe to release the MMU lock and yield within the loop, but > + * if exiting the loop early, the caller must drop the reference to the most > + * recent root. (Unless keeping a live reference is desirable.) > + */ Rather than encourage manually dropping the reference, what adding about a scary warning about not exiting the loop early? At this point, it seems unlikely that we'll end up with a legitimate use case for exiting yield_safe() early. And if we do, I think it'd be better to provide a macro to do the bookeeping instead of open coding it in the caller. And maybe throw a blurb into the changelog about that so future developers understand that that scary warning isn't set in stone? /* * The yield_safe() variant of the TDP root iterator gets and puts references to * the roots it iterates over. This makes it safe to release the MMU lock and * yield within the loop, but the caller MUST NOT exit the loop early. */