From patchwork Mon Mar 11 09:35:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?5qKm6b6Z6JGj?= X-Patchwork-Id: 13588437 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A7F37C54E58 for ; Mon, 11 Mar 2024 09:34:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=yw3wp7BcVZBX7oR4DO5qW9CiBhyKqYLF3a6Xn7bhllM=; b=Q4dHszVGIO2lHz W84wFRE+MKakXkvlh7S8wEefMu7MgIsqpfwMDc1b5+Y2boq5zgQeQVei6nqS4/g5gSgliCcGfIvMG qLRdPupm61FBOpBojf+dd3WWFyXHqyf3JN5ZH98SvaQjD22oeZpu5XjN4xq3H+Ovg7PwZW5HavgTt 4S/243qYVZcCmllkliuZZSYpaLwRPEADTSXF46OyQVbErcr8lNdx+rt0e6SE+9Qh8RuroP64QaW8A eT7Nn4ON/+GKSGIr0vT+t0+woKb8N2kwjBL4LkDKV4Ln+kD9cUslwUDou7l8Lmj5rOC2nCKchzPOL 5nJNZG/eRn7EsGMzQGAQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rjc3K-00000000rr5-3rdT; Mon, 11 Mar 2024 09:34:39 +0000 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rjc3G-00000000rnD-1UiK for linux-arm-kernel@lists.infradead.org; Mon, 11 Mar 2024 09:34:36 +0000 Received: by mail-pg1-x52f.google.com with SMTP id 41be03b00d2f7-5d4d15ec7c5so3659252a12.1 for ; Mon, 11 Mar 2024 02:34:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1710149668; x=1710754468; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=q244jOj8DhushyleU2bCeCOJVSr6CNZEpUiu8vv3bQA=; b=D7CnoXGLu/amEX8H96FAO62VC4FNmi68qNCg9f/SZveCnaGYzfY304g3XZ8VQM0z0x gxug48rnGAAsYrY9Kogale8AZuLL68ikmzVDHcEGb+K+7nPwp/PVu+/XGNkDP+7NfdPk RamWss8Eq9k2BOM4H2e7LppK/GtCU88kYqApNF1d7QrHlyHOjdfV/9RfpVRXEuCeEENn +/i7pKlYocOJKAuKbeOjn7JL8GApnJTTLb3Mp5dCjYZrulqwf1Phfml5srd7AtpVGaB/ 35EmyJ8wGMb4y1gSuiaDduJWQGLQ8tv14NgnohUsKfxXrrM5Kq7Fs++nsc8G7giCb0WA geRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710149668; x=1710754468; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=q244jOj8DhushyleU2bCeCOJVSr6CNZEpUiu8vv3bQA=; b=xMOXSamXHmXopod5gUXY3d4fD7gCdrxZyclfwoi/sgjyUo9kKa+RcjQDcVkbnx11j0 43f6xht7GONmcNjaG2fkMsiLrGH0xhZs417GdAVucnUYS+YEXStdo7nft0F5LPJyU3d0 4nhouY8pwwtOOaa2XymDFq0NGW7qeYoGQQYvcDtdUgc5SYT1rC1DSL0yuAQy5sxMyK5Z BIPtZxrHIWST0nlWN09DDjPZb2/IHB31vxgQDYGvj70Mqu4CtF3A8xy9tX6gAeNkOO4Z vFths4CZD5Zr4+uUxVwTnsNcc0IpbWwbmGG5XMcG0JEMyY+iwuftHFlC4RA4j+OHuA3t Ymuw== X-Forwarded-Encrypted: i=1; AJvYcCUgbo4o89Vs6rTPdLVbkQTJKkbDBT8a6g9x/b0Vk9vjY5O5AeUTDZcw4s/xKvsr81Alf32ZmXSPCpJJpin1koC2Aei3BK7AS0WdCbvo9fm4KycGf54= X-Gm-Message-State: AOJu0YwTzhjzremHVOLcLpqYRb00OaYfs0jxOxpZ1jQjwrnWkqOkH8Hb 5Tvep+Nej3Mja407spHQOzUwmdJcYOGIAD9bFWGOnIWj01aaufaG7JxI6uBCeH8= X-Google-Smtp-Source: AGHT+IElS3MTAJT6oKHCK2ojNj/jD5/lk4XV4NzHmUyXpO9L/Z2ncrXc9UjeUvKY0ZMwD8Lzv32+BQ== X-Received: by 2002:a05:6a20:9f99:b0:1a1:7df5:ee5a with SMTP id mm25-20020a056a209f9900b001a17df5ee5amr9151303pzb.35.1710149668389; Mon, 11 Mar 2024 02:34:28 -0700 (PDT) Received: from localhost.localdomain ([43.129.25.208]) by smtp.gmail.com with ESMTPSA id h9-20020a170902f7c900b001dcad9cbf8bsm4253365plw.239.2024.03.11.02.34.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 02:34:27 -0700 (PDT) From: Menglong Dong To: andrii@kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, agordeev@linux.ibm.com, borntraeger@linux.ibm.com, svens@linux.ibm.com, davem@davemloft.net, dsahern@kernel.org, dave.hansen@linux.intel.com, x86@kernel.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, quentin@isovalent.com, bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, netdev@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, Menglong Dong Subject: [PATCH bpf-next v2 0/9] bpf: make tracing program support multi-link Date: Mon, 11 Mar 2024 17:35:17 +0800 Message-Id: <20240311093526.1010158-1-dongmenglong.8@bytedance.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240311_023434_848882_25588FA6 X-CRM114-Status: GOOD ( 20.52 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org For now, the BPF program of type BPF_PROG_TYPE_TRACING is not allowed to be attached to multiple hooks, and we have to create a BPF program for each kernel function, for which we want to trace, even through all the program have the same (or similar) logic. This can consume extra memory, and make the program loading slow if we have plenty of kernel function to trace. In this series, we add the support to allow attaching a tracing BPF program to multi hooks, which is similar to BPF_TRACE_KPROBE_MULTI. In the 1st patch, we add the support to record index of the accessed function args of the target for tracing program. Meanwhile, we add the function btf_check_func_part_match() to compare the accessed function args of two function prototype. This function will be used in the next commit. In the 2nd patch, we refactor the struct modules_array to ptr_array, as we need similar function to hold the target btf, target program and kernel modules that we reference to in the following commit. In the 3rd patch, we introduce the struct bpf_tramp_link_conn to be the bridge between bpf_link and trampoline, as the releation between bpf_link and trampoline is not one-to-one anymore. In the 4th patch, we add the struct bpf_tramp_multi_link and bpf_trampoline_multi_{link,unlink}_prog for multi-link of trampoline. In the 5th patch, we add target btf to the function args of bpf_check_attach_target(), then the caller can specify the btf to check. The 6th patch is the main part to add multi-link supporting for tracing. For now, only the following attach type is supported: BPF_TRACE_FENTRY_MULTI BPF_TRACE_FEXIT_MULTI BPF_MODIFY_RETURN_MULTI The attach type of BPF_TRACE_RAW_TP has different link type, so we skip this part in this series for now. In the 7th and 8th patches, we add multi-link supporting of tracing to libbpf. Note that we don't free btfs that we load after the bpf programs are loaded into the kernel now if any programs of type tracing multi-link existing, as we need to lookup the btf types during attaching. In the 9th patch, we add the testcases for this series. Changes since v1: - According to the advice of Alexei, introduce multi-link for tracing instead of attaching a tracing program to multiple trampolines with creating multi instance of bpf_link. Menglong Dong (9): bpf: tracing: add support to record and check the accessed args bpf: refactor the modules_array to ptr_array bpf: trampoline: introduce struct bpf_tramp_link_conn bpf: trampoline: introduce bpf_tramp_multi_link bpf: verifier: add btf to the function args of bpf_check_attach_target bpf: tracing: add multi-link support libbpf: don't free btf if program of multi-link tracing existing libbpf: add support for the multi-link of tracing selftests/bpf: add testcases for multi-link of tracing arch/arm64/net/bpf_jit_comp.c | 4 +- arch/riscv/net/bpf_jit_comp64.c | 4 +- arch/s390/net/bpf_jit_comp.c | 4 +- arch/x86/net/bpf_jit_comp.c | 4 +- include/linux/bpf.h | 51 ++- include/linux/bpf_verifier.h | 1 + include/uapi/linux/bpf.h | 10 + kernel/bpf/bpf_struct_ops.c | 2 +- kernel/bpf/btf.c | 113 ++++- kernel/bpf/syscall.c | 425 +++++++++++++++++- kernel/bpf/trampoline.c | 97 +++- kernel/bpf/verifier.c | 24 +- kernel/trace/bpf_trace.c | 48 +- net/bpf/test_run.c | 3 + net/core/bpf_sk_storage.c | 2 + tools/bpf/bpftool/common.c | 3 + tools/include/uapi/linux/bpf.h | 10 + tools/lib/bpf/bpf.c | 10 + tools/lib/bpf/bpf.h | 6 + tools/lib/bpf/libbpf.c | 215 ++++++++- tools/lib/bpf/libbpf.h | 16 + tools/lib/bpf/libbpf.map | 2 + .../selftests/bpf/bpf_testmod/bpf_testmod.c | 49 ++ .../bpf/prog_tests/tracing_multi_link.c | 153 +++++++ .../selftests/bpf/progs/tracing_multi_test.c | 209 +++++++++ 25 files changed, 1366 insertions(+), 99 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/tracing_multi_link.c create mode 100644 tools/testing/selftests/bpf/progs/tracing_multi_test.c