From nobody Wed Apr 1 13:52:04 2026 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8311C3ED5DD for ; Tue, 31 Mar 2026 09:41:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774950071; cv=none; b=jsoityY6AWlvNaANCunhCzExygs644cFFHGQfPjq4RP+d5Fo6eRy6DqbkvGS9UfKVBKINbFR4KioTF6HecY931FC3yJ4dI8yfoS3uMbzIHv/dfzWuUK82zV1Zn9GFZw+dY4c5kANc18xKwH1623efTEft7CCrwFZqK5Pnye9dNM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774950071; c=relaxed/simple; bh=zjNVLLXf4FRHJi7xCfsZ+zGLUv7oWEI5xrhgjiSQRC8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y9IBYxGYh3VPFPvlAet3qo2G2fCmfhg0ndMN7IP/B3J9rMAaOSV/o0HNueYdKo3dW8xA450lnwflgbMQvrY7i5d/2QGaYp+KTpTA2IZeNGpUGqn+zptg3JiIfUh8itYtnPBlYzPJRbKdwnnmJT9sVwC1TR9hNhnox+R+RFdDjQc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=NGa2xUSe; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="NGa2xUSe" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-48374014a77so65529775e9.3 for ; Tue, 31 Mar 2026 02:41:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1774950068; x=1775554868; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YyqEXQLqFypyPuwNGUA2n6cHuK6us3URoKl8kCVt9ls=; b=NGa2xUSeCkijHWUQ4SEODgDs4eyiM/buangUwXWG0DoIJsFrEyZLw9glFQ/0r1daUD 9svMB4v8WJMgMHQRO3MbRrnKk1vMcYG/oewh01uF7bMhFbl66K6mDKSH8SNyZTU0QKzg Qe0D+QQ6W4vMK6ua7R92/rQ6CpW+DJXBfpb7d2nBMN6iQEf60Qqi4aYX0uR2I6GVgBwx inf4NmoffbyCx9zUCQQ6UlsjQxdMbm3Bl/cGgN2m+PxaSTi+Vn9TM9M74jv//QIefMa/ IlGuVVspisUwVE46ZHt79jfCXMO5fxKwyko90hY8J/LmRD9Kosaz6V+EXKxjnvnh3s0c LyZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774950068; x=1775554868; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=YyqEXQLqFypyPuwNGUA2n6cHuK6us3URoKl8kCVt9ls=; b=cnuTtyW4hXDGmqxnv5b6XyQEK7zjpidzhawEmaulksfwEwQtnrAS4cFfvBwkTHIbKX aBL+jAWe5lFv1eRZNIiO057Koz0Y55eF93chq7UjiS0PJXDGrb4E8aIACFO2535fwplm JNoL4sKK1o2oXJq/rouCq5ESX/rbUkUstr6IDNEK+bm3cSJXmiStehuDxa8C/89CgbUR MuF+qAG2IQxD9Zg0imlCJ9Iiftw07zJN2PnmV9QAhL3+vttsXOrjTjL6DxysroEAQLFB Lyi7pPjddrU4KutHcQ9C0wk0ccOw7zlSUNwRL+MfPmcbW84pHUzH4sKDRM20HAn4iXL1 jjxQ== X-Forwarded-Encrypted: i=1; AJvYcCUfiPcHTl+7/fl2jz8T9eLczK1GuTG6XuYW1VNnyzglVXLeC/aZcX+0LNfB1WyqNHUppxN22gMNbnz0Wuo=@vger.kernel.org X-Gm-Message-State: AOJu0YwsqsJ3YQBx9IZreTT/BVV3afe4vGeZgNBDiSRC7PLxjJ8jDzAS 5DAJ2u/9dFAmvqmo2MFmMi6s3V9Bja+KkjfkDevwf+zIdCoyMmB99hOP47wDo7Ss850= X-Gm-Gg: ATEYQzyq9AwR3u/YRhiBJGWylx69NMTDewvVSTYTo9bDEqFTDdO235cYCkgnxqCaJkD RRB8bPF2BNGYnoBOOHem3TMUi9VC7u8bIGoVcccwlTnHDAl8AVm3ZocqG+0XaHs84BXu5dvv98B jYG9RlVX/1/cwEOAiIda5WIxiyk+To8WHCGqrBa8k37W1UT6rGoJ73/dyfwjhnqTZXi45NPdl/5 9NWf7AFD1QJcD66nRWqxltHxKmA9DelELXGmDY8njgGrRj4efRqDHQZ7QsMIBOE8eq1KGjpZSQr gUj1psSrfnVgWElh8ZiQ51hLnQy+QMwY4CHFCRsCS46sf4MyvLec6IMOEZJ79mILV9jy/MPxZK3 qHEthfuW1AN1WsSWOSBOsu7HnccN7ilRwMRJz/gChBh7A6JY16VNSmOgqvB5lJPFFt/obEQsstj jvWaF/++yS6HRbNO4XFJNbWxmTkBLP3FFL4mgGRkpArXKm8/CTfK9Td5kYvpaamg== X-Received: by 2002:a05:600c:1d1f:b0:486:d76c:fa57 with SMTP id 5b1f17b1804b1-48727eb81bbmr291469285e9.17.1774950067635; Tue, 31 Mar 2026 02:41:07 -0700 (PDT) Received: from f16.localdomain ([121.167.230.140]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b2427afbe0sm130079565ad.72.2026.03.31.02.41.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2026 02:41:06 -0700 (PDT) From: Hoyeon Lee To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau Cc: Eduard Zingerman , Kumar Kartikeya Dwivedi , Song Liu , Yonghong Song , Jiri Olsa , Shuah Khan , Feng Yang , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v3 1/2] libbpf: clarify raw-address single kprobe attach behavior Date: Tue, 31 Mar 2026 18:37:46 +0900 Message-ID: <20260331094058.49229-2-hoyeon.lee@suse.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260331094058.49229-1-hoyeon.lee@suse.com> References: <20260331094058.49229-1-hoyeon.lee@suse.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" bpf_program__attach_kprobe_opts() documents single-kprobe attach through func_name, with an optional offset. For the PMU-based non-legacy path, func_name =3D NULL with an absolute address in offset already works as well, but that is not described in the API. This commit clarifies this existing non-legacy behavior. For PMU-based attach, callers can use func_name =3D NULL with an absolute address in offset as the raw-address form. For legacy tracefs/debugfs kprobes, reject this form explicitly. Signed-off-by: Hoyeon Lee --- tools/lib/bpf/libbpf.c | 33 ++++++++++++++++++++++++--------- tools/lib/bpf/libbpf.h | 3 ++- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 9ea41f40dc82..8cf90a073455 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -11523,7 +11524,8 @@ static int determine_uprobe_retprobe_bit(void) #define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32 =20 static int perf_event_open_probe(bool uprobe, bool retprobe, const char *n= ame, - uint64_t offset, int pid, size_t ref_ctr_off) + uint64_t offset_or_addr, int pid, + size_t ref_ctr_off) { const size_t attr_sz =3D sizeof(struct perf_event_attr); struct perf_event_attr attr; @@ -11558,7 +11560,7 @@ static int perf_event_open_probe(bool uprobe, bool = retprobe, const char *name, attr.type =3D type; attr.config |=3D (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT; attr.config1 =3D ptr_to_u64(name); /* kprobe_func or uprobe_path */ - attr.config2 =3D offset; /* kprobe_addr or probe_offset */ + attr.config2 =3D offset_or_addr; /* kprobe_addr or probe_offset */ =20 /* pid filter is meaningful only for uprobes */ pfd =3D syscall(__NR_perf_event_open, &attr, @@ -11648,6 +11650,15 @@ static void gen_probe_legacy_event_name(char *buf,= size_t buf_sz, } } =20 +static void gen_kprobe_target(char *buf, size_t buf_sz, const char *name, + uint64_t offset_or_addr) +{ + if (name) + snprintf(buf, buf_sz, "%s+0x%" PRIx64, name, offset_or_addr); + else + snprintf(buf, buf_sz, "0x%" PRIx64, offset_or_addr); +} + static int add_kprobe_event_legacy(const char *probe_name, bool retprobe, const char *kfunc_name, size_t offset) { @@ -11781,6 +11792,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_pr= ogram *prog, const struct bpf_kprobe_opts *opts) { DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); + char probe_target[KSYM_NAME_LEN + 32]; enum probe_attach_mode attach_mode; char *legacy_probe =3D NULL; struct bpf_link *link; @@ -11816,6 +11828,11 @@ bpf_program__attach_kprobe_opts(const struct bpf_p= rogram *prog, default: return libbpf_err_ptr(-EINVAL); } + if (!func_name && legacy) + return libbpf_err_ptr(-ENOTSUP); + + gen_kprobe_target(probe_target, sizeof(probe_target), + func_name, offset); =20 if (!legacy) { pfd =3D perf_event_open_probe(false /* uprobe */, retprobe, @@ -11835,21 +11852,19 @@ bpf_program__attach_kprobe_opts(const struct bpf_= program *prog, offset, -1 /* pid */); } if (pfd < 0) { - err =3D -errno; - pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n", + err =3D pfd; + pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n", prog->name, retprobe ? "kretprobe" : "kprobe", - func_name, offset, - errstr(err)); + probe_target, errstr(err)); goto err_out; } link =3D bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); err =3D libbpf_get_error(link); if (err) { close(pfd); - pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n", + pr_warn("prog '%s': failed to attach to %s '%s': %s\n", prog->name, retprobe ? "kretprobe" : "kprobe", - func_name, offset, - errstr(err)); + probe_target, errstr(err)); goto err_clean_legacy; } if (legacy) { diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 0be34852350f..f75f3ab0f20c 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -557,7 +557,7 @@ struct bpf_kprobe_opts { size_t sz; /* custom user-provided value fetchable through bpf_get_attach_cookie() */ __u64 bpf_cookie; - /* function's offset to install kprobe to */ + /* function offset, or raw address if func_name =3D=3D NULL (non-legacy) = */ size_t offset; /* kprobe is return probe */ bool retprobe; @@ -565,6 +565,7 @@ struct bpf_kprobe_opts { enum probe_attach_mode attach_mode; size_t :0; }; + #define bpf_kprobe_opts__last_field attach_mode =20 LIBBPF_API struct bpf_link * --=20 2.52.0