From patchwork Tue Aug 16 23:40:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Luo X-Patchwork-Id: 12945356 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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DB251C25B0E for ; Tue, 16 Aug 2022 23:40:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233550AbiHPXkR (ORCPT ); Tue, 16 Aug 2022 19:40:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232911AbiHPXkR (ORCPT ); Tue, 16 Aug 2022 19:40:17 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E66072EFE for ; Tue, 16 Aug 2022 16:40:16 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-334d894afd8so10630817b3.19 for ; Tue, 16 Aug 2022 16:40:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:mime-version:message-id:date:from:to:cc; bh=UNi3nY4e74LKnS5YZ6ZQXRLBLNXdoKR2Gl4hi8aAWbs=; b=Xmee1qyOOONHqAhhrJGYFvgUBhm3gCjCEV5vgj3L9ZM1NyG4ELIV/0YWtfNoQzJc3k nzNypeYlgJGUpxYl2Q1stqtMwxIcN3nPj7v3JW2cDRnTceyzza8Dc+yR+jO4KkeXIF6V WW/PSazQCY8bbJFqkbz0KS4Sg2sL25EuTtyPvwXEyVneM1LNiQSPoxgmJLVu7l7BzQeg RARedZ7NT8BDlr54gOToUf0PPTCSqAmELKUlN2kX6NcpgOjpjZh1fHH5Ho9FzS9VEHTs HVkUYOlO2zrgg6y69bWlOhXswdGeYdm6wR1CJL/+gG//5JqiIJKrnBTiqkaIK46Uc5Oz AhHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc; bh=UNi3nY4e74LKnS5YZ6ZQXRLBLNXdoKR2Gl4hi8aAWbs=; b=MtjkMz0D120my0YCjyC7YwsunqW1o1W7m5G1kvZ0qA1riUjbCjSLoR0Smo9zcZzRJ3 aW105mn2Rz/XUatf/WCc0upRlkUcB7fKVmGf5YJJ87Sjo7sKr6NCik/kkISuQaa0hFGu cjFyYzv8u6axLxpdjhyRxyrgI29qhtREmaUxjQmnJWFIpMQbRa6XDknuqMGDsgIW55mj 7cfp5gN5gdXUmL2igRDCePbn7jLAREdYfGRFMRvAa0vrLZVcOTw+t71qBt3U1D2wMyvJ BDd+7w5hikM4NfrCsPhV4HosFsMhqODp2WQLBHR+aDZ17JSzNglZHl4csTFvE3OVqhMI 1RYQ== X-Gm-Message-State: ACgBeo1pVgGGL3Oi/XYab0eOfO1dIr+qllwk63OrGQ0nAriHrgcVQ+N1 S/1s9pwzQS0Gy3sPYoiW+KotEuob8kU= X-Google-Smtp-Source: AA6agR7nLWQJjytbdBXE8SR1wADUvUrjNlZbAQtIn1LNmfCYCa89XbJSlbCkY6bFv4sjNu0fO32SQfPBdMo= X-Received: from haoluo.svl.corp.google.com ([2620:15c:2d4:203:7e6e:c287:3dc6:d4d8]) (user=haoluo job=sendgmr) by 2002:a25:404c:0:b0:68f:d76a:4a59 with SMTP id n73-20020a25404c000000b0068fd76a4a59mr1256750yba.410.1660693215606; Tue, 16 Aug 2022 16:40:15 -0700 (PDT) Date: Tue, 16 Aug 2022 16:40:11 -0700 Message-Id: <20220816234012.910255-1-haoluo@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [PATCH bpf-next v2 1/2] libbpf: Allows disabling auto attach From: Hao Luo To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , KP Singh , John Fastabend , Stanislav Fomichev , Yosry Ahmed , Hao Luo Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Adds libbpf APIs for disabling auto-attach for individual functions. This is motivated by the use case of cgroup iter [1]. Some iter types require their parameters to be non-zero, therefore applying auto-attach on them will fail. With these two new APIs, users who want to use auto-attach and these types of iters can disable auto-attach on the program and perform manual attach. [1] https://lore.kernel.org/bpf/CAEf4BzZ+a2uDo_t6kGBziqdz--m2gh2_EUwkGLDtMd65uwxUjA@mail.gmail.com/ Signed-off-by: Hao Luo --- tools/lib/bpf/libbpf.c | 15 ++++++++++++++- tools/lib/bpf/libbpf.h | 2 ++ tools/lib/bpf/libbpf.map | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index aa05a99b913d..0159a43c7efd 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -417,6 +417,7 @@ struct bpf_program { int fd; bool autoload; + bool autoattach; bool mark_btf_static; enum bpf_prog_type type; enum bpf_attach_type expected_attach_type; @@ -755,6 +756,8 @@ bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog, prog->autoload = true; } + prog->autoattach = true; + /* inherit object's log_level */ prog->log_level = obj->log_level; @@ -8314,6 +8317,16 @@ int bpf_program__set_autoload(struct bpf_program *prog, bool autoload) return 0; } +bool bpf_program__autoattach(const struct bpf_program *prog) +{ + return prog->autoattach; +} + +void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach) +{ + prog->autoattach = autoattach; +} + const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog) { return prog->insns; @@ -12346,7 +12359,7 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) struct bpf_program *prog = *s->progs[i].prog; struct bpf_link **link = s->progs[i].link; - if (!prog->autoload) + if (!prog->autoload || !prog->autoattach) continue; /* auto-attaching not supported for this program */ diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 61493c4cddac..88a1ac34b12a 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -260,6 +260,8 @@ LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog); LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog); LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog); LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload); +LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog); +LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach); struct bpf_insn; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 119e6e1ea7f1..2b928dc21af0 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -358,6 +358,8 @@ LIBBPF_1.0.0 { bpf_obj_get_opts; bpf_prog_query_opts; bpf_program__attach_ksyscall; + bpf_program__autoattach; + bpf_program__set_autoattach; btf__add_enum64; btf__add_enum64_value; libbpf_bpf_attach_type_str; From patchwork Tue Aug 16 23:40:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Luo X-Patchwork-Id: 12945357 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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 114E1C28B2B for ; Tue, 16 Aug 2022 23:40:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229472AbiHPXkV (ORCPT ); Tue, 16 Aug 2022 19:40:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237482AbiHPXkU (ORCPT ); Tue, 16 Aug 2022 19:40:20 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C70CF75CDD for ; Tue, 16 Aug 2022 16:40:18 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-334370e5ab9so33241737b3.22 for ; Tue, 16 Aug 2022 16:40:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=D08oWd4LjFqivxR059JhoCieMYjNYb3QgJK4ULKJads=; b=Zd3BC7tgcxZnVGrjx21FVrnzY80tCScyV8zDCKSI+7F3gEaY56vZZcgjbaX+ZcwRQc HZ/Wu390bK80ivlifGXr9LF9OjpnJgcHdlAN6/+kQsh4fnm02aO3v518wNfWMO7vEsh8 6FlZgcS14qYwXC7gGmtTXj4JyINBIh4mtFkaeh8icfL1r/HMSvGijpysXJSL2af8/b44 9lUKgKYKSDrJ8tlmc184899jxmmsb07fOYenHKZHSRRIHhcNHz+vDjGZXeccPEBaBV5t UpkzDmgLfBcTysKw2hwq81ayyaXRVrZFObFAFssMbKqUipJndVhEPOAp3NVCLDwglk8T J8Ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=D08oWd4LjFqivxR059JhoCieMYjNYb3QgJK4ULKJads=; b=uOW5QSfatxC0KIrWZIpPsGRehFAF3Uzz7Kegmsxx6XYMXlv8Lu4fCL5FyhwfCBopOM Yj+xkYcuv/fiOBSuc6eEYg6qraNwk6pprZqNk4Jf+sgZGWp+eMAaREZjOnSN5bGLlhrS QFG6hMsiUbi1bbtYvzdt6PqnvV6BTFViqdKePK49wkCCLSuhJdbTpmDj2EwFFPxhnJWf voq1Ysj29Z7zQ82rWPgU6Ovkwrn48EyPUPYDPa7vNUbYDPJIbGjvNjvmh53ygidF2ziI uJ8yp8q6b0eD+1w2pXasFOt/O9Tt9DpaCv0a4Z4y6WEQqT2g3DRefZ5A4YL09bhaAWUd RLrw== X-Gm-Message-State: ACgBeo3ML5jIM3F1DbxgTWqmXSWVWFVb8nPzev9ROoMa+gHifbnmb8Kr W//A+r2C7Kns68WrHFlstgxTvsBSA7c= X-Google-Smtp-Source: AA6agR6agp/FjD0u3wYPK2e4qb7uNMXZOBvd/d4O6snkHxKspfot09Y1KmGgtlJ6DmBU+Qib6xRCaQngUhE= X-Received: from haoluo.svl.corp.google.com ([2620:15c:2d4:203:7e6e:c287:3dc6:d4d8]) (user=haoluo job=sendgmr) by 2002:a05:6902:72c:b0:676:d795:d6f8 with SMTP id l12-20020a056902072c00b00676d795d6f8mr17854320ybt.406.1660693218147; Tue, 16 Aug 2022 16:40:18 -0700 (PDT) Date: Tue, 16 Aug 2022 16:40:12 -0700 In-Reply-To: <20220816234012.910255-1-haoluo@google.com> Message-Id: <20220816234012.910255-2-haoluo@google.com> Mime-Version: 1.0 References: <20220816234012.910255-1-haoluo@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [PATCH bpf-next v2 2/2] selftests/bpf: Tests libbpf autoattach APIs From: Hao Luo To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , KP Singh , John Fastabend , Stanislav Fomichev , Yosry Ahmed , Hao Luo Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Adds test for libbpf APIs that toggle bpf program auto-attaching. Signed-off-by: Hao Luo --- .../selftests/bpf/prog_tests/autoattach.c | 30 +++++++++++++++++++ .../selftests/bpf/progs/test_autoattach.c | 23 ++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/autoattach.c create mode 100644 tools/testing/selftests/bpf/progs/test_autoattach.c diff --git a/tools/testing/selftests/bpf/prog_tests/autoattach.c b/tools/testing/selftests/bpf/prog_tests/autoattach.c new file mode 100644 index 000000000000..b34b5945d297 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/autoattach.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ + +#include +#include "test_autoattach.skel.h" + +void test_autoattach(void) +{ + struct test_autoattach* skel; + + skel = test_autoattach__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open_and_load")) + goto cleanup; + + /* disable auto-attach for prog2 */ + bpf_program__set_autoattach(skel->progs.prog2, false); + ASSERT_TRUE(bpf_program__autoattach(skel->progs.prog1), "autoattach_prog1"); + ASSERT_FALSE(bpf_program__autoattach(skel->progs.prog2), "autoattach_prog2"); + if (!ASSERT_OK(test_autoattach__attach(skel), "skel_attach")) + goto cleanup; + + usleep(1); + + ASSERT_TRUE(skel->bss->prog1_called, "attached_prog1"); + ASSERT_FALSE(skel->bss->prog2_called, "attached_prog2"); + +cleanup: + test_autoattach__destroy(skel); +} + diff --git a/tools/testing/selftests/bpf/progs/test_autoattach.c b/tools/testing/selftests/bpf/progs/test_autoattach.c new file mode 100644 index 000000000000..11a44493ebce --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_autoattach.c @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ + +#include "vmlinux.h" +#include + +bool prog1_called = false; +bool prog2_called = false; + +SEC("raw_tp/sys_enter") +int prog1(const void *ctx) +{ + prog1_called = true; + return 0; +} + +SEC("raw_tp/sys_exit") +int prog2(const void *ctx) +{ + prog2_called = true; + return 0; +} +