Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1980761rda; Tue, 24 Oct 2023 08:49:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDVc13WoGRvcdTM6AYVDH/rt52DMLbwjfTUGD/2w1C+YSnRnXuMaLZ/4a5oPAsZYy+12nx X-Received: by 2002:a05:6a21:18f:b0:17b:129b:196f with SMTP id le15-20020a056a21018f00b0017b129b196fmr3761862pzb.11.1698162581062; Tue, 24 Oct 2023 08:49:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698162581; cv=none; d=google.com; s=arc-20160816; b=Y5jUDuOOOFR6F5s95bRq7A1RTUjoJ9jfpRYZuY9+tPRpJWAC0sxmZeNDFGcQUO6SYW ipw2v00S43isHOhia9QwJP63YfNBuyFFBDTaXOeXiAT7GGBSJ1+GXVTQGo3avcjnVQP/ KLMV1a75JjVcvYOwsujRoanGhNOMm8pGMihtafIQSj1wBSIh7qFj2Fa8SLDZ98PqHuIn lTvo25DfAJztJrdBeXpR4JvePYPGn8sh7OSdAkWV+mK+uWkBN4SD4IDdbAd0zA8E4mjO kubyhD+xWq5KatD2CBtnulMsmhMjQt41nzmsAtgO/8+52vgWJbBgd8tu2tlAG+2SlRnX 4/aQ== 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=G80d6SYNl+i6LLR4j1qPSwa+o2+/ZMsAHEKXJ7GB4aE=; fh=eGVOtDT2nDetRjxA+mQ/VvmBMAa3M+EcxJYmy6wni+c=; b=WofWbbKjzjrj/0/Fg04PY39tuI9aXGyXxx/uxDNnYuXw3RYwwqABWDlUy4KqtPjvAS Gj2hFKUxW/z9dcgiJfwmDsGXc0ujNzoGPaGi6YUxjqtzgyrk6xr3hhXFHZfC8B5CvpGu QCDatKsBvniDQBLIh47wcbn4vNUipLzMBfzTicn1Xj+iE+DaF/LWbk9ETJOM7zPw5hzv HvY6kdXZU+fuR/OTSbEG3FtGSnCQzITbMKVzNikJmMDZr4UIqVu7ZwWA8Kw53AS+2ERP kxLrN9FvPB8ax0cU6S4wNi5jSOhM67tH+T8F/AO862SsYV7PymLH8PSq6O5F4+Tc4XIQ R59Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=b3kjausx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id l133-20020a633e8b000000b00565e7a3342dsi8894671pga.256.2023.10.24.08.49.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 08:49:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=b3kjausx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id B550B80BB5E0; Tue, 24 Oct 2023 08:49:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343942AbjJXPta (ORCPT + 99 others); Tue, 24 Oct 2023 11:49:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234724AbjJXPt2 (ORCPT ); Tue, 24 Oct 2023 11:49:28 -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 61803C2 for ; Tue, 24 Oct 2023 08:49:26 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1c9cce40f7eso38798685ad.3 for ; Tue, 24 Oct 2023 08:49:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698162566; x=1698767366; 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=G80d6SYNl+i6LLR4j1qPSwa+o2+/ZMsAHEKXJ7GB4aE=; b=b3kjausxWoRgpyBk1nGj26eczvmNxVsJavTGXE5lQezJqNWoQ2BxoNG12rRUB3nTom pN1HmxTbWY7RmK3OsJluezg1IlO162t9JmgENIdFSvKxBF3M9J52tZfuBLNIGGef55qK qGcKmYq/ZY8+xlnF/J49KxUU6nXE684qB9G6T1wuE2+QQgMENpVxkuD4UeFo5k84NTT8 o9vJiKborwoR03oYnZPsKKKA9x1oVCNev3Ifs50n5M5I3pyJeXyL9XPTYU7P4+vrhWgh nRpqLXk/Q35BryIv8uIPBT3ZLPo1dMkds0y28Wg8RiwyA/40QG7BMLLJE+T1y/2+fVNh iUKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698162566; x=1698767366; 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=G80d6SYNl+i6LLR4j1qPSwa+o2+/ZMsAHEKXJ7GB4aE=; b=Ae+ppl7PVRBuy82RdH+X8jD0/hvXo1o6YddeK6Durtbn/qgAD/5uKZlbj/sXu0n2ag WQ7S/l0D9bw+n4GnWjumHKOy4DcH4SP9Euv8STznBVEOr9w4dGOIL6oQQgsj8sUm4OvQ slKLFQ+ko0UScww66t3M5NNp+8/cmFVMop1higXF/yfu0KITZHgJksoV5lINZT8MgXEr rzJ8a60kpT0yVKADOyt6SoKAKgWsUmjn3T7JQIvMZcTPm6xb1A+qtYtVzJIIxcO5Hge1 W+3AYqklVf7n08kshRMCQxurDG5XGrKVwK3AvoH9CsDEvwUH3Dx5yX+4qv9PHLXLtbZo AW8g== X-Gm-Message-State: AOJu0YwecLYwlmRtveXOvsWi4MoFnMEwEOHiNay7dng6V8pvS1D7Qoq5 pqbq7FHPtVZlkSqTyoIH8Y/iPnQJJIw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:a3cd:b0:1cb:e677:2f08 with SMTP id q13-20020a170902a3cd00b001cbe6772f08mr39702plb.10.1698162565810; Tue, 24 Oct 2023 08:49:25 -0700 (PDT) Date: Tue, 24 Oct 2023 08:49:24 -0700 In-Reply-To: Mime-Version: 1.0 References: <20231018204624.1905300-1-seanjc@google.com> <20231018204624.1905300-3-seanjc@google.com> Message-ID: Subject: Re: [PATCH 2/3] KVM: Always flush async #PF workqueue when vCPU is being destroyed From: Sean Christopherson To: Xu Yilun Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Al Viro , David Matlack Content-Type: text/plain; charset="us-ascii" 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 morse.vger.email 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 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 08:49:38 -0700 (PDT) On Tue, Oct 24, 2023, Xu Yilun wrote: > On Wed, Oct 18, 2023 at 01:46:23PM -0700, Sean Christopherson wrote: > > @@ -126,7 +124,19 @@ void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu) > > list_first_entry(&vcpu->async_pf.done, > > typeof(*work), link); > > list_del(&work->link); > > + > > + spin_unlock(&vcpu->async_pf.lock); > > + > > + /* > > + * The async #PF is "done", but KVM must wait for the work item > > + * itself, i.e. async_pf_execute(), to run to completion. If > > + * KVM is a module, KVM must ensure *no* code owned by the KVM > > + * (the module) can be run after the last call to module_put(), > > + * i.e. after the last reference to the last vCPU's file is put. > > + */ > > + flush_work(&work->work); > > I see the flush_work() is inside the check: > > while (!list_empty(&vcpu->async_pf.done)) > > Is it possible all async_pf are already completed but the work item, > i.e. async_pf_execute, is not completed before this check? That the > work is scheduled out after kvm_arch_async_page_present_queued() and > all APF_READY requests have been handled. In this case the work > synchronization will be skipped... Good gravy. Yes, I assumed KVM wouldn't be so crazy to delete the work before it completed, but I obviously didn't see this comment in async_pf_execute(): /* * apf may be freed by kvm_check_async_pf_completion() after * this point */ The most straightforward fix I see is to also flush the work in kvm_check_async_pf_completion(), and then delete the comment. The downside is that there's a small chance a vCPU could be delayed waiting for the work to complete, but that's a very, very small chance, and likely a very small delay. kvm_arch_async_page_present_queued() unconditionaly makes a new request, i.e. will effectively delay entering the guest, so the remaining work is really just: trace_kvm_async_pf_completed(addr, cr2_or_gpa); __kvm_vcpu_wake_up(vcpu); mmput(mm); Since mmput() can't drop the last reference to the page tables if the vCPU is still alive. I think I'll spin off the async #PF fix to a separate series. There's are other tangetially related cleanups that can be done, e.g. there's no reason to pin the page tables while work is queued, async_pf_execute() can do mmget_not_zero() and then bail if the process is dying. Then there's no need to do mmput() when canceling work.