mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Алексей Сафин" <a.safin@rosa.ru>
To: Yafang Shao <laoar.shao@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	lvc-patches@linuxtesting.org, stable@vger.kernel.org
Subject: Re: [PATCH] bpf: hashtab: fix 32-bit overflow in memory usage calculation
Date: Fri, 7 Nov 2025 09:58:16 +0300	[thread overview]
Message-ID: <afcb878e-d233-4c87-a0fc-803612c8c91f@rosa.ru> (raw)
In-Reply-To: <CALOAHbCcfszFFDuABhPHoMioT26GAXOKZzMqww0QY1wKogNm1g@mail.gmail.com>

Yes, that looks even better to me. Changing value_size to u64 at declaration
makes the arithmetic safe everywhere and keeps the code cleaner.

I agree with this version.

Should I prepare a v2 patch with this modification, or will you take it 
from here?

07.11.2025 04:58, Yafang Shao пишет:
> On Fri, Nov 7, 2025 at 4:59 AM Alexei Safin <a.safin@rosa.ru> wrote:
>> The intermediate product value_size * num_possible_cpus() is evaluated
>> in 32-bit arithmetic and only then promoted to 64 bits. On systems with
>> large value_size and many possible CPUs this can overflow and lead to
>> an underestimated memory usage.
>>
>> Cast value_size to u64 before multiplying.
>>
>> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>>
>> Fixes: 304849a27b34 ("bpf: hashtab memory usage")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Alexei Safin <a.safin@rosa.ru>
>> ---
>>   kernel/bpf/hashtab.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
>> index 570e2f723144..7ad6b5137ba1 100644
>> --- a/kernel/bpf/hashtab.c
>> +++ b/kernel/bpf/hashtab.c
>> @@ -2269,7 +2269,7 @@ static u64 htab_map_mem_usage(const struct bpf_map *map)
>>                  usage += htab->elem_size * num_entries;
>>
>>                  if (percpu)
>> -                       usage += value_size * num_possible_cpus() * num_entries;
>> +                       usage += (u64)value_size * num_possible_cpus() * num_entries;
>>                  else if (!lru)
>>                          usage += sizeof(struct htab_elem *) * num_possible_cpus();
>>          } else {
>> @@ -2281,7 +2281,7 @@ static u64 htab_map_mem_usage(const struct bpf_map *map)
>>                  usage += (htab->elem_size + LLIST_NODE_SZ) * num_entries;
>>                  if (percpu) {
>>                          usage += (LLIST_NODE_SZ + sizeof(void *)) * num_entries;
>> -                       usage += value_size * num_possible_cpus() * num_entries;
>> +                       usage += (u64)value_size * num_possible_cpus() * num_entries;
>>                  }
>>          }
>>          return usage;
>> --
>> 2.50.1 (Apple Git-155)
>>
> Thanks for the fix. What do you think about this change?
>
> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> index 4a9eeb7aef85..f9084158bfe2 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
> @@ -2251,7 +2251,7 @@ static long bpf_for_each_hash_elem(struct
> bpf_map *map, bpf_callback_t callback_
>   static u64 htab_map_mem_usage(const struct bpf_map *map)
>   {
>          struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
> -       u32 value_size = round_up(htab->map.value_size, 8);
> +       u64 value_size = round_up(htab->map.value_size, 8);
>          bool prealloc = htab_is_prealloc(htab);
>          bool percpu = htab_is_percpu(htab);
>          bool lru = htab_is_lru(htab);
>
>

  reply	other threads:[~2025-11-07  6:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-06 20:58 Alexei Safin
2025-11-07  1:58 ` Yafang Shao
2025-11-07  6:58   ` Алексей Сафин [this message]
2025-11-07  7:42     ` Yafang Shao

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=afcb878e-d233-4c87-a0fc-803612c8c91f@rosa.ru \
    --to=a.safin@rosa.ru \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=laoar.shao@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lvc-patches@linuxtesting.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=yonghong.song@linux.dev \
    /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

all inboxes | Powered by JetHome®