From: Yonghong Song <yhs@fb.com>
To: Xiu Jianfeng <xiujianfeng@huawei.com>, <ast@kernel.org>,
<daniel@iogearbox.net>, <andrii@kernel.org>, <kafai@fb.com>,
<songliubraving@fb.com>, <john.fastabend@gmail.com>,
<kpsingh@kernel.org>
Cc: <netdev@vger.kernel.org>, <bpf@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf-next] bpf: Use struct_size() helper
Date: Mon, 20 Dec 2021 08:20:02 -0800 [thread overview]
Message-ID: <168b7dbd-777e-ac20-a49a-4a62a8ed872b@fb.com> (raw)
In-Reply-To: <20211220113048.2859-1-xiujianfeng@huawei.com>
On 12/20/21 3:30 AM, Xiu Jianfeng wrote:
> In an effort to avoid open-coded arithmetic in the kernel, use the
> struct_size() helper instead of open-coded calculation.
>
> Link: https://github.com/KSPP/linux/issues/160
> Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
Ack with a minor comments below.
Acked-by: Yonghong Song <yhs@fb.com>
> ---
> kernel/bpf/local_storage.c | 3 +--
> kernel/bpf/reuseport_array.c | 6 +-----
> 2 files changed, 2 insertions(+), 7 deletions(-)
>
> diff --git a/kernel/bpf/local_storage.c b/kernel/bpf/local_storage.c
> index 035e9e3a7132..23f7f9d08a62 100644
> --- a/kernel/bpf/local_storage.c
> +++ b/kernel/bpf/local_storage.c
> @@ -163,8 +163,7 @@ static int cgroup_storage_update_elem(struct bpf_map *map, void *key,
> return 0;
> }
>
> - new = bpf_map_kmalloc_node(map, sizeof(struct bpf_storage_buffer) +
> - map->value_size,
> + new = bpf_map_kmalloc_node(map, struct_size(new, data, map->value_size),
> __GFP_ZERO | GFP_ATOMIC | __GFP_NOWARN,
> map->numa_node);
> if (!new)
> diff --git a/kernel/bpf/reuseport_array.c b/kernel/bpf/reuseport_array.c
> index 93a55391791a..556a769b5b80 100644
> --- a/kernel/bpf/reuseport_array.c
> +++ b/kernel/bpf/reuseport_array.c
> @@ -152,16 +152,12 @@ static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr)
> {
> int numa_node = bpf_map_attr_numa_node(attr);
> struct reuseport_array *array;
> - u64 array_size;
>
> if (!bpf_capable())
> return ERR_PTR(-EPERM);
>
> - array_size = sizeof(*array);
> - array_size += (u64)attr->max_entries * sizeof(struct sock *);
We have u64 type conversion here while struct_size used type 'size_t'.
So we won't have problems for 64bit system. But for 32bit system, we may
have a slight different behavior here as the current code can return a
u64 value and the struct_size(...) returns a size_t type value with max
value SIZE_MAX.
In __bpf_map_area_alloc() we have a check:
if (size >= SIZE_MAX)
return NULL;
so we should be fine for 32bit system.
> -
> /* allocate all map elements and zero-initialize them */
> - array = bpf_map_area_alloc(array_size, numa_node);
> + array = bpf_map_area_alloc(struct_size(array, ptrs, attr->max_entries), numa_node);
> if (!array)
> return ERR_PTR(-ENOMEM);
>
next prev parent reply other threads:[~2021-12-20 16:20 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-20 11:30 Xiu Jianfeng
2021-12-20 16:20 ` Yonghong Song [this message]
2021-12-21 23:40 ` 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=168b7dbd-777e-ac20-a49a-4a62a8ed872b@fb.com \
--to=yhs@fb.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=songliubraving@fb.com \
--cc=xiujianfeng@huawei.com \
/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®