Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp275643imw; Tue, 12 Jul 2022 19:53:11 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tVat0k/r0LejP0B20k7uwdY+30xFCjftEiPJrgbzyauC4l/DWSArJKR6qOjutUu2f3kV2Y X-Received: by 2002:aa7:8186:0:b0:528:c344:ed6e with SMTP id g6-20020aa78186000000b00528c344ed6emr976951pfi.35.1657680790775; Tue, 12 Jul 2022 19:53:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657680790; cv=none; d=google.com; s=arc-20160816; b=IJSBTEMw/yCKDI+mU7C5tM0R4Judhj4BAMizTDn71XDuSyZeBBo1iHjUT38hxxGkKo yh0CQx/6bZNzZqkn2o+na9Q2hn15OcH7YfQgrm/ohj4gEUOtC0hDfhHID5KC9idmzqYe HSCiaOrqjuoWpZm+RxgKI6C3zBNoYLl6rUn3JunHbd/nyqz4B5NDbZxZu1jFaUTDR1wu EoaU1qs4gPazVwRsFFO7WzBZ6fKrvR+ZYz2/mI2AGFpgmhyl/fZz5YSRZ/73Z7s9aY7p P/pFAFh63Ne+sogEI9blW3H7G8eo0YLYk6EwtZOCzlCk16wRn5b+S/R3M2bU5Km7lgM8 IjIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=i4p6sm+Eaw9H7Av5dxRsvpT90Zq1tawHBOllyyWvACc=; b=Lw4uFKEebdIN8ZS86gEPogFCPW2NGQC7RWXNjBniEWS3H6o4DgXgjaUFnQELoNLRTe fjpE8qHev9x/BMDB5FQJSdupkQcf4HWTS0CIRVe9f869Z1NVsiqbMIBjz1K9zkHVWYJM VI5MqYOBYczBE9ClbrEi+ly4aLnQc8HQuJnZVPHVqlIV7ywfIUufHla2sA6BVRnie0PZ SZdYJUlPQ7KQB60u7ouEwT1Ix04HigP/kHZka8s5Fc03Ig8MDAZWen2ZZCtyqSzH9GTO xGagjZ8XRyNkNKC9p+giSpzl5czDs2e8ZizWoQNIqoR1fIL56LJ6DzzBoyO3RIhm1u0j SSlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=OLRPCA99; 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 t5-20020a63d245000000b004117951a3c9si15061482pgi.537.2022.07.12.19.52.58; Tue, 12 Jul 2022 19:53:10 -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=OLRPCA99; 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 S230412AbiGMBS6 (ORCPT + 99 others); Tue, 12 Jul 2022 21:18:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229674AbiGMBS5 (ORCPT ); Tue, 12 Jul 2022 21:18:57 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 498802251A; Tue, 12 Jul 2022 18:18:56 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id o3-20020a17090a744300b001ef8f7f3dddso1105164pjk.3; Tue, 12 Jul 2022 18:18:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=i4p6sm+Eaw9H7Av5dxRsvpT90Zq1tawHBOllyyWvACc=; b=OLRPCA998W2mo1p8Be6KjHk7nFx2CLbdGrEeS0v29zc77Y1qCAjdoEZgP2/WhRwCzJ KTwIFCXaJ4HSojneMaX2p9Agmu70nr1IdUWo7SGn5cfKEaDJMa6jP/Qj53GVCH7Ofiry NXsdsAGTogVQzlCX7YxhxAoWhsXR27znvdS3qoDF0Lc010cUX3/RQUqRlpTbBTd535qb jnccZRO9MdwdQE4CL6ypDJn+hUTUGuy9Z1mUVtINPzdg1Ks7j4pDaMaYg9RowIu+NLOs fs2YESYSZB6YT23o0tPkakwwc6xbK7MYY/MHmkm7IuokJDQBxLEDGFH8cCykymnJOoD4 m5Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=i4p6sm+Eaw9H7Av5dxRsvpT90Zq1tawHBOllyyWvACc=; b=fAgQFVxfKaLqZkem5WHRnoFx01JdwRfUN4B1ineEKv8eTJIo5cxMgLwcThBPm1r+ig LV0I60zTVLCrO9G0bdHhqMu2SVLcZdIKJbe7Xah9e4uBAF8gtFmmaJfvHcGZQjh5dkEg EbtdE75hdNvJUcR93XEF/B4iEffCbqXIpvt9HM31G6TLdIS9DPkEw6efVBe8jRVD/dFf MAYIBRZArqKVl2sZFibQPWSkONHEkjsbyS4ox5HBU8D6RgoJ0+yjo3xqsNQoZgs8I1uG OZS8CEUqMuX0pI0FprSLTOKTW8WgEAr8PTAJNRhE7zp10iUgQP5zIUah2o4zmSyZ/YbA Y2xA== X-Gm-Message-State: AJIora87DCaEkd3QgaokbGoPK76XuUonEGC47+QnoLB+gZcxZYCHkgN2 xGVerzBMmZxrHQZt5HH9v24= X-Received: by 2002:a17:90b:4a04:b0:1ef:c318:ef9c with SMTP id kk4-20020a17090b4a0400b001efc318ef9cmr7313697pjb.67.1657675135536; Tue, 12 Jul 2022 18:18:55 -0700 (PDT) Received: from macbook-pro-3.dhcp.thefacebook.com ([2620:10d:c090:400::5:580c]) by smtp.gmail.com with ESMTPSA id j4-20020a170902c3c400b0016bf24611e7sm7478169plj.5.2022.07.12.18.18.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jul 2022 18:18:55 -0700 (PDT) Date: Tue, 12 Jul 2022 18:18:51 -0700 From: Alexei Starovoitov To: James Hilliard Cc: "Jose E. Marchesi" , Andrii Nakryiko , Quentin Monnet , Yonghong Song , bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Nathan Chancellor , Nick Desaulniers , Tom Rix , Networking , Linux Kernel Mailing List , llvm@lists.linux.dev Subject: Re: [PATCH v2] bpf/scripts: Generate GCC compatible helpers Message-ID: <20220713011851.4a2tnqhdd5f5iwak@macbook-pro-3.dhcp.thefacebook.com> References: <20220706172814.169274-1-james.hilliard1@gmail.com> <87v8s260j1.fsf@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 Tue, Jul 12, 2022 at 07:10:27PM -0600, James Hilliard wrote: > On Tue, Jul 12, 2022 at 10:48 AM Alexei Starovoitov > wrote: > > > > On Tue, Jul 12, 2022 at 4:20 AM Jose E. Marchesi > > wrote: > > > > > > > > > > CC Quentin as well > > > > > > > > On Mon, Jul 11, 2022 at 5:11 PM James Hilliard > > > > wrote: > > > >> > > > >> On Mon, Jul 11, 2022 at 5:36 PM Yonghong Song wrote: > > > >> > > > > >> > > > > >> > > > > >> > On 7/6/22 10:28 AM, James Hilliard wrote: > > > >> > > The current bpf_helper_defs.h helpers are llvm specific and don't work > > > >> > > correctly with gcc. > > > >> > > > > > >> > > GCC appears to required kernel helper funcs to have the following > > > >> > > attribute set: __attribute__((kernel_helper(NUM))) > > > >> > > > > > >> > > Generate gcc compatible headers based on the format in bpf-helpers.h. > > > >> > > > > > >> > > This adds conditional blocks for GCC while leaving clang codepaths > > > >> > > unchanged, for example: > > > >> > > #if __GNUC__ && !__clang__ > > > >> > > void *bpf_map_lookup_elem(void *map, const void *key) > > > >> > > __attribute__((kernel_helper(1))); > > > >> > > #else > > > >> > > static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1; > > > >> > > #endif > > > >> > > > > >> > It does look like that gcc kernel_helper attribute is better than > > > >> > '(void *) 1' style. The original clang uses '(void *) 1' style is > > > >> > just for simplicity. > > > >> > > > >> Isn't the original style going to be needed for backwards compatibility with > > > >> older clang versions for a while? > > > > > > > > I'm curious, is there any added benefit to having this special > > > > kernel_helper attribute vs what we did in Clang for a long time? > > > > Did GCC do it just to be different and require workarounds like this > > > > or there was some technical benefit to this? > > > > > > We did it that way so we could make trouble and piss you off. > > > > > > Nah :) > > > > > > We did it that way because technically speaking the clang construction > > > works relying on particular optimizations to happen to get correct > > > compiled programs, which is not guaranteed to happen and _may_ break in > > > the future. > > > > > > In fact, if you compile a call to such a function prototype with clang > > > with -O0 the compiler will try to load the function's address in a > > > register and then emit an invalid BPF instruction: > > > > > > 28: 8d 00 00 00 03 00 00 00 *unknown* > > > > > > On the other hand the kernel_helper attribute is bullet-proof: will work > > > with any optimization level, with any version of the compiler, and in > > > our opinion it is also more readable, more tidy and more correct. > > > > > > Note I'm not saying what you do in clang is not reasonable; it may be, > > > obviously it works well enough for you in practice. Only that we have > > > good reasons for doing it differently in GCC. > > > > Not questioning the validity of the reasons, but they created > > the unnecessary difference between compilers. > > Sounds to me like clang is relying on an unreliable hack that may > be difficult to implement in GCC, so let's see what's the best option > moving forwards in terms of a migration path for both GCC and clang. The following is a valid C code: static long (*foo) (void) = (void *) 1234; foo(); and GCC has to generate correct assembly assuming it runs at -O1 or higher. There is no indirect call insn defined in BPF ISA yet, so the -O0 behavior is undefined. > Or we can just feature detect kernel_helper and leave the (void *)1 style > fallback in place until we drop support for clang variants that don't support > kernel_helper. This would provide GCC compatibility and a better migration > path for clang as well as clang will then automatically use the new variant > whenever support for kernel_helper is introduced. Support for valid C code will not be dropped from clang.