From nobody Sun Dec 28 00:50:21 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C6A9C4332F for ; Thu, 14 Dec 2023 09:19:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234542AbjLNJT0 (ORCPT ); Thu, 14 Dec 2023 04:19:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229691AbjLNJTY (ORCPT ); Thu, 14 Dec 2023 04:19:24 -0500 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E15BFA6 for ; Thu, 14 Dec 2023 01:19:29 -0800 (PST) Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-33646dbedc9so413914f8f.3 for ; Thu, 14 Dec 2023 01:19:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1702545568; x=1703150368; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=h6KCWUfDFNx0O1wfeTExInhDSJOXnrOXPLAO9F5T2Tc=; b=FME2U1jZAN/RcqbaWDbJ6qxJRRDSghjiaGn7MaUj8UhNs5WXK13qDUXKIUFWQ93A2u I+knYpHfh68YG3uwzcDzAnsi0vn+FH7cIERR/+aEqqPX04tfydY871mbxXThYdxYr6zl 9QWsXbsHjXxDMP1rXOfRuRRsSf+UXSJGgCZfN+vZbNC3NLcZxpawyY6J9ebeyKF9Z7xH q0IEQpKc8alwU2no1OQ5Dx0QA7770wSg/6Y5W/h+jfN622QcirJT5M0/6oGnV/6Y5bl1 kK4waTbJEvd0TTszKq3/AS8w7XvRXRhWOY4cKM54XQ3i1un1pqxR9ceNChG6N6m3HZCd bTDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702545568; x=1703150368; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=h6KCWUfDFNx0O1wfeTExInhDSJOXnrOXPLAO9F5T2Tc=; b=dt9EXs04AwAXfxuo4WE6Plc1rGBlhgNjSTkt3GpYmnaj9KD2XzVe4ltY9VActeS7iw WRWnUKTpm9RIWHi0CZo214YNGspSUjOX35994i726nA++p88Vp9mhku1gqOrtNBlbgR7 FqALCcQgFyNEEgmA7swI1Wsz9TL6t2c/eJtU0dBWoF0A/xqkfEib6jIq+z4tjee28l/4 bckqFPtkZA1toSkljFga428o0YRqUPO6dEfiZWGudCyi6vVraNxEEuULIQ4NgO9kjo7n rX40vFzO/sawwnfnlBMXnHlXAasvLM5T8+rdxDMM74sxxBC8dh3fLDfWhQKjCf4fuDsF 2sXQ== X-Gm-Message-State: AOJu0YxmZ8C61LQGyRN8RlUe39FVYQojrk4jP47Uu0RRTufaJDDna7Vu hLaBJzsq2VZhCPZMyu/nhOje0e+4ixM0GeWefv8= X-Google-Smtp-Source: AGHT+IE+/WP/FC7LiEa4MnpNFglnHlJbYeul0msNqi5hQUjLnP0ubLugQKBNDKkqr12g0ooL+bvRKA== X-Received: by 2002:a05:6000:4c6:b0:333:1c97:48c4 with SMTP id h6-20020a05600004c600b003331c9748c4mr4481255wri.7.1702545568234; Thu, 14 Dec 2023 01:19:28 -0800 (PST) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id m11-20020adfe94b000000b00333504001acsm15470130wrn.15.2023.12.14.01.19.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 01:19:27 -0800 (PST) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Zong Li , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH -fixes v2] riscv: Check if the code to patch lies in the exit section Date: Thu, 14 Dec 2023 10:19:26 +0100 Message-Id: <20231214091926.203439-1-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Otherwise we fall through to vmalloc_to_page() which panics since the address does not lie in the vmalloc region. Fixes: 043cb41a85de ("riscv: introduce interfaces to patch kernel code") Signed-off-by: Alexandre Ghiti Reviewed-by: Charlie Jenkins --- Changes in v2: - Fix XIP rv32 build failure arch/riscv/include/asm/sections.h | 1 + arch/riscv/kernel/patch.c | 11 ++++++++++- arch/riscv/kernel/vmlinux-xip.lds.S | 2 ++ arch/riscv/kernel/vmlinux.lds.S | 2 ++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/sections.h b/arch/riscv/include/asm/sec= tions.h index 32336e8a17cb..a393d5035c54 100644 --- a/arch/riscv/include/asm/sections.h +++ b/arch/riscv/include/asm/sections.h @@ -13,6 +13,7 @@ extern char _start_kernel[]; extern char __init_data_begin[], __init_data_end[]; extern char __init_text_begin[], __init_text_end[]; extern char __alt_start[], __alt_end[]; +extern char __exittext_begin[], __exittext_end[]; =20 static inline bool is_va_kernel_text(uintptr_t va) { diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c index 13ee7bf589a1..37e87fdcf6a0 100644 --- a/arch/riscv/kernel/patch.c +++ b/arch/riscv/kernel/patch.c @@ -14,6 +14,7 @@ #include #include #include +#include =20 struct patch_insn { void *addr; @@ -25,6 +26,14 @@ struct patch_insn { int riscv_patch_in_stop_machine =3D false; =20 #ifdef CONFIG_MMU + +static inline bool is_kernel_exittext(uintptr_t addr) +{ + return system_state < SYSTEM_RUNNING && + addr >=3D (uintptr_t)__exittext_begin && + addr < (uintptr_t)__exittext_end; +} + /* * The fix_to_virt(, idx) needs a const value (not a dynamic variable of * reg-a0) or BUILD_BUG_ON failed with "idx >=3D __end_of_fixed_addresses". @@ -35,7 +44,7 @@ static __always_inline void *patch_map(void *addr, const = unsigned int fixmap) uintptr_t uintaddr =3D (uintptr_t) addr; struct page *page; =20 - if (core_kernel_text(uintaddr)) + if (core_kernel_text(uintaddr) || is_kernel_exittext(uintaddr)) page =3D phys_to_page(__pa_symbol(addr)); else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) page =3D vmalloc_to_page(addr); diff --git a/arch/riscv/kernel/vmlinux-xip.lds.S b/arch/riscv/kernel/vmlinu= x-xip.lds.S index 50767647fbc6..8c3daa1b0531 100644 --- a/arch/riscv/kernel/vmlinux-xip.lds.S +++ b/arch/riscv/kernel/vmlinux-xip.lds.S @@ -29,10 +29,12 @@ SECTIONS HEAD_TEXT_SECTION INIT_TEXT_SECTION(PAGE_SIZE) /* we have to discard exit text and such at runtime, not link time */ + __exittext_begin =3D .; .exit.text : { EXIT_TEXT } + __exittext_end =3D .; =20 .text : { _text =3D .; diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.ld= s.S index 492dd4b8f3d6..002ca58dd998 100644 --- a/arch/riscv/kernel/vmlinux.lds.S +++ b/arch/riscv/kernel/vmlinux.lds.S @@ -69,10 +69,12 @@ SECTIONS __soc_builtin_dtb_table_end =3D .; } /* we have to discard exit text and such at runtime, not link time */ + __exittext_begin =3D .; .exit.text : { EXIT_TEXT } + __exittext_end =3D .; =20 __init_text_end =3D .; . =3D ALIGN(SECTION_ALIGN); --=20 2.39.2