From patchwork Sat Oct 7 04:44:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 13412188 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 959DE39D for ; Sat, 7 Oct 2023 04:45:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gJmQsX4A" Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BF33BF for ; Fri, 6 Oct 2023 21:45:03 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d81e9981ff4so3609540276.3 for ; Fri, 06 Oct 2023 21:45:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696653902; x=1697258702; darn=vger.kernel.org; h=cc:to:from:subject:mime-version:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=GMqnFgEnYGV7L+VhDywAwYo6eyJsbJGDIIS4AN+4+k0=; b=gJmQsX4ASu/vmfsVM3oqNJuHpQXc7XAfWkC2eiQ4RR/oB+LOZj+vGfsxCXn7z7xVwy O6QYv3lw0AKDxTPHbJ1GLqpITn+hkUK/t4mvgFBLV93YwE6aGo1grDQiazSP01S6/sq0 ze6QHX67Vp1sTOAfV5aR7CpUf84vxwT/ufGqFj2QwbxMygO8SLZW/UM6GP8t8EL3yUnb V68LvzDKT8f57pWaoGLVdiTokom8uPdIjpN5pkx4cwwou5VHPGv1YvWFP7frbqG6Aldn xu6TxQQHGfnd4rQPwHWa6CBffAu978hQjb3sIWmixUHYMw/laKxmj6JIYTZ/9cvwIWdW OG3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696653902; x=1697258702; h=cc:to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=GMqnFgEnYGV7L+VhDywAwYo6eyJsbJGDIIS4AN+4+k0=; b=XSyh6MuKI8Zx85pQEr3vhM5JhylDaEXjxd6Ct7zjXSUjOZ/qpAvfEDkJzex0GkkIco 9XJ6w2+ukldi8qUshD/oN+CPwIj4gFv8v8vTmklJo1Ws0LTgFqMIaOAlryPT0CiquhaT Y3g+mxjSkbGTUedQbd9WP4tj9SRaDnnHvpafuzSipSSjmQW09AcLcw0cUQcW2AgUbFmq yMCzmNb3jRLAnAg+a2VJV4OHQQApNNmEFNw8FTQ0D8vcz7WFAWaLhETkDMw5ntXMXbL0 MNG+7D12obV8P9Ubt6PNsJ0D1QUr+H2QRU63TnrNvhNUnpcA8bnVXJJkXBeI2/ijmwgU uioQ== X-Gm-Message-State: AOJu0Yw1kDh+osRYgF7OSfZJg3Iecg1mXTXUEthTMf6IMzqIX7vjwVTI 2PynEauR4BFbsIGxosayQGy9gJP/6jlY X-Google-Smtp-Source: AGHT+IGqsoSCI+X+R3Da3bNooRUWbQL/a6S4rxNuVu2HDFeQLf3tBnt4ztfQwRKhCpzS6Mtmexsna3XbgGof X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:7031:b36b:6c77:bfd0]) (user=irogers job=sendgmr) by 2002:a25:9390:0:b0:d07:7001:495b with SMTP id a16-20020a259390000000b00d077001495bmr161858ybm.11.1696653902279; Fri, 06 Oct 2023 21:45:02 -0700 (PDT) Date: Fri, 6 Oct 2023 21:44:38 -0700 Message-Id: <20231007044439.25171-1-irogers@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.609.gbb76f46606-goog Subject: [PATCH v6 1/2] bpftool: Align output skeleton ELF code From: Ian Rogers To: Quentin Monnet , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Matt Smith , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Alan Maguire Cc: Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net libbpf accesses the ELF data requiring at least 8 byte alignment, however, the data is generated into a C string that doesn't guarantee alignment. Fix this by assigning to an aligned char array. Use sizeof on the array, less one for the \0 terminator, rather than generating a constant. Fixes: a6cc6b34b93e ("bpftool: Provide a helper method for accessing skeleton's embedded ELF data") Signed-off-by: Ian Rogers Acked-by: Quentin Monnet Reviewed-by: Alan Maguire --- tools/bpf/bpftool/gen.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index 04c47745b3ea..882bf8e6e70e 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -1209,7 +1209,7 @@ static int do_skeleton(int argc, char **argv) codegen("\ \n\ \n\ - s->data = %2$s__elf_bytes(&s->data_sz); \n\ + s->data = %1$s__elf_bytes(&s->data_sz); \n\ \n\ obj->skeleton = s; \n\ return 0; \n\ @@ -1218,12 +1218,12 @@ static int do_skeleton(int argc, char **argv) return err; \n\ } \n\ \n\ - static inline const void *%2$s__elf_bytes(size_t *sz) \n\ + static inline const void *%1$s__elf_bytes(size_t *sz) \n\ { \n\ - *sz = %1$d; \n\ - return (const void *)\"\\ \n\ - " - , file_sz, obj_name); + static const char data[] __attribute__((__aligned__(8))) = \"\\\n\ + ", + obj_name + ); /* embed contents of BPF object file */ print_hex(obj_data, file_sz); @@ -1231,6 +1231,9 @@ static int do_skeleton(int argc, char **argv) codegen("\ \n\ \"; \n\ + \n\ + *sz = sizeof(data) - 1; \n\ + return (const void *)data; \n\ } \n\ \n\ #ifdef __cplusplus \n\ From patchwork Sat Oct 7 04:44:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 13412189 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1AE155252 for ; Sat, 7 Oct 2023 04:45:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qBv3h5ZS" Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 722ACBD for ; Fri, 6 Oct 2023 21:45:05 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9299cac11aso2962912276.2 for ; Fri, 06 Oct 2023 21:45:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696653904; x=1697258704; darn=vger.kernel.org; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yPsPdGtdKQ/v+DaiUF2STLjrD0JxItabAMPBxz4xd3o=; b=qBv3h5ZSjnKAZMcIR3rEeG8D9YBorLBnlRnH+5KnfHKBS/njECi+rzSr14+qRGsklf OA8RdosWCQCLkCXcBxB8kcS3RN5zjO+cXmz1DT6eI0Xkr53FJvXzsvxMrDHnQXYlSdnG MKYAD2nbqyZ61Nlbo/KyIuiCTyRoPE78HokmLxZlwM9wzigPoFzhzm4dHcA2vSZDV7lc BVQKApSCQEyfk7lHyBTflDKh88lfNvX/GjnDknT6gQFr7AaaybrEpok9ZUqwtW2m7iNx ets0NELxQBxspjtd53+OqhOPGcULjXiFdNz3WA07xBLO5nWk11MYLV2DXAS0cHeBQLAN BYsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696653904; x=1697258704; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yPsPdGtdKQ/v+DaiUF2STLjrD0JxItabAMPBxz4xd3o=; b=gYl9gaJhk7jLPjsqYI75QFmFEiD9zQpJQP70bI0mHzQNaH2lOhL77sWyub9dz5hLLS ZS4BowFtLUcWuuT9en2ps+kuYPBuXWT0ntCCSG1IpMvEf5SosG6kDxvBF54P03KCYvKB wR857KJnf0PvRW28rtLZ1QgS1QOlu1g3uZHCQ8SAKJXI1ZHVkbQJMOsJg4JfUcAP/0ka 7m+1wb7X/nZ8O6Z/KZl4P0uLcSqpyW6Qz8r1Fx9qf6t3WvxXHnuzc5AVDYwyghuDuZki tGEfufLYghAEaLOzPQleEd/bRxVj0IbG9CmheMlD4gq8kXwiPIr4JQE7dpnxVX1zisqg AlvA== X-Gm-Message-State: AOJu0YziiNGvadFZrcVhdK4XCp6Bob5jkOLekK5RcOTXiqfJvPVJD3qp 5zRgzJ1nzVhxo1BKNuMEcU2hsgYWM1NO X-Google-Smtp-Source: AGHT+IHzbgs54ou2YatAT0qKH8s7DIqxUdn8GunFexNWDbAOBMS09tyfscwUD2Nf5+CdRIxy28Y311Twj5CM X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:7031:b36b:6c77:bfd0]) (user=irogers job=sendgmr) by 2002:a25:248:0:b0:d8c:cc9b:81cc with SMTP id 69-20020a250248000000b00d8ccc9b81ccmr148880ybc.3.1696653904627; Fri, 06 Oct 2023 21:45:04 -0700 (PDT) Date: Fri, 6 Oct 2023 21:44:39 -0700 In-Reply-To: <20231007044439.25171-1-irogers@google.com> Message-Id: <20231007044439.25171-2-irogers@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231007044439.25171-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.609.gbb76f46606-goog Subject: [PATCH v6 2/2] bpftool: Align bpf_load_and_run_opts insns and data From: Ian Rogers To: Quentin Monnet , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Matt Smith , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Alan Maguire Cc: Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net A C string lacks alignment so use aligned arrays to avoid potential alignment problems. Switch to using sizeof (less 1 for the \0 terminator) rather than a hardcode size constant. Signed-off-by: Ian Rogers Acked-by: Quentin Monnet --- tools/bpf/bpftool/gen.c | 43 ++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index 882bf8e6e70e..ee3ce2b8000d 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -708,17 +708,22 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h codegen("\ \n\ - skel->%1$s = skel_prep_map_data((void *)\"\\ \n\ - ", ident); + { \n\ + static const char data[] __attribute__((__aligned__(8))) = \"\\\n\ + "); mmap_data = bpf_map__initial_value(map, &mmap_size); print_hex(mmap_data, mmap_size); codegen("\ \n\ - \", %1$zd, %2$zd); \n\ - if (!skel->%3$s) \n\ - goto cleanup; \n\ - skel->maps.%3$s.initial_value = (__u64) (long) skel->%3$s;\n\ - ", bpf_map_mmap_sz(map), mmap_size, ident); + \"; \n\ + \n\ + skel->%1$s = skel_prep_map_data((void *)data, %2$zd,\n\ + sizeof(data) - 1);\n\ + if (!skel->%1$s) \n\ + goto cleanup; \n\ + skel->maps.%1$s.initial_value = (__u64) (long) skel->%1$s;\n\ + } \n\ + ", ident, bpf_map_mmap_sz(map)); } codegen("\ \n\ @@ -733,32 +738,30 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h { \n\ struct bpf_load_and_run_opts opts = {}; \n\ int err; \n\ - \n\ - opts.ctx = (struct bpf_loader_ctx *)skel; \n\ - opts.data_sz = %2$d; \n\ - opts.data = (void *)\"\\ \n\ + static const char opts_data[] __attribute__((__aligned__(8))) = \"\\\n\ ", - obj_name, opts.data_sz); + obj_name); print_hex(opts.data, opts.data_sz); codegen("\ \n\ \"; \n\ + static const char opts_insn[] __attribute__((__aligned__(8))) = \"\\\n\ "); - - codegen("\ - \n\ - opts.insns_sz = %d; \n\ - opts.insns = (void *)\"\\ \n\ - ", - opts.insns_sz); print_hex(opts.insns, opts.insns_sz); codegen("\ \n\ \"; \n\ + \n\ + opts.ctx = (struct bpf_loader_ctx *)skel; \n\ + opts.data_sz = sizeof(opts_data) - 1; \n\ + opts.data = (void *)opts_data; \n\ + opts.insns_sz = sizeof(opts_insn) - 1; \n\ + opts.insns = (void *)opts_insn; \n\ + \n\ err = bpf_load_and_run(&opts); \n\ if (err < 0) \n\ return err; \n\ - ", obj_name); + "); bpf_object__for_each_map(map, obj) { const char *mmap_flags;