libbpf: Add uprobe ref counter offset support for USDT semaphores
authorAndrii Nakryiko <andrii@kernel.org>
Sun, 15 Aug 2021 07:06:08 +0000 (00:06 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 16 Aug 2021 22:45:08 +0000 (00:45 +0200)
When attaching to uprobes through perf subsystem, it's possible to specify
offset of a so-called USDT semaphore, which is just a reference counted u16,
used by kernel to keep track of how many tracers are attached to a given
location. Support for this feature was added in [0], so just wire this through
uprobe_opts. This is important to enable implementing USDT attachment and
tracing through libbpf's bpf_program__attach_uprobe_opts() API.

  [0] a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210815070609.987780-16-andrii@kernel.org
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf.h

index 62ce878..88d8825 100644 (file)
@@ -9152,13 +9152,19 @@ static int determine_uprobe_retprobe_bit(void)
        return parse_uint_from_file(file, "config:%d\n");
 }
 
+#define PERF_UPROBE_REF_CTR_OFFSET_BITS 32
+#define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
+
 static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
-                                uint64_t offset, int pid)
+                                uint64_t offset, int pid, size_t ref_ctr_off)
 {
        struct perf_event_attr attr = {};
        char errmsg[STRERR_BUFSIZE];
        int type, pfd, err;
 
+       if (ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
+               return -EINVAL;
+
        type = uprobe ? determine_uprobe_perf_type()
                      : determine_kprobe_perf_type();
        if (type < 0) {
@@ -9181,6 +9187,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
        }
        attr.size = sizeof(attr);
        attr.type = type;
+       attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
        attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
        attr.config2 = offset;           /* kprobe_addr or probe_offset */
 
@@ -9219,7 +9226,7 @@ bpf_program__attach_kprobe_opts(struct bpf_program *prog,
        pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
 
        pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name,
-                                   offset, -1 /* pid */);
+                                   offset, -1 /* pid */, 0 /* ref_ctr_off */);
        if (pfd < 0) {
                pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n",
                        prog->name, retprobe ? "kretprobe" : "kprobe", func_name,
@@ -9289,6 +9296,7 @@ bpf_program__attach_uprobe_opts(struct bpf_program *prog, pid_t pid,
        DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
        char errmsg[STRERR_BUFSIZE];
        struct bpf_link *link;
+       size_t ref_ctr_off;
        int pfd, err;
        bool retprobe;
 
@@ -9296,10 +9304,11 @@ bpf_program__attach_uprobe_opts(struct bpf_program *prog, pid_t pid,
                return libbpf_err_ptr(-EINVAL);
 
        retprobe = OPTS_GET(opts, retprobe, false);
+       ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0);
        pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
 
-       pfd = perf_event_open_probe(true /* uprobe */, retprobe,
-                                   binary_path, func_offset, pid);
+       pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path,
+                                   func_offset, pid, ref_ctr_off);
        if (pfd < 0) {
                pr_warn("prog '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
                        prog->name, retprobe ? "uretprobe" : "uprobe",
index 1f4a672..f177d89 100644 (file)
@@ -284,6 +284,10 @@ bpf_program__attach_kprobe_opts(struct bpf_program *prog,
 struct bpf_uprobe_opts {
        /* size of this struct, for forward/backward compatiblity */
        size_t sz;
+       /* offset of kernel reference counted USDT semaphore, added in
+        * a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
+        */
+       size_t ref_ctr_offset;
        /* custom user-provided value fetchable through bpf_get_attach_cookie() */
        __u64 bpf_cookie;
        /* uprobe is return probe, invoked at function return time */