Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp7314691ioo; Fri, 3 Jun 2022 04:16:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNGaGzqWoRgPA52v9DQb8kAx3aNIuHREEehbsjeCl2pII9Nfc2uI/XfCoqArnfDe7tM8cu X-Received: by 2002:a17:903:246:b0:153:84fe:a9b0 with SMTP id j6-20020a170903024600b0015384fea9b0mr9700608plh.163.1654254984452; Fri, 03 Jun 2022 04:16:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654254984; cv=none; d=google.com; s=arc-20160816; b=hVlI4ZaJ6xlKJMNl/LtY4ioRHqzXMqfkayGr/1CKyL15mkt8XFTviq+FMrchbeG8EK NtJuGh+ZtlKlHbmiWqp/U+GEwxs21dhvlHUfLWpAwus515Er+FjVozPsF5oPVrAVyeKM GekIU8G5Hk2caAbFOg5XAiKC/81US6ZOnLu5rMmhuLLK9eD2mZlR9KJuEVk3qG01j+Ai tC/Axs+JQtzRd1Obt52rXPTq71NIECZ96nPHjhunGlXMNzgQ3qny6DYz/4ZCe38FJuAB PvAb7Qd1D/5fnyCy8S+2wUPJK7VPA7HVl8ZjTQ5fKNdDdCIuyQx6NgUprieEN55lFkFa /tsg== 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=KF5yq6Enx4Bdz3Avcm29+ohVRRzQl9wm8VtLxXbcBX4=; b=ryzJj+/yT2d/7wvyxae5MXfadtY13/OTDz5J9TKAUVZK6aePinEznfiXoUfj+wV21K G5Wx/B1zz+lMzmwIvcfV+NL4PxyizaijJwtXabefSuhsJfPupIMD5YCSZkn/PXAlY55b 7iRrNqY3yzJfmgX1FRXiH1icwBgzMPmSCx+gMdPNfi68p3/SLRnSWS97cvS0VNB1KE0q jltTP4QLJPL1nu/l4OvITCjHPSnqi3G8tDVoC+BJUZP5NrF9XsjNo6aatMguCqg49Icg Z0rbUnik/kU28xLpQpcqApFyskE+hXmUNBhTN6/wdvV0Rd5ngpE76mxnQbBMsXQsX78J 73VA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=cRTNqE6j; 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 u2-20020a170902b28200b00158e93cfafesi8840648plr.132.2022.06.03.04.16.09; Fri, 03 Jun 2022 04: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=cRTNqE6j; 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 S240764AbiFCFeK (ORCPT + 99 others); Fri, 3 Jun 2022 01:34:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240737AbiFCFeJ (ORCPT ); Fri, 3 Jun 2022 01:34:09 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA13638DBF; Thu, 2 Jun 2022 22:34:07 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id a2so10970668lfc.2; Thu, 02 Jun 2022 22:34:07 -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=KF5yq6Enx4Bdz3Avcm29+ohVRRzQl9wm8VtLxXbcBX4=; b=cRTNqE6jJImVaKeCqWv+jepE5wEhrLx3ue0PRddNVpBoagJiFrAttnxaoZ2rLka0rg zHfldgd630eYlv+XewYFN6JbIpJl2Ai9EPfsWAbI3ALVuQkqs59uAh7rpbObuBzQ+df3 yn6L9qUp1cdwU7u39uPEY+6WFEe7pKcMJDCR5bZolHTB7OYI+Lc7sma8CJlBE0SQL6JX eD8IKUIStNsuiCtAe3OzsKQ4XhD0iwl8yYnpqGtBrtH/2755DM3RZREaugOidxVsl5vO QnWjgo4vrclRYZ/vJorWoeRqxc6YkavgfZMnIypwhgMWHyGZ1eOz175MNESsedSCV0jK Y80g== 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=KF5yq6Enx4Bdz3Avcm29+ohVRRzQl9wm8VtLxXbcBX4=; b=w+XwfeJnKArWQlB6TUI3Rfe3XOpLQCQciVL6JyoYYI1W+HH/iYspb+J7VQoW0hwwld 2pxcwaKG8Qva3Q9CbVmW103DciXsV7lMO6Tg1ye/4n5gkjk7346RCpW/xebvWa7W+baM An1WSK0VIHKOtHKrDmdmHmQ8VBZ62hzRB2zUrYiqrjBei1t8/Imx0RYgWzO4wJAzyCLC LyMHSTe2cWwl9OSWynLySmNzkqeES8Oz/Nu/0jJmKWsJ9uFwyIGAcrSEsFX6jfBOkc5/ XaFqCwOMKXTFIYZpyhRrrQJezEwl1MQmWxZZEkLBr3sp8h8RrcXhw+3ZhXoDgzU2rYSx wFGQ== X-Gm-Message-State: AOAM531QTp0PL+crni4SgRG7iyG5t8NrqQW95ky+yFRBCqb9nGXoRXYN o4+9spBUHdxR+v4yS6qParRgPKWlzOV4agooy5Q= X-Received: by 2002:a05:6512:1398:b0:448:bda0:99f2 with SMTP id p24-20020a056512139800b00448bda099f2mr52603360lfa.681.1654234446024; Thu, 02 Jun 2022 22:34:06 -0700 (PDT) MIME-Version: 1.0 References: <20220603041701.2799595-1-irogers@google.com> In-Reply-To: From: Andrii Nakryiko Date: Thu, 2 Jun 2022 22:33:54 -0700 Message-ID: Subject: Re: [PATCH] libbpf: Fix is_pow_of_2 To: Zvi Effron Cc: Ian Rogers , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Networking , bpf , open list , Yuze Chi 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 Thu, Jun 2, 2022 at 9:31 PM Zvi Effron wrote: > > On Thu, Jun 2, 2022 at 9:17 PM Ian Rogers wrote: > > > > From: Yuze Chi > > > > There is a missing not. Consider a power of 2 number like 4096: > > > > x && (x & (x - 1)) > > 4096 && (4096 & (4096 - 1)) > > 4096 && (4096 & 4095) > > 4096 && 0 > > 0 > > > > with the not this is: > > x && !(x & (x - 1)) > > 4096 && !(4096 & (4096 - 1)) > > 4096 && !(4096 & 4095) > > 4096 && !0 > > 4096 && 1 > > 1 > > > > Reported-by: Yuze Chi > > Signed-off-by: Yuze Chi > > Signed-off-by: Ian Rogers > > --- > > tools/lib/bpf/libbpf.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > > index 3f4f18684bd3..fd0414ea00df 100644 > > --- a/tools/lib/bpf/libbpf.c > > +++ b/tools/lib/bpf/libbpf.c > > @@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map); > > > > static bool is_pow_of_2(size_t x) > > { > > - return x && (x & (x - 1)); > > + return x && !(x & (x - 1)); ugh... *facepalm* > > No idea if anyone cares about the consistency, but in linker.c (same directory) > the same static function is defined using == 0 at the end instead of using the > not operator. > > Aside from the consistency issue, personally I find the == 0 version a little > bit easier to read and understand because it's a bit less dense (and a "!" next > to a "(" is an easy character to overlook). > I agree, even more so, logical not used with arbitrary integer (not a pointer or bool) is a mental stumbling block for me, so much so that I avoid doing !strcmp(), for example. But in this case, I'm not sure why I copy/pasted is_pow_of_2() instead of moving the one from linker.c into libbpf_internal.h as static inline. Let's do that instead? > > } > > > > static size_t adjust_ringbuf_sz(size_t sz) > > -- > > 2.36.1.255.ge46751e96f-goog > >