Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp886645lqo; Fri, 10 May 2024 20:46:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUR8E73cX4wW6+qm9CeVXGcG5yyyQ8IYtu6jkf1BHLnu3gaQHXWxFPjGe+yicDQjIrfWZeNU0TflJICuz7ggKNCd4iNdyVqOnMHr3y4kQ== X-Google-Smtp-Source: AGHT+IGceZ7ojpCz7IX9zhUXbVJTOga4ClIHbpXjhLvVPDIyJ7eYsBRoPYD1p+c3iKjT1/aZKUg9 X-Received: by 2002:a05:620a:5637:b0:792:c0f7:857e with SMTP id af79cd13be357-792c75f0eedmr491425185a.54.1715399218426; Fri, 10 May 2024 20:46:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715399218; cv=pass; d=google.com; s=arc-20160816; b=wIK8ZYKobVIxR8rWpgyLU8HrkD09u+KYIniDVj8FPiCWptmZxjdjabWSD0EG/1PGjh ZBmSV97r4zB8NBPxyhSE+7jkqyEnEMOHBWt+5oGPe9oAqdMUrX7FAs/as1gSbeJOJYD0 5F4Cc06dV24ijWbIS7EkMvbX0/Kgl769w4xpoM0IjoqquU/JI1lij0MjWJhCweobr7Yv 7n/71erqxWmykBfVb8L9vM0xgO7r/Cr8YhYEq85fWd1kdoOc+jyJlIcBc6vQsUJwp2ta 1ljtBVXryiklvc6fghMeLtsXlQJh7ijlv2X+4CYIGUMKHeQoKKLAFOwrp/o5pLbbznfB BxNQ== ARC-Message-Signature: i=2; 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:message-id:date:subject:cc:to :from:dkim-signature; bh=/oQPtMgHD8rA3njQCT4IplE8E+0JmO4poKrpVU3HxCw=; fh=IevZxWlFSuQrJShMaTjVUq7vxk50Tc3mVu3PPT6WiVQ=; b=GedUr4szlvB+xOp+UHl5nWDCNSmIscHABwanGcCb/T5EthKH8+LrPTX8zlmC4wnq6L 1sZizxir8iOcWhQ7Rrr0N/tM676/lecRsvAw4WCHn08M1hTz4OBbRl2WTVLsniLpul8z EfxJ0R1YN8epI2XItlaCey5BcRrldN1sxDYQPcNbYII1eKHtgrKy9B1Q2JPTp+oQoj1+ RUv6osEv3b2HpsVG3YP7o2TPKwa3q7ODBLfvGGi1UMPxs2Tya/EIADtk742IBkt+2STO 3Ny2DFXbi3gpMQIk/lpbqYoG2z0VAeGbW2yrYy7FSYLJ7d5o3hPrLS8/KN0uiLF4nIby va0A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@antgroup.com header.s=default header.b=IkF1OcpO; arc=pass (i=1 spf=pass spfdomain=antgroup.com dkim=pass dkdomain=antgroup.com dmarc=pass fromdomain=antgroup.com); spf=pass (google.com: domain of linux-kernel+bounces-176411-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176411-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-792bf340407si483493485a.518.2024.05.10.20.46.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 May 2024 20:46:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-176411-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@antgroup.com header.s=default header.b=IkF1OcpO; arc=pass (i=1 spf=pass spfdomain=antgroup.com dkim=pass dkdomain=antgroup.com dmarc=pass fromdomain=antgroup.com); spf=pass (google.com: domain of linux-kernel+bounces-176411-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176411-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com 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 874CF1C21145 for ; Sat, 11 May 2024 03:46:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 115E53DBB7; Sat, 11 May 2024 03:46:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b="IkF1OcpO" Received: from out0-212.mail.aliyun.com (out0-212.mail.aliyun.com [140.205.0.212]) (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 3DC941843; Sat, 11 May 2024 03:46:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=140.205.0.212 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715399206; cv=none; b=gOt08WsRbWbxINGExO+q4ooGF6KThYcZ/EsYYffQ8DWOJ0FiST87UvZ4Np2T8WI8c0Q8LQjQSrUWJu9miZvpP0fPTuZi9kn6orfaxG7A7jfOWF0iZ47yUFH/X/PORLJlTgBZTUzESiidR2wh1s94HG5FXO79mjaPPWEbpldGOqI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715399206; c=relaxed/simple; bh=hZ03FoXZ1F+HCTHSptsV5XuAZGqY5U010SnORP5VsWg=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=AM3XBXVWly+PPgp1i0k06f7t2Iye+Ail4tTnMtYn7vVaG1Af8uHBERJlm3kj27ZKkKshCOiqWEVy0J97zJTwX57BVajNLYcYQAvuPsQFgQVpIeDbMP/h7yKryKuQJCPJCeYi2py5sgYcmgSrz0j0DddWibidBC7jdQybsHswzAY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com; spf=pass smtp.mailfrom=antgroup.com; dkim=pass (1024-bit key) header.d=antgroup.com header.i=@antgroup.com header.b=IkF1OcpO; arc=none smtp.client-ip=140.205.0.212 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antgroup.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=antgroup.com DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=antgroup.com; s=default; t=1715399199; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=/oQPtMgHD8rA3njQCT4IplE8E+0JmO4poKrpVU3HxCw=; b=IkF1OcpOqrZOX8gHWEO4Ay8znEZHzAnY+dLaEd3Y6LbM3jWMasIFN0RRhY0o+BeA/kY/b8s+a6u6rlgqlMulbxWcCW1s6u/vS5u9EuSPHLR+meeEgl9+B+/WBGM50FZmiY/5jdw6ItPYHthFVAurHxE1tnGJfeHFqKivsUX2/7Y= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R311e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047205;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---.XYYlGFt_1715399198; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.XYYlGFt_1715399198) by smtp.aliyun-inc.com; Sat, 11 May 2024 11:46:39 +0800 From: "Hou Wenlong" To: kvm@vger.kernel.org Cc: "Lai Jiangshan" , "Sean Christopherson" , "Paolo Bonzini" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , , "H. Peter Anvin" , Subject: [PATCH v2] KVM: x86/mmu: Only allocate shadowed translation cache for sp->role.level <= KVM_MAX_HUGEPAGE_LEVEL Date: Sat, 11 May 2024 11:46:37 +0800 Message-Id: <5b0cda8a7456cda476b14fca36414a56f921dd52.1715398655.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Only the indirect SP with sp->role.level <= KVM_MAX_HUGEPAGE_LEVEL might have leaf gptes, so allocation of shadowed translation cache is needed only for it. Then, it can use sp->shadowed_translation to determine whether to use the information in the shadowed translation cache or not. Also, extend the WARN in FNAME(sync_spte)() to ensure that this won't break shadow_mmu_get_sp_for_split(). Suggested-by: Lai Jiangshan Signed-off-by: Hou Wenlong --- v1->v2: - Remove the order change in kvm_mmu_page_get_gfn() to keep smallest diff. - Drop the helper. - Extend the WARN in FNAME(sync_spte). arch/x86/kvm/mmu/mmu.c | 11 +++++------ arch/x86/kvm/mmu/paging_tmpl.h | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index fc3b59b59ee1..dc6f6a272e98 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -719,7 +719,7 @@ static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index) if (sp->role.passthrough) return sp->gfn; - if (!sp->role.direct) + if (sp->shadowed_translation) return sp->shadowed_translation[index] >> PAGE_SHIFT; return sp->gfn + (index << ((sp->role.level - 1) * SPTE_LEVEL_BITS)); @@ -733,7 +733,7 @@ static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index) */ static u32 kvm_mmu_page_get_access(struct kvm_mmu_page *sp, int index) { - if (sp_has_gptes(sp)) + if (sp->shadowed_translation) return sp->shadowed_translation[index] & ACC_ALL; /* @@ -754,7 +754,7 @@ static u32 kvm_mmu_page_get_access(struct kvm_mmu_page *sp, int index) static void kvm_mmu_page_set_translation(struct kvm_mmu_page *sp, int index, gfn_t gfn, unsigned int access) { - if (sp_has_gptes(sp)) { + if (sp->shadowed_translation) { sp->shadowed_translation[index] = (gfn << PAGE_SHIFT) | access; return; } @@ -1697,8 +1697,7 @@ static void kvm_mmu_free_shadow_page(struct kvm_mmu_page *sp) hlist_del(&sp->hash_link); list_del(&sp->link); free_page((unsigned long)sp->spt); - if (!sp->role.direct) - free_page((unsigned long)sp->shadowed_translation); + free_page((unsigned long)sp->shadowed_translation); kmem_cache_free(mmu_page_header_cache, sp); } @@ -2199,7 +2198,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_shadow_page(struct kvm *kvm, sp = kvm_mmu_memory_cache_alloc(caches->page_header_cache); sp->spt = kvm_mmu_memory_cache_alloc(caches->shadow_page_cache); - if (!role.direct) + if (!role.direct && role.level <= KVM_MAX_HUGEPAGE_LEVEL) sp->shadowed_translation = kvm_mmu_memory_cache_alloc(caches->shadowed_info_cache); set_page_private(virt_to_page(sp->spt), (unsigned long)sp); diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index 7a87097cb45b..89b5d73e9e3c 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -911,7 +911,7 @@ static int FNAME(sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int gpa_t pte_gpa; gfn_t gfn; - if (WARN_ON_ONCE(!sp->spt[i])) + if (WARN_ON_ONCE(!sp->spt[i] || !sp->shadowed_translation)) return 0; first_pte_gpa = FNAME(get_level1_sp_gpa)(sp); -- 2.31.1