Received: by 2002:a05:7412:8d11:b0:fa:4934:9f with SMTP id bj17csp522279rdb; Mon, 15 Jan 2024 05:12:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IGzdqWNq6gjh+IJOfDezSXa5CQf6d7yg/ph/VsRKC5hqJR7wAqiwKrO5FAjKPNd7Pv8Tct+ X-Received: by 2002:a17:906:ee8d:b0:a2a:ef4e:e352 with SMTP id wt13-20020a170906ee8d00b00a2aef4ee352mr2156939ejb.108.1705324325822; Mon, 15 Jan 2024 05:12:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705324325; cv=none; d=google.com; s=arc-20160816; b=UmMjJhgtqEDSlIwHTLlO/sPMparZTuWBg3fQD5ObvZzGOfx/9b5r5IHIIiBvLu2fx0 mrZ+8SsGPuiDaIFO80Wg5zZsATl7yKt/VT6R4sdRrUTp2orgf4fONUUUhVqo7uzDXbwA FYrqL9a5NpV+nu7FZK/ePSQFTRo4kyMSz4QT5sllARM6uMKoGbFRTGU4Kz6jFStwjA8o j+A8vcoCOOr7sdzIVPEKTrznU+o9TbzOFXAUugZzrVOVv2XfuZMAvTUpRMp0cfp7i3VC IGd5bdHgJmCwNcHXYdpI8gypSZb6Q3AFHR1U5f4n4qrJqIOkt1brCMtb/WBC0kdW7lLw HHkg== 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=HpTHih+c+MXqgtTxQrob57yxKxiW8tzgWbbCUyiyjoTdMpNoJ11gA+YzGKkKMYAAiP z5RspAaK9nF0z3AVxrTXQbJxGgOJZXqBYr8Kfju2oTYQ3ZiPH0jRueeANUXEJEXMzIhb jAuJ7jg5dez6CZOUmZt/DWEqIIcW1apDcO27pY5vJqKCpMAmMzz0LKcPLS1VRRMvytb0 4olwdzvFUeSZpjj+2/ZdnuvtUxFtftN7FngF9fROx+BEYSN10hKsJnutvOb+do5CKQ8/ nKEVjoGqVAwdfySytHh00WuKY2Sy+Bd03PX5OOnEjRvz8AEVKLKO1wqiOZ9z1DFu4gnr uz7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=ioaQBr0o; spf=pass (google.com: domain of linux-kernel+bounces-26004-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-26004-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id f1-20020a170906c08100b00a2b28a25dbcsi3733824ejz.292.2024.01.15.05.12.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 05:12:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-26004-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=ioaQBr0o; spf=pass (google.com: domain of linux-kernel+bounces-26004-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-26004-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 912AD1F21C76 for ; Mon, 15 Jan 2024 13:12:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 84A5D17C6E; Mon, 15 Jan 2024 13:09:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xen.org header.i=@xen.org header.b="ioaQBr0o" 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 243D9175B9; Mon, 15 Jan 2024 13:09:46 +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=ioaQBr0o5KTn9Br5QwqGYCUaG3 cA8gKp8XP14OrX09JaP3bWITXuVxtZ4KhlKAPJT0VuhtQoREzz7+C1LEDXb1enNxkELT3/ZO49gsM eATdbbyFhpqDdsNdeYA85Ier3DbXG1O1PgbvHWoiGO1qZrjvReY4VwsonrKvX8HJM6SA=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rPMiY-00035a-Ik; Mon, 15 Jan 2024 13:09:30 +0000 Received: from 54-240-197-231.amazon.com ([54.240.197.231] 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 1rPMXf-0002kM-QU; Mon, 15 Jan 2024 12:58:16 +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 v12 18/20] KVM: pfncache: check the need for invalidation under read lock first Date: Mon, 15 Jan 2024 12:57:05 +0000 Message-Id: <20240115125707.1183-19-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240115125707.1183-1-paul@xen.org> References: <20240115125707.1183-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