Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp2296715rwn; Fri, 9 Sep 2022 11:18:57 -0700 (PDT) X-Google-Smtp-Source: AA6agR5Uh64yDM4ZjS6rFVpCGeTzwU2Sdkv6iMR+0l7M4cGglJyy+Tt5PVt+mQwq5b34dREZPCWs X-Received: by 2002:a17:90a:28a5:b0:200:43bc:5728 with SMTP id f34-20020a17090a28a500b0020043bc5728mr10971669pjd.188.1662747537268; Fri, 09 Sep 2022 11:18:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662747537; cv=none; d=google.com; s=arc-20160816; b=sElS+9MWvWu5/zoXD4fGNROYqvy58YGsoq80nWHCKLuaYl1kLklM4KRGXBaOmDRJtB gYr5mhibG3P+OxjQoDaiW4rKxAF+qaPdY7YsdsAz3OVyOgAK71sFQhs6UZjLaYWVvvrL ItTCy6vQrGARhr/5ByPA9Eua+7LWySv1A5xXkhjSS7XPUSYYdTlAcsDMvK78dRLDrMRe sIWErWFj99gtbC20EdqMEU4LbtTuW2e2PsOCzyHDv2Mb37Xt59lvqLSxq2EMvF7yiCC9 QPVd9EsougpKQ5m5t3ZqSyCT92545bnhKXZ2T/wmwyRZcGhegncjD6B1UgMC2UqTrsC2 EQKQ== 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=PRi5c+744JgXz7r9GuZCijMImqB0eIkuk0+iSaMrpvM=; b=DtjzzTt6V1fVfvEmxmQX4W/YTW/SQAY7icxfp7yuYsXbiJSC1vE+yUADmVmix+rY5q DzSHi/TX1gr6dbLcn/DpsP3Utqzs3P6ixzEQW6364kOP7Gq7XqhcH6yb5OWfPG0MARV4 YuEV848S1y5Ip0GuX6qI0zJinH1zLGL7rvhSjP24TNI2nP8mUyBLSvRRdASc+WW9WPV4 JvVmzHtNDGb0ExxGRzs2K/NqLb0Z7jVgZlFFn+fMif+TzEUZyRyNxn9e/qNIUEA4aHYG Dz//MrqmRDRRGssQXaSowz9fYNm3t6feW6p/T7AVhVJS5n04b21vSyr6PPyyNmUmeCYq cTaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="Ynq/dlNK"; 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 z5-20020a63e545000000b0042b3b3a63bfsi1283483pgj.467.2022.09.09.11.18.45; Fri, 09 Sep 2022 11:18:57 -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="Ynq/dlNK"; 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 S229835AbiIISFQ (ORCPT + 99 others); Fri, 9 Sep 2022 14:05:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230114AbiIISFL (ORCPT ); Fri, 9 Sep 2022 14:05:11 -0400 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E70B14B0FA; Fri, 9 Sep 2022 11:05:08 -0700 (PDT) Received: by mail-ej1-x62b.google.com with SMTP id y3so5864415ejc.1; Fri, 09 Sep 2022 11:05:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=PRi5c+744JgXz7r9GuZCijMImqB0eIkuk0+iSaMrpvM=; b=Ynq/dlNKNVDIzzbnzG46hZDxhfEdxgRb+cFXx402hqOIdhi2Ra4HrDsGEyYDwzxGK5 q8zryMdSGYJtehI9TrM1vnoN0fmCDMrnuESlsmC96HIub3jLNKuQwTd/gVvcZf7s27WB sHPx6mF+Mwr+q7lwuX9/pgwadgb2qypXBAMje/r5Udk7fq7fWGOggMOyAG8M9fpfwScv 1kS1yoUB8MtudRILMEf/2iYk+3wvzIvZrnGXUvvp4Ip55JrUm4ykmbwSQLXXgqGZ0h3u 1u8bo80J5Mx8MhCWo3KNspts+i4ojiMFOUyuZk1DYGBhZIyI9Fz5DysMj/llalvTErOe cN9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=PRi5c+744JgXz7r9GuZCijMImqB0eIkuk0+iSaMrpvM=; b=iha3LNQJi0JmIjttET4NRXg9pGKGGQYWy5UJp3A4HCLoqWfj5hsfzFtdKn+752UWCI XmNinph4Ea/dMgN1cSTzsOQ/7zPT1eC1YkCHreqAs2Sy9ZzrqLYRZJX1nYWSw/see0HJ 9XUxyRrB9YDAmYrLnB0tYwGF+K112ax4RyX39IFfYNYppii5s8d7d9tKb1vfP67YhjEY +/Uivl8zIF7g41U2XwmS3XrVw3b65v9MlKFzPeaQXA47fmC9xBhVdXky+MvoDNnBjzJD cnse0Q8ZjgmWzBs7wYqQxULAhDqMm7dOcul0iZOmRs8CYnQrcMok5VUsIzaZRh4aQL5D 8q2Q== X-Gm-Message-State: ACgBeo1kGACFjWvSit2MKwk6DYp2UmEwtk7vQ+g7CiwPxLBaNtoCcEmA JdxlQ7CETCmEOm+Xn4ACKsVUjCZy4fvR/ERrt38= X-Received: by 2002:a17:907:2bd8:b0:770:77f2:b7af with SMTP id gv24-20020a1709072bd800b0077077f2b7afmr1250011ejc.545.1662746703412; Fri, 09 Sep 2022 11:05:03 -0700 (PDT) MIME-Version: 1.0 References: <20220829210546.755377-1-james.hilliard1@gmail.com> In-Reply-To: <20220829210546.755377-1-james.hilliard1@gmail.com> From: Andrii Nakryiko Date: Fri, 9 Sep 2022 11:04:51 -0700 Message-ID: Subject: Re: [PATCH v2] libbpf: add GCC support for bpf_tail_call_static To: James Hilliard Cc: bpf@vger.kernel.org, Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, llvm@lists.linux.dev 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, Aug 29, 2022 at 2:05 PM James Hilliard wrote: > > The bpf_tail_call_static function is currently not defined unless > using clang >= 8. > > To support bpf_tail_call_static on GCC we can check if __clang__ is > not defined to enable bpf_tail_call_static. > > We need to use GCC assembly syntax when the compiler does not define > __clang__ as LLVM inline assembly is not fully compatible with GCC. > > Signed-off-by: James Hilliard > --- > Changes v1 -> v2: > - drop __BPF__ check as GCC now defines __bpf__ > --- > tools/lib/bpf/bpf_helpers.h | 19 +++++++++++++------ > 1 file changed, 13 insertions(+), 6 deletions(-) > > diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h > index 7349b16b8e2f..867b734839dd 100644 > --- a/tools/lib/bpf/bpf_helpers.h > +++ b/tools/lib/bpf/bpf_helpers.h > @@ -131,7 +131,7 @@ > /* > * Helper function to perform a tail call with a constant/immediate map slot. > */ > -#if __clang_major__ >= 8 && defined(__bpf__) > +#if (!defined(__clang__) || __clang_major__ >= 8) && defined(__bpf__) > static __always_inline void > bpf_tail_call_static(void *ctx, const void *map, const __u32 slot) > { > @@ -139,8 +139,8 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot) > __bpf_unreachable(); > > /* > - * Provide a hard guarantee that LLVM won't optimize setting r2 (map > - * pointer) and r3 (constant map index) from _different paths_ ending > + * Provide a hard guarantee that the compiler won't optimize setting r2 > + * (map pointer) and r3 (constant map index) from _different paths_ ending > * up at the _same_ call insn as otherwise we won't be able to use the > * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel > * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key > @@ -148,12 +148,19 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot) > * > * Note on clobber list: we need to stay in-line with BPF calling > * convention, so even if we don't end up using r0, r4, r5, we need > - * to mark them as clobber so that LLVM doesn't end up using them > - * before / after the call. > + * to mark them as clobber so that the compiler doesn't end up using > + * them before / after the call. > */ > - asm volatile("r1 = %[ctx]\n\t" > + asm volatile( > +#ifdef __clang__ > + "r1 = %[ctx]\n\t" > "r2 = %[map]\n\t" > "r3 = %[slot]\n\t" > +#else > + "mov %%r1,%[ctx]\n\t" > + "mov %%r2,%[map]\n\t" > + "mov %%r3,%[slot]\n\t" > +#endif Hey James, I don't think it's a good idea to have a completely different BPF asm syntax in GCC-BPF vs what Clang is supporting. Note that Clang syntax is also what BPF users see in BPF verifier log and in llvm-objdump output, so that's what BPF users are familiar with. This will cause constant and unavoidable maintenance burden both for libraries like libbpf and end users and their BPF apps as well. Given you are trying to make GCC-BPF part of the BPF ecosystem, please think about how to help the ecosystem, move it forward and unify it, not how to branch out and have Clang vs GCC differences everywhere. There is a lot of embedded BPF asm in production applications, having to write something as trivial as `r1 = X` in GCC or Clang-specific ways is a huge burden. As such, we've reverted your patch ([0]). Please add de facto BPF asm syntax support to GCC-BPF and this change won't be necessary. [0] https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/commit/?id=665f5d3577ef43e929d59cf39683037887c351bf > "call 12" > :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot) > : "r0", "r1", "r2", "r3", "r4", "r5"); > -- > 2.34.1 >