Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1239984rdb; Mon, 19 Feb 2024 08:30:05 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWVVkKHH3Lb5objT+Fn/2zObC4/T8nE4o/XWYESvPaYR0Hh3KWEPh0v1ikcyhp4ZUARW0fswDiZEOumId5joluwoiNZFsEC86/s9NfvuA== X-Google-Smtp-Source: AGHT+IE6mGekqPiEpODBdB3rvTVQfOmkRsqUnj43S0Gm/yfCrD0p4GDVB8TrSkrXL+a4Q/FjOGwd X-Received: by 2002:a05:6a00:e:b0:6e2:43cd:ed4c with SMTP id h14-20020a056a00000e00b006e243cded4cmr7150242pfk.3.1708360205159; Mon, 19 Feb 2024 08:30:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708360205; cv=pass; d=google.com; s=arc-20160816; b=jAA/lPDJZWYGHNZTdEtoTEbrlVJaeU8DSQN6AKq807bjE/oOKPYsoISdIWuLgY5F+V Ij55CIJvJIAgouSx2iJjPKCxrl9slhw+5HP6Aq9sYIjtCTvDl8thQzD8v9XmGXckPZgs JL0GerBhtfnaQt59jdeOXPLp8eruryFUowt2BJUE70k0aNT7b+v/rih+8BYL3zTBfA02 nfoxowUNBPGeOw844KqYvpWeOvIEXeNrNZBcT48fHo7NZIKSv/9SBeR4CWehaCj8Uxu7 heDb9+40J32NeDBEiQtpkOTxIQgkDWnbPjUcXLzjwJ74ieMVJo2ZJlC9B1XyPitKv6bm ZMzQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=kV4Xk2huOrp/L+Nm3iKSutzuSqeBJO0qWZtpNnNc3Yg=; fh=975nUjm/5Tvv+TwDZcgMRfki0UMZiSZXCwYjYR8WL5I=; b=clEjOn2FCWAIz1FBPBYjmBUWdjn6COTrMhYT3j8+QCO7RfywmFribCUos4DtE6RiqK FqKbU+9yopfJ7nzqgSIkFAYiGUym3OncRe7ysQ6bzQ6GfMBnCjCsaJFNi0dKwIgDtVDm /PU6zfZT3SJJF+yhOhipxz/Z0W+hb06ZWJEwsY7e9KXkOFemzrSetVD+rwMDdoXhIoPa 3Qq9LijMpsboYH8j67I1ih5b2/saHO7Jqw7/2hCUyaL2ejRPfjqEHUKZOq8yOApGLU1p DpkySj7I9Bn1XMkPUI13rDqCIy+oF44CPeRO+v9BL1yWSDmmSfCV2SzaF4kAEkOh3dTP VIbQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="JKdrVC//"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-71440-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71440-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id ca21-20020a056a00419500b006e1074642eesi4697028pfb.78.2024.02.19.08.30.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 08:30:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-71440-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="JKdrVC//"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-71440-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71440-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 8AA3828296D for ; Mon, 19 Feb 2024 14:03:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9741D37155; Mon, 19 Feb 2024 14:03:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="JKdrVC//" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F86036B09; Mon, 19 Feb 2024 14:03:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708351384; cv=none; b=Xxs8d3O0oz+HQ+C0Ja+lAe5M/+qkLQ6VaDqKybfrfQI4xAivWGN0btMjteH95kW3MBYzkjXAkWsPlZ29MlnlBgoHCBxYu2IPqcPdFDEvRcX+O/Q5nakpDnUJzx4r+Jyqq5PWgvoQ2QPzijjwY7VAbaticHvcuj5NMqqrZNfe/lM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708351384; c=relaxed/simple; bh=wHheWqOg2lnrXFJQJwK4hCwjRP1jqy2jGPo/csvfdrM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=L75m7U5+OT8vGH7SmQcD2cxXoyOmeAIpY8E7zSybXOWCF4wkW0203m7M6eNAfaEGAkGKLKNOCdIJ6fmHLMctOIKKHbcgcvMT3YiaoL4AnPNmRgcIZrA8FmAozIlI5hCHst7i3YJz0M3A5N/krJhgfW63nZhUfFzLBgvS91vw2oE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=JKdrVC//; arc=none smtp.client-ip=198.175.65.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708351383; x=1739887383; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=wHheWqOg2lnrXFJQJwK4hCwjRP1jqy2jGPo/csvfdrM=; b=JKdrVC//GNaIbK1i5uTYZQ4qMuusEE/T/srpkOcRtEzuarH2BAft74Q5 3i2Fvw6XrD4S/UR5A4uYJIgyrb9B0hTUv6VwydVBli3r1XvGceuA+j+zl OzGLJqniqIm+1SNIAm1dPNzO4NpU1Z1xZ+wZ5xOS2pNKvM7XRcpmIQwKU DexXAORBnOlw/A13FmZrcRk3pSkEjY9b2jl4aev2aks5Yh3GceXuGB/nI PVcUFeE+D2SqT1bgh8eIwfvO6J6EK8zOQj2w2Uxc1LtwnkYHKqOoxuWeo t3DVVmwMTJNNrDB8CK1yURIF0W+FSCy6eb3az0RStMC0tbTPQXtuOddcC Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10988"; a="2294745" X-IronPort-AV: E=Sophos;i="6.06,170,1705392000"; d="scan'208";a="2294745" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Feb 2024 06:03:02 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,170,1705392000"; d="scan'208";a="4407006" Received: from yilunxu-optiplex-7050.sh.intel.com (HELO localhost) ([10.239.159.165]) by fmviesa007.fm.intel.com with ESMTP; 19 Feb 2024 06:03:00 -0800 Date: Mon, 19 Feb 2024 21:59:08 +0800 From: Xu Yilun To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack Subject: Re: [PATCH 1/4] KVM: Always flush async #PF workqueue when vCPU is being destroyed Message-ID: References: <20240110011533.503302-1-seanjc@google.com> <20240110011533.503302-2-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240110011533.503302-2-seanjc@google.com> > void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu) > @@ -114,7 +132,6 @@ void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu) > #else > if (cancel_work_sync(&work->work)) { > mmput(work->mm); > - kvm_put_kvm(vcpu->kvm); /* == work->vcpu->kvm */ > kmem_cache_free(async_pf_cache, work); > } > #endif > @@ -126,7 +143,18 @@ 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); > - kmem_cache_free(async_pf_cache, work); > + > + 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. > + */ > + kvm_flush_and_free_async_pf_work(work); I have a new concern when I re-visit this patchset. Form kvm_check_async_pf_completion(), I see async_pf.queue is always a superset of async_pf.done (except wake-all work, which is not within concern). And done work would be skipped from sync (cancel_work_sync()) by: if (!work->vcpu) continue; But now with this patch we also sync done works, how about we just sync all queued work instead. diff --git a/virt/kvm/async_pf.c b/virt/kvm/async_pf.c index e033c79d528e..2268f16a36c2 100644 --- a/virt/kvm/async_pf.c +++ b/virt/kvm/async_pf.c @@ -71,7 +71,6 @@ static void async_pf_execute(struct work_struct *work) spin_lock(&vcpu->async_pf.lock); first = list_empty(&vcpu->async_pf.done); list_add_tail(&apf->link, &vcpu->async_pf.done); - apf->vcpu = NULL; spin_unlock(&vcpu->async_pf.lock); if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first) @@ -101,13 +100,6 @@ void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu) typeof(*work), queue); list_del(&work->queue); - /* - * We know it's present in vcpu->async_pf.done, do - * nothing here. - */ - if (!work->vcpu) - continue; - spin_unlock(&vcpu->async_pf.lock); #ifdef CONFIG_KVM_ASYNC_PF_SYNC flush_work(&work->work); This way we don't have to sync twice for the same purpose, also we could avoid reusing work->vcpu as a "done" flag which confused me a bit. Thanks, Yilun > + spin_lock(&vcpu->async_pf.lock); > } > spin_unlock(&vcpu->async_pf.lock); >