From patchwork Tue Jul 23 06:32:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zheng Yejian X-Patchwork-Id: 13739393 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F11C514A0A0; Tue, 23 Jul 2024 06:32:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721716329; cv=none; b=Dcm1vPt1N1BxSm98vFcMN3wBvFCUdOjOG7D5CYAbk2qyumsscQpJrypfcFYWTF9OqM9MdV1aZ1soqDxsKZyqHLCq2FrkunQ+HyorMlXtLTHscLTL5R6D6mFZhG7i/hNWaz48+GBu9QPq5wXzO8kKgcY1R0xqKWsRy6vxkKFXHyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721716329; c=relaxed/simple; bh=zn8p2mp8QXQP5W9qSz5N2++LCfixPhpzw/topBgjCvw=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=clqIY786lS5BmpimiLJVLm5A4iz8CQZkHFs/W9IJUkPG6HC20oA2JvkptjpJ60gNrB2pntcVRLQrp8csNJPx+1B07eG8KZMP+Oi+usLnEJQxpHg2bjVYACKbSbVlbIOKzU5LJU2YxOsazSZ3PvO6uJ7OAbtvtwjE7BSKpWvVWig= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4WSnNz5nhQz4f3lCp; Tue, 23 Jul 2024 14:31:47 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 50B5E1A140D; Tue, 23 Jul 2024 14:32:01 +0800 (CST) Received: from localhost.localdomain (unknown [10.67.175.61]) by APP2 (Coremail) with SMTP id Syh0CgA34wpOTp9mjImuAw--.48686S2; Tue, 23 Jul 2024 14:31:59 +0800 (CST) From: Zheng Yejian To: masahiroy@kernel.org, peterz@infradead.org, rostedt@goodmis.org, mhiramat@kernel.org, mark.rutland@arm.com, mpe@ellerman.id.au, npiggin@gmail.com, christophe.leroy@csgroup.eu, naveen.n.rao@linux.ibm.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, mcgrof@kernel.org, mathieu.desnoyers@efficios.com, nathan@kernel.org, nicolas@fjasle.eu, ojeda@kernel.org, akpm@linux-foundation.org, surenb@google.com, pasha.tatashin@soleen.com, kent.overstreet@linux.dev, james.clark@arm.com, jpoimboe@kernel.org Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, bpf@vger.kernel.org, zhengyejian@huaweicloud.com Subject: [PATCH v2 0/5] kallsyms: Emit symbol for holes in text and fix weak function issue Date: Tue, 23 Jul 2024 14:32:53 +0800 Message-Id: <20240723063258.2240610-1-zhengyejian@huaweicloud.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: Syh0CgA34wpOTp9mjImuAw--.48686S2 X-Coremail-Antispam: 1UD129KBjvJXoWxWrW7ur4kXFWktryDur45KFg_yo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rp/ Background of this patch set can be found in v1: https://lore.kernel.org/all/20240613133711.2867745-1-zhengyejian1@huawei.com/ Here add a reproduction to show the impact to livepatch: 1. Add following hack to make livepatch-sample.ko do patch on do_one_initcall() which has an overriden weak function behind in vmlinux, then print the actually used __fentry__ location: diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c index 90408500e5a3..20b75e7b1771 100644 --- a/kernel/livepatch/patch.c +++ b/kernel/livepatch/patch.c @@ -173,6 +173,8 @@ static int klp_patch_func(struct klp_func *func) unsigned long ftrace_loc; ftrace_loc = ftrace_location((unsigned long)func->old_func); + pr_info("%s: old_func=%pS ftrace_loc=%pS (%lx)\n", __func__, + func->old_func, (void *)ftrace_loc, ftrace_loc); if (!ftrace_loc) { pr_err("failed to find location for function '%s'\n", func->old_name); diff --git a/samples/livepatch/livepatch-sample.c b/samples/livepatch/livepatch-sample.c index cd76d7ebe598..c6e8e78e23b8 100644 --- a/samples/livepatch/livepatch-sample.c +++ b/samples/livepatch/livepatch-sample.c @@ -38,7 +38,7 @@ static int livepatch_cmdline_proc_show(struct seq_file *m, void *v) static struct klp_func funcs[] = { { - .old_name = "cmdline_proc_show", + .old_name = "do_one_initcall", .new_func = livepatch_cmdline_proc_show, }, { } }; 2. We can see the size of do_one_initcall() is 0x27e: $ nm -nS vmlinux | grep do_one_initcall ffffffff810021e0 000000000000027e T do_one_initcall 3. Insert the livepatch ko, the expected ftrace_loc is do_one_initcall+0x4/0x27e but actually do_one_initcall+0x294/0x2c0 which is invalid, also its size is inaccurate. Then this livepatch can not properly work!!! # insmod livepatch-sample.ko [ 17.942451] livepatch: klp_patch_func: old_func=do_one_initcall+0x0/0x2c0 ftrace_loc=do_one_initcall+0x294/0x2c0 (ffffffff81002474) # # cat /sys/kernel/tracing/available_filter_functions_addrs | grep ffffffff81002474 ffffffff81002474 __ftrace_invalid_address___660 After this patch, this issue is gone: # insmod livepatch-sample.ko [ 42.408632] livepatch: klp_patch_func: old_func=do_one_initcall+0x0/0x27e ftrace_loc=do_one_initcall+0x4/0x27e (ffffffff810021e4) # cat /sys/kernel/tracing/available_filter_functions_addrs | grep ffffffff810021e4 ffffffff810021e4 do_one_initcall # cat /sys/kernel/tracing/enabled_functions do_one_initcall (1) I M tramp: 0xffffffffa0020000 (klp_ftrace_handler+0x0/0x258)->klp_ftrace_handler+0x0/0x258 Changes: v1->v2: - Drop patch which titled "kallsyms: Optimize multiple times of realloc() to one time of malloc()" as suggested by Masahiro; Link: https://lore.kernel.org/all/CAK7LNAQkSnZ1nVXiZH9kg52H-A_=urcsv-W7wGXvunMGhGX8Vw@mail.gmail.com/ - Changes in PATCH 1/5: - Change may_exist_hole_after_symbol() to has_hole() and return bool type; - User realloc instead of malloc another table in emit_hole_symbols(); Link: https://lore.kernel.org/all/CAK7LNAQaLc6aDK85qQtPHoCkQSGyL-TxXjpgJTfehe2Q1=jMSA@mail.gmail.com/ - Use empty symbol type/name as a special case to represent the hole instead of using symbol "t__hole_symbol_XXXXX"; Link: https://lore.kernel.org/all/CAK7LNARiR5z9hPRG932T7YjRWqkX_qZ7WKmbxx7iTo2w5YJojQ@mail.gmail.com/ - Changes in PATCH 3/5: - Now name of hole symbol is NULL, so if __fentry__ is located in a hole, kallsyms_lookup() find an NULL name then will return 0, so drop the needless kallsyms_is_hole_symbol(). Zheng Yejian (5): kallsyms: Emit symbol at the holes in the text module: kallsyms: Determine exact function size ftrace: Skip invalid __fentry__ in ftrace_process_locs() ftrace: Fix possible out-of-bound issue in ftrace_process_locs() ftrace: Revert the FTRACE_MCOUNT_MAX_OFFSET workaround arch/powerpc/include/asm/ftrace.h | 7 -- arch/x86/include/asm/ftrace.h | 7 -- include/linux/module.h | 14 +++ kernel/module/kallsyms.c | 42 ++++++-- kernel/trace/ftrace.c | 171 ++++++------------------------ scripts/kallsyms.c | 95 ++++++++++++++++- scripts/link-vmlinux.sh | 4 +- scripts/mksysmap | 2 +- 8 files changed, 173 insertions(+), 169 deletions(-)