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 156AAC63797 for ; Tue, 31 Jan 2023 18:02:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230432AbjAaSCU (ORCPT ); Tue, 31 Jan 2023 13:02:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230016AbjAaSBq (ORCPT ); Tue, 31 Jan 2023 13:01:46 -0500 Received: from mail-yb1-xb34.google.com (mail-yb1-xb34.google.com [IPv6:2607:f8b0:4864:20::b34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2F5810AB3 for ; Tue, 31 Jan 2023 10:01:42 -0800 (PST) Received: by mail-yb1-xb34.google.com with SMTP id h5so19159031ybj.8 for ; Tue, 31 Jan 2023 10:01:42 -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=hG4GAMkUnvveluiCUBOIudNumzLbEEo3tvq5Dtd6sdM=; b=d8qqUxV1LG4n6/gqDu6Ip6tvP9Bw16loECWFruKdK+lu3xR/rbBa7gms//GRnUwkqw +P8e1LOicvj3Rzj3fXCHZxm6mu27qHlO7N4bfPcsGIVVEds4/Ou+w8GtAQ8hvor7aPNd bsZWRkL2k5mZHxy3UxUOV5wNaF+v5qStsCiiH5CcR95TMSxPzfqANnIFPVZ6rH0t8WTy xAFkZKEUP5LqQbAUvL7YlnHPaJ749U/45pnv3C5JwjIvIxAYEzVuIWK+vGuWOaNVifl7 egboZzNGbBXvHxLOG0jSSzqcO0EjPYo1lDZ1/lKC55N8gxZVS2LsvNiJH30QN2IqXRQx vE9Q== 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=hG4GAMkUnvveluiCUBOIudNumzLbEEo3tvq5Dtd6sdM=; b=R8TvPFtYq7ka1MIuwGc2apSefA6CNpduQMQwv7hS4Tg+0krr4vdmuSM2Tvwyk8YLug sDDyKpDAYyFwGThXvPQ870dyOuwXsZY5cJBgQDEWGW6Rr0YH8P1xK2wLiFUhDMp0FicB ihmLhdMAyrmBoybxKoLRnRPN+oYsxy39gm/GnAtWEzrAllCF8rXvQRmM9HOTyt6K/HPi DPBvOlBnKSzYkhpzXCIJmqBemSkdSPIu0ZZCh3WPr2RGQa/ZUl+p68bVuYBTs+8ax/Ah FJVCsSpX85GgztFQm/Zwjn6sxoqBD/+myuTPWNN6LDiKrsX4+YTVptvT9gU5pBhrntA3 1Qsg== X-Gm-Message-State: AFqh2krXbY8DgILVUWqarm/OISV+VEqQ6gVc5xOPaBI+qCexZtQHglPa XPIiPKs0h+MBdqX5ihlcsP2Tf1ggrRfySEGO2tSAoA== X-Google-Smtp-Source: AMrXdXuysloz9vrHn2UXJqYC8vrizUYsw18sf4LEybqxDbf32L5omhHK4yRO0E2Krl1z4AII4KGSvzly/0dZIcjlJHg= X-Received: by 2002:a25:105:0:b0:801:33b9:d9ac with SMTP id 5-20020a250105000000b0080133b9d9acmr4614810ybb.2.1675188101952; Tue, 31 Jan 2023 10:01:41 -0800 (PST) MIME-Version: 1.0 References: <20230125213857.824959-1-vipinsh@google.com> In-Reply-To: From: Vipin Sharma Date: Tue, 31 Jan 2023 10:01:05 -0800 Message-ID: Subject: Re: [Patch] KVM: x86/mmu: Make optimized __handle_changed_spte() for clear dirty log To: David Matlack Cc: Sean Christopherson , Ben Gardon , pbonzini@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 31, 2023 at 9:41 AM David Matlack wrote: > > On Mon, Jan 30, 2023 at 3:49 PM Sean Christopherson wrote: > > > > On Mon, Jan 30, 2023, David Matlack wrote: > > > On Wed, Jan 25, 2023 at 5:49 PM Sean Christopherson wrote: > > > [...] > > > > --- > > > > arch/x86/kvm/mmu/tdp_mmu.c | 92 ++++++++++---------------------------- > > > > 1 file changed, 24 insertions(+), 68 deletions(-) > > > > > > > > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > > > > index bba33aea0fb0..2f78ca43a276 100644 > > > > --- a/arch/x86/kvm/mmu/tdp_mmu.c > > > > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > > > [...] > > > > @@ -1289,8 +1244,7 @@ static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter, > > > > new_spte = mark_spte_for_access_track(new_spte); > > > > } > > > > > > > > - tdp_mmu_set_spte_no_acc_track(kvm, iter, new_spte); > > > > - > > > > + kvm_tdp_mmu_write_spte(iter->sptep, iter->old_spte, new_spte, iter->level); > > > > > > This can race with fast_page_fault() setting the W-bit and the CPU > > > setting the D-bit. i.e. This call to kvm_tdp_mmu_write_spte() could > > > clear the W-bit or D-bit. > > > > Ugh, right. Hrm. Duh, I just didn't go far enough. A straight XCHG is silly. > > Except for the access-tracking mess, KVM wants to clear a single bit. Writing > > the entire thing and potentially clobbering bits is wasteful and unnecessarily > > dangerous. And the access-tracking code already needs special handling. > > > > We can just simplify this all by adding a helper to clear a single bit (and > > maybe even use clear_bit() and __clear_bit() if we save the bit number for the > > W/A/D bits and not just the mask). And if it weren't for EPT (different A/D > > locations), we could even use static asserts to restrict the usage to the W/A/D > > bits :-/ Oh well. > > > > E.g. this > > This patch looks good. Vipin can you incorporate this in your next version? On it.