From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF89DC433EF for ; Thu, 13 Jan 2022 19:31:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230046AbiAMT3N (ORCPT ); Thu, 13 Jan 2022 14:29:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231237AbiAMT3M (ORCPT ); Thu, 13 Jan 2022 14:29:12 -0500 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B6E6C06161C for ; Thu, 13 Jan 2022 11:29:12 -0800 (PST) Received: by mail-pf1-x42f.google.com with SMTP id m21so768444pfd.3 for ; Thu, 13 Jan 2022 11:29:12 -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=ROVS5rBm3Cz2W7S8I9dH4k+dRH+0gu+Fwbo6sb8zAv4=; b=qYxLeriBR4hEl0PAMbDC6uHko0lBwc7MNT6+tSu1ZVoUxbUJ8n44ouopnHLy/mz092 mz0sOLWk0wVlutM9QlUvbj+FYEzJBPkcGFOZlp7JELxPfbibtXdFoO9q4WKKi2Ykm0nl +UgRytc99ed0b0jF0XFc2IF64mGrk+yito7s3BSv6GYy+6cyEdMUqKSil1L1ILha7YoP raE0ZCCb443bMAIknGUdU1r3qHxZ7NGBRurlMZeql9L4KZp0UsH0FBohCimq36x5aBxN i976s9+72rkrxev9bctJrT/2V3fi315+S40dE9xd9ib9eywhvjGcCzLMpC7u8f0o4aRO F+qQ== 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=ROVS5rBm3Cz2W7S8I9dH4k+dRH+0gu+Fwbo6sb8zAv4=; b=AdHqQvGsBir7yY+qzuJTziTmTpE5ele24YSgW1TyoqDRRjd6XPU5nQkmjJcTR2fO6H 7PKQGztYAToB6qGiOrAnlTdm9IvOxEiocnYbccB6Afg7eNZGATvUENOCnTVW1j8wi6x9 7Yjatsm/6MPXJ8EGCMBf+KF7UBHqm4S/Hkzgu2rPPGbKHWORlRc/1jAogLox7pCo1eZw mcwflULH15YXp3IYC4BmJQCIT59zKX+4jvjSbDRN4Ll/2VTTGS2Z6eNLNu6jg9iKbr0n 6rQNmgGhyAfF1J+GpLPymP3OgCwaTLc9kHYQ8PHwmVkJVAf+omxsmgEXeT6rl/RWh9Hq 6TdA== X-Gm-Message-State: AOAM530kJcXA2oWFcUL3CnfwccfgklSGG7ONAa469iIq6IzZ4s63zLPs nU8OG3GwhzmWuzdeyUEVzF/tzQ== X-Google-Smtp-Source: ABdhPJzp+3opgjvBAgLmiA+ZDVQGYD2H3ErTGoj7k05eePcJiZvAUhJhuN+yqFX0jKDuereypjnrHg== X-Received: by 2002:a63:7845:: with SMTP id t66mr5203707pgc.103.1642102151862; Thu, 13 Jan 2022 11:29:11 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id n26sm2818946pgb.91.2022.01.13.11.29.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jan 2022 11:29:11 -0800 (PST) Date: Thu, 13 Jan 2022 19:29:07 +0000 From: Sean Christopherson To: David Matlack Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Ben Gardon , kvm list , stable@vger.kernel.org Subject: Re: [PATCH 1/2] KVM: x86/mmu: Fix write-protection of PTs mapped by the TDP MMU Message-ID: References: <20220112215801.3502286-1-dmatlack@google.com> <20220112215801.3502286-2-dmatlack@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org On Thu, Jan 13, 2022, David Matlack wrote: > On Thu, Jan 13, 2022 at 9:04 AM David Matlack wrote: > > > > On Wed, Jan 12, 2022 at 4:29 PM Sean Christopherson wrote: > > > > Oh interesting. I actually find that confusing because it can easily > > > > lead to the MMU-writable bit staying set. Here we are protecting GFNs > > > > and we're opting to leave the MMU-writable bit set. It takes a lot of > > > > digging to figure out that this is safe because if MMU-writable is set > > > > and the SPTE cannot be locklessly be made writable then it implies > > > > Host-writable is clear, and Host-writable can't be reset without > > > > syncing the all shadow pages reachable by the MMU. Oh and the > > > > MMU-writable bit is never consulted on its own (e.g. We never iterate > > > > through all SPTEs to find the ones that are !MMU-writable). > > > > > > Ah, you've missed the other wrinkle: MMU-writable can bet set iff Host-writable > > > is set. In other words, the MMU-writable bit is never left set because it can't > > > be set if spte_can_locklessly_be_made_writable() returns false. > > The changed_pte notifier looks like it clears Host-writable without > clearing MMU-writable. Specifically the call chain: > > kvm_mmu_notifier_change_pte() > kvm_set_spte_gfn() > kvm_tdp_mmu_set_spte_gfn() > set_spte_gfn() > kvm_mmu_changed_pte_notifier_make_spte() > > Is there some guarantee that old_spte is !MMU-writable at this point? Ugh, I misread that code, multiple times. There's no guarantee, it was likely just missed when MMU-writable was introduced. Note, you literally cannot hit that code path in current kernels. See commit c13fda237f08 ("KVM: Assert that notifier count is elevated in .change_pte()"). So whatever you do is effectively untestable. I really want to rip out .change_pte(), but I also don't want to do any performance testing to justify removing the code instead of fixing it proper, so it's hung around as a zombie... > If not I could easily change kvm_mmu_changed_pte_notifier_make_spte() > to also clear MMU-writable and preserve the invariant. Yes, please.