From patchwork Tue Dec 19 16:11:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 181095 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp2071222dyi; Tue, 19 Dec 2023 08:49:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IFaMhIymS8UmCxSTvGqF4Fw087w3+2MkbgEFraBQGOLY9Qw5DT8vwhT/82K6pS7E+VrQcDO X-Received: by 2002:a17:903:24d:b0:1d3:442e:f0a2 with SMTP id j13-20020a170903024d00b001d3442ef0a2mr8436163plh.139.1703004593763; Tue, 19 Dec 2023 08:49:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703004593; cv=none; d=google.com; s=arc-20160816; b=RFsJtB6pcfNwA0PdSvTSYG2aLt4Ppwx96ug6Xkok9usUoeGaPNeaICfijnkV949KKz Io7XyeMq6UnLQHS7r9Tg8VTjjHcnHNvXtIsAHZc9Z85phyX4LxQNP8E7W6YgcnUGJJK8 yt5qOAhgePA+2t9V46PcD4bUsXK0hbox+q8MYh9O2nS6USYpMDwnk98VfmNJoDKcAX6U wCIWCv6SBJ21JZMm2dfwx8RN7gXNWacFmf1VcQR1nbhbaSEhdYSvmc5MClcUb8t6qZBv Xqh/ILgYS+InAqMIrixyFsduD/yWTpljQPj2iD7d73ZaYuh3iSV8j7dzq9dPe+0qIhjT pqFw== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5578-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id b5-20020a170902a9c500b001ce5b8cfe7dsi19799180plr.230.2023.12.19.08.49.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 08:49:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5578-ouuuleilei=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=@xen.org header.s=20200302mail header.b=p+0hw2zs; spf=pass (google.com: domain of linux-kernel+bounces-5578-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5578-ouuuleilei=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B671A28A279 for ; Tue, 19 Dec 2023 16:43:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 88538374C0; Tue, 19 Dec 2023 16:41:51 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785729745241765116 X-GMAIL-MSGID: 1785729745241765116 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); }