Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1624530pxu; Tue, 24 Nov 2020 05:15:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJw3kUwnHoEwLfOD1t1vFYKgYAXL7J/mtSsreH6JlwYW08flGHxznjRwCqsWvxsw0ih3G9TO X-Received: by 2002:a17:906:fc4:: with SMTP id c4mr4239229ejk.490.1606223703349; Tue, 24 Nov 2020 05:15:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606223703; cv=none; d=google.com; s=arc-20160816; b=vL5m9Jsk6BtCGbHTYSiasTAF1KqkpGH5NLy+gdpEvp+f+XRm3HgaPFV2MFDUEXwkI4 qylzygckG0LvWyxmyBp6RF9lPYO9aFoeh+pYEbcv7NxyOEQ5pT/hjB9pxB7bPArTs7Dl i1uom0hqpvk/oUDYH1XqNpbFCaqhgBzUXV/uZQ609fPnaem9kOyBOjya2bf7axWToMut LXjd81DbpPNaTIh8kN6qHK+TOc1j0/UqrEFumP5oD4H+ROF3jQmT71qqFIHNwpB/hkq/ PAbtU9wciK+MCKj7HQMDk4b623a9FeD1RpaXeEJgG4JBMWZs+MBhb4S6IGLMtoHmapgf oV+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:user-agent:references:in-reply-to :subject:cc:to:from:date:content-transfer-encoding:mime-version :dkim-signature; bh=yT9Ny50+CJcVKKnw9i15cpoCQJD4TMRUGHPplRR/f3g=; b=0dwdewHBTAarTw/4+YGOUDlspbQI0pV6GVHyWLVPyBasPHKZabt6a/aIh/s6jeSJew GU766qdnC1sQn0y2ln8N5Ww4RW5Ma3aXm5UAjXr+tBBGPWXpM2Yp41f0SwMhCDw3yk8D pgltcfn8jbu04bUwd7lUuAbmYx44fbBveMy4FVyfprJUtsYshk0e9EMpwe9Gf8iLnhzR 9fbF1GKjILClnyZKSOSxqovKicb+Qs11ZfDNlvoAIIFshJuh6Trce3KwLDzT5yxpZTjh iMGQbhrNfQA/zFH13mwkHOovNUz2lyJS2cYU3WT5K/yEYDGxHl1/fmjkQ87kL9mCDVS6 rU0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="fm38o4/Y"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y8si4530891ejp.36.2020.11.24.05.14.38; Tue, 24 Nov 2020 05:15:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="fm38o4/Y"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387881AbgKXNJZ (ORCPT + 99 others); Tue, 24 Nov 2020 08:09:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:60816 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387608AbgKXNJY (ORCPT ); Tue, 24 Nov 2020 08:09:24 -0500 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A74B0206D9; Tue, 24 Nov 2020 13:09:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1606223363; bh=ms1OC1jgGP8rsglnaN/qBcxxZ1UrxXPNJn5K7DfbYok=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=fm38o4/YbY3u5xAjT/QoQgA1caa9U6lR0nCkzzQekwnHqnxLb/C+Wsv/bS/2UwCZ7 qdZPbc39etdVCOT6LDni6bEfWfn+MmbwIfEP0HJAQXk4R3u4R9k/az2kg29WITPtcz zsyN6M7NYuYMrMY1tzazE5eO3hg/nXE4NvonBgEs= Received: from disco-boy.misterjones.org ([51.254.78.96] helo=www.loen.fr) by disco-boy.misterjones.org with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94) (envelope-from ) id 1khY4P-00DFma-EF; Tue, 24 Nov 2020 13:09:21 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Tue, 24 Nov 2020 13:09:21 +0000 From: Marc Zyngier To: David Brazdil Cc: kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, James Morse , Julien Thierry , Suzuki K Poulose , Catalin Marinas , Will Deacon , Mark Rutland , Andrew Scull , Ard Biesheuvel , kernel-team@android.com Subject: Re: [RFC PATCH 2/6] kvm: arm64: Fix up RELA relocations in hyp code/data In-Reply-To: <20201119162543.78001-3-dbrazdil@google.com> References: <20201119162543.78001-1-dbrazdil@google.com> <20201119162543.78001-3-dbrazdil@google.com> User-Agent: Roundcube Webmail/1.4.9 Message-ID: X-Sender: maz@kernel.org X-SA-Exim-Connect-IP: 51.254.78.96 X-SA-Exim-Rcpt-To: dbrazdil@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, ascull@google.com, ardb@kernel.org, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-11-19 16:25, David Brazdil wrote: > KVM nVHE code runs under a different VA mapping than the kernel, hence > so far it relied only on PC-relative addressing to avoid accidentally > using a relocated kernel VA from a constant pool (see hyp_symbol_addr). > > So as to reduce the possibility of a programmer error, fixup the > relocated addresses instead. Let the kernel relocate them to kernel VA > first, but then iterate over them again, filter those that point to hyp > code/data and convert the kernel VA to hyp VA. > > This is done after kvm_compute_layout and before apply_alternatives. > > Signed-off-by: David Brazdil > --- > arch/arm64/include/asm/kvm_mmu.h | 1 + > arch/arm64/kernel/smp.c | 4 +- > arch/arm64/kvm/va_layout.c | 76 ++++++++++++++++++++++++++++++++ > 3 files changed, 80 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/include/asm/kvm_mmu.h > b/arch/arm64/include/asm/kvm_mmu.h > index 5168a0c516ae..e5226f7e4732 100644 > --- a/arch/arm64/include/asm/kvm_mmu.h > +++ b/arch/arm64/include/asm/kvm_mmu.h > @@ -105,6 +105,7 @@ alternative_cb_end > void kvm_update_va_mask(struct alt_instr *alt, > __le32 *origptr, __le32 *updptr, int nr_inst); > void kvm_compute_layout(void); > +void kvm_fixup_hyp_relocations(void); > > static __always_inline unsigned long __kern_hyp_va(unsigned long v) > { > diff --git a/arch/arm64/kernel/smp.c b/arch/arm64/kernel/smp.c > index 18e9727d3f64..30241afc2c93 100644 > --- a/arch/arm64/kernel/smp.c > +++ b/arch/arm64/kernel/smp.c > @@ -434,8 +434,10 @@ static void __init hyp_mode_check(void) > "CPU: CPUs started in inconsistent modes"); > else > pr_info("CPU: All CPU(s) started at EL1\n"); > - if (IS_ENABLED(CONFIG_KVM)) > + if (IS_ENABLED(CONFIG_KVM)) { > kvm_compute_layout(); > + kvm_fixup_hyp_relocations(); > + } > } > > void __init smp_cpus_done(unsigned int max_cpus) > diff --git a/arch/arm64/kvm/va_layout.c b/arch/arm64/kvm/va_layout.c > index d8cc51bd60bf..b80fab974896 100644 > --- a/arch/arm64/kvm/va_layout.c > +++ b/arch/arm64/kvm/va_layout.c > @@ -10,6 +10,7 @@ > #include > #include > #include > +#include > #include > #include > > @@ -82,6 +83,81 @@ __init void kvm_compute_layout(void) > init_hyp_physvirt_offset(); > } > > +#define __load_elf_u64(s) \ > + ({ \ > + extern u64 s; \ > + u64 val; \ > + \ > + asm ("ldr %0, =%1" : "=r"(val) : "S"(&s)); \ > + val; \ > + }) I'm not sure I get the rational about the naming here. None of this has much to do with ELF, but seems to just load a value from a constant pool. > + > +static bool __is_within_bounds(u64 addr, char *start, char *end) > +{ > + return start <= (char*)addr && (char*)addr < end; > +} > + > +static bool __is_in_hyp_section(u64 addr) > +{ > + return __is_within_bounds(addr, __hyp_text_start, __hyp_text_end) || > + __is_within_bounds(addr, __hyp_rodata_start, __hyp_rodata_end) > || > + __is_within_bounds(addr, > + CHOOSE_NVHE_SYM(__per_cpu_start), > + CHOOSE_NVHE_SYM(__per_cpu_end)); > +} > + > +static void __fixup_hyp_rel(u64 addr) > +{ > + u64 *ptr, kern_va, hyp_va; > + > + /* Adjust the relocation address taken from ELF for KASLR. */ > + addr += kaslr_offset(); > + > + /* Skip addresses not in any of the hyp sections. */ > + if (!__is_in_hyp_section(addr)) > + return; > + > + /* Get the LM alias of the relocation address. */ > + ptr = (u64*)kvm_ksym_ref((void*)addr); Why the casting? We should be perfectly fine without. nit: we really need to change the name of this helper, it doesn't have anything to do with symbols anymore. And actually, lm_alias() *is* the right thing to use here (we don't relocate anything on VHE). > + > + /* > + * Read the value at the relocation address. It has already been > + * relocated to the actual kernel kimg VA. > + */ > + kern_va = (u64)kvm_ksym_ref((void*)*ptr); Same comment. > + > + /* Convert to hyp VA. */ > + hyp_va = __early_kern_hyp_va(kern_va); > + > + /* Store hyp VA at the relocation address. */ > + *ptr = __early_kern_hyp_va(kern_va); > +} > + > +static void __fixup_hyp_rela(void) > +{ > + Elf64_Rela *rel; > + size_t i, n; > + > + rel = (Elf64_Rela*)(kimage_vaddr + __load_elf_u64(__rela_offset)); > + n = __load_elf_u64(__rela_size) / sizeof(*rel); > + > + for (i = 0; i < n; ++i) > + __fixup_hyp_rel(rel[i].r_offset); > +} > + > +/* > + * The kernel relocated pointers to kernel VA. Iterate over > relocations in > + * the hypervisor ELF sections and convert them to hyp VA. This avoids > the > + * need to only use PC-relative addressing in hyp. > + */ > +__init void kvm_fixup_hyp_relocations(void) > +{ > + if (!IS_ENABLED(CONFIG_RELOCATABLE) || has_vhe()) What do we do if CONFIG_RELOCATABLE is not selected? As far as I can tell, bad things will happen... I'm also worried that at this stage, the kernel is broken, until you remove the other bits involved in runtime offsetting pointers. Thanks, M. -- Jazz is not dead. It just smells funny...