bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexei Starovoitov <ast@fb.com>
To: Steven Rostedt <rostedt@goodmis.org>,
	Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: <davem@davemloft.net>, <daniel@iogearbox.net>,
	<andrii@kernel.org>, <paulmck@kernel.org>, <bpf@vger.kernel.org>,
	<kernel-team@fb.com>
Subject: Re: [PATCH v2 bpf] ftrace: Fix modify_ftrace_direct.
Date: Tue, 16 Mar 2021 12:50:23 -0700	[thread overview]
Message-ID: <12902675-a8e5-f41b-0dea-bc340b2614c7@fb.com> (raw)
In-Reply-To: <20210316154557.0c513225@gandalf.local.home>

On 3/16/21 12:45 PM, Steven Rostedt wrote:
> On Tue, 16 Mar 2021 12:10:46 -0700
> Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
> 
>> From: Alexei Starovoitov <ast@kernel.org>
>>
>> The following sequence of commands:
>>    register_ftrace_direct(ip, addr1);
>>    modify_ftrace_direct(ip, addr1, addr2);
>>    unregister_ftrace_direct(ip, addr2);
>> will cause the kernel to warn:
>> [   30.179191] WARNING: CPU: 2 PID: 1961 at kernel/trace/ftrace.c:5223 unregister_ftrace_direct+0x130/0x150
>> [   30.180556] CPU: 2 PID: 1961 Comm: test_progs    W  O      5.12.0-rc2-00378-g86bc10a0a711-dirty #3246
>> [   30.182453] RIP: 0010:unregister_ftrace_direct+0x130/0x150
>>
>> When modify_ftrace_direct() changes the addr from old to new it should update
>> the addr stored in ftrace_direct_funcs. Otherwise the final
>> unregister_ftrace_direct() won't find the address and will cause the splat.
>>
>> Fixes: 0567d6809182 ("ftrace: Add modify_ftrace_direct()")
>> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
>> ---
>> Steven,
>> I think I've changed it the way you requested. Please ack if so.
> 
> The changes look fine, but I just found another issue that needs to be
> handled as well.
> 
> 
>> @@ -5329,6 +5339,7 @@ int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
>>   int modify_ftrace_direct(unsigned long ip,
>>   			 unsigned long old_addr, unsigned long new_addr)
>>   {
>> +	struct ftrace_direct_func *direct, *new_direct;
>>   	struct ftrace_func_entry *entry;
>>   	struct dyn_ftrace *rec;
>>   	int ret = -ENODEV;
>> @@ -5344,6 +5355,20 @@ int modify_ftrace_direct(unsigned long ip,
>>   	if (entry->direct != old_addr)
>>   		goto out_unlock;
>>   
>> +	direct = ftrace_find_direct_func(old_addr);
>> +	if (WARN_ON(!direct))
>> +		goto out_unlock;
>> +	if (direct->count > 1) {
>> +		ret = -ENOMEM;
>> +		new_direct = ftrace_alloc_direct_func(new_addr);
>> +		if (!new_direct)
>> +			goto out_unlock;
>> +		direct->count--;
>> +		new_direct->count++;
>> +	} else {
>> +		direct->addr = new_addr;
>> +	}
>> +
>>   	/*
>>   	 * If there's no other ftrace callback on the rec->ip location,
>>   	 * then it can be changed directly by the architecture.
> 
> Everything looks good above, but then looking below this code we have:
> 
> 	if (ftrace_rec_count(rec) == 1) {
> 		ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
> 	} else {
> 		entry->direct = new_addr;
> 		ret = 0;
> 	}
> 
> Where if ftrace_modify_direct_caller() fails, you need to put back the
> direct descriptors to where they were.
> 
> 	struct ftrace_direct_func *new_direct = NULL;
> 
> 	[..]
> 
> 	if (unlikely(ret && new_direct)) {
> 		direct->count++;
> 		list_del_rcu(&new_direct->next);
> 		synchronize_rcu_tasks();
> 		kfree(new_direct);
> 	}
> 			
> The above is highly unlikely to happen, but it could.

Sure. Will respin.

      reply	other threads:[~2021-03-16 19:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-16 19:10 [PATCH v2 bpf] ftrace: Fix modify_ftrace_direct Alexei Starovoitov
2021-03-16 19:45 ` Steven Rostedt
2021-03-16 19:50   ` Alexei Starovoitov [this message]

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=12902675-a8e5-f41b-0dea-bc340b2614c7@fb.com \
    --to=ast@fb.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=kernel-team@fb.com \
    --cc=paulmck@kernel.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;
as well as URLs for NNTP newsgroup(s).