From patchwork Wed Aug 21 09:09:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13771110 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 4975D1A284A; Wed, 21 Aug 2024 09:09:51 +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=1724231392; cv=none; b=kiH4Lsipb3F4hq8+ZRtupOw4qZhXB7kdtrxZXNjuon31R1OBmHYH4BYGP2JEreNfCPIcTimXp1JDmvnLiMJ1xr+CB5k12eX9SNFkzRBiWIUhEamgc+iU5GBfiFN9lfPZ27PcqaOvvjGLsvm9wK0FwqYJccl0Tl2sNdP8Uid9v+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724231392; c=relaxed/simple; bh=RULpRj+x6xiQ9FxrwCrODXVeFnHaWP7jw8+W86HvXwA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OsL5jeauKTsRmA8FAdomjJxkVn84M5Svvus4hSJPgpbIdVbsTojk6QfobyQkkZIDrTdFZ3kbhYqwNjComhN+oTxWOqVTH/+h2fsoEjrYbH9dtwGblDIYOXaIyN2gedM/yz4Np8NGuTw15/tTRoGNsD6qKtl0pH9tokLv05+xiaw= 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=UwRQy2WX; 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="UwRQy2WX" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1fee6435a34so42663735ad.0; Wed, 21 Aug 2024 02:09:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724231390; x=1724836190; 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=bg+SydAic1DN9KXNLTqh5Rc9lnjAu61/0BbcjkZohkI=; b=UwRQy2WX/hg40klj5nTHUT6MrQQEuTP8hxVsPPkrkve+Q0XlFm/NAti8jUSTA0R7oE 0hS0l4b2RSflgt3E5zpRTNekSaS+QKFATFvOONWpUYOlzaFOLE9myQKMLb3/TKydop/4 QoN7imqPuppOfw+tN+sgpMUkFXmexplVg8A8ViWZO6h5gaDI30nKiE1AjhLBOLYzTeEc sB1Z4xfscU6OZTHHnr6QnEQ8c8pNMF4MmDfUbe0fwEBotlBod3VFQIn8/0G9SZeMBWZu zEIDIEGfcDhsUrogQMzt2N2E2Gj6c+rztG4clHpO8rrTxNHQpqOWtmJuKlvxgGBrFHlO LC3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724231390; x=1724836190; 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=bg+SydAic1DN9KXNLTqh5Rc9lnjAu61/0BbcjkZohkI=; b=I/2giCDyXaf/h0WdRQmwsRG+9RvHuUthSsqITevJu92BP8Dk+TV1zuMVOqKiurA2Hc ZqIXcg0BLnOnr3MnQK+tuvgJ56JrtTLlHFEA1S9q3d6S8ylMQ2on91mZkoyRNNuqMLRd 3VUxi+GDpjCA6lzqUKULj1QTIoKS3lRC6+u1d/uoOT3yhdUeHyA/dLOdXkA5fe7tp0Ah mB9QmFEvLpue8NXTPiBKHdLxDri9zWB6CNx2SNWqfSollW6LSh7CWUQQ6Z8iXz+urNx+ BWLwhDAhZhLWVWWn3PMMkAhiWlNR/8rCkZYDvwnQE0EDi1qcQPhljaSUU5KAIOLaezQM CRIA== X-Forwarded-Encrypted: i=1; AJvYcCXExVr/vxlnd+g0hD3HzQbWnMJ0cHyfavgdW6GZQL9o9wydOGFWCznmn0wBWno2zAVwk1vkdmbhJ5B/Qnue6xs=@vger.kernel.org X-Gm-Message-State: AOJu0YzcFJEEkaVac0AGfDaIjhyDXhisHaWzqsBtT2c4POTr3mvI4Kki PjxIXfYPulGngxwgxYGjGi6aRzyfsB1SmD9Bqrds/KJONkNeDoln2uQDkzwF X-Google-Smtp-Source: AGHT+IEw6lwbnf2ZWiyTT68HHAWnAM89KsUZV+OpNNv1NIE/IsiaF4luDJlh44nmg/uDuGf6DkE/NQ== X-Received: by 2002:a17:903:41c6:b0:202:3296:eec1 with SMTP id d9443c01a7336-20368199110mr17054885ad.36.1724231390237; Wed, 21 Aug 2024 02:09:50 -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-201f031c5e1sm90191405ad.94.2024.08.21.02.09.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 02:09:49 -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 Subject: [PATCH bpf-next v1 1/8] libbpf: Improve log message formatting Date: Wed, 21 Aug 2024 02:09:19 -0700 Message-Id: <91de76f9924a8fad2273532e8ed2536656213f68.1724216108.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..25e7c44d9f95 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 21 09:09:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13771111 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 C76471CBE9C; Wed, 21 Aug 2024 09:09:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724231397; cv=none; b=odqAItvZ9fnPhOy8awwABgxHVWRgEH5v/0v8/n7OIJs3p3c6VhRE2EO7yPUHlSzPANNxpm9DhnkpIGEB49P1LgDMeETerxcCbIyCqxoAPt/nEhMGsxdf9LKr0ENLFsdSQqR9002jnLkcBvWHDwykMF0ObYhyorgVYeUDpAiBDfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724231397; c=relaxed/simple; bh=UJoIZbAwd0dcyPldGPlUH3SNsQcKTlS8LGIL6P+fhDs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=haylikR3XNpmCzxGe75DZbOlISWYB3EGpwldpnaGUS64IonbbviL4Vl752wLeWhgoDuqPB6yITq4/z0+yl4K8+6lWSUIv+3FxMJDqCMrPKM5BxgPYCHpYcSCQSZywRuUGC7q0AfJb/wNRvdf6/4BSMF4TOUwFRJ/TP58GlajPFY= 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=j7DIE1N7; arc=none smtp.client-ip=209.85.214.170 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="j7DIE1N7" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-20219a0fe4dso35631385ad.2; Wed, 21 Aug 2024 02:09:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724231395; x=1724836195; 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=j7DIE1N7PlT9s+b0c+OfkUubiMaXEEgIHjgROoNgISsYCz+PexGjKeycaEzFXZjeXn JB4j0OCP+6ptsipuD3lMmtvxOuYC6UjUvFCVPEUz+/oG92fY0jQMH2UMf2f3oVR62vL5 3heS5Pj9YhQizcUX3y97Jf4z3UeyiTC+94bCZDq3dd2CHjo17ko3+A20zoC3y/i6XOjB rBMlRiXC1kg02AG7bdyBO5Cus1fYAaDGxSTCZHk+vK2R3GVViVatf8mKbzKxnuy0D8Fj R45JcL4nmIH0G7fVsRfuK8tL7oYsg6urcfUNJoG+qw1OggQuDArVn1XCFDtFvktdP8MI xtyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724231395; x=1724836195; 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=YOKhv57oI+NSDkkAsqpog3CbIceiscTIMZ4GmlctAEsh9OQK4sX2aNHuxL8EIaCJbl +zB3h7DaX/mqM708J+/Rmp47I00tApqabA7jhg5y0RVU46yE0QqGPSSSyohVtz/O8sn9 F0zcjlSArGZV+opauVfAKLOswOFhht9MsCwzqBZg/ZF2ZmJ9kx6MsMm1tpiHn6IYKQpf 2lAZtUadCrJxUgCSQIrrXobWNOh6rs7DIxsNnM31KPiOXyxEf3NMngtogghYw0Xp189U QVt/A33A06Nq4LCMR+hKWe6pkSPFHHBD8AobP2TMdw+cPHMRF50TIx/M3rc1/AKC4h5g zHWA== X-Forwarded-Encrypted: i=1; AJvYcCVAiEvqoVnUnnGMf+io0hCs/GnHyDKR4X9EZJUpHeJrpUJ7qDkyTGnFyDmK4pftnDuTgXslVR+0G7gDoSoz//A=@vger.kernel.org X-Gm-Message-State: AOJu0YzXzjOsHn4VpM31+ddHgqf4lfu1UWyChbNpna1iMmIwTG2l9Sew YRxVAuqw+QOiIIYKrBnswhJPcbAyNuQCu3LHXC0wdyfjayi7srxsXpEOE40l X-Google-Smtp-Source: AGHT+IHUddUyzwSvkPvBUsbeRbwCgyG1H46fnbK7AmlrU7O2Fi2RBuY2PGTjz7Td3RVHQkoHfaBXIA== X-Received: by 2002:a17:902:db03:b0:1fb:93e6:94a3 with SMTP id d9443c01a7336-20367c08e9bmr15979605ad.18.1724231394465; Wed, 21 Aug 2024 02:09:54 -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-201f031c5e1sm90191405ad.94.2024.08.21.02.09.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 02:09:54 -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 Subject: [PATCH bpf-next v1 2/8] libbpf: Fix header comment typos for BTF.ext Date: Wed, 21 Aug 2024 02:09:20 -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 Wed Aug 21 09:09:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13771112 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 19E1B1E3003; Wed, 21 Aug 2024 09:10:00 +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=1724231402; cv=none; b=cElC9H9EUG737Ngr3OFwHLVHmzyufgnTqEp1UFFv/VCXFTFWtlC8r0Vwu8SNAo5/JcYM8LWKvu/dZygr2YsBQtiZwdAIfZAAd0bKUC847s3AQN1KvMXTsd0e4Z5aXZX8TRUPtkIwofl2NEZNNYimef+sA74R6qxpSqkLVRzsUNg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724231402; c=relaxed/simple; bh=p+VcdB9HCA/6O6oQIVFwtXKCMYlajKRORAljsPWPaw4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=INkORzG4TvDmymSiHmOVE4uceUYfUnieWw7oZkiY4AjAN//eCbIhgJ1vX7AHZ575dCRVvGvuGNsGxInuxeSRghJX29SP0KL8iei9d528vOQotrgaJRQsRMQrezNkIPz+l5ly3ia6ovU2/EpGtnpe+peb4wb+Pkleop8/IE5ZGXY= 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=NUFGkL3S; 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="NUFGkL3S" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-201e52ca0caso48479065ad.3; Wed, 21 Aug 2024 02:10:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724231400; x=1724836200; 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=NUFGkL3SGXmRGjiLtyBoh93D1wq9TK0eBox5v4HAMuGwInrhe+30O85Ws8NUkHOg3Z x6SBibCak7uOHDqxcq5f3yY8OSTpNiJ9xCep8Ig3QIDVL/RVvhAd9xsjb7reRs9bCa09 jd7KKMda/IcmAtXoRIcvvzQQCy+PUSHcHpMi+0pqotkUEh8Tq4dx9+cLylKJb7wilzJr TEJQ3SbMXJUF3PPY9N000nCaiJ4OJqN8A6AuYwmQ1qWenNxU0PcKXjU1FTWszVP8FoX2 yF2a1qDtk+2j4ixZgdDG7+2tLHd0XErGrD4JUS5XHv6Fb7SzPCTB1BuS5wkJ8FurL8O6 Ho0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724231400; x=1724836200; 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=MnkzHdKTuHmUWFb9gvTtlMFnj+uXMd8/rHmRc7Tx03J2p1OpJhFVcBVGFNGhTFRE2I 3iCJU1Cvw9t4s6whI8RpC8IQxe0tMjo4AtwdKl1CaKuzv3jpB6s2v6/AwFxvHvY1BSwX ogLcam8BUvRY8+ABiWLxRGJU83RvBYwRAFbsDl1Plm9w/nXqVCv+Qh8n2JZwCfICbGjO pzuPA1U0r56g0pTNRVV7wTsHi26SrY+K6DP3K0IUA7fhriZaid38rOGEZ1lGKeP5fLfl nB4yquXy2WEcphJHyoUdXBWhB7P6mapeRJFD31aG37w2hOMiC0zFjikVxgWEPq6fJqUK g4bw== X-Forwarded-Encrypted: i=1; AJvYcCX3HiJuvrtYwVZn6zS7f9bgKB+Ufv2/9/nBd4XoQV5B35JXLJwIf2ruFc3dSzzTqyuVJQu21EGmMgUCk5Tt4jc=@vger.kernel.org X-Gm-Message-State: AOJu0YwQnz7ibvHLGtrMWMRZZqK++aiv819ZMlolD1cyxxmssOYfhwKJ 5rZE1+8NtCwZDgPX3kAwcXB2OiPPTapzVF0GESfD6DreSqNyU17BqtKR8B9P X-Google-Smtp-Source: AGHT+IHAozIxcg8F0MvaXToyw1b/Cz9pkiUO3DZh1uuuK5DSSTePym4QeklQXj5YhXEgfGjI4+zucA== X-Received: by 2002:a17:903:2305:b0:1fd:64ef:da17 with SMTP id d9443c01a7336-203681e3d04mr17719495ad.41.1724231400282; Wed, 21 Aug 2024 02:10:00 -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-201f031c5e1sm90191405ad.94.2024.08.21.02.09.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 02:10:00 -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 Subject: [PATCH bpf-next v1 3/8] libbpf: Fix output .symtab byte-order during linking Date: Wed, 21 Aug 2024 02:09:21 -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 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 21 09:09:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13771113 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 BC75D1E3004; Wed, 21 Aug 2024 09:10:08 +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=1724231410; cv=none; b=fAgwJXRskv4lXbZr3u8Qvw1mOp1gVGmyglpevvUzuXeciGErXog7bd9XFEb/7bdlx4e78fDI6ZFWKEcO/HIWE03jNmTvi/c6g5xd0H5btE8/YojOTLC1s4eu+5YVfF1Y5sZbs3QWXtU0SZ9ObZDnyBZFxhnXVx7Fa6+j6pWMXPg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724231410; c=relaxed/simple; bh=RcnnaHrY4fMAGL5jIkPD59CIepAORsN9Q6/v1K9Vv7c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hJ9y/cUEdVSwGKFG3yawnpERLsrGpoUEAmqlGW//nweExQe8+ziNFEdZBbIlAZWMwmGDNQSadMlmqAuhdjM0NJ9ZTjD/j55T8nmufSjm4Tj+wHeNdkuvp3M7TrAyrSRq5A8F812GemmN4IEstaHktpGrXnU5kscS27AD02bvhRM= 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=XaxxXyu0; 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="XaxxXyu0" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2021c03c13aso4569365ad.1; Wed, 21 Aug 2024 02:10:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724231408; x=1724836208; 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=1aLmYF0iKxzGNvG6bLQuspXuhD1KszRtAYR8l9iASOs=; b=XaxxXyu0BKEPdxLvsKmrivzG/fS/vsfcgCeBKJW1juVI0fdH/VQYLz2IXc6+N7BXed i48lGHSlKMvFA184/EgC9l1w7ryRqrK7xoZyRrb89HEmzYa13wrJ8c8N3OX1lBiq0itt +aaXWYDmKiuURWnwobAgCCUqg9VKfx6GSIJLiVaVEhTBpw09VUnk/vEfsYMQ+R1ZqOHq nTlpWvDPEP5xwqNozywTrRpNiKgXO4vBH1AaHzS636XFbF+/bxDcG6a8l8xi9Nwf9wF+ G3NpthGMH8yOCjFLgjIPn+z2+tbZkhXPnT7pTn/WVQBlcSRUKzGhxNMKM8oACeP3Aizp iDIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724231408; x=1724836208; 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=1aLmYF0iKxzGNvG6bLQuspXuhD1KszRtAYR8l9iASOs=; b=dugWfFMfumsej/moif18v6ZQWCjpSRp3NxVATT/ASBgRN9VLK/nj8Du7flr9UHMh1R eOEBbGKbK4JnaRathqATk3xLGvb3ogU7dxEQPwjOKOu8Rp6Tx0yahg93e7Et9UPHVCLL QU4PP4FJZ2YyvL5QpwPBBfWsJcJ5CoQ1G/FX0J26ZOHKt3jM/YmWHeaPaOCpBlshg0iz KGV8+9FZJUu6TwGUC3tkwCRtDLRdtb4sudBS4GTYmIBChfCkXvd7x3V3mPT88yFoDiMI gQBt4FPyazveVXowJ+Pl+xx2OsxZcy9bcvLfTVo12SDnmVY0XCYIUzKMbwGOxacKkH+P fXjg== X-Forwarded-Encrypted: i=1; AJvYcCUtoON9pxqDJRxwioJepdkWbOTh5/hhmlkVhy3up30jZqddUxcT/0UgcBG3RQ7EUXySUfDDybXNOH5BCu+PGMw=@vger.kernel.org X-Gm-Message-State: AOJu0Ywd6Mbe3y2PD0lsjuuLprIvQV2WmiHR++lcryeX76lNuBQ2pEYz YJtLZTpETieqg5WSXIUfOioF1KKj87+qp10+GRzMh7XdqGIp/QZUpcTAoN7J X-Google-Smtp-Source: AGHT+IHCJRaXAqYze4M3E0mQXD9sAFPRkO8IcXq5oUF4ClyasKZpi8wx81Cxk4aFLvUJQsiLBHDfxA== X-Received: by 2002:a17:902:b402:b0:202:21:eb2a with SMTP id d9443c01a7336-20367d0cf67mr24246015ad.19.1724231407795; Wed, 21 Aug 2024 02:10:07 -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-201f031c5e1sm90191405ad.94.2024.08.21.02.10.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 02:10:07 -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 Subject: [PATCH bpf-next v1 4/8] libbpf: Support BTF.ext loading and output in either endianness Date: Wed, 21 Aug 2024 02:09:22 -0700 Message-Id: <72ac2339af4046bcc6a78a798de6491f9f1d41d6.1724216108.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]:commit 3289959b97ca ("libbpf: Support BTF loading and raw data output in both endianness") [2]:commit 8fd27bf69b86 ("libbpf: Add BPF static linker BTF and BTF.ext support") Signed-off-by: Tony Ambardar --- tools/lib/bpf/btf.c | 163 ++++++++++++++++++++++++++++++-- tools/lib/bpf/btf.h | 2 + tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/libbpf_internal.h | 2 + 4 files changed, 160 insertions(+), 9 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index f5081de86ee0..fba6988a7820 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -2884,6 +2884,52 @@ int btf__add_decl_tag(struct btf *btf, const char *value, int ref_type_id, return btf_commit_type(btf, sz); } +/* + * Swap endianness of the info segment in a BTF.ext data section: + * - requires BTF.ext header data in native byte order + * - expects info record fields are 32-bit + */ +static int btf_ext_bswap_info(void *data, const __u32 data_size) +{ + const struct btf_ext_header *hdr = data; + __u32 info_size, sum_len, i, *p; + + if (data_size < offsetofend(struct btf_ext_header, hdr_len)) { + pr_warn("BTF.ext initial header not found\n"); + return -EINVAL; + } + + if (data_size < hdr->hdr_len) { + pr_warn("BTF.ext header not found\n"); + return -EINVAL; + } + + if (hdr->hdr_len < offsetofend(struct btf_ext_header, line_info_len)) { + pr_warn("BTF.ext header missing func_info, line_info\n"); + return -EINVAL; + } + + sum_len = hdr->func_info_len + hdr->line_info_len; + if (hdr->hdr_len >= offsetofend(struct btf_ext_header, core_relo_len)) + sum_len += hdr->core_relo_len; + + info_size = data_size - hdr->hdr_len; + if (info_size != sum_len) { + pr_warn("BTF.ext info size mismatch with header data\n"); + return -EINVAL; + } + + if (info_size && info_size % sizeof(__u32)) { + pr_warn("BTF.ext info size not 32-bit multiple\n"); + return -EINVAL; + } + + p = data + hdr->hdr_len; + for (i = 0; i < info_size / sizeof(__u32); i++, p++) + *p = bswap_32(*p); + return 0; +} + struct btf_ext_sec_setup_param { __u32 off; __u32 len; @@ -3022,24 +3068,56 @@ 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 */ +static void btf_ext_bswap_hdr(struct btf_ext_header *h) { - const struct btf_ext_header *hdr = (struct btf_ext_header *)data; + __u32 hdr_len = h->hdr_len; /* need native byte-order */ - if (data_size < offsetofend(struct btf_ext_header, hdr_len) || - data_size < hdr->hdr_len) { - pr_debug("BTF.ext header not found\n"); + if (h->magic == bswap_16(BTF_MAGIC)) + hdr_len = bswap_32(hdr_len); + + 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); +} + +static int btf_ext_parse_hdr(struct btf_ext *btf_ext) +{ + struct btf_ext_header *hdr = btf_ext->hdr; + __u32 hdr_len, data_size = btf_ext->data_size; + + if (data_size < offsetofend(struct btf_ext_header, hdr_len)) { + pr_debug("BTF.ext initial header not found\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 (data_size < hdr_len) { + pr_debug("BTF.ext header not found\n"); + return -EINVAL; + } + + /* Maintain native byte-order in memory for introspection */ + if (btf_ext->swapped_endian) + btf_ext_bswap_hdr(hdr); + if (hdr->version != BTF_VERSION) { pr_debug("Unsupported BTF.ext version:%u\n", hdr->version); return -ENOTSUP; @@ -3066,6 +3144,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,7 +3165,12 @@ 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_hdr(btf_ext); + if (err) + goto done; + + if (btf_ext->swapped_endian) + err = btf_ext_bswap_info(btf_ext->data, btf_ext->data_size); if (err) goto done; @@ -3119,15 +3203,76 @@ 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; + int err; + + 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) { + err = btf_ext_bswap_info(data, data_sz); + if (err) { + free(data); + return NULL; + } + btf_ext_bswap_hdr(data); + 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..8c4cbaba6194 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -167,6 +167,8 @@ 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 21 09:09: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: 13771114 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 036831E302E; Wed, 21 Aug 2024 09:10:19 +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=1724231421; cv=none; b=WITfiK49SSRDOtGcKrRAiFHdSNa4Xqcu8viRnRIXVL54tdfWSRlyNfIGTIlta+Fr7Exn+UwRuU4+PMhAFQjw0F1SiY0uOdc8UJ0Tcvkq0Iu6a5QaY4hGEx4FdA43W3uFlbbcQxm4LkQQacibzpbXzZAnxikJCv03kIkKuiLMrPE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724231421; c=relaxed/simple; bh=2FSjsTIxChjJ7UDO6ZzkAAnWPtQd4NHAnWX6C5k7eao=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OoDL0KyhrYmsWUQJDm9I7tBbAErpw7IH3YUsHYSiwXSNGWlKapbs78BzHQoqnqUgPcaR45vu9KIh8kU+1rDt7givNlVsDCMxe68ZcwBr3LDhS1cgy1fEPZonJM3eAyFu+ZfnrtARWrfXdlvnTBrhun7e8bOj4Pq7sTproSTq7FY= 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=KHMwEqyv; 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="KHMwEqyv" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-20219a0fe4dso35633845ad.2; Wed, 21 Aug 2024 02:10:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724231419; x=1724836219; 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=y00tHlY2avYO/rkQs0/uAiE4KzWaFN2sK6H2K9tNmfU=; b=KHMwEqyvSLd0RDljooIGJlWCLdgzjOm9vyLeaL3T+G670y2/Tu0c3dIowtocvu6Why egsHh6vnmSTPOA27YD/vsjfpvOHe4p4E1zyw53v9x/MOyOX5tIUeOqYl/MS5Vr1lCNgE ygu0OkfjROeyducOoTmIDar+D/U2GOpq7yLczYKIsT5fXnE/9Qx2tds/KKY5UflVQcsP e+8932FXa3QIGyf328XXHXtKoXg/vgHK3aNF3Va3Bpzgk2SlN0E3U44rbC1aOoRMOkIU BjC2OqWe/XK+fYe+MUrKgilfLg7kwmKDi5B7bclUSoGxoTkb+7yORa+ScM+cCFQuRiu0 ENuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724231419; x=1724836219; 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=y00tHlY2avYO/rkQs0/uAiE4KzWaFN2sK6H2K9tNmfU=; b=CKEOuGDvq6OXYF7fTaEiptVVukj40z7IytBGrvm/GjJHP/3XwF3Hhz4xElho1oamCJ 9ANAP6usa3O++Ujszz+gP7G1tj2hlc8Giy4gOybUrZgav0SReRLWLwwmttLfMGKkUWS3 76f51G2PtaRXhqkikEo56GZBh8ge3yNzt1axZzQhNsn2H43Ar/24ai5SZH3BIK4wLZyF 5qVxHcuW0OC2CIY1Qts/dz533XM0rvbNIyI42+2Ma1DXOqo5gtorNua23oZEyJtnF4JS eQpMn+9vghLIsjMkC7x6trO4Y+de3xDAxoo8z4sWlEDKIDX7cOF2UWapxPKurNbQl/N1 rBnQ== X-Forwarded-Encrypted: i=1; AJvYcCXYtBTOd42KHbOmv4ZjwdxoCnHxFZ0bFpt8dVxmaLOCB8E366sjVFlH2dKZJTHOr8xxi3eRuXCP3KWfJXd9jSU=@vger.kernel.org X-Gm-Message-State: AOJu0YwIlsPB7inU32UqYxYIkkk2hean4C7XGxkb1oGzeBXeRkQxsSyF iaY/jYehbcsgX4q/b3bzvesmUZBLeO+jv/Sz30/O269Ir1BGqfCFMa1QEEty X-Google-Smtp-Source: AGHT+IFxn6TkFSO6Y3brJ4NzV7wiL7tsGYdorgm4VlDTG/fw6XW26Qgar30DWVOuwAIGaUiussI5nA== X-Received: by 2002:a17:902:e544:b0:202:1547:66b7 with SMTP id d9443c01a7336-20367d3be4bmr18768105ad.37.1724231419049; Wed, 21 Aug 2024 02:10:19 -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-201f031c5e1sm90191405ad.94.2024.08.21.02.10.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 02:10:18 -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 Subject: [PATCH bpf-next v1 5/8] libbpf: Support opening bpf objects of either endianness Date: Wed, 21 Aug 2024 02:09:23 -0700 Message-Id: <62a2ef41629ad5ef7db48d720959527462e1beca.1724216108.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 bpf_object__open() to access files of either endianness, and convert included BPF programs to native byte-order in-memory for introspection. Signed-off-by: Tony Ambardar --- tools/lib/bpf/libbpf.c | 21 +++++++++++++++++++-- tools/lib/bpf/libbpf_internal.h | 11 +++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8a0a0c1e37e1..a542031f4f73 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 byteorder\n", + prog->name, prog->insns_cnt); + } +} + static const struct btf_member * find_member_by_offset(const struct btf_type *t, __u32 bit_offset) { @@ -1610,7 +1625,6 @@ static int bpf_object__check_endianness(struct bpf_object *obj) #else # error "Unrecognized __BYTE_ORDER__" #endif - pr_warn("elf: endianness mismatch in %s.\n", obj->path); return -LIBBPF_ERRNO__ENDIAN; } @@ -3953,6 +3967,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 (bpf_object__check_endianness(obj)) + bpf_object_bswap_progs(obj); + /* sort BPF programs by section name and in-section instruction offset * for faster search */ @@ -7993,7 +8011,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); diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 6b0270c83537..f53daa601c6f 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) +{ + /* dst_reg & src_reg nibbles */ + __u8 *regs = (__u8 *)insn + offsetofend(struct bpf_insn, code); + + *regs = (*regs >> 4) | (*regs << 4); + 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 21 09:09: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: 13771115 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.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 C74871E3004; Wed, 21 Aug 2024 09:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724231436; cv=none; b=X/iTbfh0sEMrpVGfyiMT0IS0lixnPe5ztSS6f/JXGhb37LrKHrRiL1KC2XaDTDGD/1QEn8htHFv/UQ+P/29oEFk/1bKMguQxXI9blCJN4vWF39DNUvRe98KiuQ7ZA3Gdq+mzlybHCnJst96uAevFQLnwgZYLNORIbUInI71fN/8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724231436; c=relaxed/simple; bh=yQB0lQx3oD6X/5PKXL3UHA6cbOEzk7oqb3jdmQ2PX7Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MV7F9Qpg2Akcysp3CnsPhQXQOWSNCS/VI10xS74eYS0z1hhzFhN6+mYv4f/+Ptuj2wtWenInG7E6m8B2Efjdm8n0cmq4qY4f5l9VGtVSx2NwEcoWg1tRgBp37L28jAecpo/Pn80+a9wnXMBU+0l7kP93YMbiefqxH5QmDoyIpng= 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=m1dmcTE+; arc=none smtp.client-ip=209.85.215.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="m1dmcTE+" Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7a1be7b7bb5so4578025a12.0; Wed, 21 Aug 2024 02:10:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724231434; x=1724836234; 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=om4vqqQ/0cNqedfLTqsqxQTqvqKbzb5r3RH1OMQkD3s=; b=m1dmcTE+OPCo3ifAYGhN8SH0/3ZJ6DdRSrhCT6FXAd2E66D5aI6Q+nNO2DQpyPcCD5 FRDnPYHcR6nxSzzET8LjWR0lB3uDhqvu57I9ns9TDA1iEPpd+C1M6URzZb9JXsQYfPkb zoYteVWi7wDp1duik8gZCp1ZRQh8w/GFGSEef6pP1pWHlkpl9gd+DYC5JkajqpWluY2Y cP17TCS8azp3OyVEKlI1OL2ldvfg9l/zX4kCiygynHGB+MhWilG3GBxwapLqs9bd33bi dIgkos3qeHPCwIqgKn8ISsN0simurzwNUztd2vNm2YNB7LTP0KZN9s9NScD2H0zJ/yHB W+Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724231434; x=1724836234; 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=om4vqqQ/0cNqedfLTqsqxQTqvqKbzb5r3RH1OMQkD3s=; b=EN7h40prydWOt5ILLDIS37gtGnfyhkI4HKRK1OL5lv2x+o1utLwq/D6l270E3dNMzv 9ZmlYduvAEdn3TsK6N2tkhn4fV+Kl/COjLv5uChQNBi6iEttlAOjawA7pG8eqY0pzMv/ YQjWgEpRaYbG/sz8VJzBTBR2mcM+v0pT/1CSEmIoJvnFqsz2avNF6SYntS0I2xZfrKAD rDxzr9vOna6/M23sQYhezkiHeYRvGvjtuvlyld2mpVfd+ar5eXCc48Yyqb8UND2BUtI6 eSYWlLxQp5b8cgbY5yjC7HkLHpHZIz8KIJAcr6xgIn/GnEdv4ofQsCQUgO8qgDaZneDp Y9Hg== X-Forwarded-Encrypted: i=1; AJvYcCXtleFfKjMGzTjN/Y+cpVf6fqugdT65KYsWTLFUDkD1sqNG1izktkrnte80X7ARzr45Sx+cFW4gqyQw1N3rGi8=@vger.kernel.org X-Gm-Message-State: AOJu0YyzLgbXMDT6hQSKeopVZLGqyGSPftMyOsEgKZyUj4T4Dm6NZjCb ifnQYhnBSN8/k6zkcfl7JZ7Uo3vWygpk6h217jDppAHDuIGJnKd+/mXRr8Lj X-Google-Smtp-Source: AGHT+IFNwJQGj5kRUTj3LgCg83CvL1G4H++bYKBiSDC6MSU1huXlu1ZxG9cIX94vVKBK6kLJZ9UWQQ== X-Received: by 2002:a05:6a20:c78c:b0:1ca:7440:9b56 with SMTP id adf61e73a8af0-1cad7f6c81amr2240050637.1.1724231433924; Wed, 21 Aug 2024 02:10: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-201f031c5e1sm90191405ad.94.2024.08.21.02.10.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 02:10: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 Subject: [PATCH bpf-next v1 6/8] libbpf: Support linking bpf objects of either endianness Date: Wed, 21 Aug 2024 02:09:24 -0700 Message-Id: <06df51af59486b89018279fa32f840b505f47856.1724216108.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 | 106 ++++++++++++++++++++++++++++++++++------- 1 file changed, 90 insertions(+), 16 deletions(-) diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c index 7489306cd6f7..778b2b9d65a2 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_elf("unsupported byte order of ELF file %s", filename); + pr_warn("linker: unknown byte order of ELF file %s\n", filename); return err; } + if (*link_byteorder == ELFDATANONE) { + *link_byteorder = 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("linker: 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,16 @@ 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)) { + err = exec_sec_bswap(dst->raw_data + dst_align_sz, + src->shdr->sh_size); + if (err) { + pr_warn("%s: error changing insns endianness\n", __func__); + return err; + } + } } dst->sec_sz = dst_final_sz; @@ -2630,6 +2673,14 @@ 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)) { + err = exec_sec_bswap(sec->raw_data, sec->sec_sz); + if (err) { + pr_warn("error finalizing insns endianness\n"); + return libbpf_err(err); + } + } sec->data->d_buf = sec->raw_data; } @@ -2696,6 +2747,13 @@ static int emit_elf_data_sec(struct bpf_linker *linker, const char *sec_name, return 0; } +static inline enum btf_endianness +linker_btf_endianness(const struct bpf_linker *linker) +{ + unsigned char byteorder = linker->elf_hdr->e_ident[EI_DATA]; + return byteorder == ELFDATA2MSB ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; +} + static int finalize_btf(struct bpf_linker *linker) { LIBBPF_OPTS(btf_dedup_opts, opts); @@ -2742,6 +2800,22 @@ static int finalize_btf(struct bpf_linker *linker) return err; } + /* Set .BTF and .BTF.ext output byte order */ + err = btf__set_endianness(linker->btf, + linker_btf_endianness(linker)); + 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, + linker_btf_endianness(linker)); + 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 21 09:09: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: 13771116 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 377721E3884; Wed, 21 Aug 2024 09:10:42 +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=1724231444; cv=none; b=oT4ZPxXpiBi9pLkP2tck6XUCyjDg9uMfqgOOcQgJW4xKgKkhzS8EATSLhTMwCZYI2KQo8Msnw2/4gg5B+6Tn2Jekthk7bVekuH/0vjSox3qWRWALBERxKvcPYvOhFImlJXlEZUeHcvupsBG8IzauQp0tyG8B7GlMAMzHK5450iM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724231444; c=relaxed/simple; bh=F8xd9x3E4ETOLdDXSHHkDGKgz9k3t2OZgUuiYJsz2cA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YoSDbM4aP0jbKfdpARMsJxgMD1Cv1sEmbe4UsqQY0GZwP4vstSt3MBLXbYeI7h0kYbQ8+ChDIb3FQhb2jqS/PISwVdd/mVEcAGiAsBpJFJBHZbdbYfTRZKZUveRz4qTapxAPcaK1eZuSqv8cW68tW4x94UxnZGu6dQfrOkWlTOY= 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=hbUCTQ9c; 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="hbUCTQ9c" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-20230059241so27055945ad.3; Wed, 21 Aug 2024 02:10:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724231441; x=1724836241; 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=/YF2Or2+UDegoN4zsVYYJAQmdZCiT94Old9jsN6y3yM=; b=hbUCTQ9c3NQXD1Yi+7LaNuRmBeKUjU5vnnVG+u7tQq2J84HL4w2JsBz6C7xd9p2oGE BrZWTV6zk7ZoTGTRkAoxn00lbwF02qdPAn5QVjA1tRoeeTunW9LkukFnq+X8Lvd3EmtF XDhydziWVlG2vxcvKJUCC8nbyyI+h3KKJKDxY4VcCNSF/m/QPk6ldXgUiD6eJl/pnWfq AsMswQVEV6R3ClfWh8xTbZMz58ffcdj0L3p7nx2ZqKuwAZ6ceOTPPUNRDheEA71V54Cg 8/9HPxjgLdmRx5Se+XgqP9MxjF4PCWAmSCC77POkiPCSB8CeNhqSjR+GZBtgIiu8WECD Z1ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724231441; x=1724836241; 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=/YF2Or2+UDegoN4zsVYYJAQmdZCiT94Old9jsN6y3yM=; b=pvIQTUqfBfRuTMSgR5oXSPHJL7tC0WhukNkYajywxtxpna9BrgBWBsZ/yXzTfMDmcB 6rkYPmgPxWjHdIxKdpaejjwvgM0B7CmaPctB8yW5HiDA6q3qADhr2WJVViRB/LKS/wTs Ene6DidVYcJRxneBbOCHOVdsE/kIUGZTJAPPQ2U4UBLnYr/uBJrtkuGsGiEsznDA/++6 TV7NDGCJgAJKp/iarRZ8kXg1+wO3JB3Y0RbJlWX/mcSFbTGwk47n8fhXNZX1mG5jSgdn IYO9t1LP0GrvXBjrXBT3tJbPNwkJD8S5N0vDW3FTC4zngxyamXtYAFcYWun05b5Q63cI CpLA== X-Forwarded-Encrypted: i=1; AJvYcCWbk2VJ0OpoQRgl3ldjP135CL/o/fAX92cWPmJzIp/1SJ4cudmdqtYc5rmCOEMJk68QGhI3i7yxRlIQkVsFh9w=@vger.kernel.org X-Gm-Message-State: AOJu0YyOqWb76NbPn6TcKhagbQDNTq5pXP2AAaLgAXKWqqDcu+55alV2 b1FBLiN+x6htfv2sLiXZdR2QsBgCgYijgjc2xo05XPlejY9VkleemhiDb0DL X-Google-Smtp-Source: AGHT+IGcy+AxMbYyfPS9gtq/amna4agqPfjHiQZcZjvTtypV927HOU4qAlf76ZrFxxYATT9EPpBFAQ== X-Received: by 2002:a17:902:f0d4:b0:202:23f1:ebfa with SMTP id d9443c01a7336-20368096895mr12117545ad.52.1724231441356; Wed, 21 Aug 2024 02:10: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-201f031c5e1sm90191405ad.94.2024.08.21.02.10.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 02:10:41 -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 Subject: [PATCH bpf-next v1 7/8] libbpf: Support creating light skeleton of either endianness Date: Wed, 21 Aug 2024 02:09:25 -0700 Message-Id: <449311df398c086c4b065b727206b3f0ad0fedf3.1724216108.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 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 | 179 ++++++++++++++++++++++++------- tools/lib/bpf/libbpf.c | 1 + tools/lib/bpf/skel_internal.h | 3 +- 4 files changed, 143 insertions(+), 41 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..d9d396946977 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,33 @@ 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 setting the correct value in situ and is simpler than + * writing a separate converter for *all fields* of *all records* included + * in union bpf_attr. + */ +#define move_tgt_endian(lval, rval) { \ + if (!gen->swapped_endian) \ + lval = (rval); \ + else \ + switch (sizeof(lval)) { \ + case 2: \ + lval = bswap_16(rval); \ + break; \ + case 4: \ + lval = bswap_32(rval); \ + break; \ + case 8: \ + lval = bswap_64(rval); \ + break; \ + default: \ + lval = (rval); \ + pr_warn("unsupported bswap size!\n"); \ + } \ + } + void bpf_gen__load_btf(struct bpf_gen *gen, const void *btf_raw_data, __u32 btf_raw_size) { @@ -422,11 +458,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; + move_tgt_endian(attr.btf_size, 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,23 +495,25 @@ 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; + move_tgt_endian(attr.map_type, map_type); + move_tgt_endian(attr.key_size, key_size); + move_tgt_endian(attr.value_size, value_size); + move_tgt_endian(attr.map_flags, map_attr->map_flags); + move_tgt_endian(attr.map_extra, 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; + move_tgt_endian(attr.numa_node, map_attr->numa_node); + move_tgt_endian(attr.map_ifindex, map_attr->map_ifindex); + move_tgt_endian(attr.max_entries, max_entries); + move_tgt_endian(attr.btf_key_type_id, map_attr->btf_key_type_id); + move_tgt_endian(attr.btf_value_type_id, 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); + attr.map_name, map_idx, map_type, map_attr->btf_value_type_id); map_create_attr = add_data(gen, &attr, attr_size); + pr_debug("gen: map_create: map_create_attr: off %d size %d\n", + map_create_attr, attr_size); if (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, @@ -784,12 +824,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 +880,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 +971,34 @@ static void cleanup_relos(struct bpf_gen *gen, int insns) cleanup_core_relo(gen); } +/* Covert 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 +1010,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); + move_tgt_endian(attr.prog_type, prog_type); + move_tgt_endian(attr.expected_attach_type, load_attr->expected_attach_type); + move_tgt_endian(attr.attach_btf_id, load_attr->attach_btf_id); + move_tgt_endian(attr.prog_ifindex, load_attr->prog_ifindex); + attr.kern_version = 0; + move_tgt_endian(attr.insn_cnt, (__u32)insn_cnt); + move_tgt_endian(attr.prog_flags, load_attr->prog_flags); + + move_tgt_endian(attr.func_info_rec_size, load_attr->func_info_rec_size); + move_tgt_endian(attr.func_info_cnt, 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); + + move_tgt_endian(attr.line_info_rec_size, load_attr->line_info_rec_size); + move_tgt_endian(attr.line_info_cnt, 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); + + move_tgt_endian(attr.core_relo_rec_size, sizeof(struct bpf_core_relo)); + move_tgt_endian(attr.core_relo_cnt, 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 +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)); + move_tgt_endian(tgt_slot, 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 a542031f4f73..8b6c212eb9a3 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9106,6 +9106,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 = bpf_object__check_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 21 09:09: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: 13771117 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.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 07C1B1E3884; Wed, 21 Aug 2024 09:10:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724231451; cv=none; b=pCoGeXiZRl1EppcSR4+OJEMmAFn7DFl/1yNatyeHCr9uvOMog5HcLlc4x3AEsMrJD61IRobwywyOo/Wxcy/u0jdh4ok7+511Lb8oFdtyF/K/5Qhz8ZrHb7ew2saelQblmV2aK3MLZ7/al3E66H6LK0K/SFsxzwZnn2xsqUFQaco= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724231451; c=relaxed/simple; bh=jOVV1mUFmYUSpiM3Wvxtf5xJ3j3iWH6e+YjabNu1HEs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=G52UdBiv6bhAoM7+Oawc1y4Xc+/jUc7TYn7xsIWa1eCMqxSa1rggLb95FoLH5Z/ukbTC+C0f88kaczU6G4ssAobs8fYNaNv2XBKAHOw3h3ljyORRrrYFOgPT7oFlY64Xu9G/AbkeiAqIN1i41sVJUVs5rvn0Q/+v3XihNo3nmos= 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=bmwmy6BQ; arc=none smtp.client-ip=209.85.214.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="bmwmy6BQ" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1fee6435a34so42669155ad.0; Wed, 21 Aug 2024 02:10:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724231449; x=1724836249; 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=RdS8Gy+ep2HLBkBMxdTVMiQLpqkMJJLaLRhG56RDYoI=; b=bmwmy6BQRSDKRL/Uq5Hi118gMRoJqPzV9KYKnb7KGctiIe0RHg1T8qBMcTfSVz4MMT KdoNym4137fClk+tTYkGQJD1G1hC7afVDN8zR6+PYpOfHenpYygy1kte+GUe1gi0hwsV RjanaSbqJUgYqC26AxQ9jA6Du8WlE//Jjg0yKU+wBnSzc7OZQQq50DntrWwE4JMqJ9Px eY+l6npYY5qAf0kU8u0pQG3Cm0uoLbRf3cc7fdOphzoqmXcmXGUbgEUpj0IEacbxVfYL 53sbtUeS0PnEAByi7uiOw1LW9Bm7BaTxdZ7xDHC3rJxGynFXgEH5LHUpkHUX/QMhETX8 gTxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724231449; x=1724836249; 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=RdS8Gy+ep2HLBkBMxdTVMiQLpqkMJJLaLRhG56RDYoI=; b=Z5EnJoJVTDHNflmdcmanTIQogtg0N4LI8T4sbwu0SFbdZTjVxMvcPRhbJUk3rJf0yW wI4zNDMAuZdlcZqhYaNzOZafK11PEEJj3KI71+dtk/zgppoC3kXYLL7HpXZD2dvNphVA EzTT5UxPsy4UhPo7AeSMbbH3Hzg7ZixoqH7zVVnSr9VU9lywYoAlDRMhHdBCi2IVW6z6 IQselMmBooH+mb9k2Wwd1e/k3sh4KuTWyIbwBBX9IJ7YztEd0Lrg2QWc1VIK8h79o1Xg GodHqQoVlhdFZ66Lpmp79cexdI/FPSWFz/Q8kyzCcrWU4kzA6grhpYyseAp1F26QZ1Am edCA== X-Forwarded-Encrypted: i=1; AJvYcCWuARcviVV1iDfHlYtJhWsecchB5t4IqiUbcynyKJAmlRX6r5ofE7+hQtwTOt53l4iK2Ar0YVpmfOasP1zFMGY=@vger.kernel.org X-Gm-Message-State: AOJu0YzzZIWEDFb3f2W3Mi2ptNwmyD3JEIKx80UNAe5Gj86hsZkK961I aL1D4mSkP8EO3jwcqlPbIAIcOTwU9O4HSsyZK5Zw0B2Ho9DwS5HWOylTBVRR X-Google-Smtp-Source: AGHT+IH98hIkJylIYSiG+E0o1DWyqiOa/70qeq3+sEmv/mgfnnSvy9r3GJ8qRODZthWWyehPeFQ6Pw== X-Received: by 2002:a17:902:ea0a:b0:1ff:56b8:2c1b with SMTP id d9443c01a7336-203682151c6mr19130645ad.60.1724231449092; Wed, 21 Aug 2024 02:10:49 -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-201f031c5e1sm90191405ad.94.2024.08.21.02.10.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Aug 2024 02:10:48 -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 Subject: [PATCH bpf-next v1 8/8] selftests/bpf: Support cross-endian building Date: Wed, 21 Aug 2024 02:09:26 -0700 Message-Id: <80190c7c8c65aa9ed61341949090fe08f5252c18.1724216108.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 3b9f345f7909..69ec6960af42 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -402,6 +402,7 @@ endef IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E -