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 94CB0C433FE for ; Fri, 20 May 2022 15:02:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350561AbiETPCK (ORCPT ); Fri, 20 May 2022 11:02:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231347AbiETPCI (ORCPT ); Fri, 20 May 2022 11:02:08 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 184DA4F455 for ; Fri, 20 May 2022 08:02:07 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id t11-20020a17090a6a0b00b001df6f318a8bso11711224pjj.4 for ; Fri, 20 May 2022 08:02:07 -0700 (PDT) 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:content-transfer-encoding:in-reply-to; bh=qZz8FZUKECzNbnFY9lR47szCmiT+m34JVlG5HP+QTn0=; b=DPzty7wOyFBPH7nBmaSCp1zF+Qu7AFgBU08fZ/J6ZV3I9UZOl9K3s2S6vt+V/oqBjY sHUOsi3rrOntar7AKCoxb+FfbEpm/Q+wIRL2ycC7L8K3BRwBxtMd+Md8Qu6Pubhky3VY LbUhHE5/gUHLm+RzAxoWBf//KHBuH5+MXjl225wZ3HiYBUXzuFJBZPZZJz4pRuAvPPUf 2nQoEss9uwYAA/4yNtIWVB4XDC1j/hQ/ovuk3sHMLwTIcH2EGnymMWY0RbNcXhpy44Wa JUM61y34EIspVfWpJCVMC5oZrL9m7SJOGr97rM7irmcuG+X8pUb0R2sSpM6I/mur9cfS CneQ== 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:content-transfer-encoding :in-reply-to; bh=qZz8FZUKECzNbnFY9lR47szCmiT+m34JVlG5HP+QTn0=; b=AAMt/a2BFYNw4dVy90MYFlPnFzammTQT3vGUUag7nnirCB6cnqaNnyc5ElNRZoSgGU HgsDR5w6+kzMvO8PzyZNqfow/NkTJUS2PkSCc08FmAJ+tOYs8e72HNiix3CKL+2qyuGb urXkzF0ayx+gdBGgzUW9APikyvQQ28rZfGg+5crZXQKuYr8pMZDLDAhzABedL/E+ljDP HqaP9KrkPCFbJub7SKNiJe9gc5bK94FsnLVt/55EyvYUjqrdS2DOf9wB9DkZnq+t0Agy iIb7xfS6g0+dgWZMkWe1RZwboKcenm8y7WNiEbSS9RKxCUO7Jh5ws3Jl4siivSVCtCK1 X6yg== X-Gm-Message-State: AOAM531jpBFsVS029sXEbAkGgOx3WLixj4HlWrfE6juNNyQ/EGgBr8YD oUL1ETpLMVvkOf4cgzEBJtLdcw== X-Google-Smtp-Source: ABdhPJwGx7/ziDKYfEwbA7cv/TFh1iHd2mnAZbRvFEIzyRocHqSZvq6rVVrPIugR9QUgSsO/fi3hvw== X-Received: by 2002:a17:90a:4d49:b0:1df:78ca:ae49 with SMTP id l9-20020a17090a4d4900b001df78caae49mr11190224pjh.121.1653058926286; Fri, 20 May 2022 08:02:06 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id p17-20020a170902c71100b0015e8d4eb28fsm5665018plp.217.2022.05.20.08.02.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 May 2022 08:02:05 -0700 (PDT) Date: Fri, 20 May 2022 15:02:02 +0000 From: Sean Christopherson To: Paolo Bonzini Cc: Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Mingwei Zhang , Maxim Levitsky Subject: Re: [PATCH v2 6/8] KVM: Fix multiple races in gfn=>pfn cache refresh Message-ID: References: <20220427014004.1992589-1-seanjc@google.com> <20220427014004.1992589-7-seanjc@google.com> <035a5300-27e1-e212-1ed7-0449e9d20615@redhat.com> <4c617e69-f80a-1ee1-635e-c198cf93187e@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <4c617e69-f80a-1ee1-635e-c198cf93187e@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 20, 2022, Paolo Bonzini wrote: > On 5/20/22 16:49, Paolo Bonzini wrote: > > On 4/27/22 03:40, Sean Christopherson wrote: > > > +         * Wait for mn_active_invalidate_count, not mmu_notifier_count, > > > +         * to go away, as the invalidation in the mmu_notifier event > > > +         * occurs_before_  mmu_notifier_count is elevated. > > > +         * > > > +         * Note, mn_active_invalidate_count can change at any time as > > > +         * it's not protected by gpc->lock.  But, it is guaranteed to > > > +         * be elevated before the mmu_notifier acquires gpc->lock, and > > > +         * isn't dropped until after mmu_notifier_seq is updated.  So, > > > +         * this task may get a false positive of sorts, i.e. see an > > > +         * elevated count and wait even though it's technically safe to > > > +         * proceed (becase the mmu_notifier will invalidate the cache > > > +         *_after_  it's refreshed here), but the cache will never be > > > +         * refreshed with stale data, i.e. won't get false negatives. > > > > I am all for lavish comments, but I think this is even too detailed. > > What about: > > And in fact this should be moved to a separate function. > > diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c > index 50ce7b78b42f..321964ff42e1 100644 > --- a/virt/kvm/pfncache.c > +++ b/virt/kvm/pfncache.c > @@ -112,6 +112,36 @@ static void gpc_release_pfn_and_khva(struct kvm *kvm, kvm_pfn_t pfn, void *khva) > } > } > + > +static inline bool mmu_notifier_retry_cache(struct kvm *kvm, unsigned long mmu_seq) > +{ > + /* > + * mn_active_invalidate_count acts for all intents and purposes > + * like mmu_notifier_count here; but we cannot use the latter > + * because the invalidation in the mmu_notifier event occurs > + * _before_ mmu_notifier_count is elevated. > + * > + * Note, it does not matter that mn_active_invalidate_count > + * is not protected by gpc->lock. It is guaranteed to > + * be elevated before the mmu_notifier acquires gpc->lock, and > + * isn't dropped until after mmu_notifier_seq is updated. > + */ > + if (kvm->mn_active_invalidate_count) > + return true; > + > + /* > + * Ensure mn_active_invalidate_count is read before > + * mmu_notifier_seq. This pairs with the smp_wmb() in > + * mmu_notifier_invalidate_range_end() to guarantee either the > + * old (non-zero) value of mn_active_invalidate_count or the > + * new (incremented) value of mmu_notifier_seq is observed. > + */ > + smp_rmb(); > + if (kvm->mmu_notifier_seq != mmu_seq) > + return true; > + return false; This can be return kvm->mmu_notifier_seq != mmu_seq; Looks good otherwise. It'll probably yield a smaller diff too.