From nobody Sat Nov 23 18:04:46 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 203B26AAD for ; Tue, 12 Nov 2024 00:34:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731371641; cv=none; b=SH5enkqhdDziQrbHqvPwdu77xKfPESWatm8YKke8ukw7w3orrVJ61F2bcJHQ0+pd8TGuJCY5pZlc7FsPVESTbGpQn5y36VBkITZa2uDMpaRLuJHug+/5Uulx8VYng2rltnQbwfX5aR2+BavQpQ0whMQ0BH45/amqSnQXlXkby9w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731371641; c=relaxed/simple; bh=doRnVM65BJCYlkuDOmvxg8MXcgVyTMB2b7SwBGyaCXs=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=UY790Ux+SU4o0v8c0SJ8HBPPPrRTdCUOljEhN263gX6PxHWMzvBegMRux1XQZlSh8yUdd8h7pVGeEwfp8zNCnrxdLoOQ+iPXmRaZViUG/xEEPC2aqZ+JSBs0LFtpk97q2aMaqO3fiSrM9vG2J8rUh7EeOnM0wAtpbU3GcSRFCQA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--kaleshsingh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=o3IMHTsj; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kaleshsingh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="o3IMHTsj" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e2904d0cad0so8632767276.1 for ; Mon, 11 Nov 2024 16:34:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731371639; x=1731976439; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=uPqm/j9GML5grkd0K90HJBJ+6dp+iW/S4dreeHv8WiA=; b=o3IMHTsjCMGPNM17Ezo000/+Dj7W0OljpDRmZyc/Np9Xn5v56Qt0YjYzb9vkoO8wA6 LKYFVwFj0tNXHxJOa93SrhgZ3f+gHBBHaHQv+xSzuC1guyFu2cMPZc4znn7dOD89uyjH bdibA/E0qnwdZHFuqbvMsDMO4UHmGeL3gAqSV9yI0sQA1FZCH7undc6KkwpPN5xD2bJ9 P7WRJFoK1hdeDk51O3TaaqL+YRBTGAtEcf7rtx/OiYnOb4ZjLnod/XZ7dXQaALzevs08 wSGaIVBX8DYBZNzz5bBcB3xgYI8ObXw3QHhEU1sSTqDOyZTB0Zh461V7K95/Y/HrEfMm 8qbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731371639; x=1731976439; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=uPqm/j9GML5grkd0K90HJBJ+6dp+iW/S4dreeHv8WiA=; b=XyyyQw93E3SFXhO1OJrDTVuTmzOapnlPfrx5b7veLNoujVOAq+UCTuw4Y8Tmpp3mQm kaZE6IK1vOafwV/Qq8xwXPVcHKcbn5v4J31Thdw737N2tZD/P+xklP+c47a23jC2FWdv beRoQcJIDvL/zj3dguP+EGHeDuJhONll2w33ofeCujqyUtgOa7TRlYyQD98ab+lNrjG6 NtklLKMtLX1LfrlPEiYmM0U7AuLGTuVctxI4MWf7HnkiL2crttCJV0mhR2srwp7YxKbE WO37CI/8emqzLRUk9mFvQOhddVO7Iizvf6rrOFt+6LBrrph6tN+PkAYiVRguz5+KNA5/ lxLg== X-Forwarded-Encrypted: i=1; AJvYcCXdlD0NX2+bwqUP4vrZWXpZ3MKhju78YThX1qAKvrdlhZ7CFGCSg+2CzlKH2FnCu8cH3LaVxEOVIzFr4g8=@vger.kernel.org X-Gm-Message-State: AOJu0YwHQvjzYD9eoOKPG7tf4K2/B9E0HYkDKy4AZI4DxBcAhLcHC/mR u1eOix5fir4nnsYpXBl5S84WnN1Tw0DFwcGo1BbiunMg1JgN2McTKC789PVZddvq13JIaGbl6xp i30+nTcm0FOiexoPi0KZMqQ== X-Google-Smtp-Source: AGHT+IF2AUPCYUIW/bjEyF6Bagqvriyp8owPVn//zZAu+iYnJp2HZR2/FMhkCxa9HQYN4qZCYWpxjJqGcHfQ1GzhLg== X-Received: from kalesh.mtv.corp.google.com ([2a00:79e0:2e3f:8:64e3:b06b:ffa7:2d32]) (user=kaleshsingh job=sendgmr) by 2002:a25:aaaa:0:b0:e30:d61e:b110 with SMTP id 3f1490d57ef6-e337f8bfd18mr20453276.5.1731371639080; Mon, 11 Nov 2024 16:33:59 -0800 (PST) Date: Mon, 11 Nov 2024 16:32:50 -0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241112003336.1375584-1-kaleshsingh@google.com> Subject: [PATCH v2] arm64: kvm: Introduce nvhe stack size constants From: Kalesh Singh To: will@kernel.or, maz@kernel.org, broonie@kernel.org, mark.rutland@arm.com Cc: kernel-team@android.com, android-mm@google.com, Kalesh Singh , Will Deacon , Catalin Marinas , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Ard Biesheuvel , D Scott Phillips , Wang Jinchao , Ankit Agrawal , Andrey Konovalov , Puranjay Mohan , "Madhavan T. Venkataraman" , Randy Dunlap , "=?UTF-8?q?Pierre-Cl=C3=A9ment=20Tosi?=" , Bjorn Helgaas , Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor nvhe stack code to use NVHE_STACK_SIZE/SHIFT constants, instead of directly using PAGE_SIZE/SHIFT. This makes the code a bit easier to read, without introducing any functional changes. Cc: Marc Zyngier Cc: Mark Brown Cc: Mark Rutland Cc: Will Deacon Signed-off-by: Kalesh Singh --- Changes in v2: - Drop Kconfig option for nVHE stack size arch/arm64/include/asm/memory.h | 5 ++++- arch/arm64/include/asm/stacktrace/nvhe.h | 2 +- arch/arm64/kvm/arm.c | 18 +++++++++--------- arch/arm64/kvm/hyp/nvhe/host.S | 4 ++-- arch/arm64/kvm/hyp/nvhe/mm.c | 12 ++++++------ arch/arm64/kvm/hyp/nvhe/stacktrace.c | 4 ++-- arch/arm64/kvm/mmu.c | 12 ++++++------ arch/arm64/kvm/stacktrace.c | 6 +++--- 8 files changed, 33 insertions(+), 30 deletions(-) diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memor= y.h index 0480c61dbb4f..96f236f0c141 100644 --- a/arch/arm64/include/asm/memory.h +++ b/arch/arm64/include/asm/memory.h @@ -145,13 +145,16 @@ =20 #define OVERFLOW_STACK_SIZE SZ_4K =20 +#define NVHE_STACK_SHIFT PAGE_SHIFT +#define NVHE_STACK_SIZE (UL(1) << NVHE_STACK_SHIFT) + /* * With the minimum frame size of [x29, x30], exactly half the combined * sizes of the hyp and overflow stacks is the maximum size needed to * save the unwinded stacktrace; plus an additional entry to delimit the * end. */ -#define NVHE_STACKTRACE_SIZE ((OVERFLOW_STACK_SIZE + PAGE_SIZE) / 2 + size= of(long)) +#define NVHE_STACKTRACE_SIZE ((OVERFLOW_STACK_SIZE + NVHE_STACK_SIZE) / 2 = + sizeof(long)) =20 /* * Alignment of kernel segments (e.g. .text, .data). diff --git a/arch/arm64/include/asm/stacktrace/nvhe.h b/arch/arm64/include/= asm/stacktrace/nvhe.h index 44759281d0d4..171f9edef49f 100644 --- a/arch/arm64/include/asm/stacktrace/nvhe.h +++ b/arch/arm64/include/asm/stacktrace/nvhe.h @@ -47,7 +47,7 @@ static inline void kvm_nvhe_unwind_init(struct unwind_sta= te *state, =20 DECLARE_KVM_NVHE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],= overflow_stack); DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_stacktrace_info, kvm_stacktrace_i= nfo); -DECLARE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); +DECLARE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base); =20 void kvm_nvhe_dump_backtrace(unsigned long hyp_offset); =20 diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 48cafb65d6ac..d19bffbfcb9e 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -61,7 +61,7 @@ static enum kvm_wfx_trap_policy kvm_wfe_trap_policy __rea= d_mostly =3D KVM_WFX_NOTR =20 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); =20 -DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); +DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base); DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); =20 DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt); @@ -2359,7 +2359,7 @@ static void __init teardown_hyp_mode(void) =20 free_hyp_pgds(); for_each_possible_cpu(cpu) { - free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); + free_pages(per_cpu(kvm_arm_hyp_stack_base, cpu), NVHE_STACK_SHIFT - PAGE= _SHIFT); free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order= ()); =20 if (free_sve) { @@ -2547,15 +2547,15 @@ static int __init init_hyp_mode(void) * Allocate stack pages for Hypervisor-mode */ for_each_possible_cpu(cpu) { - unsigned long stack_page; + unsigned long stack_base; =20 - stack_page =3D __get_free_page(GFP_KERNEL); - if (!stack_page) { + stack_base =3D __get_free_pages(GFP_KERNEL, NVHE_STACK_SHIFT - PAGE_SHIF= T); + if (!stack_base) { err =3D -ENOMEM; goto out_err; } =20 - per_cpu(kvm_arm_hyp_stack_page, cpu) =3D stack_page; + per_cpu(kvm_arm_hyp_stack_base, cpu) =3D stack_base; } =20 /* @@ -2624,9 +2624,9 @@ static int __init init_hyp_mode(void) */ for_each_possible_cpu(cpu) { struct kvm_nvhe_init_params *params =3D per_cpu_ptr_nvhe_sym(kvm_init_pa= rams, cpu); - char *stack_page =3D (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); + char *stack_base =3D (char *)per_cpu(kvm_arm_hyp_stack_base, cpu); =20 - err =3D create_hyp_stack(__pa(stack_page), ¶ms->stack_hyp_va); + err =3D create_hyp_stack(__pa(stack_base), ¶ms->stack_hyp_va); if (err) { kvm_err("Cannot map hyp stack\n"); goto out_err; @@ -2638,7 +2638,7 @@ static int __init init_hyp_mode(void) * __hyp_pa() won't do the right thing there, since the stack * has been mapped in the flexible private VA space. */ - params->stack_pa =3D __pa(stack_page); + params->stack_pa =3D __pa(stack_base); } =20 for_each_possible_cpu(cpu) { diff --git a/arch/arm64/kvm/hyp/nvhe/host.S b/arch/arm64/kvm/hyp/nvhe/host.S index 3d610fc51f4d..58f0cb2298cc 100644 --- a/arch/arm64/kvm/hyp/nvhe/host.S +++ b/arch/arm64/kvm/hyp/nvhe/host.S @@ -188,12 +188,12 @@ SYM_FUNC_END(__host_hvc) =20 /* * Test whether the SP has overflowed, without corrupting a GPR. - * nVHE hypervisor stacks are aligned so that the PAGE_SHIFT bit + * nVHE hypervisor stacks are aligned so that the NVHE_STACK_SHIFT bit * of SP should always be 1. */ add sp, sp, x0 // sp' =3D sp + x0 sub x0, sp, x0 // x0' =3D sp' - x0 =3D (sp + x0) - x0 =3D sp - tbz x0, #PAGE_SHIFT, .L__hyp_sp_overflow\@ + tbz x0, #NVHE_STACK_SHIFT, .L__hyp_sp_overflow\@ sub x0, sp, x0 // x0'' =3D sp' - x0' =3D (sp + x0) - sp =3D x0 sub sp, sp, x0 // sp'' =3D sp' - x0 =3D (sp + x0) - x0 =3D sp =20 diff --git a/arch/arm64/kvm/hyp/nvhe/mm.c b/arch/arm64/kvm/hyp/nvhe/mm.c index 8850b591d775..f41c7440b34b 100644 --- a/arch/arm64/kvm/hyp/nvhe/mm.c +++ b/arch/arm64/kvm/hyp/nvhe/mm.c @@ -360,10 +360,10 @@ int pkvm_create_stack(phys_addr_t phys, unsigned long= *haddr) =20 prev_base =3D __io_map_base; /* - * Efficient stack verification using the PAGE_SHIFT bit implies + * Efficient stack verification using the NVHE_STACK_SHIFT bit implies * an alignment of our allocation on the order of the size. */ - size =3D PAGE_SIZE * 2; + size =3D NVHE_STACK_SIZE * 2; addr =3D ALIGN(__io_map_base, size); =20 ret =3D __pkvm_alloc_private_va_range(addr, size); @@ -373,12 +373,12 @@ int pkvm_create_stack(phys_addr_t phys, unsigned long= *haddr) * at the higher address and leave the lower guard page * unbacked. * - * Any valid stack address now has the PAGE_SHIFT bit as 1 + * Any valid stack address now has the NVHE_STACK_SHIFT bit as 1 * and addresses corresponding to the guard page have the - * PAGE_SHIFT bit as 0 - this is used for overflow detection. + * NVHE_STACK_SHIFT bit as 0 - this is used for overflow detection. */ - ret =3D kvm_pgtable_hyp_map(&pkvm_pgtable, addr + PAGE_SIZE, - PAGE_SIZE, phys, PAGE_HYP); + ret =3D kvm_pgtable_hyp_map(&pkvm_pgtable, addr + NVHE_STACK_SIZE, + NVHE_STACK_SIZE, phys, PAGE_HYP); if (ret) __io_map_base =3D prev_base; } diff --git a/arch/arm64/kvm/hyp/nvhe/stacktrace.c b/arch/arm64/kvm/hyp/nvhe= /stacktrace.c index ed6b58b19cfa..5b6eeab1a774 100644 --- a/arch/arm64/kvm/hyp/nvhe/stacktrace.c +++ b/arch/arm64/kvm/hyp/nvhe/stacktrace.c @@ -28,7 +28,7 @@ static void hyp_prepare_backtrace(unsigned long fp, unsig= ned long pc) struct kvm_nvhe_stacktrace_info *stacktrace_info =3D this_cpu_ptr(&kvm_st= acktrace_info); struct kvm_nvhe_init_params *params =3D this_cpu_ptr(&kvm_init_params); =20 - stacktrace_info->stack_base =3D (unsigned long)(params->stack_hyp_va - PA= GE_SIZE); + stacktrace_info->stack_base =3D (unsigned long)(params->stack_hyp_va - NV= HE_STACK_SIZE); stacktrace_info->overflow_stack_base =3D (unsigned long)this_cpu_ptr(over= flow_stack); stacktrace_info->fp =3D fp; stacktrace_info->pc =3D pc; @@ -54,7 +54,7 @@ static struct stack_info stackinfo_get_hyp(void) { struct kvm_nvhe_init_params *params =3D this_cpu_ptr(&kvm_init_params); unsigned long high =3D params->stack_hyp_va; - unsigned long low =3D high - PAGE_SIZE; + unsigned long low =3D high - NVHE_STACK_SIZE; =20 return (struct stack_info) { .low =3D low, diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 0f7658aefa1a..e366ed8650fe 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -704,10 +704,10 @@ int create_hyp_stack(phys_addr_t phys_addr, unsigned = long *haddr) =20 mutex_lock(&kvm_hyp_pgd_mutex); /* - * Efficient stack verification using the PAGE_SHIFT bit implies + * Efficient stack verification using the NVHE_STACK_SHIFT bit implies * an alignment of our allocation on the order of the size. */ - size =3D PAGE_SIZE * 2; + size =3D NVHE_STACK_SIZE * 2; base =3D ALIGN_DOWN(io_map_base - size, size); =20 ret =3D __hyp_alloc_private_va_range(base); @@ -724,12 +724,12 @@ int create_hyp_stack(phys_addr_t phys_addr, unsigned = long *haddr) * at the higher address and leave the lower guard page * unbacked. * - * Any valid stack address now has the PAGE_SHIFT bit as 1 + * Any valid stack address now has the NVHE_STACK_SHIFT bit as 1 * and addresses corresponding to the guard page have the - * PAGE_SHIFT bit as 0 - this is used for overflow detection. + * NVHE_STACK_SHIFT bit as 0 - this is used for overflow detection. */ - ret =3D __create_hyp_mappings(base + PAGE_SIZE, PAGE_SIZE, phys_addr, - PAGE_HYP); + ret =3D __create_hyp_mappings(base + NVHE_STACK_SIZE, NVHE_STACK_SIZE, + phys_addr, PAGE_HYP); if (ret) kvm_err("Cannot map hyp stack\n"); =20 diff --git a/arch/arm64/kvm/stacktrace.c b/arch/arm64/kvm/stacktrace.c index 3ace5b75813b..b9744a932920 100644 --- a/arch/arm64/kvm/stacktrace.c +++ b/arch/arm64/kvm/stacktrace.c @@ -50,7 +50,7 @@ static struct stack_info stackinfo_get_hyp(void) struct kvm_nvhe_stacktrace_info *stacktrace_info =3D this_cpu_ptr_nvhe_sym(kvm_stacktrace_info); unsigned long low =3D (unsigned long)stacktrace_info->stack_base; - unsigned long high =3D low + PAGE_SIZE; + unsigned long high =3D low + NVHE_STACK_SIZE; =20 return (struct stack_info) { .low =3D low, @@ -60,8 +60,8 @@ static struct stack_info stackinfo_get_hyp(void) =20 static struct stack_info stackinfo_get_hyp_kern_va(void) { - unsigned long low =3D (unsigned long)*this_cpu_ptr(&kvm_arm_hyp_stack_pag= e); - unsigned long high =3D low + PAGE_SIZE; + unsigned long low =3D (unsigned long)*this_cpu_ptr(&kvm_arm_hyp_stack_bas= e); + unsigned long high =3D low + NVHE_STACK_SIZE; =20 return (struct stack_info) { .low =3D low, base-commit: 59b723cd2adbac2a34fc8e12c74ae26ae45bf230 --=20 2.47.0.277.g8800431eea-goog