From nobody Thu Dec 18 19:25:10 2025 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 5409F4C96 for ; Wed, 26 Feb 2025 01:05:37 +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=1740531937; cv=none; b=Sn1QRZSrZfGY8Jrpm52JdxoCyETE1pdhnZalr5gK5Ug3D3cD0bakuUra+TVZIIu8+vC0P8CraJJSVoGRGcnsv1H9ytgcvuj3/M5IBmeyL//Q/e2/tFrSdyyemHDm5Olh0KmaoSNwTuRnTsJ4hqjCOXivkGReLmCbxfX6mdqJMaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740531937; c=relaxed/simple; bh=X6zLOLeQZJjOVQdhs+fZU6VdfZOuMQ2OC0ArLkxixdM=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=D18kyYLw42bHu4KI76FPNIXbZqsvrkAqy0Iq8FRpoLy/Y27TSfXlW9tAJVJPAXIQ1497qDW9s8dY7k3Ub0ftu5s8jtQoka6/cIis4KFd2Nheo6eiI4fRP6KoYu/MdaT+6jKuTF5JBNEKfX/BjrjTxIuELEIoLmOV9mU/JekaEok= 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 DEFD6C4CEE6; Wed, 26 Feb 2025 01:05:36 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1tn5sO-00000009EYl-2RGr; Tue, 25 Feb 2025 20:06:16 -0500 Message-ID: <20250226010616.432688791@goodmis.org> User-Agent: quilt/0.68 Date: Tue, 25 Feb 2025 20:05:57 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Masahiro Yamada , Catalin Marinas , Will Deacon , Mark Brown , Nathan Chancellor , "Arnd Bergmann" Subject: [for-next][PATCH 1/4] ftrace: Test mcount_loc addr before calling ftrace_call_addr() References: <20250226010556.526552688@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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(). Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Andrew Morton Cc: Masahiro Yamada Cc: Catalin Marinas Cc: Will Deacon Cc: Mark Brown Link: https://lore.kernel.org/20250225182054.290128736@goodmis.org 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@ap= p.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 =3D start_pg; while (p < end) { unsigned long end_offset; - addr =3D ftrace_call_adjust(*p++); + + addr =3D *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; } =20 + addr =3D ftrace_call_adjust(addr); + end_offset =3D (pg->index+1) * sizeof(pg->records[0]); if (end_offset > PAGE_SIZE << pg->order) { /* We should have allocated enough */ --=20 2.47.2 From nobody Thu Dec 18 19:25:10 2025 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 6C8146BB5B for ; Wed, 26 Feb 2025 01:05:37 +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=1740531937; cv=none; b=C1hCGFY+7iqrB4jqe5tH91lbzB9QEGxOYxODraPC6CRVjZGsJpcJ5fIaIR/9/3vAxLdW9ciHd66gTc5cPpuwWMF7VnCxrGK7QkSCsvtkBH8ERZqZqm5GhOGVs3ShvqMGmqIHAoODoRYzhpeCaACxKAlwSeYlAq9m8IhWgmx6IHM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740531937; c=relaxed/simple; bh=npe4+D3FjjabDkZCU8vHG/H5KpBHyP6JcROnGMMiG14=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=mOZxDlvyvTVRUplR7n2bbbciSxSkrSaV2L6Nt0vJ/syDnvle9ExX8z8lSo0FmQhEzYIr5WbIH3k93c8OgyYdxYF/NWp3NFV+ooSqZzvegxHGieVOEc+Ai2Qde3wNHxsRclXezl8tnGkiQgV8FcXW8ocjkIaEoQXoOToglUYH7cc= 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 10FE5C4CEEB; Wed, 26 Feb 2025 01:05:37 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1tn5sO-00000009EZG-3A40; Tue, 25 Feb 2025 20:06:16 -0500 Message-ID: <20250226010616.603055165@goodmis.org> User-Agent: quilt/0.68 Date: Tue, 25 Feb 2025 20:05:58 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Masahiro Yamada , Catalin Marinas , Will Deacon , "Arnd Bergmann" , Nathan Chancellor , Mark Brown Subject: [for-next][PATCH 2/4] ftrace: Check against is_kernel_text() instead of kaslr_offset() References: <20250226010556.526552688@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Andrew Morton Cc: Masahiro Yamada Cc: Catalin Marinas Cc: Will Deacon Cc: "Arnd Bergmann" Link: https://lore.kernel.org/20250225182054.471759017@goodmis.org 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@si= rena.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 =3D 0; /* Shut up gcc */ unsigned long pages; int ret =3D -ENOMEM; @@ -7056,9 +7055,6 @@ static int ftrace_process_locs(struct module *mod, ftrace_pages->next =3D start_pg; } =20 - /* For zeroed locations that were shifted for core kernel */ - kaslr =3D !mod ? kaslr_offset() : 0; - p =3D start; pg =3D 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 =3D=3D 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; } --=20 2.47.2 From nobody Thu Dec 18 19:25:10 2025 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 BB3B215573A for ; Wed, 26 Feb 2025 01:05:37 +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=1740531937; cv=none; b=HtLMIsEx4w80SDQ5Xkwo3JX5JPdtYz6G/zDEbQE3eH6kBM46+/w+t0zEQyvq0kCT/doHP8sX+Cun2KJRGTBGEhBsycCLjfClCUt6QvugN9xIq5uduq6T5i3hvPCOS6P/dtTLWFJLQbU2jMKAiUg19w0vRQEPAcuDY0PTQqYMmQQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740531937; c=relaxed/simple; bh=lxxp7ttCQOpNKNUOpZILzSwscCviuGV0JaaK1i0JVNA=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=aA+IJoTxyHflQDpm5hnGhHDPt4P+we4xebK3B/gS/olX8MmmCvJjRdBrmuBmHW7roNYVRV3L8fAVCyHtpOdMmHlsJqOI979aV/MJt9EFAKqWsnaRdrS1O/brXDZl2Tv/Pg43FR0/zgzqNw8j0KWplf+/ExVXzH8Wxg0bKy2O7n8= 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 2BECFC4CEEF; Wed, 26 Feb 2025 01:05:37 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1tn5sO-00000009EZk-3s6f; Tue, 25 Feb 2025 20:06:16 -0500 Message-ID: <20250226010616.775247087@goodmis.org> User-Agent: quilt/0.68 Date: Tue, 25 Feb 2025 20:05:59 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Masahiro Yamada , Catalin Marinas , Will Deacon , Mark Brown , "Arnd Bergmann" , Nathan Chancellor Subject: [for-next][PATCH 3/4] scripts/sorttable: Use normal sort if theres no relocs in the mcount section References: <20250226010556.526552688@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Andrew Morton Cc: Masahiro Yamada Cc: Catalin Marinas Cc: Will Deacon Cc: Mark Brown Link: https://lore.kernel.org/20250225182054.648398403@goodmis.org Fixes: b3d09d06e052 ("arm64: scripts/sorttable: Implement sorting mcount_lo= c at boot for arm64") Reported-by: "Arnd Bergmann" Tested-by: Nathan Chancellor Closes: https://lore.kernel.org/all/893cd8f1-8585-4d25-bf0f-4197bf872465@ap= p.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); } =20 - if (sort_reloc) + if (sort_reloc) { count =3D 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 =3D fill_addrs(vals, size, start_loc); + sort_reloc =3D 0; + } + } else count =3D fill_addrs(vals, size, start_loc); =20 if (count < 0) { --=20 2.47.2 From nobody Thu Dec 18 19:25:10 2025 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 BB3F715575C for ; Wed, 26 Feb 2025 01:05:37 +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=1740531937; cv=none; b=myfoYnyvDalWXJW2L0upILeVSFd5vvVgnoxS6dPldndb8qeqLkviBfdk2V1ZTgdcJrn0PLqJUu1+YRpW1/Zp6CekcLQHc0QdrZeOX1FdSlJiOKs9jcaqkeJWZil6h7yJqvpiarYgkEzZmjfLf4XQdIN1kg/El7LTiG9D7Vj+xNI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740531937; c=relaxed/simple; bh=bN4wLmU21n6ebSzvaJ8q1Sy1XYCq7ySgO0WZX8FrzWQ=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=QTnK1Ni1MlOPaO7M/VBsuwVsbJPeUq0RcMeBl471ARFwnN6vkTap/kgwc+HUaTdoAjeqdBpwflKag4NOowKflEGrkjXEQFh48np1uV9ci5bp3kxkDvyuw9RGUuTp3aNYSkSbUpS/UcnU+WytwDnUZv/Nxj4HjlnkDAarCbbIZLk= 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 66B4CC4CEE8; Wed, 26 Feb 2025 01:05:37 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1tn5sP-00000009EaF-0O2Y; Tue, 25 Feb 2025 20:06:17 -0500 Message-ID: <20250226010616.944876524@goodmis.org> User-Agent: quilt/0.68 Date: Tue, 25 Feb 2025 20:06:00 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Masahiro Yamada , Catalin Marinas , Will Deacon , "Arnd Bergmann" , Mark Brown , Nathan Chancellor Subject: [for-next][PATCH 4/4] scripts/sorttable: Allow matches to functions before function entry References: <20250226010556.526552688@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt ARM 64 uses -fpatchable-function-entry=3D4,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. Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Andrew Morton Cc: Masahiro Yamada Cc: Catalin Marinas Cc: Will Deacon Cc: "Arnd Bergmann" Cc: Mark Brown Link: https://lore.kernel.org/20250225182054.815536219@goodmis.org 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; } =20 +/* 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 =3D *(const uint64_t *)K; const struct func_info *a =3D A; =20 - if (key < a->addr) + if (key + before_func < a->addr) return -1; return key >=3D a->addr + a->size; } @@ -1253,6 +1256,8 @@ static int do_file(char const *const fname, void *add= r) #ifdef MCOUNT_SORT_ENABLED sort_reloc =3D true; rela_type =3D 0x403; + /* arm64 uses patchable function entry placing before function */ + before_func =3D 8; #endif /* fallthrough */ case EM_386: --=20 2.47.2