Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5858977rdb; Thu, 14 Dec 2023 01:19:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IHhP/DtEYrK6X7e22HtP8QlaM73RNYwq/PS7BNCXVFnzFkqPt/KhJVOD25PnuBksMwpxRee X-Received: by 2002:a05:6359:c83:b0:170:2e7f:bb98 with SMTP id go3-20020a0563590c8300b001702e7fbb98mr11105846rwb.55.1702545580026; Thu, 14 Dec 2023 01:19:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702545579; cv=none; d=google.com; s=arc-20160816; b=KYje+Qp7pSAXaSMYm4TfSSEtdGhGt3mEKeqq3HK3DAGEgsBstLGyH3AslM0zBVENDj IgsjaiWnSY/sRY1JiIyWIeCNJIUxKFioYvsx92L2Xri+UAnuSq7qri5NsOaoZKV/hnUq m8wq7I6875LzgG4k0scIIv97Zpp/Z6Ym1rjLUhPKNmQoHz7dG6LNeDg2xbry+NsMvnMl xNR1rV/cDpK3yGzbbadh8g0A0yu5/CMw2FesQ6qzgaF1GhaG5ODwv8kg1yu6nI35PSfB lVm7fOodEMnvGuWSFJKaAmW/9cnYXDtrgh3ToxeBf8FSUy1m/PybRHiB0Wk3uPsPzMxv T0SQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=h6KCWUfDFNx0O1wfeTExInhDSJOXnrOXPLAO9F5T2Tc=; fh=dEw3DIsbsC5LzCygmEWNQYDH8Ck0NHJuGdnYX5wQcgQ=; b=Y8F8QPK5xJzIykjcQl7Og8jLuDZO9w09M/LpQMpSydlkk9ZUAUe5ouNxZ3FxgC9W6X qR/Ki0TrfK381L1jIoaf81nKQKdougQTgfvdMD9fVs2tblA0jIKlZ/StTcS1GGiOXjQN 6wdAacPyy+qciYf9C2UjfQunxyXfP0eW3FlTQbbCcTKET9I6/wLFh0ybX/szed8JA7e7 iyKoh6y+rjaie9X3s+IyN1+lwtUT6+Q5He/l0b7xH+TTw/uU8AYtWrLD4aknsEjEWRiy NN64KctKwzqxqo9lfWYHmE+UjeEzoGfrcksuBFsM4LAfuIp0nWKzku4/XTYqPvryTAa7 ph7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=FME2U1jZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id s14-20020a17090ad48e00b0028af776869asi1586981pju.86.2023.12.14.01.19.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 01:19:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=FME2U1jZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 8F20780F5F00; Thu, 14 Dec 2023 01:19:37 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234413AbjLNJTZ (ORCPT + 99 others); Thu, 14 Dec 2023 04:19:25 -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-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: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Thu, 14 Dec 2023 01:19:37 -0800 (PST) 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 --- 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/sections.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[]; 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 struct patch_insn { void *addr; @@ -25,6 +26,14 @@ struct patch_insn { int riscv_patch_in_stop_machine = false; #ifdef CONFIG_MMU + +static inline bool is_kernel_exittext(uintptr_t addr) +{ + return system_state < SYSTEM_RUNNING && + addr >= (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 >= __end_of_fixed_addresses". @@ -35,7 +44,7 @@ static __always_inline void *patch_map(void *addr, const unsigned int fixmap) uintptr_t uintaddr = (uintptr_t) addr; struct page *page; - if (core_kernel_text(uintaddr)) + if (core_kernel_text(uintaddr) || is_kernel_exittext(uintaddr)) page = phys_to_page(__pa_symbol(addr)); else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) page = vmalloc_to_page(addr); diff --git a/arch/riscv/kernel/vmlinux-xip.lds.S b/arch/riscv/kernel/vmlinux-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 = .; .exit.text : { EXIT_TEXT } + __exittext_end = .; .text : { _text = .; diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.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 = .; } /* we have to discard exit text and such at runtime, not link time */ + __exittext_begin = .; .exit.text : { EXIT_TEXT } + __exittext_end = .; __init_text_end = .; . = ALIGN(SECTION_ALIGN); -- 2.39.2