From: Yonghong Song <yonghong.song@linux.dev>
To: Rong Tao <rtoax@foxmail.com>,
andrii@kernel.org, ast@kernel.org, vmalik@redhat.com
Cc: Rong Tao <rongtao@cestc.cn>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
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>, Mykola Lysenko <mykolal@fb.com>,
Shuah Khan <shuah@kernel.org>,
"open list:BPF [GENERAL] (Safe Dynamic Programs and Tools)"
<bpf@vger.kernel.org>, open list <linux-kernel@vger.kernel.org>,
"open list:KERNEL SELFTEST FRAMEWORK"
<linux-kselftest@vger.kernel.org>
Subject: Re: [PATCH bpf-next v3 1/2] bpf: add bpf_strcasecmp kfunc
Date: Tue, 2 Sep 2025 09:54:54 -0700 [thread overview]
Message-ID: <e960900d-3811-4b8b-b4b3-bb23048ef5d6@linux.dev> (raw)
In-Reply-To: <tencent_0E0C830021A02CBCCB6D95AE57CFD100C407@qq.com>
On 9/2/25 2:19 AM, Rong Tao wrote:
> From: Rong Tao <rongtao@cestc.cn>
>
> bpf_strcasecmp() function performs same like bpf_strcmp() except ignoring
> the case of the characters.
>
> Signed-off-by: Rong Tao <rongtao@cestc.cn>
> ---
> kernel/bpf/helpers.c | 68 +++++++++++++++++++++++++++++++-------------
> 1 file changed, 48 insertions(+), 20 deletions(-)
>
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 401b4932cc49..238fd992c786 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -3349,45 +3349,72 @@ __bpf_kfunc void __bpf_trap(void)
> * __get_kernel_nofault instead of plain dereference to make them safe.
> */
>
> -/**
> - * bpf_strcmp - Compare two strings
> - * @s1__ign: One string
> - * @s2__ign: Another string
> - *
> - * Return:
> - * * %0 - Strings are equal
> - * * %-1 - @s1__ign is smaller
> - * * %1 - @s2__ign is smaller
> - * * %-EFAULT - Cannot read one of the strings
> - * * %-E2BIG - One of strings is too large
> - * * %-ERANGE - One of strings is outside of kernel address space
> - */
> -__bpf_kfunc int bpf_strcmp(const char *s1__ign, const char *s2__ign)
> +int __bpf_strcasecmp(const char *s1, const char *s2, bool ignore_case)
The function __bpf_strcasecmp should be a static function.
> {
> char c1, c2;
> int i;
>
> - if (!copy_from_kernel_nofault_allowed(s1__ign, 1) ||
> - !copy_from_kernel_nofault_allowed(s2__ign, 1)) {
> + if (!copy_from_kernel_nofault_allowed(s1, 1) ||
> + !copy_from_kernel_nofault_allowed(s2, 1)) {
> return -ERANGE;
> }
>
> guard(pagefault)();
> for (i = 0; i < XATTR_SIZE_MAX; i++) {
> - __get_kernel_nofault(&c1, s1__ign, char, err_out);
> - __get_kernel_nofault(&c2, s2__ign, char, err_out);
> + __get_kernel_nofault(&c1, s1, char, err_out);
> + __get_kernel_nofault(&c2, s2, char, err_out);
> + if (ignore_case) {
> + c1 = tolower(c1);
> + c2 = tolower(c2);
> + }
> if (c1 != c2)
> return c1 < c2 ? -1 : 1;
> if (c1 == '\0')
> return 0;
> - s1__ign++;
> - s2__ign++;
> + s1++;
> + s2++;
> }
> return -E2BIG;
> err_out:
> return -EFAULT;
> }
>
> +/**
> + * bpf_strcmp - Compare two strings
> + * @s1__ign: One string
> + * @s2__ign: Another string
> + *
> + * Return:
> + * * %0 - Strings are equal
> + * * %-1 - @s1__ign is smaller
> + * * %1 - @s2__ign is smaller
> + * * %-EFAULT - Cannot read one of the strings
> + * * %-E2BIG - One of strings is too large
> + * * %-ERANGE - One of strings is outside of kernel address space
> + */
> +__bpf_kfunc int bpf_strcmp(const char *s1__ign, const char *s2__ign)
> +{
> + return __bpf_strcasecmp(s1__ign, s2__ign, false);
> +}
> +
> +/**
> + * bpf_strcasecmp - Compare two strings, ignoring the case of the characters
> + * @s1__ign: One string
> + * @s2__ign: Another string
> + *
> + * Return:
> + * * %0 - Strings are equal
> + * * %-1 - @s1__ign is smaller
> + * * %1 - @s2__ign is smaller
> + * * %-EFAULT - Cannot read one of the strings
> + * * %-E2BIG - One of strings is too large
> + * * %-ERANGE - One of strings is outside of kernel address space
> + */
> +__bpf_kfunc int bpf_strcasecmp(const char *s1__ign, const char *s2__ign)
> +{
> + return __bpf_strcasecmp(s1__ign, s2__ign, true);
> +}
> +
> /**
> * bpf_strnchr - Find a character in a length limited string
> * @s__ign: The string to be searched
> @@ -3832,6 +3859,7 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
> #endif
> BTF_ID_FLAGS(func, __bpf_trap)
> BTF_ID_FLAGS(func, bpf_strcmp);
> +BTF_ID_FLAGS(func, bpf_strcasecmp);
> BTF_ID_FLAGS(func, bpf_strchr);
> BTF_ID_FLAGS(func, bpf_strchrnul);
> BTF_ID_FLAGS(func, bpf_strnchr);
next prev parent reply other threads:[~2025-09-02 16:55 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1756804522.git.rtoax@foxmail.com>
2025-09-02 9:19 ` [PATCH bpf-next v3 1/2] bpf: add bpf_strcasecmp kfunc Rong Tao
2025-09-02 9:45 ` Viktor Malik
2025-09-02 16:54 ` Yonghong Song [this message]
2025-09-02 23:49 ` Rong Tao
2025-09-02 9:19 ` [PATCH bpf-next v3 2/2] selftests/bpf: Test kfunc bpf_strcasecmp Rong Tao
2025-09-03 11:35 [PATCH bpf-next v3 1/2] bpf: add bpf_strcasecmp kfunc Sebastian Ramadan
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=e960900d-3811-4b8b-b4b3-bb23048ef5d6@linux.dev \
--to=yonghong.song@linux.dev \
--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=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mykolal@fb.com \
--cc=rongtao@cestc.cn \
--cc=rtoax@foxmail.com \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=vmalik@redhat.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 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.