Received: by 2002:ac8:4602:0:b0:405:464a:c27a with SMTP id p2csp2532450qtn; Tue, 25 Jul 2023 11:26:26 -0700 (PDT) X-Google-Smtp-Source: APBJJlG7G/JCzNMaeVW5EAxC0u1xe/lotzx6LTw02MBFt+YhRmYqmqbpV6ygO1eO/AiLkQIHKzKx X-Received: by 2002:aa7:cd42:0:b0:522:5980:ae08 with SMTP id v2-20020aa7cd42000000b005225980ae08mr282510edw.18.1690309586582; Tue, 25 Jul 2023 11:26:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690309586; cv=none; d=google.com; s=arc-20160816; b=ScIVmwnO0yHVRwwujpL2y0nwjtzEWbzbmzs9OSvjTruzU+vGsqVRcb82tfVKi2SxP8 kVbXxigjw1m6tTnrOVnSSyI18wc2mYWye3ZEv1TAX5Xlsj0ODEXzU5vfup149F5KYCy6 p7zupolibgvgua6BiDlKKpnykvjhlbrtNIeTneyglGtfGhNhO0T2AFnndYDQrq89mStm 4MEn+GzjOc238/6Zl+/LWb1TCqfgK14YUVjclzZyVIqzifv+3xjO6FtB6zyqMBOb+JQc qkkqGHIY0ooJCyNFqL0PlFMHTz/Ie9BNykWRz2poMMNAQZeX7vhwx6zNMVlvNpgCgCBB cs2Q== 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:dkim-signature; bh=BIo8qHauKdQX+T8FT8wbaojm6/JdQZAwM03PT/kIykI=; fh=xcKzdnaY6g0iVmTPXvrxZn2Gc5tDGrPbnib4+NxvqAg=; b=JdE6tlYqxGI16YER+WHfnVQYJQMt/46ktuoTl9l9la9WVFFMRriWBpf37NYHT813AK jYINhN0MBKq8/AheoVFHqKFIFC9++qLyDADtxEF4Xq4EXo96+cSXBcHz6gGwVBhn3P7J ENXYQ+Yf6W/smyIWM2cEmLWDDms4N6hdJRt+gv1gJCmy5RyQi0XEcFSiNC+MF+cWXKOP j8SCivZTXSJg2HNbV72BeUHyZkzqz/KGHguRA6M9IsYZcGAK3Fh7P/jT+8P/JghQ7fMj a+fgdSH2uJqC1rWydigif6NyGiVNFamD86K5W+IOu5k6rPgivYVYiTjTu5nDtNiwD8vB pODw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=7nXQcBRp; 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 q17-20020a056402033100b0051e029c13efsi8209041edw.365.2023.07.25.11.26.00; Tue, 25 Jul 2023 11:26:26 -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=20221208 header.b=7nXQcBRp; 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 S229683AbjGYSFh (ORCPT + 99 others); Tue, 25 Jul 2023 14:05:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232241AbjGYSFe (ORCPT ); Tue, 25 Jul 2023 14:05:34 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A52E1FE2 for ; Tue, 25 Jul 2023 11:05:32 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5704970148dso69824867b3.3 for ; Tue, 25 Jul 2023 11:05:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690308331; x=1690913131; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BIo8qHauKdQX+T8FT8wbaojm6/JdQZAwM03PT/kIykI=; b=7nXQcBRpxoTRBB3DVtfi1G3SLzO+8rbX0pZK3GqB/ZiRovVnhgEY5C+Gs70dPcpJKC v8EbJPLKK/PtzLxWiGJ5qeO4LJqB7HN3pWAhYbANBnQSVYoMTUMzCaeCD3zEfSqoM+r2 fDpLY03C1R0xadVk8zh9oHGZMZqSmWV+dliNXRuRczTowFXc/oEsFJjHeqmHR0h/7zjV 1SaakHlCZawqmTagIefeOP2xnfIobskVh1rmXibU4Cwg3i96EQeqcAc0By/i+P6tJXi9 1AMdfmti0ZHyvBS0qJY6Ig052U8rUWQC7aHvZS0sxZXtvMY234BUecIw+5qK/UmUColq VbWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690308331; x=1690913131; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BIo8qHauKdQX+T8FT8wbaojm6/JdQZAwM03PT/kIykI=; b=QhqWgIGM7nyWhVShJFV01zl8xjPuhlI5k2qC0KZeoXCEZImvUDE2JDoXUq7BOuy5oU lvJBGgInC3d6w7+HjYKeQ38IHZq0ir8EtPKLbgxruJSfakZG17RcsMVG8AXAeOd5j0zW mM6Lq9ZE89WTH6/wO3e0UFe7UIrP+jw70AnCYrCfQhnMdVKCi8yWdISHriRAJlFfyh3i fKKhJTco55Z6eQUdri0kzW5gJcP9gqs3cjQFaH4EmyRGFiVp+BgSDWfz0v6p+jSn4vRt 22aAgHyT+gdnVzWamo2bmIkrpVS3bxY2IFRkHrEFR5mL7r9Ef3rwYxyvV2/nJro6tcts /zBA== X-Gm-Message-State: ABy/qLb629BCHXzjP+5bxsah+xT7R8K1FiHMDhc5YBr1zoDAGJpN0STd WQ1rFuqzmbAMCOCjOWPgsxnSURlCUGM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:4509:0:b0:573:5797:4b9e with SMTP id s9-20020a814509000000b0057357974b9emr213ywa.1.1690308331572; Tue, 25 Jul 2023 11:05:31 -0700 (PDT) Date: Tue, 25 Jul 2023 11:05:29 -0700 In-Reply-To: Mime-Version: 1.0 References: <20230718234512.1690985-1-seanjc@google.com> <20230718234512.1690985-2-seanjc@google.com> Message-ID: Subject: Re: [RFC PATCH v11 01/29] KVM: Wrap kvm_gfn_range.pte in a per-action union From: Sean Christopherson To: Xu Yilun Cc: Yan Zhao , Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Yu Zhang , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , Vlastimil Babka , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Content-Type: text/plain; charset="us-ascii" 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,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_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 Fri, Jul 21, 2023, Xu Yilun wrote: > On 2023-07-21 at 14:26:11 +0800, Yan Zhao wrote: > > On Tue, Jul 18, 2023 at 04:44:44PM -0700, Sean Christopherson wrote: > > > > May I know why KVM now needs to register to callback .change_pte()? > > I can see the original purpose is to "setting a pte in the shadow page > table directly, instead of flushing the shadow page table entry and then > getting vmexit to set it"[1]. > > IIUC, KVM is expected to directly make the new pte present for new > pages in this callback, like for COW. Yes. > > As also commented in kvm_mmu_notifier_change_pte(), .change_pte() must be > > surrounded by .invalidate_range_{start,end}(). > > > > While kvm_mmu_notifier_invalidate_range_start() has called kvm_unmap_gfn_range() > > to zap all leaf SPTEs, and page fault path will not install new SPTEs > > successfully before kvm_mmu_notifier_invalidate_range_end(), > > kvm_set_spte_gfn() should not be able to find any shadow present leaf entries to > > update PFN. > > I also failed to figure out how the kvm_set_spte_gfn() could pass > several !is_shadow_present_pte(iter.old_spte) check then write the new > pte. It can't. .change_pte() has been dead code on x86 for 10+ years at this point, and if my assessment from a few years back still holds true, it's dead code on all architectures. The only reason I haven't formally proposed dropping the hook is that I don't want to risk the patch backfiring, i.e. I don't want to prompt someone to care enough to try and fix it. commit c13fda237f08a388ba8a0849785045944bf39834 Author: Sean Christopherson Date: Fri Apr 2 02:56:49 2021 +0200 KVM: Assert that notifier count is elevated in .change_pte() In KVM's .change_pte() notification callback, replace the notifier sequence bump with a WARN_ON assertion that the notifier count is elevated. An elevated count provides stricter protections than bumping the sequence, and the sequence is guarnateed to be bumped before the count hits zero. When .change_pte() was added by commit 828502d30073 ("ksm: add mmu_notifier set_pte_at_notify()"), bumping the sequence was necessary as .change_pte() would be invoked without any surrounding notifications. However, since commit 6bdb913f0a70 ("mm: wrap calls to set_pte_at_notify with invalidate_range_start and invalidate_range_end"), all calls to .change_pte() are guaranteed to be surrounded by start() and end(), and so are guaranteed to run with an elevated notifier count. Note, wrapping .change_pte() with .invalidate_range_{start,end}() is a bug of sorts, as invalidating the secondary MMU's (KVM's) PTE defeats the purpose of .change_pte(). Every arch's kvm_set_spte_hva() assumes .change_pte() is called when the relevant SPTE is present in KVM's MMU, as the original goal was to accelerate Kernel Samepage Merging (KSM) by updating KVM's SPTEs without requiring a VM-Exit (due to invalidating the SPTE). I.e. it means that .change_pte() is effectively dead code on _all_ architectures. x86 and MIPS are clearcut nops if the old SPTE is not-present, and that is guaranteed due to the prior invalidation. PPC simply unmaps the SPTE, which again should be a nop due to the invalidation. arm64 is a bit murky, but it's also likely a nop because kvm_pgtable_stage2_map() is called without a cache pointer, which means it will map an entry if and only if an existing PTE was found. For now, take advantage of the bug to simplify future consolidation of KVMs's MMU notifier code. Doing so will not greatly complicate fixing .change_pte(), assuming it's even worth fixing. .change_pte() has been broken for 8+ years and no one has complained. Even if there are KSM+KVM users that care deeply about its performance, the benefits of avoiding VM-Exits via .change_pte() need to be reevaluated to justify the added complexity and testing burden. Ripping out .change_pte() entirely would be a lot easier.