From patchwork Thu Aug 22 09:24: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: 821994 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (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 1D6C2157A43; Thu, 22 Aug 2024 09:24:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318686; cv=none; b=I7ZEAEo3ZDh8Icp3kdSQq8VCf0WuR5K4Ur+gO+J2nKfhJwc0d1ixeTai23LtyQ37T759YL0JS3D/xjP2TTyU8h+ONAKDJOmrau1JlgF/n8ynY08OH97TMTfoqlAGjTWePkmbWacoEZYJCD1rXyHDqtLzXsuQGNIIlIEfRwjAoaM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318686; c=relaxed/simple; bh=W7u+8w+h8hZWKGfd+QED6nqeYS6S2UYV8DBHimrvaIk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CvcphsD0gpBBlH5vH7OAIpes8JGGj7kjvKnmF6Qsx33SnYakCZHLSj5COvNxtM2eVC/cN18kAQBzVvizf3ceEvwRvdtsL+ukYPDltCPBkJzgh2oWjkcpAfibeVYOVdCVoRqvifFs7OkW2PIqo2Jo5LUsCBsWVX7xyL0VIp9NLe4= 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=Wj7eNYZo; arc=none smtp.client-ip=209.85.216.41 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="Wj7eNYZo" Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-2d439583573so407452a91.3; Thu, 22 Aug 2024 02:24:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724318684; x=1724923484; 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=uvAD4v48CuguNlhDVFd/S44woq/SZ1SlM5Yjoy53vzE=; b=Wj7eNYZoFCI2JyGvycIh7z19mpeYGeTc7dDSRbUJcrhLohVOwgtMBrgtb6yY4SRac8 50n/q0Bp25JwN6J5WuvWbKbQk0tcsKa9TnVLiiL6KpDxSJfeK/hqeZ9hEJ+me0/X0chv xK4h2deSTk5DSsuTbA3GgvKVRe8fXeeS4ZJq48A+1nIYI4/TXQMnu+4yy1ZXnpsaFCGY Xnb4Y0QuhpILNAzgBr9FldEcY+KNEo1akTQcBaSwjAT+qOr43rltrwLWaH2Qih98k4yL vmPd4CZFpJAHJFhSk1M6Q1F9vP4aUrmU7nWlnuzCHHdGOV3V2svREDUaKvf6Us+MD04O Umkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318684; x=1724923484; 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=uvAD4v48CuguNlhDVFd/S44woq/SZ1SlM5Yjoy53vzE=; b=JVpqc7q2Q4vdAoPiETENAgEPAWWCqCTKHsMRiTsOwKGxwq3i93218on8DjW1pycXXD AglJgmq/ttMK9hoaxEKnXbAV/l5lbLlDODaDC5PX3BVBbACEOIZwOAsy3TBsWj5JiSYv LkF/USSDuzM13flbEwxoUT6rYmOV8Akp9AN0oMFuXvyZZnnmpmZkK847phAUKfqYLYM+ Cb7uKxIKRviAGbT2R4SmLEqQ7JsN+0wtXRJiJo5Gzj3BkWVbfFQt52hUWthKHWOLC81+ 1/93zlGhDMXplHnj5D4gywVbetUwBWsBxCcmdGT525HGV//YqQJjICJssDDghqhMPxOz 0b4g== X-Forwarded-Encrypted: i=1; AJvYcCVd/52Sw/euvkivHgwXt0LRgVY4u1TxlmXTQsTfTquvObKe8dF2c/IZD4gz/F56i9GklBkhkOnv5awtpQ7+RzM=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1Bk7TiRRDglbbop1yB2c/78ekpL8Fs1FbY6teY45QY8lfwd51 97bt4er3FHwmiSQUCYqBtGJ1ryQPJt2A8U8X4zbwLdq6mEwmju12PxRnWFp0 X-Google-Smtp-Source: AGHT+IHF81E+fAH2gWFVog0C1kxu5da0aOSjgtlE9SKqoV3Fy7X8je8xGrVyVaX/IFvrCNd1bf6pqQ== X-Received: by 2002:a17:90b:380e:b0:2cb:4e69:eaa3 with SMTP id 98e67ed59e1d1-2d5e99c53abmr6020841a91.8.1724318683925; Thu, 22 Aug 2024 02:24:43 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d5eb90decasm3548820a91.23.2024.08.22.02.24.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:24:43 -0700 (PDT) From: Tony Ambardar X-Google-Original-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 v2 1/8] libbpf: Improve log message formatting Date: Thu, 22 Aug 2024 02:24:24 -0700 Message-Id: <884c0e57404f3ad15bb658bc55b7d16928069828.1724313164.git.tony.ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Tony Ambardar Fix missing newlines and extraneous terminal spaces in messages. Signed-off-by: Tony Ambardar --- tools/lib/bpf/btf.c | 7 ++++--- 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, 9 insertions(+), 8 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 32c00db3b91b..cf4f7bd7ff5c 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -2940,7 +2940,8 @@ 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 +3029,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 +3291,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 Thu Aug 22 09:24: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: 821502 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.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 0AC7513C9CB; Thu, 22 Aug 2024 09:24:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318692; cv=none; b=jR+7sivkuorjMDfVehIwbdBTCgZJRpboCcgk59//Aim9rgZeCe46DOamnjk48gKywlwE4vTrI94w9QwRTvznodFqm8u76cGWhV5TAW3PZapNdutmjcO0cgK92TNSoXYnhCuFVSEPm3dnlQuUt7Je0P4Bvm/3E8wdHxfNXITKnbg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318692; c=relaxed/simple; bh=VBNLRm/K6S9P+VrNfuUGLUml+N9DcK1q+aeGBKmB8I4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mzadIdcqpcpCTzYOblCN74I981xFkM4hkt6hLQv7yqp02pb0bQmpogXZQbKHVCBrajAoOos8Zxz31hipPElf09xYzNhxalZoH2iv5OQmjw0zuBUX+kE3/HRKJWOXyFfUEKn/5iL/3Ey2NrMStsnTVLKIi2eDOCmCi1z+aLszY0c= 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=RH5eplnu; arc=none smtp.client-ip=209.85.210.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="RH5eplnu" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-714226888dfso522342b3a.1; Thu, 22 Aug 2024 02:24:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724318690; x=1724923490; 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=QSnIlYklgq3so9gt2lwPJQSnXdrAywKGiJfE+S1Ka1Q=; b=RH5eplnu9TaSOrrbJmUQsgOzuv/lFA79tVJI5RVYdCz1mvqGKTv+gOOTTbsaymaAdB 9syW3j+tn/VwAEGDXcqBmsHvzJ0JZZx4kstXll6Gn+HEVAW/r3LHV3Uc2602J7o+eqCX OUt8ouhmIxLgyngBQILq30zRGaEpWvaR08roZK/d1uKKk0kjPikvYydhaLRe+QQqTlSb KwcH9a1HZzTyKhOXQF4U5jeJndLE3a5Y+HI3Tjd7OSbki3iYh+X8SUezJYA8hibBuDt4 ZX9qNmbM0d8eCiGvyadMX8c6JlB1jlnLv20Mh8tpM0N7RUr44Cqh/hpv2qoa9prD3shV Bkyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318690; x=1724923490; 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=QSnIlYklgq3so9gt2lwPJQSnXdrAywKGiJfE+S1Ka1Q=; b=YoUjEhvxbBaJmnwgyhNRe6PVI5Og4vrqfPC/wdXLwwrRP1VxQ4NKCCxh0PJdTrM++Z PPuLZ0YlyEgh5ktMyscC0p4kn/evFQirSvpo/9v0vHGlqXtB6XPfYobe3v8hyfqNkvaf dNKrHq0QsdpX+ZBfEYBIUBD/aG/DRjEA79ORmM034fURwtFffOPcPIfHb4yd7hmjv6Er LeDGunhLA3t5mIXZPJF0+YseXj4xm34Bel6/Jy2kF94pQGLI6j8mLaSVYiRKA4QJleS4 zl0dZX/sGbHBNM3DQsloD5CiAzyEliy/iZBNOq4H+Wq3lLk/BqKKu3NfO73S6OoObOzH GGbg== X-Forwarded-Encrypted: i=1; AJvYcCXc7ZYv51+Ur8oxo2K5n7XZRP2N1LQDlKQI3DQaYU4/HKuEroIvFdXVVEOmAEX+nZWaEZJWH5wJTYGGBtZt5yM=@vger.kernel.org X-Gm-Message-State: AOJu0YzQ07ZZQG5nSH52Q5W6Q3LWvt8PWMbIALfPyNp5CD0YdiKTHPwa UyqJzwlpoLqT0IKKC9tJgXnVNwZgum4SFwLRYsS1DfvVbmf1QJPTP+fqHGfp X-Google-Smtp-Source: AGHT+IFX4lIZ5kUnWhuWkmyJ5Dy3yqobDegARzbZFbTUzpTUHNcI/yzIpG9ZwxU8jxjrr1PzyA8B2A== X-Received: by 2002:a05:6a20:2d23:b0:1c0:e629:3926 with SMTP id adf61e73a8af0-1cad7f89740mr6692997637.16.1724318690142; Thu, 22 Aug 2024 02:24: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 98e67ed59e1d1-2d5eb90decasm3548820a91.23.2024.08.22.02.24.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:24:49 -0700 (PDT) From: Tony Ambardar X-Google-Original-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 v2 2/8] libbpf: Fix header comment typos for BTF.ext Date: Thu, 22 Aug 2024 02:24:25 -0700 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Tony Ambardar 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 Thu Aug 22 09:24: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: 821993 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 DC9CE16F27E; Thu, 22 Aug 2024 09:24:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318697; cv=none; b=dEKGCrzuZdIU5ba0osOHO1vKmMt1iVHqextsCx4Avfh8+LEC7ku3oAHCcVTtVyZ7gVXBpH8TaRR1hWA5LfY7LmY4hM/K86sdAOw5rG4y0uSRBOIuLBdO5Qu7mEJygmRTkpcdfpBNmqkot6X+zIRr7TAoFNLxc+MBeUvj5zBAW9I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318697; c=relaxed/simple; bh=SPaS0CNuTHL/ufDMOW9NsyS5JNzQD9xoiWPpMupOO8g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bbj8cKbOsi5vWTEtKhPc4YjitbGCbBbG0Usm/GignngegZ1unl/gG0f3m+C9YJZng9m5z/xCbfIEZRBZ+d7koacxqVGc82zBVy2hVsZ2mhP9wLNMmQIJm35TM34MKxCnk4BogHH1kMkSISd0PXVxCEF4R5BF1JS25Izzwz/Kf/8= 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=SA7uhL3B; arc=none smtp.client-ip=209.85.216.46 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="SA7uhL3B" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2d3bae081efso457338a91.1; Thu, 22 Aug 2024 02:24:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724318695; x=1724923495; 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=HzyqOirBVYrCbnvRdKY3/sWPWtp6YYJZw3JHvwNRh1s=; b=SA7uhL3BeVVmZHxSVlLkxD1Gug0x9wcvCug+GHB9i2y3B9cdsUhKl74HruixnH1jW9 Ie/pUEPdNissdQMYZyxGi+V2i1Zq+36INm3Vp44xaZciZJaHkGhp/ukV2eMk8bNmfryx NhaiztLZR8qIu9DY9PSSOG54Nh8aLv20n5+gWOs38EWJ5LxWZ+KKx4F/QB/u48drBmg1 fnW1OItCXct6NN7W1qsAff898WLa6e5ux9/80dVoXs21M7h9ZxXzNb/gf2RFJr5J21OP 5ct2Oz12g6KVQ6xcz4zWeo5R74P2OJoESRSOYgbixe/gCBsPrhK3gHOUoRmiP/159zSo +1VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318695; x=1724923495; 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=HzyqOirBVYrCbnvRdKY3/sWPWtp6YYJZw3JHvwNRh1s=; b=pmRZJEWvb26lAWgHSx3WcB6udd8JTkQ9UcPj4E9H3filSKXrmGdZIu61jHemAPB6b2 xsAvH5eWg+jO44SNiiDXmpvXxO3WwVtVtGyvbRS7eU+6D0OACXVfCLbxjYHPqmSSwNFD u4Oa7QGz54tgXso6uGmxCDr2D4cE2iwIEY8tksY6qFVsotNWtEVfseavfU89m+2L3riE kIlffqG6ZNvFzwuKP0Ei8FvWhZDnX8K5ZW5Neb/kej4Ue+N4do5XGbEWHhVHWbcxZYP3 dCvph1O3aH5rmJdO2AOodjrM9i5MsUUDD2EAxZr5hyVtJDvkj2aHVDVnbUJpNjgBDEge K5+g== X-Forwarded-Encrypted: i=1; AJvYcCX71Km1VV7KU/zpEwXyTPiXvXvaaHkg8+qcZMHqA1oguRxgSuqOqz7pccsFO5ogRajbSoFtpb61JP7bdi/Gid0=@vger.kernel.org X-Gm-Message-State: AOJu0YwUMdU66st/oxDhcHZWpzXIx7Poin4E27QjGtQ2nc8zYM10WTHn 8C7X8ZvcgaiW1lkTl14STIDG806egtw22Nj7Er0QnQKyGje+6sRd+yeO7xO3 X-Google-Smtp-Source: AGHT+IH3YNoFwSFw91LF9bFUj522IRvk+GYylZd0RcXJndhWcrFuv2egOPEFH9ltvthADYIz3FsWDw== X-Received: by 2002:a17:90b:4f89:b0:2d3:dca0:89b7 with SMTP id 98e67ed59e1d1-2d5e99fb6d1mr6494251a91.3.1724318694935; Thu, 22 Aug 2024 02:24: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 98e67ed59e1d1-2d5eb90decasm3548820a91.23.2024.08.22.02.24.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:24:54 -0700 (PDT) From: Tony Ambardar X-Google-Original-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 v2 3/8] libbpf: Fix output .symtab byte-order during linking Date: Thu, 22 Aug 2024 02:24:26 -0700 Message-Id: <501a1b0e34bb9db377a81a6fd088c36cf3f98df3.1724313164.git.tony.ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Tony Ambardar 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 Thu Aug 22 09:24: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: 821501 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (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 4F00217BB2A; Thu, 22 Aug 2024 09:25:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318701; cv=none; b=lv8v6CUAi9Znr/mqyascrXfNFCDCc0o/W7qLfeEc7uzlP4jvJ+q/9J3/8x4QmKrMiOmyr0jmFEVejsbeuuahpymJfKkp3fyP+H+i38oPMr5EUFB4VZYh6lWq/4IqNS0+hJOMYUwHPULkaWxrjweJJ4P0gR5Plm06M9scdXtx9rE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318701; c=relaxed/simple; bh=26TlPTEbZ905jwsgRIUv0ZCFSfQYUKHgv8/ohfDKF/4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=o3XuOtUJLeLn4l+W0wbbPBzBtKCnOnwNsPz9yzT5yZFyETjgBEZ1LvB4SeqlA/TGtyi6OrKc8H7qfwrfp/rwVvRwm4kdF08cK+I+Io0qLqjyUnuZUKqtWChSA2XHqI0gOjHh/ottJsSnisoNl4aVA0K7ugXAA/WZQH21dyeFg6k= 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=bXufyS9D; arc=none smtp.client-ip=209.85.216.48 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="bXufyS9D" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-2d37e5b7b02so412653a91.1; Thu, 22 Aug 2024 02:25:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724318699; x=1724923499; 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=PzODO6nzCICIUMlgGVk4+Q48fOL7xNsv4iWIgFEFZJQ=; b=bXufyS9DekrUO2DuEua+D//mmgi16MeWAOqrxOuiZ/KweFNo7yeEj7Ag8yAqthhgFS rPoQqBLLxo1O8V/eNvL4jG1q0AuOS4HxkK9wTEx+rV5j+fAGKpiFuQDb3cU3AA6wfASQ ZI9/rk4Tnw85+bIrWq9y20Q9pE2aZbUzhKBa8/LVvny8RWytiP/RygmdAMZpYNpLFUBq 9qfU3BMSZE0tacsRrnvIp86O2fPe2QO+vLQmfqNlcqiSOWcVI43VDs6uK4G8GLbDq2ka PmR2x+BA29l5HJopD42tl+rGQDhhgqsS8gtFHeftIiiedgGNtB7b1hDNSe8oUXWuVHDk Ml1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318699; x=1724923499; 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=PzODO6nzCICIUMlgGVk4+Q48fOL7xNsv4iWIgFEFZJQ=; b=T/qiTieFwQJhlliWxCW9luo18roC6sAGXmh9nhChmGt91frlbG4rO4EioSIbCJUysf 7JRT+OWq2XbVgbuHTnzq3YQNpwe4TIQEdruh26PWEicClSc88ubL6f61WK2/+UeSZMsP XUFB0p1ce4jLq7tLKD9MkmMCwxkOssxLxVQnVSGXeOxPwDZdcHiYEbMp4zRh4Rh6s/gR pH503+fZIY0LqW+AKaIB20xsXNzzUgmSqYlH5kiJoYI7+s6Ml1e7sQNRwylDSPP+cZwN JgMaTZHZZ/3tnqL9IOPtGXrwOeTtEEcTNmdDP1JCTRt6NSkDfcsNNqYsvSZ0TMQKTERL /zFw== X-Forwarded-Encrypted: i=1; AJvYcCUEa8pN0sZm/7+U3qYBgdutnJac8hhLZnALr3I/eRYecwHmiCSQajTQ1XxdlItB/g/jQPwEbMYQchSV/CchPPw=@vger.kernel.org X-Gm-Message-State: AOJu0Yw+GAq/Cu7udfkekoXYmu2cuvP6zu8r865A9RDjiON+TOov3UeJ AZBlh/nN0798BgWXp66TuENlyEDZeQKBxjWiwlmGSRH1JDa9V8+UuFYlnOqg X-Google-Smtp-Source: AGHT+IGJDau4qkVoFMHfwEDtn11l++mOLQnCQaK/s1uuqFS684Xa10HpWSz3VYY2R5otiQhAtiU81w== X-Received: by 2002:a17:90a:8cb:b0:2c9:6514:39ff with SMTP id 98e67ed59e1d1-2d5e9dbbb7cmr5467079a91.33.1724318699240; Thu, 22 Aug 2024 02:24: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 98e67ed59e1d1-2d5eb90decasm3548820a91.23.2024.08.22.02.24.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:24:59 -0700 (PDT) From: Tony Ambardar X-Google-Original-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 v2 4/8] libbpf: Support BTF.ext loading and output in either endianness Date: Thu, 22 Aug 2024 02:24:27 -0700 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Tony Ambardar 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 | 3 + tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/libbpf_internal.h | 2 + 4 files changed, 161 insertions(+), 9 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index cf4f7bd7ff5c..cb4ee4ed4b5c 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; @@ -3023,24 +3069,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; @@ -3067,6 +3145,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); } @@ -3087,7 +3166,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; @@ -3120,15 +3204,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) +{ + return (is_host_big_endian() != btf_ext->swapped_endian) ? + BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; +} + +int btf_ext__set_endianness(struct btf_ext *btf_ext, enum btf_endianness endian) +{ + bool is_tgt_big_endian = (endian == BTF_BIG_ENDIAN); + + if (endian != BTF_LITTLE_ENDIAN && endian != BTF_BIG_ENDIAN) + return libbpf_err(-EINVAL); + + btf_ext->swapped_endian = (is_host_big_endian() != is_tgt_big_endian); + + if (!btf_ext->swapped_endian) { + free(btf_ext->data_swapped); + btf_ext->data_swapped = NULL; + } + return 0; +} struct btf_dedup; diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index b68d216837a9..e3cf91687c78 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -167,6 +167,9 @@ LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset); LIBBPF_API struct btf_ext *btf_ext__new(const __u8 *data, __u32 size); LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext); LIBBPF_API const void *btf_ext__raw_data(const struct btf_ext *btf_ext, __u32 *size); +LIBBPF_API enum btf_endianness btf_ext__endianness(const struct btf_ext *btf_ext); +LIBBPF_API int btf_ext__set_endianness(struct btf_ext *btf_ext, + enum btf_endianness endian); LIBBPF_API int btf__find_str(struct btf *btf, const char *s); LIBBPF_API int btf__add_str(struct btf *btf, const char *s); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 8f0d9ea3b1b4..5c17632807b6 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -421,6 +421,8 @@ LIBBPF_1.5.0 { global: btf__distill_base; btf__relocate; + btf_ext__endianness; + btf_ext__set_endianness; bpf_map__autoattach; bpf_map__set_autoattach; bpf_program__attach_sockmap; diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 8cda511a1982..6b0270c83537 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -484,6 +484,8 @@ struct btf_ext { struct btf_ext_header *hdr; void *data; }; + void *data_swapped; + bool swapped_endian; struct btf_ext_info func_info; struct btf_ext_info line_info; struct btf_ext_info core_relo_info; From patchwork Thu Aug 22 09:24: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: 821992 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (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 B478417BB2A; Thu, 22 Aug 2024 09:25:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318706; cv=none; b=SLLJJqZTpqWgzQ8jH/ky9OTHTYSKJkyN9rbohIg9N8Ev0dABFFBY1EUuk5jj0B1TuTEjVQKVbGommSutr66HN0uNkjDWm609vKfXS8jOkpsCLYWb7xVe1AvbAvvWeAqM7v2PFlJYeIZEwk55cgyP3oLHTZ/QvMCZPgltSN1tt6Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318706; c=relaxed/simple; bh=TxA2C+GdCqOmHIAvvr4Uuh0LZXojsqIzQodG1UXIbJw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Zpgg5jBp0qRQss59/qiuKMkSdXpPE3yc4NlL5MJgm94AZChgqJQtLwMJ+iB19qTHWMz8JJYt3cikG7olDSeDjMcMsPSBOSnRKqdF/p6BP1MgmuV8xfpzI0jNkTQAdP5+38f+6tqCGeN453Ip6CdHcN+6zIRuNZkrvMBVcRDwnko= 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=YSWxAXgK; arc=none smtp.client-ip=209.85.216.49 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="YSWxAXgK" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2d3bc043e81so451990a91.2; Thu, 22 Aug 2024 02:25:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724318704; x=1724923504; 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=hIq9mb8QDFOrDtUTONaareYk+pebO6PU/g3DNG9tstM=; b=YSWxAXgKa4b1YmvvacKbsFSjJzbV4LxDZ855qoIEk2W7HlJMEeLtAeUHxkNcihM+iC MNBVQW+c/3PX8kpU1807UXoTeivfohDBtoS5CmnWXuHCmssnBZxYMN9OXGfV4i0OQCd1 SchQqOvPK6zmEJdQ8C+qf+H1OlVOan0q3ZJRyL2cFVZe6Z1CyYBEbkEoEpOzRxESps4t S8wuHVNuhTi3BpP+qgUPH7xYbaIqUFRzF3qKJ+VduPh3xH9g0Iu8zcVIpr3Dilw/c/O5 o45QkPHRaSQBsF7yiL/9CLxi5zfewj/B7LFfDlmhkoGwrW/tq+QVdxYsL6CaBjXmpf3C BDDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318704; x=1724923504; 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=hIq9mb8QDFOrDtUTONaareYk+pebO6PU/g3DNG9tstM=; b=CWKXMcQ/+iq32K3OAba6t57TT++2l6Nd6ZAZ1brmDkxyKyTcUzFOPiFMC9tAGb/fbl CEXb8dpPAziE9Ws6ekwHJC6IjJc2c+bH0AcmuT1ndtbwnQ6fhYPqgy6HIcOfsMhX2Rdd +fwVgJIEUutiTRfODH26M2USmd3tUqDEuvKU7w6fmPrO51NWdYbO46RcaDRuhky6Zoim 6ic6HyGp501vGFDkBF+BXvAqOoVblVCh6ttwaz+n6G+eoyJCKKHCp6BFTOZLn8dKiqPs n7D+Iq9XrRKec1u59UiKznOJBDRL00NVlFhQzYPAYIaoOZEzWxmfiGm6NLBBsWWRGNbA 6/kg== X-Forwarded-Encrypted: i=1; AJvYcCUNyXNhy2Y2F0dSDMLbK1aOjKrhaLALeal3ASF4+AZJEN+HDhig036XJQ7V7tBYMfD22VCuSBQA79MVoBuRAnI=@vger.kernel.org X-Gm-Message-State: AOJu0Yw0c5R85chF/LdXpFHQrogOe70llJJJ8RVsQcBK4XBZVdDW3KfI apXYTTzTb71zN7x9HT4rWS38S18fPgB5O4OXWSJk2YswVOjVjiiuDnvGyuxz X-Google-Smtp-Source: AGHT+IF4NbmvsWWc8JOu4qe9+WWagOMF5F5qnjixbQqeDaZwde0vuaZs3+C9Rzt6XrfJQ25OqL30oA== X-Received: by 2002:a17:90a:6fc1:b0:2cf:c9df:4cc8 with SMTP id 98e67ed59e1d1-2d61728e621mr1501220a91.38.1724318703682; Thu, 22 Aug 2024 02:25:03 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d5eb90decasm3548820a91.23.2024.08.22.02.25.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:25:03 -0700 (PDT) From: Tony Ambardar X-Google-Original-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 v2 5/8] libbpf: Support opening bpf objects of either endianness Date: Thu, 22 Aug 2024 02:24:28 -0700 Message-Id: <3b65982b50a9ca77a13d7a5a07b8b5d37abc477f.1724313164.git.tony.ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Tony Ambardar 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 Thu Aug 22 09:24: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: 821500 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.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 6A5D117C7C7; Thu, 22 Aug 2024 09:25:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318711; cv=none; b=uO90f5/lbdy/vM4+Uf8fF1UctgZoyboUer/Oflu0t0xDJQOnpkXxZDoOpIkXlWZvVuIcHUJUKiY9o6kZJsnv9aKw+Py5jpwLE+vmp7G0+AwNUgZtAIM9TYEboXCsKmoPSjEVdYjfn8BWNlbmoG3FAGG9GWSJt2rUBz+8eKQ5010= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318711; c=relaxed/simple; bh=07KmEz8U1Ki/J/dfw9Y29T9TVvcCa/OYF+krcbEiVcI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qhPL4Baa6V5RgVTuBXmulKb7QG0PZuCaOqBeAAZAQfhUQRr5Lff/wibf5nuXU8FeDlZgGynyc3OYX0GxVIvQbW3EVXIHMzbDI/yoiyorgqWBmOJUHlSzBOTIWSaxwUY5p4Xv4Zs8PDk2M0q12R4BDx2a9K9NK07HqPGg26xlYs0= 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=C5qMdFzJ; arc=none smtp.client-ip=209.85.210.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="C5qMdFzJ" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7143ae1b48fso143920b3a.1; Thu, 22 Aug 2024 02:25:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724318709; x=1724923509; 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=W16ikeO+zpfqJqCTabePrGDL48+AE63HibtRRZxWkk4=; b=C5qMdFzJUnYXavTLedhSnCFNwK6Dt3RsBkQZLTq0W07sHPHP8hii0cdTpC283jKp7Y OlKpewGVdlPl6uCkjSegp4EhL4Ex2r7aKRcuDrJZ2zpDUn8n2iuuP3NWEahmu6goG2Vw /k4GXAHJPUYNxaCIr8GjqWPCBXWZYcP75RZ5B/RLS2X9zbf86F9pgjZqxmZwlQLdB8zv 93XSx/jIGd0u/lzNn1c9ft+2h35ry4wlmsmKBc+dwRCPC2cn1jcAYDu3i+HQfvyyU6AD F4sUgFeYtNYo+O+CKdkU++Hoxc80MZ+HJM2mxtP7zKOaMfrLlLXY61E89FEzfMXnX7J2 QXDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318709; x=1724923509; 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=W16ikeO+zpfqJqCTabePrGDL48+AE63HibtRRZxWkk4=; b=dY7FuWvIK185QRmAY8tDYc4HCpRb00QB5TpGXYV24QXWchA0+awbcGC8Z548ub93Z5 ZJOHA5tWXnGjJdHo1PsTfBPLXleqzmm7YShnGunkq4fIdXMPEB58yebnC3sJ6q7qNN4I otL8Ye1TujbSO89Ml2IsG2s+rMXObsxgqvRrHy3PhHfnCxaXNW4GxLjmo5qBOYK3xopR vYKsKDi8wRqIKstYQjrTydKVQCk2j4ES7Orh1oSspQknGyfiliKbnX77iVu0VUlDiGyv zjNW6j4HqPbYnbDit8xxMUZb+3s8JQawCfkGp3/djytxZ+a1UQkQB6UCQzkZzQtWBVbE e2qg== X-Forwarded-Encrypted: i=1; AJvYcCVjmL7a0MimtNvvEBwXdFABwNCfV+0LCBq6b612s/OC6PajW4QGpCZwp5rwXYFNRKM6YTmDordVvEbF+2ur8Gw=@vger.kernel.org X-Gm-Message-State: AOJu0YwQJY5F9+NMdtfEPxqZUbila6/FMRqumqsgDbbDdRjsngdD3Dry XYn6q+SPseygmlN0G+sRkTpTBUVD1wGMP20a6C4Zb73cE5z4yvrX2ctQ3ZU8 X-Google-Smtp-Source: AGHT+IEphvEhhEPcq69wpQggNS7hr78OA4Ew+Otm25LrLwIu5OhSG6+kCwgpgczlajZ95KzScPIveg== X-Received: by 2002:a05:6a20:439f:b0:1c2:8af6:31c2 with SMTP id adf61e73a8af0-1caeb34ef2fmr1345209637.44.1724318708521; Thu, 22 Aug 2024 02:25: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 98e67ed59e1d1-2d5eb90decasm3548820a91.23.2024.08.22.02.25.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:25:08 -0700 (PDT) From: Tony Ambardar X-Google-Original-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 v2 6/8] libbpf: Support linking bpf objects of either endianness Date: Thu, 22 Aug 2024 02:24:29 -0700 Message-Id: <0511638ed6191f4434be23ed7fb56001ccb5b687.1724313164.git.tony.ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Tony Ambardar 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..9bf218db443e 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 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 Thu Aug 22 09:24: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: 821991 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.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 4483117CA04; Thu, 22 Aug 2024 09:25:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318719; cv=none; b=AJH3hNlrSQc2BwDXKUFiek+mT8hBscUliCHz/ivDBFe0xTPBz85t7SIPfP9sqrsN+9KsW1kDZhB2R8pALdMWT541v+qANOo8EPkTCWKUnjHhx09sYlzOb2uCh0m0v5Xhgd2n6hcCrnAM0+46D2XZzJgwm/jG9SRV+VfLEE1FLB8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318719; c=relaxed/simple; bh=3iba5shuWIPRob4xl4OC8v28mHjKpMh2GQUEY5z+3sY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OQytQJMAawsOEQN2HpkeX+Q9B8d6f/XO8r6FdWGKKagtrmW6jxN5hhhe7+f2J8G47GAKHIdAMnZMx+3xkaz+15FuC62ugZgifZTI1YyPOiBu+kUQJMMJSL8su0KhzRG5JCvfG4WbApxomjDo1tvxF1plYysAmMLIkG5fL252ncg= 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=CJkUf13o; arc=none smtp.client-ip=209.85.215.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="CJkUf13o" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-7c6aee8e8daso418035a12.0; Thu, 22 Aug 2024 02:25:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724318716; x=1724923516; 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=R/2EqU7ePbWpHVQO4hsS/Hu1P6Ii9BBD02yY+gGUd20=; b=CJkUf13oMBXA/lJ150PLqlH9edmgqwYCxfCe9CMVKn1mucshFzk4oQlKF4Bldydiv4 IB9QR0kRAMQKE6Zf1ouk0dA3v8Nm4l7UGMu9LTqP2bPykCpJkPmBr59vOuoynXqIqAKm bokrCuKSfp4p860CoGruipVhsHqz847ZspstFIMOS2pl11pXMzlt9HqCsefRhkwxDhqz dUbeHxdfrgDkbctEeVy3ML4izETuuGBSd6Bh6T1EZ64m93NE+A1bvzypBsiajeZXmnG4 ZEXYk6hjkYclL+hX3wsTJy10aCObDnx285P5XPayIHxe8LrIpHD123jnmH0sln6Wo5K2 5Pow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318716; x=1724923516; 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=R/2EqU7ePbWpHVQO4hsS/Hu1P6Ii9BBD02yY+gGUd20=; b=t7P4PZWexg7oURQjP6Tl3qprih6MYqA53V1oLap8/0rlxu3knpAtWjRoWWZ7ny64Jh LB/NOxdKyaf94t5OMZFtOEsXkIu91Aq4bHwYxX9hTBzV6I/8MsP8ZcyUtPwOl8IjOeG+ 29K3hyyMfMdjN47YMk7ewp64t79UrNzxvQBwDn8x27ejhS67n1KxYGeHKkbrqNoNrJDP JXC7FrCRgx2vMavGGqxgYdTiNHjTh1InaHrSSS0RiFvTO1bJCBu4Ip/y5k1B2b/gUTfV mArrIJugDtzF1qM/oYUO6XlSQ1tg9N5UuJWSH3MjX6T/3DGDZtBjvctKFezFDu8UaZnf G5Xw== X-Forwarded-Encrypted: i=1; AJvYcCWnwiH6PrgzwgM6ygKfIwJip0tj2SUIz845TYHQNlLvLcQboyVIA7itTWs5utMQkZSNqlpgZEtl8XbTGD00kXg=@vger.kernel.org X-Gm-Message-State: AOJu0Yx8mSKbaQTibSFtTEvrtb00QYBNeUAxa3rIqE4URHxS3Hcx2W8Z KDBcHbAK/DirsL7CLWPJ0pM9neQIRyYviH8725X7jgzqaPpgo2TTVQNAGk+a X-Google-Smtp-Source: AGHT+IG0uI8/6be/Wxk3fVJRCm9SwVLlF3b/C3lWzJ651CC/ujMUdANwIzttDWZBYnlq3Wued8voMQ== X-Received: by 2002:a17:90a:7142:b0:2ca:7636:2214 with SMTP id 98e67ed59e1d1-2d6169beddcmr1378874a91.4.1724318716221; Thu, 22 Aug 2024 02:25: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 98e67ed59e1d1-2d5eb90decasm3548820a91.23.2024.08.22.02.25.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:25:15 -0700 (PDT) From: Tony Ambardar X-Google-Original-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 v2 7/8] libbpf: Support creating light skeleton of either endianness Date: Thu, 22 Aug 2024 02:24:30 -0700 Message-Id: <94a6100651e3f61166703cf143f6366b15ee0d21.1724313164.git.tony.ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Tony Ambardar 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..00ed416a2f69 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,28 +495,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; + 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); + 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 +538,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 +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 Thu Aug 22 09:24:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 821499 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 9B2A0157A43; Thu, 22 Aug 2024 09:25:28 +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=1724318730; cv=none; b=Zd+7nitmf1vhWMD1f1hZjvHs13aSfvC38kKoQzuaAuAH9kxHd3raQc+InxIwzYzUAvqYISujz1A+blc0VjWJSXsfW7PhGe4qS899coNodT8MPKv7HGYy8JdKwSUnB52DWG8qL3RV7pBc+i/BuA/gfEOQ2yqyZdN9o8WqDW74c+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724318730; c=relaxed/simple; bh=I8dp+kesygSB9+8F1GpPULPBgIQF3hHl+DnvJFnCgiQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=N1Lp9M27X5oFjkmOnC0ohIL0vEbrRHuu7J35qnVA9lhKZeP+vCjoEWABkvTg+KPB5llmg4FIy7WZ1whHPBidzEQ97cloYGQAkIirV+8oxknJkZ2ayYu4r1Gwipuni3G/lgzQ7GU9fQ/kIYuWlNb0vZUTsFIlNO5PA9vH5gAiv6o= 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=AgBmHHqK; 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="AgBmHHqK" Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-6c5bcb8e8edso417091a12.2; Thu, 22 Aug 2024 02:25:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724318728; x=1724923528; 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=QpEuvKhFfEwio+ubyjOlPYD9dr64SM/zhh4p3oDzBHk=; b=AgBmHHqKmlaFwgi3oSVTVdj4KgInmXAG5hH1wQ/fQpY1liR9xOu5amZlr/a0TXAj3R HXHoNuBvGthjP45AfxsyyB/LZoSB6h2J8SEZRPm8PQ4XxNY1gAlBKgCP9bxcBktd741t fIhVrZwLlP/2gE5ZJLL/K/jPkB49iT0nbdN7iJqC9CxYWL4813AL71NoNpdPRKrKneww /CwVqMVaDeJ5hlPLgPwZ0SBZ/EgEVTKwKhadc96n+EP8bc9Ux7buEV4muJbdXtXgw9cD H1PxZ6EOnLDSm1uFzpBf24HAE9OIGZaXuWcvpUOqezxz0K0tRbVQQbLiMdt6pn0Ot5jx kHFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724318728; x=1724923528; 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=QpEuvKhFfEwio+ubyjOlPYD9dr64SM/zhh4p3oDzBHk=; b=j6rXBYVDhH/NRDlFpyVda+ugvWmCla6UqmZphRYjQPBrZB5ijm7ZVp+6CScHGORupY DkBS4SGlSr2vIg++7RvHVQzW8a0NWy8N8BxJdKjkmmr+KB2dkaYp4WcO5QTEaLK4VLAb w9ieKVwwiJSXl9U+kBnUzvyHqzpAGVYZyGLtEc/xCTnCUOzxXQLkZdJM4nkcZ3e5RFlk BdrXJ4cWMQqUfdI/QfvluBAhZAV8i7wZuELZUoNc64fuNMtgivPa2fLtK5UmUExn6Rnf qzPm1gIIhNmJPJ+KQpxMWmWXtlS1aRjdoH5rsqHq8I8L5AjRNxghvHD9glsIhOzySrCd 67Wg== X-Forwarded-Encrypted: i=1; AJvYcCWC0a+51zD8ZdfyKvBdWQz3GBGD6m56B9HhT4DQi6Ci7mzzeJZn16uTfg31+ryAC6+FYJPP0/zBZq+S1cGmvfc=@vger.kernel.org X-Gm-Message-State: AOJu0YwDgm/lf7h5qQk0+uoJuycKdFdK46m3RHcg6RxO975+XnAlf0Wd EjvzxOPpUxAk1WM97p9B+AGiyU9TnZ2Pv6ghyhuiAgLq/VyGY5uDOJ+tcQPR X-Google-Smtp-Source: AGHT+IE0ocyOXhI1kzh65on43bQLjOXxVjqRZt8SwimkRCelqkpvDRDK2/cT9gH4NW6ZNZ4yGDyFMg== X-Received: by 2002:a17:90a:f601:b0:2d3:b49f:acdd with SMTP id 98e67ed59e1d1-2d616b280famr1381963a91.21.1724318727559; Thu, 22 Aug 2024 02:25:27 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d5eb90decasm3548820a91.23.2024.08.22.02.25.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 02:25:27 -0700 (PDT) From: Tony Ambardar X-Google-Original-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 v2 8/8] selftests/bpf: Support cross-endian building Date: Thu, 22 Aug 2024 02:24:31 -0700 Message-Id: <58f6fcdb52b7a5b9ad173c266910da6f08ebfff6.1724313164.git.tony.ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Tony Ambardar 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 ec7d425c4022..11cc930ae3c5 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -437,6 +437,7 @@ endef IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E -