mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zheng Yejian <zhengyejian1@huawei.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: <rostedt@goodmis.org>, <mcgrof@kernel.org>, <mhiramat@kernel.org>,
	<mark.rutland@arm.com>, <mathieu.desnoyers@efficios.com>,
	<jpoimboe@kernel.org>, <linux-modules@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>,
	<linux-trace-kernel@vger.kernel.org>, <bpf@vger.kernel.org>
Subject: Re: [RFC PATCH] ftrace: Skip __fentry__ location of overridden weak functions
Date: Tue, 11 Jun 2024 09:56:51 +0800	[thread overview]
Message-ID: <57e499a4-e26d-148f-317d-233e873d11b4@huawei.com> (raw)
In-Reply-To: <20240607150228.GR8774@noisy.programming.kicks-ass.net>

On 2024/6/7 23:02, Peter Zijlstra wrote:
> On Fri, Jun 07, 2024 at 07:52:11PM +0800, Zheng Yejian wrote:
>> ftrace_location() was changed to not only return the __fentry__ location
>> when called for the __fentry__ location, but also when called for the
>> sym+0 location after commit aebfd12521d9 ("x86/ibt,ftrace: Search for
>> __fentry__ location"). That is, if sym+0 location is not __fentry__,
>> ftrace_location() would find one over the entire size of the sym.
>>
>> However, there is case that more than one __fentry__ exist in the sym
>> range (described below) and ftrace_location() would find wrong __fentry__
>> location by binary searching, which would cause its users like livepatch/
>> kprobe/bpf to not work properly on this sym!
>>
>> The case is that, based on current compiler behavior, suppose:
>>   - function A is followed by weak function B1 in same binary file;
>>   - weak function B1 is overridden by function B2;
>> Then in the final binary file:
>>   - symbol B1 will be removed from symbol table while its instructions are
>>     not removed;
>>   - __fentry__ of B1 will be still in __mcount_loc table;
>>   - function size of A is computed by substracting the symbol address of
>>     A from its next symbol address (see kallsyms_lookup_size_offset()),
>>     but because symbol info of B1 is removed, the next symbol of A is
>>     originally the next symbol of B1. See following example, function
>>     sizeof A will be (symbol_address_C - symbol_address_A):
>>
>>       symbol_address_A
>>       symbol_address_B1 (Not in symbol table)
>>       symbol_address_C
>>
>> The weak function issue has been discovered in commit b39181f7c690
>> ("ftrace: Add FTRACE_MCOUNT_MAX_OFFSET to avoid adding weak function")
>> but it didn't resolve the issue in ftrace_location().
>>
>> There may be following resolutions:
> 
> Oh gawd, sodding weak functions again.
> 
> I would suggest changing scipts/kallsyms.c to emit readily identifiable
> symbol names for all the weak junk, eg:
> 
>    __weak_junk_NNNNN
> 

Sorry for the late reply, I just had a long noon holiday :>

scripts/kallsyms.c is compiled and used to handle symbols in vmlinux.o
or vmlinux.a, see kallsyms_step() in scripts/link-vmlinux.sh, those
overridden weak symbols has been removed from symbol table of vmlinux.o
or vmlinux.a. But we can found those symbols from original xx/xx.o file,
for example, the weak free_initmem() in in init/main.c is overridden,
its symbol is not in vmlinx but is still in init/main.o .

How about traversing all origin xx/xx.o and finding all weak junk symbols ?

> That instantly fixes the immediate problem and Steve's horrid hack can
> go away.
> 

Yes, this can be done in same patch series.

> Additionally, I would add a boot up pass that would INT3 fill all such
> functions and remove/invalidate all
> static_call/static_jump/fentry/alternative entry that is inside of them.
> 
> 
> 

--

Thanks,
Zheng Yejian

  parent reply	other threads:[~2024-06-11  1:57 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-07 11:52 Zheng Yejian
2024-06-07 15:02 ` Peter Zijlstra
2024-06-07 15:39   ` Steven Rostedt
2024-06-11  1:56   ` Zheng Yejian [this message]
2024-06-11  9:21     ` Peter Zijlstra
2024-06-11  9:36       ` Zheng Yejian
2024-11-13  7:32 ` Dropify

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=57e499a4-e26d-148f-317d-233e873d11b4@huawei.com \
    --to=zhengyejian1@huawei.com \
    --cc=bpf@vger.kernel.org \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mcgrof@kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®