Received: by 2002:a05:7412:bc1a:b0:d7:7d3a:4fe2 with SMTP id ki26csp498705rdb; Sat, 19 Aug 2023 10:05:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF7xmo+wJvt9yypBksxAS8KrW+vhDjotSlHD7gFpXhH/YpMv1ve/Zd/JT/6hBRbopTSNABi X-Received: by 2002:a92:c90c:0:b0:345:a3d0:f0d4 with SMTP id t12-20020a92c90c000000b00345a3d0f0d4mr2967863ilp.3.1692464752289; Sat, 19 Aug 2023 10:05:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692464752; cv=none; d=google.com; s=arc-20160816; b=QVsiG00KyPtkkX+BHZrrDRP0oFSbQqGAV8ThnsxoySl418EOz4j0WhVOQ4ULHQP2uZ ufd25RRKIK7AJrcRBBxx72Gyg6uni5Z8cIHtYHhOH9V31EUMyqGbIw0cnPjL/Y8UCPt5 NzSlUln5qJk+Ui2bQ+AM8idmU+Z4dqyPmipHQUjNf0xX57u4qOsApiDy9yViqekCDpzX NLbIXV19VDPcIWqDKGH4OHuEy3wuJ1vTutywPRJKA1PqqkN4ww6PBViidFWlVn6KfKG8 QBupwn9o+WYuI2ayMF/WhbgcWtJw1Rf2vDFN8d3Ge+ttmgxYsKu7/qI6huVq9cJJ/O1j ZpoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9vZ9EkczMUQHUI00OPclFNxnghSqdUkMpBjBIB2myX0=; fh=nfQzfXi1DrvQ1gw1y6AeNruXWcD1QXK/7wCHIGz2V+k=; b=hGfdsWVWaKFEdmbC26poADiqSICFoeTkkOpTclfiaeqFi5odrnkicGTrUfkvI2pl1V NTQnXKxUjpIwHCTu17o2auQOcKVG3UIRDCxJcEWJ18ZJ7rmwJ6Fs3QXhNJQte0pfUGRn PTUpTKauiOiRvsGcD3uRMvUM3AKDUOk/zTMirZ9f9UOkeWzqWT33rLmL5K9snONM3WqT /Dt1c+YsK5lWEFy/9WWVbwS7O49wD9wQeUfJ8SxPGoas5aUypd2Hx1UUUk3ZRAvhH5+5 dfLx3CU1e6KQwQdYyk2NXH1+9Q6Vw6sWxZqt/hAlR4dkcvc+cmZ3FP3qKx8xC6eJy6U9 D7lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KZA55ttx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id p19-20020a639513000000b005538e9fb8cbsi3654686pgd.550.2023.08.19.10.05.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Aug 2023 10:05:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KZA55ttx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4D0E0127B4C; Sat, 19 Aug 2023 01:53:56 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238775AbjHORTq (ORCPT + 99 others); Tue, 15 Aug 2023 13:19:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238707AbjHORTN (ORCPT ); Tue, 15 Aug 2023 13:19:13 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0517B1BCD; Tue, 15 Aug 2023 10:19:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1692119951; x=1723655951; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IOB6egI1P24F5biT3CsDzSkUO+xJDoBD1NOa36eza/w=; b=KZA55ttxImdfat01Tx3P5+QAA6gpaQlO4e6dXN0lRmWXdZGJi5BflaIX x5aBEoyn3fWUi9O5D+tfS23Kv/g/0Q0Zho9GaCPJYmNIgIytMl83BloUl 3YxzYBvFAcLV5wQHQZCbuR3pWAtLR/21tMq1p8NhsA3EHp2Qm4xiL4MSj gYoy6o3dE4Ifv9S7CRxxJ5KK/j0hMXlWIYfxSSeimQVO9IA6iKw30zBG4 Z9GJxjCTXDAHJqYLUlF3wM1oGcTxEzFtCaueLI/jjwh8JOGJdj/OH1q0p d9Srq/woilkkJz0XUDh2VZTopE5t3K3IjnJ9Gytwo9F4FbKIHcZsWNAvc w==; X-IronPort-AV: E=McAfee;i="6600,9927,10803"; a="436229847" X-IronPort-AV: E=Sophos;i="6.01,175,1684825200"; d="scan'208";a="436229847" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Aug 2023 10:19:09 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10803"; a="907693407" X-IronPort-AV: E=Sophos;i="6.01,175,1684825200"; d="scan'208";a="907693407" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Aug 2023 10:19:07 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Michael Roth , Paolo Bonzini , Sean Christopherson , erdemaktas@google.com, Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, linux-coco@lists.linux.dev, Chao Peng , Ackerley Tng , Vishal Annapurve , Yuan Yao , Jarkko Sakkinen , Xu Yilun , Quentin Perret , wei.w.wang@intel.com, Fuad Tabba Subject: [PATCH 7/8] KVM: gmem: Avoid race with kvm_gmem_release and mmu notifier Date: Tue, 15 Aug 2023 10:18:54 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Isaku Yamahata Add slots_lock around kvm_flush_shadow_all(). kvm_gmem_release() via fput() and kvm_mmu_notifier_release() via mmput() can be called simultaneously on process exit because vhost, /dev/vhost_{net, vsock}, can delay the call to release mmu_notifier, kvm_mmu_notifier_release() by its kernel thread. Vhost uses get_task_mm() and mmput() for the kernel thread to access process memory. mmput() can defer after closing the file. kvm_flush_shadow_all() and kvm_gmem_release() can be called simultaneously. With TDX KVM, HKID releasing by kvm_flush_shadow_all() and private memory releasing by kvm_gmem_release() can race. Add slots_lock to kvm_mmu_notifier_release(). Signed-off-by: Isaku Yamahata --- virt/kvm/kvm_main.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 49380cd62367..4855d0b7a859 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -927,9 +927,16 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn, struct kvm *kvm = mmu_notifier_to_kvm(mn); int idx; + /* + * Avoide race with kvm_gmem_release(). + * This function is called via mmu notifier, mmu_release(). + * kvm_gmem_release() is called via fput() on process exit. + */ + mutex_lock(&kvm->slots_lock); idx = srcu_read_lock(&kvm->srcu); kvm_flush_shadow_all(kvm); srcu_read_unlock(&kvm->srcu, idx); + mutex_unlock(&kvm->slots_lock); } static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { -- 2.25.1