From patchwork Tue Feb 25 18:20:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 13990491 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 E10F71993B2; Tue, 25 Feb 2025 18:20:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740507615; cv=none; b=W+vnFNLVzN1XCyJDHf1APbbYrrdLI6apW/l4SxTl5xfhpn4p+bxJMOqrpAM9HguASuJrJyP0LLLlQ4z1l08sES/jSYiQ6sRxUxXKg4zZ+gBmUkv+YdFXg8YjWlFYdqUP9zGx5vjgLXLzoJCoqG/tIE51M37cy98kad1g7nGc5nc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740507615; c=relaxed/simple; bh=mawHT+XCFiZ2E8sUhLd3IyCVPgu+SmxgXKOIoYUw0VE=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=DuOFF5s6E8wgFk0gHN9dqYrWrKjJOJ9YtJu9P0NK9/UD+LIwcGvSftrXHh6XxUEXoahyaqhKVHEzQBciqKLbJCVLl0LIyPFpayQaKbEdJ5C8LiqoOdkpAhUlZvq/8dip2DZHz2/xv98Oame2o/S+a1jJ7OlTnlKQmZOAAnv1ZWw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63DA3C4CEDD; Tue, 25 Feb 2025 18:20:15 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1tmzY6-000000095Xv-1qYA; Tue, 25 Feb 2025 13:20:54 -0500 Message-ID: <20250225182054.290128736@goodmis.org> User-Agent: quilt/0.68 Date: Tue, 25 Feb 2025 13:20:05 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Masahiro Yamada , Catalin Marinas , Will Deacon , Nathan Chancellor , "Arnd Bergmann" , Mark Brown Subject: [PATCH 1/4] ftrace: Test mcount_loc addr before calling ftrace_call_addr() References: <20250225182004.473875894@goodmis.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Steven Rostedt The addresses in the mcount_loc can be zeroed and then moved by KASLR making them invalid addresses. ftrace_call_addr() for ARM 64 expects a valid address to kernel text. If the addr read from the mcount_loc section is invalid, it must not call ftrace_call_addr(). Move the addr check before calling ftrace_call_addr() in ftrace_process_locs(). Fixes: ef378c3b8233 ("scripts/sorttable: Zero out weak functions in mcount_loc table") Reported-by: Nathan Chancellor Reported-by: "Arnd Bergmann" Tested-by: Nathan Chancellor Closes: https://lore.kernel.org/all/20250225025631.GA271248@ax162/ Closes: https://lore.kernel.org/all/91523154-072b-437b-bbdc-0b70e9783fd0@app.fastmail.com/ Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ftrace.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 27c8def2139d..183f72cf15ed 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -7063,7 +7063,9 @@ static int ftrace_process_locs(struct module *mod, pg = start_pg; while (p < end) { unsigned long end_offset; - addr = ftrace_call_adjust(*p++); + + addr = *p++; + /* * Some architecture linkers will pad between * the different mcount_loc sections of different @@ -7075,6 +7077,8 @@ static int ftrace_process_locs(struct module *mod, continue; } + addr = ftrace_call_adjust(addr); + end_offset = (pg->index+1) * sizeof(pg->records[0]); if (end_offset > PAGE_SIZE << pg->order) { /* We should have allocated enough */ From patchwork Tue Feb 25 18:20:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 13990493 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 0A69C1A38F9; Tue, 25 Feb 2025 18:20:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740507616; cv=none; b=IHNGjTME8iIrts62oYLlctkDFI3TATZ8mteO6n/Egwql5bPLTanQ6ZzucV4AaIEQoZ1W4zQ0uVXs72ETpnZIPWUEqzar3Ag3Daz10RDgcImhfZGjYiOp7e6tqAy+gjKC3gvQTAhQO4/QPMArysB6qmXQVt42vV0GSZLKu7HzNuc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740507616; c=relaxed/simple; bh=bjXwCEFL3RTxjvXbt/lE5V7lqwwfrOXJ5cW/b9UMM8E=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=Wv+SD+LCVKPwpZvl5mPWpEhqKwdvEpOfNt3vrHWE2TuRbgKxmYqPEv/PNFgMyau9jLdRvSujcUjBzbGHHFv7Y/WEBysLUBALMhr6EV6aC8cWKzzENqYgu1kUn8Kg8lj6yPBQwffNTliuO56uSOQdrfbr6t5mMgsgYevQOVbGs2E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9EE33C4CEEE; Tue, 25 Feb 2025 18:20:15 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1tmzY6-000000095YQ-2bvl; Tue, 25 Feb 2025 13:20:54 -0500 Message-ID: <20250225182054.471759017@goodmis.org> User-Agent: quilt/0.68 Date: Tue, 25 Feb 2025 13:20:06 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Masahiro Yamada , Catalin Marinas , Will Deacon , Nathan Chancellor , "Arnd Bergmann" , Mark Brown Subject: [PATCH 2/4] ftrace: Check against is_kernel_text() instead of kaslr_offset() References: <20250225182004.473875894@goodmis.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Steven Rostedt As kaslr_offset() is architecture dependent and also may not be defined by all architectures, when zeroing out unused weak functions, do not check against kaslr_offset(), but instead check if the address is within the kernel text sections. If KASLR added a shift to the zeroed out function, it would still not be located in the kernel text. This is a more robust way to test if the text is valid or not. Fixes: ef378c3b8233 ("scripts/sorttable: Zero out weak functions in mcount_loc table") Reported-by: Nathan Chancellor Reported-by: Mark Brown Tested-by: Nathan Chancellor Closes: https://lore.kernel.org/all/20250224180805.GA1536711@ax162/ Closes: https://lore.kernel.org/all/5225b07b-a9b2-4558-9d5f-aa60b19f6317@sirena.org.uk/ Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ftrace.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 183f72cf15ed..bec7b5dbdb3b 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -7004,7 +7004,6 @@ static int ftrace_process_locs(struct module *mod, unsigned long count; unsigned long *p; unsigned long addr; - unsigned long kaslr; unsigned long flags = 0; /* Shut up gcc */ unsigned long pages; int ret = -ENOMEM; @@ -7056,9 +7055,6 @@ static int ftrace_process_locs(struct module *mod, ftrace_pages->next = start_pg; } - /* For zeroed locations that were shifted for core kernel */ - kaslr = !mod ? kaslr_offset() : 0; - p = start; pg = start_pg; while (p < end) { @@ -7072,7 +7068,18 @@ static int ftrace_process_locs(struct module *mod, * object files to satisfy alignments. * Skip any NULL pointers. */ - if (!addr || addr == kaslr) { + if (!addr) { + skipped++; + continue; + } + + /* + * If this is core kernel, make sure the address is in core + * or inittext, as weak functions get zeroed and KASLR can + * move them to something other than zero. It just will not + * move it to an area where kernel text is. + */ + if (!mod && !(is_kernel_text(addr) || is_kernel_inittext(addr))) { skipped++; continue; } From patchwork Tue Feb 25 18:20:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 13990495 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 5A8C01A9B46; Tue, 25 Feb 2025 18:20:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740507616; cv=none; b=DuqDnd3ycbN9lrhsrpXDw548Xnj/U3Ua0XgoZVBmsKfGjdF48xAadGIz2Tirt0mBzs/9NWQESXBsjd7Te6GaRPcoNan6ZMlT0+TNwVTkDUK+nxdE4kRAw1sk2Fqplo02KYaG97QeqOdGUuSnxS12Cc6uLsqa6UPgohWxVh5ZzN0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740507616; c=relaxed/simple; bh=K/F1CY7g3Rg1wpx3W23+5xHOx/IOS3QKZnW8nZXMLXE=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=pRAItC2amRNkhM5OpEk+8QQwoYWRaN5yjL4Mq0DHgaJTwT/voWH+mzm5tDwNVdNlGV1L1+qRG2KUOqSBV2Ncys6f8Jja4nGz1ieXzddWLdEamQchbDdAOdYLj9c2AQjKUgwcvdlrn4iodBZYFqUKQzU97+ULkPtGGPPk+wLR9Go= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CFFD2C4CEF1; Tue, 25 Feb 2025 18:20:15 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1tmzY6-000000095Yu-3KXv; Tue, 25 Feb 2025 13:20:54 -0500 Message-ID: <20250225182054.648398403@goodmis.org> User-Agent: quilt/0.68 Date: Tue, 25 Feb 2025 13:20:07 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Masahiro Yamada , Catalin Marinas , Will Deacon , Nathan Chancellor , "Arnd Bergmann" , Mark Brown Subject: [PATCH 3/4] scripts/sorttable: Use normal sort if theres no relocs in the mcount section References: <20250225182004.473875894@goodmis.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Steven Rostedt When ARM 64 is compiled with gcc, the mcount_loc section will be filled with zeros and the addresses will be located in the Elf_Rela sections. To sort the mcount_loc section, the addresses from the Elf_Rela need to be placed into an array and that is sorted. But when ARM 64 is compiled with clang, it does it the same way as other architectures and leaves the addresses as is in the mcount_loc section. To handle both cases, ARM 64 will first try to sort the Elf_Rela section, and if it doesn't find any functions, it will then fall back to the sorting of the addresses in the mcount_loc section itself. Fixes: b3d09d06e052 ("arm64: scripts/sorttable: Implement sorting mcount_loc at boot for arm64") Reported-by: "Arnd Bergmann" Tested-by: Nathan Chancellor Closes: https://lore.kernel.org/all/893cd8f1-8585-4d25-bf0f-4197bf872465@app.fastmail.com/ Signed-off-by: Steven Rostedt (Google) --- scripts/sorttable.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/sorttable.c b/scripts/sorttable.c index 23c7e0e6c024..07ad8116bc8d 100644 --- a/scripts/sorttable.c +++ b/scripts/sorttable.c @@ -827,9 +827,14 @@ static void *sort_mcount_loc(void *arg) pthread_exit(m_err); } - if (sort_reloc) + if (sort_reloc) { count = fill_relocs(vals, size, ehdr, emloc->start_mcount_loc); - else + /* gcc may use relocs to save the addresses, but clang does not. */ + if (!count) { + count = fill_addrs(vals, size, start_loc); + sort_reloc = 0; + } + } else count = fill_addrs(vals, size, start_loc); if (count < 0) { From patchwork Tue Feb 25 18:20:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 13990494 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 0F7B51A4E98; Tue, 25 Feb 2025 18:20:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740507616; cv=none; b=jvg2XT6KG+2PuysBmH8uCoSISJR1tjwZPiCXHcu5LhhP2TXEXFJ674chEiUccCVaZJFuzQR815BrP6ww9jWDoLYJ0u3JnpWpCJL+J1stWz/1tmeJQIurvoEWyFksuAZcz7zp0iYGL8k3hd1isZ0TBbq0dXg3SRXPx1WbSim4ETs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740507616; c=relaxed/simple; bh=YdLBlvD7KuxzSEIk+RyaPCgrtz0J+VoLSvcf9Vme8s8=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=aTCzGPdvIEiaZERP+jETJE3twA+oizEhBWsO/Z0DztPLJ4VX3lytTUn9N+Nt2zYJkszzNgvldI1Bf6aZQkgnwrbQGTYsIVVLFrpeLBUUD8lh5lnZAW0DMGODw4jdFyNGIPge6LnrWzoci4wT6cdcm0uj5jKZ+8TapaNm+6+Vc+s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E8A1EC4CEF2; Tue, 25 Feb 2025 18:20:15 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1tmzY6-000000095ZO-41vx; Tue, 25 Feb 2025 13:20:54 -0500 Message-ID: <20250225182054.815536219@goodmis.org> User-Agent: quilt/0.68 Date: Tue, 25 Feb 2025 13:20:08 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Masahiro Yamada , Catalin Marinas , Will Deacon , Nathan Chancellor , "Arnd Bergmann" , Mark Brown Subject: [PATCH 4/4] scripts/sorttable: Allow matches to functions before function entry References: <20250225182004.473875894@goodmis.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Steven Rostedt ARM 64 uses -fpatchable-function-entry=4,2 which adds padding before the function and the addresses in the mcount_loc point there instead of the function entry that is returned by nm. In order to find a function from nm to make sure it's not an unused weak function, the entries in the mcount_loc section needs to match the entries from nm. Since it can be an instruction before the entry, add a before_func variable that ARM 64 can set to 8, and if the mcount_loc entry is within 8 bytes of the nm function entry, then it will be considered a match. Fixes: ef378c3b82338 ("scripts/sorttable: Zero out weak functions in mcount_loc table") Tested-by: Nathan Chancellor Signed-off-by: Steven Rostedt (Google) --- scripts/sorttable.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/sorttable.c b/scripts/sorttable.c index 07ad8116bc8d..7b4b3714b1af 100644 --- a/scripts/sorttable.c +++ b/scripts/sorttable.c @@ -611,13 +611,16 @@ static int add_field(uint64_t addr, uint64_t size) return 0; } +/* Used for when mcount/fentry is before the function entry */ +static int before_func; + /* Only return match if the address lies inside the function size */ static int cmp_func_addr(const void *K, const void *A) { uint64_t key = *(const uint64_t *)K; const struct func_info *a = A; - if (key < a->addr) + if (key + before_func < a->addr) return -1; return key >= a->addr + a->size; } @@ -1253,6 +1256,8 @@ static int do_file(char const *const fname, void *addr) #ifdef MCOUNT_SORT_ENABLED sort_reloc = true; rela_type = 0x403; + /* arm64 uses patchable function entry placing before function */ + before_func = 8; #endif /* fallthrough */ case EM_386: