Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3982014rdb; Thu, 14 Sep 2023 08:23:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF00uegXvADBjIaFGzDTkGGWBpMTCkjQsCI7nD/ZsnVgkefqoX3LEWQV96hzInXK7noGW3S X-Received: by 2002:a05:6a00:cca:b0:68a:54e5:24e6 with SMTP id b10-20020a056a000cca00b0068a54e524e6mr6729918pfv.8.1694705034424; Thu, 14 Sep 2023 08:23:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694705034; cv=none; d=google.com; s=arc-20160816; b=aA5HPpIiX0Cgs/Q1kdoRHnSCYgIs5rdm2AiiyUe1N5iBukAbDne4/ti/gmSLD6lbbx XETCorA4CkHckL/5MeEvUj9R73O0rnox7Hl2j1H7OzpRZTkLJqzrPnGFUrMa87Kp6D9f iYOm/ifxLX9qmw40xkQmgL1z6jBa9dqAsvn8DtJp9BL4Hu+bzXHR36YGzc7pM3alUP0L KayBhqOmqC3JkWscrz+mfHqKsuLjPNWm4JPmyituOvs0WzjaGgiDzL7RqVd3M2asxU5Z B9Ibd2cFDpxmxI6Q1qwUdlNfiVu2D6VVPhRE1/EqP0+hVn6U0KZXFFBMS3ovxQRF95UW WC2w== 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=OinN3M2z4I/Qg8aCda0iI1MRdyz332KlEPPYu5muKwI=; fh=gompYcYfbN0KWK4TxoyMUfNkLaTErm7K8MqCnBQWTQQ=; b=vVXvo03vPykratXT16RZdNBmF93kFvHe3PF1OZquT2x5IqA1zubrcs0FtAuimJ45Sk c89AZytKU9UgVqjqugEBgFcHDnY6eFK8641aFr6+z90v8RyEt8WCfwkoW+ulZb6gnvIH gXRYXLmiVR6BTNxnx/qDBAWAkgjk4BHT9ENOYDeE1X56WPaRFiZgO/VQZCvs1CUJj/p9 U4+Ul9S7wpF3ANAL8f0F9xA5vFf21ydwMoD/925ovs3yO/Sb+7rUKAduS3m6Aao/9xKR 0UlSxR0x1Vw00ooiNvAWTipMq7hIaARzdTGk4mJJ0mmx9HpMAfci9LWs/rjT3R56BU0X omcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xr88NZ3h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id n45-20020a056a000d6d00b0068fdc320489si1675636pfv.315.2023.09.14.08.23.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 08:23:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xr88NZ3h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id F060E80732B8; Thu, 14 Sep 2023 07:20:12 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239984AbjINOUE (ORCPT + 99 others); Thu, 14 Sep 2023 10:20:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239945AbjINOUD (ORCPT ); Thu, 14 Sep 2023 10:20:03 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE7D8DF for ; Thu, 14 Sep 2023 07:19:59 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d818e01823aso1038494276.2 for ; Thu, 14 Sep 2023 07:19:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694701199; x=1695305999; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OinN3M2z4I/Qg8aCda0iI1MRdyz332KlEPPYu5muKwI=; b=xr88NZ3hWTH3lnvGOdBo9NgKqtBG+RfRf+Ma5OOyyYFnq+jFzL65M+V4WoaB9eRMHm 971zh2UCBLYdP76uV+Mq30FSgo6lprEyIAHWM6SBOSR9aKHFIc5lP5hALT7lI7DW6hc3 2tZv6v0YmnnxQkLA7ALzjmjCaSIA/hUFh0usopy9109tuTlYRCtAakmcXQ/bUatDDHbU h5CBK2/vAkKySfz1KsqCjlBwoLMQf8sHXlk0K2SGK3ckc7R6E0h0vvWF8riWY0TWrINA Zq48Rcr1pRGc9ZQz2lqK1ZZMhdG35Thg67A8WQXciGYHbeYiyMYi5tFD6yK2xNXXqIYv i2IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694701199; x=1695305999; 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=OinN3M2z4I/Qg8aCda0iI1MRdyz332KlEPPYu5muKwI=; b=HWktG6OD7kHYnaxm2/2cnpUZUvRg7UMqAsevM64LMxy17VY8ET8WFfsiM5nVC6ABfm 8oALACHgKhfU31aCBKSWxZu27WfTlC3xQMGaCbq0JI71BDhZN4yI1wJOAfKr2OoV5DEJ Z7iz0JC7SO0GhEqFp1QISyawAH+8/5nwKbYSw2Dg1to82h1CXtqKt9AB2W3IFhQYxe4r zVqtVOXvxvWrcEeWSjhAkblpKvhXGA0ccf8tJAB4JeZW999EJ8+z5rGghyAmlWEi10dL Ta0KpU4UyiY8vZMhSEfNsV0Qg3r3Mwg7PB2mqa4F7e/zgEqO315jA0CiA1FiorQuH3L9 3W3g== X-Gm-Message-State: AOJu0YwUPT57hNm0AQo8MaB52YXMqmAKkWTh/RV9yoFu+Eqym1ca1Pd8 JZoMLeQSctxIBvz1gsTQtoijJWqPby0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:e6c4:0:b0:d78:f45:d7bd with SMTP id d187-20020a25e6c4000000b00d780f45d7bdmr129688ybh.4.1694701199053; Thu, 14 Sep 2023 07:19:59 -0700 (PDT) Date: Thu, 14 Sep 2023 07:19:57 -0700 In-Reply-To: <54d3e6bf-d374-caa5-0920-bb2fe3b7595c@linux.intel.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> <20230914015531.1419405-3-seanjc@google.com> <54d3e6bf-d374-caa5-0920-bb2fe3b7595c@linux.intel.com> Message-ID: Subject: Re: [RFC PATCH v12 02/33] KVM: Use gfn instead of hva for mmu_notifier_retry From: Sean Christopherson To: Binbin Wu Cc: Chao Peng , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Content-Type: text/plain; charset="us-ascii" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 14 Sep 2023 07:20:13 -0700 (PDT) X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,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 groat.vger.email On Thu, Sep 14, 2023, Binbin Wu wrote: > > On 9/14/2023 9:55 AM, Sean Christopherson wrote: > > +void kvm_mmu_invalidate_end(struct kvm *kvm) > > { > > /* > > * This sequence increase will notify the kvm page fault that > > @@ -833,6 +848,13 @@ void kvm_mmu_invalidate_end(struct kvm *kvm, unsigned long start, > > * in conjunction with the smp_rmb in mmu_invalidate_retry(). > > */ > > kvm->mmu_invalidate_in_progress--; > > + > > + /* > > + * Assert that at least one range must be added between start() and > > + * end(). Not adding a range isn't fatal, but it is a KVM bug. > > + */ > > + WARN_ON_ONCE(kvm->mmu_invalidate_in_progress && > > + kvm->mmu_invalidate_range_start == INVALID_GPA); > Should the check happen before the decrease of kvm->mmu_invalidate_in_progress? > Otherwise, KVM calls kvm_mmu_invalidate_begin(), then kvm_mmu_invalidate_end() > the check will not take effect. Indeed. I'm pretty sure I added this code, not sure what I was thinking. There's no reason to check mmu_invalidate_in_progress, it's not like KVM allows mmu_invalidate_in_progress to go negative. The comment is also a bit funky. I'll post a fixup patch to make it look like this (assuming I'm not forgetting a subtle reason for guarding the check with the in-progress flag): /* * Assert that at least one range was added between start() and end(). * Not adding a range isn't fatal, but it is a KVM bug. */ WARN_ON_ONCE(kvm->mmu_invalidate_range_start == INVALID_GPA); Regarding kvm->mmu_invalidate_in_progress, this would be a good opportunity to move the BUG_ON() into the common end(), e.g. as is, an end() without a start() from something other than the generic mmu_notifier would go unnoticed. And I _think_ we can replace the BUG_ON() with a KVM_BUG_ON() without putting the kernel at risk. E.g. diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index dd948276e5d6..54480655bcce 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -870,6 +870,7 @@ void kvm_mmu_invalidate_end(struct kvm *kvm) * in conjunction with the smp_rmb in mmu_invalidate_retry(). */ kvm->mmu_invalidate_in_progress--; + KVM_BUG_ON(kvm->mmu_invalidate_in_progress < 0, kvm); /* * Assert that at least one range was added between start() and end(). @@ -905,8 +906,6 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, */ if (wake) rcuwait_wake_up(&kvm->mn_memslots_update_rcuwait); - - BUG_ON(kvm->mmu_invalidate_in_progress < 0); } static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,