From patchwork Tue Oct 3 19:14:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 13407920 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 D85903CCED for ; Tue, 3 Oct 2023 19:15:08 +0000 (UTC) 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 ED94E9E for ; Tue, 3 Oct 2023 12:15:06 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d816fa2404aso1573908276.0 for ; Tue, 03 Oct 2023 12:15:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696360506; x=1696965306; darn=vger.kernel.org; h=cc:to:from:subject:mime-version:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=DJOW1cRndRnD6u1zoIC+jIQu2Yw55MSP06y/ktBFUbM=; b=yNyvNMbSL0y5xjTXnhme9KSZR05SSUpDq5O0ghbV21+Fx8LCrsRixX8ODc7jCv9jB8 Y8HZqIhN6GTh87Pzkx/1NC2BA8/fnHBtMTgLM27TdHSJugGQ1GmJZfJHVL9gpnDlM9r3 EFoXWrT3np/HMs4cfDdgfgX+mIBUKgcfJW5WDscfIkp58kRftvJEFVBzM1SexfqWuwyE 5P9ky4qOYNaDb3cLAF8mh4J0haQxXdT4lXXgT2AT3uch/anPsFqv4VzQJ9u3zmC/7g2W 8EwmEUQEsBH2BhqJbDmokvLYTlEbN3TZdI1m8lcmEaQCd/lpNy6fVQwyKI/bF3mw3xPJ 8wow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696360506; x=1696965306; h=cc:to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=DJOW1cRndRnD6u1zoIC+jIQu2Yw55MSP06y/ktBFUbM=; b=Ewm+NyeQT1xN668gDqpxM1ERb0PZeeOYTvK/ctwPD07A9qpOBqY2NKNECxpc1InjpT AUoRiu3B0oPPG2iBLhpCxF8uGlPqxXADa41w9gug6XrmHRyHW7lWnv+CJOFD+9fPV1c7 k/sdB7Pi/g3CzYHm0MQ7pJgXkdEaacKuZ0H8AvkJx9zaaGuoT26bjR4jJEmAMc68MdOU +cMeXqQUytxIAIEE28hoGSXj1Dq9pOCxQPFlWfOet/bt8Q0w1WqIu84Rf+FeZ3+OZBf0 4J2zlwx2ez0b0kTXd85zvdFG5brJvZ9eeV6z79iOo2EJx89C1mSquyiSZmB1eZpVlQDy l+qA== X-Gm-Message-State: AOJu0Ywaj0eMONSmVl/gqyXoJJcB2O/hKt8EoYgB3scAKpFboKIVW1vh Z8cfgan73MR1ECG4vZZ0nNO6a+0SuJJk X-Google-Smtp-Source: AGHT+IGYcOJgNvoSVWzy2L5c6hkFXm1/Pg3MYXRBpB1h+5OFOsqbKD3loQoJmupL6OgyShodwAT3B9/Rnq1p X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:51d:1f25:c2d8:4514]) (user=irogers job=sendgmr) by 2002:a25:db4b:0:b0:d77:bcce:eb11 with SMTP id g72-20020a25db4b000000b00d77bcceeb11mr2196ybf.10.1696360506122; Tue, 03 Oct 2023 12:15:06 -0700 (PDT) Date: Tue, 3 Oct 2023 12:14:11 -0700 Message-Id: <20231003191412.3171385-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.582.g8ccd20d70d-goog Subject: [PATCH v2 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 , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ian Rogers , Alan Maguire 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_NONE, 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 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 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 2883660d6b67..b8ebcee9bc56 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 = (void *)%2$s__elf_bytes(&s->data_sz); \n\ + s->data = (void *)%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 Tue Oct 3 19:14:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 13407921 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 30F4F3CCED for ; Tue, 3 Oct 2023 19:15:10 +0000 (UTC) Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA1B5AB for ; Tue, 3 Oct 2023 12:15:09 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-59c09bcf078so20975407b3.1 for ; Tue, 03 Oct 2023 12:15:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696360509; x=1696965309; 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=6N4htVJIGGb5h3erLQS3Hd93PuTJER9wVr3iHSknNQY=; b=UFcwdhuuCbyuM9r8KJhOm8K7BhN5+pS+CtOGM3l/0IvsEnA1tiC2mnu9jFWkcHE/Kb TWEYpPbi2vdIyHbih0y68SHVGdX6K2QTn+EQWvNGfdUAInEbMZg0SHy/cTNiZCum7tXg mQFUVSoBm2LnKHNel8V6j1Otww5Tcp31ZCaws2QrQgLBbhg/k6kcfND8goBaLj1wwlk6 pRXsQtmbmzdiNHPnjbBbqbfZnnYwgljubGwP8Czu4QITDq2qq+2KLPxTddEvDnACl1Jc WtufP0K18NXD+QJtLRj6uwJm7lMiVYV1coCOjKbBac1DZXa69AIb9LUuz23i6K45F5iu /gLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696360509; x=1696965309; 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=6N4htVJIGGb5h3erLQS3Hd93PuTJER9wVr3iHSknNQY=; b=kYEDoGkqLw3rZA2iQmmTXZwQ7QMa2GM/8N/MBvr0e5x+L293/iJg7v+pBAh29f16TM cQPe4gSLb33dNNO18viQVkUUkyHemvzWOqLEjHi+4PMAX+jUQ7KCbp+9gGwwfjIX1+cv Qp18rVJhi0wPgm4KWbP466z3TLVsb+5o/rnPexF42/gLRTTHJlwbdq1yWJG80WVunh2p KhENzn52xBFCQc4Sh2Z8XD88/K0xHvlRi2DO2oMi/ttLSMVg3kfxzDET+toJQa3usPLW vjgLEJAl33atXSo0YrogSRpFkt8teKzQ/alH1nY4t3otNusonAd2m7knuFzx/E8VSL+/ nd6Q== X-Gm-Message-State: AOJu0Yw7n2NWYV53gLmDRLqcAtTF9CQbZ7hZp1v7ZSdth3cUib6mbZOS BKLZoiaWfuZptRH94V8uci/363IekTW/ X-Google-Smtp-Source: AGHT+IEYZ1pBWCmXLN1j2N35fPF2rv8nARnMlWixIYVn+j22HysMgdNODZ6bnzSYzPGqFXn01/UUVNBsXHhZ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:51d:1f25:c2d8:4514]) (user=irogers job=sendgmr) by 2002:a81:d444:0:b0:59b:ebe0:9fcd with SMTP id g4-20020a81d444000000b0059bebe09fcdmr9061ywl.7.1696360508657; Tue, 03 Oct 2023 12:15:08 -0700 (PDT) Date: Tue, 3 Oct 2023 12:14:12 -0700 In-Reply-To: <20231003191412.3171385-1-irogers@google.com> Message-Id: <20231003191412.3171385-2-irogers@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231003191412.3171385-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.582.g8ccd20d70d-goog Subject: [PATCH v2 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 , bpf@vger.kernel.org, linux-kernel@vger.kernel.org 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_NONE, 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 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 --- I was unable to find a test case for this part of the codegen using Linux perf and libbpf tools code bases. --- 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 b8ebcee9bc56..e9a59b254c66 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;