BPF List
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii@kernel.org>
To: <bpf@vger.kernel.org>, <ast@kernel.org>, <daniel@iogearbox.net>
Cc: <andrii@kernel.org>, <kernel-team@fb.com>,
	Nathan Chancellor <nathan@kernel.org>
Subject: [PATCH bpf-next] libbpf: clean up ringbuf size adjustment implementation
Date: Tue, 10 May 2022 11:51:59 -0700	[thread overview]
Message-ID: <20220510185159.754299-1-andrii@kernel.org> (raw)

Drop unused iteration variable, move overflow prevention check into the
for loop.

Fixes: 0087a681fa8c ("libbpf: Automatically fix up BPF_MAP_TYPE_RINGBUF size, if necessary")
Reported-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/lib/bpf/libbpf.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 15117b9a4d1e..eb4565a89eab 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4951,7 +4951,7 @@ static bool is_pow_of_2(size_t x)
 static size_t adjust_ringbuf_sz(size_t sz)
 {
 	__u32 page_sz = sysconf(_SC_PAGE_SIZE);
-	__u32 i, mul;
+	__u32 mul;
 
 	/* if user forgot to set any size, make sure they see error */
 	if (sz == 0)
@@ -4967,9 +4967,7 @@ static size_t adjust_ringbuf_sz(size_t sz)
 	 * user-set size to satisfy both user size request and kernel
 	 * requirements and substitute correct max_entries for map creation.
 	 */
-	for (i = 0, mul = 1; ; i++, mul <<= 1) {
-		if (mul > UINT_MAX / page_sz) /* prevent __u32 overflow */
-			break;
+	for (mul = 1; mul <= UINT_MAX / page_sz; mul <<= 1) {
 		if (mul * page_sz > sz)
 			return mul * page_sz;
 	}
-- 
2.30.2


             reply	other threads:[~2022-05-10 18:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-10 18:51 Andrii Nakryiko [this message]
2022-05-11 12:10 ` [PATCH bpf-next] libbpf: clean up ringbuf size adjustment implementation patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220510185159.754299-1-andrii@kernel.org \
    --to=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=nathan@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox