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 58679C7EE30 for ; Tue, 23 May 2023 14:19:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237225AbjEWOTs (ORCPT ); Tue, 23 May 2023 10:19:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232929AbjEWOTq (ORCPT ); Tue, 23 May 2023 10:19:46 -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 26D96118 for ; Tue, 23 May 2023 07:19:45 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1afa6afcef4so14972795ad.1 for ; Tue, 23 May 2023 07:19:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684851584; x=1687443584; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=YJTOtttasiWLDEUcufe2rn3c2B61VJIjpPGMO/P/Yds=; b=brwx0PSOMA0wdDk4Q3ZV6Zhimz9MGX1rc3oTrPLZdLDxR99oxRU+kgTiRQZeIUGp/B dyf6U+6YZAZ0UTzApeHQKfgEnBhOiOJ/qXVvQ9J1F3RjGUiuExrD9+BmRrRlbu21nlVD Wx38qijldoSLQu16rAmMxw27fIDqOXmDcc7YNTYCcS5xe37Q9LSgSZUY1x8lcrA4BzR8 Gn6XlWuegWj0mDFi1OGEJ901Pjqo4OoenFzAOsc9erWa0V37gRvkLtOzMhqrjLYhuzgg EJnTJnwVBYHzHNPBMQ/b1Fhyc00bJU+3iH49CfEJT1yRwLmSMHit03ZKsytirD36EFwd gllQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684851584; x=1687443584; 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=YJTOtttasiWLDEUcufe2rn3c2B61VJIjpPGMO/P/Yds=; b=ECBe5ju552My/WWiGRyEarMHGLZhj+lHcfnrAYFQjvpU8aCNboSlI8CYeEfhdcCjiB QNR8pwE2FCCbRxLm458v5C4lsnpSE+nBjDJXt2QB1Jr0Z6y+rVhodhc80X5I3y6PAru6 msS5CF7j4Jseg3lMxZg/PwD3Z4Mg+93RKFAubkQ8SLjV73p9k3IEXzqyDR+M6+SlrzPe ErRC0BX9xF8N4VDOFMclEMdfJ84wh+LpoKWTYdH6PkfpeUit7ruau45FTXbznPu1jwKH Dgq0LOmEVBC2cMPpyMOUdTQzzXT4SAjOJ4AZsAJP/9T7Aggw9oFMuVMTLERt6rVF9pcZ EtDw== X-Gm-Message-State: AC+VfDyoh5w6WqGAIey2tmwyJkG+KUonsMJFIFluyalbCbsxaWuBOUw/ HBxcB/SgnfePu2zERqGH/gWCIrEhCJg= X-Google-Smtp-Source: ACHHUZ7XDfHM+pb9CMgdhxQCTf7TNLGglKW4K4aM789UbMGnxCZDh8BKaFM+lAod2VMVzlBmEBluGi8tvqw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:eb11:b0:1a1:b318:2776 with SMTP id l17-20020a170902eb1100b001a1b3182776mr3427854plb.0.1684851584603; Tue, 23 May 2023 07:19:44 -0700 (PDT) Date: Tue, 23 May 2023 07:19:43 -0700 In-Reply-To: Mime-Version: 1.0 References: <20220706082016.2603916-1-chao.p.peng@linux.intel.com> <20220706082016.2603916-9-chao.p.peng@linux.intel.com> Message-ID: Subject: Re: [PATCH v7 08/14] KVM: Rename mmu_notifier_* From: Sean Christopherson To: Kautuk Consul Cc: Chao Peng , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, qemu-devel@nongnu.org, linux-kselftest@vger.kernel.org, Paolo Bonzini , Jonathan Corbet , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H . Peter Anvin" , Hugh Dickins , Jeff Layton , "J . Bruce Fields" , Andrew Morton , Shuah Khan , Mike Rapoport , Steven Price , "Maciej S . Szmigiero" , Vlastimil Babka , Vishal Annapurve , Yu Zhang , "Kirill A . Shutemov" , luto@kernel.org, jun.nakajima@intel.com, dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com, aarcange@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, Quentin Perret , Michael Roth , mhocko@suse.com, Muchun Song Content-Type: text/plain; charset="us-ascii" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 23, 2023, Kautuk Consul wrote: > On 2022-07-06 16:20:10, Chao Peng wrote: > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > > index e9153b54e2a4..c262ebb168a7 100644 > > --- a/include/linux/kvm_host.h > > +++ b/include/linux/kvm_host.h > > @@ -765,10 +765,10 @@ struct kvm { > > > > #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) > > struct mmu_notifier mmu_notifier; > > - unsigned long mmu_notifier_seq; > > - long mmu_notifier_count; > > - gfn_t mmu_notifier_range_start; > > - gfn_t mmu_notifier_range_end; > > + unsigned long mmu_updating_seq; > > + long mmu_updating_count; > > Can we convert mmu_updating_seq and mmu_updating_count to atomic_t ? Heh, can we? Yes. Should we? No. > I see that not all accesses to these are under the kvm->mmu_lock > spinlock. Ya, working as intended. Ignoring gfn_to_pfn_cache for the moment, all accesses to mmu_invalidate_in_progress (was mmu_notifier_count / mmu_updating_count above) are done under mmu_lock. And for for mmu_notifier_seq (mmu_updating_seq above), all writes and some reads are done under mmu_lock. The only reads that are done outside of mmu_lock are the initial snapshots of the sequence number. gfn_to_pfn_cache uses a different locking scheme, the comments in mmu_notifier_retry_cache() do a good job explaining the ordering. > This will also remove the need for putting separate smp_wmb() and > smp_rmb() memory barriers while accessing these structure members. No, the memory barriers aren't there to provide any kind of atomicity. The barriers exist to ensure that stores and loads to/from the sequence and invalidate in-progress counts are ordered relative to the invalidation (stores to counts) and creation (loads) of SPTEs. Making the counts atomic changes nothing because atomic operations don't guarantee the necessary ordering. E.g. when handling a page fault, KVM snapshots the sequence outside of mmu_lock _before_ touching any state that is involved in resolving the host pfn, e.g. primary MMU state (VMAs, host page tables, etc.). After the page fault task acquires mmu_lock, KVM checks that there are no in-progress invalidations and that the sequence count is the same. This ensures that if there is a concurrent page fault and invalidation event, the page fault task will either acquire mmu_lock and create SPTEs _before_ the invalidation is processed, or the page fault task will observe either an elevated mmu_invalidate_in_progress or a different sequence count, and thus retry the page fault, if the page fault task acquires mmu_lock after the invalidation event.