From patchwork Tue Sep 10 16:30:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13798887 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1DBC9ECE58A for ; Tue, 10 Sep 2024 16:31:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BBEF08D0092; Tue, 10 Sep 2024 12:31:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B6D438D0002; Tue, 10 Sep 2024 12:31:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9BEBD8D0092; Tue, 10 Sep 2024 12:31:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 783828D0002 for ; Tue, 10 Sep 2024 12:31:37 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3F5A2120808 for ; Tue, 10 Sep 2024 16:31:37 +0000 (UTC) X-FDA: 82549369434.17.3A6E152 Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by imf27.hostedemail.com (Postfix) with ESMTP id 26DB74000C for ; Tue, 10 Sep 2024 16:31:34 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=VecqFMsI; spf=pass (imf27.hostedemail.com: domain of "prvs=976277991=roypat@amazon.co.uk" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=976277991=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725985843; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=umyhObMpA1DDGyKFrbGuBdzuz/jHr6ecIUxm4GlHskc=; b=s5emUQ6++3zc/8vycMiAI4gYC80QQjb59tJ3Esueoa92NHNd0B00cHdTOqFYJbLtX5ls2K Y58apJa68xMPY7bygQvi3VtzRef7sXJxS+cRqSvOjSAYpbWzZF6Sf9cxIiubwD3iopg9lu wBh2pKK/q7fXisonK/BJ3h/9Iwk6uQQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=VecqFMsI; spf=pass (imf27.hostedemail.com: domain of "prvs=976277991=roypat@amazon.co.uk" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=976277991=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725985843; a=rsa-sha256; cv=none; b=afmsHILZXkq6bryR32eDkhr8Fbj5hMYzF/l50c6LFukYhGmqkbGTgDcdqbnTxbrgQtM+5c MJT9B2iJh/U2qm4fimbAmlNoVvfe1pgt9tJLLt16QWwU0jAY1rpyRrGREu/hmDvbmnRonm 9SUlk0OFOjH/DOxxEmUoppNh8Y5AYws= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1725985896; x=1757521896; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=umyhObMpA1DDGyKFrbGuBdzuz/jHr6ecIUxm4GlHskc=; b=VecqFMsIrjtbDd9gCVZOgzOrOfVReEC0uhPtNcpJcwHK8H6vmzyfP/j7 26AibA83aN/5MXzOSyt/hs2LtnhKgl+Xux5W3mlZWsL61mpphkXzyxCQJ xHnoUE/WtZr0UO4vISDoQkp631+Zh+fa9wo7Uh8igzfT5cw4IeeO0i9FS A=; X-IronPort-AV: E=Sophos;i="6.10,217,1719878400"; d="scan'208";a="367280463" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-33001.sea14.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Sep 2024 16:31:33 +0000 Received: from EX19MTAUEC001.ant.amazon.com [10.0.44.209:40383] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.48.28:2525] with esmtp (Farcaster) id f846bd55-b4c7-479e-9859-a7c32f90264a; Tue, 10 Sep 2024 16:31:31 +0000 (UTC) X-Farcaster-Flow-ID: f846bd55-b4c7-479e-9859-a7c32f90264a Received: from EX19D008UEA004.ant.amazon.com (10.252.134.191) by EX19MTAUEC001.ant.amazon.com (10.252.135.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 10 Sep 2024 16:31:25 +0000 Received: from EX19MTAUWB001.ant.amazon.com (10.250.64.248) by EX19D008UEA004.ant.amazon.com (10.252.134.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 10 Sep 2024 16:31:25 +0000 Received: from ua2d7e1a6107c5b.home (172.19.88.180) by mail-relay.amazon.com (10.250.64.254) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Tue, 10 Sep 2024 16:31:21 +0000 From: Patrick Roy To: , , , , , , , , , , , , , , , , , , , , CC: Patrick Roy , , , , , Subject: [RFC PATCH v2 07/10] kvm: pfncache: invalidate when memory attributes change Date: Tue, 10 Sep 2024 17:30:33 +0100 Message-ID: <20240910163038.1298452-8-roypat@amazon.co.uk> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240910163038.1298452-1-roypat@amazon.co.uk> References: <20240910163038.1298452-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 26DB74000C X-Stat-Signature: bfx7jhzeqbcsysbeppnghxz3fzyofter X-HE-Tag: 1725985894-282260 X-HE-Meta: U2FsdGVkX19BZ2jbZ/tq7N5jx3yjIrtlicAtRD3zKeBRl8b1reL43RQXnoC4UWb2kFWq1CPbGjSQpXVre3HOFdaRL5x3dWc/2aKsnuHSJ/5K0nJW4UTiHf0dzkRuSgXvaa51iKoJFl+8iQOrDesC1OD8v0/OYUMK8CG08kV5KfoY6WpP/zEt3EeAhRnGXpIIt3XnrDG5S5XKZWw3pbCvyqhW22fDtnsNl2kGcFazDIa6cYlPvDhop4e0FwENnhV6xjw68KVctkDDjSLs1ekBIvP9n2bI1dSG6VzpbjhpX9lamIYYKwXkdS4JHbhYLSrAiiLiLzoRYlgSVY2WCOxdv//JqP1xnMdy+LtR1Rmo4CdDET9IQqbI3kVlSSp0T3ToRFtr5s5o0DWkT9Kgbb/dquAZRVHVG0UphsdhWaBnncKmOI5noVbZdeqe4sF0e0J7wuShNkYvG2+DTms6Z1oyM7IsvWEYoAPtFSuC5zbM+mk27LxZiyzCUJ2QcmQlavxBsP+fP9UDwSCJBLOFEbqz2xqhMpVTDvdDXCeb91/2syFE8ZXlG/L+/6ZP3FNVoAduLE7+2Vazm0hQKZXQ+W0+7XWfwd2BDij4iHmpiLCbBQ1+blrHaVa4SKL1ph61qp1spoMP1zxxxFM+ARrqE8kzNuvDG257vfjTY94DsQ4w5jmooQj1EF+QhGt2qChQvG3qs0KrRgYaVEwWpq0lu0H1sIN58aj7YEwyGcG6iWJuCssOj+s9wd3ZtTvIiaL7hb1eopEVPl+aV7vFBD/kYyyMB4eiDVx+aTP1TCK9/qCika+0kSH2UNCY4bFitFNv18VGh1Bw/1RGkmP/td1jIQ8yYmUeKxFQXRGkHEcH4yetXDSSPg35VyuPP/VGTwgFkjWDofE6iQShkEWk5QJtm8+5Z9lbuAYg112nL8hpoOEd3B3fdC15ajYKPpMauqvGRFTUo/QjWeVR//WVblD5GjS WAS1mJ6H qbVHT0cGEmwA+/7ytew1BFZWB/UV0q/64YtnISknc6cgF6UkwH/CJINeFRUpbKzt7hNp3fnuwbA5GV1oD6V7itBC76os5Jh6Nr1UrivvS2tadhf+J9LDkhOxC6J8BJSyzyyGPtvpFr2StuQzf+zl8LOD1SszORNhzizoMqhT0Vh5i6PXZJ0iy4svPpPbAtQOhLY66PbomXA/LPu1FjewifRwzUN3OeM1/L5CnAVKa0rwlFRtFCzNGqR76GF91EiX+TPT/XHnxNC7MUvLxfZXzJgeNupjlVuhaQNRTQ0L+xnipna2B/Z9UGTswwzUc6zl0ght5Sk282uzosB3KVB3GWhf/tIditR548e5tjpCnkcGbbhA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Invalidate gfn_to_pfn_caches when the memory attributes of the gfn it contains change. Since gfn_to_pfn_caches are not hooked up to KVM's MMU notifiers, but rather have to be invalidated right _before_ KVM's MMU notifiers are triggers, adopt the approach used by kvm_mmu_notifier_invalidate_range_start for invalidating gpcs inside kvm_vm_set_mem_attributes. Signed-off-by: Patrick Roy --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 5 +++++ virt/kvm/kvm_mm.h | 10 +++++++++ virt/kvm/pfncache.c | 45 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index cd28eb34aaeb1..7d36164a2cee5 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -840,6 +840,7 @@ struct kvm { #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES /* Protected by slots_locks (for writes) and RCU (for reads) */ struct xarray mem_attr_array; + bool attribute_change_in_progress; #endif char stats_id[KVM_STATS_NAME_SIZE]; }; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 13347fb03d4a9..183f7ce57a428 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2533,6 +2533,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end, mutex_lock(&kvm->slots_lock); + /* Nothing to do if the entire range as the desired attributes. */ if (kvm_range_has_memory_attributes(kvm, start, end, attributes)) goto out_unlock; @@ -2547,6 +2548,9 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end, goto out_unlock; } + kvm->attribute_change_in_progress = true; + gfn_to_pfn_cache_invalidate_gfns_start(kvm, start, end); + kvm_handle_gfn_range(kvm, &pre_set_range); for (i = start; i < end; i++) { @@ -2558,6 +2562,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end, kvm_handle_gfn_range(kvm, &post_set_range); out_unlock: + kvm->attribute_change_in_progress = false; mutex_unlock(&kvm->slots_lock); return r; diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h index 715f19669d01f..5a53d888e4b18 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -27,12 +27,22 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool interruptible, void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, unsigned long start, unsigned long end); + +void gfn_to_pfn_cache_invalidate_gfns_start(struct kvm *kvm, + gfn_t start, + gfn_t end); #else static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, unsigned long start, unsigned long end) { } + +static inline void gfn_to_pfn_cache_invalidate_gfns_start(struct kvm *kvm, + gfn_t start, + gfn_t end) +{ +} #endif /* HAVE_KVM_PFNCACHE */ #ifdef CONFIG_KVM_PRIVATE_MEM diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index f0039efb9e1e3..6de934a8a153f 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -57,6 +57,43 @@ void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, unsigned long start, spin_unlock(&kvm->gpc_lock); } +/* + * Identical to `gfn_to_pfn_cache_invalidate_start`, except based on gfns + * instead of uhvas. + */ +void gfn_to_pfn_cache_invalidate_gfns_start(struct kvm *kvm, gfn_t start, gfn_t end) +{ + struct gfn_to_pfn_cache *gpc; + + spin_lock(&kvm->gpc_lock); + list_for_each_entry(gpc, &kvm->gpc_list, list) { + read_lock_irq(&gpc->lock); + + /* + * uhva based gpcs must not be used with gmem enabled memslots + */ + if (kvm_is_error_gpa(gpc->gpa)) { + read_unlock_irq(&gpc->lock); + continue; + } + + if (gpc->valid && !is_error_noslot_pfn(gpc->pfn) && + gpa_to_gfn(gpc->gpa) >= start && gpa_to_gfn(gpc->gpa) < end) { + read_unlock_irq(&gpc->lock); + + write_lock_irq(&gpc->lock); + if (gpc->valid && !is_error_noslot_pfn(gpc->pfn) && + gpa_to_gfn(gpc->gpa) >= start && gpa_to_gfn(gpc->gpa) < end) + gpc->valid = false; + write_unlock_irq(&gpc->lock); + continue; + } + + read_unlock_irq(&gpc->lock); + } + spin_unlock(&kvm->gpc_lock); +} + static bool kvm_gpc_is_valid_len(gpa_t gpa, unsigned long uhva, unsigned long len) { @@ -141,6 +178,14 @@ static inline bool mmu_notifier_retry_cache(struct kvm *kvm, unsigned long mmu_s if (kvm->mn_active_invalidate_count) return true; + /* + * Similarly to the above, attribute_change_in_progress is set + * before gfn_to_pfn_cache_invalidate_start is called in + * kvm_vm_set_mem_attributes, and isn't cleared until after + * mmu_invalidate_seq is updated. + */ + if (kvm->attribute_change_in_progress) + return true; /* * Ensure mn_active_invalidate_count is read before * mmu_invalidate_seq. This pairs with the smp_wmb() in