Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp750452iof; Mon, 6 Jun 2022 11:48:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJythDL/sd292XUhuYHErBKCo+wp777dszirUrw5LDMNS6iHyezNQb159kG4JfjMDjiwwaZX X-Received: by 2002:a63:6c02:0:b0:3fb:a914:e6dd with SMTP id h2-20020a636c02000000b003fba914e6ddmr22103222pgc.101.1654541323660; Mon, 06 Jun 2022 11:48:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654541323; cv=none; d=google.com; s=arc-20160816; b=QXnC5J/VJ0RXxjxBoJXajgnHsB+zP3/J6m9xOUC4o+SNWLI+l+uVC2F26Qj4LFcnyi THSsQCZ8YdbnVohQLrfIh6Yo2Jl75rwY9Fe3N4lqFkXiLBm0djdvHMffVH1R2+Gyd7TP e6VSCdwUBLNQ2jGA5FACTITyz/zZ6Q6E5atjt+a/uJu0mAsZXdKN+fLNG4umGEWWnyve YPMMkFqeYo/n851uP0NXJmseG2cfxq+buvfVS2ZmAbUps22cB+5vB4F0ojXt/h/nu0sn dZ8STE92sObOFlRJcLx7BIXxievr7MQjQP2L0EZVYHNj17Qomg3SfxwV8EKdQAIAmuGY DziQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=97rzWQFE1AtsQDQrkeu4wHy1IeluYAPjOrBN3JDfUAQ=; b=yYMjckXDrszpOkCbnaMvVR6TDq+Y5daFYOiURDgrRXM2652UjGaFuTIaS1nOdjHfw8 ZMMnfa7JTY1+K9LqH/f0TGBb9wtU0knCb9AB3Pq8tCevn343Z53W7YUOlKU9A/fsGYj9 KWrKSD55vdBHVGqNLFTMzFPU+JBCfUw150Yl2Ba7faLSfWdURHOLCNyXY6WB7XSGswqq 80UpqovKisATxI6hLPpC8PzMgLww/yVwN/7X2vpY2vTAdh4popETxVMd5W20FI+2MLkd ZOJjrfblZO0jQuTHKojEfFdzcCb1w9sRwHY7apOsXVqdaF1md9mjK/Ae9c2bHzYQ9iyr Z+9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mpXT18X6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r26-20020a63a55a000000b003fd84399beasi8847929pgu.687.2022.06.06.11.48.31; Mon, 06 Jun 2022 11:48:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mpXT18X6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230274AbiFFSDA (ORCPT + 99 others); Mon, 6 Jun 2022 14:03:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230057AbiFFSC7 (ORCPT ); Mon, 6 Jun 2022 14:02:59 -0400 Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACEEE6FD15; Mon, 6 Jun 2022 11:02:57 -0700 (PDT) Received: by mail-lf1-x131.google.com with SMTP id be31so24545047lfb.10; Mon, 06 Jun 2022 11:02:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=97rzWQFE1AtsQDQrkeu4wHy1IeluYAPjOrBN3JDfUAQ=; b=mpXT18X63OmEoHZ3hFHcWblFdaKwXBAQMhZ+WYqGEfsmLoT3U7sLE124TUStzu6mHq AHfrfXGbrbRGd7o0jM5pWhUB81F0X2DEVOG2YPad83PazQ3jgGmylZKpOBVQ5v/GCJie YJ8IASvmXIEVcuDyD9bm+ZXPPrjihzwNYImxnUs3lakMD4ctX6bRzmGaZqqqXn8t+wCf x0xdYypbguFmnxrDbe9EERWfyglU6pv82vwb/jMpZ8khA2YMfr/S128QhgrVaWbgcUBD 6uTp/Bs4Fezs6xqYOiTWi3wo0X9lXZvU1QRE3thiQaGmPJiGvqtKXHzeJK1GsZXm1+G8 o77w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=97rzWQFE1AtsQDQrkeu4wHy1IeluYAPjOrBN3JDfUAQ=; b=eMhdRNHi/2ikiuFVr/E5WeXJaibVmfGC8hF61mqAESF3tPiKNQXJklxepmDbRwPH0f e1QMGbaklFcimigc06WZvmnyT6n/Sxec0oq5I9yMMG632nN7b4dLGquyMNc/YmA7fIvx ULJvXR5ufdn61V+vEUYEZAe59x5GW+aSIHV0z99SBxNxnDsuVCOknt/ifQ2hftixFNKO MYodHAE3349CQjLRZMPepe+r3aLQS7MfEBNGdZOTAMLxH/hmUV5H1hxKEquW/LmCzhdv LYSKnJ8jI6QZZeA2ZfWOiHYFdso4Xfy4BOOg6nxW8DkOADnWq20ubRu0mgq4SUNhoPtS NVtQ== X-Gm-Message-State: AOAM532/8iJjD5N7aiJhknyzgZ8bB+FFSpUzCxEu9U18PJ56hsPgjXVP 4cY5vvKXx+/Q25kCHr87NT8uYTmz+0rdlukqVjw= X-Received: by 2002:a05:6512:b2a:b0:479:12f5:91ba with SMTP id w42-20020a0565120b2a00b0047912f591bamr13971198lfu.443.1654538574348; Mon, 06 Jun 2022 11:02:54 -0700 (PDT) MIME-Version: 1.0 References: <20220606132741.3462925-1-james.hilliard1@gmail.com> In-Reply-To: <20220606132741.3462925-1-james.hilliard1@gmail.com> From: Andrii Nakryiko Date: Mon, 6 Jun 2022 11:02:42 -0700 Message-ID: Subject: Re: [PATCH 1/1] libbpf: fix broken gcc pragma macros in bpf_helpers.h/bpf_tracing.h To: James Hilliard Cc: bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , "open list:BPF (Safe dynamic programs and tools)" , open list Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 On Mon, Jun 6, 2022 at 6:28 AM James Hilliard wrote: > > It seems the gcc preprocessor breaks unless pragmas are wrapped > individually inside macros. > > Fixes errors like: > error: expected identifier or '(' before '#pragma' > 106 | SEC("cgroup/bind6") > | ^~~ > > error: expected '=', ',', ';', 'asm' or '__attribute__' before '#pragma' > 114 | char _license[] SEC("license") = "GPL"; > | ^~~ > We've been using this macro in this form for a while with no errors. How do you get these errors in the first place? _Pragma is supposed to be a full equivalent of #pragma specifically to be able to be used in macros, so these work-arounds shouldn't be necessary. Let's first try to root cause this. > Signed-off-by: James Hilliard > --- > tools/lib/bpf/bpf_helpers.h | 26 ++++++++++++++------------ > tools/lib/bpf/bpf_tracing.h | 26 ++++++++++++++------------ > 2 files changed, 28 insertions(+), 24 deletions(-) > > diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h > index fb04eaf367f1..6d159082727d 100644 > --- a/tools/lib/bpf/bpf_helpers.h > +++ b/tools/lib/bpf/bpf_helpers.h > @@ -22,11 +22,13 @@ > * To allow use of SEC() with externs (e.g., for extern .maps declarations), > * make sure __attribute__((unused)) doesn't trigger compilation warning. > */ > +#define __gcc_helpers_pragma(x) _Pragma(#x) > +#define __gcc_helpers_diag_pragma(x) __gcc_helpers_pragma("GCC diagnostic " #x) > #define SEC(name) \ > - _Pragma("GCC diagnostic push") \ > - _Pragma("GCC diagnostic ignored \"-Wignored-attributes\"") \ > + __gcc_helpers_diag_pragma(push) \ > + __gcc_helpers_diag_pragma(ignored "-Wignored-attributes") \ > __attribute__((section(name), used)) \ > - _Pragma("GCC diagnostic pop") \ > + __gcc_helpers_diag_pragma(pop) > > /* Avoid 'linux/stddef.h' definition of '__always_inline'. */ > #undef __always_inline > @@ -215,10 +217,10 @@ enum libbpf_tristate { > static const char ___fmt[] = fmt; \ > unsigned long long ___param[___bpf_narg(args)]; \ > \ > - _Pragma("GCC diagnostic push") \ > - _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ > + __gcc_helpers_diag_pragma(push) \ > + __gcc_helpers_diag_pragma(ignored "-Wint-conversion") \ > ___bpf_fill(___param, args); \ > - _Pragma("GCC diagnostic pop") \ > + __gcc_helpers_diag_pragma(pop) \ > \ > bpf_seq_printf(seq, ___fmt, sizeof(___fmt), \ > ___param, sizeof(___param)); \ > @@ -233,10 +235,10 @@ enum libbpf_tristate { > static const char ___fmt[] = fmt; \ > unsigned long long ___param[___bpf_narg(args)]; \ > \ > - _Pragma("GCC diagnostic push") \ > - _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ > + __gcc_helpers_diag_pragma(push) \ > + __gcc_helpers_diag_pragma(ignored "-Wint-conversion") \ > ___bpf_fill(___param, args); \ > - _Pragma("GCC diagnostic pop") \ > + __gcc_helpers_diag_pragma(pop) \ > \ > bpf_snprintf(out, out_size, ___fmt, \ > ___param, sizeof(___param)); \ > @@ -264,10 +266,10 @@ enum libbpf_tristate { > static const char ___fmt[] = fmt; \ > unsigned long long ___param[___bpf_narg(args)]; \ > \ > - _Pragma("GCC diagnostic push") \ > - _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ > + __gcc_helpers_diag_pragma(push) \ > + __gcc_helpers_diag_pragma(ignored "-Wint-conversion") \ > ___bpf_fill(___param, args); \ > - _Pragma("GCC diagnostic pop") \ > + __gcc_helpers_diag_pragma(pop) \ > \ > bpf_trace_vprintk(___fmt, sizeof(___fmt), \ > ___param, sizeof(___param)); \ > diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h > index 01ce121c302d..e08ffc290b3e 100644 > --- a/tools/lib/bpf/bpf_tracing.h > +++ b/tools/lib/bpf/bpf_tracing.h > @@ -422,16 +422,18 @@ struct pt_regs; > * This is useful when using BPF helpers that expect original context > * as one of the parameters (e.g., for bpf_perf_event_output()). > */ > +#define __gcc_tracing_pragma(x) _Pragma(#x) > +#define __gcc_tracing_diag_pragma(x) __gcc_tracing_pragma("GCC diagnostic " #x) > #define BPF_PROG(name, args...) \ > name(unsigned long long *ctx); \ > static __attribute__((always_inline)) typeof(name(0)) \ > ____##name(unsigned long long *ctx, ##args); \ > typeof(name(0)) name(unsigned long long *ctx) \ > { \ > - _Pragma("GCC diagnostic push") \ > - _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ > + __gcc_tracing_diag_pragma(push) \ > + __gcc_tracing_diag_pragma(ignored "-Wint-conversion") \ > return ____##name(___bpf_ctx_cast(args)); \ > - _Pragma("GCC diagnostic pop") \ > + __gcc_tracing_diag_pragma(pop) \ > } \ > static __attribute__((always_inline)) typeof(name(0)) \ > ____##name(unsigned long long *ctx, ##args) > @@ -462,10 +464,10 @@ static __attribute__((always_inline)) typeof(name(0)) \ > ____##name(struct pt_regs *ctx, ##args); \ > typeof(name(0)) name(struct pt_regs *ctx) \ > { \ > - _Pragma("GCC diagnostic push") \ > - _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ > + __gcc_tracing_diag_pragma(push) \ > + __gcc_tracing_diag_pragma(ignored "-Wint-conversion") \ > return ____##name(___bpf_kprobe_args(args)); \ > - _Pragma("GCC diagnostic pop") \ > + __gcc_tracing_diag_pragma(pop) \ > } \ > static __attribute__((always_inline)) typeof(name(0)) \ > ____##name(struct pt_regs *ctx, ##args) > @@ -486,10 +488,10 @@ static __attribute__((always_inline)) typeof(name(0)) \ > ____##name(struct pt_regs *ctx, ##args); \ > typeof(name(0)) name(struct pt_regs *ctx) \ > { \ > - _Pragma("GCC diagnostic push") \ > - _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ > + __gcc_tracing_diag_pragma(push) \ > + __gcc_tracing_diag_pragma(ignored "-Wint-conversion") \ > return ____##name(___bpf_kretprobe_args(args)); \ > - _Pragma("GCC diagnostic pop") \ > + __gcc_tracing_diag_pragma(pop) \ > } \ > static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args) > > @@ -520,10 +522,10 @@ ____##name(struct pt_regs *ctx, ##args); \ > typeof(name(0)) name(struct pt_regs *ctx) \ > { \ > struct pt_regs *regs = PT_REGS_SYSCALL_REGS(ctx); \ > - _Pragma("GCC diagnostic push") \ > - _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ > + __gcc_tracing_diag_pragma(push) \ > + __gcc_tracing_diag_pragma(ignored "-Wint-conversion") \ > return ____##name(___bpf_syscall_args(args)); \ > - _Pragma("GCC diagnostic pop") \ > + __gcc_tracing_diag_pragma(pop) \ > } \ > static __attribute__((always_inline)) typeof(name(0)) \ > ____##name(struct pt_regs *ctx, ##args) > -- > 2.25.1 >