Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1505695rdb; Mon, 19 Feb 2024 19:52:12 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXSgxZklQYIjLtr0Knopik1kEC4CUOFbwlWUFCO5onff2uydXO1Wc16l43Csjl4pbumSo0Zxlgw/uxMPn2Dn6mWSGBTPi1lRLr2uGrXxA== X-Google-Smtp-Source: AGHT+IHSI98a0NRALnQBC8whyv5jz0sgzIc+eVSUjf7VUfAPPXUnXfj56PrUt1zQvFabkydAvO67 X-Received: by 2002:ae9:e205:0:b0:785:c9d1:549a with SMTP id c5-20020ae9e205000000b00785c9d1549amr15765752qkc.56.1708401131824; Mon, 19 Feb 2024 19:52:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708401131; cv=pass; d=google.com; s=arc-20160816; b=btY/Av0ZHdHzBReYhlJj2i1eUz2/jLhKH3lD1Iod7FaPVvYenAdHUu9sw+dwKjcPTv nhxVQ1dSR6U/Lot85TRryZulKbTMG6RRc53WY3isw7lIaThq4Z0V9klUGS3d71IkTqOG q0O79Y1+WMLx4y90k8YV/kIiejaBPwNM/zp1dr4TDj5+aZRf+TQt22XtjLtWAhKSwFGi gtjtnrMCrZYPSrg1PsHwM3ckCBNbo57+BY5PpwGdzKuEiHQBrVcWo3h6FQSEzlqcUXZN otnEks6lZ1ovMBoRYygXLi1qutzQicw+XgaCXlv+wsNJ8YjKgVo/mJ3jpk/tTCIV1HXA E/Qg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Snh1k6Nld5+bv3WZ3l7dPau1Zu+9Ra2XDZxa5NzBZqk=; fh=A+zPyWPVFWYVSgFMFnfcWXZfPRtQoKJmYTqr6D9fHNE=; b=RCIb7W/i7+1reXDB3hxlkhZ5N5KKUqiD3ur8/T3rS42b0NGiPrqN8AHYVgzL8NR7K4 LrFRte4beNObF4QXKEnSsgQyx9+cMmU95H7lelqGRWe9D3OZ11Y9TiPqx9mc02f1SGzl wRGETfViboWJnXWlbAj4Xaa9xNVpW9538ScbIc3QsKJrf3b+/UBFEumbh5GnGdXSu7ja I4kt4/Y7FKKtSIrZ9saSIm783n4Pe5YY36ZVzPGuy0fBMIdqii3GOviF4PxbBw07kEOz PDzU/mH/5ICSboh7UOpP+Y1eqz1bolxv1LOkvMup+5JAV5kbzWMkFJ4AsuGALAddh3ht rOXQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=a8MUyTXR; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-72293-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72293-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id oq2-20020a05620a610200b00785dadcf1fdsi7583626qkn.23.2024.02.19.19.52.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 19:52:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72293-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=a8MUyTXR; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-72293-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72293-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 828341C21292 for ; Tue, 20 Feb 2024 03:52:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9F36D58207; Tue, 20 Feb 2024 03:51:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="a8MUyTXR" Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) (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 5423C58135 for ; Tue, 20 Feb 2024 03:51:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.195 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708401091; cv=none; b=Eg10Oz2HZPZqBLST5EShvgjcNd4z6eQTobPxT4aFaK+fHOT3S6vzLnqOG3fs8GZcZcrzv4xWuFK4v+GGVtYI+E/hBGdKvYgy9iGr2Kxd/8Ou63cJXJc31i9ImaLHiMurDQGS/SJm4TRVhyQhMIBKG342Kj6fXGeZbi8ihOx9fzY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708401091; c=relaxed/simple; bh=6+Gbjw6HF7/xondO+kClgQlDhke7d2kt7MutGrzeEn0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bsS9an/PTTaNxveJkWNyucVNgOQ0WNL/bmcAiN0hgCsLNek0ktqAqmsqUCITFS/yx7vsBI3CxrAcHDpr9a+t+vw6SlXED2B62wC8M8hq9JxJqjMAsgDuhQdu0TuKzQ0eflpZNoqmppeJz1qe9Zw4de5kjXWVc/dNyCE2Ie5SM1E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=a8MUyTXR; arc=none smtp.client-ip=209.85.214.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Received: by mail-pl1-f195.google.com with SMTP id d9443c01a7336-1d746ce7d13so32803235ad.0 for ; Mon, 19 Feb 2024 19:51:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1708401090; x=1709005890; 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=Snh1k6Nld5+bv3WZ3l7dPau1Zu+9Ra2XDZxa5NzBZqk=; b=a8MUyTXR7yP1fLN1nWIOnw2HxG5Xy5AKZOcM+fOaja0wIen1PJiPqjXrO8h/NibpX4 D9PJg+qoSd0oNjgJW4PVFh05q3U7EqAo+MtViXgXFa7YQBvTQFUKcaykgLnwxtN8et2t tDoyWJ1RII2Z7PIJX4qFup3JK4LW9ZbXWhOnhOIyGs+l+mU05KxP5z+ldChlBMsGFixA kOq7LrQOqbogP8HhJMIq2Ny5kWZlCbbVKXlqmmsiKJZpG5mjGpI//Mj8eeHfIQKeYuPj DoTVW4iRKohrMrX8Y/GD/FNcl4ttyc8kshUif4PASLqtXSNJHDgSbQM0QUGyyPo9j/NY G1iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708401090; x=1709005890; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Snh1k6Nld5+bv3WZ3l7dPau1Zu+9Ra2XDZxa5NzBZqk=; b=M4/2+66DQRH6eWiVlWXq+AgspPW6nfBITpVHKO8lLaTnGMKLnc35Ko/1GET+BGjY8E WyeG/ceY4WphQ0dJxsZa6RfQfnWGfPImFUsSG5xsXuSbk+21mPJkPfHh/6WJLmrA0WPu Ao/1fOxAm4ISRjANPtJWxBNXqe5oCDIXxK1lmZFw++q4KE0MZlw3geSOnTw4YK4KWFK5 WInqbBqXrzaQXNy9nijCX5GWJM/QepwUOA/FxcUUqHPeI0bXFrPuoU8moCaws662A+f5 472t0c/KZNdIpbgfJlsyHiqZlmHNOc5Y27nLoxYvN2E/1ufBxGR9OfCX5g6eJP21XARS KJjA== X-Forwarded-Encrypted: i=1; AJvYcCXPw3eTGCzbLciQGwyo0quao7OeQ03u0STjXqiBX5XCCYULd3JrC0t1DvzFX0az9hzyw4ZSY8bIXxUuvgnOH1V1i+P4qnizb1VxAJIs X-Gm-Message-State: AOJu0YxjxaJRzqYx45b16inVmPvNH3DI2hAi4sL5fsC8vIwKk+KV+8wM WERKUT+E1EK9TNj8jcv6Caqxe//B6p+ZfZsqWRcw65OD4fwtgnh+10al3LxMs6Y= X-Received: by 2002:a17:902:6806:b0:1db:cbff:df15 with SMTP id h6-20020a170902680600b001dbcbffdf15mr7399748plk.9.1708401089700; Mon, 19 Feb 2024 19:51:29 -0800 (PST) Received: from localhost.localdomain ([43.129.25.208]) by smtp.gmail.com with ESMTPSA id jz7-20020a170903430700b001d94678a76csm5131723plb.117.2024.02.19.19.51.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 19:51:29 -0800 (PST) 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, mykolal@fb.com, shuah@kernel.org, mcoquelin.stm32@gmail.com, alexandre.torgue@foss.st.com, thinker.li@gmail.com, dongmenglong.8@bytedance.com, zhoufeng.zf@bytedance.com, davemarchevsky@fb.com, dxu@dxuuu.xyz, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org Subject: [PATCH bpf-next 4/5] libbpf: add the function libbpf_find_kernel_btf_id() Date: Tue, 20 Feb 2024 11:51:04 +0800 Message-Id: <20240220035105.34626-5-dongmenglong.8@bytedance.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240220035105.34626-1-dongmenglong.8@bytedance.com> References: <20240220035105.34626-1-dongmenglong.8@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add new function libbpf_find_kernel_btf_id() to find the btf type id of the kernel, including vmlinux and modules. Signed-off-by: Menglong Dong --- tools/lib/bpf/libbpf.c | 83 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 3 ++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 87 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 01f407591a92..44e34007de8c 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9500,6 +9500,89 @@ int libbpf_find_vmlinux_btf_id(const char *name, return libbpf_err(err); } +int libbpf_find_kernel_btf_id(const char *name, + enum bpf_attach_type attach_type, + int *btf_obj_fd, int *btf_type_id) +{ + struct btf *btf, *vmlinux_btf; + struct bpf_btf_info info; + __u32 btf_id = 0, len; + char btf_name[64]; + int err, fd; + + vmlinux_btf = btf__load_vmlinux_btf(); + err = libbpf_get_error(vmlinux_btf); + if (err) + return libbpf_err(err); + + err = find_attach_btf_id(vmlinux_btf, name, attach_type); + if (err > 0) { + *btf_type_id = err; + *btf_obj_fd = 0; + err = 0; + goto out; + } + + /* kernel too old to support module BTFs */ + if (!feat_supported(NULL, FEAT_MODULE_BTF)) { + err = -EOPNOTSUPP; + goto out; + } + + while (true) { + err = bpf_btf_get_next_id(btf_id, &btf_id); + if (err) { + err = -errno; + goto out; + } + + fd = bpf_btf_get_fd_by_id(btf_id); + if (fd < 0) { + if (errno == ENOENT) + continue; + err = -errno; + goto out; + } + + len = sizeof(info); + memset(&info, 0, sizeof(info)); + info.name = ptr_to_u64(btf_name); + info.name_len = sizeof(btf_name); + + err = bpf_btf_get_info_by_fd(fd, &info, &len); + if (err) { + err = -errno; + goto fd_out; + } + + if (!info.kernel_btf || strcmp(btf_name, "vmlinux") == 0) { + close(fd); + continue; + } + + btf = btf_get_from_fd(fd, vmlinux_btf); + err = libbpf_get_error(btf); + if (err) + goto fd_out; + + err = find_attach_btf_id(btf, name, attach_type); + if (err > 0) { + *btf_type_id = err; + *btf_obj_fd = fd; + err = 0; + break; + } + close(fd); + continue; +fd_out: + close(fd); + break; + } +out: + btf__free(vmlinux_btf); + return err; +} + static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) { struct bpf_prog_info info; diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 5723cbbfcc41..ca151bbec833 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -306,6 +306,9 @@ LIBBPF_API int libbpf_attach_type_by_name(const char *name, enum bpf_attach_type *attach_type); LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name, enum bpf_attach_type attach_type); +LIBBPF_API int libbpf_find_kernel_btf_id(const char *name, + enum bpf_attach_type attach_type, + int *btf_obj_fd, int *btf_type_id); /* Accessors of bpf_program */ struct bpf_program; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 86804fd90dd1..73c60f47b4bb 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -413,4 +413,5 @@ LIBBPF_1.4.0 { bpf_token_create; btf__new_split; btf_ext__raw_data; + libbpf_find_kernel_btf_id; } LIBBPF_1.3.0; -- 2.39.2