From: Kui-Feng Lee <sinquersw@gmail.com>
To: Andrei Matei <andreimatei1@gmail.com>
Cc: Song Liu <song@kernel.org>, Jiri Olsa <olsajiri@gmail.com>,
bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
lsf-pc@lists.linux-foundation.org,
Andrii Nakryiko <andrii.nakryiko@gmail.com>,
Yonghong Song <yonghong.song@linux.dev>,
Oleg Nesterov <oleg@redhat.com>,
Daniel Borkmann <daniel@iogearbox.net>
Subject: Re: [LSF/MM/BPF TOPIC] faster uprobes
Date: Tue, 12 Mar 2024 22:42:09 -0700 [thread overview]
Message-ID: <c999d7ce-4c49-4b4e-9e4e-07ed4efc7b79@gmail.com> (raw)
In-Reply-To: <CABWLses3HSLcYYHp64nb4CLxq9t7tifh9VzpSAnjnDDAGvr1UQ@mail.gmail.com>
On 3/12/24 18:32, Andrei Matei wrote:
> On Tue, Mar 12, 2024 at 1:16 PM Kui-Feng Lee <sinquersw@gmail.com> wrote:
>>
>>
>>
>> On 3/8/24 07:43, Andrei Matei wrote:
>>> On Thu, Mar 7, 2024 at 6:02 PM Kui-Feng Lee <sinquersw@gmail.com> wrote:
>>>>
>>>>
>>>>
>>>> On 3/5/24 15:53, Song Liu wrote:
>>>>> On Tue, Mar 5, 2024 at 9:18 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>>>>>>
>>>>>> On Fri, Mar 01, 2024 at 11:39:03AM -0800, Kui-Feng Lee wrote:
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On 2/29/24 06:39, Jiri Olsa wrote:
>>>>>>>> One of uprobe pain points is having slow execution that involves
>>>>>>>> two traps in worst case scenario or single trap if the original
>>>>>>>> instruction can be emulated. For return uprobes there's one extra
>>>>>>>> trap on top of that.
>>>>>>>>
>>>>>>>> My current idea on how to make this faster is to follow the optimized
>>>>>>>> kprobes and replace the normal uprobe trap instruction with jump to
>>>>>>>> user space trampoline that:
>>>>>>>>
>>>>>>>> - executes syscall to call uprobe consumers callbacks
>>>>>>>> - executes original instructions
>>>>>>>> - jumps back to continue with the original code
>>>>>>>>
>>>>>>>> There are of course corner cases where above will have trouble or
>>>>>>>> won't work completely, like:
>>>>>>>>
>>>>>>>> - executing original instructions in the trampoline is tricky wrt
>>>>>>>> rip relative addressing
>>>>>>>>
>>>>>>>> - some instructions we can't move to trampoline at all
>>>>>>>>
>>>>>>>> - the uprobe address is on page boundary so the jump instruction to
>>>>>>>> trampoline would span across 2 pages, hence the page replace won't
>>>>>>>> be atomic, which might cause issues
>>>>>>>>
>>>>>>>> - ... ? many others I'm sure
>>>>>>>>
>>>>>>>> Still with all the limitations I think we could be able to speed up
>>>>>>>> some amount of the uprobes, which seems worth doing.
>>>>>>>
>>>>>>> Just a random idea related to this.
>>>>>>> Could we also run jit code of bpf programs in the user space to collect
>>>>>>> information instead of going back to the kernel every time?
>>>>>
>>>>> I was thinking about a similar idea. I guess these user space BPF
>>>>> programs will have limited features that we can probably use them
>>>>> update bpf maps. For this limited scope, we still need bpf_arena.
>>>>> Otherwise, the user space bpf program will need to update the bpf
>>>>> maps with sys_bpf(), which adds the same overhead as triggering
>>>>
>>>> That is true. However, even without bpf_arena, it still works with
>>>> some workarounds without going through sys_bpf().
>>>
>>> Anything making uprobes faster would be very welcomed for my project. The
>>> biggest performance problem for us is the cost of bpf_probe_read_user()
>>> relative to raw memory access. Every call to this helper walks the process'
>>
>> "raw memory access"? Do you mean not going through any helper function,
>> reading from a pointer directly?
>
> Right.
> I recognize that, as long as bpf runs "in the kernel", one cannot simply
> dereference a user-space pointer since the kernel is a different virtual memory
> space (*). Still, I wish there bpf_probe_read_user() were faster.
>
> (*) Or, is it indeed a different memory space or is the kernel's virtual
> address space mapped into every process? Did this change through KPTI? I would
> be curious to read a good resource on what exactly it means to switch from
> user-space to the kernel and back, if such a thing exists.
FYI! This is architecture dependent. AFAIK, with x86 platforms, kernel
can access the memory of the user space directly if it is in a
process/task context. But, you should not relies on it.
If you look into bpf_probe_read_user(), it eventually do something like
"rep movsb" on x86 platforms. Access user space memory directly with
some extra checks. So, the bottleneck here can be the extra checks and
memory copying. If you access small chunks like what you said bellow,
the overhead of checks could be expensive.
>
>>
>>> page table to check that the access would not cause a fault (I think); this is
>>> very slow. I wonder if there's some other option that would keep the safety
>>> requirement for the memory access -- I'm imagining an optimistic mode where the
>>> raw access is performed (in the target process' memory space) and, in the rare
>>> case when a fault happens, the kernel would somehow recover from the fault and
>>
>> I am not very familiar with this part. I read the implementation of
>> bpf_probe_read_user() a little bit. It does what you mentioned here. It
>> would cause page faults, however, the handler will skip the instruction
>> leaving the counter non-zero. By checking the counter, it knows the
>> instruction is not completed, and returns an error.
>>
>> I am curious about what your access pattern looks like. Does it access a
>> large number of small chunks of data? Or, does it access a small number
>> of big chunks of data?
>
> My access pattern looks like a lot of small reads. Some of these reads could be
> done at the same time if we had a vectorized API (i.e. some of the pointers are
> known in advance); for others there are data dependencies (i.e. we need to
> dereference a pointer to know what we'll want to read next). Specifically, the
> use case is a debugger of sorts which uses BPF uprobes for poking around in the
> target process' memory, rather than the more traditional ptrace-based
> techniques (ptrace being very slow). This debugger needs to walk a lot of
> thread stacks by following stack pointers or by using DWARF unwind information,
> and then it further reads data structures from the target process' stacks and
> heaps, chasing pointers recursively.
A related information. You may already know that bpf_probe_read_user()
can fail if a page fault happens. A vectorized API probably doesn't
change it. It is a limitation of non-sleepable BPF programs. Sleepable
BPF programs should be able to overcome it.
>
>
>>
>>> fail the bpf_probe_read_user() helper. Would something like that be technically
>>> feasible / has there been any prior interest in faster access to user memory
>>>
>>> A more limited option that might be helpful would be a vectorized version of
>>> bpf_probe_read_user() that verifies many pointers at once.
>>>
>>>
>>>>
>>>>> the program with a syscall.
>>>>>
>>>>>>
>>>>>> sorry for late reply, do you mean like ubpf? the scope of this change
>>>>>> is to speed up the generic uprobe, ebpf is just one of the consumers
>>>>>
>>>>> I guess this means we need a new syscall?
>>>>>
>>>>> Thanks,
>>>>> Song
>>>>
prev parent reply other threads:[~2024-03-13 5:42 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-29 14:39 [LSF/MM/BPF TOPIC] faster uprobes Jiri Olsa
2024-03-01 0:25 ` Andrii Nakryiko
2024-03-01 8:18 ` Jiri Olsa
2024-03-01 17:01 ` Alexei Starovoitov
2024-03-01 17:26 ` Andrii Nakryiko
2024-03-01 18:08 ` Yunwei 123
2024-03-03 10:20 ` Jiri Olsa
2024-03-05 0:55 ` Andrii Nakryiko
2024-03-05 8:24 ` Jiri Olsa
2024-03-05 15:30 ` Jiri Olsa
2024-03-05 17:30 ` Andrii Nakryiko
2024-03-11 10:59 ` Jiri Olsa
2024-03-11 15:06 ` Oleg Nesterov
2024-03-11 16:46 ` Jiri Olsa
2024-03-11 17:02 ` Oleg Nesterov
2024-03-11 21:11 ` Jiri Olsa
2024-03-11 17:32 ` Andrii Nakryiko
2024-03-11 21:26 ` Jiri Olsa
2024-03-11 23:05 ` Andrii Nakryiko
2024-03-02 20:46 ` Jiri Olsa
2024-03-02 21:08 ` Alexei Starovoitov
2024-03-02 21:49 ` Oleg Nesterov
2024-03-01 19:39 ` Kui-Feng Lee
2024-03-05 17:18 ` Jiri Olsa
2024-03-05 23:53 ` Song Liu
2024-03-07 9:15 ` Jiri Olsa
2024-03-07 23:02 ` Kui-Feng Lee
2024-03-08 15:43 ` Andrei Matei
2024-03-12 17:16 ` Kui-Feng Lee
2024-03-13 1:32 ` Andrei Matei
2024-03-13 5:42 ` Kui-Feng Lee [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=c999d7ce-4c49-4b4e-9e4e-07ed4efc7b79@gmail.com \
--to=sinquersw@gmail.com \
--cc=andreimatei1@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=lsf-pc@lists.linux-foundation.org \
--cc=oleg@redhat.com \
--cc=olsajiri@gmail.com \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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