Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1432957rwb; Wed, 14 Dec 2022 10:05:47 -0800 (PST) X-Google-Smtp-Source: AA0mqf4NpovoqvEBypuwB3Cfo7GtIhbq2lrCDDXlILJOqy25iq+Zc+wNrlKeyeNWoWHiux8mrJ+D X-Received: by 2002:a17:902:aa08:b0:189:fb78:a7d4 with SMTP id be8-20020a170902aa0800b00189fb78a7d4mr27628988plb.21.1671041147559; Wed, 14 Dec 2022 10:05:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671041147; cv=none; d=google.com; s=arc-20160816; b=wOXjsTZM69BfhD6yZdz43ZNuTVqZtmcCqWDga7+U5+jIT3KETFRkd8FPC9Y80k8Tdg cjJvPro0Z/DNQcuMaPtr331E7DGyaJ/Q6Q7WnOu00JHk9xxpmP+Q9vi7PCUYvoKB4VPX LpkBlVFVbaM/KLFhHFFMLmtrjkNjUeC2BpCy07DJALjMcxzNTpHy8uGQQYqz2O96VnCd Ebpd6YrV2tDJb/1nfbHo85KM4WrcRNR3+j4Hly0lccaJyoXEcYE7H/JQevN80JQtZzKk 9lR7/YRnBGAQt6EL/aronC1rjtqE1OhQfVH7w019QLNqzoeprmRqpIFWB7gcEdTCrse/ 0lIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=qtzqksgbDSBUnfThhwNIuG565HLgSHV/Y7SFXSX6iRk=; b=rIGeexIh/mGKi9Ty58a+ZkBlo3SUGjJCpVWd16N22fN6BvK9/aOqKMXHdELkW2AeKO 6QdNhmj0hrcGjPkuVFu/kDn0fY13mPJDBF0qL4miYBYLx7Orowfw32H78rfldTpVQ4x5 0jYtEaxqf9v4yjomB5sjbQvY5hwxlM/Hf/CoQorInM7futZ8J7e8Uhxp5B186mgb2hK9 Ywax3o8ify5vCUm1fEAkfqfsK7Cd//mrZmlHHMwu/Tm/g8zT5vZZkGv6N1Lp6tY0idNO 575dCpiZUvqwijp5jMWNzZSpiVTYW8ZMM+XQlcVUhZMbj7RomLW0tX4VDtUU2xP4PKVG 7D7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Uo4p9ILj; 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 u2-20020a170902bf4200b00186e8c3782esi3155251pls.386.2022.12.14.10.05.26; Wed, 14 Dec 2022 10:05:47 -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=Uo4p9ILj; 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 S237889AbiLNRxV (ORCPT + 69 others); Wed, 14 Dec 2022 12:53:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237321AbiLNRxS (ORCPT ); Wed, 14 Dec 2022 12:53:18 -0500 Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 712201274D for ; Wed, 14 Dec 2022 09:53:17 -0800 (PST) Received: by mail-qk1-x72b.google.com with SMTP id pe2so1598005qkn.1 for ; Wed, 14 Dec 2022 09:53:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=qtzqksgbDSBUnfThhwNIuG565HLgSHV/Y7SFXSX6iRk=; b=Uo4p9ILjJJYHbbHc3nJgt5xQFrwb3luZ6P6bVtkhqfKaeP3jDnRd6GXebGp25O1t3H cd2MP63e1aX2ujsbzlY3acrZbjlxvH+WKpV9udfhvXXTIm7pTrnBtS6UKUsdBjFyR9ZA me9TX1JZ5xpN4SV5vgLl3Bp3F4EcDGabBjuWUjSRqb5T3Sc5+EXPYUx1QgU1ppU+owo9 HiBede2+++uqyp1C1faFk3XpPsZ5W62tfkD3I6SN2eIDB3texOcYkSwNrhK8mXi9uiaY HA88oxlHm1vVoUML4lIbjAqPuRqfZD22ZQso1JPnOzvxTRvAHjVh2UMOPwnTsVwmPAOe OoCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qtzqksgbDSBUnfThhwNIuG565HLgSHV/Y7SFXSX6iRk=; b=En+wSaQyw/0L+tjzuluCqOprRgPAiC5h+wMCqQtRe7qD/E9Oyd3Yd3dXOWz0oJC81U 5mCT2PIwAwrB/Q21Xt5g5Y9Ccu0j+7Z/KwsrmTHsrdwgIb1AaUogN2HoLgJW0TJou4QH 0oK1jnSma1pHuaYpz2afnXonznenXgPCJJFQcpw7ZxMOETr1s/hn1HYDpTjBmCcZbl8V XskCy5Ev+Xl9rk7BnksMTCmg1U+wXANqEswadbwbHrXdCKSJE8FdBDmIpZbGffCZv9As M7NrBl61hbSJCIKAr1yhuBGz2tAEm7kD5JOeCl3fLCw4rS5d3qfQoTn64VoH4LHAOjcS fJkQ== X-Gm-Message-State: ANoB5pmk68FnqUW0OfepNna+8r580avLcsuB7ilZ5GXonpem1nf43EOG 3EkVOStkGdItE6x24Q5n6L9UBUtTWC5nWoUW358koQ== X-Received: by 2002:a37:808:0:b0:6fe:d95e:3cae with SMTP id 8-20020a370808000000b006fed95e3caemr8831426qki.768.1671040396424; Wed, 14 Dec 2022 09:53:16 -0800 (PST) MIME-Version: 1.0 References: <20221206173601.549281-1-bgardon@google.com> <20221206173601.549281-3-bgardon@google.com> In-Reply-To: From: Ben Gardon Date: Wed, 14 Dec 2022 09:53:05 -0800 Message-ID: Subject: Re: [PATCH 2/7] KVM: x86/MMU: Move rmap_iterator to rmap.h To: Sean Christopherson Cc: David Matlack , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Peter Xu , Vipin Sharma Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 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, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_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: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 13, 2022 at 4:59 PM Sean Christopherson wrote: > > On Tue, Dec 13, 2022, Ben Gardon wrote: > > On Fri, Dec 9, 2022 at 3:04 PM David Matlack wrote: > > > > > > > +/* > > > > + * Used by the following functions to iterate through the sptes linked by a > > > > + * rmap. All fields are private and not assumed to be used outside. > > > > + */ > > > > +struct rmap_iterator { > > > > + /* private fields */ > > > > + struct pte_list_desc *desc; /* holds the sptep if not NULL */ > > > > + int pos; /* index of the sptep */ > > > > +}; > > > > + > > > > +u64 *rmap_get_first(struct kvm_rmap_head *rmap_head, > > > > + struct rmap_iterator *iter); > > > > +u64 *rmap_get_next(struct rmap_iterator *iter); > > > > + > > > > +#define for_each_rmap_spte(_rmap_head_, _iter_, _spte_) \ > > > > + for (_spte_ = rmap_get_first(_rmap_head_, _iter_); \ > > > > + _spte_; _spte_ = rmap_get_next(_iter_)) > > > > + > > > > > > I always found these function names and kvm_rmap_head confusing since > > Heh, you definitely aren't the only one. > > > > they are about iterating through the pte_list_desc data structure. The > > > rmap (gfn -> list of sptes) is a specific application of the > > > pte_list_desc structure, but not the only application. There's also > > > parent_ptes in struct kvm_mmu_page, which is not an rmap, just a plain > > > old list of ptes. > > > > > While you are refactoring this code, what do you think about doing the > > > following renames? > > > > > > struct kvm_rmap_head -> struct pte_list_head > > > struct rmap_iterator -> struct pte_list_iterator > > > rmap_get_first() -> pte_list_get_first() > > > rmap_get_next() -> pte_list_get_next() > > > for_each_rmap_spte() -> for_each_pte_list_entry() > > I would strongly prefer to keep "spte" in this one regardless of what other naming > changes we do (see below). Maybe just for_each_spte()? IMO, "pte_list_entry" > unnecessarily obfuscates that it's a list of SPTEs. > > > > Then we can reserve the term "rmap" just for the actual rmap > > > (slot->arch.rmap), and code that deals with sp->parent_ptes will become > > > a lot more clear IMO (because it will not longer mention rmap). > > > > > > e.g. We go from this: > > > > > > struct rmap_iterator iter; > > > u64 *sptep; > > > > > > for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) { > > > ... > > > } > > > > > > To this: > > > > > > struct pte_list_iterator iter; > > > u64 *sptep; > > > > > > for_each_pte_list_entry(&sp->parent_ptes, &iter, sptep) { > > > ... > > > } > > > > I like this suggestion, and I do think it'll make things more > > readable. It's going to be a huge patch to rename all the instances of > > kvm_rmap_head, but it's probably worth it. > > I generally like this idea too, but tying into my above comment, before jumping > in I think we should figure out what end state we want, i.e. get the bikeshedding > out of the way now to hopefully avoid dragging out a series while various things > get nitpicked. > > E.g. if we if we just rename the structs and their macros, then we'll end up with > things like > > static bool slot_rmap_write_protect(struct kvm *kvm, > struct pte_list_head *rmap_head, > const struct kvm_memory_slot *slot) > { > return rmap_write_protect(rmap_head, false); > } > > which isn't terrible, but there's still opportunity for cleanup, e.g. > rmap_write_protect() could easily be sptes_write_protect() or write_protect_sptes(). > > That will generate a naming conflict of sorts with pte_list_head if we don't also > rename that to spte_list_head. And I think capturing that it's a list of SPTEs and > not guest PTEs will be helpful in general. > > And if we rename pte_list_head, then we might as well commit 100% and use consisnent > nomenclature across the board, e.g. end up with > > static bool sptes_clear_dirty(struct kvm *kvm, struct sptes_list_head *head, > const struct kvm_memory_slot *slot) > { > u64 *sptep; > struct spte_list_iterator iter; > bool flush = false; > > for_each_spte(head, &iter, sptep) { > if (spte_ad_need_write_protect(*sptep)) > flush |= spte_wrprot_for_clear_dirty(sptep); > else > flush |= spte_clear_dirty(sptep); > } > > return flush; > } > > versus the current > > static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head, > const struct kvm_memory_slot *slot) > { > u64 *sptep; > struct rmap_iterator iter; > bool flush = false; > > for_each_rmap_spte(rmap_head, &iter, sptep) > if (spte_ad_need_write_protect(*sptep)) > flush |= spte_wrprot_for_clear_dirty(sptep); > else > flush |= spte_clear_dirty(sptep); > > return flush; > } I'd be happy to see some consistent SPTE-based naming in the Shadow MMU and more or less get rid of the rmap naming scheme. Once you change to spte_list_head or whatever, the use of the actual rmap (an array of spte_list_heads) becomes super narrow. Given the potential for enormous scope creep on what's already going to be a long series, I'm inclined to split this work into two parts: 1. Move code from mmu.c to shadow_mmu.c with minimal cleanups / refactors / renames; just move the code 2. Clean up naming conventions: make the functions exported in shadow_mmu.h consistent, get rid of the whole rmap naming scheme, etc. That way git-blame will preserve context around the renames / refactors which would be obfuscated if we did 2 before 1, and we can reduce merge conflicts.