Skip to content

Conversation

@maxgio92
Copy link
Contributor

@maxgio92 maxgio92 commented Apr 12, 2025

This patch introduces support for multi uprobe attach via uprobe_multi link [1], to support attach of multiple uprobes and usdt probes without involving perf events. Normal uprobe is attached through the perf event and attaching many uprobes generates one perf event each.

New BPFProg.AttachUprobeMulti and BPFProg.AttachURetprobeMulti methods are available from this patch, which are
astractions over the libbpf bpf_program__attach_uprobe_multi helper, to attach uprobes and uretprobes respectively.

Moreover, the API is simplified over the bpf_program__attach_uprobe_multi, by abstracting the bpf_uprobe_multi_opts API. Following the libbpf API, only the second set of inputs of the libbpf API is supported now, that is, mentioning libbpf:
bpf_program__attach_uprobe_multi() attaches a BPF program to multiple
uprobes with uprobe_multi link.

User can specify 2 mutually exclusive set of inputs:

  1. use only path/func_pattern/pid arguments

  2. use path/pid with allowed combinations of
    syms/offsets/ref_ctr_offsets/cookies/cnt

The Go binding API indeed supports the path and offsets set of inputs, in a similar fashion the BPFProg.AttachUprobe and
BPFProg.AttachUretprobe provide.

[1]
https://lore.kernel.org/bpf/[email protected]/

@maxgio92 maxgio92 changed the title feat: add support for uprobe_multi feat: add support for uprobe_multi link Apr 12, 2025
@CLAassistant
Copy link

CLAassistant commented Apr 12, 2025

CLA assistant check
All committers have signed the CLA.

@maxgio92 maxgio92 force-pushed the uprobe-multi-support branch 3 times, most recently from 9effc2a to 052ab74 Compare April 13, 2025 18:19
@maxgio92 maxgio92 force-pushed the uprobe-multi-support branch 2 times, most recently from 212413a to bf641e7 Compare April 17, 2025 18:29
@maxgio92 maxgio92 force-pushed the uprobe-multi-support branch 2 times, most recently from 5ed33df to 276caf5 Compare April 17, 2025 18:47
@geyslan
Copy link
Member

geyslan commented Apr 17, 2025

@maxgio92 Tks for this great contribution. We'll get into it soon.

@maxgio92 maxgio92 marked this pull request as ready for review April 18, 2025 13:30
@maxgio92
Copy link
Contributor Author

maxgio92 commented Apr 19, 2025

Thank you @geyslan!
I've looked in the meantime at the CI failed checks, but none of them seem related to the new tests for uprobe multi link 🤔

@geyslan
Copy link
Member

geyslan commented Apr 29, 2025

Thank you @geyslan! I've looked in the meantime at the CI failed checks, but none of them seem related to the new tests for uprobe multi link 🤔

Perhaps due to the double definition in vmlinux.h? LMK.

@maxgio92 maxgio92 force-pushed the uprobe-multi-support branch from fe675c2 to 26f12b4 Compare April 30, 2025 11:22
@maxgio92
Copy link
Contributor Author

My bad @geyslan, I fixed the declarations, thanks.
The difference there is the new unions for the stack and data segments registers, for the support to Intel FRED available since 6.9.

May I ask you a check and possibly a review?
Thank you so much!

@maxgio92 maxgio92 force-pushed the uprobe-multi-support branch from 2ccb0bf to 4d79b3f Compare May 1, 2025 07:50
@geyslan
Copy link
Member

geyslan commented May 17, 2025

May I ask you a check and possibly a review? Thank you so much!

Of course! I'll do it as soon as I have a free slot. And thanks again for your contribution!

@yanivagman FYI.

@geyslan
Copy link
Member

geyslan commented May 23, 2025

I'll get here soon.

@geyslan geyslan requested a review from Copilot May 23, 2025 21:54
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for multi uprobe and uretprobe attach via a new uprobe_multi link, simplifying the API compared to using individual perf events. Key changes include new methods in the BPFProg API, updated BPF C helper and binding functions, and associated selftests in Go, C, shell script, and Makefile.

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
selftest/uprobe-multi/test.go Added Go selftest exercising multiple probe attachments
selftest/uprobe-multi/test.c Added C selftest for uprobe multi functionality
selftest/uprobe-multi/run.sh Shell script to automate selftests
selftest/uprobe-multi/main.go Main selftest implementation using multi uprobes
selftest/uprobe-multi/main.bpf.c BPF C code for ring buffer event notifications
prog.go New API additions for multi uprobe attach
libbpfgo.h & libbpfgo.c Bindings for the new uprobe_multi attach helper
Makefile and go.mod Build and dependency updates for the new functionality
selftest/common/vmlinux.h Minor adjustments to support the new BPF changes

@geyslan geyslan added the feature New feature or request label May 24, 2025
Copy link
Member

@geyslan geyslan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good. I've requested small changes and we'll be good to go.

--- EDIT

Just forgot to mention before. Please make the commits atomic, I mean, this has changes that are later removed partially in the next.

I ask that since I don't want squash them later 👍🏻.

@maxgio92 maxgio92 force-pushed the uprobe-multi-support branch 5 times, most recently from 83943a9 to f4b1692 Compare May 30, 2025 16:35
@maxgio92 maxgio92 force-pushed the uprobe-multi-support branch from f4b1692 to a111cd1 Compare May 30, 2025 16:38
@maxgio92
Copy link
Contributor Author

maxgio92 commented May 30, 2025

Hi @geyslan, thank you so much. I've addressed all the points.
LMK if anything else needs to be addressed.

@maxgio92 maxgio92 requested a review from geyslan May 30, 2025 16:40
Comment on lines +634 to +635
// func_pattern argument to libbpf bpf_program__attach_uprobe_multi() which accepts a regular expression
// to specify functions to attach BPF program to, is currently not supported.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this it's not available yet, right?

Copy link
Contributor Author

@maxgio92 maxgio92 May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. We do only in the cgo API as decided to simplify its introduction next - considering the fix

Massimiliano Giovagnoli and others added 2 commits May 30, 2025 21:47
This patch introduces support for multi uprobe attach
via uprobe_multi link [1], to support attach of multiple
uprobes and usdt probes without involving perf events.
Normal uprobe is attached through the perf event and
attaching many uprobes generates one perf event each.

New BPFProg.AttachUprobeMulti and BPFProg.AttachURetprobeMulti
methods are available from this patch, which are
astractions over the libbpf bpf_program__attach_uprobe_multi
helper, to attach uprobes and uretprobes respectively.

Moreover, the API is simplified over the
bpf_program__attach_uprobe_multi, by abstracting the
bpf_uprobe_multi_opts API. Following the libbpf API, only the second set
of inputs of the libbpf API is supported now, that is, mentioning
libbpf:
  bpf_program__attach_uprobe_multi() attaches a BPF program to multiple
  uprobes with uprobe_multi link.

  User can specify 2 mutually exclusive set of inputs:

  1) use only path/func_pattern/pid arguments

  2) use path/pid with allowed combinations of
     syms/offsets/ref_ctr_offsets/cookies/cnt

The Go binding API indeed supports the path and offsets set of inputs,
in a similar fashion the BPFProg.AttachUprobe and
BPFProg.AttachUretprobe provide.

[1]
https://lore.kernel.org/bpf/[email protected]/

Signed-off-by: Massimiliano Giovagnoli <[email protected]>
This commit updates the vmlinux.h header to support intel
fred into the stack and data segment registers.

Signed-off-by: Massimiliano Giovagnoli <[email protected]>
@maxgio92 maxgio92 force-pushed the uprobe-multi-support branch from 7611df3 to 7f58ebb Compare May 30, 2025 19:49
@maxgio92
Copy link
Contributor Author

Everything has been addressed @geyslan. Thanks for your patience :-)
Let me know if you prefer to address the support for the func_pattern argument within this PR. As decided, for now it's only supported in the CGO API to ease future contribution.

@geyslan
Copy link
Member

geyslan commented May 30, 2025

No worries. The missing API can be handled next. Thanks again for this new feature. 🚀

@maxgio92
Copy link
Contributor Author

Right. min has been introduced in Go 1.21, so tests for Go 1.20 is failing now :) Let me address it soon

@geyslan
Copy link
Member

geyslan commented May 30, 2025

Right. min has been introduced in Go 1.21, so tests for Go 1.20 is failing now :) Let me address it soon

Indeed. Shame on me.

Comment on lines +161 to +166
struct bpf_uprobe_multi_opts opts = {};
opts.sz = sizeof(opts);
opts.offsets = offsets;
opts.cookies = cookies;
opts.cnt = cnt;
opts.retprobe = retprobe;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for the future: Currently, bpf_uprobe_multi_opts is only used by bpf_program__attach_uprobe_multi. If this changes, it may be better to manage it with dedicated _new() and _free() handlers.

@maxgio92
Copy link
Contributor Author

No problem @geyslan at all. I've just added a facade to min so that the test behaves correctly for Go < 1.21 as well - tested with 1.18, 1.19 and 1.20.

@geyslan
Copy link
Member

geyslan commented May 31, 2025

Maybe just placing the Min in the test file or even its logic replacing min would be simpler. WDYT?

P.S.: I used min to not pollute logs.

@maxgio92
Copy link
Contributor Author

maxgio92 commented May 31, 2025

@geyslan What about just removing it? The value this debug brings is not worth it IMO. I mean:

diff --git a/selftest/uprobe-multi/main.go b/selftest/uprobe-multi/main.go
index 61fa1a7..eafcbdc 100644
--- a/selftest/uprobe-multi/main.go
+++ b/selftest/uprobe-multi/main.go
@@ -192,10 +192,6 @@ func getFunSyms(name string) ([]elf.Symbol, error) {
 		return nil, err
 	}
 	log.Printf("found %d symbols in %s\n", len(syms), name)
-	log.Printf("showing first %d symbols\n", utils.Min(10, len(syms)))
-	for i := 0; i < utils.Min(10, len(syms)); i++ {
-		log.Printf("symbol %d: %v\n", i, syms[i])
-	}
 	for _, sym := range syms {
 		// Exclude non-function symbols.
 		if elf.ST_TYPE(sym.Info) != elf.STT_FUNC {

In the meantime, I've simplified the backward compatible min, in case we want to keep it.

@maxgio92 maxgio92 force-pushed the uprobe-multi-support branch from deb7220 to 925afa4 Compare May 31, 2025 14:09
@maxgio92 maxgio92 requested a review from geyslan May 31, 2025 14:16
@geyslan geyslan merged commit 0884266 into aquasecurity:main Jun 2, 2025
26 checks passed
@geyslan
Copy link
Member

geyslan commented Jun 2, 2025

@maxgio92 🚀

@maxgio92
Copy link
Contributor Author

maxgio92 commented Jun 2, 2025

Thank you so much @geyslan !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants