Received: by 2002:a05:7412:d008:b0:f9:6acb:47ec with SMTP id bd8csp35613rdb; Tue, 19 Dec 2023 08:42:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IGkxz6qR2EmJudClIHqFr+5yKnAlI3tJ2sEjOipeC9cV9N/1zQWvQoga8/PRLdamGZA6WK1 X-Received: by 2002:ad4:5dca:0:b0:67f:143d:b8ca with SMTP id m10-20020ad45dca000000b0067f143db8camr11163670qvh.44.1703004161757; Tue, 19 Dec 2023 08:42:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703004161; cv=none; d=google.com; s=arc-20160816; b=LGUK0Ric4rZxF0o1QyVe1DXbVim/3GbN+Tq/ShpAo7i6uyGUU6j9OH0lfNnDcR2OLN W2VVGRgEFkbZb0pFrJibO2b3TjKFpMS2TOQgKetCSaqJf+iKKobd+S0cKWsGJ/WcpK6M AMxp854q3yCF7tu+P57O5jBl06ii1Re2yD9hH8Ni/RB40EZRNQiLw8EtodfjlvCpTVlP HcL+sYEsm8EkJDVqpAsYq7b2WB933A74wjFNxAyQCSi8s4dMLeEibKf5lwIa5N/AxTnl eJcRAS+ueIfweZWek3ETQPTfgAp9ZZngjR0Jnyz8bafWBESW94En3cuM3xJBg15GOiB2 qVkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:to:from:dkim-signature; bh=z7zH+v6OnxjwAjiPJoCCBsbBFCz5/p3/vhyVHwUde2s=; fh=aAPqvXm40pY9772UiBOljEePwtz/3Q0zIOpw238tj3M=; b=zjPqoTZS0I6Gj3K8l96dUMAyzw3pgIasWaVuc1qtaC0MOGoGstjpt2JhYhv9tkHsmp KK1m5+xQtLr/V0ouDHeyJ3PPwBPbPxrfjDVybCG4KlL2zYE3mbrI6kGbHZ0B+st8wN/X 2zivZbGeUCwhtHW3awvHQZlQBWmheFAh8pr2Fzt3+HjGaIHwVF/6VBBdjm9kyD88SxWR sp98E2pdXQJR388AVMbi0ZpEparFTkqRNKAHlQjHtyvcFziCW34hF/DUHduDPqd3CDJq pu2len1Jd2V13gj4oVeQsdPgCBrEQI7P/Vq5XMPcUqWJxGUxc1cgOBPIk56wJrY7GhbD kr5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=p+0hw2zs; spf=pass (google.com: domain of linux-kernel+bounces-5578-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5578-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id r12-20020a0ccc0c000000b0067f234b2c59si8904270qvk.422.2023.12.19.08.42.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 08:42:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5578-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=p+0hw2zs; spf=pass (google.com: domain of linux-kernel+bounces-5578-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5578-linux.lists.archive=gmail.com@vger.kernel.org" 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 34F461C24583 for ; Tue, 19 Dec 2023 16:42:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4FA5E20DDC; Tue, 19 Dec 2023 16:41:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xen.org header.i=@xen.org header.b="p+0hw2zs" X-Original-To: linux-kernel@vger.kernel.org Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) (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 4731A358AB; Tue, 19 Dec 2023 16:41:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xen.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:To:From; bh=z7zH+v6OnxjwAjiPJoCCBsbBFCz5/p3/vhyVHwUde2s=; b=p+0hw2zsYJOzIGTveKGHhHTwG3 +fuOn7o4duxP5GOylzP/2h4eiWIGHrlQIr2s0s/tF7In22LN7qFw+yvrera/lZGJkRR6BByhU+d4y 9MCpDA+p7j1WuD2SwFFPRIHwEiX+wEuiWftpnJWOG0mJxl54FO4ziy5iQXCDrLqZNw9s=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rFd9y-000657-BU; Tue, 19 Dec 2023 16:41:34 +0000 Received: from 54-240-197-226.amazon.com ([54.240.197.226] helo=REM-PW02S00X.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rFckR-0005h9-QH; Tue, 19 Dec 2023 16:15:12 +0000 From: Paul Durrant To: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , David Woodhouse , Paul Durrant , Shuah Khan , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v11 18/19] KVM: pfncache: check the need for invalidation under read lock first Date: Tue, 19 Dec 2023 16:11:08 +0000 Message-Id: <20231219161109.1318-19-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231219161109.1318-1-paul@xen.org> References: <20231219161109.1318-1-paul@xen.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Paul Durrant Taking a write lock on a pfncache will be disruptive if the cache is heavily used (which only requires a read lock). Hence, in the MMU notifier callback, take read locks on caches to check for a match; only taking a write lock to actually perform an invalidation (after a another check). Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: Sean Christopherson Cc: Paolo Bonzini Cc: David Woodhouse v10: - New in this version. --- virt/kvm/pfncache.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index ae822bff812f..70394d7c9a38 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -29,14 +29,30 @@ void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, unsigned long start, spin_lock(&kvm->gpc_lock); list_for_each_entry(gpc, &kvm->gpc_list, list) { - write_lock_irq(&gpc->lock); + read_lock_irq(&gpc->lock); /* Only a single page so no need to care about length */ if (gpc->valid && !is_error_noslot_pfn(gpc->pfn) && gpc->uhva >= start && gpc->uhva < end) { - gpc->valid = false; + read_unlock_irq(&gpc->lock); + + /* + * There is a small window here where the cache could + * be modified, and invalidation would no longer be + * necessary. Hence check again whether invalidation + * is still necessary once the write lock has been + * acquired. + */ + + write_lock_irq(&gpc->lock); + if (gpc->valid && !is_error_noslot_pfn(gpc->pfn) && + gpc->uhva >= start && gpc->uhva < end) + gpc->valid = false; + write_unlock_irq(&gpc->lock); + continue; } - write_unlock_irq(&gpc->lock); + + read_unlock_irq(&gpc->lock); } spin_unlock(&kvm->gpc_lock); } -- 2.39.2