From nobody Mon Jun 29 22:11:04 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 86C39C433EF for ; Wed, 2 Feb 2022 13:53:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344513AbiBBNxp (ORCPT ); Wed, 2 Feb 2022 08:53:45 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:40037 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344519AbiBBNxo (ORCPT ); Wed, 2 Feb 2022 08:53:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643810023; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iTqwruTWPfHMFTIBAKX8hnY20kDuzrc4oApkYAdRO6g=; b=iRI0cYP1pUemaRfko4RHOvJP0M//SqkNTd2mz7b+lUYyUn4PYctsilYSCoh9THo4J4LYnm mCVPSzHNY08NIGhpdySu4ZddtgIedmahyacLUTIkf+LE7uEzU7ma2CS+3gjEW3eFV3ByTj vGu8mslNKm0Vn9PhAUkdz96V4JmBXmw= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-474-qFrdEgoNNDugvktvKNFeXw-1; Wed, 02 Feb 2022 08:53:42 -0500 X-MC-Unique: qFrdEgoNNDugvktvKNFeXw-1 Received: by mail-ed1-f69.google.com with SMTP id k5-20020a508ac5000000b00408dec8390aso10346666edk.13 for ; Wed, 02 Feb 2022 05:53:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iTqwruTWPfHMFTIBAKX8hnY20kDuzrc4oApkYAdRO6g=; b=5wHMuVvClWqAN/TmgGlWtezHRfmCbEQRIsQsSff28j0Cy5WQIcHmtQvP/Fcb4oRYDn 5bs/R1Aiq2D6U8uFU6rVad6K3Cyr3EYT61HVvYbdpke9un74A/gC4q56vt+AkAcEgZr4 UsG0H7M65oqz6n1wTFashGJPZncqy7yUZjLLDleflWkpuyCCiAPX9dgz671M543D2A45 wyzolI/XBTGw71xVOXCnUNXwWaU/jXCL/ryO9AiazSnnXy0m4E2ptQwauQl1jUrRXvU7 MDLTX4fisNjdvfons5fMVCNfshR/CUkhhT8CW7V35FNFYCqO4pHx0+L/Ip+9XwL0QkqW oejg== X-Gm-Message-State: AOAM533y9QsQqfmHPBiYqqe42HUxhXaNaTA7bVKC2qXV5LDdF6ANacyP gkDkjxfQzYDg04hR2UN/WuJPUZAL9xyd2mbBmTRYWPIXO4dSSn+1VglUBEfK+vluAbuuLBeZ+Pr HYEZb2bkwWNfOtzGdVcT5rSet X-Received: by 2002:a17:906:3602:: with SMTP id q2mr24970112ejb.331.1643810021166; Wed, 02 Feb 2022 05:53:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJyXzqoQS1ZvJxxLsZ+BwuIIPfRrZtm3dn+Sy3pGMKpT/12UlVJwv5gPSUhL3DCYSekTahKqjg== X-Received: by 2002:a17:906:3602:: with SMTP id q2mr24970087ejb.331.1643810020887; Wed, 02 Feb 2022 05:53:40 -0800 (PST) Received: from krava.redhat.com (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id a18sm2368808edu.31.2022.02.02.05.53.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 05:53:40 -0800 (PST) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Jiri Olsa Subject: [PATCH 1/8] bpf: Add support to attach kprobe program with fprobe Date: Wed, 2 Feb 2022 14:53:26 +0100 Message-Id: <20220202135333.190761-2-jolsa@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202135333.190761-1-jolsa@kernel.org> References: <20220202135333.190761-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adding new link type BPF_LINK_TYPE_FPROBE that attaches kprobe program through fprobe API. The fprobe API allows to attach probe on multiple functions at once very fast, because it works on top of ftrace. On the other hand this limits the probe point to the function entry or return. The kprobe program gets the same pt_regs input ctx as when it's attached through the perf API. Adding new attach type BPF_TRACE_FPROBE that enables such link for kprobe program. User provides array of addresses or symbols with count to attach the kprobe program to. The new link_create uapi interface looks like: struct { __aligned_u64 syms; __aligned_u64 addrs; __u32 cnt; __u32 flags; } fprobe; The flags field allows single BPF_F_FPROBE_RETURN bit to create return fpro= be. Signed-off-by: Masami Hiramatsu Signed-off-by: Jiri Olsa --- include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 13 ++ kernel/bpf/syscall.c | 248 ++++++++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 13 ++ 4 files changed, 270 insertions(+), 5 deletions(-) diff --git a/include/linux/bpf_types.h b/include/linux/bpf_types.h index 48a91c51c015..e279cea46653 100644 --- a/include/linux/bpf_types.h +++ b/include/linux/bpf_types.h @@ -140,3 +140,4 @@ BPF_LINK_TYPE(BPF_LINK_TYPE_XDP, xdp) #ifdef CONFIG_PERF_EVENTS BPF_LINK_TYPE(BPF_LINK_TYPE_PERF_EVENT, perf) #endif +BPF_LINK_TYPE(BPF_LINK_TYPE_FPROBE, fprobe) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index a7f0ddedac1f..c0912f0a3dfe 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -997,6 +997,7 @@ enum bpf_attach_type { BPF_SK_REUSEPORT_SELECT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, BPF_PERF_EVENT, + BPF_TRACE_FPROBE, __MAX_BPF_ATTACH_TYPE }; =20 @@ -1011,6 +1012,7 @@ enum bpf_link_type { BPF_LINK_TYPE_NETNS =3D 5, BPF_LINK_TYPE_XDP =3D 6, BPF_LINK_TYPE_PERF_EVENT =3D 7, + BPF_LINK_TYPE_FPROBE =3D 8, =20 MAX_BPF_LINK_TYPE, }; @@ -1118,6 +1120,11 @@ enum bpf_link_type { */ #define BPF_F_XDP_HAS_FRAGS (1U << 5) =20 +/* link_create.fprobe.flags used in LINK_CREATE command for + * BPF_TRACE_FPROBE attach type to create return probe. + */ +#define BPF_F_FPROBE_RETURN (1U << 0) + /* When BPF ldimm64's insn[0].src_reg !=3D 0 then this can have * the following extensions: * @@ -1472,6 +1479,12 @@ union bpf_attr { */ __u64 bpf_cookie; } perf_event; + struct { + __aligned_u64 syms; + __aligned_u64 addrs; + __u32 cnt; + __u32 flags; + } fprobe; }; } link_create; =20 diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 72ce1edde950..0cfbb112c8e1 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -32,6 +32,7 @@ #include #include #include +#include =20 #define IS_FD_ARRAY(map) ((map)->map_type =3D=3D BPF_MAP_TYPE_PERF_EVENT_A= RRAY || \ (map)->map_type =3D=3D BPF_MAP_TYPE_CGROUP_ARRAY || \ @@ -3015,8 +3016,235 @@ static int bpf_perf_link_attach(const union bpf_att= r *attr, struct bpf_prog *pro fput(perf_file); return err; } +#else +static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_pro= g *prog) +{ + return -EOPNOTSUPP; +} #endif /* CONFIG_PERF_EVENTS */ =20 +#ifdef CONFIG_FPROBE + +struct bpf_fprobe_link { + struct bpf_link link; + struct fprobe fp; + unsigned long *addrs; +}; + +static void bpf_fprobe_link_release(struct bpf_link *link) +{ + struct bpf_fprobe_link *fprobe_link; + + fprobe_link =3D container_of(link, struct bpf_fprobe_link, link); + unregister_fprobe(&fprobe_link->fp); +} + +static void bpf_fprobe_link_dealloc(struct bpf_link *link) +{ + struct bpf_fprobe_link *fprobe_link; + + fprobe_link =3D container_of(link, struct bpf_fprobe_link, link); + kfree(fprobe_link->addrs); + kfree(fprobe_link); +} + +static const struct bpf_link_ops bpf_fprobe_link_lops =3D { + .release =3D bpf_fprobe_link_release, + .dealloc =3D bpf_fprobe_link_dealloc, +}; + +static int fprobe_link_prog_run(struct bpf_fprobe_link *fprobe_link, + struct pt_regs *regs) +{ + int err; + + if (unlikely(__this_cpu_inc_return(bpf_prog_active) !=3D 1)) { + err =3D 0; + goto out; + } + + rcu_read_lock(); + migrate_disable(); + err =3D bpf_prog_run(fprobe_link->link.prog, regs); + migrate_enable(); + rcu_read_unlock(); + + out: + __this_cpu_dec(bpf_prog_active); + return err; +} + +static void fprobe_link_entry_handler(struct fprobe *fp, unsigned long ent= ry_ip, + struct pt_regs *regs) +{ + unsigned long saved_ip =3D instruction_pointer(regs); + struct bpf_fprobe_link *fprobe_link; + + /* + * Because fprobe's regs->ip is set to the next instruction of + * dynamic-ftrace insturction, correct entry ip must be set, so + * that the bpf program can access entry address via regs as same + * as kprobes. + */ + instruction_pointer_set(regs, entry_ip); + + fprobe_link =3D container_of(fp, struct bpf_fprobe_link, fp); + fprobe_link_prog_run(fprobe_link, regs); + + instruction_pointer_set(regs, saved_ip); +} + +static void fprobe_link_exit_handler(struct fprobe *fp, unsigned long entr= y_ip, + struct pt_regs *regs) +{ + unsigned long saved_ip =3D instruction_pointer(regs); + struct bpf_fprobe_link *fprobe_link; + + instruction_pointer_set(regs, entry_ip); + + fprobe_link =3D container_of(fp, struct bpf_fprobe_link, fp); + fprobe_link_prog_run(fprobe_link, regs); + + instruction_pointer_set(regs, saved_ip); +} + +static int fprobe_resolve_syms(const void *usyms, u32 cnt, + unsigned long *addrs) +{ + unsigned long addr, size; + const char **syms; + int err =3D -ENOMEM; + unsigned int i; + char *func; + + size =3D cnt * sizeof(*syms); + syms =3D kzalloc(size, GFP_KERNEL); + if (!syms) + return -ENOMEM; + + func =3D kzalloc(KSYM_NAME_LEN, GFP_KERNEL); + if (!func) + goto error; + + if (copy_from_user(syms, usyms, size)) { + err =3D -EFAULT; + goto error; + } + + for (i =3D 0; i < cnt; i++) { + err =3D strncpy_from_user(func, syms[i], KSYM_NAME_LEN); + if (err =3D=3D KSYM_NAME_LEN) + err =3D -E2BIG; + if (err < 0) + goto error; + + err =3D -EINVAL; + if (func[0] =3D=3D '\0') + goto error; + addr =3D kallsyms_lookup_name(func); + if (!addr) + goto error; + if (!kallsyms_lookup_size_offset(addr, &size, NULL)) + size =3D MCOUNT_INSN_SIZE; + addr =3D ftrace_location_range(addr, addr + size - 1); + if (!addr) + goto error; + addrs[i] =3D addr; + } + + err =3D 0; +error: + kfree(syms); + kfree(func); + return err; +} + +static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_p= rog *prog) +{ + struct bpf_fprobe_link *link =3D NULL; + struct bpf_link_primer link_primer; + unsigned long *addrs; + u32 flags, cnt, size; + void __user *uaddrs; + void __user *usyms; + int err; + + /* no support for 32bit archs yet */ + if (sizeof(u64) !=3D sizeof(void *)) + return -EINVAL; + + if (prog->expected_attach_type !=3D BPF_TRACE_FPROBE) + return -EINVAL; + + flags =3D attr->link_create.fprobe.flags; + if (flags & ~BPF_F_FPROBE_RETURN) + return -EINVAL; + + uaddrs =3D u64_to_user_ptr(attr->link_create.fprobe.addrs); + usyms =3D u64_to_user_ptr(attr->link_create.fprobe.syms); + if ((!uaddrs && !usyms) || (uaddrs && usyms)) + return -EINVAL; + + cnt =3D attr->link_create.fprobe.cnt; + if (!cnt) + return -EINVAL; + + size =3D cnt * sizeof(*addrs); + addrs =3D kzalloc(size, GFP_KERNEL); + if (!addrs) + return -ENOMEM; + + if (uaddrs) { + if (copy_from_user(addrs, uaddrs, size)) { + err =3D -EFAULT; + goto error; + } + } else { + err =3D fprobe_resolve_syms(usyms, cnt, addrs); + if (err) + goto error; + } + + link =3D kzalloc(sizeof(*link), GFP_KERNEL); + if (!link) { + err =3D -ENOMEM; + goto error; + } + + bpf_link_init(&link->link, BPF_LINK_TYPE_FPROBE, + &bpf_fprobe_link_lops, prog); + + err =3D bpf_link_prime(&link->link, &link_primer); + if (err) + goto error; + + if (flags & BPF_F_FPROBE_RETURN) + link->fp.exit_handler =3D fprobe_link_exit_handler; + else + link->fp.entry_handler =3D fprobe_link_entry_handler; + + link->addrs =3D addrs; + + err =3D register_fprobe_ips(&link->fp, addrs, cnt); + if (err) { + bpf_link_cleanup(&link_primer); + return err; + } + + return bpf_link_settle(&link_primer); + +error: + kfree(link); + kfree(addrs); + return err; +} +#else /* !CONFIG_FPROBE */ +static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_p= rog *prog) +{ + return -EOPNOTSUPP; +} +#endif + #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd =20 static int bpf_raw_tracepoint_open(const union bpf_attr *attr) @@ -4248,7 +4476,7 @@ static int tracing_bpf_link_attach(const union bpf_at= tr *attr, bpfptr_t uattr, return -EINVAL; } =20 -#define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len +#define BPF_LINK_CREATE_LAST_FIELD link_create.fprobe.flags static int link_create(union bpf_attr *attr, bpfptr_t uattr) { enum bpf_prog_type ptype; @@ -4272,7 +4500,6 @@ static int link_create(union bpf_attr *attr, bpfptr_t= uattr) ret =3D tracing_bpf_link_attach(attr, uattr, prog); goto out; case BPF_PROG_TYPE_PERF_EVENT: - case BPF_PROG_TYPE_KPROBE: case BPF_PROG_TYPE_TRACEPOINT: if (attr->link_create.attach_type !=3D BPF_PERF_EVENT) { ret =3D -EINVAL; @@ -4280,6 +4507,14 @@ static int link_create(union bpf_attr *attr, bpfptr_= t uattr) } ptype =3D prog->type; break; + case BPF_PROG_TYPE_KPROBE: + if (attr->link_create.attach_type !=3D BPF_PERF_EVENT && + attr->link_create.attach_type !=3D BPF_TRACE_FPROBE) { + ret =3D -EINVAL; + goto out; + } + ptype =3D prog->type; + break; default: ptype =3D attach_type_to_prog_type(attr->link_create.attach_type); if (ptype =3D=3D BPF_PROG_TYPE_UNSPEC || ptype !=3D prog->type) { @@ -4311,13 +4546,16 @@ static int link_create(union bpf_attr *attr, bpfptr= _t uattr) ret =3D bpf_xdp_link_attach(attr, prog); break; #endif -#ifdef CONFIG_PERF_EVENTS case BPF_PROG_TYPE_PERF_EVENT: case BPF_PROG_TYPE_TRACEPOINT: - case BPF_PROG_TYPE_KPROBE: ret =3D bpf_perf_link_attach(attr, prog); break; -#endif + case BPF_PROG_TYPE_KPROBE: + if (attr->link_create.attach_type =3D=3D BPF_PERF_EVENT) + ret =3D bpf_perf_link_attach(attr, prog); + else + ret =3D bpf_fprobe_link_attach(attr, prog); + break; default: ret =3D -EINVAL; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index a7f0ddedac1f..c0912f0a3dfe 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -997,6 +997,7 @@ enum bpf_attach_type { BPF_SK_REUSEPORT_SELECT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, BPF_PERF_EVENT, + BPF_TRACE_FPROBE, __MAX_BPF_ATTACH_TYPE }; =20 @@ -1011,6 +1012,7 @@ enum bpf_link_type { BPF_LINK_TYPE_NETNS =3D 5, BPF_LINK_TYPE_XDP =3D 6, BPF_LINK_TYPE_PERF_EVENT =3D 7, + BPF_LINK_TYPE_FPROBE =3D 8, =20 MAX_BPF_LINK_TYPE, }; @@ -1118,6 +1120,11 @@ enum bpf_link_type { */ #define BPF_F_XDP_HAS_FRAGS (1U << 5) =20 +/* link_create.fprobe.flags used in LINK_CREATE command for + * BPF_TRACE_FPROBE attach type to create return probe. + */ +#define BPF_F_FPROBE_RETURN (1U << 0) + /* When BPF ldimm64's insn[0].src_reg !=3D 0 then this can have * the following extensions: * @@ -1472,6 +1479,12 @@ union bpf_attr { */ __u64 bpf_cookie; } perf_event; + struct { + __aligned_u64 syms; + __aligned_u64 addrs; + __u32 cnt; + __u32 flags; + } fprobe; }; } link_create; =20 --=20 2.34.1 From nobody Mon Jun 29 22:11:04 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 28D65C433F5 for ; Wed, 2 Feb 2022 13:53:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344587AbiBBNxx (ORCPT ); Wed, 2 Feb 2022 08:53:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:41645 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344519AbiBBNxw (ORCPT ); Wed, 2 Feb 2022 08:53:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643810031; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rU7PA92V+49dsAGPuIeIV/v8wUH1DOur5mrpoKB+KuE=; b=SvaY1mJscb8/mRyCJkqZ/bFuLaz1tGaMT1sEj3IRpn/VavLo+rUivxvnDmqeWRFIOMKNpp oUrseF0wrR0JivNPgMT0OUHyxq5FTWye2pa42DMwspsffNp9RAV+QeY1JnubT9gkeZJCRG ef/46mH8hNz+qq7DNaJoSu7xwd7MljY= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-615-sYgERnktOMGohcfJoxwb-w-1; Wed, 02 Feb 2022 08:53:48 -0500 X-MC-Unique: sYgERnktOMGohcfJoxwb-w-1 Received: by mail-ed1-f72.google.com with SMTP id w3-20020a50c443000000b0040696821132so10412149edf.22 for ; Wed, 02 Feb 2022 05:53:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rU7PA92V+49dsAGPuIeIV/v8wUH1DOur5mrpoKB+KuE=; b=bpDEG4opEycE7jZoFnbfjesR0qpPSJtaEcxyNSfK3rLmFSLdxxCK8y+qtiYRsf40Gt P1D0DP9tIIEHJzEI/Kv9W9gvsaFviG0lfOT5ABHyGYGYcBsgBWVnc9dizhoDCVKgTIgy TIh6VvSkx5irPfZ+HoDCPtvNOIDAezTPLjKffr7pgIru3LIVqsWPusM0TnKaMSfuzn16 yKuJ6/PQVYoLDl2NWERnP3hEKedvefQ8CuX8I7kCWaMuuCao+HegS8mDFCwvPyFeyr64 tLch6q5mdmPIOJG3AF3HCgqkhphGGvMA9NDrPka/MTVHdCodGM08jrzyhf18BVUMQyOX iUEg== X-Gm-Message-State: AOAM531Nh1X3T3jdNgFbNY8+XHSPunPxY3YuZML7YvQwS5hig87h8hdM +cO3WJSu6N2OPM4MelW52gxX5I+qJFvVyrJG0EpyaKwyb4OzsHm4B19dRwZc+gWdbqytwoRn3+L 6ewWb06JbLE8FujKFwdzefy8H X-Received: by 2002:a17:907:2ce1:: with SMTP id hz1mr19339608ejc.681.1643810027526; Wed, 02 Feb 2022 05:53:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJwwQirmhCLHvQ2UdBTeDbmq1tdFdGGrZl5m4jins0KciHUjrD9AYG1LHNgFrDoNGbtQI+UUJg== X-Received: by 2002:a17:907:2ce1:: with SMTP id hz1mr19339588ejc.681.1643810027285; Wed, 02 Feb 2022 05:53:47 -0800 (PST) Received: from krava.redhat.com (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id by4sm2868404edb.107.2022.02.02.05.53.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 05:53:46 -0800 (PST) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Jiri Olsa Subject: [PATCH 2/8] bpf: Add bpf_get_func_ip kprobe helper for fprobe link Date: Wed, 2 Feb 2022 14:53:27 +0100 Message-Id: <20220202135333.190761-3-jolsa@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202135333.190761-1-jolsa@kernel.org> References: <20220202135333.190761-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adding support to call get_func_ip_fprobe helper from kprobe programs attached by fprobe link. Also adding support to inline it, because it's single load instruction. Signed-off-by: Jiri Olsa --- kernel/bpf/verifier.c | 19 ++++++++++++++++++- kernel/trace/bpf_trace.c | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 1ae41d0cf96c..a745ded00635 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -13625,7 +13625,7 @@ static int do_misc_fixups(struct bpf_verifier_env *= env) continue; } =20 - /* Implement bpf_get_func_ip inline. */ + /* Implement tracing bpf_get_func_ip inline. */ if (prog_type =3D=3D BPF_PROG_TYPE_TRACING && insn->imm =3D=3D BPF_FUNC_get_func_ip) { /* Load IP address from ctx - 16 */ @@ -13640,6 +13640,23 @@ static int do_misc_fixups(struct bpf_verifier_env = *env) continue; } =20 + /* Implement kprobe/fprobe bpf_get_func_ip inline. */ + if (prog_type =3D=3D BPF_PROG_TYPE_KPROBE && + eatype =3D=3D BPF_TRACE_FPROBE && + insn->imm =3D=3D BPF_FUNC_get_func_ip) { + /* Load IP address from ctx (struct pt_regs) ip */ + insn_buf[0] =3D BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, + offsetof(struct pt_regs, ip)); + + new_prog =3D bpf_patch_insn_data(env, i + delta, insn_buf, 1); + if (!new_prog) + return -ENOMEM; + + env->prog =3D prog =3D new_prog; + insn =3D new_prog->insnsi + i + delta; + continue; + } + patch_call_imm: fn =3D env->ops->get_func_proto(insn->imm, env->prog); /* all functions that have prototype and verifier allowed diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index a2024ba32a20..28e59e31e3db 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1036,6 +1036,19 @@ static const struct bpf_func_proto bpf_get_func_ip_p= roto_kprobe =3D { .arg1_type =3D ARG_PTR_TO_CTX, }; =20 +BPF_CALL_1(bpf_get_func_ip_fprobe, struct pt_regs *, regs) +{ + /* This helper call is inlined by verifier. */ + return regs->ip; +} + +static const struct bpf_func_proto bpf_get_func_ip_proto_fprobe =3D { + .func =3D bpf_get_func_ip_fprobe, + .gpl_only =3D false, + .ret_type =3D RET_INTEGER, + .arg1_type =3D ARG_PTR_TO_CTX, +}; + BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) { struct bpf_trace_run_ctx *run_ctx; @@ -1279,7 +1292,8 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, cons= t struct bpf_prog *prog) return &bpf_override_return_proto; #endif case BPF_FUNC_get_func_ip: - return &bpf_get_func_ip_proto_kprobe; + return prog->expected_attach_type =3D=3D BPF_TRACE_FPROBE ? + &bpf_get_func_ip_proto_fprobe : &bpf_get_func_ip_proto_kprobe; case BPF_FUNC_get_attach_cookie: return &bpf_get_attach_cookie_proto_trace; default: --=20 2.34.1 From nobody Mon Jun 29 22:11:04 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D2C1C433FE for ; Wed, 2 Feb 2022 13:54:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344610AbiBBNyD (ORCPT ); Wed, 2 Feb 2022 08:54:03 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:55735 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344617AbiBBNx5 (ORCPT ); Wed, 2 Feb 2022 08:53:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643810037; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nmdcIiImDczn6f3CR2CX3Iu2tcVi06TPYmGxM9JxMVg=; b=hrTIgHd0C8OvikynHahp9p2SKQZ+wzqftqkzBMO6dwXwqZnuigLrLcIvHuS0HiEiC6Oy05 AXJlT3URsrQzRVgMmH8oO33gGRM8Vn/vPhqbuxeY1NwddrJ994kmwWj/vxC2rTJRcdSOY4 hhHetvPi4cRP0rGJgb/spR9M0fsKf2s= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-497-2-jMSIXVMgqnMbf3qWt9fA-1; Wed, 02 Feb 2022 08:53:56 -0500 X-MC-Unique: 2-jMSIXVMgqnMbf3qWt9fA-1 Received: by mail-ej1-f72.google.com with SMTP id ky6-20020a170907778600b0068e4bd99fd1so8155354ejc.15 for ; Wed, 02 Feb 2022 05:53:56 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nmdcIiImDczn6f3CR2CX3Iu2tcVi06TPYmGxM9JxMVg=; b=s6nR8jpoRRMa0LwT3nCiXL7YWDuA5ESFyszetRPNDuWZGqhxRGXvRzaYV7NKtPvc4q iOe+PHFB5+Y/2AJs9O2/Bgf6WoLuQhMOABX5YuCTEoiwkqzYlGjH/s/rFXlCHqs0cXd+ fMnvBZPdeZnzP5ebBN+XlwvCtazlX+RFYXahNrLalcDMb0OmIb85fSSEWug1TTSryepQ fHCXhB6UUyZ0PfP3kszYcUjO7ob1klc+PMImMDYQ5nKnrijQwMsh4UZulusU1Sgc0tR2 cHcFQkIegcXbHubaOMwSqu402HKQGZyaPiyc843ml7H2t3ztPYCMLWT3VrUe4OMUDlLp uxVw== X-Gm-Message-State: AOAM533yZ8chr9iOHmuhnB5kgVt4GKJu3nuX3sxdTD995Ef8tTiPP1jX ybxAUa5E4cI4tQGS2LNMhf03X1geRn0r+6AkY6yrxV4ZvGllJM8zOdbvizn9iYQE6d6U3Q9thN3 PIPR1KzoedlL8ZuM+ZVKhIwvd X-Received: by 2002:a17:906:604d:: with SMTP id p13mr24728340ejj.639.1643810033735; Wed, 02 Feb 2022 05:53:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJyBR9DR4TppVEV3M7QqQMxwZJh36m2BbKzoYg1EmmV3Cx7Lz1/QJk2/Gf1EUqW/sQJhcqZWZA== X-Received: by 2002:a17:906:604d:: with SMTP id p13mr24728329ejj.639.1643810033497; Wed, 02 Feb 2022 05:53:53 -0800 (PST) Received: from krava.redhat.com (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id ce20sm3260162ejb.169.2022.02.02.05.53.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 05:53:53 -0800 (PST) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Jiri Olsa Subject: [PATCH 3/8] bpf: Add bpf_cookie support to fprobe Date: Wed, 2 Feb 2022 14:53:28 +0100 Message-Id: <20220202135333.190761-4-jolsa@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202135333.190761-1-jolsa@kernel.org> References: <20220202135333.190761-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adding support to call bpf_get_attach_cookie helper from kprobe program attached by fprobe link. The bpf_cookie is provided by array of u64 values, where each value is paired with provided function address with the same array index. Suggested-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- include/linux/bpf.h | 2 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 83 +++++++++++++++++++++++++++++++++- kernel/trace/bpf_trace.c | 16 ++++++- tools/include/uapi/linux/bpf.h | 1 + 5 files changed, 100 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 6eb0b180d33b..7b65f05c0487 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1301,6 +1301,8 @@ static inline void bpf_reset_run_ctx(struct bpf_run_c= tx *old_ctx) #endif } =20 +u64 bpf_fprobe_cookie(struct bpf_run_ctx *ctx, u64 ip); + /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */ #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0) /* BPF program asks to set CN on the packet. */ diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index c0912f0a3dfe..0dc6aa4f9683 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1484,6 +1484,7 @@ union bpf_attr { __aligned_u64 addrs; __u32 cnt; __u32 flags; + __aligned_u64 bpf_cookies; } fprobe; }; } link_create; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 0cfbb112c8e1..6c5e74bc43b6 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -33,6 +33,8 @@ #include #include #include +#include +#include =20 #define IS_FD_ARRAY(map) ((map)->map_type =3D=3D BPF_MAP_TYPE_PERF_EVENT_A= RRAY || \ (map)->map_type =3D=3D BPF_MAP_TYPE_CGROUP_ARRAY || \ @@ -3025,10 +3027,18 @@ static int bpf_perf_link_attach(const union bpf_att= r *attr, struct bpf_prog *pro =20 #ifdef CONFIG_FPROBE =20 +struct bpf_fprobe_cookie { + unsigned long addr; + u64 bpf_cookie; +}; + struct bpf_fprobe_link { struct bpf_link link; struct fprobe fp; unsigned long *addrs; + struct bpf_run_ctx run_ctx; + struct bpf_fprobe_cookie *bpf_cookies; + u32 cnt; }; =20 static void bpf_fprobe_link_release(struct bpf_link *link) @@ -3045,6 +3055,7 @@ static void bpf_fprobe_link_dealloc(struct bpf_link *= link) =20 fprobe_link =3D container_of(link, struct bpf_fprobe_link, link); kfree(fprobe_link->addrs); + kfree(fprobe_link->bpf_cookies); kfree(fprobe_link); } =20 @@ -3053,9 +3064,37 @@ static const struct bpf_link_ops bpf_fprobe_link_lop= s =3D { .dealloc =3D bpf_fprobe_link_dealloc, }; =20 +static int bpf_fprobe_cookie_cmp(const void *_a, const void *_b) +{ + const struct bpf_fprobe_cookie *a =3D _a; + const struct bpf_fprobe_cookie *b =3D _b; + + if (a->addr =3D=3D b->addr) + return 0; + return a->addr < b->addr ? -1 : 1; +} + +u64 bpf_fprobe_cookie(struct bpf_run_ctx *ctx, u64 ip) +{ + struct bpf_fprobe_link *fprobe_link; + struct bpf_fprobe_cookie *val, key =3D { + .addr =3D (unsigned long) ip, + }; + + if (!ctx) + return 0; + fprobe_link =3D container_of(ctx, struct bpf_fprobe_link, run_ctx); + if (!fprobe_link->bpf_cookies) + return 0; + val =3D bsearch(&key, fprobe_link->bpf_cookies, fprobe_link->cnt, + sizeof(key), bpf_fprobe_cookie_cmp); + return val ? val->bpf_cookie : 0; +} + static int fprobe_link_prog_run(struct bpf_fprobe_link *fprobe_link, struct pt_regs *regs) { + struct bpf_run_ctx *old_run_ctx; int err; =20 if (unlikely(__this_cpu_inc_return(bpf_prog_active) !=3D 1)) { @@ -3063,12 +3102,16 @@ static int fprobe_link_prog_run(struct bpf_fprobe_l= ink *fprobe_link, goto out; } =20 + old_run_ctx =3D bpf_set_run_ctx(&fprobe_link->run_ctx); + rcu_read_lock(); migrate_disable(); err =3D bpf_prog_run(fprobe_link->link.prog, regs); migrate_enable(); rcu_read_unlock(); =20 + bpf_reset_run_ctx(old_run_ctx); + out: __this_cpu_dec(bpf_prog_active); return err; @@ -3161,10 +3204,12 @@ static int fprobe_resolve_syms(const void *usyms, u= 32 cnt, =20 static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_p= rog *prog) { + struct bpf_fprobe_cookie *bpf_cookies =3D NULL; struct bpf_fprobe_link *link =3D NULL; struct bpf_link_primer link_primer; + void __user *ubpf_cookies; + u32 flags, cnt, i, size; unsigned long *addrs; - u32 flags, cnt, size; void __user *uaddrs; void __user *usyms; int err; @@ -3205,6 +3250,37 @@ static int bpf_fprobe_link_attach(const union bpf_at= tr *attr, struct bpf_prog *p goto error; } =20 + ubpf_cookies =3D u64_to_user_ptr(attr->link_create.fprobe.bpf_cookies); + if (ubpf_cookies) { + u64 *tmp; + + err =3D -ENOMEM; + tmp =3D kzalloc(size, GFP_KERNEL); + if (!tmp) + goto error; + + if (copy_from_user(tmp, ubpf_cookies, size)) { + kfree(tmp); + err =3D -EFAULT; + goto error; + } + + size =3D cnt * sizeof(*bpf_cookies); + bpf_cookies =3D kzalloc(size, GFP_KERNEL); + if (!bpf_cookies) { + kfree(tmp); + goto error; + } + + for (i =3D 0; i < cnt; i++) { + bpf_cookies[i].addr =3D addrs[i]; + bpf_cookies[i].bpf_cookie =3D tmp[i]; + } + + sort(bpf_cookies, cnt, sizeof(*bpf_cookies), bpf_fprobe_cookie_cmp, NULL= ); + kfree(tmp); + } + link =3D kzalloc(sizeof(*link), GFP_KERNEL); if (!link) { err =3D -ENOMEM; @@ -3224,6 +3300,8 @@ static int bpf_fprobe_link_attach(const union bpf_att= r *attr, struct bpf_prog *p link->fp.entry_handler =3D fprobe_link_entry_handler; =20 link->addrs =3D addrs; + link->bpf_cookies =3D bpf_cookies; + link->cnt =3D cnt; =20 err =3D register_fprobe_ips(&link->fp, addrs, cnt); if (err) { @@ -3236,6 +3314,7 @@ static int bpf_fprobe_link_attach(const union bpf_att= r *attr, struct bpf_prog *p error: kfree(link); kfree(addrs); + kfree(bpf_cookies); return err; } #else /* !CONFIG_FPROBE */ @@ -4476,7 +4555,7 @@ static int tracing_bpf_link_attach(const union bpf_at= tr *attr, bpfptr_t uattr, return -EINVAL; } =20 -#define BPF_LINK_CREATE_LAST_FIELD link_create.fprobe.flags +#define BPF_LINK_CREATE_LAST_FIELD link_create.fprobe.bpf_cookies static int link_create(union bpf_attr *attr, bpfptr_t uattr) { enum bpf_prog_type ptype; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 28e59e31e3db..b54b2ef93928 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1049,6 +1049,18 @@ static const struct bpf_func_proto bpf_get_func_ip_p= roto_fprobe =3D { .arg1_type =3D ARG_PTR_TO_CTX, }; =20 +BPF_CALL_1(bpf_get_attach_cookie_fprobe, struct pt_regs *, regs) +{ + return bpf_fprobe_cookie(current->bpf_ctx, regs->ip); +} + +static const struct bpf_func_proto bpf_get_attach_cookie_proto_fprobe =3D { + .func =3D bpf_get_attach_cookie_fprobe, + .gpl_only =3D false, + .ret_type =3D RET_INTEGER, + .arg1_type =3D ARG_PTR_TO_CTX, +}; + BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) { struct bpf_trace_run_ctx *run_ctx; @@ -1295,7 +1307,9 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, cons= t struct bpf_prog *prog) return prog->expected_attach_type =3D=3D BPF_TRACE_FPROBE ? &bpf_get_func_ip_proto_fprobe : &bpf_get_func_ip_proto_kprobe; case BPF_FUNC_get_attach_cookie: - return &bpf_get_attach_cookie_proto_trace; + return prog->expected_attach_type =3D=3D BPF_TRACE_FPROBE ? + &bpf_get_attach_cookie_proto_fprobe : + &bpf_get_attach_cookie_proto_trace; default: return bpf_tracing_func_proto(func_id, prog); } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index c0912f0a3dfe..0dc6aa4f9683 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1484,6 +1484,7 @@ union bpf_attr { __aligned_u64 addrs; __u32 cnt; __u32 flags; + __aligned_u64 bpf_cookies; } fprobe; }; } link_create; --=20 2.34.1 From nobody Mon Jun 29 22:11:04 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1FE9EC433EF for ; Wed, 2 Feb 2022 13:54:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344614AbiBBNyR (ORCPT ); Wed, 2 Feb 2022 08:54:17 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:30581 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344616AbiBBNyE (ORCPT ); Wed, 2 Feb 2022 08:54:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643810044; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jBnKFQ30kOdAO0O49lD07YWI/2GEebyH6rvvven/zqI=; b=BW5QoziwiXa/flqH8owlwPDHDyuClNMlLpnrYvkVtcMTzx78cgKd5RxLIBJK3Aps0BTo5g VisC80HJZaFoo1FuN2mEGBEy3Lw8sAbkdKa7/o9VNviAcqLmM2xve85sySuLGNaLYwp1wJ PIJDo83uuyU1EQCvEJpBYp/tXZCCAPE= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-426-JvUGRzKzOLuIEG9Iy0TQYg-1; Wed, 02 Feb 2022 08:54:01 -0500 X-MC-Unique: JvUGRzKzOLuIEG9Iy0TQYg-1 Received: by mail-ej1-f69.google.com with SMTP id 13-20020a170906328d00b006982d0888a4so8103664ejw.9 for ; Wed, 02 Feb 2022 05:54:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jBnKFQ30kOdAO0O49lD07YWI/2GEebyH6rvvven/zqI=; b=YUAY0f+OP9Ozcd6W8mP3sEd6mzuW4wX21mbXuXggd3LJdUDwSdFSWrSw309Z+4Qdmy slUTdQXhS3rJaFZKbLmUw/VFiPv/NszjnEREgbOd8gTTQvcpxRdsUvp1FiUNadskBi63 RgG26NnQsA5txR/XU2A6S4/mpePWU3ciulke6efoeHNw7AYrs0jW5jDbVsKAlO3eNwJm whE7Wfd8HZ0A0kxdNa2F6ft6Y46C2OXPcchjst8S786UgK63Z0CfWef7ErQto2Vni2s+ QXoZ0uq1fHL5wD3JSmcTQyOVf+N0ec7JTna/v5wRuN+Wfm3Cx485zuEEnMnrlmh+WCeg PAyw== X-Gm-Message-State: AOAM531wr8Hzx047TEh/JQF52UagtT+bwFfNTXjlzwPh2/zad2fmD1Fh yEllpUYGEzz7bGcM5RcOBSjVjlCb0tL94MBmop9yoAA3/RYehfCY2q7ILqfE9NstsToxX+5GigI Gui7WoJbswvpwxbmfLDb9F1jD X-Received: by 2002:a50:bb0a:: with SMTP id y10mr8988219ede.441.1643810039919; Wed, 02 Feb 2022 05:53:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJwpHD34n0QXTYfKs783CZ7B9M9fR7e6UeoB6pLhcbQSrQnEbVZssHdo7gWyGcK97/TJlR+epw== X-Received: by 2002:a50:bb0a:: with SMTP id y10mr8988201ede.441.1643810039727; Wed, 02 Feb 2022 05:53:59 -0800 (PST) Received: from krava.redhat.com (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id f4sm15819702ejh.93.2022.02.02.05.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 05:53:59 -0800 (PST) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Jiri Olsa Subject: [PATCH 4/8] libbpf: Add libbpf__kallsyms_parse function Date: Wed, 2 Feb 2022 14:53:29 +0100 Message-Id: <20220202135333.190761-5-jolsa@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202135333.190761-1-jolsa@kernel.org> References: <20220202135333.190761-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move the kallsyms parsing in internal libbpf__kallsyms_parse function, so it can be used from other places. It will be used in following changes. Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 62 ++++++++++++++++++++------------- tools/lib/bpf/libbpf_internal.h | 5 +++ 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 1b0936b016d9..7d595cfd03bc 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -7165,12 +7165,10 @@ static int bpf_object__sanitize_maps(struct bpf_obj= ect *obj) return 0; } =20 -static int bpf_object__read_kallsyms_file(struct bpf_object *obj) +int libbpf__kallsyms_parse(void *arg, kallsyms_cb_t cb) { char sym_type, sym_name[500]; unsigned long long sym_addr; - const struct btf_type *t; - struct extern_desc *ext; int ret, err =3D 0; FILE *f; =20 @@ -7189,35 +7187,51 @@ static int bpf_object__read_kallsyms_file(struct bp= f_object *obj) if (ret !=3D 3) { pr_warn("failed to read kallsyms entry: %d\n", ret); err =3D -EINVAL; - goto out; + break; } =20 - ext =3D find_extern_by_name(obj, sym_name); - if (!ext || ext->type !=3D EXT_KSYM) - continue; - - t =3D btf__type_by_id(obj->btf, ext->btf_id); - if (!btf_is_var(t)) - continue; - - if (ext->is_set && ext->ksym.addr !=3D sym_addr) { - pr_warn("extern (ksym) '%s' resolution is ambiguous: 0x%llx or 0x%llx\n= ", - sym_name, ext->ksym.addr, sym_addr); - err =3D -EINVAL; - goto out; - } - if (!ext->is_set) { - ext->is_set =3D true; - ext->ksym.addr =3D sym_addr; - pr_debug("extern (ksym) %s=3D0x%llx\n", sym_name, sym_addr); - } + err =3D cb(arg, sym_addr, sym_type, sym_name); + if (err) + break; } =20 -out: fclose(f); return err; } =20 +static int kallsyms_cb(void *arg, unsigned long long sym_addr, + char sym_type, const char *sym_name) +{ + struct bpf_object *obj =3D arg; + const struct btf_type *t; + struct extern_desc *ext; + + ext =3D find_extern_by_name(obj, sym_name); + if (!ext || ext->type !=3D EXT_KSYM) + return 0; + + t =3D btf__type_by_id(obj->btf, ext->btf_id); + if (!btf_is_var(t)) + return 0; + + if (ext->is_set && ext->ksym.addr !=3D sym_addr) { + pr_warn("extern (ksym) '%s' resolution is ambiguous: 0x%llx or 0x%llx\n", + sym_name, ext->ksym.addr, sym_addr); + return -EINVAL; + } + if (!ext->is_set) { + ext->is_set =3D true; + ext->ksym.addr =3D sym_addr; + pr_debug("extern (ksym) %s=3D0x%llx\n", sym_name, sym_addr); + } + return 0; +} + +static int bpf_object__read_kallsyms_file(struct bpf_object *obj) +{ + return libbpf__kallsyms_parse(obj, kallsyms_cb); +} + static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name, __u16 kind, struct btf **res_btf, struct module_btf **res_mod_btf) diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_interna= l.h index bc86b82e90d1..fb3b07d401df 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -449,6 +449,11 @@ __s32 btf__find_by_name_kind_own(const struct btf *btf= , const char *type_name, =20 extern enum libbpf_strict_mode libbpf_mode; =20 +typedef int (*kallsyms_cb_t)(void *arg, unsigned long long sym_addr, + char sym_type, const char *sym_name); + +int libbpf__kallsyms_parse(void *arg, kallsyms_cb_t cb); + /* handle direct returned errors */ static inline int libbpf_err(int ret) { --=20 2.34.1 From nobody Mon Jun 29 22:11:04 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2EEB2C433EF for ; Wed, 2 Feb 2022 13:54:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344710AbiBBNyX (ORCPT ); Wed, 2 Feb 2022 08:54:23 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:34751 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344679AbiBBNyK (ORCPT ); Wed, 2 Feb 2022 08:54:10 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643810049; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gqIBYhSED2KiLRiPVvQEcn95LWAaRUgov7svs5RNRz4=; b=Hu0l9O7ZHMS+sNZB3UsrrjPCVNpGtHd0VKhxdOYKbyv5Yi0f73V0nmPsetPrx/CKtNmhcT eLIQZxTFSuOO+wbdWs0VimFQQpVUg1i0Ly1oP0QjGN1qXiBX+vfe9Myj0UmItWbB+2BkLO jGnp8Bq7TIgi28atfiSpcmkuaiLDZ70= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-267-cXZrbyTHOoqgUg9rjesszw-1; Wed, 02 Feb 2022 08:54:09 -0500 X-MC-Unique: cXZrbyTHOoqgUg9rjesszw-1 Received: by mail-ej1-f72.google.com with SMTP id lb14-20020a170907784e00b006aa178894fcso8177906ejc.6 for ; Wed, 02 Feb 2022 05:54:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gqIBYhSED2KiLRiPVvQEcn95LWAaRUgov7svs5RNRz4=; b=6nxIvAUtK1uK9E/O6BrtxZTk18p9KNeYNOrzHC2iTHcjebnAc+J97Z7qBet5TYbAoK Aq5OboQQjIxuKOzLwcXY2s5m84+ayhDJR4aAi6fys2t5B3oOBTxPYqbAKOthxAFtiIUK 7KMSGloXoBKE1p4JYIblpkm2x+T3Medzyayk/HSOXoY6J4GwXdN3qbNmvPGyALCsviFA R5W/5cFxrSaal02GUqOIXXZ/XrvePFrBFFHXw5sgP51bv/7OdMnbHxxZRpXqK9ECYZQP TwkGYgmpRoJMG8PP5CKs+ft84xb0iOPm6/5ZaPfd8fveyerGsvGZk6Dml5spazNVwtoJ iuXw== X-Gm-Message-State: AOAM532r6dBuQa4TovI/jQxLrngiOJV0BQcKACpnktLLILO4uclGAymC hkkKfKiLQGQhaf6RwHvM12nqWKmG6MU4pAXzzZ5V4Gzv2ZabCIL+1UZKIFc6lHAS8tlmZo4wsB9 zjhuK8EEti0RrFRr1tX4boC7l X-Received: by 2002:a05:6402:509:: with SMTP id m9mr29640223edv.237.1643810046235; Wed, 02 Feb 2022 05:54:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJwcUwK13/nY2kcApXDzGsFhV/cZCeMZ9htyubnhs5SDp70e98r81NKwfnOABmoL6EkvsLeXhg== X-Received: by 2002:a05:6402:509:: with SMTP id m9mr29640200edv.237.1643810046059; Wed, 02 Feb 2022 05:54:06 -0800 (PST) Received: from krava.redhat.com (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id s7sm15703501ejo.212.2022.02.02.05.54.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 05:54:05 -0800 (PST) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Jiri Olsa Subject: [PATCH 5/8] libbpf: Add bpf_link_create support for multi kprobes Date: Wed, 2 Feb 2022 14:53:30 +0100 Message-Id: <20220202135333.190761-6-jolsa@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202135333.190761-1-jolsa@kernel.org> References: <20220202135333.190761-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adding new kprobe struct in bpf_link_create_opts object to pass multi kprobe data to link_create attr API. Signed-off-by: Jiri Olsa --- tools/lib/bpf/bpf.c | 7 +++++++ tools/lib/bpf/bpf.h | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 418b259166f8..98156709a96c 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -853,6 +853,13 @@ int bpf_link_create(int prog_fd, int target_fd, if (!OPTS_ZEROED(opts, perf_event)) return libbpf_err(-EINVAL); break; + case BPF_TRACE_FPROBE: + attr.link_create.fprobe.syms =3D OPTS_GET(opts, fprobe.syms, 0); + attr.link_create.fprobe.addrs =3D OPTS_GET(opts, fprobe.addrs, 0); + attr.link_create.fprobe.cnt =3D OPTS_GET(opts, fprobe.cnt, 0); + attr.link_create.fprobe.flags =3D OPTS_GET(opts, fprobe.flags, 0); + attr.link_create.fprobe.bpf_cookies =3D OPTS_GET(opts, fprobe.bpf_cookie= s, 0); + break; default: if (!OPTS_ZEROED(opts, flags)) return libbpf_err(-EINVAL); diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index c2e8327010f9..114e828ae027 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -413,10 +413,17 @@ struct bpf_link_create_opts { struct { __u64 bpf_cookie; } perf_event; + struct { + __u64 syms; + __u64 addrs; + __u32 cnt; + __u32 flags; + __u64 bpf_cookies; + } fprobe; }; size_t :0; }; -#define bpf_link_create_opts__last_field perf_event +#define bpf_link_create_opts__last_field fprobe.bpf_cookies =20 LIBBPF_API int bpf_link_create(int prog_fd, int target_fd, enum bpf_attach_type attach_type, --=20 2.34.1 From nobody Mon Jun 29 22:11:04 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9AF0C433FE for ; Wed, 2 Feb 2022 13:54:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231156AbiBBNyV (ORCPT ); Wed, 2 Feb 2022 08:54:21 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:24242 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229535AbiBBNyP (ORCPT ); Wed, 2 Feb 2022 08:54:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643810055; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oO9kCxYzu+S91SbPZpT/UZqtqo7+3S7Tz0558nP+sM4=; b=fADYtXwDyZE3T8rGeH59pclR012WdVNgye3r58n3myzKPxo96XfI9aQWN+m9cUHnB0xaEQ CsGMzYn4sn9ZrkMeGlyl0v5O4zj+XYu22Outf9KfTUhM5brhKOFJyUvl5mmZt/Y6AED4yx a75elTzAG/ojJxT1QZmTMsp+cuaM3+U= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-378-uoaelx7wN_O15rf4uRrMtQ-1; Wed, 02 Feb 2022 08:54:14 -0500 X-MC-Unique: uoaelx7wN_O15rf4uRrMtQ-1 Received: by mail-ej1-f72.google.com with SMTP id q19-20020a1709064c9300b006b39291ff3eso8175863eju.5 for ; Wed, 02 Feb 2022 05:54:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oO9kCxYzu+S91SbPZpT/UZqtqo7+3S7Tz0558nP+sM4=; b=2mwn7WDid/0DiFtvkEhO7C6j6/ec+HhgEynZdjQcVZt9rXWV3pNcToplRlNgpMJbGG ApcGqg2sQrhN9uWuwjPBQ+60Oyhrz4HHsxxHQhIEFQ72V6Dy1FW3uRcH33ymFS+4JxSN TmawwSichZ8RbpClZfQDixHK+x+Tm4+2PAcNaQKZp1l6rER+Dx+cTixCYPfVVPgZ6iXW wiYXSsljpi7fhRFuZj95OBr/O3dLyxEWPcM7tiuulJYB0d14KtDYsTrWWVn6ZA/llZtF XZvFfjQnH//Bv5CAIkPJglzniBHXgEKanpN8yUAgQ4jpRU7TtVmKkNLcxPjhagsYvBMl 0svg== X-Gm-Message-State: AOAM533QR8Kw18Rd8D31103VaFhLFGKle9lSQjAKrjolCWPRXdA0dA2t eNuTj77uT6UN1aTnNMdW6lk/b8jDQRK6814c1XXLo/MqUggrp1Nr5/Jx7HvNl5/2gnHoX0FTGCO 77AeSythk5N8Lhjlb5jXREFew X-Received: by 2002:a50:ee09:: with SMTP id g9mr1824085eds.387.1643810052945; Wed, 02 Feb 2022 05:54:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJz1Sui85jvHnptZNNYz6ZXNsvi0FZvRDG85OezXcljt811sTnYz4JPS1Z27F+E8W9jeUIvoQg== X-Received: by 2002:a50:ee09:: with SMTP id g9mr1824065eds.387.1643810052726; Wed, 02 Feb 2022 05:54:12 -0800 (PST) Received: from krava.redhat.com (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id by22sm15763502ejb.5.2022.02.02.05.54.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 05:54:12 -0800 (PST) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Jiri Olsa Subject: [PATCH 6/8] libbpf: Add bpf_program__attach_kprobe_opts for multi kprobes Date: Wed, 2 Feb 2022 14:53:31 +0100 Message-Id: <20220202135333.190761-7-jolsa@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202135333.190761-1-jolsa@kernel.org> References: <20220202135333.190761-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adding support to bpf_program__attach_kprobe_opts to load kprobes to multiple functions. If the kprobe program has BPF_TRACE_FPROBE as expected_attach_type it will use the new fprobe link to attach the program. In this case it will use 'func_name' as pattern for functions to attach. Adding also support to use '*' wildcard in 'kprobe/kretprobe' section name by SEC macro, like: SEC("kprobe/bpf_fentry_test*") SEC("kretprobe/bpf_fentry_test*") This will set kprobe's expected_attach_type to BPF_TRACE_FPROBE, and attach it to provided functions pattern. Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 136 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 7d595cfd03bc..6b343ef77ed8 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -8607,13 +8607,15 @@ static struct bpf_link *attach_trace(const struct b= pf_program *prog, long cookie static struct bpf_link *attach_lsm(const struct bpf_program *prog, long co= okie); static struct bpf_link *attach_iter(const struct bpf_program *prog, long c= ookie); =20 +static int init_kprobe(struct bpf_program *prog, long cookie); + static const struct bpf_sec_def section_defs[] =3D { SEC_DEF("socket", SOCKET_FILTER, 0, SEC_NONE | SEC_SLOPPY_PFX), SEC_DEF("sk_reuseport/migrate", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_= MIGRATE, SEC_ATTACHABLE | SEC_SLOPPY_PFX), SEC_DEF("sk_reuseport", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT, SEC_ATTAC= HABLE | SEC_SLOPPY_PFX), - SEC_DEF("kprobe/", KPROBE, 0, SEC_NONE, attach_kprobe), + SEC_DEF("kprobe/", KPROBE, 0, SEC_NONE, attach_kprobe, .init_fn =3D init= _kprobe), SEC_DEF("uprobe/", KPROBE, 0, SEC_NONE), - SEC_DEF("kretprobe/", KPROBE, 0, SEC_NONE, attach_kprobe), + SEC_DEF("kretprobe/", KPROBE, 0, SEC_NONE, attach_kprobe, .init_fn =3D i= nit_kprobe), SEC_DEF("uretprobe/", KPROBE, 0, SEC_NONE), SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE), SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE | SEC_SLOPPY_PFX | SEC_DEPR= ECATED), @@ -10031,6 +10033,123 @@ static int perf_event_kprobe_open_legacy(const ch= ar *probe_name, bool retprobe, return pfd; } =20 +struct fprobe_resolve { + const char *name; + __u64 *addrs; + __u32 alloc; + __u32 cnt; +}; + +static bool glob_matches(const char *glob, const char *s) +{ + int n =3D strlen(glob); + + if (n =3D=3D 1 && glob[0] =3D=3D '*') + return true; + + if (glob[0] =3D=3D '*' && glob[n - 1] =3D=3D '*') { + const char *subs; + /* substring match */ + + /* this is hacky, but we don't want to allocate + * for no good reason + */ + ((char *)glob)[n - 1] =3D '\0'; + subs =3D strstr(s, glob + 1); + ((char *)glob)[n - 1] =3D '*'; + + return subs !=3D NULL; + } else if (glob[0] =3D=3D '*') { + size_t nn =3D strlen(s); + /* suffix match */ + + /* too short for a given suffix */ + if (nn < n - 1) + return false; + return strcmp(s + nn - (n - 1), glob + 1) =3D=3D 0; + } else if (glob[n - 1] =3D=3D '*') { + /* prefix match */ + return strncmp(s, glob, n - 1) =3D=3D 0; + } else { + /* exact match */ + return strcmp(glob, s) =3D=3D 0; + } +} + +static int resolve_fprobe_cb(void *arg, unsigned long long sym_addr, + char sym_type, const char *sym_name) +{ + struct fprobe_resolve *res =3D arg; + __u64 *p; + + if (!glob_matches(res->name, sym_name)) + return 0; + + if (res->cnt =3D=3D res->alloc) { + res->alloc =3D max((__u32) 16, res->alloc * 3 / 2); + p =3D libbpf_reallocarray(res->addrs, res->alloc, sizeof(__u32)); + if (!p) + return -ENOMEM; + res->addrs =3D p; + } + res->addrs[res->cnt++] =3D sym_addr; + return 0; +} + +static struct bpf_link * +attach_fprobe_opts(const struct bpf_program *prog, + const char *func_name, + const struct bpf_kprobe_opts *kopts) +{ + DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts); + struct fprobe_resolve res =3D { + .name =3D func_name, + }; + struct bpf_link *link =3D NULL; + char errmsg[STRERR_BUFSIZE]; + int err, link_fd, prog_fd; + bool retprobe; + + err =3D libbpf__kallsyms_parse(&res, resolve_fprobe_cb); + if (err) + goto error; + if (!res.cnt) { + err =3D -ENOENT; + goto error; + } + + retprobe =3D OPTS_GET(kopts, retprobe, false); + + opts.fprobe.addrs =3D (__u64) res.addrs; + opts.fprobe.cnt =3D res.cnt; + opts.flags =3D retprobe ? BPF_F_FPROBE_RETURN : 0; + + link =3D calloc(1, sizeof(*link)); + if (!link) { + err =3D -ENOMEM; + goto error; + } + link->detach =3D &bpf_link__detach_fd; + + prog_fd =3D bpf_program__fd(prog); + link_fd =3D bpf_link_create(prog_fd, 0, BPF_TRACE_FPROBE, &opts); + if (link_fd < 0) { + err =3D -errno; + pr_warn("prog '%s': failed to attach to %s: %s\n", + prog->name, res.name, + libbpf_strerror_r(err, errmsg, sizeof(errmsg))); + goto error; + } + link->fd =3D link_fd; + free(res.addrs); + return link; + +error: + free(link); + free(res.addrs); + return libbpf_err_ptr(err); +} + struct bpf_link * bpf_program__attach_kprobe_opts(const struct bpf_program *prog, const char *func_name, @@ -10047,6 +10166,9 @@ bpf_program__attach_kprobe_opts(const struct bpf_pr= ogram *prog, if (!OPTS_VALID(opts, bpf_kprobe_opts)) return libbpf_err_ptr(-EINVAL); =20 + if (prog->expected_attach_type =3D=3D BPF_TRACE_FPROBE) + return attach_fprobe_opts(prog, func_name, opts); + retprobe =3D OPTS_GET(opts, retprobe, false); offset =3D OPTS_GET(opts, offset, 0); pe_opts.bpf_cookie =3D OPTS_GET(opts, bpf_cookie, 0); @@ -10112,6 +10234,14 @@ struct bpf_link *bpf_program__attach_kprobe(const = struct bpf_program *prog, return bpf_program__attach_kprobe_opts(prog, func_name, &opts); } =20 +static int init_kprobe(struct bpf_program *prog, long cookie) +{ + /* If we have wildcard, switch to fprobe link. */ + if (strchr(prog->sec_name, '*')) + bpf_program__set_expected_attach_type(prog, BPF_TRACE_FPROBE); + return 0; +} + static struct bpf_link *attach_kprobe(const struct bpf_program *prog, long= cookie) { DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts); @@ -10127,7 +10257,7 @@ static struct bpf_link *attach_kprobe(const struct = bpf_program *prog, long cooki else func_name =3D prog->sec_name + sizeof("kprobe/") - 1; =20 - n =3D sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset); + n =3D sscanf(func_name, "%m[a-zA-Z0-9_.*]+%li", &func, &offset); if (n < 1) { err =3D -EINVAL; pr_warn("kprobe name is invalid: %s\n", func_name); --=20 2.34.1 From nobody Mon Jun 29 22:11:04 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7EFE1C43217 for ; Wed, 2 Feb 2022 13:54:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344684AbiBBNyb (ORCPT ); Wed, 2 Feb 2022 08:54:31 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:42246 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344628AbiBBNyV (ORCPT ); Wed, 2 Feb 2022 08:54:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643810061; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bLaeMerEotaa1PvsxAUE2R3tXfnbR0hxnx4SsL7NZbg=; b=T5AvYGZjIs9OJrp4dJ2aMexJ8FB4RHMhD63MfGewDrE6m8GARDkPqTywm+q8HbGRY30/D/ TTSn6/Ta1I7XJRa9Sxszg/Bbg51AfyBkf6rGL1UroKtDtka5n2hSS5y7qicXmJhVu0NAfj 35UwfjD1xmII8HZcLkkS3k15R6VOvgY= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-83-huYZw9RfNjSvN4Q3VUswIg-1; Wed, 02 Feb 2022 08:54:20 -0500 X-MC-Unique: huYZw9RfNjSvN4Q3VUswIg-1 Received: by mail-ej1-f69.google.com with SMTP id h22-20020a1709060f5600b006b11a2d3dcfso8182764ejj.4 for ; Wed, 02 Feb 2022 05:54:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bLaeMerEotaa1PvsxAUE2R3tXfnbR0hxnx4SsL7NZbg=; b=idbFTDY6yCpI7PZ66s4DWlTw3Ds8oa5LGzouyX3NpQjxBOeNZsuWPtamj+vxWibiCY NhS6RBDGCqM8TKzSbne89eqW/fKrWqqgec5g3ZYZY7kRlWXbE1jjeOxIjDcMa1OCuGAv UjclKghuHRIPlA+DfhM3NTnskpTLalNN4ojLtGiPF6pau5f5cY1XMeCsLqsyPgeb4tQr SsZU5fjGxSROSSaDj3crExfCSwdLWf+VSx+YUMSFUxGx09rNp8PyE1z7mRfpNK4kwKCK lVL3Mmd+uQGSrA7neleb7t4mGVvyLNotOl8DqqR1Y1iUdoB+QSH/RQFc8XyXZ4VyQika sXCw== X-Gm-Message-State: AOAM530kh2LiQ13I6eYaURX6LxgbVTShH5wdD+yblY/NU6vdJUD+b1Ed CxhIMCQ243QVaA8lyCiwiH+ayywalHCPEGLh/5KjOqzdZkOVIqCkaFkHvBEvzQs1zzItSO2+rYY Pw/de7urQecosWioKFuyl2gji X-Received: by 2002:a17:907:7da8:: with SMTP id oz40mr19493095ejc.328.1643810059321; Wed, 02 Feb 2022 05:54:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJz1VYc7yglvMYwmqUOo2giJKTPpWpi3bTqqc0M8BtqUBkJ+RuokTlGaYir/ixadM+hilTAryg== X-Received: by 2002:a17:907:7da8:: with SMTP id oz40mr19493065ejc.328.1643810059057; Wed, 02 Feb 2022 05:54:19 -0800 (PST) Received: from krava.redhat.com (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id jl17sm16087939ejc.13.2022.02.02.05.54.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 05:54:18 -0800 (PST) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Jiri Olsa Subject: [PATCH 7/8] selftest/bpf: Add fprobe attach test Date: Wed, 2 Feb 2022 14:53:32 +0100 Message-Id: <20220202135333.190761-8-jolsa@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202135333.190761-1-jolsa@kernel.org> References: <20220202135333.190761-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adding kprobe attach test that uses new fprobe interface to attach kprobe program to multiple functions. The test is attaching to bpf_fentry_test* functions and uses single trampoline program bpf_prog_test_run to trigger bpf_fentry_test* functions. Signed-off-by: Jiri Olsa --- .../selftests/bpf/prog_tests/fprobe_test.c | 117 ++++++++++++++++++ tools/testing/selftests/bpf/progs/fprobe.c | 58 +++++++++ 2 files changed, 175 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/fprobe_test.c create mode 100644 tools/testing/selftests/bpf/progs/fprobe.c diff --git a/tools/testing/selftests/bpf/prog_tests/fprobe_test.c b/tools/t= esting/selftests/bpf/prog_tests/fprobe_test.c new file mode 100644 index 000000000000..dcbde37ec369 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/fprobe_test.c @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "fprobe.skel.h" +#include "trace_helpers.h" + +static void test_skel_api(void) +{ + struct fprobe *skel =3D NULL; + __u32 duration =3D 0, retval; + int err, prog_fd; + + skel =3D fprobe__open_and_load(); + if (!ASSERT_OK_PTR(skel, "fprobe__open_and_load")) + goto cleanup; + + err =3D fprobe__attach(skel); + if (!ASSERT_OK(err, "fprobe__attach")) + goto cleanup; + + prog_fd =3D bpf_program__fd(skel->progs.test1); + err =3D bpf_prog_test_run(prog_fd, 1, NULL, 0, + NULL, NULL, &retval, &duration); + ASSERT_OK(err, "test_run"); + ASSERT_EQ(retval, 0, "test_run"); + + ASSERT_EQ(skel->bss->test2_result, 8, "test2_result"); + ASSERT_EQ(skel->bss->test3_result, 8, "test3_result"); + +cleanup: + fprobe__destroy(skel); +} + +static void test_link_api(struct bpf_link_create_opts *opts) +{ + int err, prog_fd, link1_fd =3D -1, link2_fd =3D -1; + struct fprobe *skel =3D NULL; + __u32 duration =3D 0, retval; + + skel =3D fprobe__open_and_load(); + if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load")) + goto cleanup; + + prog_fd =3D bpf_program__fd(skel->progs.test2); + link1_fd =3D bpf_link_create(prog_fd, 0, BPF_TRACE_FPROBE, opts); + if (!ASSERT_GE(link1_fd, 0, "link_fd")) + goto cleanup; + + opts->fprobe.flags =3D BPF_F_FPROBE_RETURN; + prog_fd =3D bpf_program__fd(skel->progs.test3); + link2_fd =3D bpf_link_create(prog_fd, 0, BPF_TRACE_FPROBE, opts); + if (!ASSERT_GE(link2_fd, 0, "link_fd")) + goto cleanup; + + skel->bss->test2_result =3D 0; + skel->bss->test3_result =3D 0; + + prog_fd =3D bpf_program__fd(skel->progs.test1); + err =3D bpf_prog_test_run(prog_fd, 1, NULL, 0, + NULL, NULL, &retval, &duration); + ASSERT_OK(err, "test_run"); + ASSERT_EQ(retval, 0, "test_run"); + + ASSERT_EQ(skel->bss->test2_result, 8, "test2_result"); + ASSERT_EQ(skel->bss->test3_result, 8, "test3_result"); + +cleanup: + if (link1_fd !=3D -1) + close(link1_fd); + if (link2_fd !=3D -1) + close(link2_fd); + fprobe__destroy(skel); +} + +static void test_link_api_addrs(void) +{ + DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts); + __u64 addrs[8]; + + kallsyms_find("bpf_fentry_test1", &addrs[0]); + kallsyms_find("bpf_fentry_test2", &addrs[1]); + kallsyms_find("bpf_fentry_test3", &addrs[2]); + kallsyms_find("bpf_fentry_test4", &addrs[3]); + kallsyms_find("bpf_fentry_test5", &addrs[4]); + kallsyms_find("bpf_fentry_test6", &addrs[5]); + kallsyms_find("bpf_fentry_test7", &addrs[6]); + kallsyms_find("bpf_fentry_test8", &addrs[7]); + + opts.fprobe.addrs =3D (__u64) addrs; + opts.fprobe.cnt =3D 8; + test_link_api(&opts); +} + +static void test_link_api_syms(void) +{ + DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts); + const char *syms[8] =3D { + "bpf_fentry_test1", + "bpf_fentry_test2", + "bpf_fentry_test3", + "bpf_fentry_test4", + "bpf_fentry_test5", + "bpf_fentry_test6", + "bpf_fentry_test7", + "bpf_fentry_test8", + }; + + opts.fprobe.syms =3D (__u64) syms; + opts.fprobe.cnt =3D 8; + test_link_api(&opts); +} + +void test_fprobe_test(void) +{ + test_skel_api(); + test_link_api_syms(); + test_link_api_addrs(); +} diff --git a/tools/testing/selftests/bpf/progs/fprobe.c b/tools/testing/sel= ftests/bpf/progs/fprobe.c new file mode 100644 index 000000000000..baf7086203f9 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/fprobe.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +char _license[] SEC("license") =3D "GPL"; + +extern const void bpf_fentry_test1 __ksym; +extern const void bpf_fentry_test2 __ksym; +extern const void bpf_fentry_test3 __ksym; +extern const void bpf_fentry_test4 __ksym; +extern const void bpf_fentry_test5 __ksym; +extern const void bpf_fentry_test6 __ksym; +extern const void bpf_fentry_test7 __ksym; +extern const void bpf_fentry_test8 __ksym; + +/* No tests, just to trigger bpf_fentry_test* through tracing test_run */ +SEC("fentry/bpf_modify_return_test") +int BPF_PROG(test1) +{ + return 0; +} + +__u64 test2_result =3D 0; + +SEC("kprobe/bpf_fentry_test*") +int test2(struct pt_regs *ctx) +{ + __u64 addr =3D bpf_get_func_ip(ctx); + + test2_result +=3D (const void *) addr =3D=3D &bpf_fentry_test1 || + (const void *) addr =3D=3D &bpf_fentry_test2 || + (const void *) addr =3D=3D &bpf_fentry_test3 || + (const void *) addr =3D=3D &bpf_fentry_test4 || + (const void *) addr =3D=3D &bpf_fentry_test5 || + (const void *) addr =3D=3D &bpf_fentry_test6 || + (const void *) addr =3D=3D &bpf_fentry_test7 || + (const void *) addr =3D=3D &bpf_fentry_test8; + return 0; +} + +__u64 test3_result =3D 0; + +SEC("kretprobe/bpf_fentry_test*") +int test3(struct pt_regs *ctx) +{ + __u64 addr =3D bpf_get_func_ip(ctx); + + test3_result +=3D (const void *) addr =3D=3D &bpf_fentry_test1 || + (const void *) addr =3D=3D &bpf_fentry_test2 || + (const void *) addr =3D=3D &bpf_fentry_test3 || + (const void *) addr =3D=3D &bpf_fentry_test4 || + (const void *) addr =3D=3D &bpf_fentry_test5 || + (const void *) addr =3D=3D &bpf_fentry_test6 || + (const void *) addr =3D=3D &bpf_fentry_test7 || + (const void *) addr =3D=3D &bpf_fentry_test8; + return 0; +} --=20 2.34.1 From nobody Mon Jun 29 22:11:04 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4CCBDC433F5 for ; Wed, 2 Feb 2022 13:54:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344731AbiBBNym (ORCPT ); Wed, 2 Feb 2022 08:54:42 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:57031 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344738AbiBBNy2 (ORCPT ); Wed, 2 Feb 2022 08:54:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643810067; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ndZKvuTAfuQTlyvC6PEP1xi3KVMCTEpQydu3XzbTIRw=; b=B+F3+zJYMQGXEcQEKAbkiBr4Xwk3mI8zsaxPXEudX+YwaZuKxJ2WARBO3SZ1JCrdts7Y9n gw5lghS74xdgUTXd0FJ5E/uQkwPfbbZ4WM3NrqXSnECQWeU3MGXjnDz3n1n94M2LqpRjPL G7mdP9lq5ixEUzif55s1rbQYcHe5nE4= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-126-zVvEhTUKOXKWxD4i5HOVWQ-1; Wed, 02 Feb 2022 08:54:26 -0500 X-MC-Unique: zVvEhTUKOXKWxD4i5HOVWQ-1 Received: by mail-ed1-f70.google.com with SMTP id h21-20020aa7c955000000b0040390b2bfc5so10425845edt.15 for ; Wed, 02 Feb 2022 05:54:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ndZKvuTAfuQTlyvC6PEP1xi3KVMCTEpQydu3XzbTIRw=; b=e5dgxgdk+vJ+wBlESOj5MOv/0wKxNIBYzvId/g+/LAMNRtAOfZDsbWCfhF7CwL0sOE e2xFBye9PbcDrMOEc37HJDVS0okWWCrHwcht2ytyJ1l/zYIQl74RS73Jpmndni8DrDD+ 8okSNWH7yaNo96+5IN3VPmkISFHM5JUanHHdevhfpbaLdhRb6aWygeHKWZZHyo9JBXpI vQFxrPcX9RykLuYgLSvnO+6OFZIfHD0QMM20ObMtH7cekO5i2NeRpsl/zv3y3u3pC6zA VOXERiKYKbmuppLekTuWE9Jnjn0J8AVqvfhzq3IN/oqs1nsIK2B3XsUKxJky7BGGd0+c vyDw== X-Gm-Message-State: AOAM532MWeWboj3UQQMSqD9fft42MziYsGdNfMr6W0pxWgvST18ky4EG pB8/YkI4xA/3OQ1DgUpTkzSNnll2dDB5j4JVvuxDkB9X/v8sbw6/uPxxzExyq2MHwJX3OPv6r+e 87USPshqkcfbDeIRtcTHJrmt+ X-Received: by 2002:a05:6402:16cf:: with SMTP id r15mr30306469edx.406.1643810065536; Wed, 02 Feb 2022 05:54:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJy6fmv5Qiwb3TMVnK4uiLl9uYsqBio1+nlTWUQc1NRXW+Y88hbgmFWbH25fj5/DYfJhAAS34w== X-Received: by 2002:a05:6402:16cf:: with SMTP id r15mr30306452edx.406.1643810065387; Wed, 02 Feb 2022 05:54:25 -0800 (PST) Received: from krava.redhat.com (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id kw5sm8493321ejc.140.2022.02.02.05.54.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 05:54:24 -0800 (PST) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Jiri Olsa Subject: [PATCH 8/8] selftest/bpf: Add fprobe test for bpf_cookie values Date: Wed, 2 Feb 2022 14:53:33 +0100 Message-Id: <20220202135333.190761-9-jolsa@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202135333.190761-1-jolsa@kernel.org> References: <20220202135333.190761-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adding bpf_cookie test for kprobe attached by fprobe link. Signed-off-by: Jiri Olsa --- .../selftests/bpf/prog_tests/bpf_cookie.c | 73 +++++++++++++++++++ .../selftests/bpf/progs/fprobe_bpf_cookie.c | 62 ++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.c diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c b/tools/te= sting/selftests/bpf/prog_tests/bpf_cookie.c index cd10df6cd0fc..bf70d859c598 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c @@ -7,6 +7,7 @@ #include #include #include "test_bpf_cookie.skel.h" +#include "fprobe_bpf_cookie.skel.h" =20 /* uprobe attach point */ static void trigger_func(void) @@ -63,6 +64,76 @@ static void kprobe_subtest(struct test_bpf_cookie *skel) bpf_link__destroy(retlink2); } =20 +static void fprobe_subtest(void) +{ + DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts); + int err, prog_fd, link1_fd =3D -1, link2_fd =3D -1; + struct fprobe_bpf_cookie *skel =3D NULL; + __u32 duration =3D 0, retval; + __u64 addrs[8], cookies[8]; + + skel =3D fprobe_bpf_cookie__open_and_load(); + if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load")) + goto cleanup; + + kallsyms_find("bpf_fentry_test1", &addrs[0]); + kallsyms_find("bpf_fentry_test2", &addrs[1]); + kallsyms_find("bpf_fentry_test3", &addrs[2]); + kallsyms_find("bpf_fentry_test4", &addrs[3]); + kallsyms_find("bpf_fentry_test5", &addrs[4]); + kallsyms_find("bpf_fentry_test6", &addrs[5]); + kallsyms_find("bpf_fentry_test7", &addrs[6]); + kallsyms_find("bpf_fentry_test8", &addrs[7]); + + cookies[0] =3D 1; + cookies[1] =3D 2; + cookies[2] =3D 3; + cookies[3] =3D 4; + cookies[4] =3D 5; + cookies[5] =3D 6; + cookies[6] =3D 7; + cookies[7] =3D 8; + + opts.fprobe.addrs =3D (__u64) &addrs; + opts.fprobe.cnt =3D 8; + opts.fprobe.bpf_cookies =3D (__u64) &cookies; + prog_fd =3D bpf_program__fd(skel->progs.test2); + + link1_fd =3D bpf_link_create(prog_fd, 0, BPF_TRACE_FPROBE, &opts); + if (!ASSERT_GE(link1_fd, 0, "link1_fd")) + return; + + cookies[0] =3D 8; + cookies[1] =3D 7; + cookies[2] =3D 6; + cookies[3] =3D 5; + cookies[4] =3D 4; + cookies[5] =3D 3; + cookies[6] =3D 2; + cookies[7] =3D 1; + + opts.flags =3D BPF_F_FPROBE_RETURN; + prog_fd =3D bpf_program__fd(skel->progs.test3); + + link2_fd =3D bpf_link_create(prog_fd, 0, BPF_TRACE_FPROBE, &opts); + if (!ASSERT_GE(link2_fd, 0, "link2_fd")) + goto cleanup; + + prog_fd =3D bpf_program__fd(skel->progs.test1); + err =3D bpf_prog_test_run(prog_fd, 1, NULL, 0, + NULL, NULL, &retval, &duration); + ASSERT_OK(err, "test_run"); + ASSERT_EQ(retval, 0, "test_run"); + + ASSERT_EQ(skel->bss->test2_result, 8, "test2_result"); + ASSERT_EQ(skel->bss->test3_result, 8, "test3_result"); + +cleanup: + close(link1_fd); + close(link2_fd); + fprobe_bpf_cookie__destroy(skel); +} + static void uprobe_subtest(struct test_bpf_cookie *skel) { DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts); @@ -249,6 +320,8 @@ void test_bpf_cookie(void) =20 if (test__start_subtest("kprobe")) kprobe_subtest(skel); + if (test__start_subtest("rawkprobe")) + fprobe_subtest(); if (test__start_subtest("uprobe")) uprobe_subtest(skel); if (test__start_subtest("tracepoint")) diff --git a/tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.c b/tools/= testing/selftests/bpf/progs/fprobe_bpf_cookie.c new file mode 100644 index 000000000000..42cb109e5a30 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +char _license[] SEC("license") =3D "GPL"; + +extern const void bpf_fentry_test1 __ksym; +extern const void bpf_fentry_test2 __ksym; +extern const void bpf_fentry_test3 __ksym; +extern const void bpf_fentry_test4 __ksym; +extern const void bpf_fentry_test5 __ksym; +extern const void bpf_fentry_test6 __ksym; +extern const void bpf_fentry_test7 __ksym; +extern const void bpf_fentry_test8 __ksym; + +/* No tests, just to trigger bpf_fentry_test* through tracing test_run */ +SEC("fentry/bpf_modify_return_test") +int BPF_PROG(test1) +{ + return 0; +} + +__u64 test2_result =3D 0; + +SEC("kprobe/bpf_fentry_test*") +int test2(struct pt_regs *ctx) +{ + __u64 cookie =3D bpf_get_attach_cookie(ctx); + __u64 addr =3D bpf_get_func_ip(ctx); + + test2_result +=3D (const void *) addr =3D=3D &bpf_fentry_test1 && cookie = =3D=3D 1; + test2_result +=3D (const void *) addr =3D=3D &bpf_fentry_test2 && cookie = =3D=3D 2; + test2_result +=3D (const void *) addr =3D=3D &bpf_fentry_test3 && cookie = =3D=3D 3; + test2_result +=3D (const void *) addr =3D=3D &bpf_fentry_test4 && cookie = =3D=3D 4; + test2_result +=3D (const void *) addr =3D=3D &bpf_fentry_test5 && cookie = =3D=3D 5; + test2_result +=3D (const void *) addr =3D=3D &bpf_fentry_test6 && cookie = =3D=3D 6; + test2_result +=3D (const void *) addr =3D=3D &bpf_fentry_test7 && cookie = =3D=3D 7; + test2_result +=3D (const void *) addr =3D=3D &bpf_fentry_test8 && cookie = =3D=3D 8; + + return 0; +} + +__u64 test3_result =3D 0; + +SEC("kretprobe/bpf_fentry_test*") +int test3(struct pt_regs *ctx) +{ + __u64 cookie =3D bpf_get_attach_cookie(ctx); + __u64 addr =3D bpf_get_func_ip(ctx); + + test3_result +=3D (const void *) addr =3D=3D &bpf_fentry_test1 && cookie = =3D=3D 8; + test3_result +=3D (const void *) addr =3D=3D &bpf_fentry_test2 && cookie = =3D=3D 7; + test3_result +=3D (const void *) addr =3D=3D &bpf_fentry_test3 && cookie = =3D=3D 6; + test3_result +=3D (const void *) addr =3D=3D &bpf_fentry_test4 && cookie = =3D=3D 5; + test3_result +=3D (const void *) addr =3D=3D &bpf_fentry_test5 && cookie = =3D=3D 4; + test3_result +=3D (const void *) addr =3D=3D &bpf_fentry_test6 && cookie = =3D=3D 3; + test3_result +=3D (const void *) addr =3D=3D &bpf_fentry_test7 && cookie = =3D=3D 2; + test3_result +=3D (const void *) addr =3D=3D &bpf_fentry_test8 && cookie = =3D=3D 1; + + return 0; +} --=20 2.34.1