From: Martin KaFai Lau <martin.lau@linux.dev>
To: Florian Lehner <dev@der-flo.net>
Cc: ast@kernel.org, daniel@iogearbox.net, davem@davemloft.net,
kuba@kernel.org, hawk@kernel.org, john.fastabend@gmail.com,
andrii@kernel.org, song@kernel.org, yhs@fb.com,
kpsingh@kernel.org, sdf@google.com, haoluo@google.com,
jolsa@kernel.org, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next] bpf: check max_entries before allocating memory
Date: Wed, 26 Oct 2022 12:21:29 -0700 [thread overview]
Message-ID: <bce0b434-5a6e-3423-4782-8ecb3f87939a@linux.dev> (raw)
In-Reply-To: <20221026085053.76561-1-dev@der-flo.net>
On 10/26/22 1:50 AM, Florian Lehner wrote:
> For maps of type BPF_MAP_TYPE_CPUMAP memory is allocated first before
> checking the max_entries argument. If then max_entries is greater than
> NR_CPUS additional work needs to be done to free allocated memory before
> an error is returned.
> This changes moves the check on max_entries before the allocation
> happens.
>
> Signed-off-by: Florian Lehner <dev@der-flo.net>
> ---
> kernel/bpf/cpumap.c | 19 ++++++++-----------
> 1 file changed, 8 insertions(+), 11 deletions(-)
>
> diff --git a/kernel/bpf/cpumap.c b/kernel/bpf/cpumap.c
> index b5ba34ddd4b6..87e9f89a8140 100644
> --- a/kernel/bpf/cpumap.c
> +++ b/kernel/bpf/cpumap.c
> @@ -97,29 +97,26 @@ static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
> attr->map_flags & ~BPF_F_NUMA_NODE)
> return ERR_PTR(-EINVAL);
>
> + /* Pre-limit array size based on NR_CPUS, not final CPU check */
> + if (attr->max_entries > NR_CPUS)
> + return ERR_PTR(-E2BIG);
> +
> cmap = bpf_map_area_alloc(sizeof(*cmap), NUMA_NO_NODE);
> if (!cmap)
> return ERR_PTR(-ENOMEM);
>
> bpf_map_init_from_attr(&cmap->map, attr);
>
> - /* Pre-limit array size based on NR_CPUS, not final CPU check */
> - if (cmap->map.max_entries > NR_CPUS) {
> - err = -E2BIG;
> - goto free_cmap;
> - }
> -
> /* Alloc array for possible remote "destination" CPUs */
> cmap->cpu_map = bpf_map_area_alloc(cmap->map.max_entries *
> sizeof(struct bpf_cpu_map_entry *),
> cmap->map.numa_node);
> - if (!cmap->cpu_map)
> - goto free_cmap;
> + if (!cmap->cpu_map) {
> + bpf_map_area_free(cmap);
> + return ERR_PTR(err);
lgtm. May as well take this chance to remove the "err" variable and directly
return ERR_PTR(-ENOMEM) instead.
> + }
>
> return &cmap->map;
> -free_cmap:
> - bpf_map_area_free(cmap);
> - return ERR_PTR(err);
> }
>
> static void get_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
prev parent reply other threads:[~2022-10-26 19:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-26 8:50 [PATCH bpf-next] bpf: check max_entries before allocating memory Florian Lehner
2022-10-26 10:03 ` Florian Lehner
2022-10-26 19:21 ` Martin KaFai Lau [this message]
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=bce0b434-5a6e-3423-4782-8ecb3f87939a@linux.dev \
--to=martin.lau@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=dev@der-flo.net \
--cc=haoluo@google.com \
--cc=hawk@kernel.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=kuba@kernel.org \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=yhs@fb.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