From: Jiri Olsa <olsajiri@gmail.com>
To: Oleg Nesterov <oleg@redhat.com>
Cc: Jiri Olsa <olsajiri@gmail.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
bpf@vger.kernel.org, Song Liu <songliubraving@fb.com>,
Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>,
"Borislav Petkov (AMD)" <bp@alien8.de>,
x86@kernel.org
Subject: Re: [PATCH RFC bpf-next 4/3] uprobe: ensure sys_uretprobe uses sysret
Date: Thu, 21 Mar 2024 21:29:34 +0100 [thread overview]
Message-ID: <ZfyYriG3ttxbkpix@krava> (raw)
In-Reply-To: <20240321121456.GC14646@redhat.com>
On Thu, Mar 21, 2024 at 01:14:56PM +0100, Oleg Nesterov wrote:
> On 03/21, Jiri Olsa wrote:
> >
> > On Thu, Mar 21, 2024 at 11:17:51AM +0100, Oleg Nesterov wrote:
> > > On 03/21, Jiri Olsa wrote:
> > > >
> > > > On Wed, Mar 20, 2024 at 04:28:48PM +0100, Oleg Nesterov wrote:
> > > >
> > > > SNIP
> > > >
> > > > > SYSCALL_DEFINE0(uretprobe)
> > > > > {
> > > > > struct pt_regs *regs = task_pt_regs(current);
> > > > > unsigned long err, ip, sp, r11_cx_ax[3];
> > > > >
> > > > > err = copy_from_user(r11_cx_ax, (void __user*)regs->sp, sizeof(r11_cx_ax));
> > > > > WARN_ON_ONCE(err);
> > > > >
> > > > > // Q1: apart from ax, do we really care?
> > > > > // expose the "right" values of r11/cx/ax/sp to uprobe_consumer's
> > > > > regs->r11 = r11_cx_ax[0];
> > > > > regs->cx = r11_cx_ax[1];
> > > > > regs->ax = r11_cx_ax[2];
> > > > > regs->sp += sizeof(r11_cx_ax);
> > > > > regs->orig_ax = -1;
> > > > >
> > > > > ip = regs->ip;
> > > > > sp = regs->sp;
> > > > >
> > > > > uprobe_handle_trampoline(regs);
> > > > >
> > > > > // Q2: is it possible? do we care?
> > > > > // uprobe_consumer has changed sp, we can do nothing,
> > > > > // just return via iret.
> > > > > if (regs->sp != sp)
> > > > > return regs->ax;
> > > > > regs->sp -= sizeof(r11_cx_ax);
> > > > >
> > > > > // Q3: is it possible? do we care?
> > > > > // for the case uprobe_consumer has changed r11/cx
> > > > > r11_cx_ax[0] = regs->r11;
> > > > > r11_cx_ax[1] = regs->cx;
> > > >
> > > > I wonder we could add test for this as well, and check we return
> > > > proper register values in case the consuer changed them, will check
> > > >
> > > > >
> > > > > // comment to explain this hack
> > > > > r11_cx_ax[2] = regs->ip;
> > > > > regs->ip = ip;
> > > >
> > > > we still need restore regs->ip in case do_syscall_64 decides to do
> > > > iret for some reason, right?
> > >
> > > I don't understand... could you spell?
> >
> > I was wondering why to restore regs->ip for sysret path, but do_syscall_64
> > can decide to do iret return (for which we need proper regs->ip) even if we
> > prepare cx/r11 registers for sysexit
>
> Still don't understand... Yes, we prepare cx/r11 to avoid iret if possible.
> But (apart from performance) we do not care if do_syscall_64() picks iret.
> Either way
>
> regs->ip = ip;
>
> above ensures that usermode returns to uretprobe_syscall_entry right after
> the syscall insn.
hm, I think above ensures that do_syscall_64 will skip the 'regs->cx != regs->ip'
check.. and after the sysret returns to rcx register value and ignores regs->ip
but in any case we need to set it
> ... Then popq %r11/cx will restore r11/cx even if they were
> changed by uprobe_consumer's. And then "retq" will return to the address
> "returned" by handle_trampoline(regs) because we do
>
> // comment to explain this hack
> r11_cx_ax[2] = regs->ip;
>
> after handle_trampoline(). This all doesn't depend on iret-or-sysret.
>
> OK, I am sure you understand this, so I guess I misunderstood your concerns.
thanks for the patience ;-)
jirka
next prev parent reply other threads:[~2024-03-21 20:29 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-18 9:31 [PATCH RFC bpf-next 0/3] uprobe: uretprobe speed up Jiri Olsa
2024-03-18 9:31 ` [PATCH RFC bpf-next 1/3] uprobe: Add uretprobe syscall to speed up return probe Jiri Olsa
2024-03-18 14:22 ` Oleg Nesterov
2024-03-19 1:11 ` Andrii Nakryiko
2024-03-19 6:32 ` Oleg Nesterov
2024-03-19 16:20 ` Andrii Nakryiko
2024-03-19 10:54 ` Jiri Olsa
2024-03-18 9:31 ` [PATCH RFC bpf-next 2/3] selftests/bpf: Add uretprobe syscall test Jiri Olsa
2024-03-19 1:16 ` Andrii Nakryiko
2024-03-19 11:09 ` Jiri Olsa
2024-03-18 9:31 ` [PATCH RFC bpf-next 3/3] selftests/bpf: Mark uprobe trigger functions with nocf_check attribute Jiri Olsa
2024-03-19 1:22 ` Andrii Nakryiko
2024-03-19 11:11 ` Jiri Olsa
2024-03-22 13:40 ` Jiri Olsa
2024-03-19 10:25 ` [PATCH RFC bpf-next 4/3] uprobe: ensure sys_uretprobe uses sysret Oleg Nesterov
2024-03-19 11:08 ` Jiri Olsa
2024-03-19 16:25 ` Andrii Nakryiko
2024-03-19 16:38 ` Oleg Nesterov
2024-03-19 19:35 ` Jiri Olsa
2024-03-19 19:31 ` Jiri Olsa
2024-03-19 20:13 ` Andrii Nakryiko
2024-03-20 11:04 ` Jiri Olsa
2024-03-20 14:37 ` Oleg Nesterov
2024-03-20 15:28 ` Oleg Nesterov
2024-03-20 17:44 ` Andrii Nakryiko
2024-03-20 19:08 ` Jiri Olsa
2024-03-21 10:10 ` Oleg Nesterov
2024-03-21 9:59 ` Jiri Olsa
2024-03-21 10:17 ` Oleg Nesterov
2024-03-21 10:52 ` Jiri Olsa
2024-03-21 12:14 ` Oleg Nesterov
2024-03-21 20:29 ` Jiri Olsa [this message]
2024-03-22 8:48 ` Oleg Nesterov
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=ZfyYriG3ttxbkpix@krava \
--to=olsajiri@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bp@alien8.de \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=john.fastabend@gmail.com \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
--cc=songliubraving@fb.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
--cc=yhs@fb.com \
/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