From patchwork Tue Jan 30 06:17:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tiezhu Yang X-Patchwork-Id: 193854 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1029843dyb; Mon, 29 Jan 2024 22:19:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IH3hl8vRZYEQj2BHLzYh0j5y9Z1i/2S7bicsneOGh8n8seFE96cHFJk+juSlzXQ24NiXeV/ X-Received: by 2002:ad4:5ba9:0:b0:68c:6216:35da with SMTP id 9-20020ad45ba9000000b0068c621635damr507455qvq.55.1706595563308; Mon, 29 Jan 2024 22:19:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706595563; cv=pass; d=google.com; s=arc-20160816; b=OKMJtLlQeje6mz6pf/y48KxePbB5/evO6KxHud1dtla46vCV7aZf+NCnfZ1H2u8qEB ORxOOY0DlN0gKMRshmlasEyaRjxSE5BFzpFgCtMvSmV83KFrzGbajo8d9urLxFYaHs+v a5/6BuHWSPMkCtB3UAP5RV5Y6z2YAF4YYt1mS8ZP3zwbzs4GSvVmiankWxCUiwU4I2GF UicEa9bjb7X3KBh64/8LjrFXhGWF+hEpaVRnxkoXuVAmI5FXVT/y7h+wCHhzftn7FC21 GT4BpdkSARs5tXSWfQnXBGw5tzO6KrUZhBvEEPENNMx9Y2NCpZbDU5Lkb39KNC/k3hd4 sP+Q== 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:references:in-reply-to:message-id :date:subject:cc:to:from; bh=SQgvXihGGZrtbo3wB7dWoYoN0BLsMDC+Bd8sGPHKuh8=; fh=3JsZntdbC0M+r2W9mBgl7JRPxEEj/7A1ld74TBnFi8o=; b=tVx3lf8ZgJQU3epg/67zAVc86Z3nVExiA9yN0TNgTYWImJBHoqu7TmBQ6+BRayAnAg ayj/W6Utnas2hUar1fVeviW7AIkcArsN9IA7jUnf6ELefKMsYZMMS80CdwjNIe0dlts6 MFmazREuYAozbGD/xLRGQkw6dkBHkF6JSL7Y7tv9zRQRDJxOKwU3zaTipDcFzLzbVbAz g1gKSSKlHjTceONE8g8Pdxxz/d5UOo8mG6vj3blxxS8HjykeoMKQ0eblnyDh/IRDVN0X Hkgh/J7KLm0dmRIYT3H16LfkLrTJG5FC1nSXSRKvSO66K3OR0p3hK4YYYY0d9USUwvQt kYtQ== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=loongson.cn); spf=pass (google.com: domain of linux-kernel+bounces-43983-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43983-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id gm9-20020a056214268900b0068c3b604a86si7110685qvb.155.2024.01.29.22.19.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 22:19:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-43983-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=loongson.cn); spf=pass (google.com: domain of linux-kernel+bounces-43983-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43983-ouuuleilei=gmail.com@vger.kernel.org" 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 F40E91C21A38 for ; Tue, 30 Jan 2024 06:19:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2D84054664; Tue, 30 Jan 2024 06:17:46 +0000 (UTC) Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D8DEA524AC for ; Tue, 30 Jan 2024 06:17:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706595464; cv=none; b=s0MqtXj2WUFUC5Ed2h7kJzmifli/W6iyWWIW56m0l1e8V/4LQa+ATmPg8cwQAXSKzeqyHdW19nvCdLachK6PElg/OU1QSsByCdB0o1jOKBDYyLhTy/fknD0dwDwWfKfQY7KhXH/wz1pl/PM/WNmqFSQKbsytQ94zsSS7a+taOEk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706595464; c=relaxed/simple; bh=Im8sg4sg0+MkTbsC0WSX5gry32M+Ugq4I0b6kEVX5D4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vu2Ip1E0NDIQyLtagVcGPNJT/e+kw3xr9FnTexAD2i5Jd71sVYhMKYAuwpXPPTvfqul7DK14vcy31ajEUjbVWwdSD3Hy7V8Iz85TenbuEG8oc9ikGd2Dn7dUUA6SO1hj9r6izsiQYoS6iRpqj6zj083apYfrhvXgrrDpQtrOAvc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8BxTOmElLhlHjYIAA--.15174S3; Tue, 30 Jan 2024 14:17:40 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxnhN7lLhlYXQnAA--.34034S10; Tue, 30 Jan 2024 14:17:38 +0800 (CST) From: Tiezhu Yang To: Josh Poimboeuf , Peter Zijlstra , Huacai Chen Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v6 8/8] LoongArch: Add kernel livepatching support Date: Tue, 30 Jan 2024 14:17:30 +0800 Message-ID: <20240130061730.21118-9-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240130061730.21118-1-yangtiezhu@loongson.cn> References: <20240130061730.21118-1-yangtiezhu@loongson.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: AQAAf8BxnhN7lLhlYXQnAA--.34034S10 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWxXw4rGFWxZFWfAw1ktr4rCrX_yoW7Jr17pr sxAFnxtr4UWF4Iqr9FkFyFgr98Xw1kurWagF9xKa4rCF9rZ340gr1kuFyDZFWUZ395C34x Wr1rKa4DWa15A3XCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Fb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWr XVW3AwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7V AKI48JMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v2 6r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17 CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26ryj6F1UMIIF 0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU0E38UUUUUU== X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789495149502097785 X-GMAIL-MSGID: 1789495149502097785 The arch-specified function ftrace_regs_set_instruction_pointer() has been implemented in arch/loongarch/include/asm/ftrace.h, so here only implement arch_stack_walk_reliable() function. Here are the test logs: [root@linux fedora]# cat /proc/cmdline BOOT_IMAGE=/vmlinuz-6.8.0-rc2 root=/dev/sda3 [root@linux fedora]# modprobe livepatch-sample [root@linux fedora]# cat /proc/cmdline this has been live patched [root@linux fedora]# echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled [root@linux fedora]# rmmod livepatch_sample [root@linux fedora]# cat /proc/cmdline BOOT_IMAGE=/vmlinuz-6.8.0-rc2 root=/dev/sda3 [root@linux fedora]# dmesg -t | tail -5 livepatch: enabling patch 'livepatch_sample' livepatch: 'livepatch_sample': starting patching transition livepatch: 'livepatch_sample': patching complete livepatch: 'livepatch_sample': starting unpatching transition livepatch: 'livepatch_sample': unpatching complete Co-developed-by: Jinyang He Signed-off-by: Jinyang He Signed-off-by: Tiezhu Yang --- arch/loongarch/Kconfig | 4 +++ arch/loongarch/include/asm/thread_info.h | 2 ++ arch/loongarch/kernel/stacktrace.c | 41 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index 17be0bff0f40..ecdc685daeea 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -132,6 +132,7 @@ config LOONGARCH select HAVE_KPROBES_ON_FTRACE select HAVE_KRETPROBES select HAVE_KVM + select HAVE_LIVEPATCH select HAVE_MOD_ARCH_SPECIFIC select HAVE_NMI select HAVE_OBJTOOL if AS_HAS_EXPLICIT_RELOCS @@ -141,6 +142,7 @@ config LOONGARCH select HAVE_PERF_USER_STACK_DUMP select HAVE_PREEMPT_DYNAMIC_KEY select HAVE_REGS_AND_STACK_ACCESS_API + select HAVE_RELIABLE_STACKTRACE if UNWINDER_ORC select HAVE_RETHOOK select HAVE_RSEQ select HAVE_RUST @@ -695,6 +697,8 @@ config KASAN_SHADOW_OFFSET default 0x0 depends on KASAN +source "kernel/livepatch/Kconfig" + menu "Power management options" config ARCH_SUSPEND_POSSIBLE diff --git a/arch/loongarch/include/asm/thread_info.h b/arch/loongarch/include/asm/thread_info.h index 8cb653d49a54..8bf0e6f51546 100644 --- a/arch/loongarch/include/asm/thread_info.h +++ b/arch/loongarch/include/asm/thread_info.h @@ -86,6 +86,7 @@ register unsigned long current_stack_pointer __asm__("$sp"); #define TIF_LASX_CTX_LIVE 18 /* LASX context must be preserved */ #define TIF_USEDLBT 19 /* LBT was used by this task this quantum (SMP) */ #define TIF_LBT_CTX_LIVE 20 /* LBT context must be preserved */ +#define TIF_PATCH_PENDING 21 /* pending live patching update */ #define _TIF_SIGPENDING (1<regs[3] = (unsigned long)__builtin_frame_address(0); + regs->csr_era = (unsigned long)__builtin_return_address(0); + } else { + regs->regs[3] = thread_saved_fp(task); + regs->csr_era = thread_saved_ra(task); + } + regs->regs[1] = 0; + regs->regs[22] = 0; + + for (unwind_start(&state, task, regs); + !unwind_done(&state) && !unwind_error(&state); + unwind_next_frame(&state)) { + addr = unwind_get_return_address(&state); + + /* + * A NULL or invalid return address probably means there's some + * generated code which __kernel_text_address() doesn't know about. + */ + if (!addr) + return -EINVAL; + + if (!consume_entry(cookie, addr)) + return -EINVAL; + } + + /* Check for stack corruption */ + if (unwind_error(&state)) + return -EINVAL; + + return 0; +} + static int copy_stack_frame(unsigned long fp, struct stack_frame *frame) {