All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Leon Hwang" <leon.hwang@linux.dev>
To: "Andrii Nakryiko" <andrii.nakryiko@gmail.com>
Cc: <bpf@vger.kernel.org>, <ast@kernel.org>, <andrii@kernel.org>,
	<daniel@iogearbox.net>, <olsajiri@gmail.com>,
	<yonghong.song@linux.dev>, <song@kernel.org>, <eddyz87@gmail.com>,
	<dxu@dxuuu.xyz>, <deso@posteo.net>, <kernel-patches-bot@fb.com>
Subject: Re: [PATCH bpf-next v3 1/6] bpf: Introduce internal check_map_flags helper function
Date: Tue, 26 Aug 2025 23:24:50 +0800	[thread overview]
Message-ID: <DCCGPAPLTR3C.2CXCTTKA7W0A0@linux.dev> (raw)
In-Reply-To: <CAEf4Bzbku_8oNkB5VmrNPNnWg6h5YVPTP2WTMgYcrbfwpzSUoA@mail.gmail.com>

On Sat Aug 23, 2025 at 6:14 AM +08, Andrii Nakryiko wrote:
> On Thu, Aug 21, 2025 at 9:08 AM Leon Hwang <leon.hwang@linux.dev> wrote:
>>
>> It is to unify map flags checking for lookup, update, lookup_batch and
>> update_batch.
>>
>> Therefore, it will be convenient to check BPF_F_CPU flag in this helper
>> function for them in next patch.
>>
>> Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
>> ---
>>  kernel/bpf/syscall.c | 45 ++++++++++++++++++++++----------------------
>>  1 file changed, 22 insertions(+), 23 deletions(-)
>>
>> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
>> index 0fbfa8532c392..19f7f5de5e7dc 100644
>> --- a/kernel/bpf/syscall.c
>> +++ b/kernel/bpf/syscall.c
>> @@ -1654,6 +1654,17 @@ static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
>>         return NULL;
>>  }
>>
>> +static int check_map_flags(struct bpf_map *map, u64 flags, bool check_flag)
>
> "check_map_flags" is super generically named... (and actually
> misleading, it's not map flags you are checking), so I think it should
> be something along the lines of "check_map_op_flag", i.e. map
> *operation* flag?
>
> but also check_flag bool argument name for a function called "check
> flags" is so confusing... The idea here is whether we should enforce
> there is no *extra* flags beyond those common for all operations,
> right? So maybe call it "allow_extra_flags" or alternatively
> "strict_extra_flags", something suggesting that his is something in
> addition to common flags
>
> alternatively, and perhaps best of all, I'd move that particular check
> outside and just maintain something like ARRAY_CREATE_FLAG_MASK for
> each operation, checking it explicitly where appropriate. WDYT?
>

Ack.

Following this idea, the checking functions will be

static inline bool bpf_map_check_op_flags(struct bpf_map *map, u64 flags, bool strict_extra_flags,
                                          u64 extra_flags_mask)
{
        if (strict_extra_flags && ((u32)flags & extra_flags_mask))
                return -EINVAL;

        if ((flags & BPF_F_LOCK) && !btf_record_has_field(map->record, BPF_SPIN_LOCK))
                return -EINVAL;

        if (!(flags & BPF_F_CPU) && flags >> 32)
                return -EINVAL;

        if ((flags & (BPF_F_CPU | BPF_F_ALL_CPUS)) && !bpf_map_supports_cpu_flags(map->map_type))
                return -EINVAL;

        return 0;
}

#define BPF_MAP_LOOKUP_ELEM_EXTRA_FLAGS_MASK (~(BPF_F_LOCK | BPF_F_CPU | BPF_F_ALL_CPUS))

static inline bool bpf_map_check_update_flags(struct bpf_map *map, u64 flags)
{
        return bpf_map_check_op_flags(map, flags, false, 0);
}

static inline bool bpf_map_check_lookup_flags(struct bpf_map *map, u64 flags)
{
        return bpf_map_check_op_flags(map, flags, true, BPF_MAP_LOOKUP_ELEM_EXTRA_FLAGS_MASK);
}

static inline bool bpf_map_check_batch_flags(struct bpf_map *map, u64 flags)
{
        return bpf_map_check_op_flags(map, flags, true, BPF_MAP_LOOKUP_ELEM_EXTRA_FLAGS_MASK);
}

These functions are better than check_map_flags().

Thanks,
Leon

> pw-bot: cr
>
> [...]

  reply	other threads:[~2025-08-26 15:25 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-21 16:08 [PATCH bpf-next v3 0/6] Introduce BPF_F_CPU flag for percpu maps Leon Hwang
2025-08-21 16:08 ` [PATCH bpf-next v3 1/6] bpf: Introduce internal check_map_flags helper function Leon Hwang
2025-08-22 22:14   ` Andrii Nakryiko
2025-08-26 15:24     ` Leon Hwang [this message]
2025-08-26 22:50       ` Andrii Nakryiko
2025-08-21 16:08 ` [PATCH bpf-next v3 2/6] bpf: Introduce BPF_F_CPU flag for percpu_array maps Leon Hwang
2025-08-22 22:14   ` Andrii Nakryiko
2025-08-26 14:45     ` Leon Hwang
2025-08-21 16:08 ` [PATCH bpf-next v3 3/6] bpf: Introduce BPF_F_CPU flag for percpu_hash and lru_percpu_hash maps Leon Hwang
2025-08-22 22:14   ` Andrii Nakryiko
2025-08-26 15:14     ` Leon Hwang
2025-08-21 16:08 ` [PATCH bpf-next v3 4/6] bpf: Introduce BPF_F_CPU flag for percpu_cgroup_storage maps Leon Hwang
2025-08-21 16:08 ` [PATCH bpf-next v3 5/6] libbpf: Support BPF_F_CPU for percpu maps Leon Hwang
2025-08-22 22:20   ` Andrii Nakryiko
2025-08-26 15:35     ` Leon Hwang
2025-08-26 22:50       ` Andrii Nakryiko
2025-08-21 16:08 ` [PATCH bpf-next v3 6/6] selftests/bpf: Add cases to test BPF_F_CPU flag Leon Hwang

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=DCCGPAPLTR3C.2CXCTTKA7W0A0@linux.dev \
    --to=leon.hwang@linux.dev \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=deso@posteo.net \
    --cc=dxu@dxuuu.xyz \
    --cc=eddyz87@gmail.com \
    --cc=kernel-patches-bot@fb.com \
    --cc=olsajiri@gmail.com \
    --cc=song@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.