From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.177 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796263037; b=ecNJH1LTXlRTpvaf9jJfMmS0925hYB0uiImIJLxt0SheIEBO1WA7eptlkcEsFQy5jOOK zhLB4oIzzgr+PDxCky4X55ucHz8L+C9wTw6GqjC/n56gZwLZNfR/RZKF8ev7ZxrUCa2BH QrgcjBCrYKvcCzY1v8W0eFGKdJNFIpvDo7VOUGHdOqO/GPB5vUTzWi1l9isGGZF0Srch5 a5MIVwQxWOArMLopVuKJ3bBZM8FQsZzPHNKMI3AnHpC6a00IQiGKyPG8rZaovtWgEnmZ0 /kPhM6IK6ICejGKqZ638rB2GZo8CCfC+MUSh21E3B1+gnD+n2XAzuIwoT7gsKMGuIZQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796263037; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Content-Type:Date: Message-Id:Cc:Subject:From:To:In-Reply-To:References: Content-Transfer-Encoding:MIME-Version; bh=88WOGsEQW6hHA+6eBRPGOJZvmG62iFw8YJ9Hn2QSdmA=; b=IxWf/fxMVfRy98j5laKvcDHupuds6BOrfbKOnvc/1caLMEL1Hf40aUOGhkMP9NFiQcup 0Bd7Q++3na9tej3ZPXU6BQ8J8NIY4QHit7Qs1iRLatPyuVNalmvCRRF7L86zGG3rkV1VE PpZoFnerHxKUDywXzjq8E78uslHrZ5dfNOWTJqRSZDRMewEuMeuFi6fZjM0sXr1kSFzlq CwucIwz5A22i7B5B0KsHIPtaVEimgKw68CQWmcz3deLyOJwb/FPBLlGjBtyRVOHsIZBMp aPgtWstlzN5M8xSvkPKKMB4IqQccobg8dGUm+4Qz+oXTTqmYzVsU5l5Pyz3iH83ka9g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=r8dWI8jU; arc=none smtp.remote-ip=209.85.215.177 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5843606A32C for ; Thu, 3 Dec 2026 01:57:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A5843606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=r8dWI8jU Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-cdb9f760337so219506a12.0 for ; Wed, 02 Dec 2026 17:57:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796263036; x=1796867836; darn=vger.kernel.org; h=mime-version:content-transfer-encoding:references:in-reply-to:to :from:subject:cc:message-id:date:content-type:from:to:cc:subject :date:message-id:reply-to:content-type; bh=88WOGsEQW6hHA+6eBRPGOJZvmG62iFw8YJ9Hn2QSdmA=; b=r8dWI8jUZfcttrKlwIipjXLrTdx1Ve3aqgaQQc7j1dDM3uAbwWeYDUpmMJmwKey40k ZEn98u60M/EUqYKppe5o8CsjWhbNnWUW18Ki86qY85ZjIHrBUECCGD3os+VMgbRQZPKu m+n+rpdq1n5PaPibIpJFaqFFRhDuBrGe4fzdwJ4v4NbKbQVDV7SlTsg04V2TvAM5Daj3 3PyJ91X3k601gJRDJyCc3xS9hEsS3qW8gWfsVW/TViDcXB/LYtLVUYmhNRD/EfSwYYes 3E9VjJSvdfkXo0GYku2xujlcGNkxSiGoQwFa/YlDJB9GxbXj39kAKaXZevm9mgTNQGds szrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796263036; x=1796867836; h=mime-version:content-transfer-encoding:references:in-reply-to:to :from:subject:cc:message-id:date:content-type:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=88WOGsEQW6hHA+6eBRPGOJZvmG62iFw8YJ9Hn2QSdmA=; b=emb1OM4lPhvkGQzxGZ8eSLTqtoVsTCwOzkb6H4UkG/PaaJM68b2WuxIlQoTF2EudLX AgbF3BWyXY467y7l/WCsukBDms5ecnq9uiiJA4vhFsIoDUQ0ar+dmBz6YG5tGqE8ALuN mSJEgneS+9sFKrESw6GdBHTFLDQIGyKJv0tXwSyozw3LKh3MvVD/gn2yUveSTGge+9+j FhyLN5myVdDxmWoJaeg8LfSeMznhVYR7NPHc6SFVZ3T+YZXwu8xkMSuwVnc54rq+G3Gz EyWo8WIalH8Q8hs0MK3qW8pndDieiOELyRAkrMEyMrctFUeP2ArRFhkFqyj+4q0OqKpm 8psw== X-Gm-Message-State: APuU4Z+b3fUEWWATs9364II/H+F1qcx9uwY/mMtrMhJa/fqFZVoVHoOJ HOOQpcO/mqdEQ5j799hmO/JIzl+mtxkcc+tG8LyF7NcibHJSxZ2y4apU X-Gm-Gg: AZFdTZcMthr5K2uox0muDgIz2+m1SvlUechXaWV3uRUqRm26oHoPDcaOlsYHxpweVSl aLjA/nkRx4L79KWsqGjUopXfGHy8ZXo8jJXQEzW0Uzp+R5HkGFpPTBD0o/ViP4eacnxmAbbY34s WmzWlapzDxJpY0YNAjP0vCMm4BkFLXIYsGZQhb/DJeEWILs1JsLu8du3fvQJeUUJYskfZg4t/Em SdRP7frs/U1i++KjEyih9VOJ8ZmHKHIoBO5LNFLIr7XysvlxgMJxoRPUhXLxtqvYbhuIMYhsYYc MGcuAAJsLqlO1/v57M1cdrijYEiRgbh8497/Kv6TU4+s4rttpoYluqfCb4Sn7C5+s3AD+gBe13I 8S7/01WWcHti0C7j2TCEYhuDykFdAEOfhxccqjaPTnxYIbNBMsxJiLuUapKK9Z68pnGxDLHePAn ixohG1BBMR0Hr+gP9aTAQHkCCRiMxMdW5YEq5pyY1SaFFUw1zYQQhSN903PHwS4/+S65b64XET0 A6r+JXhu7pjZkkrDP9Ig0AOQQY8A05HqJeA2J3uWtsCUo8P8OVwuGnkjtoDxRJ/dpFyKz1icXQ1 fOPIs6hth3sjcy6c7OnO2ay5Wp/m4q8VvosrAozMqr6+QTSwmGtFC2LWJlBD/8E62I7G9PS2h4c 0uXHjHDJC2iHG5R6WcmrpFXQD+gWP/6qV+A== X-Received: by 2002:a17:90b:39ad:b0:3ae:3b3d:c617 with SMTP id 98e67ed59e1d1-3b5cb71c182mr387388a91.29.1796263035524; Wed, 02 Dec 2026 17:57:15 -0800 (PST) Received: from localhost ([153.61.198.253]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5c7649f12sm1773434a91.2.2026.12.02.17.57.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 02 Dec 2026 17:57:15 -0800 (PST) Content-Type: text/plain; charset=UTF-8 Date: Thu, 03 Dec 2026 01:57:14 +0000 Message-Id: Cc: , , , , "Song Liu" , "Yonghong Song" , "Quentin Monnet" , "Alan Maguire" Subject: Re: [PATCH RFC bpf-next 00/31] bpf: Attach kprobe multi link to inlined functions From: "Alexei Starovoitov" To: "Jiri Olsa" , "Daniel Borkmann" , "Andrii Nakryiko" , "Masami Hiramatsu" In-Reply-To: <20261202123035.10335-1-jolsa@kernel.org> References: <20261202123035.10335-1-jolsa@kernel.org> Content-Transfer-Encoding: 8bit X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 On Wed, Dec 02, 2026 at 04:30 AM Jiri Olsa wrote: > This patchset adds BPF_F_KPROBE_MULTI_INLINE flag that attaches also > the inlined instances of the functions in the syms array. The > instances are resolved from inline BTF (CONFIG_DEBUG_INFO_BTF_INLINE) > of vmlinux and modules and each gets its own kprobe. I like the idea. kprobes behind the kprobe multi link is the piece that was missing. But the lookup by name in the kernel is kinda heavy. In Alan's RFC thread I leaned towards the kernel doing the whole thing. The reason: kprobe multi could not attach to arbitrary IPs. Patch 7 shows that it can. That changes the trade off. Lets start with the mechanism only. addrs[] takes any address that kprobes accept. fprobe gets the ftrace locations. Every other address gets a kprobe. Such addrs fail with -ENOENT in fprobe_init() today, so no need for a flag. User space reads vmlinux.inline from sysfs and passes the sites. libbpf works this way already. It resolves "tcp_*" in user space and passes addrs. In this series wildcards can't use inline (patch 19). To match fully inlined functions libbpf has to read vmlinux.inline anyway. this series addrs[] ----------------- ----------------------- ------------------------- name -> sites kernel, exact names user space, wildcards too subset of sites no yes vmlinux.inline 2nd copy, ~10 Mbytes, mmap in user space never freed cookie per function per site args bpf_get_func_arg() cookie -> arg spec, like usdt.bpf.h arch x86-64 any with kprobes link info 5 new fields as is fdinfo 2 new lines as is, %pS bpftool new table, BTF lookup print sym+off new flags 2 0 Your use case from Alan's v2 thread works too: trace foo() only where it is inlined in bar(). User space picks the sites. Then there is no need for BTF parsing in the kernel, module_sect_addr(), regs_get_dwarf_register(), inline_* in bpf_link_info, STRICT, etc. Patches 2 and 3 stay. If tracers show that the lookup has to be in the kernel we add it on top of addrs[]. uapi doesn't go the other way. What the diff doesn't do: It is all or none. imo that's the right default. It doesn't say which address kprobes refuse. The link_create log can name it. A flag to skip such addresses can come later. link info shows what is attached. It calls register_kprobes(). enable_kprobes() goes on top. Two functions inlined at one address share one cookie. The kernel side is below. It compiles, I didn't attach anything with it: diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 2031c37..57c6e91 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2360,6 +2360,16 @@ struct bpf_session_run_ctx { }; #ifdef CONFIG_FPROBE +struct bpf_kprobe_multi_link; + +/* For an address in link->addrs that is not an ftrace location. */ +struct bpf_kprobe_multi_kprobe { + struct kprobe kp; + struct bpf_kprobe_multi_link *link; + /* As in link->addrs. register_kprobe() can move kp.addr behind endbr. */ + unsigned long addr; +}; + struct bpf_kprobe_multi_link { struct bpf_link link; struct fprobe fp; @@ -2368,6 +2378,9 @@ struct bpf_kprobe_multi_link { u32 cnt; u32 mods_cnt; struct module **mods; + struct bpf_kprobe_multi_kprobe *kprobes; + struct kprobe **kps; + u32 kps_cnt; }; struct bpf_kprobe_multi_run_ctx { @@ -2459,6 +2472,7 @@ static void bpf_kprobe_multi_link_release(struct bpf_link *link) kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); /* Don't wait for RCU GP here. */ unregister_fprobe_async(&kmulti_link->fp); + unregister_kprobes(kmulti_link->kps, kmulti_link->kps_cnt); kprobe_multi_put_modules(kmulti_link->mods, kmulti_link->mods_cnt); } @@ -2467,12 +2481,24 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) struct bpf_kprobe_multi_link *kmulti_link; kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); + kvfree(kmulti_link->kprobes); + kvfree(kmulti_link->kps); kvfree(kmulti_link->addrs); kvfree(kmulti_link->cookies); kfree(kmulti_link->mods); kfree(kmulti_link); } +static unsigned long kprobe_multi_missed(const struct bpf_kprobe_multi_link *link) +{ + unsigned long missed = link->fp.nmissed; + u32 i; + + for (i = 0; i < link->kps_cnt; i++) + missed += link->kprobes[i].kp.nmissed; + return missed; +} + static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link, struct bpf_link_info *info) { @@ -2490,7 +2516,7 @@ static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link, kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); info->kprobe_multi.count = kmulti_link->cnt; info->kprobe_multi.flags = kmulti_link->link.flags; - info->kprobe_multi.missed = kmulti_link->fp.nmissed; + info->kprobe_multi.missed = kprobe_multi_missed(kmulti_link); if (!uaddrs) return 0; @@ -2537,7 +2563,7 @@ static void bpf_kprobe_multi_show_fdinfo(const struct bpf_link *link, "kprobe_cnt:\t%u\n" "missed:\t%lu\n", kmulti_link->cnt, - kmulti_link->fp.nmissed); + kprobe_multi_missed(kmulti_link)); seq_printf(seq, "%s\t %s\n", "cookie", "func"); for (int i = 0; i < kmulti_link->cnt; i++) { @@ -2621,7 +2647,7 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) static __always_inline int kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, unsigned long entry_ip, struct ftrace_regs *fregs, - bool is_return, void *data) + struct pt_regs *regs, bool is_return, void *data) { struct bpf_kprobe_multi_run_ctx run_ctx = { .session_ctx = { @@ -2632,11 +2658,10 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, .entry_ip = entry_ip, }; struct bpf_run_ctx *old_run_ctx; - struct pt_regs *regs; int err; /* - * graph tracer framework ensures we won't migrate, so there is no need + * fprobe and kprobe handlers don't migrate, so there is no need * to use migrate_disable for bpf_prog_run again. The check here just for * __this_cpu_inc_return. */ @@ -2649,11 +2674,14 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, } rcu_read_lock(); - regs = ftrace_partial_regs(fregs, bpf_kprobe_multi_pt_regs_ptr()); + /* fprobe has fregs, kprobe has regs */ + if (fregs) + regs = ftrace_partial_regs(fregs, bpf_kprobe_multi_pt_regs_ptr()); old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx); err = bpf_prog_run(link->link.prog, regs); bpf_reset_run_ctx(old_run_ctx); - ftrace_partial_regs_update(fregs, bpf_kprobe_multi_pt_regs_ptr()); + if (fregs) + ftrace_partial_regs_update(fregs, bpf_kprobe_multi_pt_regs_ptr()); rcu_read_unlock(); out: @@ -2671,7 +2699,7 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, link = container_of(fp, struct bpf_kprobe_multi_link, fp); err = kprobe_multi_link_prog_run(link, ftrace_get_entry_ip(fentry_ip), - fregs, false, data); + fregs, NULL, false, data); return is_kprobe_session(link->link.prog) ? err : 0; } @@ -2684,8 +2712,18 @@ kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip, link = container_of(fp, struct bpf_kprobe_multi_link, fp); kprobe_multi_link_prog_run(link, ftrace_get_entry_ip(fentry_ip), - fregs, true, data); + fregs, NULL, true, data); +} + +static int kprobe_multi_kprobe_handler(struct kprobe *kp, struct pt_regs *regs) +{ + struct bpf_kprobe_multi_kprobe *mkp; + + mkp = container_of(kp, struct bpf_kprobe_multi_kprobe, kp); + kprobe_multi_link_prog_run(mkp->link, mkp->addr, NULL, regs, false, NULL); + return 0; } +NOKPROBE_SYMBOL(kprobe_multi_kprobe_handler); static int symbols_cmp_r(const void *a, const void *b, const void *priv) { @@ -2804,6 +2842,73 @@ static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt) return 0; } +/* fprobe takes the ftrace locations. Every other address gets a kprobe. */ +static int kprobe_multi_register(struct bpf_kprobe_multi_link *link) +{ + u32 i, cnt = link->cnt, fp_cnt = 0, kp_cnt = 0; + struct bpf_kprobe_multi_kprobe *mkp; + unsigned long *addrs; + int err = 0; + + addrs = kvmalloc_objs(*addrs, cnt); + if (!addrs) + return -ENOMEM; + + /* ftrace locations fill addrs from the start, the rest from the end. */ + for (i = 0; i < cnt; i++) { + if (ftrace_location(link->addrs[i])) + addrs[fp_cnt++] = link->addrs[i]; + else + addrs[cnt - ++kp_cnt] = link->addrs[i]; + } + + if (kp_cnt) { + /* + * No return probe, session and override in the middle of + * a function. Fail the way fprobe_init() does today. + */ + if (link->fp.exit_handler || link->link.prog->kprobe_override) { + err = -ENOENT; + goto out; + } + + /* bpf_kprobe_multi_link_dealloc() frees both. */ + link->kprobes = kvcalloc(kp_cnt, sizeof(*link->kprobes), GFP_KERNEL); + link->kps = kvmalloc_objs(*link->kps, kp_cnt); + if (!link->kprobes || !link->kps) { + err = -ENOMEM; + goto out; + } + + for (i = 0; i < kp_cnt; i++) { + mkp = &link->kprobes[i]; + mkp->link = link; + mkp->addr = addrs[fp_cnt + i]; + mkp->kp.addr = (kprobe_opcode_t *)mkp->addr; + mkp->kp.pre_handler = kprobe_multi_kprobe_handler; + link->kps[i] = &mkp->kp; + } + + /* All or none. */ + err = register_kprobes(link->kps, kp_cnt); + if (err) + goto out; + } + + if (fp_cnt) { + err = register_fprobe_ips(&link->fp, addrs, fp_cnt); + if (err) { + unregister_kprobes(link->kps, kp_cnt); + goto out; + } + } + + link->kps_cnt = kp_cnt; +out: + kvfree(addrs); + return err; +} + int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) { struct bpf_kprobe_multi_link *link = NULL; @@ -2944,7 +3049,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr } link->mods_cnt = err; - err = register_fprobe_ips(&link->fp, addrs, cnt); + err = kprobe_multi_register(link); if (err) { kprobe_multi_put_modules(link->mods, link->mods_cnt); bpf_link_cleanup(&link_primer); pw-bot: cr