From patchwork Wed Aug 28 11:11:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13781125 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 4D31113D53D; Wed, 28 Aug 2024 11:12:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843540; cv=none; b=hWOhplaWxG6qgI3rYffmA2uPPm9Eevj9r6ZmryGlQiOZfF9aJxOQN3xHhy9c+7ZREMlQpl+zKPs817YflqqfNNukOhI4RoLnasnotbX5Mo1OKzi0B3H5FzqeJinz3uRnauQ4kar5XSrIYUqWWokzX8tIsamaiksbwZTF3zAHeB0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843540; c=relaxed/simple; bh=OfS31y7CMGl383UND8A8tV3VaDssrFQuOg4dCa5VMIA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hrC42WEp3ta62zDwegQ32KxcRZ+5ML8Rz4Ds313OXhIRyLCEt9M01tfAHMnFdzhdf2ck88GcQx8PL28YvjJ0UPK2yj7ArMEZYm2RC3JoZMuu1yscHwaPvK2Ou57/HSZWmMD9fobeIK7jjRV/tW8OMmUQ/rILQO9YZsGwr7Lz6QY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hKDuZah+; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hKDuZah+" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-202508cb8ebso44508365ad.3; Wed, 28 Aug 2024 04:12:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724843538; x=1725448338; 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=W8R8TPIxkmmj3JhM7dtAmgzbws1KXgQVijrPpPFtSJ8=; b=hKDuZah+/vyy/5f40G2OypSzwBZvBHNAzRsLD7JWIhhpnCnQrHn+xc79Tb0x7mHx9g fSSXCY1XZr1IR3VU9nlLQWmr1Teur9ereqCOnPK+urZVEdM0XZIwN95AjirU2z5ejJFL 5JIHSLlvexXP+qasDF+jRR8vaeLIGYMI4QgKkMY768w/IXUUI19H6IjwW7TeI+Sg2lAP afKBruWU8C38jDnq9fczjmlFJCZKA7Ns6ebScJDDZ75C+ZuoOWdNpU0gCEJ0OEc7qve0 ThXsD7/1pbKp8/2Tpc2I6oU3vi9wWQ6vRP52oPeCO7Nv9WnVRTz5p8r4G60I7Ees/P+Z tN4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724843538; x=1725448338; 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=W8R8TPIxkmmj3JhM7dtAmgzbws1KXgQVijrPpPFtSJ8=; b=hSYkCrwwOXqmiosIPbjj/gQEP2fy0EffddMk2HbA5jKwo3DR2UCSS09zWzzxghdWxb LG++7EHD/wa2YadsVF2eDKJEMLad1dMOTdGuPUB25CEZHlUwLOAboFnRta17R1QIxOWm jfjYOweHOaiUEPPKxw8yvjnxuZAEdnw6BV+XJlU25HWWTPv2AjK+nQ2tEEVvwZV04W3o DWZiVhYEV3Vul0eAchHQ+5FJVRiFF+Df0OUMO52sv2i5/EycXFNpTyhZ+hU5ce9RUDTK S6S8Xyx4Ulxq7Ns590TlFfwQG1HdEO+TnocJ8iaSTGsWDS+xS5MMVsjwBNzNAnwtZp1t /+Vw== X-Forwarded-Encrypted: i=1; AJvYcCWArAmoSrXDPKqdwpV95utUGy6CQtwKNy1oXEOhGweFTM7KZp+VM7iGpz9scoaU8Zdao/zl+Y7Wr0OE/QN/lHs=@vger.kernel.org X-Gm-Message-State: AOJu0YwRzvAha3uaTMs6VZcRYJk7tbHqXs7Vf3gPiN/uwzxTg5KcxH0j k1XeYuNHic8sikt4QNhqiuTbD/4lHKYSnY88bCqUWAxQ3JHh0twmZ2TOrWrW X-Google-Smtp-Source: AGHT+IEGD2jtEO1F50nUJoFkERrZoWNWXIqXEOTLbpEoqXoaFjDpiH6U+XEqrnShjDrnGvY05cekbA== X-Received: by 2002:a17:902:da88:b0:204:e578:1b32 with SMTP id d9443c01a7336-204f9c50d04mr19389565ad.53.1724843538050; Wed, 28 Aug 2024 04:12:18 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-203855678f9sm97369395ad.1.2024.08.28.04.12.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 04:12:17 -0700 (PDT) From: Tony Ambardar To: bpf@vger.kernel.org Cc: Tony Ambardar , linux-kselftest@vger.kernel.org, Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Ilya Leoshkevich , Quentin Monnet Subject: [PATCH bpf-next v3 1/8] libbpf: Improve log message formatting Date: Wed, 28 Aug 2024 04:11:51 -0700 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Fix missing newlines and extraneous terminal spaces in messages. Signed-off-by: Tony Ambardar --- tools/lib/bpf/btf.c | 6 +++--- tools/lib/bpf/btf_dump.c | 2 +- tools/lib/bpf/btf_relocate.c | 2 +- tools/lib/bpf/libbpf.c | 4 ++-- tools/lib/bpf/relo_core.c | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 32c00db3b91b..f5081de86ee0 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -2940,7 +2940,7 @@ static int btf_ext_setup_info(struct btf_ext *btf_ext, /* If no records, return failure now so .BTF.ext won't be used. */ if (!info_left) { - pr_debug("%s section in .BTF.ext has no records", ext_sec->desc); + pr_debug("%s section in .BTF.ext has no records\n", ext_sec->desc); return -EINVAL; } @@ -3028,7 +3028,7 @@ static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) if (data_size < offsetofend(struct btf_ext_header, hdr_len) || data_size < hdr->hdr_len) { - pr_debug("BTF.ext header not found"); + pr_debug("BTF.ext header not found\n"); return -EINVAL; } @@ -3290,7 +3290,7 @@ int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts) d = btf_dedup_new(btf, opts); if (IS_ERR(d)) { - pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d)); + pr_debug("btf_dedup_new failed: %ld\n", PTR_ERR(d)); return libbpf_err(-EINVAL); } diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index 894860111ddb..18cbcf342f2b 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -1304,7 +1304,7 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id, * chain, restore stack, emit warning, and try to * proceed nevertheless */ - pr_warn("not enough memory for decl stack:%d", err); + pr_warn("not enough memory for decl stack: %d\n", err); d->decl_stack_cnt = stack_start; return; } diff --git a/tools/lib/bpf/btf_relocate.c b/tools/lib/bpf/btf_relocate.c index 4f7399d85eab..b72f83e15156 100644 --- a/tools/lib/bpf/btf_relocate.c +++ b/tools/lib/bpf/btf_relocate.c @@ -428,7 +428,7 @@ static int btf_relocate_rewrite_strs(struct btf_relocate *r, __u32 i) } else { off = r->str_map[*str_off]; if (!off) { - pr_warn("string '%s' [offset %u] is not mapped to base BTF", + pr_warn("string '%s' [offset %u] is not mapped to base BTF\n", btf__str_by_offset(r->btf, off), *str_off); return -ENOENT; } diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index e55353887439..8a0a0c1e37e1 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -12753,7 +12753,7 @@ struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog, } if (prog->type != BPF_PROG_TYPE_EXT) { - pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace", + pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace\n", prog->name); return libbpf_err_ptr(-EINVAL); } @@ -13843,7 +13843,7 @@ int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s) map_type = btf__type_by_id(btf, map_type_id); if (!btf_is_datasec(map_type)) { - pr_warn("type for map '%1$s' is not a datasec: %2$s", + pr_warn("type for map '%1$s' is not a datasec: %2$s\n", bpf_map__name(map), __btf_kind_str(btf_kind(map_type))); return libbpf_err(-EINVAL); diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c index 63a4d5ad12d1..7632e9d41827 100644 --- a/tools/lib/bpf/relo_core.c +++ b/tools/lib/bpf/relo_core.c @@ -1339,7 +1339,7 @@ int bpf_core_calc_relo_insn(const char *prog_name, cands->cands[i].id, cand_spec); if (err < 0) { bpf_core_format_spec(spec_buf, sizeof(spec_buf), cand_spec); - pr_warn("prog '%s': relo #%d: error matching candidate #%d %s: %d\n ", + pr_warn("prog '%s': relo #%d: error matching candidate #%d %s: %d\n", prog_name, relo_idx, i, spec_buf, err); return err; } From patchwork Wed Aug 28 11:11:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13781126 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 1A38913D53D; Wed, 28 Aug 2024 11:12:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843544; cv=none; b=K5G9bzh/aoKiV0VhCbrQOuTFyLI80xrOTvyPXuDMY/0pZHYYx6eTdTXi6t+g5YlQn4apg28KLS7bfoolun+AotyhE+GnoNQ/HDOB4Alon/fiTXGlE1yBuotGnss2BtVxmLPCUG2YX6Sz0wp81/pZZofR2fJIM01AoH1YwwXADkE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843544; c=relaxed/simple; bh=UJoIZbAwd0dcyPldGPlUH3SNsQcKTlS8LGIL6P+fhDs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=s+6sYHIDrurNNdei0lJxnGyyA5Xr9n6MOQzi3DV10MK6Pu2LXnjWRgpee1t8DmCgseqJ6oJXYa7PQ2nzzcjsMa7uFsKHI/mik0YCRxu8s0qvqrLa6f4PzJGD3dvex+iTPk0zMPsz4SZaSQ87Iiqxgoxfsr6GGEglfzodcVQgYYk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AhPraint; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AhPraint" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-202089e57d8so3323795ad.0; Wed, 28 Aug 2024 04:12:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724843542; x=1725448342; 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=p7S9DLK/xi9tXkFIx+X5J9oUvgA+SnKs7nCanjbxcE8=; b=AhPraintUZlmgQoYKncsG0/yWLtft5BgJH6IpCJbyGRBY1YRV4991J8eBTz295j4iK 7nKUsxbZkhwi21hQkeLAzkw7LqByf3BpNqEGVpozKrh4ab5kZRkDVAyyrMNKdzIysXGY pfD0DJPQw8HUPBrYQN1+puSu+2k/7hSskW+7WLla9i6UKlc5kLWctyMDZg3nkLU60C8D K6NOyflLoSsZZxj9c4TFt17m42tZB2bB0xAooZ/m00OgzREQBt64XrV0DMpIL/I4Tt2+ men/YXTn1v2v1SlIBSzXyuRX8fE2KtItKW4umiQUfbJTOFR3sD3L3jdPXL9Dj4t/kqKa yksg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724843542; x=1725448342; 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=p7S9DLK/xi9tXkFIx+X5J9oUvgA+SnKs7nCanjbxcE8=; b=aBfwRmvrSK1xNWH4lcJCEumVcMq5zcX9wKtcPwIpp/Vz1zooP5WaYqdxTAiApmJcOf kEE/m/OPg793oDZZB7OFps8s5b3WhKqK9TWmPZuKXbCrby6ToATjmbx+K3aZoGPc8MYK anp7f3MI5OU1dcUkpcr63/o/jlRXpITQM5KYYlk4pq6f/XmcgxmpssrQBVyGJWeZRV5B SnRli9ZcHu7xvyLGzfW0Ushap6Vqyqiqnf98G87ySVA4TiQFlQg2mhmxwq9e6PkM7A8I zAJHWoLTVjVFz6X7TntTU0SLtgVRb4MjY9bVLj5WjrjPsZ+SJMBNeGjg1GkRUi2LRzSY EcZA== X-Forwarded-Encrypted: i=1; AJvYcCXqK8jGiPzbvzDR+qD5DLDvuKZ9Z+888GQFa+n63rofW5jMJaTpMMRlfPVz98cRhDtXSizP+Yhh5clMmjcxmqY=@vger.kernel.org X-Gm-Message-State: AOJu0YymlSrSeQhGtAxC+XrOZJ7gMdfECDkd128df0/wrruUlY7xznMx KmUFn3GwgHcz+B6U5G817nrZRB/TH+vlGv1wwUfcOsif3EDl4PZhPdhUPB9l X-Google-Smtp-Source: AGHT+IEPIc8xe+SGGPTh5+Wb+z07lGclv1e7VW4Pscx+ktaHVaAW5DwfVGeqIV6JYcKM9RV2qfDITA== X-Received: by 2002:a17:902:ea12:b0:202:21:eb2a with SMTP id d9443c01a7336-204f9bdf0ddmr30608845ad.19.1724843542111; Wed, 28 Aug 2024 04:12:22 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-203855678f9sm97369395ad.1.2024.08.28.04.12.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 04:12:21 -0700 (PDT) From: Tony Ambardar To: bpf@vger.kernel.org Cc: Tony Ambardar , linux-kselftest@vger.kernel.org, Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Ilya Leoshkevich , Quentin Monnet Subject: [PATCH bpf-next v3 2/8] libbpf: Fix header comment typos for BTF.ext Date: Wed, 28 Aug 2024 04:11:52 -0700 Message-Id: <299dcd6119c4a69649c0331db55c7d23bcdc7465.1724843049.git.tony.ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Mention struct btf_ext_info_sec rather than non-existent btf_sec_func_info in BTF.ext struct documentation. Signed-off-by: Tony Ambardar --- tools/lib/bpf/libbpf_internal.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 408df59e0771..8cda511a1982 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -448,11 +448,11 @@ struct btf_ext_info { * * The func_info subsection layout: * record size for struct bpf_func_info in the func_info subsection - * struct btf_sec_func_info for section #1 + * struct btf_ext_info_sec for section #1 * a list of bpf_func_info records for section #1 * where struct bpf_func_info mimics one in include/uapi/linux/bpf.h * but may not be identical - * struct btf_sec_func_info for section #2 + * struct btf_ext_info_sec for section #2 * a list of bpf_func_info records for section #2 * ...... * From patchwork Wed Aug 28 11:11:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13781127 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 3455316CD13; Wed, 28 Aug 2024 11:12:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843546; cv=none; b=HelKnDKbVvj0KGTCQihN9A37yk7HNF+DY6w9C45e6VuEmKyr/rtMnFm6EiRXBjl+FE35glshFfEIE84uxMRwbjVNqlRkMv2eFTXA0pLvXg5C/iSpK/07x/jvrl20PRZHFawieObp0/CT9SmLrS1qjwpon1NFC/wB3284zYqHH44= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843546; c=relaxed/simple; bh=p+VcdB9HCA/6O6oQIVFwtXKCMYlajKRORAljsPWPaw4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=D9h0xjSo/Geva6jTc58zOrY3fE+9z1wNbLs2+VltVyuH/ZC/fKD/2atkBOf4W5g9eX3eEU2QRpoZqXQYM8u5evy6ee3z/ixv/y93HkrydbrI1wNZL+9F+cph0D6KdTlHuNrXju0YtBWbXDUfSzWPIHdPeWwMXS2t6OfG6Xnp3r0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dCv7wRhW; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dCv7wRhW" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-201f2b7fe0dso56591905ad.1; Wed, 28 Aug 2024 04:12:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724843544; x=1725448344; 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=+DiB3vSoKAyJWAd5i8cP5vFqzZUUYpSQG/CLty7944A=; b=dCv7wRhWmxqSGs6D3tw6mS56m0cELcD3pqlANtoDd0hVP9muyGC3ZMCemN7R67V3wK mlEtaUq74KZWDu2LIqP2vLw1OaqDeVtw9TdCTae7azsuwBziDGhKlOWDLuFtuQn/PyvE Swtn2Rzk+paOg+KMN9l2niDFDc7KlsT4HFuSHiZ1I31F0MImL9cTeoNkhpbjvSMuMoSS GHpzWmiPNKkCU0eq+BsZcshM9njoNjNWVBldG88skSPdmTha2fgfnOsP0Grs3Txgp5z+ LsZYnijSSxyk7H2xnSxmvLT5S7HxEs7h/NoVFX0yG4p7ceeEB3epekaeFsnGPYK4fD+b yg1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724843544; x=1725448344; 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=+DiB3vSoKAyJWAd5i8cP5vFqzZUUYpSQG/CLty7944A=; b=mygeHQc1QvnakQQZruPUc0DkOTo/c/Ws9um3wHu7emjoWCsRu0XvWGUmvcHyEhrWb2 zlrQzogZOBQP/GkyPFyL3NQnVGrPDFsVyi3CguWBX1KDdql6hm5HHGyq4A3gsQgoyahN yZvb6hn2ZnjfVFzcwliky4npEZxleP/PpgSdCvAtHkYazlGAphBMFjDPJcUYjYb6k/lf lLt06PJQBeqppYzcbGmydih7aqNLG4OhnNEwgUaRUT2BlBedqklmdDBTMmite7nZQMGI l+qW+0Pn3H4so4wmmxSPjx6CJ+osx1j7LB7GQQEg3RbsZ+Vl69xYze0VOWwRgGeGQtfM O/RQ== X-Forwarded-Encrypted: i=1; AJvYcCUjdelNcUSmTAtZItv8VVmk+FXtQlx4LDwALh/cOYs0EI7SjduZna6YuGSxXVTI09Vkoe9e6DYLJjqCj6w9FfA=@vger.kernel.org X-Gm-Message-State: AOJu0YxSiGfzubFjrhObAxkGXjgRWekJYQe/tycZJm2rXgySEThABLY2 XAEmB7FxwntCRQtQV0P2eWqntvUHQZl9668KLvjncBtPHSl5alnm9pLtKBD+ X-Google-Smtp-Source: AGHT+IHA56i203JmiU+eFcmRoDAIfwWdWWqvblFBbcfXwd85GoLcPbhzIPIhPdcutqkyRjzfjplO6A== X-Received: by 2002:a17:903:40c6:b0:201:f9c1:f543 with SMTP id d9443c01a7336-2039e47a4damr148843695ad.18.1724843544199; Wed, 28 Aug 2024 04:12:24 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-203855678f9sm97369395ad.1.2024.08.28.04.12.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 04:12:23 -0700 (PDT) From: Tony Ambardar To: bpf@vger.kernel.org Cc: Tony Ambardar , linux-kselftest@vger.kernel.org, Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Ilya Leoshkevich , Quentin Monnet Subject: [PATCH bpf-next v3 3/8] libbpf: Fix output .symtab byte-order during linking Date: Wed, 28 Aug 2024 04:11:53 -0700 Message-Id: <290a54c2a1a507c265cf19dc068f3186da78de06.1724843049.git.tony.ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Object linking output data uses the default ELF_T_BYTE type for '.symtab' section data, which disables any libelf-based translation. Explicitly set the ELF_T_SYM type for output to restore libelf's byte-order conversion, noting that input '.symtab' data is already correctly translated. Fixes: faf6ed321cf6 ("libbpf: Add BPF static linker APIs") Signed-off-by: Tony Ambardar --- tools/lib/bpf/linker.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c index 9cd3d4109788..7489306cd6f7 100644 --- a/tools/lib/bpf/linker.c +++ b/tools/lib/bpf/linker.c @@ -396,6 +396,8 @@ static int init_output_elf(struct bpf_linker *linker, const char *file) pr_warn_elf("failed to create SYMTAB data"); return -EINVAL; } + /* Ensure libelf translates byte-order of symbol records */ + sec->data->d_type = ELF_T_SYM; str_off = strset__add_str(linker->strtab_strs, sec->sec_name); if (str_off < 0) From patchwork Wed Aug 28 11:11:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13781128 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 A6EFA16D330; Wed, 28 Aug 2024 11:12:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843550; cv=none; b=jWnvABxQ/ge+HKHuYpPVFac6kF7n61XggC45lQ7LZSBTiQGcZOOtJUpVNQHK6IfbmjMO7Jx9/2ZkaBV7fIhAnoTL8THFKV1We433yUK8fEJf6wlaOkRUj2JU9LxU1CQK41eU+kQ60ncgE/X28B2AX1y5Pn9S7krNr4nnN3c2ZB0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843550; c=relaxed/simple; bh=cCSoUGIyKrugqLiIrLt7jjiKapYN9RFiwzRWrolQPk0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WVoBwXc4EuHpQ0maSXBVNXRdMqxvkDB8+aBE0jcTbQz7Vh0PdrMOAT1p53sDEXLgzYDxu9smDNimXvkzodLUR9hv+mYbL7sr+RIYeBLXRvVdMxbuY7FHNM2hDaD2kOZ4ezvACedR9kTKu5TgNGtq/LsZpWXOzpmIrr6FjxV2oAE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iStrVjgl; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iStrVjgl" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-202146e9538so59242945ad.3; Wed, 28 Aug 2024 04:12:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724843547; x=1725448347; 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=Rd+XrfNSZgsU9ay0utzjZwVOwYbcth9X5XnC4C0ipnM=; b=iStrVjglZtYnvaSRVkfF4bMTcR9vOB0sULVcAoF961Q77Yyu4JBIAA77hJfuqGqxKE DBZU3z52rk9rz4+t/NYT5CH8juXCUUfAyclCbitOU5NNbier9zYYAiL9kyFwckaNiRmW uBbV8WWY2IjbOMcvpK6FBkJURLeHFwGEaskWhFNR4ZfgtunOXMDQ+oha4Z101UWsiWVj OyM2Iu3xv3U6Az6OZXto9z1tnuxUGUwg/hXv6mQSiFpKZwcbiT6kxWfWmHaWzFOLflQ3 x2yNYwSefz/va6IG93Wg483F3QwrAew7y/gpAwfJVM27QoKDO7rEJf55ioKGLVCMQTmr Qiew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724843547; x=1725448347; 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=Rd+XrfNSZgsU9ay0utzjZwVOwYbcth9X5XnC4C0ipnM=; b=m3jYrpPFQDUHwWV2zM6QwS+fF8LFk1fnxQSrZM/3UwNZhBmOrg+evd6XOhPag6VjbY R2f/yOsvH4vYU4vwjtHlju9EwAqe8ZMflRSWM01fnymJiXGeqWog0j2xeo8T5rH7rwY0 T0JLRS/DoPnkmdN1D0dnujjahDcKN9qZ64DjcLCSUryjUb1ZKrGKAFdQKWhGZLozrZWX Rcoc8FumJ4VcHX/nXRxEYpOroz8cLjO9KE5ptzIO6NGCCq86Nd8xBiZDbCg0QELEIBhL kqucDoAEwhrHi9tBBzhPabdD1GFgUm9dfGcs3ViDWqMqYiNfvK4OybP8N8+Vee2SyOjs FpQQ== X-Forwarded-Encrypted: i=1; AJvYcCUpsVe/4RTko7T03tNy33VRPYLRsrT2mlNEE0qjzdR/oYVz4MlIPymv0HX8bp8sfbMTBzCecJFPm3OHvx3avsY=@vger.kernel.org X-Gm-Message-State: AOJu0YzX0q3L8tf6ND8yNEkvZbD8eO57kqEVHXIPssIivZc/JNe7s1Qi G8bvhxs5ZUM9plhMdfvWX8cz0NMfz0pQfbtY229OEDtHpDrFDxujDoy7YfcO X-Google-Smtp-Source: AGHT+IFd9upWmhr62Rf/sNzxKWYyWgaHmVHrbeF4ItfcbombynVBijjEcl43rRnN37UOKnXQSrRcUg== X-Received: by 2002:a17:902:dace:b0:1fb:57e7:5bb4 with SMTP id d9443c01a7336-2039e4bbee4mr207546275ad.37.1724843547492; Wed, 28 Aug 2024 04:12:27 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-203855678f9sm97369395ad.1.2024.08.28.04.12.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 04:12:27 -0700 (PDT) From: Tony Ambardar To: bpf@vger.kernel.org Cc: Tony Ambardar , linux-kselftest@vger.kernel.org, Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Ilya Leoshkevich , Quentin Monnet Subject: [PATCH bpf-next v3 4/8] libbpf: Support BTF.ext loading and output in either endianness Date: Wed, 28 Aug 2024 04:11:54 -0700 Message-Id: <2f0ebdf09179d2674080aad60a9befd8acd89177.1724843049.git.tony.ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Support for handling BTF data of either endianness was added in [1], but did not include BTF.ext data for lack of use cases. Later, support for static linking [2] provided a use case, but this feature and later ones were restricted to native-endian usage. Add support for BTF.ext handling in either endianness. Convert BTF.ext data to native endianness when read into memory for further processing, and support raw data access that restores the original byte-order for output. Add new API functions btf_ext__endianness() and btf_ext__set_endianness() for query and setting byte-order, as already exist for BTF data. [1] 3289959b97ca ("libbpf: Support BTF loading and raw data output in both endianness") [2] 8fd27bf69b86 ("libbpf: Add BPF static linker BTF and BTF.ext support") Signed-off-by: Tony Ambardar --- tools/lib/bpf/btf.c | 226 +++++++++++++++++++++++++++++--- tools/lib/bpf/btf.h | 3 + tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/libbpf_internal.h | 2 + 4 files changed, 217 insertions(+), 16 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index f5081de86ee0..07e39131d02f 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -3022,25 +3022,136 @@ static int btf_ext_setup_core_relos(struct btf_ext *btf_ext) return btf_ext_setup_info(btf_ext, ¶m); } -static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) +/* Swap byte-order of BTF.ext header with any endianness */ +static void btf_ext_bswap_hdr(struct btf_ext *btf_ext, __u32 hdr_len) { - const struct btf_ext_header *hdr = (struct btf_ext_header *)data; + struct btf_ext_header *h = btf_ext->hdr; - if (data_size < offsetofend(struct btf_ext_header, hdr_len) || - data_size < hdr->hdr_len) { - pr_debug("BTF.ext header not found\n"); + h->magic = bswap_16(h->magic); + h->hdr_len = bswap_32(h->hdr_len); + h->func_info_off = bswap_32(h->func_info_off); + h->func_info_len = bswap_32(h->func_info_len); + h->line_info_off = bswap_32(h->line_info_off); + h->line_info_len = bswap_32(h->line_info_len); + + if (hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) + return; + + h->core_relo_off = bswap_32(h->core_relo_off); + h->core_relo_len = bswap_32(h->core_relo_len); +} + +/* Callback funcs to swap each type of info record, returning its size */ +static int func_info_bswap(void *p) +{ + struct bpf_func_info *fi = p; + + fi->insn_off = bswap_32(fi->insn_off); + fi->type_id = bswap_32(fi->type_id); + return sizeof(*fi); +} + +static int line_info_bswap(void *p) +{ + struct bpf_line_info *li = p; + + li->insn_off = bswap_32(li->insn_off); + li->file_name_off = bswap_32(li->file_name_off); + li->line_off = bswap_32(li->line_off); + li->line_col = bswap_32(li->line_col); + return sizeof(*li); +} + +static int core_relo_bswap(void *p) +{ + struct bpf_core_relo *cr = p; + + _Static_assert(sizeof(cr->kind) == sizeof(__u32), + "enum bpf_core_relo_kind is not 32-bit\n"); + cr->insn_off = bswap_32(cr->insn_off); + cr->type_id = bswap_32(cr->type_id); + cr->access_str_off = bswap_32(cr->access_str_off); + cr->kind = bswap_32(cr->kind); + return sizeof(*cr); +} + +/* Swap byte-order of a generic info subsection */ +static void info_subsec_bswap(const struct btf_ext_header *hdr, bool native, + __u32 off, __u32 len, int (*fn_swap)(void *)) +{ + __u32 left, i, *rs, rec_size, num_info; + struct btf_ext_info_sec *si; + void *p; + + if (len == 0) + return; + + rs = (void *)hdr + hdr->hdr_len + off; /* record size */ + si = (void *)rs + sizeof(__u32); /* sec info #1 */ + rec_size = native ? *rs : bswap_32(*rs); + *rs = bswap_32(*rs); + left = len - sizeof(__u32); + while (left > 0) { + num_info = native ? si->num_info : bswap_32(si->num_info); + si->sec_name_off = bswap_32(si->sec_name_off); + si->num_info = bswap_32(si->num_info); + left -= offsetof(struct btf_ext_info_sec, data); + p = si->data; + for (i = 0; i < num_info; i++) /* list of records */ + p += fn_swap(p); + si = p; + left -= rec_size * num_info; + } +} + +/* + * Swap endianness of the info segment in a BTF.ext data section: + * - requires BTF.ext header data in native byte order + * - only support info structs from BTF version 1 + * - native: current info data is native endianness + */ +static void btf_ext_bswap_info(struct btf_ext *btf_ext, bool native) +{ + const struct btf_ext_header *hdr = btf_ext->hdr; + + /* Swap func_info subsection byte-order */ + info_subsec_bswap(hdr, native, hdr->func_info_off, hdr->func_info_len, + &func_info_bswap); + + /* Swap line_info subsection byte-order */ + info_subsec_bswap(hdr, native, hdr->line_info_off, hdr->line_info_len, + &line_info_bswap); + + /* Swap core_relo subsection byte-order (if present) */ + if (hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) + return; + + info_subsec_bswap(hdr, native, hdr->core_relo_off, hdr->core_relo_len, + &core_relo_bswap); +} + +/* Validate hdr data & info sections, convert to native endianness */ +static int btf_ext_parse(struct btf_ext *btf_ext) +{ + struct btf_ext_header *hdr = btf_ext->hdr; + __u32 hdr_len, info_size, data_size = btf_ext->data_size; + + if (data_size < offsetofend(struct btf_ext_header, hdr_len)) { + pr_debug("BTF.ext header too short\n"); return -EINVAL; } + hdr_len = hdr->hdr_len; if (hdr->magic == bswap_16(BTF_MAGIC)) { - pr_warn("BTF.ext in non-native endianness is not supported\n"); - return -ENOTSUP; + btf_ext->swapped_endian = true; + hdr_len = bswap_32(hdr_len); } else if (hdr->magic != BTF_MAGIC) { pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic); return -EINVAL; } - if (hdr->version != BTF_VERSION) { + /* Ensure known version of structs, current BTF_VERSION == 1 */ + if (hdr->version != 1) { pr_debug("Unsupported BTF.ext version:%u\n", hdr->version); return -ENOTSUP; } @@ -3050,11 +3161,42 @@ static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) return -ENOTSUP; } - if (data_size == hdr->hdr_len) { + if (data_size < hdr_len) { + pr_debug("BTF.ext header not found\n"); + return -EINVAL; + } else if (data_size == hdr_len) { pr_debug("BTF.ext has no data\n"); return -EINVAL; } + /* Verify mandatory hdr info details present */ + if (hdr_len < offsetofend(struct btf_ext_header, line_info_len)) { + pr_warn("BTF.ext header missing func_info, line_info\n"); + return -EINVAL; + } + + /* Keep hdr native byte-order in memory for introspection */ + if (btf_ext->swapped_endian) + btf_ext_bswap_hdr(btf_ext, hdr_len); + + /* Basic info section consistency checks*/ + info_size = btf_ext->data_size - hdr_len; + if (info_size & 0x03) { + pr_warn("BTF.ext info size not 4-byte multiple\n"); + return -EINVAL; + } + info_size -= hdr->func_info_len + hdr->line_info_len; + if (hdr_len >= offsetofend(struct btf_ext_header, core_relo_len)) + info_size -= hdr->core_relo_len; + if (info_size) { + pr_warn("BTF.ext info size mismatch with header data\n"); + return -EINVAL; + } + + /* Keep infos native byte-order in memory for introspection */ + if (btf_ext->swapped_endian) + btf_ext_bswap_info(btf_ext, !btf_ext->swapped_endian); + return 0; } @@ -3066,6 +3208,7 @@ void btf_ext__free(struct btf_ext *btf_ext) free(btf_ext->line_info.sec_idxs); free(btf_ext->core_relo_info.sec_idxs); free(btf_ext->data); + free(btf_ext->data_swapped); free(btf_ext); } @@ -3086,15 +3229,10 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size) } memcpy(btf_ext->data, data, size); - err = btf_ext_parse_hdr(btf_ext->data, size); + err = btf_ext_parse(btf_ext); if (err) goto done; - if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, line_info_len)) { - err = -EINVAL; - goto done; - } - err = btf_ext_setup_func_info(btf_ext); if (err) goto done; @@ -3119,15 +3257,71 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size) return btf_ext; } +static void *btf_ext_raw_data(const struct btf_ext *btf_ext_ro, __u32 *size, + bool swap_endian) +{ + struct btf_ext *btf_ext = (struct btf_ext *)btf_ext_ro; + const __u32 data_sz = btf_ext->data_size; + void *data; + + data = swap_endian ? btf_ext->data_swapped : btf_ext->data; + if (data) { + *size = data_sz; + return data; + } + + data = calloc(1, data_sz); + if (!data) + return NULL; + memcpy(data, btf_ext->data, data_sz); + + if (swap_endian) { + btf_ext_bswap_info(btf_ext, true); + btf_ext_bswap_hdr(btf_ext, btf_ext->hdr->hdr_len); + btf_ext->data_swapped = data; + } + + *size = data_sz; + return data; +} + const void *btf_ext__raw_data(const struct btf_ext *btf_ext, __u32 *size) { + __u32 data_sz; + void *data; + + data = btf_ext_raw_data(btf_ext, &data_sz, btf_ext->swapped_endian); + if (!data) + return errno = ENOMEM, NULL; + *size = btf_ext->data_size; - return btf_ext->data; + return data; } __attribute__((alias("btf_ext__raw_data"))) const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size); +enum btf_endianness btf_ext__endianness(const struct btf_ext *btf_ext) +{ + if (is_host_big_endian()) + return btf_ext->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN; + else + return btf_ext->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; +} + +int btf_ext__set_endianness(struct btf_ext *btf_ext, enum btf_endianness endian) +{ + if (endian != BTF_LITTLE_ENDIAN && endian != BTF_BIG_ENDIAN) + return libbpf_err(-EINVAL); + + btf_ext->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN); + + if (!btf_ext->swapped_endian) { + free(btf_ext->data_swapped); + btf_ext->data_swapped = NULL; + } + return 0; +} struct btf_dedup; diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index b68d216837a9..e3cf91687c78 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -167,6 +167,9 @@ LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset); LIBBPF_API struct btf_ext *btf_ext__new(const __u8 *data, __u32 size); LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext); LIBBPF_API const void *btf_ext__raw_data(const struct btf_ext *btf_ext, __u32 *size); +LIBBPF_API enum btf_endianness btf_ext__endianness(const struct btf_ext *btf_ext); +LIBBPF_API int btf_ext__set_endianness(struct btf_ext *btf_ext, + enum btf_endianness endian); LIBBPF_API int btf__find_str(struct btf *btf, const char *s); LIBBPF_API int btf__add_str(struct btf *btf, const char *s); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 8f0d9ea3b1b4..5c17632807b6 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -421,6 +421,8 @@ LIBBPF_1.5.0 { global: btf__distill_base; btf__relocate; + btf_ext__endianness; + btf_ext__set_endianness; bpf_map__autoattach; bpf_map__set_autoattach; bpf_program__attach_sockmap; diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 8cda511a1982..6b0270c83537 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -484,6 +484,8 @@ struct btf_ext { struct btf_ext_header *hdr; void *data; }; + void *data_swapped; + bool swapped_endian; struct btf_ext_info func_info; struct btf_ext_info line_info; struct btf_ext_info core_relo_info; From patchwork Wed Aug 28 11:11:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13781129 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 CC35816C866; Wed, 28 Aug 2024 11:12:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843554; cv=none; b=sADr7ZQsgtmmfFgBhAyAQQjry7vkoGKfzPQGFza6JPJ3R0WChBgoDncSkEwS1jrDxgsnA3sXeMi/gh40q3pE2N4XXWofbVJn/Fdqv+lSZBhHLrpUQIitYAbgSO3PYZxkcpZfvGdQ1uO2ilm+gkcZkkt1k/yDnDi8sUOi5XfBWis= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843554; c=relaxed/simple; bh=TFmo7jLEnYrhP8A58hd61Cvucm5weeCITeWXJicaggY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=L1eWHYZD8T8lO6jvvoxAWr2bPaEYfPvGyj6wASaI059mMNvLyavUnsPdGnBPRlE26LRCV+uVc3msvDg1mbvJNgOMiOCiWQxPoouHcwrlX65et2SJuznPIuE/YXOvQzQQcDh3pzB3X12a5l7tq4t+utdgRcIN7l5/MrThelbhhcE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=W4b1COWS; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="W4b1COWS" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-20230059241so55795545ad.3; Wed, 28 Aug 2024 04:12:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724843551; x=1725448351; 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=18WDQ6Euc19xaDRjDIZJNMiPYXZ56Cc/DlgzvBkCFQE=; b=W4b1COWSdwfE44TeAXEcP1eF1H3Dnw+uPYyZh1qRArKM1QqzvHdS2A4pleRYMwlDL2 WGf7wIZAAL9ECQGMIO8GnbDV4R/HAbfOSSsLpG1mzfvhR5DtiXDq2KTW5KOxXICI2SeV PLcKklQVrXoM7fAUcPWNxxy7Lj0nIAVWGcKckplKcO6wJHWuRcrOPa9p/rUpbL2kUi9e X5Az8IN3eGXEF7CLUWYJb7xeTgiKTKbXdQVLR7TCj2WaSmOitqME38/a5aPhICJDW6OV F/VGU6PZwjeHrveTKsWGvEakZkA6FCzqGDHMJBkv56ffLVXMuQq3nKl/+xM795PpI5gJ 4oog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724843551; x=1725448351; 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=18WDQ6Euc19xaDRjDIZJNMiPYXZ56Cc/DlgzvBkCFQE=; b=H08AwkH3Es3QhXnzYCRoSo1A68aVRF25DugBw49n7UlYuhVICJaxjmcWzGZe7z9+jn U9WxI49yaYsqMtdV2EaVfi2NTpeBMVZYisxZiInj3eDCC3fTvd5ZeIrgJGRWvUKnYHoB TEgUVFEwD6X0c2g9vyE1Ga2aNVukPinsXqw0GkH7lUD/QOsJNB5MvNW5q9Yr2rLpHvc+ BjbypTmA/UFycZUUazkhdPveXdIXqVpc7nSuD1df83xRtweYAnoNc+PE7zJGGCcmnJfe kbKEbK0pIFpBygOLsz0VdCySgi6eddsPj/M9y9kltremRGez1HAoeiH3Ier/lpRJ4i+R 6tkA== X-Forwarded-Encrypted: i=1; AJvYcCVLaOpuSojVlmd36Qud+m1yGQadiK460ELIqIyqtC15WPcqYxVfRSFiauBnaHlOo+0iy2nTn2SLHAAu80iSvzs=@vger.kernel.org X-Gm-Message-State: AOJu0Yx09CA41IlYQAXA0zYN/17m7dcTsALcpu6wl7jF84yNVcSCSJ73 B1EdVoLGEzxICKEjaxn4Nsttt8mIbR4Hm6k7S5pgYI1LcjxODn/YlSfser75 X-Google-Smtp-Source: AGHT+IHFp0gi3BEc4vX4bZTdn2dlfk4YIvQpFGzlC9jSOpu8ssJi7c+sb3epfcbPwF0e1MdLm2MBvg== X-Received: by 2002:a17:903:31c3:b0:202:2b3e:28d0 with SMTP id d9443c01a7336-2039e44c41fmr130155665ad.8.1724843550700; Wed, 28 Aug 2024 04:12:30 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-203855678f9sm97369395ad.1.2024.08.28.04.12.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 04:12:30 -0700 (PDT) From: Tony Ambardar To: bpf@vger.kernel.org Cc: Tony Ambardar , linux-kselftest@vger.kernel.org, Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Ilya Leoshkevich , Quentin Monnet Subject: [PATCH bpf-next v3 5/8] libbpf: Support opening bpf objects of either endianness Date: Wed, 28 Aug 2024 04:11:55 -0700 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Allow bpf_object__open() to access files of either endianness, and convert included BPF programs to native byte-order in-memory for introspection. Loading BPF objects of non-native byte-order is still disallowed however. Signed-off-by: Tony Ambardar --- tools/lib/bpf/libbpf.c | 34 +++++++++++++++++++++++++-------- tools/lib/bpf/libbpf_internal.h | 11 +++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8a0a0c1e37e1..27a5cb8ccf80 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -940,6 +940,21 @@ bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data, return 0; } +static void bpf_object_bswap_progs(struct bpf_object *obj) +{ + struct bpf_program *prog = obj->programs; + struct bpf_insn *insn; + int p, i; + + for (p = 0; p < obj->nr_programs; p++, prog++) { + insn = prog->insns; + for (i = 0; i < prog->insns_cnt; i++, insn++) + bpf_insn_bswap(insn); + pr_debug("prog '%s': converted %zu insns to native byte order\n", + prog->name, prog->insns_cnt); + } +} + static const struct btf_member * find_member_by_offset(const struct btf_type *t, __u32 bit_offset) { @@ -1599,19 +1614,15 @@ static int bpf_object__elf_init(struct bpf_object *obj) return err; } -static int bpf_object__check_endianness(struct bpf_object *obj) +static bool is_native_endianness(struct bpf_object *obj) { #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2LSB) - return 0; + return obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2LSB; #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB) - return 0; + return obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB; #else # error "Unrecognized __BYTE_ORDER__" #endif - pr_warn("elf: endianness mismatch in %s.\n", obj->path); - return -LIBBPF_ERRNO__ENDIAN; } static int @@ -3953,6 +3964,10 @@ static int bpf_object__elf_collect(struct bpf_object *obj) return -LIBBPF_ERRNO__FORMAT; } + /* change BPF program insns to native endianness for introspection */ + if (!is_native_endianness(obj)) + bpf_object_bswap_progs(obj); + /* sort BPF programs by section name and in-section instruction offset * for faster search */ @@ -7993,7 +8008,6 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, } err = bpf_object__elf_init(obj); - err = err ? : bpf_object__check_endianness(obj); err = err ? : bpf_object__elf_collect(obj); err = err ? : bpf_object__collect_externs(obj); err = err ? : bpf_object_fixup_btf(obj); @@ -8498,6 +8512,10 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch if (obj->gen_loader) bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps); + else if (!is_native_endianness(obj)) { + pr_warn("object '%s' is not native endianness\n", obj->name); + return libbpf_err(-LIBBPF_ERRNO__ENDIAN); + } err = bpf_object_prepare_token(obj); err = err ? : bpf_object__probe_loading(obj); diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 6b0270c83537..020bcbf8a566 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -590,6 +591,16 @@ static inline bool is_ldimm64_insn(struct bpf_insn *insn) return insn->code == (BPF_LD | BPF_IMM | BPF_DW); } +static inline void bpf_insn_bswap(struct bpf_insn *insn) +{ + __u8 tmp_reg = insn->dst_reg; + + insn->dst_reg = insn->src_reg; + insn->src_reg = tmp_reg; + insn->off = bswap_16(insn->off); + insn->imm = bswap_32(insn->imm); +} + /* Unconditionally dup FD, ensuring it doesn't use [0, 2] range. * Original FD is not closed or altered in any other way. * Preserves original FD value, if it's invalid (negative). From patchwork Wed Aug 28 11:11:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13781130 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (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 A81CC16EB53; Wed, 28 Aug 2024 11:12:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843556; cv=none; b=roNuMfgRPunnUg9yCIjteMPQDncfBwQ8dTEPXr8mCBC9rzfS+YMB2UrY5apgNIENO1BO3g1+UNI1920l59xF1gdqHJ9N7TOYrMKwy3mfUhfR+t+Vu7StDRhDiSBq5PVQjigs2Ymm1pvf1zHyJg5nj6Q3NCEcJJC/2mj0nymaXTQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843556; c=relaxed/simple; bh=bZFiVLxsPqJFp+7OFMaqX0ZvCLgCDYIYtrLGMsK2oz0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kqfBaaE2GA/iVPwUmrpj5DcyRZwbWWoOky5Ti959rT7XFrT+Y6rt+77kQVTaoqM5QxbZb+MF1+UQ4gPKdQ1iP8BR8lHZ0RMXb62XRXcfsyEi2GZgXYQKZP7rP7g86flUVX3XHi3n/leW03Hlvku1UWAvszhDQ7U5y/ZHOIluTI0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ZLytrttJ; arc=none smtp.client-ip=209.85.210.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZLytrttJ" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-7142a30e3bdso377769b3a.0; Wed, 28 Aug 2024 04:12:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724843553; x=1725448353; 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=lCLEqvYFcYfbFDmBpnULkP78s+23ipk0h/fW+02tbBM=; b=ZLytrttJRy82WPqijQuaGfs7NX80h1QGZ4XBhr9Oihn/RX8jomyRkRAvxXU/hk9B67 E0ejuPggmJGrxWs3f+UvPC1GO78gxneHECHA9nE7MVsT7eH59VFrYVYP6AlcBsIK05LC cTcp63hMY1nzT8lQ+dvQiZuZDyj8ByL3bbq9oiXotgW9TvRacf/wcSqxmeotElt377Wb 5UerZyRrXssSxj/Rq+Bpt8YF4yd6DXvdthsnmrZw7hCcDBG+HXCKELLn7Uq7oKQ/X4P3 NjXB9HU5vJRQeWpyTmJ0P1PpHwemSrsaW/dpGzu7zYINL5/6oq4LJMwI4xAD2QqTK4ZI VFbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724843553; x=1725448353; 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=lCLEqvYFcYfbFDmBpnULkP78s+23ipk0h/fW+02tbBM=; b=YUFyzM9PW9ktFMkMU7NfjJHSJnNrzkJdheduFWP6YnL+avAotX6NWwIp/AxuxbQMyq mq1zASZPL/CkzyaRP/I5FRuVIAKckdU2J9EEWc1+DOUXwyB9F8+sAbAcav/WEI5uwWfv ItVQcuxpwwArMFoSMKeRdcZakn4HFv65w8Ikc/u5VKzDgGcdKy4/EKm80beGM4q826mR b/Fwe97ahhZjYOiY1C97A9LX03LSukqeAQiAYMs3cqHQOlpkjnZp1GAWpkmJa4Ngrtyc y35f7jNq7swCWzd9jBPz+j25ic/ncH1CeWKiNTSn1HtgnkHF/6dooXX+3iN+eHdP/yK6 JUag== X-Forwarded-Encrypted: i=1; AJvYcCV5sPuHIC0jUwGwod6GrqsUaoAaBD58carpwlIrWQuunspIMKfJo3lMusd6PsguI/nmA5VXddXbdIF9sKKLyfY=@vger.kernel.org X-Gm-Message-State: AOJu0YyhfEq3F5Fgvi+/F/Mlrh4eUaLyFEHM6rrUuJU++2attKSXSSZH vNtD5WaQUyMryuZcSldtll7dcdfi4rgcAaIQHsnP6YilP2aXnwASeugBiiUU X-Google-Smtp-Source: AGHT+IFs1BgkDZRzQVSkj+QbKuBXXVtOvntvqDEvkJy8E02YpVC0gLSPckg3xS14m2EOCi3FDtA3XA== X-Received: by 2002:a17:902:7082:b0:203:b05a:ad9f with SMTP id d9443c01a7336-204f9c83994mr19646995ad.32.1724843553383; Wed, 28 Aug 2024 04:12:33 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-203855678f9sm97369395ad.1.2024.08.28.04.12.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 04:12:33 -0700 (PDT) From: Tony Ambardar To: bpf@vger.kernel.org Cc: Tony Ambardar , linux-kselftest@vger.kernel.org, Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Ilya Leoshkevich , Quentin Monnet Subject: [PATCH bpf-next v3 6/8] libbpf: Support linking bpf objects of either endianness Date: Wed, 28 Aug 2024 04:11:56 -0700 Message-Id: <715c93be5421e319b0640f17a5c423fdebda9329.1724843049.git.tony.ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Allow static linking object files of either endianness, checking that input files have consistent byte-order, and setting output endianness from input. Linking requires in-memory processing of programs, relocations, sections, etc. in native endianness, and output conversion to target byte-order. This is enabled by built-in ELF translation and recent BTF/BTF.ext endianness functions. Further add local functions for swapping byte-order of sections containing BPF insns. Signed-off-by: Tony Ambardar --- tools/lib/bpf/linker.c | 90 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 16 deletions(-) diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c index 7489306cd6f7..bd97da68eed6 100644 --- a/tools/lib/bpf/linker.c +++ b/tools/lib/bpf/linker.c @@ -135,6 +135,7 @@ struct bpf_linker { int fd; Elf *elf; Elf64_Ehdr *elf_hdr; + bool swapped_endian; /* Output sections metadata */ struct dst_sec *secs; @@ -324,13 +325,8 @@ static int init_output_elf(struct bpf_linker *linker, const char *file) linker->elf_hdr->e_machine = EM_BPF; linker->elf_hdr->e_type = ET_REL; -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2LSB; -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2MSB; -#else -#error "Unknown __BYTE_ORDER__" -#endif + /* Set unknown ELF endianness, assign later from input files */ + linker->elf_hdr->e_ident[EI_DATA] = ELFDATANONE; /* STRTAB */ /* initialize strset with an empty string to conform to ELF */ @@ -541,19 +537,21 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename, const struct bpf_linker_file_opts *opts, struct src_obj *obj) { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - const int host_endianness = ELFDATA2LSB; -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - const int host_endianness = ELFDATA2MSB; -#else -#error "Unknown __BYTE_ORDER__" -#endif int err = 0; Elf_Scn *scn; Elf_Data *data; Elf64_Ehdr *ehdr; Elf64_Shdr *shdr; struct src_sec *sec; + unsigned char obj_byteorder; + unsigned char link_byteorder = linker->elf_hdr->e_ident[EI_DATA]; +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + const unsigned char host_byteorder = ELFDATA2LSB; +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + const unsigned char host_byteorder = ELFDATA2MSB; +#else +#error "Unknown __BYTE_ORDER__" +#endif pr_debug("linker: adding object file '%s'...\n", filename); @@ -579,11 +577,25 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename, pr_warn_elf("failed to get ELF header for %s", filename); return err; } - if (ehdr->e_ident[EI_DATA] != host_endianness) { + + /* Linker output endianness set by first input object */ + obj_byteorder = ehdr->e_ident[EI_DATA]; + if (obj_byteorder != ELFDATA2LSB && obj_byteorder != ELFDATA2MSB) { + err = -EOPNOTSUPP; + pr_warn("unknown byte order of ELF file %s\n", filename); + return err; + } + if (link_byteorder == ELFDATANONE) { + linker->elf_hdr->e_ident[EI_DATA] = obj_byteorder; + linker->swapped_endian = obj_byteorder != host_byteorder; + pr_debug("linker: set %s-endian output byte order\n", + obj_byteorder == ELFDATA2MSB ? "big" : "little"); + } else if (link_byteorder != obj_byteorder) { err = -EOPNOTSUPP; - pr_warn_elf("unsupported byte order of ELF file %s", filename); + pr_warn("byte order mismatch with ELF file %s\n", filename); return err; } + if (ehdr->e_type != ET_REL || ehdr->e_machine != EM_BPF || ehdr->e_ident[EI_CLASS] != ELFCLASS64) { @@ -1111,6 +1123,27 @@ static bool sec_content_is_same(struct dst_sec *dst_sec, struct src_sec *src_sec return true; } +static bool is_exec_sec(struct dst_sec *sec) +{ + if (!sec || sec->ephemeral) + return false; + return (sec->shdr->sh_type == SHT_PROGBITS) && + (sec->shdr->sh_flags & SHF_EXECINSTR); +} + +static int exec_sec_bswap(void *raw_data, int size) +{ + const int insn_cnt = size / sizeof(struct bpf_insn); + struct bpf_insn *insn = raw_data; + int i; + + if (size % sizeof(struct bpf_insn)) + return -EINVAL; + for (i = 0; i < insn_cnt; i++, insn++) + bpf_insn_bswap(insn); + return 0; +} + static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src_sec *src) { void *tmp; @@ -1170,6 +1203,10 @@ static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src memset(dst->raw_data + dst->sec_sz, 0, dst_align_sz - dst->sec_sz); /* now copy src data at a properly aligned offset */ memcpy(dst->raw_data + dst_align_sz, src->data->d_buf, src->shdr->sh_size); + + /* convert added bpf insns to native byte-order */ + if (linker->swapped_endian && is_exec_sec(dst)) + exec_sec_bswap(dst->raw_data + dst_align_sz, src->shdr->sh_size); } dst->sec_sz = dst_final_sz; @@ -2630,6 +2667,10 @@ int bpf_linker__finalize(struct bpf_linker *linker) if (!sec->scn) continue; + /* restore sections with bpf insns to target byte-order */ + if (linker->swapped_endian && is_exec_sec(sec)) + exec_sec_bswap(sec->raw_data, sec->sec_sz); + sec->data->d_buf = sec->raw_data; } @@ -2698,6 +2739,7 @@ static int emit_elf_data_sec(struct bpf_linker *linker, const char *sec_name, static int finalize_btf(struct bpf_linker *linker) { + enum btf_endianness link_endianness; LIBBPF_OPTS(btf_dedup_opts, opts); struct btf *btf = linker->btf; const void *raw_data; @@ -2742,6 +2784,22 @@ static int finalize_btf(struct bpf_linker *linker) return err; } + /* Set .BTF and .BTF.ext output byte order */ + link_endianness = linker->elf_hdr->e_ident[EI_DATA] == ELFDATA2MSB ? + BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; + err = btf__set_endianness(linker->btf, link_endianness); + if (err) { + pr_warn("failed to set .BTF output endianness: %d\n", err); + return err; + } + if (linker->btf_ext) { + err = btf_ext__set_endianness(linker->btf_ext, link_endianness); + if (err) { + pr_warn("failed to set .BTF.ext output endianness: %d\n", err); + return err; + } + } + /* Emit .BTF section */ raw_data = btf__raw_data(linker->btf, &raw_sz); if (!raw_data) From patchwork Wed Aug 28 11:11:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13781131 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 08FA1172767; Wed, 28 Aug 2024 11:12:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843559; cv=none; b=JRAibOiKpW1ZyPrQRCNPtrYCdVfTOAkTyDzYSnatY/MWRwqEUpad7oA17Mit2LfhRTi2za2I8Zgld/giYQnz4ElMsgbbjk3jRGjXCR8Oa9wSreXzO90e/Izfd93VAqWpog/H66waCiZNLNRcykdaDyILYY1DNLMA5tCAL8/xJ2Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843559; c=relaxed/simple; bh=Nj8+74z3FgL0oq7tYkEuVz77IgLZtDA7c3NprnvCWKw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=J2If4SNpEYdMMWcTPTDdaroQ4aLGRFrQ2fxH6SIXQrVv9fdGkMrV8/DPgozcGW0FT4JoYRsWgpvWp94phylhERHH7HMQCSXHVccAEZNgqr8uouT+wsmTwVPUVpDQ3zzMXyXmYk8nIMeVhr3FWhBikQYqKKcf4vwolVRM1z/o5uQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ALEpsqD9; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ALEpsqD9" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-202146e9538so59244765ad.3; Wed, 28 Aug 2024 04:12:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724843557; x=1725448357; 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=FL+MstLZzhgmhBrAVs33NC8PQ7U+wxqxkgXFE5BJp9s=; b=ALEpsqD9X8QAd5vGnM9J2aFDcRApskHsNz6S0cDxQHRfDCX7WwnvGD9FT3iUHpvTxV HeqdR5JgGChd0OcT/UPmN2sluh9//jDd6jp+c38nNvx2Wy87mKqCi0Hc0r2F5J8qtP6m w2V1HwRBm3VXz/ZwgNaB6PvOEPpTbEDkS+gmcz7GnKK8sihfMSjO7ilPIcMadszpIaHt cgo6+AUmrHjE0ax6A3JF/9QGvf2RBmfBSdydftO0gdWHf6J0XC4G2VaP4vSzEZLSRGmw eHJvvygyXa0YaitAN1EmvjgzWaKWLAjAZYBMKXbxQ+tvZ87SdbqCKZK/O72pXcyvBXzz MoOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724843557; x=1725448357; 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=FL+MstLZzhgmhBrAVs33NC8PQ7U+wxqxkgXFE5BJp9s=; b=VinZKZp3Ep1pa2iZdSG0QxeLnSDYaIVpkBvjeAzZguBFl6UiDbxm2o3x+q+D+pIAgI zFYfyUDGukAHhhPUjxYjnxl/FDABukHNrFhVC+4gk/Ux9qIyq8BIFMl7yUGjoBluAKDT IMlDhJVdz6FpRBDshmqpxgDEsda82y93/oJB/gUkw1eYq01CZ3RDgAXs7RDpTfThmXUf pcLmwKiUwRHYj2jN7H++g2hxTqszdFvtKTpWhxNPQxqajQZKJldkti83R9jzXRPs7KAp YdyiAXB1BBuETDBq93jC8PaGf2HrXIcxQbxeIJ4/j3SNILnrarrabQaaUVUJ9eEh1xEI PEIA== X-Forwarded-Encrypted: i=1; AJvYcCXV/LeFx5wTJHWW9qPbzTU269JP69D6EvTcoJqExX9ErY+T/zTKfAVzGuJ6Qzg4/IMgX7TS7lWI5uokwA2LKrA=@vger.kernel.org X-Gm-Message-State: AOJu0YxlVD8Ccok3o9W7+iL3DG8DvbkRQglkTRtonbrsCXSoZ6r8wHA1 GONqS1b3CthogZLWvZgxynHCCDT0nHZO9aUiPU2XPa+3n8ZF3+lwsDchVEIO X-Google-Smtp-Source: AGHT+IFTSihLWy7VUDJz/7DXRiFRlY6LdwcaAPe85w87YWrZMRn5IwXnFX7IOJWoFssOWHBtyT0mBw== X-Received: by 2002:a17:902:d4cd:b0:202:3dcf:8c47 with SMTP id d9443c01a7336-2039e50d41cmr223739475ad.59.1724843556838; Wed, 28 Aug 2024 04:12:36 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-203855678f9sm97369395ad.1.2024.08.28.04.12.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 04:12:36 -0700 (PDT) From: Tony Ambardar To: bpf@vger.kernel.org Cc: Tony Ambardar , linux-kselftest@vger.kernel.org, Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Ilya Leoshkevich , Quentin Monnet Subject: [PATCH bpf-next v3 7/8] libbpf: Support creating light skeleton of either endianness Date: Wed, 28 Aug 2024 04:11:57 -0700 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Track target endianness in 'struct bpf_gen' and process in-memory data in native byte-order, but on finalization convert the embedded loader BPF insns to target endianness. The light skeleton also includes a target-accessed data blob which is heterogeneous and thus difficult to convert to target byte-order on finalization. Add support functions to convert data to target endianness as it is added to the blob. Also add additional debug logging for data blob structure details and skeleton loading. Signed-off-by: Tony Ambardar --- tools/lib/bpf/bpf_gen_internal.h | 1 + tools/lib/bpf/gen_loader.c | 185 +++++++++++++++++++++++-------- tools/lib/bpf/libbpf.c | 1 + tools/lib/bpf/skel_internal.h | 3 +- 4 files changed, 145 insertions(+), 45 deletions(-) diff --git a/tools/lib/bpf/bpf_gen_internal.h b/tools/lib/bpf/bpf_gen_internal.h index fdf44403ff36..6ff963a491d9 100644 --- a/tools/lib/bpf/bpf_gen_internal.h +++ b/tools/lib/bpf/bpf_gen_internal.h @@ -34,6 +34,7 @@ struct bpf_gen { void *data_cur; void *insn_start; void *insn_cur; + bool swapped_endian; ssize_t cleanup_label; __u32 nr_progs; __u32 nr_maps; diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c index cf3323fd47b8..ace22bd1bb94 100644 --- a/tools/lib/bpf/gen_loader.c +++ b/tools/lib/bpf/gen_loader.c @@ -401,6 +401,15 @@ int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps) opts->insns_sz = gen->insn_cur - gen->insn_start; opts->data = gen->data_start; opts->data_sz = gen->data_cur - gen->data_start; + + /* use target endianness for embedded loader */ + if (gen->swapped_endian) { + struct bpf_insn *insn = (struct bpf_insn *)opts->insns; + int insn_cnt = opts->insns_sz / sizeof(struct bpf_insn); + + for (i = 0; i < insn_cnt; i++) + bpf_insn_bswap(insn++); + } } return gen->error; } @@ -414,6 +423,31 @@ void bpf_gen__free(struct bpf_gen *gen) free(gen); } +/* + * Fields of bpf_attr are set to values in native byte-order before being + * written to the target-bound data blob, and may need endian conversion. + * This macro allows providing the correct value in situ more simply than + * writing a separate converter for *all fields* of *all records* included + * in union bpf_attr. Note that sizeof(rval) should match the assignment + * target to avoid runtime problems. + */ +#define tgt_endian(rval) ({ \ + typeof(rval) _val; \ + if (!gen->swapped_endian) \ + _val = (rval); \ + else { \ + switch (sizeof(rval)) { \ + case 1: _val = (rval); break; \ + case 2: _val = bswap_16(rval); break; \ + case 4: _val = bswap_32(rval); break; \ + case 8: _val = bswap_64(rval); break; \ + default:_val = (rval); \ + pr_warn("unsupported bswap size!\n"); \ + } \ + } \ + _val; \ +}) + void bpf_gen__load_btf(struct bpf_gen *gen, const void *btf_raw_data, __u32 btf_raw_size) { @@ -422,11 +456,13 @@ void bpf_gen__load_btf(struct bpf_gen *gen, const void *btf_raw_data, union bpf_attr attr; memset(&attr, 0, attr_size); - pr_debug("gen: load_btf: size %d\n", btf_raw_size); btf_data = add_data(gen, btf_raw_data, btf_raw_size); + pr_debug("gen: load_btf: off %d size %d\n", btf_data, btf_raw_size); - attr.btf_size = btf_raw_size; + attr.btf_size = tgt_endian(btf_raw_size); btf_load_attr = add_data(gen, &attr, attr_size); + pr_debug("gen: load_btf: btf_load_attr: off %d size %d\n", + btf_load_attr, attr_size); /* populate union bpf_attr with user provided log details */ move_ctx2blob(gen, attr_field(btf_load_attr, btf_log_level), 4, @@ -457,28 +493,30 @@ void bpf_gen__map_create(struct bpf_gen *gen, union bpf_attr attr; memset(&attr, 0, attr_size); - attr.map_type = map_type; - attr.key_size = key_size; - attr.value_size = value_size; - attr.map_flags = map_attr->map_flags; - attr.map_extra = map_attr->map_extra; + attr.map_type = tgt_endian(map_type); + attr.key_size = tgt_endian(key_size); + attr.value_size = tgt_endian(value_size); + attr.map_flags = tgt_endian(map_attr->map_flags); + attr.map_extra = tgt_endian(map_attr->map_extra); if (map_name) libbpf_strlcpy(attr.map_name, map_name, sizeof(attr.map_name)); - attr.numa_node = map_attr->numa_node; - attr.map_ifindex = map_attr->map_ifindex; - attr.max_entries = max_entries; - attr.btf_key_type_id = map_attr->btf_key_type_id; - attr.btf_value_type_id = map_attr->btf_value_type_id; + attr.numa_node = tgt_endian(map_attr->numa_node); + attr.map_ifindex = tgt_endian(map_attr->map_ifindex); + attr.max_entries = tgt_endian(max_entries); + attr.btf_key_type_id = tgt_endian(map_attr->btf_key_type_id); + attr.btf_value_type_id = tgt_endian(map_attr->btf_value_type_id); pr_debug("gen: map_create: %s idx %d type %d value_type_id %d\n", - attr.map_name, map_idx, map_type, attr.btf_value_type_id); + map_name, map_idx, map_type, map_attr->btf_value_type_id); map_create_attr = add_data(gen, &attr, attr_size); - if (attr.btf_value_type_id) + pr_debug("gen: map_create: map_create_attr: off %d size %d\n", + map_create_attr, attr_size); + if (map_attr->btf_value_type_id) /* populate union bpf_attr with btf_fd saved in the stack earlier */ move_stack2blob(gen, attr_field(map_create_attr, btf_fd), 4, stack_off(btf_fd)); - switch (attr.map_type) { + switch (map_type) { case BPF_MAP_TYPE_ARRAY_OF_MAPS: case BPF_MAP_TYPE_HASH_OF_MAPS: move_stack2blob(gen, attr_field(map_create_attr, inner_map_fd), 4, @@ -498,8 +536,8 @@ void bpf_gen__map_create(struct bpf_gen *gen, /* emit MAP_CREATE command */ emit_sys_bpf(gen, BPF_MAP_CREATE, map_create_attr, attr_size); debug_ret(gen, "map_create %s idx %d type %d value_size %d value_btf_id %d", - attr.map_name, map_idx, map_type, value_size, - attr.btf_value_type_id); + map_name, map_idx, map_type, value_size, + map_attr->btf_value_type_id); emit_check_err(gen); /* remember map_fd in the stack, if successful */ if (map_idx < 0) { @@ -784,12 +822,12 @@ static void emit_relo_ksym_typeless(struct bpf_gen *gen, emit_ksym_relo_log(gen, relo, kdesc->ref); } -static __u32 src_reg_mask(void) +static __u32 src_reg_mask(struct bpf_gen *gen) { -#if defined(__LITTLE_ENDIAN_BITFIELD) - return 0x0f; /* src_reg,dst_reg,... */ -#elif defined(__BIG_ENDIAN_BITFIELD) - return 0xf0; /* dst_reg,src_reg,... */ +#if defined(__LITTLE_ENDIAN_BITFIELD) /* src_reg,dst_reg,... */ + return gen->swapped_endian ? 0xf0 : 0x0f; +#elif defined(__BIG_ENDIAN_BITFIELD) /* dst_reg,src_reg,... */ + return gen->swapped_endian ? 0x0f : 0xf0; #else #error "Unsupported bit endianness, cannot proceed" #endif @@ -840,7 +878,7 @@ static void emit_relo_ksym_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo, emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 3)); clear_src_reg: /* clear bpf_object__relocate_data's src_reg assignment, otherwise we get a verifier failure */ - reg_mask = src_reg_mask(); + reg_mask = src_reg_mask(gen); emit(gen, BPF_LDX_MEM(BPF_B, BPF_REG_9, BPF_REG_8, offsetofend(struct bpf_insn, code))); emit(gen, BPF_ALU32_IMM(BPF_AND, BPF_REG_9, reg_mask)); emit(gen, BPF_STX_MEM(BPF_B, BPF_REG_8, BPF_REG_9, offsetofend(struct bpf_insn, code))); @@ -931,11 +969,34 @@ static void cleanup_relos(struct bpf_gen *gen, int insns) cleanup_core_relo(gen); } +/* Convert func, line, and core relo info records to target endianness, + * checking the blob size is consistent with 32-bit fields. + */ +static void info_blob_bswap(struct bpf_gen *gen, int info_off, __u32 size) +{ + __u32 *field = gen->data_start + info_off; + int i, cnt = size / sizeof(__u32); + + if (size && (size % sizeof(__u32))) { + pr_warn("info records not using 32-bit fields!\n"); + return; + } + if (gen->swapped_endian) + for (i = 0; i < cnt; i++, field++) + *field = bswap_32(*field); +} + void bpf_gen__prog_load(struct bpf_gen *gen, enum bpf_prog_type prog_type, const char *prog_name, const char *license, struct bpf_insn *insns, size_t insn_cnt, struct bpf_prog_load_opts *load_attr, int prog_idx) { + int func_info_tot_sz = load_attr->func_info_cnt * + load_attr->func_info_rec_size; + int line_info_tot_sz = load_attr->line_info_cnt * + load_attr->line_info_rec_size; + int core_relo_tot_sz = gen->core_relo_cnt * + sizeof(struct bpf_core_relo); int prog_load_attr, license_off, insns_off, func_info, line_info, core_relos; int attr_size = offsetofend(union bpf_attr, core_relo_rec_size); union bpf_attr attr; @@ -947,32 +1008,60 @@ void bpf_gen__prog_load(struct bpf_gen *gen, license_off = add_data(gen, license, strlen(license) + 1); /* add insns to blob of bytes */ insns_off = add_data(gen, insns, insn_cnt * sizeof(struct bpf_insn)); + pr_debug("gen: prog_load: license off %d insn off %d\n", + license_off, insns_off); - attr.prog_type = prog_type; - attr.expected_attach_type = load_attr->expected_attach_type; - attr.attach_btf_id = load_attr->attach_btf_id; - attr.prog_ifindex = load_attr->prog_ifindex; - attr.kern_version = 0; - attr.insn_cnt = (__u32)insn_cnt; - attr.prog_flags = load_attr->prog_flags; - - attr.func_info_rec_size = load_attr->func_info_rec_size; - attr.func_info_cnt = load_attr->func_info_cnt; - func_info = add_data(gen, load_attr->func_info, - attr.func_info_cnt * attr.func_info_rec_size); + /* convert blob insns to target endianness */ + if (gen->swapped_endian) { + struct bpf_insn *insn = gen->data_start + insns_off; + int i; - attr.line_info_rec_size = load_attr->line_info_rec_size; - attr.line_info_cnt = load_attr->line_info_cnt; - line_info = add_data(gen, load_attr->line_info, - attr.line_info_cnt * attr.line_info_rec_size); + for (i = 0; i < insn_cnt; i++, insn++) + bpf_insn_bswap(insn); + } - attr.core_relo_rec_size = sizeof(struct bpf_core_relo); - attr.core_relo_cnt = gen->core_relo_cnt; - core_relos = add_data(gen, gen->core_relos, - attr.core_relo_cnt * attr.core_relo_rec_size); + attr.prog_type = tgt_endian(prog_type); + attr.expected_attach_type = tgt_endian(load_attr->expected_attach_type); + attr.attach_btf_id = tgt_endian(load_attr->attach_btf_id); + attr.prog_ifindex = tgt_endian(load_attr->prog_ifindex); + attr.kern_version = 0; + attr.insn_cnt = tgt_endian((__u32)insn_cnt); + attr.prog_flags = tgt_endian(load_attr->prog_flags); + + attr.func_info_rec_size = tgt_endian(load_attr->func_info_rec_size); + attr.func_info_cnt = tgt_endian(load_attr->func_info_cnt); + func_info = add_data(gen, load_attr->func_info, func_info_tot_sz); + pr_debug("gen: prog_load: func_info: off %d cnt %d rec size %d\n", + func_info, load_attr->func_info_cnt, + load_attr->func_info_rec_size); + + /* convert info blob fields to target endianness */ + info_blob_bswap(gen, func_info, func_info_tot_sz); + + attr.line_info_rec_size = tgt_endian(load_attr->line_info_rec_size); + attr.line_info_cnt = tgt_endian(load_attr->line_info_cnt); + line_info = add_data(gen, load_attr->line_info, line_info_tot_sz); + pr_debug("gen: prog_load: line_info: off %d cnt %d rec size %d\n", + line_info, load_attr->line_info_cnt, + load_attr->line_info_rec_size); + + /* convert info blob fields to target endianness */ + info_blob_bswap(gen, line_info, line_info_tot_sz); + + attr.core_relo_rec_size = tgt_endian((__u32)sizeof(struct bpf_core_relo)); + attr.core_relo_cnt = tgt_endian(gen->core_relo_cnt); + core_relos = add_data(gen, gen->core_relos, core_relo_tot_sz); + pr_debug("gen: prog_load: core_relos: off %d cnt %d rec size %zd\n", + core_relos, gen->core_relo_cnt, + sizeof(struct bpf_core_relo)); + + /* convert info blob fields to target endianness */ + info_blob_bswap(gen, core_relos, core_relo_tot_sz); libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name)); prog_load_attr = add_data(gen, &attr, attr_size); + pr_debug("gen: prog_load: prog_load_attr: off %d size %d\n", + prog_load_attr, attr_size); /* populate union bpf_attr with a pointer to license */ emit_rel_store(gen, attr_field(prog_load_attr, license), license_off); @@ -1068,6 +1157,8 @@ void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *pvalue, emit(gen, BPF_EMIT_CALL(BPF_FUNC_probe_read_kernel)); map_update_attr = add_data(gen, &attr, attr_size); + pr_debug("gen: map_update_elem: map_update_attr: off %d size %d\n", + map_update_attr, attr_size); move_blob2blob(gen, attr_field(map_update_attr, map_fd), 4, blob_fd_array_off(gen, map_idx)); emit_rel_store(gen, attr_field(map_update_attr, key), key); @@ -1084,14 +1175,18 @@ void bpf_gen__populate_outer_map(struct bpf_gen *gen, int outer_map_idx, int slo int attr_size = offsetofend(union bpf_attr, flags); int map_update_attr, key; union bpf_attr attr; + int tgt_slot; memset(&attr, 0, attr_size); pr_debug("gen: populate_outer_map: outer %d key %d inner %d\n", outer_map_idx, slot, inner_map_idx); - key = add_data(gen, &slot, sizeof(slot)); + tgt_slot = tgt_endian(slot); + key = add_data(gen, &tgt_slot, sizeof(tgt_slot)); map_update_attr = add_data(gen, &attr, attr_size); + pr_debug("gen: populate_outer_map: map_update_attr: off %d size %d\n", + map_update_attr, attr_size); move_blob2blob(gen, attr_field(map_update_attr, map_fd), 4, blob_fd_array_off(gen, outer_map_idx)); emit_rel_store(gen, attr_field(map_update_attr, key), key); @@ -1114,6 +1209,8 @@ void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx) memset(&attr, 0, attr_size); pr_debug("gen: map_freeze: idx %d\n", map_idx); map_freeze_attr = add_data(gen, &attr, attr_size); + pr_debug("gen: map_freeze: map_update_attr: off %d size %d\n", + map_freeze_attr, attr_size); move_blob2blob(gen, attr_field(map_freeze_attr, map_fd), 4, blob_fd_array_off(gen, map_idx)); /* emit MAP_FREEZE command */ diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 27a5cb8ccf80..c6860f24cb29 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9107,6 +9107,7 @@ int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts) if (!gen) return -ENOMEM; gen->opts = opts; + gen->swapped_endian = !is_native_endianness(obj); obj->gen_loader = gen; return 0; } diff --git a/tools/lib/bpf/skel_internal.h b/tools/lib/bpf/skel_internal.h index 1e82ab06c3eb..67e8477ecb5b 100644 --- a/tools/lib/bpf/skel_internal.h +++ b/tools/lib/bpf/skel_internal.h @@ -351,10 +351,11 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts) attr.test.ctx_size_in = opts->ctx->sz; err = skel_sys_bpf(BPF_PROG_RUN, &attr, test_run_attr_sz); if (err < 0 || (int)attr.test.retval < 0) { - opts->errstr = "failed to execute loader prog"; if (err < 0) { + opts->errstr = "failed to execute loader prog"; set_err; } else { + opts->errstr = "error returned by loader prog"; err = (int)attr.test.retval; #ifndef __KERNEL__ errno = -err; From patchwork Wed Aug 28 11:11:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13781132 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 364B71714AE; Wed, 28 Aug 2024 11:12:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843563; cv=none; b=abWPKkI6q8Jq7O1Axoh97W2hCxli67UvBbcexnrK4Ygk7V8QPrKL1x1vb7/GuX1s8J7zVgfB/Lb0/x1QRxTS+UgcAtOfGUArtYD1nok4XqoiwE+lLk4PD4KhjipIHAuEfXeGz2SdiK3y/dv3b4YNQiarLsQ2fl/qWbUuFn551Kk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724843563; c=relaxed/simple; bh=Sf7g9q6kzZ2QDVZ42l9TEK1MrgR/netlPt93lP7g9x4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LEwQWP92pHyIu0qSMAQpvBAIwEiwyqtkMIScHYbr3RIqyf3+znUNGXDTglFiueKs+rcAmzFfamqFlyoEFLyYj8LIiTkoDExXlzfqt/gZvIJGCW2UmfsxxbzjXm+pzoxrwwDBAsugkPbjITbmJmriWsSJnGasP6Zdj77u8M4jIcw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ISSdS8Dz; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ISSdS8Dz" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-202089e57d8so3325455ad.0; Wed, 28 Aug 2024 04:12:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724843561; x=1725448361; 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=DRgUXWe8dZjfzs16xzJQ+DDVhbgl0JeJ2Y/m+gXLXrs=; b=ISSdS8DzI8110ozIu8QxgiWxgd0mTOEPnauxbOHT+4gRte8p5Yt6RIkuL7DJAyJhp4 SuR58+B+6rpeSM2EG22WtHrJ08JjNa9Mjae0oTmNtW/4y8Fzbal66ECUzunjrg2lZdS5 k8E9avWgAwWPObYU+38aSx28OmL4sM/M7KCTD9N/NL7ncO6IdcFC1vHMWRzOngvo9OTR LNTlT4JeH2IV+/KgnR98DqXMFc2teJsGvgO0+U8xPnWhhMupXT1db9MH+Z9CIpc03PjN gUwW/LfXSyRKJuwp4BK5j8+ZpILTbFVsp/cacRmv75bx4dyVYrKHSOmf83QQXTgvv9e5 34CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724843561; x=1725448361; 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=DRgUXWe8dZjfzs16xzJQ+DDVhbgl0JeJ2Y/m+gXLXrs=; b=iADBLC4nEnCLUBkDq/tImqQ9gT/kylD2Np4bT0ocLbqeT+C9+Sf8LmOQIuPAC3XoJT HRPnt5kDuIOLLlc5zmr78GJJFs6uPDuOeOKF2FQAjlEboXOAGLzSs4k5JNYNcDZh62nO InBQGoFPfN6CbL+MGBRQ8Ltk+z4Ubykyv89JoFTTtuFPwHg4/5PnRJYG/k3Ie1l+mpni /D3ATWst5qRI0+zQly69chdOV0PLmE07asWq1hP+cco1IGtEpCPAAnPGgpyz2yS+3uVz gSY2M6ch8P9Y5NWd7CxdH+u7BD6WeHc0yi3s5PuBk2xUwS15D0+GU7KqL9wljUQuKkny 7P3w== X-Forwarded-Encrypted: i=1; AJvYcCWqbU7cC0GPNeoUtiG4Dtgy/EhNUcA6VzrNYBsgDRgXKzUiXF9PdkAUgvUdbt3pQBQPwMIJaN5KjxY0QxLVpgU=@vger.kernel.org X-Gm-Message-State: AOJu0YzCJd9yasQeryRRLdOP7VMpcIsRFidb1l9HFL4+cAnd1+gC7yMu icHbznWWgoGvEIMUzrA0aWEYNrREgJy1ft2+NlVCe4oYwHZfMkw01CoKTrC2 X-Google-Smtp-Source: AGHT+IF17LvQdX0nlvFZv3KKEyPcd6PlsJDl18GzJgHep9e1plVKgnsJdA6JoC+HEGZBVhHkx/ONuw== X-Received: by 2002:a17:902:c94c:b0:1fd:6ca4:f987 with SMTP id d9443c01a7336-204f9bcea59mr31930845ad.15.1724843561136; Wed, 28 Aug 2024 04:12:41 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-203855678f9sm97369395ad.1.2024.08.28.04.12.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 04:12:40 -0700 (PDT) From: Tony Ambardar To: bpf@vger.kernel.org Cc: Tony Ambardar , linux-kselftest@vger.kernel.org, Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Ilya Leoshkevich , Quentin Monnet Subject: [PATCH bpf-next v3 8/8] selftests/bpf: Support cross-endian building Date: Wed, 28 Aug 2024 04:11:58 -0700 Message-Id: <524c7472b9c5b0a5f4f6f415975807a23b9679b9.1724843049.git.tony.ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Update Makefile build rules to compile BPF programs with target endianness rather than host byte-order. With recent changes, this allows building the full selftests/bpf suite hosted on x86_64 and targeting s390x or mips64eb for example. Signed-off-by: Tony Ambardar --- tools/testing/selftests/bpf/Makefile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index c120617b64ad..27afbfa9e831 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -438,6 +438,7 @@ endef IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E -