Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2890724rwb; Mon, 15 Aug 2022 13:25:00 -0700 (PDT) X-Google-Smtp-Source: AA6agR63qtt9nX2mh1z3LA1sIChm+vOy1/Q31u1gfMnE+qf1+yYrDbGkdBPCcN+G67YDDPPFryNf X-Received: by 2002:a17:907:3da7:b0:730:cae5:7f7d with SMTP id he39-20020a1709073da700b00730cae57f7dmr11291476ejc.711.1660595100227; Mon, 15 Aug 2022 13:25:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660595100; cv=none; d=google.com; s=arc-20160816; b=xHPph9hn6KefYIN3CaM9ry1rXfVmGMIgFbMZQB5aQybeCNclZK8PRS3fu6Cjd+hJt9 ncD32c5UvMS2qCZCv5hbRCAO11H5JapcK34BWQuHRsR4JtIV1jjVMPwAO7aJ8B2TBiRS xrs9bKdlsu0is0Dm+Y1a9bJRKhp602ut8JvmPqSoPEH58xUf4yoXNtVr6J5TwoS/LHBZ cleZTKgr1HhmNXkqX5xo7qrLu/D0x5HgpqsNTf6ZxMryjiZZ3GGOrSPyfB03fHZOusv1 kE1pLwV+nSNev1t3PNJQ6rhWm2nZozrXlIrz090v6D/N1STqF+ScjH7IZ/xIzaAK51ed 2zSg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zlc0+4xnQ8hFML+ypaOe04+c7UwVwv/QJszCzyxoPgQ=; b=tZ1pOggodpyXvu/c8HGCIUpIcxEUvlmmdaAqZUtDF2C7H8TiYY5sQ80sVILP3zdftC bFa9hlUbNIZcUg4NrWkd+jpqKLhmXNpKGixrqxjmZQfGZ9ID2Wu135HlDGUeCUbJOyBq yvwF2AlxcZPSpyl3Fnnvdn26BR9omtC22UIqRy3NUCvB3X302Zb826tbCc2Qtr4lNFh0 gYqm3NxZCYIB5Zs1xOYOihITDyC4TCDTpynzrGNh/cH83hVSNhhTiqI6/fsUDR+4QA9P JYw8UjnhzqNIJCOLMxmDFxzSFRVes+dU1L4SMVTX0ZPLljs0NaqkeBX+326KaPHptqSx xFig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="rO/jRrMm"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y5-20020a056402358500b0043c4912e226si9719082edc.568.2022.08.15.13.24.32; Mon, 15 Aug 2022 13:25:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="rO/jRrMm"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346578AbiHOUQM (ORCPT + 99 others); Mon, 15 Aug 2022 16:16:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346016AbiHOUJv (ORCPT ); Mon, 15 Aug 2022 16:09:51 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8D8B631E; Mon, 15 Aug 2022 11:56:24 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 5303FB8105C; Mon, 15 Aug 2022 18:56:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 94DB7C433C1; Mon, 15 Aug 2022 18:56:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660589782; bh=qQyraYE4LbhTbwMvV6lt7Z7x61Pv/yXQ96/5OMaQNNo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rO/jRrMmgFF6FQtbzDSnuLQHlXKfr2dNMaYdKEFoUABta23KXHXjRynW/bz53qw6t mYwvK+pUIPKr8Dv73hvOsJRH3eRar0Bm16izF0Aldk46LNUae0NboQscjAc7L3oy0X cOjJkCJs9gxTyqE6WG3kM+vI6moLubGow3Sca6Fc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lai Jiangshan , Sean Christopherson , Paolo Bonzini Subject: [PATCH 5.18 0029/1095] KVM: Fully serialize gfn=>pfn cache refresh via mutex Date: Mon, 15 Aug 2022 19:50:28 +0200 Message-Id: <20220815180430.572594455@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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: Sean Christopherson commit 93984f19e7bce4c18084a6ef3dacafb155b806ed upstream. Protect gfn=>pfn cache refresh with a mutex to fully serialize refreshes. The refresh logic doesn't protect against - concurrent unmaps, or refreshes with different GPAs (which may or may not happen in practice, for example if a cache is only used under vcpu->mutex; but it's allowed in the code) - a false negative on the memslot generation. If the first refresh sees a stale memslot generation, it will refresh the hva and generation before moving on to the hva=>pfn translation. If it then drops gpc->lock, a different user of the cache can come along, acquire gpc->lock, see that the memslot generation is fresh, and skip the hva=>pfn update due to the userspace address also matching (because it too was updated). The refresh path can already sleep during hva=>pfn resolution, so wrap the refresh with a mutex to ensure that any given refresh runs to completion before other callers can start their refresh. Cc: stable@vger.kernel.org Cc: Lai Jiangshan Signed-off-by: Sean Christopherson Message-Id: <20220429210025.3293691-7-seanjc@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- include/linux/kvm_types.h | 2 ++ virt/kvm/pfncache.c | 12 ++++++++++++ 2 files changed, 14 insertions(+) --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -19,6 +19,7 @@ struct kvm_memslots; enum kvm_mr_change; #include +#include #include #include @@ -69,6 +70,7 @@ struct gfn_to_pfn_cache { struct kvm_vcpu *vcpu; struct list_head list; rwlock_t lock; + struct mutex refresh_lock; void *khva; kvm_pfn_t pfn; enum pfn_cache_usage usage; --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -157,6 +157,13 @@ int kvm_gfn_to_pfn_cache_refresh(struct if (page_offset + len > PAGE_SIZE) return -EINVAL; + /* + * If another task is refreshing the cache, wait for it to complete. + * There is no guarantee that concurrent refreshes will see the same + * gpa, memslots generation, etc..., so they must be fully serialized. + */ + mutex_lock(&gpc->refresh_lock); + write_lock_irq(&gpc->lock); old_pfn = gpc->pfn; @@ -250,6 +257,8 @@ int kvm_gfn_to_pfn_cache_refresh(struct out: write_unlock_irq(&gpc->lock); + mutex_unlock(&gpc->refresh_lock); + gpc_release_pfn_and_khva(kvm, old_pfn, old_khva); return ret; @@ -261,6 +270,7 @@ void kvm_gfn_to_pfn_cache_unmap(struct k void *old_khva; kvm_pfn_t old_pfn; + mutex_lock(&gpc->refresh_lock); write_lock_irq(&gpc->lock); gpc->valid = false; @@ -276,6 +286,7 @@ void kvm_gfn_to_pfn_cache_unmap(struct k gpc->pfn = KVM_PFN_ERR_FAULT; write_unlock_irq(&gpc->lock); + mutex_unlock(&gpc->refresh_lock); gpc_release_pfn_and_khva(kvm, old_pfn, old_khva); } @@ -290,6 +301,7 @@ int kvm_gfn_to_pfn_cache_init(struct kvm if (!gpc->active) { rwlock_init(&gpc->lock); + mutex_init(&gpc->refresh_lock); gpc->khva = NULL; gpc->pfn = KVM_PFN_ERR_FAULT;