From nobody Wed Apr 1 12:38:01 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 From nobody Wed Apr 1 12:38:01 2026 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 21D253EDAB1 for ; Tue, 31 Mar 2026 09:41:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774950075; cv=none; b=Up3KzPzYySaMhXJaVrrLsi0nQmH4CUniuwlUQ4yudVg2WLASlm0GPWAPCJEEWxaLr4hNgrukIZu972LFmwdXx12EC0A2TWJQELDE0vYnRAoxWnfUEgQbwVlqb1At38gR049L/h4EvsXqSoSu7e7V/oe8efuQ8OvwlQSGPk2j73A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774950075; c=relaxed/simple; bh=rQBh02MaAajKMfKDC8YMNgw3hVbVvfm1kxuP49DEQG4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=S+y1EGf/qNm0YnSSlBpehBoARe5lfXTAo6VNz1dlVLNQsFi8t1zCjeJ1Lyv5AT3Mp9okRycck7y63kxTCdLhZ+SJRJzcmldmAdTGtaK7o8Ih42BKh4V3enl0dwflb647zOP/7T9cGfC2EQDD9VqSmHxuS5i+6TqmZzXCtqUQgxQ= 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=LHVA964S; arc=none smtp.client-ip=209.85.128.46 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="LHVA964S" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-486b96760easo60051415e9.2 for ; Tue, 31 Mar 2026 02:41:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1774950071; x=1775554871; 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=SsziD70VgpPgjTdfHcq7GvG3ra662CqVO4P1uVRU6Ec=; b=LHVA964SVm14THwbjTqDB3ocvdVbXmAt5uWQg0I/W5iB0YDKc0L4MQwkLIdMoJqloW su0Adq7oqcoA5Y00iRuIuQe086M6i1rIBe/kUAluJ3+XSQ781Hwzu1R5qBbipuTMDX1C FZcuyIdfzgdf2nCh+o6H0X8OFU8HWyB2R7KmD3gyOYjgKRk5dlsxlAXUww3jtgQDacBr lHXITDgQ8imXaqZPdRYOasXwN74r6n/opcpPuSpkYww8F0xUxNq4oXZ1iwSzjW9xQedW gDDch+txYqvdJUDuqi2n2t0u4NhunIJdMthjgOFHlQQZqKEiwY2AokYjcfESt4StNgSn C4lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774950071; x=1775554871; 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=SsziD70VgpPgjTdfHcq7GvG3ra662CqVO4P1uVRU6Ec=; b=M+cx+v0WcaJu4GKz7+wA4WA2o4jMAdC34YstM6gpR86uMSjCtFUgcM6W+IAF3TvCRH cKx8ZLCOr2SfTBB8tfw67TgCr3WOO7J0SYL0Y2zc4KFQ2vf7WyI//ldDiF+TGS5rZ1Iw AAC8Z4RV6Rlh7d9+Qp7l4tCUyr3xhK2y5AoP+SOoODjs+qDqHZFgxyBVecB0/HWAzn4w koPE8O0bZl4jbrcu5Df/GxUXIqN1Qnh7gp+PSqI8Cza46RyWFFitegRWmrTZNCNMRd/A YbmBMkqwPWt5+MDopIl/GdNhBsxPvoGo/EDr70BFhLeWLKrBB30LI/oxsiipQnVt1o3s mISQ== X-Forwarded-Encrypted: i=1; AJvYcCWtzabz/x+j9wNYTdrjPGrOnmtHWgsSF2s3+KeCePSXmID2ovN/evWNHFasNggErzT/fyvTvqFd7lDYdvI=@vger.kernel.org X-Gm-Message-State: AOJu0YwI9j7HuV1XJEtvkUxyE53AL5pVm5Atq2sJONO15Mtk1WzzaecU kUuTjs88WL1PkuwTvtog3MdFrzn8bdW94XsZ9bt+AlA0pW3Uur6MdfRUbBwO8QG9Dxg= X-Gm-Gg: ATEYQzyLZ8et39f/jj+ZeFvC5tiaXvh4WdkEp0UfLPAahb5xh3EnqR9Gy9LXH3yCdxR Dpeet9gYDcCIj+hRYxlEBCqHtJSPupYaHGHreQ34otaxMyLy5mWlbq5HFpWFij4tlm1PAYsiCpk RYU00bNv2hCDt77BmZtg+hiQFY92yEwznN/h9MIqf068EG0bDi1/wfA8lZLVzThPvj35uTj6Bs5 14AAZkG0Nh12ZJWHQxB18CaMi79vVYt+c4g8/S2PnG3tePwj4VmaZdAhGhm8XjUVybrvxg95x/1 QxrVVkR3r9cR3NZp3f8Qc7CkYRiPovQr3Mzk4xcc2TzlU0FfHMi10fH8k3wHKm34GZiPUuJvsnr xcunf+itrY5OwoEwqEmzF2TkO7gWA17ipiYtj4I+vmiGudfC2NL6NtsKsB8Ig28TcJxh15d1GAj czaY4OF1b5k3H6xnmzfpSSeg8m3RBlJxLqa/0LmaKHuhGIqNsvZinU8CJUz+cT/A== X-Received: by 2002:a05:600c:870f:b0:488:80b6:873a with SMTP id 5b1f17b1804b1-48880b6936emr1354875e9.21.1774950071278; Tue, 31 Mar 2026 02:41:11 -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.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2026 02:41:10 -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 2/2] selftests/bpf: add test for raw-address single kprobe attach Date: Tue, 31 Mar 2026 18:37:47 +0900 Message-ID: <20260331094058.49229-3-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" Currently, attach_probe covers manual single-kprobe attaches by func_name, but not the raw-address form that the PMU-based single-kprobe path can accept. This commit adds PERF and LINK raw-address subtests by resolving SYS_NANOSLEEP_KPROBE_NAME through kallsyms, passing the absolute address in bpf_kprobe_opts.offset with func_name =3D NULL, and verifying that kprobe and kretprobe are still triggered. It also verifies that LEGACY rejects the same form. Signed-off-by: Hoyeon Lee --- .../selftests/bpf/prog_tests/attach_probe.c | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/= testing/selftests/bpf/prog_tests/attach_probe.c index 9e77e5da7097..817c4794d54e 100644 --- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c +++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c @@ -123,6 +123,82 @@ static void test_attach_probe_manual(enum probe_attach= _mode attach_mode) test_attach_probe_manual__destroy(skel); } =20 +/* manual attach address-based kprobe/kretprobe testings */ +static void test_attach_kprobe_by_addr(enum probe_attach_mode attach_mode) +{ + DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts); + struct test_attach_probe_manual *skel; + unsigned long func_addr; + + if (!ASSERT_OK(load_kallsyms(), "load_kallsyms")) + return; + + func_addr =3D ksym_get_addr(SYS_NANOSLEEP_KPROBE_NAME); + if (!ASSERT_NEQ(func_addr, 0UL, "func_addr")) + return; + + skel =3D test_attach_probe_manual__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_kprobe_manual_open_and_load")) + return; + + kprobe_opts.attach_mode =3D attach_mode; + kprobe_opts.retprobe =3D false; + kprobe_opts.offset =3D func_addr; + skel->links.handle_kprobe =3D + bpf_program__attach_kprobe_opts(skel->progs.handle_kprobe, + NULL, &kprobe_opts); + if (!ASSERT_OK_PTR(skel->links.handle_kprobe, "attach_kprobe_by_addr")) + goto cleanup; + + kprobe_opts.retprobe =3D true; + skel->links.handle_kretprobe =3D + bpf_program__attach_kprobe_opts(skel->progs.handle_kretprobe, + NULL, &kprobe_opts); + if (!ASSERT_OK_PTR(skel->links.handle_kretprobe, + "attach_kretprobe_by_addr")) + goto cleanup; + + /* trigger & validate kprobe && kretprobe */ + usleep(1); + + ASSERT_EQ(skel->bss->kprobe_res, 1, "check_kprobe_res"); + ASSERT_EQ(skel->bss->kretprobe_res, 2, "check_kretprobe_res"); + +cleanup: + test_attach_probe_manual__destroy(skel); +} + +/* reject legacy address-based kprobe attach */ +static void test_attach_kprobe_legacy_by_addr_reject(void) +{ + DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts); + struct test_attach_probe_manual *skel; + unsigned long func_addr; + + if (!ASSERT_OK(load_kallsyms(), "load_kallsyms")) + return; + + func_addr =3D ksym_get_addr(SYS_NANOSLEEP_KPROBE_NAME); + if (!ASSERT_NEQ(func_addr, 0UL, "func_addr")) + return; + + skel =3D test_attach_probe_manual__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_kprobe_manual_open_and_load")) + return; + + kprobe_opts.attach_mode =3D PROBE_ATTACH_MODE_LEGACY; + kprobe_opts.offset =3D func_addr; + skel->links.handle_kprobe =3D + bpf_program__attach_kprobe_opts(skel->progs.handle_kprobe, + NULL, &kprobe_opts); + if (ASSERT_ERR_PTR(skel->links.handle_kprobe, + "attach_kprobe_legacy_by_addr")) + ASSERT_EQ(libbpf_get_error(skel->links.handle_kprobe), -ENOTSUP, + "attach_kprobe_legacy_by_addr_err"); + + test_attach_probe_manual__destroy(skel); +} + /* attach uprobe/uretprobe long event name testings */ static void test_attach_uprobe_long_event_name(void) { @@ -416,6 +492,12 @@ void test_attach_probe(void) test_attach_probe_manual(PROBE_ATTACH_MODE_PERF); if (test__start_subtest("manual-link")) test_attach_probe_manual(PROBE_ATTACH_MODE_LINK); + if (test__start_subtest("kprobe-perf-by-addr")) + test_attach_kprobe_by_addr(PROBE_ATTACH_MODE_PERF); + if (test__start_subtest("kprobe-link-by-addr")) + test_attach_kprobe_by_addr(PROBE_ATTACH_MODE_LINK); + if (test__start_subtest("kprobe-legacy-by-addr-reject")) + test_attach_kprobe_legacy_by_addr_reject(); =20 if (test__start_subtest("auto")) test_attach_probe_auto(skel); --=20 2.52.0