Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp337090pxm; Wed, 2 Mar 2022 16:43:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJxRm2fBlvwsVTvxX+v8lmEgKWIz6cxylLk+7nlCXWwaQvmDXa9PHE7UQcKEWD+u6Q8SXauH X-Received: by 2002:a17:90b:1648:b0:1bd:28ac:859d with SMTP id il8-20020a17090b164800b001bd28ac859dmr2509846pjb.59.1646268221061; Wed, 02 Mar 2022 16:43:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646268221; cv=none; d=google.com; s=arc-20160816; b=yQfMAqZoCVNop9KxoeDL8wZ6RU2KepAn25a8fAAPAzvGxVEi+8V/wvwHKs//ltc0dE xfoSjbTo70WnkTQR6szlPdiWSol88Gl8XHS/Q0GLFT5FG9yrqFVphoWnnfzQDGsE/vw0 FVkl9a0ear0yfv+etTxMjUnocYV/rT7uit2XHPt7mYXw96vcsvmRyVhUjNKps3iDY9nX Ge2J7+AN2fYHEIYRNu7ovQl9uxv1XDQmd1VIOmPyrjrbO457KNEhe0t28uI6UTSG48CF LCpZPxsYNCF2GHI0Us30c3/FBiYTDUuTbb6gV58zFQweDZ/m4p9W5FMfMeLF9YRywA4W Oi6A== 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=s2VoxRcgzayXC4zVf8d2bV7EKQmBNoz4BFBzN6+8sx8=; b=mCyXuKaLRW1/O6sqOMmHgruCVhby9xPWX1t0QmmMLeKDZGrq1X28XqM5/379jFHgIO Qce/t2uPHHO0ZQWNgFMesMURQSiSqIgehA4sE2awHDaTjltmXB3CsTXu1gwR7yA3RJNx H2NAl8bTCQ+2/s8CJ9VZ+1tiJUIDE8QzDFRU1OMav7D9Ue3iJpsThmykc9WxoHqriBkE mHx9GSqEU6D4iC2Sn6jqtdsDOHwBxJebcxrutFVOtV5lospc8dwiiUOdCA8Okt98kB16 dZEAlvPzez2eAuIVHJGWn+YQiKo4nWkXJo1RbIHZQF7XD6HLH+AC7C5YWnpnsi8Xu9cl aJmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=irtUW00d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id q4-20020a056a00084400b004e130273ae0si582759pfk.86.2022.03.02.16.43.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Mar 2022 16:43:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=irtUW00d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 50552BD89F; Wed, 2 Mar 2022 16:00:42 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229945AbiCCABW (ORCPT + 99 others); Wed, 2 Mar 2022 19:01:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229869AbiCCABI (ORCPT ); Wed, 2 Mar 2022 19:01:08 -0500 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 019444FC6E; Wed, 2 Mar 2022 16:00:07 -0800 (PST) Received: by mail-qk1-x730.google.com with SMTP id 185so2739074qkh.1; Wed, 02 Mar 2022 16:00:06 -0800 (PST) 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=s2VoxRcgzayXC4zVf8d2bV7EKQmBNoz4BFBzN6+8sx8=; b=irtUW00dYBE7kRhx89tLLgo+OiHCquwUTazjhPRpgra/Ng8ZdA8XxCmleB4NvxllKh 4pKE6dlj50+3l1NtV4o9/4ebYa6MzILugoiY6A7PIbHurAxsmGNDq+Azak/YPX7TvXqp vWKgPbmVn8eSbnf0s0rlwVptu1i33O5KFWx6cgKlB1Iv/ADRAyqTCutf86BCCCum8X2/ Bf0AFz6Uc9OqJ9+suUpqFsrW1IvnGHcoqc6V2ESgWgVivWiJrGD9dv/iy4uRrGM+OgIG JzhbU+mrHknnWfDH4bhT9zMXZNaDWIeBUzcbCDaaOTAZ4EeZTnnWYTbM3mDIlJ1KSgyy PikQ== 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=s2VoxRcgzayXC4zVf8d2bV7EKQmBNoz4BFBzN6+8sx8=; b=wCLGp7OUJp/fLsZela7DxIvXWMqB8fMTaKGEFbVYBpJVhW5arTt6ymEBh+LPLVEJoB SBitD97j/9axjRTC5GzF8BYKe0LIwXvp+8KyFUx02QRApxCPyYgOy5yCJBzjSBDR6SEf tzAUuLR/0Dxip33xVNuSNyqvSr4CLSpBewlV4bMI2y8CR3ALVHsIRgnBJrZhjrzDHY4L 4QQTfvAlGrKVe5C47SWMfAJBYoQzSMu+s4vXQRXGiG+futo0FRgWFpP5zX+uv9/EYBZv PV9OvMQmw8H+4EplRUvT2mGrKM7g+0Nq+2sILaHvIgXScDwjnhp8el0sXeOJ+NszTQfJ euow== X-Gm-Message-State: AOAM530/h/YHG3cvh0tVvANRZMBksuBNc9m19b9MQE+GzLwZzjR/NQWO kfBELprPAxqy7sRyqyySHYOjoL5psvJ9dX7l2+qacind//ASHw== X-Received: by 2002:a02:aa85:0:b0:314:c152:4c89 with SMTP id u5-20020a02aa85000000b00314c1524c89mr26976369jai.93.1646263965076; Wed, 02 Mar 2022 15:32:45 -0800 (PST) MIME-Version: 1.0 References: <20220301165737.672007-1-ytcoode@gmail.com> In-Reply-To: <20220301165737.672007-1-ytcoode@gmail.com> From: Andrii Nakryiko Date: Wed, 2 Mar 2022 15:32:34 -0800 Message-ID: Subject: Re: [PATCH bpf-next] libbpf: Add a check to ensure that page_cnt is non-zero To: Yuntao Wang Cc: bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Networking , open list Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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, Mar 1, 2022 at 8:57 AM Yuntao Wang wrote: > > The page_cnt parameter is used to specify the number of memory pages > allocated for each per-CPU buffer, it must be non-zero and a power of 2. > > Currently, the __perf_buffer__new() function attempts to validate that > the page_cnt is a power of 2 but forgets checking for the case where > page_cnt is zero, we can fix it by replacing 'page_cnt & (page_cnt - 1)' > with '!is_power_of_2(page_cnt)'. > > Thus we also don't need to add a check in perf_buffer__new_v0_6_0() to > make sure that page_cnt is non-zero and the check for zero in > perf_buffer__new_raw_v0_6_0() can also be removed. > > The code is cleaner and more readable. > > Signed-off-by: Yuntao Wang > --- > tools/lib/bpf/libbpf.c | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-) > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index be6480e260c4..4dd1d82cd5b9 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -33,6 +33,7 @@ > #include > #include > #include > +#include we don't have this header implemented in Github repo, so this will be unnecessary painful > #include > #include > #include > @@ -10951,7 +10952,7 @@ struct perf_buffer *perf_buffer__new_raw_v0_6_0(int map_fd, size_t page_cnt, > { > struct perf_buffer_params p = {}; > > - if (page_cnt == 0 || !attr) > + if (!attr) > return libbpf_err_ptr(-EINVAL); > > if (!OPTS_VALID(opts, perf_buffer_raw_opts)) > @@ -10992,7 +10993,7 @@ static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, > __u32 map_info_len; > int err, i, j, n; > > - if (page_cnt & (page_cnt - 1)) { > + if (!is_power_of_2(page_cnt)) { so let's instead just use `page_cnt == 0 || (page_cnt & (page_cnt - 1))` here explicitly > pr_warn("page count should be power of two, but is %zu\n", > page_cnt); > return ERR_PTR(-EINVAL); > -- > 2.35.1 >