From patchwork Fri Aug 30 07:29:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13784432 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 E2BDB15F40D; Fri, 30 Aug 2024 07:29:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725002999; cv=none; b=m1VDK4nxyB0TPe2b/bmfc+4mkLwGNuwnmtdb8b6+xNnvmKOdimFsMXAr7MhMJL9NTIbylX8TAEcpaoE3XLE25PItWDfIYNz8ryFHzWkq1eksZcMEwr5fvUzwFGz/quVZPeXdLC0BNhyuGnTr/jTl4VyiQ6NBmYiLphdIX6cBztQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725002999; c=relaxed/simple; bh=/8JlYToOV5Gh2aJg+gM8mSMyGlhceXm0Nh59C6X4FrM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=edDl/VBO/P9cpSiEw3lAg5FIYv2dyN3ieli/0Kg9UFAzWaCh4QWEqGE6010etiADMKDBpz/chf3kBVq0++tY5fJuHAmfytygh61YpMUeqrZ+YDmIEJbWa5UpGmSXOw5GDltFb8s9oAR/7i0i15XOac0GXYGzJyqgrxXzia6eRD0= 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=C8gb1p06; arc=none smtp.client-ip=209.85.214.179 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="C8gb1p06" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2021a99af5eso12794285ad.1; Fri, 30 Aug 2024 00:29:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725002997; x=1725607797; 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=fFOnTSDoWpWkB6h5TfNEaXuZcI0mAABpdIpEU9t11Jw=; b=C8gb1p06nAl/LksNfA3QqjI8kd5NjmJc9sT51MRFMrqbZboY5FARwmFudvCHoC9GdK IomjN8Qd+JqAgr/D1EWT4xz9+lNBZoGVbV9KFfiLP/0HNAskw3GkQkwAJ5VLtCERYmdU QjbfAUKPOylw5Th1bRBjo50oxGnlg2GjWDG9flBeWUWU9LSkkNTTqWWAQpg1FrpkU+vl 3c09uHvYBD/hG4he57SEoTSi1RlxIt4hSyb1ngdusp/ThQFicHg/MxK6BNn266XuODBJ 2As0P5ceQdHjJ0QSdxlcTyFEW2z1wGoK5hX7WiuOMR6hJ0FlSjyRKjtWrjNG0126GVdq b3iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725002997; x=1725607797; 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=fFOnTSDoWpWkB6h5TfNEaXuZcI0mAABpdIpEU9t11Jw=; b=cUhSJOFf4jlHcj6uI7X647B5WbEjhXtck2yI/ACsPwRKjktBsJp7E7g51rVPvSugQl RFKUw6hSwM+sYACDX3Cis+HxP91rp09V/r9u+cFYhUTKOAt+b/qQ4Gf+DNWuDVOhqxBu ktePh945KFSZ4oXeL03kdpl+piBZt2ItPRmuZRYWtP7LSb9MwHv+yoXeKXS6yJkk1SQJ CoLGA/7hwjPGnKXgOoIipVNNl9/VEPsTZ+E63fLOTb7iQoOGY2w1FHaHPjrNMLFOetnq Y6qZYILBh6h5qjMlLvbVOy1DTtx/Ez0WqgMKqMiBHfS/Fvz6nQCR/L7rEblSRYgyuUiP bB1Q== X-Forwarded-Encrypted: i=1; AJvYcCXTr4JZM/yNjH6aEUxlkhYStWwC0I+EMNUSr0HmDnJQXBA2+hwNgSOUwrv7ezSeBA9ErFnDPhLIwV93YBogkUw=@vger.kernel.org X-Gm-Message-State: AOJu0Yzduk8CyMo77eYIlPVMQB7BR8zWpJ6xBgOP4czmYISfhRQDAs1U X0OUTMWBKJFH09q0pQsI8Px5peKzRGhm78624JuWKv1A7RgZaD/gh/ILNoDH X-Google-Smtp-Source: AGHT+IHdRDnNWiulS4EMKo4qcAn2TIU7lvMijeQh+WazHvpsp+DtRsDxiteBQW5kh0AJF0zyCjQWcw== X-Received: by 2002:a17:902:cec7:b0:1fa:abda:cf7b with SMTP id d9443c01a7336-2050c2068edmr70608875ad.9.1725002997031; Fri, 30 Aug 2024 00:29:57 -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-205152b4dd0sm21698085ad.15.2024.08.30.00.29.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 00:29:56 -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 v4 1/8] libbpf: Improve log message formatting Date: Fri, 30 Aug 2024 00:29:23 -0700 Message-Id: <0d337d7c8f78dc616fd68449d6bdf4e48fb67fa9.1724976539.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 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 d3a542649e6b..0226d3b50709 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -12755,7 +12755,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); } @@ -13829,7 +13829,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 Fri Aug 30 07:29:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13784433 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 C081E15F40D; Fri, 30 Aug 2024 07:30:00 +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=1725003002; cv=none; b=CzUhi9VjDVoGJfsrDYcH1BO2jCDAP/CWxxMc9HYy2AjL/fPxTlPf+mgOq68111hO0BYFwUFreLXcum5/U6A8sVCZ9IEgtv3eW+tdI38AOTarBE5VAFRuqIPj+sPn48KlfSbPd9pkjUGAcl2W5DM2lY+A1qu3i0173nfU/Xwp0zY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003002; c=relaxed/simple; bh=UJoIZbAwd0dcyPldGPlUH3SNsQcKTlS8LGIL6P+fhDs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BuCnwy6VIYbeRsdH6oF5uvCHrilbs7HK03uZ4y+tMY/C7OWHlNmMZDIISkTIF+DBw1Kb12syg6ev2sEaenZjMdH5uM+idqf9cFJAfm2n1NM7TIwnL0eMM3ONcwVDR60LqcYssDYXSWw750xiXAjSxFU86dlJ0hlWIRAeSLkGeeU= 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=a7vYhx/n; 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="a7vYhx/n" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2053525bd90so872585ad.0; Fri, 30 Aug 2024 00:30:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725003000; x=1725607800; 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=a7vYhx/ngRj5sExYjE1DHzE3ADfr5r8Gm/A27CxTM+XeAY4O7eJXH7hWJOoqJaf/2o EwqxybeChTAkCtqJVOa7jPBish6nbrouulgnOdrSFIn+tLvS9LSZQ/sI7e2CJiPV7u/K rMWWPQb99EhaYQwhyXQC+KhN8Dx4KZRPSjgYmOM5YYudOFf70eUquBreg5WixIk9kHAo I5vtmYzee2neMTVQnut6rPrVa+hu/iQD9wHl4179mkLjVNnB1ZdCwKQE+03UwNDs/iP+ HwiPC+VSOGLipZHixjSN4sKIgGbjwO/CED0Xxb0RMQ86fjDtd4Bdqxl/Eah2+eHrMnwr u8oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725003000; x=1725607800; 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=lBxql31IK7P84/YlMryvMeosGxLeSvrUl6KPTvihswwmxGPgPOAMNtyst2li4TOmah FwW7yUGlh9mWeOTsXpzxSiZ4xZ/5iFU52PaH0fzS93vkcrTj/2nQbc6+ErCpGz5WEG/V 9v4jeaXuSwcKrWEv0IhJYsAqS19SGWk13bAN8AnJb7Oujp/qBvSTsLezYAvw/i2U5skY iXXXJzU2z6mNW6GH4ybmix52d+4TUTOWsyHnRLAeWfZmMN0gSCBnvHsxKpSsLVY+MNoV 7/w3Ux8LP7X2XHQMcsjTO7GcqvA4JnNqKqVjtClLqB6vTtue5SCxRbRF5fNZp2yrVLDj sbbQ== X-Forwarded-Encrypted: i=1; AJvYcCX+qd4qgAxNUOhJoUt9To0lTTn6I5HU+GXIcWVnPFdyTJh2vdOk0TXCl6ZddAypZ+j7Iectq/NBjumMvSm5zfw=@vger.kernel.org X-Gm-Message-State: AOJu0YyQnxyvYpoX2aCvtHT4LoLTlDHgNKLb06rviVmeD34QPau5kDPi yNAtTiH6NMRunXUiAJvYRXqinoIJgMlL+HY6Zl+V2gFdT+07iiyjgSmxJtgj X-Google-Smtp-Source: AGHT+IFaw8cTmwpVNWzGzOfOyWf2b9H+orw8klmZc0zkB3oxfED/PsP49GM5HDFZbwThURgqnixSDA== X-Received: by 2002:a17:903:41d0:b0:202:4317:79c1 with SMTP id d9443c01a7336-2050c44cc9bmr50547695ad.37.1725002999873; Fri, 30 Aug 2024 00:29:59 -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-205152b4dd0sm21698085ad.15.2024.08.30.00.29.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 00:29:59 -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 v4 2/8] libbpf: Fix header comment typos for BTF.ext Date: Fri, 30 Aug 2024 00:29:24 -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 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 Fri Aug 30 07:29:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13784434 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 8E91C1607BB; Fri, 30 Aug 2024 07:30:03 +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=1725003004; cv=none; b=AzNQU+lUnjQFi9Vgz40hMMxkkEXH7zXhBiqZAI6ZW647JI0/owyM/Pjc7g7O17ipzjfr/vUSv5bi5JL9nDh8n1bmIZ5zQfaxXjVS7lTplC76aREu6R2GTcjoT64i0uL93nkNRrxmkeFIw5k6LNmTR6xdtOWtgU3U2D+dDdi9IJQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003004; c=relaxed/simple; bh=p+VcdB9HCA/6O6oQIVFwtXKCMYlajKRORAljsPWPaw4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XbSP8BZv8ptihCzwwOUoXq5g/fulflmZhPgNM7DKUNGomkvqeuio/CUGgNCrU/Ihtp3PNlA2ZD5mbT5MeqeHrUN820uLO3qv1e5vxNvJxSEb//QZGsbCYyFmrjMPiC/QCXH0UhCwnvfhxZY8SYbNsOammXB1RVQH71pfJoZuFbY= 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=E3CWSWa0; 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="E3CWSWa0" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-20227ba378eso13252255ad.0; Fri, 30 Aug 2024 00:30:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725003002; x=1725607802; 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=E3CWSWa0p3hALd/8ELtyoolhO5Otpizb46XB2wTA0hGe9v6dvyifhNK/cbSDEgoPQj juGQTqlwOua9kmOgh299NFm9bPfVc+v4qh7u4PL719NEFrMKgOfEM15O12NgxD3ZzJrT BEF6Iv7pg0QcsIp5P07Ajq6Xo2gGGo9/yVqYZZSiUXX8LYmYX1YK2VBUTe6NosPohN3j ptuJovfSU/lEm8N7eIej5W67jBxBeaBTITpy+00asr/ojB1Gcbht7aK+q4SDdyqRl8qv HdN/vDF1n2BTSYCUTf7Jwu8GB4l/8qXsfoN+KGsjNEzMjlfWT283KzbsuvQ7wZpYpd3P sApA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725003002; x=1725607802; 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=pGPjQoPg/U0bCtFcdPmTewaaGXrrZe16nUqUrtpRNbdiW7ech2kgIg3uVz0Y7zcwu1 2EOM/fDfiA8Gi4upbTuXpCJPFIVtWQTzvB3R/GrsNA3E0SiJsCHcPbQ62ejHR4PRtMai iGWQgCNsb0NmDKo0pPUX1EJ9GLzC8TvEN+ZG4f+Dju+WhWy091pMGjDcHSS8Ow6tSVcp auFUmqrGm00XrMJnyMvPxYEQh47iAAqvGSjJqyQr9G4otyLLAxQwjg7IS+IuX8Lo9vu8 WqDx/A2j2nLOUpCib190y0eT8J08oiYiMRGFXNj6NAxvYIVZFPgp3Juy4gpQSfsL+oHk OGFQ== X-Forwarded-Encrypted: i=1; AJvYcCXgk784W97QdB2ePO1OiBxPcAtgbpSAlVbtGD/onPM8MvCZWtIz1xDEO+kGmlihkrJHhyF5yZsIckZbj3zdhL8=@vger.kernel.org X-Gm-Message-State: AOJu0YzZGIp1OVRgTrjuEoYJqqiuv146Rc/sb4Ddv+guLMAPybooSmtf tkBmNkHuvXV5/vEoNpbq+5GbaEu+JXQcZn6YvJzuVW3/KjQumtPln5aRYHnw X-Google-Smtp-Source: AGHT+IFEUAuQssJkebXVGRRWusj9yjVzkcpoGUHA24SHz0QnN7F3fvrbb/FvnqH6OiwavNFDoWYqNA== X-Received: by 2002:a17:902:ea0f:b0:203:bbc9:2b93 with SMTP id d9443c01a7336-2050c2060ddmr53824545ad.1.1725003002426; Fri, 30 Aug 2024 00:30:02 -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-205152b4dd0sm21698085ad.15.2024.08.30.00.30.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 00:30:02 -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 v4 3/8] libbpf: Fix output .symtab byte-order during linking Date: Fri, 30 Aug 2024 00:29:25 -0700 Message-Id: <5672557ebf511ff2231853fa306a36d280313165.1724976539.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 Fri Aug 30 07:29:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13784435 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 CAAF61531D8; Fri, 30 Aug 2024 07:30:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003008; cv=none; b=nT4qtJCOSFh7uBk6JkOCXZpHjZNPTjKVZax5iN1Cb5xGBudti84FL+8tOa94sEqMmQNJuk1KBER8EqfRZoVf7YQJ1qfH9e5xwymdNB/bmi1LEvIsGlKGIP6DfUk8zoPOJdvubJpdJFZIlpsfTRSOLujPWXovJxxmO8lzhKC9kXk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003008; c=relaxed/simple; bh=LkrBafs0NAlbpfTuD47AlJgJMZSzJnYvog6iNcs9XIo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Hknea0gGHJxrlXz3UJMbhHwTz77Hr9Q2fC6l5/XA7gYUKatekuNyJzVTiQCz3a/IRgJfZ1aN70Jo8GzFznW0Wuo1sb4pgF3XSeemFsDF7Mzkt2Fyrws9H/QHNGaxOKUU0oDtAL8OjthI74yZT70wfw2cqIk2ecLEjdQp4hhJslw= 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=QMK52TJn; arc=none smtp.client-ip=209.85.214.178 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="QMK52TJn" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-204d391f53bso11966595ad.2; Fri, 30 Aug 2024 00:30:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725003006; x=1725607806; 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=vDhNeLRGMCGXFgaiAMyGz0HhiL4NmL5mHyzaYQ5Npec=; b=QMK52TJn9PeZSIwG8+m+hrasMIJt26J1xsgZ0jXia9fv/Guq0nMs2tznPGFygq+mJY e7LflrilYyv50Ee7PLB6e5VFArcJbJvBzJQdGcyG/5YJUCHX1VkP3w5eRHy9Vlll4rn3 cyA8o2md/zc8t8fkWf2pB++Xicg2zQb0ylXdUsZZ3Q+bIRyrVA2Mnx/ZXQhoja3iT3Tg 985rAPXa3QlHtb/Zm4xodl13h3jppmNx0Lsw33fckJR0JFk+s+v4/CifM+119JIEJsnP j+VIJlL0cZF5RB3DxrtDduoS3y5+/pkcnWu90GKSo3u6iWNFVnYxDF1iV6dAKUyW4bJ7 98CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725003006; x=1725607806; 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=vDhNeLRGMCGXFgaiAMyGz0HhiL4NmL5mHyzaYQ5Npec=; b=abiI6sPC84Q7zu5SKVPRsNa15nAXkSGFz0pf5jQ3wDz5faEy4U/yuYVgyk+6X4Y+hS +VvXLf3wyRECU/elGEurGjF0XcDL/MA0Otxu6gDTE6Sy7txaPvuqFOlbMOivBOzzfGKT U715bfELy64SX08UroM2M3MMyCt+kMqSpI33EJxMazfyZJQ6wnSwVhs2juhPtBMDF1V5 OQpglOFKx6kbKac6IF0p1wH/MiIDR0aIEE403QV6NiUv5cEaMRTsJAVCVIFMttn983wR SGU/oCz+VSx1IRwGzrdMLkYETvwY5Lw8gzbHzMSdF+g1DquimOEJ53gK+TM3SzDclsL9 U2qw== X-Forwarded-Encrypted: i=1; AJvYcCUdOnUNwNe2qDKbgdHscDqU1rItHxlGxCrVdQD4AoMeg+whXjVnlmYFd6bpN3YEQhVGDxrNwra7bBrfBRfwQLY=@vger.kernel.org X-Gm-Message-State: AOJu0YwzSTq+cauus8080GBp8ythaIz+vm0zw3fy278ohn90uVOI+yqd uzje5l39mFhRWlfjhSJHuGvzcgiwSOzm8BgqYo5AOTRWPClZ5g435b9AoPfc X-Google-Smtp-Source: AGHT+IE8AXEwfirYXZhteZv/yl1lGSPAi+7whaciuszhDey8BHMBLPY5VAWsh/R9c1PbAuXkAtVZMA== X-Received: by 2002:a17:902:d2cc:b0:1fd:6677:69b4 with SMTP id d9443c01a7336-2050c4a666bmr60117675ad.49.1725003005742; Fri, 30 Aug 2024 00:30:05 -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-205152b4dd0sm21698085ad.15.2024.08.30.00.30.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 00:30:05 -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 v4 4/8] libbpf: Support BTF.ext loading and output in either endianness Date: Fri, 30 Aug 2024 00:29:26 -0700 Message-Id: <8eaba4b675cba9035121121bba6618c9f8f65610.1724976539.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 internal header functions for byte-swapping func, line, and core info records. 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 | 192 +++++++++++++++++++++++++++++--- tools/lib/bpf/btf.h | 3 + tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/libbpf_internal.h | 33 ++++++ 4 files changed, 214 insertions(+), 16 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index f5081de86ee0..064cfe126c09 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -3022,25 +3022,102 @@ 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); +} + +/* 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, anon_info_bswap_fn_t bswap) +{ + __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 += bswap(p); + si = p; + left -= rec_size * num_info; + } +} + +/* + * Swap endianness of the whole 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, + (anon_info_bswap_fn_t)bpf_func_info_bswap); + + /* Swap line_info subsection byte-order */ + info_subsec_bswap(hdr, native, hdr->line_info_off, hdr->line_info_len, + (anon_info_bswap_fn_t)bpf_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, + (anon_info_bswap_fn_t)bpf_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 +3127,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 +3174,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 +3195,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 +3223,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..81d375015c2b 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; @@ -511,6 +513,37 @@ struct bpf_line_info_min { __u32 line_col; }; +/* Functions/typedef to help byte-swap info records, returning their size */ + +typedef int (*anon_info_bswap_fn_t)(void *); + +static inline int bpf_func_info_bswap(struct bpf_func_info *i) +{ + i->insn_off = bswap_32(i->insn_off); + i->type_id = bswap_32(i->type_id); + return sizeof(*i); +} + +static inline int bpf_line_info_bswap(struct bpf_line_info *i) +{ + i->insn_off = bswap_32(i->insn_off); + i->file_name_off = bswap_32(i->file_name_off); + i->line_off = bswap_32(i->line_off); + i->line_col = bswap_32(i->line_col); + return sizeof(*i); +} + +static inline int bpf_core_relo_bswap(struct bpf_core_relo *i) +{ + _Static_assert(sizeof(i->kind) == sizeof(__u32), + "enum bpf_core_relo_kind is not 32-bit\n"); + i->insn_off = bswap_32(i->insn_off); + i->type_id = bswap_32(i->type_id); + i->access_str_off = bswap_32(i->access_str_off); + i->kind = bswap_32(i->kind); + return sizeof(*i); +} + enum btf_field_iter_kind { BTF_FIELD_ITER_IDS, BTF_FIELD_ITER_STRS, From patchwork Fri Aug 30 07:29:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13784436 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (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 DB8541531D8; Fri, 30 Aug 2024 07:30:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003011; cv=none; b=SCeho3jptXTR60Rbi5PLUwyXTFDHegp8EN30XUtj/UnxmmIarBqMAe30cTHD1jPygYRmJ1CAGQfoRxzS6IjQfc7g1DJrPv26iweNfhkcsCf7/wh8mgTscs/l9c2MCEIexSYpaTUK3j86bQEAmyeD9jukAdkfYto3n3UP6ce1HUE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003011; c=relaxed/simple; bh=zMHgBV9MPoayBW5BxuZkI8lBHTKi/MZZKDBmxSKOIBE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oWD+QZIul4UeI9QHliAiz04++NYj4D7t7hVU42Wp6KZlcbj8o7URKZx7GQP7zuXPJgoZAbRZqpn4zaecRUT12tRyile2zjXNQK4XKasBUq0VoSbK3lVdQ6jJB0WqhnPdbBdTCfdESAf++IPUrRWrOHDS/Ij524supk9BwRJ2nvE= 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=AqWjQWKz; arc=none smtp.client-ip=209.85.215.178 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="AqWjQWKz" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-7cd8803fe0aso951750a12.0; Fri, 30 Aug 2024 00:30:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725003009; x=1725607809; 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=SSPserhYdnWbrZnczBTRe1/uEXl/4VByG9Mp4IeCUR4=; b=AqWjQWKzIsWQFeOA7oIXLyucc4m6w74KHLfHTc82MifdGXv7+PT1JuU+Gnt1i/PPt8 I9Jj2vggEfHlGHKMFkULmmVZqh6nVeFA+lrGEj1YV+55M9YXog2t5ClcAQY9pwdHfIQN kOc5oOzen1s8Fnz6nB4AmYd/ewzTUx0ZYALZ+N8fKU5kLos+2pVH80sXBatWXfoJN1jn m3WjymG7n0RN0TPMB8v6iO9RAwk3VIO0LAXQbMEsYb/1x9SKrtELHQTna1o0AD6NUtyn D91irU6ZARvTfKBvQogoZguh7QRNlDzsY/zxwMCkbFJhWmrs0CJxjwrji0dLSFujbYXk z3Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725003009; x=1725607809; 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=SSPserhYdnWbrZnczBTRe1/uEXl/4VByG9Mp4IeCUR4=; b=uul4Ncjgb3DQz2Yk4F8dybCjm6jJOj4I5BTiLX325nfMX9Hq+zGUMWkjTU7U9EY1FS PQ10w4DwKXeFNYyD2dA3EdiBp1PDuBVekudXS+l++7e0bn5Es9A/hj2f7Pumeb6oQP77 eUo7Rw1ZKu3/6JbxpEqeOGNJYFAY5HEuQxXkQIdBcru2L8ioc/GowrbC+dQ28p0xP2Cx 8rFdze5Vdh1Lxdj8fUeF7mkVu7XbuTl7ZzPlgZPd9sfvC5iroaS4nReXJn93XKpfGqAk 3A3BdVVX/FYGUS4jju+08XrcG27Unj77ljqrobxj5h6EA2suuGCzl0ICML9MWW6NoIOR kTLw== X-Forwarded-Encrypted: i=1; AJvYcCVp/bG1B5M636S4RfgTGYC2q1Kbv02RIaTqbKHLzyOPJkjIemG7uPz6a+4DHhfewcPFwwnMcojDP7trES4ZbPE=@vger.kernel.org X-Gm-Message-State: AOJu0YzQpcTYrXMf8FSG/BT4h5ifYs8s/BtdTnYDribK1JtreCroDuqm /LhDenmmgNn0AIe3+lXeDDcMZbT7ygbqTN687n08HIkW0EcQhpVyjayjav71 X-Google-Smtp-Source: AGHT+IE/vqY9Fi+mQD9ljYei5IS/FqNpda2aH4Wm4fe6ZJQnD42YS3bYOXxX3veSKUzG87nGitgMrw== X-Received: by 2002:a05:6a21:e85:b0:1c6:fb07:381e with SMTP id adf61e73a8af0-1cce10ed59fmr5125445637.44.1725003008817; Fri, 30 Aug 2024 00:30:08 -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-205152b4dd0sm21698085ad.15.2024.08.30.00.30.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 00:30:08 -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 v4 5/8] libbpf: Support opening bpf objects of either endianness Date: Fri, 30 Aug 2024 00:29:27 -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 | 49 +++++++++++++++++++++++++++------ tools/lib/bpf/libbpf_internal.h | 11 ++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 0226d3b50709..aa52870b1967 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -694,6 +694,8 @@ struct bpf_object { /* Information when doing ELF related work. Only valid if efile.elf is not NULL */ struct elf_state efile; + unsigned char byteorder; + struct btf *btf; struct btf_ext *btf_ext; @@ -940,6 +942,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) { @@ -1506,6 +1523,7 @@ static void bpf_object__elf_finish(struct bpf_object *obj) elf_end(obj->efile.elf); obj->efile.elf = NULL; + obj->efile.ehdr = NULL; obj->efile.symbols = NULL; obj->efile.arena_data = NULL; @@ -1571,6 +1589,18 @@ static int bpf_object__elf_init(struct bpf_object *obj) goto errout; } + /* Validate ELF object endianness... */ + if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB && + ehdr->e_ident[EI_DATA] != ELFDATA2MSB) { + err = -LIBBPF_ERRNO__ENDIAN; + pr_warn("elf: '%s' has unknown byte order\n", obj->path); + goto errout; + } + /* and preserve outside lifetime of bpf_object_open() */ + obj->byteorder = ehdr->e_ident[EI_DATA]; + + + if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) { pr_warn("elf: failed to get section names section index for %s: %s\n", obj->path, elf_errmsg(-1)); @@ -1599,19 +1629,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->byteorder == ELFDATA2LSB; #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB) - return 0; + return obj->byteorder == 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 +3979,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 */ @@ -7992,7 +8022,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); @@ -8500,6 +8529,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 81d375015c2b..f32e3e8378a5 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 @@ -621,6 +622,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 Fri Aug 30 07:29:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13784437 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 247651607BB; Fri, 30 Aug 2024 07:30:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003014; cv=none; b=QzHDk0QX6kvPcdbYd8zsEyygnCcGFekiiD0TJzFlWCzqnMp7423e8tK30EdTQAnWIpvfPHhqmnM7qen9VdLrLhkMni7Y1PNdVripfqm2Jwxgknl3bAN9QLTPNQ4KCUy4f/yeK34nvHHuRH81MpZW99pKvf5LFO4f5uiV1dH+uF0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003014; c=relaxed/simple; bh=bZFiVLxsPqJFp+7OFMaqX0ZvCLgCDYIYtrLGMsK2oz0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Qkm9QM13tyNacxL0nBshtGxOfJAdh9fXl/7cS0HSquXffu2tTxGCIm1ZIVZO3aBJARiZmmzpNd8ozNPFM1kyecaTiYjtdxYpKuseLc5/1eRVnt0FUtrPYCeinMHHfrUp4MTQx2cp/uJO2l3c9A4b7LnVJ2QMlBIK38ulgqJQJNg= 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=IwlWw4Ve; arc=none smtp.client-ip=209.85.214.179 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="IwlWw4Ve" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-20231aa8908so11557615ad.0; Fri, 30 Aug 2024 00:30:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725003012; x=1725607812; 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=IwlWw4Ve6JZmBni/iiuimdFP+P/QCLGMTI5BbpcDp1KjznoN8uGuZjVw8BpIqTALIT VjuNYXwQPNqJOmz0MJSEarzQqd+wQqB4ZLGD9rs4fXBdSBGbENmB3RHV+ro/AAMossRU DLrOY4HITuDz+nnL68dKM+325sgrK9rKljxkdh2m2+YkrGQOEgZHuxoO0/f1GOqnHq4m l+KGnvp0sJzn9HzdE0f4xmPXAq02m9yxuHgQVCXV2+ZZT4UgrmzNv7pypiC/LGnqfA8l mpIfny7WVFxpETKu30ulrlvBBEhVnsmhKKe3opHyAJPfmZJf+feowCK1UWgCw/2N9hIU ULXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725003012; x=1725607812; 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=NjF4EYM/ADhcxWc5pO8caOMFypcyatsgJ21goZwAvYW6idQKbd0EIiLHUr9dn4UDcH b+YqAwAw9POP1oN5rOIKMT1cAitaEGq08ZosAP/AwRucLrWSF2YUFy7pAJ7aIhGaJ0QY iiM3+2pn5lV0+qjPmgFVWfNNEwaHaRKjWu+VdNveh/razKtVlSSXv6OiL3CLgDwnToUT oGZ8pBijK98AuJXmVul9slY+HGkLSVuP9gwULoLReDvJ/yqiR5ttlTixmn3r293h3Iy9 5JMTmSGhV2fZUX6BbUPDvfbwnTm96//PBbnYlRWb3u1vrxrl0ekFvoHzfBEFLtR/B9za 7GRQ== X-Forwarded-Encrypted: i=1; AJvYcCVnFPJLi3dhQuITYU7XW/Et8dDH5X4XZrLM0IpPyaCi/eIQr7q+/EkYiIirPvo+CjvosvKlp4BK40HMJQBAnIg=@vger.kernel.org X-Gm-Message-State: AOJu0YzMxlmsSMBTjkJzr4I9lfYOb6YEQn0k62hAU0yV8lO1+24aM8Fv RK/iGuzd5RAsf5NfSwxTfYz1nv+Qi0gEXApcfDJIVQbKVtQyVaNvocQ5eZsE X-Google-Smtp-Source: AGHT+IEs9SU9D0i7I40ChktQADK5+wH4dZhhKyxvKfHl0U8D/im4JnxT8BFD2i78X9laiER7tuLdYg== X-Received: by 2002:a17:903:22c6:b0:1fd:d3db:bd06 with SMTP id d9443c01a7336-2050c34f1fcmr61723295ad.2.1725003012258; Fri, 30 Aug 2024 00:30:12 -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-205152b4dd0sm21698085ad.15.2024.08.30.00.30.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 00:30:12 -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 v4 6/8] libbpf: Support linking bpf objects of either endianness Date: Fri, 30 Aug 2024 00:29:28 -0700 Message-Id: <895753fe6de5a06dee8ac110f0c05571db73e70c.1724976539.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 Fri Aug 30 07:29:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13784438 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 622F8161936; Fri, 30 Aug 2024 07:30:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003019; cv=none; b=BThcqiD8r+eMYnydVW5XmsdAubEY8F6ez/f8hv7/zY7PDwB50B0vaAAKkZB3LT3KKRbr78PE8Cy00hq7emPtTcgC+8MijcYP1n38sgUjcUAngUlPCJxI8SA8gGNZUZ+COdje3qybYEQ+a6dfrnYtKZhSfWap9v6K28275kETbj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003019; c=relaxed/simple; bh=bllL6HbrPJrBJ3ekqFYiec8NPhvkjhjSi3M3fqI9cKU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qOhjs6IESzjlTvssAvxXNV5x7XokVYGS+YYzryTjh4bcGdQh5ij5prhHcrmSIclAVT3LwYmOsnwCszL+a+YHQ7sCkp4UPL+HPPL5yCp0BaermaZv3MJM0BQJarfzbaTzsYCEq2IbTXkDcZQ6AShZZ/4qluob28tHeem6Q6IMWPo= 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=luBe6tE2; arc=none smtp.client-ip=209.85.214.174 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="luBe6tE2" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-20536dcc6e9so622815ad.2; Fri, 30 Aug 2024 00:30:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725003016; x=1725607816; 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=UNDOETSufq6s24pi79G/IZZhrbFmySr5/NgHhHwtjTs=; b=luBe6tE2/E+vx+A3AePuGOICbZ2RvMEPDaLWreAJhZZzh/kfg2zzH4FYpK33lXfE5i r8XzUCJWT0jSXBrad9ekMkpYuNn9D3GR+3Gn/KWU8x32LSrKpH3q3vKRxE8/4EN19Igd Rp28SxRY/QWFuFqqSedF+6oFGW99pziPWRQs8Ei9PY5gNufRI2yxm+jdWCx6ZHXR2i4J 3yMhUcJ9LddZek3y6hxwzKNuWrph2wWXsFt0zFrYT0g6ry787uM53HfAegSVh0fPQxSg lDgmazA437aUELAxaZC1Zi6k7H/TFX83MkRxplKO1Lq9R1CzUkRgC4mLU41yZd6Cv9KO 12ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725003016; x=1725607816; 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=UNDOETSufq6s24pi79G/IZZhrbFmySr5/NgHhHwtjTs=; b=ptyHOxcjWp9GHK6+OIfdrXrkDOVvqb1Y6z7n7XAWE0TSXWF8v6W/Jq1wCmE2UEdCce 9Xr2bwAj2Eqht/gs08QXhpg8JBB3YPm7/j5dJeK3M0Yef/DwggXCYk33D9vsBX4H1oQo r4iDarXM8q0sMez2bUoO/8qLOs0q5xPBC/AFvpJvazG/mc2s61QbGy9tUlIyCgFNXEvl peBPeyozoi7xszGgML6Oe0MmUUUOyiTEzYjjYMrbeU6E0yiTKdWILZJBAwWQqrYHBq3T /iXIIz1PO5ZoW929EDajqmZC9UL9RhQHnxAxb+q7QViE/abb6Yhb5z2ksN1KsJKk765u swnA== X-Forwarded-Encrypted: i=1; AJvYcCW2GgqPBuyclHxFHO2axgLwNyGNEUQ/tZQ2I4yTJJWuUichOr/kmUX9usiqCHOFrWDObMVmJ4Xxmr3PJDBMLdM=@vger.kernel.org X-Gm-Message-State: AOJu0Yx696Ym+zKdA9WjO7f+KKM0brgQv/IcKAZpbshXwuqiqpldcNYt oNIPTFkrMrBIc+ns0Rp4cbH+8B7cdILv+aWC4o1WEEotq9r3XpMI2C+oWuoV X-Google-Smtp-Source: AGHT+IEETZxfjkrRBnLyzySToXL7aURI9JrcZvASMgpZSfjFy8ba/79H9i70Y/x1uDteFQMFq0WcAw== X-Received: by 2002:a17:902:cf12:b0:202:4060:22cf with SMTP id d9443c01a7336-2050c215a14mr56334135ad.10.1725003016294; Fri, 30 Aug 2024 00:30:16 -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-205152b4dd0sm21698085ad.15.2024.08.30.00.30.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 00:30:16 -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 v4 7/8] libbpf: Support creating light skeleton of either endianness Date: Fri, 30 Aug 2024 00:29:29 -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 | 187 +++++++++++++++++++++++-------- tools/lib/bpf/libbpf.c | 1 + tools/lib/bpf/skel_internal.h | 3 +- 4 files changed, 147 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..4374399bc3f8 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,33 @@ static void cleanup_relos(struct bpf_gen *gen, int insns) cleanup_core_relo(gen); } +/* Convert func, line, or 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, int info_cnt, + anon_info_bswap_fn_t bswap) +{ + void *p = gen->data_start + info_off; + int i; + + if (!gen->swapped_endian) + return; + + for (i = 0; i < info_cnt; i++) + p += bswap(p); +} + 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 +1007,63 @@ 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 func_info blob to target endianness */ + info_blob_bswap(gen, func_info, load_attr->func_info_cnt, + (anon_info_bswap_fn_t)bpf_func_info_bswap); + + 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 line_info blob to target endianness */ + info_blob_bswap(gen, line_info, load_attr->line_info_cnt, + (anon_info_bswap_fn_t)bpf_line_info_bswap); + + 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 core_relo info blob to target endianness */ + info_blob_bswap(gen, core_relos, gen->core_relo_cnt, + (anon_info_bswap_fn_t)bpf_core_relo_bswap); 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 +1159,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 +1177,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 +1211,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 aa52870b1967..1ba73c27973c 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9124,6 +9124,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 Fri Aug 30 07:29:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13784439 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 4CC0616BE0C; Fri, 30 Aug 2024 07:30:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003024; cv=none; b=q3l/MZcAzG2bow9vAH6LX04U83fquSUaaTEVHdde1eBLJ2gdoNZaNtaq995r8o8Ag4P74zkjxR4pBxroADqlUVuxZnEcQW/Z+WRmtGtJ+rgsh/cmXc//LRzrDul0I8zQi4nIunvn5b2QXCLHXEmFUWWDd4vlV61qfP1KC6VGrkU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725003024; c=relaxed/simple; bh=Sf7g9q6kzZ2QDVZ42l9TEK1MrgR/netlPt93lP7g9x4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qvu5kMo2Owsez961UyXU8cjowkfWFoajArp6mx2HKmtAwHkuitIQUZkwfgw7F840umaCYZVz6/nl9dgtT5sm7eRuI3c37QTduGxtFjT/iOK2PnvusVDKHC74cc2hK7cV0hAqkZMa1PoERMj4oGUDcbPoqX1VVwgh4hR7IpmVT5o= 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=D/+8cHw/; arc=none smtp.client-ip=209.85.214.182 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="D/+8cHw/" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-201f2b7fe0dso12818905ad.1; Fri, 30 Aug 2024 00:30:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725003022; x=1725607822; 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=D/+8cHw/6SMPg0vXP2K+PVjCQpvUBYFwPaA8ioQSgBd+MCK/eYjpdbdrudd1nazRKt S77ipLRYWnEPgDZtNLGzU5k9AbHDc59sg+ieml6ohJ8Q9MkfuD9a3CpJ3XJA+bRbU3Qo FPGrcCYBKQiHJkgZ96a54plk2gfnyWKPcTluuBzwV1YTTZisKfyshvaTA6EmVj1GHIA+ uQALOhcnhRfpnXbTc1/0bjaE6sE9otd/4nXqPpJP9kpHwwndxvWiNbVKFV0buN7oRBfy tC1KMPnyj4KnEO5OcOp9F+jFOgaOfD9pkubMa9V/kRVbMiUV3VxbalOiXQaI1MN+Vt1X GvEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725003022; x=1725607822; 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=Mbra98jErVwR7ippTWo7HOcg8rG6WsbR1LHFbs7NFsADg2+h9UpqqZT2HUyaNhaBPE 6x80uv9ALziWsr+ZO93JuOibVIergd7kIDzyADjEl8Z+Sctdg5DR7Na5vjnX4rAqpH6D LxxXOph2u9TGKOzJoYvHoS0Aje68syOSvbqRHl6TgWhKuVqdjdq4vSnX+OpHP5sqU87w 0lB7QsmwEMhrhZTdWYY5TiRPq8lxIqh6RvoccJH7gdUaKeRvpIcZnpSthAp4rQfxHkAf IkINAxQWRu+W3oW7oQ5khUrgjDb1NQxl4WRoS65GZ+0OCrsXQj9J8vIVpuCtXTDSHocy 5KGg== X-Forwarded-Encrypted: i=1; AJvYcCWzJSLLLPlRvzO/QyrH3O16Nb0Pjv02tGyKoLtnvtOgKxajS9qIrxR3atAOSMP/m98wER3+WUJFVYHV1Zpzbsw=@vger.kernel.org X-Gm-Message-State: AOJu0YzYQzeYAEnMTh4YsMENvy1ug1xVV8BkGGT67Uiw1okEHSuGcfLe U3XDkKhTL6KM4Vryiye4isWwF1DcktZwD/N5gabDDDrH/C3UgETRoQ9VKRFW X-Google-Smtp-Source: AGHT+IF8A+tzbxB2dPzmuiLZQVlvRGJkJrjG6ut3uVXWa1gSLUlWERkW2TZfKNDo8kF7/P8Ei5N+yw== X-Received: by 2002:a17:903:2444:b0:202:562c:f3cd with SMTP id d9443c01a7336-2050c4f4331mr54598785ad.56.1725003022188; Fri, 30 Aug 2024 00:30: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-205152b4dd0sm21698085ad.15.2024.08.30.00.30.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 00:30:22 -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 v4 8/8] selftests/bpf: Support cross-endian building Date: Fri, 30 Aug 2024 00:29:30 -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 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 -