From patchwork Mon Nov 13 02:23:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= X-Patchwork-Id: 164319 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp961208vqg; Sun, 12 Nov 2023 18:27:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IF/FjNSWrB23tmVoG+ZM1P45xcEhUOL4yPHezCjr5OlPWZ8PbPj5WRKgFLIuMwVEfziuBz7 X-Received: by 2002:a17:90a:9f91:b0:280:37c3:3bcf with SMTP id o17-20020a17090a9f9100b0028037c33bcfmr13790933pjp.13.1699842450242; Sun, 12 Nov 2023 18:27:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699842450; cv=none; d=google.com; s=arc-20160816; b=m2RaJeVScXgVVczF9IVAgvsECOA0bP6P5hP5StjKcwkP9fRMuUyIWC2wLBJ2RV1MDn YBcs4tBvdEs9GXtqAfuxKzZRnmErgTL5Jv7b2bVTFwcPgy4yvlQMp4E4PCYuLkVQPVqs KbSVkVKkn+s9VLJ7bd199ZS9vE2axDMR8L8u4vNF1j5CM/23DcNd0ZxI+OcYz3Y+TgS7 PW5mGTOi64LfPJ2SXSz7+VE9nJp3DcsG5QXW7/R4BZg8BWrxoh01Y9ERM13gk6tcvUfS arM5vrKDHZcwaB9roL8oBBp++1Q6AV2ShS5oWq5dc4h8a7hLfnrUriWjfBhy6L60YbxZ j6XA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PDd0CtOG2qY21ulhSV7CIv69a17fu1BGpe32SvIEFbQ=; fh=AZdbNnvkerEwIaxrkEHewrGZFSiDsGxZahLkp7gMUH8=; b=KsEyUlrXLhB/u818Qcg9IaOAhC1j42lYkLsINurDJlzlZId1C7hJYCxa9OLBVg3lf4 oTMch5i5qeWPlckrtsgE2rirz+9Cmt1w6p3tlWbkepeNgu0+OktkpNMsHVEFyud7yRu3 2+Cvw1wuHx6zu7lFU2E+FIR6ysWbxIT43aXC4hJXOe2U4M3de+rTtPT0F8Kiz2qXeMdJ eUUKLIU7aBVrlE/KVey5sAYswlsraQJr0XXuPSARIdqZ8griOJvXonYVJmSsz5Ta2ClK iYgs3V7O79heWOt7ApYzuFFS9MJ2stYzmZ/5pcSg42woy7ICD6oL0flunBmoA7omsU/u z/EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=naLeBV79; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id kb7-20020a17090ae7c700b002804113621esi9958071pjb.100.2023.11.12.18.27.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Nov 2023 18:27:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=naLeBV79; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id B45778089E45; Sun, 12 Nov 2023 18:26:33 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232988AbjKMCZU (ORCPT + 30 others); Sun, 12 Nov 2023 21:25:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233035AbjKMCYr (ORCPT ); Sun, 12 Nov 2023 21:24:47 -0500 Received: from smtp-190c.mail.infomaniak.ch (smtp-190c.mail.infomaniak.ch [IPv6:2001:1600:4:17::190c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 513E73A8D for ; Sun, 12 Nov 2023 18:24:29 -0800 (PST) Received: from smtp-3-0001.mail.infomaniak.ch (unknown [10.4.36.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4STCtM4fDdzMpvbm; Mon, 13 Nov 2023 02:24:27 +0000 (UTC) Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4STCtL2gRXzMpnPj; Mon, 13 Nov 2023 03:24:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1699842267; bh=ch9T7FXLfTJgFWbno30mYt9y51RERoKANoOanV/XFpA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=naLeBV79e37SdDz+D/dS7uyrmwvygGCWlJc+XJuRQzMv5XfDFnAdIMM6Uid+A4GTN RAhlLTNQ/RTX6yjhoengNgNRl5DnbJYrC4WmjoGlHXKyNa50wVn5zQJnJq4Mtvmw+J D2bNNNOJvWidC6klDm91oW1hugsA6v5+ManBoxf4= From: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= To: Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Ingo Molnar , Kees Cook , Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Vitaly Kuznetsov , Wanpeng Li Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , Alexander Graf , Chao Peng , "Edgecombe, Rick P" , Forrest Yuan Yu , James Gowans , James Morris , John Andersen , "Madhavan T . Venkataraman" , Marian Rotariu , =?utf-8?q?Mihai_Don=C8=9Bu?= , =?utf-8?b?TmljdciZ?= =?utf-8?b?b3IgQ8OuyJt1?= , Thara Gopinath , Trilok Soni , Wei Liu , Will Deacon , Yu Zhang , Zahra Tarkhani , =?utf-8?q?=C8=98tefan_=C8=98icler?= =?utf-8?q?u?= , dev@lists.cloudhypervisor.org, kvm@vger.kernel.org, linux-hardening@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, qemu-devel@nongnu.org, virtualization@lists.linux-foundation.org, x86@kernel.org, xen-devel@lists.xenproject.org Subject: [RFC PATCH v2 09/19] KVM: x86: Extend kvm_range_has_memory_attributes() with match_all Date: Sun, 12 Nov 2023 21:23:16 -0500 Message-ID: <20231113022326.24388-10-mic@digikod.net> In-Reply-To: <20231113022326.24388-1-mic@digikod.net> References: <20231113022326.24388-1-mic@digikod.net> MIME-Version: 1.0 X-Infomaniak-Routing: alpha X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Sun, 12 Nov 2023 18:26:33 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782413997094647204 X-GMAIL-MSGID: 1782413997094647204 This enables to check if an attribute is tied to any memory page in a range. This will be useful in a folling commit to check for KVM_MEMORY_ATTRIBUTE_HEKI_IMMUTABLE. Cc: Chao Peng Cc: Kees Cook Cc: Madhavan T. Venkataraman Cc: Sean Christopherson Cc: Yu Zhang Signed-off-by: Mickaël Salaün --- Changes since v1: * New patch --- arch/x86/kvm/mmu/mmu.c | 2 +- include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 27 ++++++++++++++++++--------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index d7010e09440d..2024ff21d036 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7279,7 +7279,7 @@ static bool hugepage_has_attrs(struct kvm *kvm, struct kvm_memory_slot *slot, const unsigned long end = start + KVM_PAGES_PER_HPAGE(level); if (level == PG_LEVEL_2M) - return kvm_range_has_memory_attributes(kvm, start, end, attrs); + return kvm_range_has_memory_attributes(kvm, start, end, attrs, true); for (gfn = start; gfn < end; gfn += KVM_PAGES_PER_HPAGE(level - 1)) { if (hugepage_test_mixed(slot, gfn, level - 1) || diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index de68390ab0f2..9ecb016a336f 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2391,7 +2391,7 @@ static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn } bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, - unsigned long attrs); + unsigned long attrs, bool match_all); bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, struct kvm_gfn_range *range); bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index e2c178db17d5..67dbaaf40c1c 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2435,11 +2435,11 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES /* - * Returns true if _all_ gfns in the range [@start, @end) have attributes - * matching the @attrs bitmask. + * According to @match_all, returns true if _all_ (respectively _any_) gfns in + * the range [@start, @end) have attributes matching the @attrs bitmask. */ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, - unsigned long attrs) + unsigned long attrs, bool match_all) { XA_STATE(xas, &kvm->mem_attr_array, start); unsigned long index; @@ -2453,16 +2453,25 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, goto out; } - has_attrs = true; + has_attrs = match_all; for (index = start; index < end; index++) { do { entry = xas_next(&xas); } while (xas_retry(&xas, entry)); - if (xas.xa_index != index || - (xa_to_value(entry) & attrs) != attrs) { - has_attrs = false; - break; + if (match_all) { + if (xas.xa_index != index || + (xa_to_value(entry) & attrs) != attrs) { + has_attrs = false; + break; + } + } else { + index = xas.xa_index; + if (index < end && + (xa_to_value(entry) & attrs) == attrs) { + has_attrs = true; + break; + } } } @@ -2578,7 +2587,7 @@ int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end, lockdep_assert_held(&kvm->slots_arch_lock); /* Nothing to do if the entire range as the desired attributes. */ - if (kvm_range_has_memory_attributes(kvm, start, end, attributes)) + if (kvm_range_has_memory_attributes(kvm, start, end, attributes, true)) return r; /*