Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1488480rwr; Wed, 3 May 2023 16:26:10 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ74aRCCxDzWY4PMF8t/y/JaGHOxlhal3HOx+B789eS/tG3gKvTlziIhGsa3q4yW+DuYU2a8 X-Received: by 2002:a17:903:22c4:b0:19e:4bc3:b1ef with SMTP id y4-20020a17090322c400b0019e4bc3b1efmr1868658plg.64.1683156369774; Wed, 03 May 2023 16:26:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683156369; cv=none; d=google.com; s=arc-20160816; b=mpWupD2OmpuvxWyfpAP9IWDWtLTHfGq8KC2HrcYN9WITKfu4Cy8CZWIR/XGFcJaeRT CE0/k+uv412rKKOhXTI9Tcv+pZHJtCm3dW48AaS4YMXsEmSzB4Uzd9iO3JWs8wn4kTCO aUvCKCSKuKWjH01NFwo0buYApEy35xynuNku2k9nW/k47qtBSiaunSj8Iz+TPCGxXtyp SgCxv8I7sbfV4NCooICNpfdK/6TXcY1LiqyUSJdTDkzHWhSzvQdVGXMqO1JMvyoQ9nVD CBTjl9hlQPYISdtuha7DKky1qwKOs142rPzAswPzBzVIgcgPy+p1Xgee4oz6HySG8OoR c20Q== 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=VcB+Y5fDyixakWTeoinQOVeecLhRUTl+pbViwwNrtuQ=; b=RAaThU5WPRJx+5ZzadbcEJ/q0ss67Zho+KkzezpRSDrOlYSBbz3Iq4ac296BSuesjN 1HVI+V989pNwf0XHwhneRBugmkvNep/BHhy7nf3Mq6+MZ4xBrzksANlZxHcBuh+Xb0JM cHr+dGxrr9901w+wDBpUSgJYmrbmbZxFSocBAeJ6OY13HbxELRt1m/MrdMrEYd3/8jbV zCbyULDs84uvKkmncTTgHiMRmgX9qRC4MPXj83P02W9ckYYh2tysu+QxzTZtze1n+3Km 6O3cGPYylN9E6fLj2cR3U1fW5MrttvzJZ0HRLNJRqJUIQ5/zzYR5QhmAWVYnlKUrdTNu yOSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=sTeZoxVD; 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 ik27-20020a170902ab1b00b001aaecc8a84dsi8953758plb.645.2023.05.03.16.25.54; Wed, 03 May 2023 16:26:09 -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=sTeZoxVD; 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 S229672AbjECXQh (ORCPT + 99 others); Wed, 3 May 2023 19:16:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229622AbjECXQf (ORCPT ); Wed, 3 May 2023 19:16:35 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AC6293E6 for ; Wed, 3 May 2023 16:16:13 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1ab0f01ce43so22158345ad.1 for ; Wed, 03 May 2023 16:16:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683155772; x=1685747772; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=VcB+Y5fDyixakWTeoinQOVeecLhRUTl+pbViwwNrtuQ=; b=sTeZoxVDxdqS3PD2YH+hzY3HPDUPF4CDBpil3ScaC5e3sXkVGT/uJEkeaOrwNg3Zgb 37BoXrNRmGg/+0SCX9mPdqUq6njuBQHxQ6D9QGXpWlyL6WdcOB8AK+eY8A422ogU7Fup Lx9HYoY9kT10KPUZIF0eOJ7o+kZcARHSXpg+uwE+qWc976HUetby61zeLGqtVWlgNCfZ W/66w8bglbdx9ioh6DT4mqwxC0A+u1kW5jlQMMLE8PzDfQ5Dsq7GnyOAJPIjYbgNCQTt HztMBYNsGySpCSBrDm76lEVZgHCRRphG93ufQqUcEFTAg6jKycPQpkvucn1e8E1JDP7d uV8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683155772; x=1685747772; 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=VcB+Y5fDyixakWTeoinQOVeecLhRUTl+pbViwwNrtuQ=; b=JbKLG2CEycxUdOfnyAw2YYzEqOXysxBmgtdTTPzgTi1uZkVoql3PAs9CiYgABEI0Gf o8RbcyoXaSuh6YOlbRWL4n9baEOhK5+b5wfs0TFu4CiCRe78VZAn97LHpzawg3IticAN jDnvBpKbY06UFB/tMeE6Kid1q5MlBSG1CZV6WKo7c1fALPix1scQnSsfHfVMnEMezXp5 RvMZKPPYM2ULWFryjQZMdPZF5EPIqiz0ETDuZRSWzks0vUy0xAjqsKYHWJuu9sHGrQh3 s5UDoLFaM4KZejhlODRbQcN6zpNPQTy7eWX4vrhSBxLcBVKEgs/fCMisEd2WkqS7DLVm CIwg== X-Gm-Message-State: AC+VfDz0ySZJ/V5Im+KDhaO5qlX5CbhxcFlAUJ9kozjxyn7uQx1U3jEl oXqScElGBYXu+7ZAjHBwErnHpwhA9ds= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:234c:b0:1a2:87a2:c932 with SMTP id c12-20020a170903234c00b001a287a2c932mr513261plh.10.1683155772723; Wed, 03 May 2023 16:16:12 -0700 (PDT) Date: Wed, 3 May 2023 16:16:10 -0700 In-Reply-To: Mime-Version: 1.0 References: <20230311002258.852397-1-seanjc@google.com> <20230311002258.852397-26-seanjc@google.com> Message-ID: Subject: Re: [PATCH v2 25/27] KVM: x86/mmu: Drop @slot param from exported/external page-track APIs From: Sean Christopherson To: Yan Zhao Cc: kvm@vger.kernel.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, Zhenyu Wang , Ben Gardon , Paolo Bonzini , intel-gvt-dev@lists.freedesktop.org, Zhi Wang 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 Finally getting back to this series... On Thu, Mar 23, 2023, Yan Zhao wrote: > On Fri, Mar 17, 2023 at 04:28:56PM +0800, Yan Zhao wrote: > > On Fri, Mar 10, 2023 at 04:22:56PM -0800, Sean Christopherson wrote: > > ... > > > +int kvm_write_track_add_gfn(struct kvm *kvm, gfn_t gfn) > > > +{ > > > + struct kvm_memory_slot *slot; > > > + int idx; > > > + > > > + idx = srcu_read_lock(&kvm->srcu); > > > + > > > + slot = gfn_to_memslot(kvm, gfn); > > > + if (!slot) { > > > + srcu_read_unlock(&kvm->srcu, idx); > > > + return -EINVAL; > > > + } > > > + > > Also fail if slot->flags & KVM_MEMSLOT_INVALID is true? > > There should exist a window for external users to see an invalid slot > > when a slot is about to get deleted/moved. > > (It happens before MOVE is rejected in kvm_arch_prepare_memory_region()). > > Or using > if (!kvm_is_visible_memslot(slot)) { > srcu_read_unlock(&kvm->srcu, idx); > return -EINVAL; > } Hrm. If the DELETE/MOVE succeeds, then the funky accounting is ok (by the end of the series) as the tracking disappears on DELETE, KVMGT will reject MOVE, and KVM proper zaps SPTEs and resets accounting on MOVE (account_shadowed() runs under mmu_lock and thus ensures all previous SPTEs are zapped before the "flush" from kvm_arch_flush_shadow_memslot() can run). If kvm_prepare_memory_region() fails though... Ah, KVM itself is safe because of the aforementioned kvm_arch_flush_shadow_memslot(). Any accounting done on a temporarily invalid memslot will be unwound when the SPTEs are zapped. So for KVM, ignoring invalid memslots is correct _and necessary_. We could clean that up by having accounted_shadowed() use the @slot from the fault, which would close the window where the fault starts with a valid memslot but then sees an invalid memslot when accounting a new shadow page. But I don't think there is a bug there. Right, and DELETE can't actually fail in the current code base, and we've established that MOVE can't possibly work. So even if this is problematic in theory, there are no _unknown_ bugs, and the known bugs are fixed by the end of the series. And at the end of the series, KVMGT drops its tracking only when the DELETE is committed. So I _think_ allowing external trackers to add and remove gfns for write-tracking in an invalid slot is actually desirable/correct. I'm pretty sure removal should be allowed as that can lead to dangling write-protection in a rollback scenario. And I can't think of anything that will break (in the kernel) if write-tracking a gfn in an invalid slot is allowed, so I don't see any harm in allowing the extremely theoretical case of KVMGT shadowing a gfn in a to-be-deleted memslot _and_ the deletion being rolled back.