From patchwork Thu Feb 20 21:59:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ihor Solodrai X-Patchwork-Id: 13984534 X-Patchwork-Delegate: bpf@iogearbox.net Received: from out-174.mta1.migadu.com (out-174.mta1.migadu.com [95.215.58.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 990AD1EA7ED for ; Thu, 20 Feb 2025 21:59:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740088755; cv=none; b=SE6HCk927jg6CBbGya7a+wT253r/ndrLDnf/bFGxfTaO+W2Rpn8zP6Z/R1tWnywyW/SbxktypGkahBTk1XaEW+sBS8NPcP0GI/+Jg3hAiG6Ejpz5OLgp5D7LpgPVSPh9cSiJBUE/S0DyAu6mdE4aD3bU+rJ8QEjj0qfPIoiLsMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740088755; c=relaxed/simple; bh=TOSygrza++He//qOVXcaQpNcfyAdM7kTtBs6knGw4V0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=fCz/kZsEn7ufH4uPzhNFa75zpViM/8pEpv7UoJMzE9IeJQfvY85HugbwGvj+83tQScvxmfLD5B7FbrywD+nYIO/rWYUxWKEsnhHj75zEs95pfAU+Wz/f+li/nMnKHGA0/tLd/2EHmuOO2uc6TH4PC4jQzXVUFYUj/2U8dQqbnOQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Ld4ima8g; arc=none smtp.client-ip=95.215.58.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Ld4ima8g" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1740088751; 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; bh=iZpJAulZucENFx39QcwmM0fgkw6mFhtqV9TUBtKQwFM=; b=Ld4ima8gcFsPO4psRhxF5xJPxiYA9JvMjoPw8sEZjIubdY8e52YhzHkwIPDVL+iTuyP3WB uL8SkOkWLAQZoJYixddbqyIqrLYVY26T/7CXLY7MnHCDnjDuTtFcixrI2BdswA7F/9qaES aV8LOV4eCQzBBveAx4iCVC9LW2Jt1+A= From: Ihor Solodrai To: bpf@vger.kernel.org Cc: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net, eddyz87@gmail.com, mykolal@fb.com, kernel-team@meta.com Subject: [PATCH bpf-next 1/2] libbpf: implement bpf_usdt_arg_size BPF function Date: Thu, 20 Feb 2025 13:59:03 -0800 Message-ID: <20250220215904.3362709-1-ihor.solodrai@linux.dev> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Patchwork-Delegate: bpf@iogearbox.net Information about USDT argument size is implicitly stored in __bpf_usdt_arg_spec, but currently it's not accessbile to BPF programs that use USDT. Implement bpf_sdt_arg_size() that returns the size of an USDT argument in bytes. Factor out __bpf_usdt_arg_spec() routine from bpf_usdt_arg(). It searches for arg_spec given ctx and arg_num. Suggested-by: Andrii Nakryiko Signed-off-by: Ihor Solodrai --- tools/lib/bpf/usdt.bpf.h | 59 ++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/tools/lib/bpf/usdt.bpf.h b/tools/lib/bpf/usdt.bpf.h index b811f754939f..6041271c5e4e 100644 --- a/tools/lib/bpf/usdt.bpf.h +++ b/tools/lib/bpf/usdt.bpf.h @@ -108,19 +108,12 @@ int bpf_usdt_arg_cnt(struct pt_regs *ctx) return spec->arg_cnt; } -/* Fetch USDT argument #*arg_num* (zero-indexed) and put its value into *res. - * Returns 0 on success; negative error, otherwise. - * On error *res is guaranteed to be set to zero. - */ -__weak __hidden -int bpf_usdt_arg(struct pt_regs *ctx, __u64 arg_num, long *res) +/* Validate ctx and arg_num, if ok set arg_spec pointer */ +static __always_inline +int __bpf_usdt_arg_spec(struct pt_regs *ctx, __u64 arg_num, struct __bpf_usdt_arg_spec **arg_spec) { struct __bpf_usdt_spec *spec; - struct __bpf_usdt_arg_spec *arg_spec; - unsigned long val; - int err, spec_id; - - *res = 0; + int spec_id; spec_id = __bpf_usdt_spec_id(ctx); if (spec_id < 0) @@ -136,7 +129,49 @@ int bpf_usdt_arg(struct pt_regs *ctx, __u64 arg_num, long *res) if (arg_num >= spec->arg_cnt) return -ENOENT; - arg_spec = &spec->args[arg_num]; + *arg_spec = &spec->args[arg_num]; + + return 0; +} + +/* Returns the size in bytes of the #*arg_num* (zero-indexed) USDT argument. + * Returns negative error if argument is not found or arg_num is invalid. + */ +static __always_inline +int bpf_usdt_arg_size(struct pt_regs *ctx, __u64 arg_num) +{ + struct __bpf_usdt_arg_spec *arg_spec; + int err; + + err = __bpf_usdt_arg_spec(ctx, arg_num, &arg_spec); + if (err) + return err; + + /* arg_spec->arg_bitshift = 64 - arg_sz * 8 + * so: arg_sz = (64 - arg_spec->arg_bitshift) / 8 + * Do a bitshift instead of a division to avoid + * "unsupported signed division" error. + */ + return (64 - arg_spec->arg_bitshift) >> 3; +} + +/* Fetch USDT argument #*arg_num* (zero-indexed) and put its value into *res. + * Returns 0 on success; negative error, otherwise. + * On error *res is guaranteed to be set to zero. + */ +__weak __hidden +int bpf_usdt_arg(struct pt_regs *ctx, __u64 arg_num, long *res) +{ + struct __bpf_usdt_arg_spec *arg_spec; + unsigned long val; + int err; + + *res = 0; + + err = __bpf_usdt_arg_spec(ctx, arg_num, &arg_spec); + if (err) + return err; + switch (arg_spec->arg_type) { case BPF_USDT_ARG_CONST: /* Arg is just a constant ("-4@$-9" in USDT arg spec). From patchwork Thu Feb 20 21:59:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ihor Solodrai X-Patchwork-Id: 13984535 X-Patchwork-Delegate: bpf@iogearbox.net Received: from out-185.mta1.migadu.com (out-185.mta1.migadu.com [95.215.58.185]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7765D1EA7ED for ; Thu, 20 Feb 2025 21:59:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.185 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740088759; cv=none; b=FfHI7QA0x3dZAOtXcZJEh4yV6WgQBDD6P5was7rFzkgOeQRjqQHPDBULxG0AYSFltKqmeONFj6dFbA+5731xgx1ymzVk5RwCBMRFH/J2lUOgOynMP7O0OrgX1RenAePEeqoz7OCurei7bVXnXol6OJ5tbRfNQLRK6vP8GMbX8Sk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740088759; c=relaxed/simple; bh=GaF1LRz0dKGSicZp73OM5lOxkCCqKQwIpcyNR0H/FFQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Emt18yMZxCO9gc7Wj16755/FfLbg2iJzluLiiJrovQwBmWmtx5yj6t0+4bBTZXxY1UaMiKBp1YK2UHBU9ypxoahIT0PuhVP21xSb3QM1NKfHqxFSlA2uk3KVX1SiP8Aq+6RjOK9XVSPOXmLSrBWs4UIqTCH2ZP/+K1USP7sV9uU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=a8kQrmz/; arc=none smtp.client-ip=95.215.58.185 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="a8kQrmz/" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1740088753; 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=IQDc/iqi6YNE3UJRAwWWXXnalLr5oJV2UaPDpRWloZ0=; b=a8kQrmz/174GJ9mdOFb9VzgaCCUiY/LLzmb1naC2DnTmyw7NOyT9sThpRCffgXVOn79IhS eWUqC8MMz+7S/I7WRaOkVxddwCi9zbxk3hQlIhdfwFCOTB7vzcWPvu+DQH3mClSH4wOEeY amADojsDjkrBimpJpiMQU6j6FWRlQaU= From: Ihor Solodrai To: bpf@vger.kernel.org Cc: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net, eddyz87@gmail.com, mykolal@fb.com, kernel-team@meta.com Subject: [PATCH bpf-next 2/2] selftests/bpf: test bpf_usdt_arg_size() function Date: Thu, 20 Feb 2025 13:59:04 -0800 Message-ID: <20250220215904.3362709-2-ihor.solodrai@linux.dev> In-Reply-To: <20250220215904.3362709-1-ihor.solodrai@linux.dev> References: <20250220215904.3362709-1-ihor.solodrai@linux.dev> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Patchwork-Delegate: bpf@iogearbox.net Update usdt tests to also check for correct behavior of bpf_usdt_arg_size(). Signed-off-by: Ihor Solodrai --- tools/testing/selftests/bpf/prog_tests/usdt.c | 11 ++++++++++- tools/testing/selftests/bpf/progs/test_usdt.c | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/usdt.c b/tools/testing/selftests/bpf/prog_tests/usdt.c index 56ed1eb9b527..495d66414b57 100644 --- a/tools/testing/selftests/bpf/prog_tests/usdt.c +++ b/tools/testing/selftests/bpf/prog_tests/usdt.c @@ -45,7 +45,7 @@ static void subtest_basic_usdt(void) LIBBPF_OPTS(bpf_usdt_opts, opts); struct test_usdt *skel; struct test_usdt__bss *bss; - int err; + int err, i; skel = test_usdt__open_and_load(); if (!ASSERT_OK_PTR(skel, "skel_open")) @@ -75,6 +75,7 @@ static void subtest_basic_usdt(void) ASSERT_EQ(bss->usdt0_cookie, 0xcafedeadbeeffeed, "usdt0_cookie"); ASSERT_EQ(bss->usdt0_arg_cnt, 0, "usdt0_arg_cnt"); ASSERT_EQ(bss->usdt0_arg_ret, -ENOENT, "usdt0_arg_ret"); + ASSERT_EQ(bss->usdt0_arg_size, -ENOENT, "usdt0_arg_size"); /* auto-attached usdt3 gets default zero cookie value */ ASSERT_EQ(bss->usdt3_cookie, 0, "usdt3_cookie"); @@ -86,6 +87,9 @@ static void subtest_basic_usdt(void) ASSERT_EQ(bss->usdt3_args[0], 1, "usdt3_arg1"); ASSERT_EQ(bss->usdt3_args[1], 42, "usdt3_arg2"); ASSERT_EQ(bss->usdt3_args[2], (uintptr_t)&bla, "usdt3_arg3"); + ASSERT_EQ(bss->usdt3_arg_sizes[0], 4, "usdt3_arg1_size"); + ASSERT_EQ(bss->usdt3_arg_sizes[1], 8, "usdt3_arg2_size"); + ASSERT_EQ(bss->usdt3_arg_sizes[2], 8, "usdt3_arg3_size"); /* auto-attached usdt12 gets default zero cookie value */ ASSERT_EQ(bss->usdt12_cookie, 0, "usdt12_cookie"); @@ -104,6 +108,11 @@ static void subtest_basic_usdt(void) ASSERT_EQ(bss->usdt12_args[10], nums[idx], "usdt12_arg11"); ASSERT_EQ(bss->usdt12_args[11], t1.y, "usdt12_arg12"); + int usdt12_expected_arg_sizes[12] = { 4, 4, 8, 8, 4, 8, 8, 8, 4, 2, 2, 1 }; + + for (i = 0; i < 12; i++) + ASSERT_EQ(bss->usdt12_arg_sizes[i], usdt12_expected_arg_sizes[i], "usdt12_arg_size"); + /* trigger_func() is marked __always_inline, so USDT invocations will be * inlined in two different places, meaning that each USDT will have * at least 2 different places to be attached to. This verifies that diff --git a/tools/testing/selftests/bpf/progs/test_usdt.c b/tools/testing/selftests/bpf/progs/test_usdt.c index 505aab9a5234..096488f47fbc 100644 --- a/tools/testing/selftests/bpf/progs/test_usdt.c +++ b/tools/testing/selftests/bpf/progs/test_usdt.c @@ -11,6 +11,7 @@ int usdt0_called; u64 usdt0_cookie; int usdt0_arg_cnt; int usdt0_arg_ret; +int usdt0_arg_size; SEC("usdt") int usdt0(struct pt_regs *ctx) @@ -26,6 +27,7 @@ int usdt0(struct pt_regs *ctx) usdt0_arg_cnt = bpf_usdt_arg_cnt(ctx); /* should return -ENOENT for any arg_num */ usdt0_arg_ret = bpf_usdt_arg(ctx, bpf_get_prandom_u32(), &tmp); + usdt0_arg_size = bpf_usdt_arg_size(ctx, bpf_get_prandom_u32()); return 0; } @@ -34,6 +36,7 @@ u64 usdt3_cookie; int usdt3_arg_cnt; int usdt3_arg_rets[3]; u64 usdt3_args[3]; +int usdt3_arg_sizes[3]; SEC("usdt//proc/self/exe:test:usdt3") int usdt3(struct pt_regs *ctx) @@ -50,12 +53,15 @@ int usdt3(struct pt_regs *ctx) usdt3_arg_rets[0] = bpf_usdt_arg(ctx, 0, &tmp); usdt3_args[0] = (int)tmp; + usdt3_arg_sizes[0] = bpf_usdt_arg_size(ctx, 0); usdt3_arg_rets[1] = bpf_usdt_arg(ctx, 1, &tmp); usdt3_args[1] = (long)tmp; + usdt3_arg_sizes[1] = bpf_usdt_arg_size(ctx, 1); usdt3_arg_rets[2] = bpf_usdt_arg(ctx, 2, &tmp); usdt3_args[2] = (uintptr_t)tmp; + usdt3_arg_sizes[2] = bpf_usdt_arg_size(ctx, 2); return 0; } @@ -64,12 +70,15 @@ int usdt12_called; u64 usdt12_cookie; int usdt12_arg_cnt; u64 usdt12_args[12]; +int usdt12_arg_sizes[12]; SEC("usdt//proc/self/exe:test:usdt12") int BPF_USDT(usdt12, int a1, int a2, long a3, long a4, unsigned a5, long a6, __u64 a7, uintptr_t a8, int a9, short a10, short a11, signed char a12) { + int i; + if (my_pid != (bpf_get_current_pid_tgid() >> 32)) return 0; @@ -90,6 +99,11 @@ int BPF_USDT(usdt12, int a1, int a2, long a3, long a4, unsigned a5, usdt12_args[9] = a10; usdt12_args[10] = a11; usdt12_args[11] = a12; + + bpf_for(i, 0, 12) { + usdt12_arg_sizes[i] = bpf_usdt_arg_size(ctx, i); + } + return 0; }