From patchwork Tue Apr 13 09:16:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 12199737 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D370C433ED for ; Tue, 13 Apr 2021 09:16:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7555E6124B for ; Tue, 13 Apr 2021 09:16:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237414AbhDMJQo (ORCPT ); Tue, 13 Apr 2021 05:16:44 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:29465 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245592AbhDMJQj (ORCPT ); Tue, 13 Apr 2021 05:16:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618305378; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0WQ0gS1Z35Vb1nS6NIi8g9cHCL5It5tExitM3okgeQ4=; b=i4yBPnVEweYI+T1OC/Bvqu0qhWp+jCJcairBrQnw+/xgYI7PcyuRf0ulnWxsj70b3Bfdx7 FPEj7L0Y/o1f4xdQqmxwbiaWxzWxDPZ2arw4Oz7fwey6sQ0vAhD6uEeeAyoUm3IPJag4Zi ZiKOoD5oNjvXvxyns9b3nfhE8Cg/wc8= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-117-oSDW4nezN0SwKIaE04sNtQ-1; Tue, 13 Apr 2021 05:16:16 -0400 X-MC-Unique: oSDW4nezN0SwKIaE04sNtQ-1 Received: by mail-ed1-f70.google.com with SMTP id k20-20020aa7d2d40000b0290382b9d237d6so946884edr.17 for ; Tue, 13 Apr 2021 02:16:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=0WQ0gS1Z35Vb1nS6NIi8g9cHCL5It5tExitM3okgeQ4=; b=Ik5Hk3hM6eo3WZs/eq9a1fBkQGj6oDYPK9hbSChFf65F2QQP0EH/Y8Sqt0Je3j327N dfWV6e+4iCLePYSMiW1MfqbnHXUAE13Oes90KJt1L2XLR2XnEzezpkhZo7IjWpalqAdh MKFjvTxjAyJ/PS6jo2rh1wGv+NOS8OgYnyxjAizfmgEYdLjwJWKIOInnABeWXgEqsA5G uW3acu6y3sMrVxx7ktoTHcl+fWAjSSunD8sFh5xMxjxIY9aX5v8aXTRhZc4/mWtGlykD Cv8XAEFpGwyvhWjuFplEsxbFaY5OBzZzjclUeywu5Hljj2sSgYJ10uzyf1NZucMApSeG D6mQ== X-Gm-Message-State: AOAM533UIb7vWhN2EwK8wLayRCOMR1Xel9zZaSyEbkAtlt80sGaycCaa F0oxrx50DLTRbVp10jYAcsSsqLtwCvLe418N/SYlDTL5VU66Bx3fLCksdMLlItZYhlC5LT31f/w Cr1sn61wz1rKc X-Received: by 2002:a50:eb92:: with SMTP id y18mr25651158edr.230.1618305375698; Tue, 13 Apr 2021 02:16:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy2WTTk9TI0R5UtWek42d/MF+SSdn93zgg4UslGBx/SiQnr5Ubifr07U6qmR+qkq6sFQMrx9g== X-Received: by 2002:a50:eb92:: with SMTP id y18mr25651139edr.230.1618305375559; Tue, 13 Apr 2021 02:16:15 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([45.145.92.2]) by smtp.gmail.com with ESMTPSA id t7sm3869182ejo.120.2021.04.13.02.16.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Apr 2021 02:16:15 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 691241804E8; Tue, 13 Apr 2021 11:16:14 +0200 (CEST) From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: Alexei Starovoitov , Daniel Borkmann Cc: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , bpf@vger.kernel.org Subject: [PATCH bpf-next v2 1/2] bpf: return target info when a tracing bpf_link is queried Date: Tue, 13 Apr 2021 11:16:06 +0200 Message-Id: <20210413091607.58945-1-toke@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net There is currently no way to discover the target of a tracing program attachment after the fact. Add this information to bpf_link_info and return it when querying the bpf_link fd. Acked-by: Andrii Nakryiko Signed-off-by: Toke Høiland-Jørgensen --- include/linux/bpf_verifier.h | 9 +++++++++ include/uapi/linux/bpf.h | 2 ++ kernel/bpf/syscall.c | 3 +++ tools/include/uapi/linux/bpf.h | 2 ++ 4 files changed, 16 insertions(+) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 51c2ffa3d901..6023a1367853 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -487,6 +487,15 @@ static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog, return ((u64)btf_obj_id(btf) << 32) | 0x80000000 | btf_id; } +/* unpack the IDs from the key as constructed above */ +static inline void bpf_trampoline_unpack_key(u64 key, u32 *obj_id, u32 *btf_id) +{ + if (obj_id) + *obj_id = key >> 32; + if (btf_id) + *btf_id = key & 0x7FFFFFFF; +} + int bpf_check_attach_target(struct bpf_verifier_log *log, const struct bpf_prog *prog, const struct bpf_prog *tgt_prog, diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 49371eba98ba..397884396671 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5379,6 +5379,8 @@ struct bpf_link_info { } raw_tracepoint; struct { __u32 attach_type; + __u32 target_obj_id; /* prog_id for PROG_EXT, otherwise btf object id */ + __u32 target_btf_id; /* BTF type id inside the object */ } tracing; struct { __u64 cgroup_id; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 6428634da57e..fd495190115e 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2551,6 +2551,9 @@ static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, container_of(link, struct bpf_tracing_link, link); info->tracing.attach_type = tr_link->attach_type; + bpf_trampoline_unpack_key(tr_link->trampoline->key, + &info->tracing.target_obj_id, + &info->tracing.target_btf_id); return 0; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 69902603012c..1a240be873d7 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5373,6 +5373,8 @@ struct bpf_link_info { } raw_tracepoint; struct { __u32 attach_type; + __u32 target_obj_id; /* prog_id for PROG_EXT, otherwise btf object id */ + __u32 target_btf_id; /* BTF type id inside the object */ } tracing; struct { __u64 cgroup_id; From patchwork Tue Apr 13 09:16:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 12199739 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A506C433B4 for ; Tue, 13 Apr 2021 09:16:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5AE65613B7 for ; Tue, 13 Apr 2021 09:16:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245592AbhDMJQp (ORCPT ); Tue, 13 Apr 2021 05:16:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:33417 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245611AbhDMJQm (ORCPT ); Tue, 13 Apr 2021 05:16:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618305381; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rl3u6G0sgRgVtBxAl6voZ2FF18vI+m2QO+lKCaXhXHc=; b=WSIPRUr1dOToWbe/WGyBotHF/LyiIXwcA+UMKbX2gXYHDxcOL2NBx+4UaZk+FAHttx1YlV XlVYP6XxUHGR76QDnDPPIPQb54yxY0+qD/graYa3v5gcmJkeHsHoHC/iMlhk7xQbs/0f6B D69v4iE0dngYVAG0GCu1M37qihnF51c= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-470-LX29eqYsOeqKhScp4f7s_g-1; Tue, 13 Apr 2021 05:16:20 -0400 X-MC-Unique: LX29eqYsOeqKhScp4f7s_g-1 Received: by mail-ed1-f70.google.com with SMTP id l9-20020a0564021249b02903827849a98dso942628edw.6 for ; Tue, 13 Apr 2021 02:16:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rl3u6G0sgRgVtBxAl6voZ2FF18vI+m2QO+lKCaXhXHc=; b=AQkeU/BTX7znf4IvSGabqu3qTqYF/TWO7/P1Yej26n2K3hB7l+bSB+kUEcfmEq0faq /JY0b699mW97vcXQO3jyLxibkMU3wc3Ak4w+2Dsl2pq+qsXlZJxGtZSCP12OxPpUS0P9 eDgJMVCmnDJAppyfV0s0woqJtS89bUdpi215mgzoB5Lr9DoU7f68tLbuBXEY5ccmdRRd nUtv7pgyoc2xzEYBA6J4u/g7InwoeBgnSz3X1DI5w9yKvbTKBnhuGMnhJyeWYN3/ROvr r+Y+cq4xc4eukb4ix/e+kI6Z+Z+CwWQozb7iSzdk6lgMSmOzmOVDqZIi8kdEXSt5UBVy 7nbg== X-Gm-Message-State: AOAM531p3G4kMZ+Rx5AwZaDihxAxwWQb5syvfFO2xOz5xGpRWXoWUDQe nWVDB/0w0h0SVTs73q5RxBSxopv8NtZuNnVgnACNeMzwIBsEQ0DenWVqc4FV2sY7ucOUGf+6rGt 4sOgwTpFlDfV7 X-Received: by 2002:a05:6402:520b:: with SMTP id s11mr34276365edd.212.1618305376180; Tue, 13 Apr 2021 02:16:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyES2low8Z5VP3nZ8zX5UWU17TTIaReKMRvfYx5lyn6Az5AUnD0dXEbXcm0eWXPbEkoFgGOig== X-Received: by 2002:a05:6402:520b:: with SMTP id s11mr34276345edd.212.1618305375971; Tue, 13 Apr 2021 02:16:15 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([45.145.92.2]) by smtp.gmail.com with ESMTPSA id t7sm3869193ejo.120.2021.04.13.02.16.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Apr 2021 02:16:15 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id E88B7180615; Tue, 13 Apr 2021 11:16:14 +0200 (CEST) From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: Alexei Starovoitov , Daniel Borkmann Cc: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , bpf@vger.kernel.org Subject: [PATCH bpf-next v2 2/2] selftests/bpf: add tests for target information in bpf_link info queries Date: Tue, 13 Apr 2021 11:16:07 +0200 Message-Id: <20210413091607.58945-2-toke@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210413091607.58945-1-toke@redhat.com> References: <20210413091607.58945-1-toke@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Extend the fexit_bpf2bpf test to check that the info for the bpf_link returned by the kernel matches the expected values. While we're updating the test, change existing uses of CHEC() to use the much easier to read ASSERT_*() macros. v2: - Convert last CHECK() call and get rid of 'duration' var - Split ASSERT_OK_PTR() checks to two separate if statements Acked-by: Andrii Nakryiko Signed-off-by: Toke Høiland-Jørgensen --- .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 58 ++++++++++++++----- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c b/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c index 5c0448910426..63990842d20f 100644 --- a/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +++ b/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c @@ -58,42 +58,73 @@ static void test_fexit_bpf2bpf_common(const char *obj_file, test_cb cb) { struct bpf_object *obj = NULL, *tgt_obj; + __u32 retval, tgt_prog_id, info_len; + struct bpf_prog_info prog_info = {}; struct bpf_program **prog = NULL; struct bpf_link **link = NULL; - __u32 duration = 0, retval; int err, tgt_fd, i; + struct btf *btf; err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC, &tgt_obj, &tgt_fd); - if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", - target_obj_file, err, errno)) + if (!ASSERT_OK(err, "tgt_prog_load")) return; DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, .attach_prog_fd = tgt_fd, ); + info_len = sizeof(prog_info); + err = bpf_obj_get_info_by_fd(tgt_fd, &prog_info, &info_len); + if (!ASSERT_OK(err, "tgt_fd_get_info")) + goto close_prog; + + tgt_prog_id = prog_info.id; + btf = bpf_object__btf(tgt_obj); + link = calloc(sizeof(struct bpf_link *), prog_cnt); + if (!ASSERT_OK_PTR(link, "link_ptr")) + goto close_prog; + prog = calloc(sizeof(struct bpf_program *), prog_cnt); - if (CHECK(!link || !prog, "alloc_memory", "failed to alloc memory")) + if (!ASSERT_OK_PTR(prog, "prog_ptr")) goto close_prog; obj = bpf_object__open_file(obj_file, &opts); - if (CHECK(IS_ERR_OR_NULL(obj), "obj_open", - "failed to open %s: %ld\n", obj_file, - PTR_ERR(obj))) + if (!ASSERT_OK_PTR(obj, "obj_open")) goto close_prog; err = bpf_object__load(obj); - if (CHECK(err, "obj_load", "err %d\n", err)) + if (!ASSERT_OK(err, "obj_load")) goto close_prog; for (i = 0; i < prog_cnt; i++) { + struct bpf_link_info link_info; + char *tgt_name; + __s32 btf_id; + + tgt_name = strstr(prog_name[i], "/"); + if (!ASSERT_OK_PTR(tgt_name, "tgt_name")) + goto close_prog; + btf_id = btf__find_by_name_kind(btf, tgt_name + 1, BTF_KIND_FUNC); + prog[i] = bpf_object__find_program_by_title(obj, prog_name[i]); - if (CHECK(!prog[i], "find_prog", "prog %s not found\n", prog_name[i])) + if (!ASSERT_OK_PTR(prog[i], prog_name[i])) goto close_prog; + link[i] = bpf_program__attach_trace(prog[i]); - if (CHECK(IS_ERR(link[i]), "attach_trace", "failed to link\n")) + if (!ASSERT_OK_PTR(link[i], "attach_trace")) goto close_prog; + + info_len = sizeof(link_info); + memset(&link_info, 0, sizeof(link_info)); + err = bpf_obj_get_info_by_fd(bpf_link__fd(link[i]), + &link_info, &info_len); + ASSERT_OK(err, "link_fd_get_info"); + ASSERT_EQ(link_info.tracing.attach_type, + bpf_program__get_expected_attach_type(prog[i]), + "link_attach_type"); + ASSERT_EQ(link_info.tracing.target_obj_id, tgt_prog_id, "link_tgt_obj_id"); + ASSERT_EQ(link_info.tracing.target_btf_id, btf_id, "link_tgt_btf_id"); } if (cb) { @@ -106,10 +137,9 @@ static void test_fexit_bpf2bpf_common(const char *obj_file, goto close_prog; err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6), - NULL, NULL, &retval, &duration); - CHECK(err || retval, "ipv6", - "err %d errno %d retval %d duration %d\n", - err, errno, retval, duration); + NULL, NULL, &retval, NULL); + ASSERT_OK(err, "prog_run"); + ASSERT_EQ(retval, 0, "prog_run_ret"); if (check_data_map(obj, prog_cnt, false)) goto close_prog;