Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp221630imw; Tue, 12 Jul 2022 18:16:24 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ukrsUU5A8xioae9MypW8y9esykz6qk6JlcKK3awyD2bf03HNg8vGgVydBh1oWRN81pThL/ X-Received: by 2002:a17:906:cc11:b0:72b:458e:5d45 with SMTP id ml17-20020a170906cc1100b0072b458e5d45mr885581ejb.589.1657674984163; Tue, 12 Jul 2022 18:16:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657674984; cv=none; d=google.com; s=arc-20160816; b=YDomalYmBG4MKbGB6YcxF5v301mWl4iVZYjKKyuHARGglYsSCdQ46V8y0MJLvsG1jH zrQiyJI19APtKj259twoGFYkde8nDRIQo0tR7HPHw8dGb/xTi3VgSteS+DUqU3K3dpbQ QgvY83BeVvf7M84YvT+I307IvPVrqYLuadg3v/LV1KkEY02UrdX2eN5OeL7Sg2UynIDI bmleNaLgH6Zg2QJ3rO4WluVjlOKxM4EB0XkHU+XQCWhmpP1wehDVYeQKeZNlkT6SDeJL yP9u+hu5N55WQDgkcTpjS6WhWl+vgKlrpHw1wSFyUctCc+Wxuz4t8p837stPrSfPHB0s tzQQ== 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=RrX3peZWnGalBRHR5HZNG+F96F6AWO/ZiWH718+O+dA=; b=kEemmpHHgMI16S+wdw5g41y3Mp1Yf3JH0kdVNu4oE0mOc7E9YIz9FCY/7r3VcasCkP 5DlRrcC1c2C69Tvy3LVRjtU9JCLK/4ZDqPl9Zd9wXyhcAsCJNRwH0+yThVWEhQi5pbpX z9YzkFgv/CprzDVWNwDB/lbgwRAhIqGGdJ7h4M+12P+3tkgne/6MB25xJHzB9NBbPAhB EoshCYR2P1pYP/E46zjm2IFGPNIhHXMJ8JqUqBSL1nMIF/B5b3veBcvgCcP6hLVyXt02 ldbK7XBxA28vbAZbZ7hjcOtCwCyR7CbOeSBZURW+xAFO1soyaNNbA5ofCHTlFPBBu2Vv yz6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=LIDvWWeR; 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 u5-20020a05640207c500b0043577827670si3813659edy.354.2022.07.12.18.15.43; Tue, 12 Jul 2022 18:16:24 -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=LIDvWWeR; 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 S229994AbiGMBKn (ORCPT + 99 others); Tue, 12 Jul 2022 21:10:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230444AbiGMBKk (ORCPT ); Tue, 12 Jul 2022 21:10:40 -0400 Received: from mail-oa1-x2b.google.com (mail-oa1-x2b.google.com [IPv6:2001:4860:4864:20::2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EAB41D0E07; Tue, 12 Jul 2022 18:10:39 -0700 (PDT) Received: by mail-oa1-x2b.google.com with SMTP id 586e51a60fabf-10bec750eedso12393729fac.8; Tue, 12 Jul 2022 18:10:39 -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=RrX3peZWnGalBRHR5HZNG+F96F6AWO/ZiWH718+O+dA=; b=LIDvWWeRQZTx/i2X47H61p2/5b0xtFm3GNtyXOO2qqIe73b8/IQH/9A2l+mGfBtK52 S3ZOG/LjzCIWzIcIE7e7/WXqyZa4Zcafr212YEoautFQw7Ne6FwelqZPQgYjvBXjzgRX 2OgdZpIDewP84Kn7FB4q0S1vfN9nYPWDpJsp+wdzvFmOo4t6eP+lU60en6T6rUjywoC1 IQCwjV2djvWMKtqXyYgbcaOOD6qKdtOb7qnEC0OnH4IPTRj2QCJShmFy8NIzgDdg8zTX /hW9is4OTJ4puVf6s77EUuJETfJfe9oLOLEA1sYCTGMs5+ZPPbkIcGh7nKA56yKq9ZkR IT2g== 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=RrX3peZWnGalBRHR5HZNG+F96F6AWO/ZiWH718+O+dA=; b=h7MtH58OFbRS+bQOi9iX7yHc2JFp7hpncldHhsbOgooxsky0++KnNSIPvOUeVL2pHp hVAwPc5HOyU1NeXBdc+45zoCarUUqmbOC5+xwf/CaxN/Myx+ZEZgB8/R2hk8xRz/WC+i l/ao4XZe59vhm6bBI+wWr8gRNb1/UT2yZzDD+kC3f5vUMfroZxX4v2BQ1WSBvVJJF1BO GnAsMKtt6kJmeh/JEoDEdXCgOXMBXe/W6JttzcZbHDf0h/Ftv+NKpwwdHe7AjWAsm8IZ 7oZaoNXus+Wte07sGgSQQqGU0iO7sYBxftSbx5T+zAt4wg2hkhMwOyofKBR8NUCYU6cD neLQ== X-Gm-Message-State: AJIora/KKW0JXhD/xZ6deN3/UW2i92bx0J0WZtVOhwo2MVkkgRwEoQP6 G7O6YOf42JpDCTQLIpE09hAw3T9hu+sTSpDed7I= X-Received: by 2002:a05:6870:d0ce:b0:f3:3856:f552 with SMTP id k14-20020a056870d0ce00b000f33856f552mr523604oaa.99.1657674639131; Tue, 12 Jul 2022 18:10:39 -0700 (PDT) MIME-Version: 1.0 References: <20220706172814.169274-1-james.hilliard1@gmail.com> <87v8s260j1.fsf@oracle.com> In-Reply-To: From: James Hilliard Date: Tue, 12 Jul 2022 19:10:27 -0600 Message-ID: Subject: Re: [PATCH v2] bpf/scripts: Generate GCC compatible helpers To: Alexei Starovoitov 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, 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 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. > We have to avoid forking. Avoiding having to maintain a separate libbpf fork for GCC compatibility is precisely the purpose of this patch. > Meaning we're not going to work around that by ifdefs in > bpf_helper_defs.h > Because gcc community will not learn the lesson and will keep > the bad practice of unnecessary forks. Is there some background I'm missing here? What's your definition of an unnecessary fork? > The best path forward here is to support both (void *) 1 style > and kernel_helper attribute in both gcc and llvm. How about we use a guard like this: #if __has_attribute(kernel_helper) new kernel_helper variant macro #else legacy clang variant macro #endif > Moving forward the bpf_helper_defs.h will stay with (void *)1 style > and when both compilers support both options we will start > transitioning to the new kernel_helpers style. 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.