Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2856367rdb; Wed, 4 Oct 2023 13:44:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF0KtYMuK4AVQf4KSPwNlIFFwqV4yASt3dfx6aq8GQG3C8yN79K9GJ82wuL/tztR4NWbSRI X-Received: by 2002:a17:902:cec4:b0:1c3:e2eb:f79d with SMTP id d4-20020a170902cec400b001c3e2ebf79dmr1067554plg.8.1696452251800; Wed, 04 Oct 2023 13:44:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696452251; cv=none; d=google.com; s=arc-20160816; b=VfSAeL0E6wJfqB6WIwcURArvhs8+RH8ChpBxePrFRQDXv8Q7tuest5xVjeOZ10SMXg 5TxJfLhfJQvIJ+1/915ET6ezu+fxaedhR05IgBgspiHhl/DpadA9UgA8yjAn/VGkjJRl P3kUrSMDVM9wnr4lVy9e0yx7qAKJ2sOYgAxJOCF2d1DC5c76zIoLh11bexcMi7j4+SW0 KEm3mD7WRaSdDpZyqADBN0ubP0xMnZXPAekU7AMvrudhnWMZ5Qiw0pCT9ttIK8x8qXtT HKN3muQ/ZiGaLkahb5p5clvIwQ8hw5SJFM0dqXXLrMWWKqov2ocEec7cL4MhrDz0m4lC tc7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=62TaHeKznYGJNCCBRaj8AgsGHCErkYyX8npqYRMhtgg=; fh=G1eh9qgGQajdskh7w5iKgrOA0JnWbVtesv5+BE/GOUM=; b=w9F7R5tIl1oboKACvDf3bg0LTSZC+IF89xTGCv4+Foil84jVc6FSthL+GXR3HCIsOo FDLrbEyJHwqqd3BHdstxrwgnDY1OTHSsHHdqzkfCqZ78jWhLbqQM8JoLTlWGBfngP+NQ Sl5cHk8o9wQ/knkY/xcyO+qaJSdkLMtPqlbqno66kLgTBGKjUDVMpq3FkivdktApgBvE 5peyFlNONN/7C82fT9s8U8tY3UCKDPcwW48r9PW2XcOz4soeCBYQZiOXizaIskDp7JNg ni1H2e/BB8S4utdbmr8htLs8gdj4leYwVF4Jpu7mdus1mc2aa16BNdR2/MEbTSXWwJUm lyRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="xoJ/4GCC"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id q193-20020a632aca000000b00584b3e195bdsi4585894pgq.58.2023.10.04.13.44.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 13:44:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="xoJ/4GCC"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 031B982915A7; Wed, 4 Oct 2023 13:44:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243772AbjJDUoH (ORCPT + 99 others); Wed, 4 Oct 2023 16:44:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243764AbjJDUoD (ORCPT ); Wed, 4 Oct 2023 16:44:03 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B785BF for ; Wed, 4 Oct 2023 13:43:57 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d7ec535fe42so400846276.1 for ; Wed, 04 Oct 2023 13:43:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696452236; x=1697057036; 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=62TaHeKznYGJNCCBRaj8AgsGHCErkYyX8npqYRMhtgg=; b=xoJ/4GCCvARflY83Xzl/+LuHonxdBfiLWf8Cf5mFmzqP1HYL2wBDu/L84XIvxm0n+z NWYTYk0L1qh/XQ0/2H3rDiw1Yd6cjgogPUS24mQJyCIsosU1Ob846qmhfJUOdrCCfc9k 1M+Ss139+t/sfmDKi5atuHFpBzQTHoj+ZmZWjdfnXVMr1nkmDeK2fga7D3aaSum4wHAs ZTpYk/0k9vvkOolwmtV2wj444G4bojJwVR/zhBMNaBNPGrX89xo1TMBRBgbmD+ylXQcJ i5ZRtCHQAkJGwf7lv7WAjidWW65CvlkRuHs1ek1Aa/MWVc4fuOgKoz/lHIuA4sVhwyUd pPnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696452236; x=1697057036; 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=62TaHeKznYGJNCCBRaj8AgsGHCErkYyX8npqYRMhtgg=; b=Wf1m5n8fQPmo/qgz2to2d8+d35Z4AdTm55aBCgd5dkdyZHbU0lUGLNDphOYugz+j8U 9drrRI5bOTEjMS069rOypqYVHH22HwW2IEMYMobpENlC9Z6J39HB+qoQGM8YTOz4/ekw e3jwWS5vuU1fQd8OPS2kO/KHf5lev/PtspBhVEE2f7MJay30YzeXveeeIdK5YlYddd87 +wmVKq0Zj4/6c/vKqoVbvW7STGixfJzpz/ekDG+ZXPV1jnMzNO/uoJGYUm7z9ZS9we8x 0Sb8rLQnlTFQ3Dq3fF5lXD5WRguTpb6EJvbu2Fsgq1YjzHT65uAb9p84syvruhE6zyyK FZ+A== X-Gm-Message-State: AOJu0Yzhpcssz5O1gYhmwOyuk1fjydOqIfV/e/n6oIej4A2Ta6NxV2af 09vuQadyzOzJQPbJCAuE7E2niHWCwXuH X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:f42:a09e:8ee2:1491]) (user=irogers job=sendgmr) by 2002:a25:b18f:0:b0:d86:55b7:97b0 with SMTP id h15-20020a25b18f000000b00d8655b797b0mr53834ybj.9.1696452236615; Wed, 04 Oct 2023 13:43:56 -0700 (PDT) Date: Wed, 4 Oct 2023 13:43:34 -0700 In-Reply-To: <20231004204334.3465116-1-irogers@google.com> Message-Id: <20231004204334.3465116-2-irogers@google.com> Mime-Version: 1.0 References: <20231004204334.3465116-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.609.gbb76f46606-goog Subject: [PATCH v3 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 Content-Type: text/plain; charset="UTF-8" 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,URIBL_BLOCKED, 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 04 Oct 2023 13:44:11 -0700 (PDT) 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 --- tools/bpf/bpftool/gen.c | 47 ++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index b8ebcee9bc56..8c36c96cd692 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -408,8 +408,8 @@ static void codegen(const char *template, ...) /* skip baseline indentation tabs */ for (n = skip_tabs; n > 0; n--, src++) { if (*src != '\t') { - p_err("not enough tabs at pos %td in template '%s'", - src - template - 1, template); + p_err("not enough tabs at pos %td in template '%s'\n'%s'", + src - template - 1, template, src); free(s); exit(-1); } @@ -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; -- 2.42.0.609.gbb76f46606-goog