From: Joanne Koong <joannelkoong@gmail.com>
To: bpf@vger.kernel.org
Cc: daniel@iogearbox.net, martin.lau@kernel.org, andrii@kernel.org,
ast@kernel.org, Kernel-team@fb.com,
Joanne Koong <joannelkoong@gmail.com>
Subject: [PATCH bpf-next v1 4/8] bpf: Add bpf_dynptr_get_size and bpf_dynptr_get_offset
Date: Wed, 7 Sep 2022 17:02:50 -0700 [thread overview]
Message-ID: <20220908000254.3079129-5-joannelkoong@gmail.com> (raw)
In-Reply-To: <20220908000254.3079129-1-joannelkoong@gmail.com>
Add two new helper functions: bpf_dynptr_get_size and
bpf_dynptr_get_offset.
bpf_dynptr_get_size returns the number of usable bytes in a dynptr and
bpf_dynptr_get_offset returns the current offset into the dynptr.
Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
---
include/uapi/linux/bpf.h | 25 ++++++++++++++++++++
kernel/bpf/helpers.c | 42 ++++++++++++++++++++++++++++++----
tools/include/uapi/linux/bpf.h | 25 ++++++++++++++++++++
3 files changed, 88 insertions(+), 4 deletions(-)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 90b6d0744df2..4ca07cf500d2 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -5485,6 +5485,29 @@ union bpf_attr {
* Return
* True if the dynptr is read-only and a valid dynptr,
* else false.
+ *
+ * long bpf_dynptr_get_size(struct bpf_dynptr *ptr)
+ * Description
+ * Get the size of *ptr*.
+ *
+ * Size refers to the number of usable bytes. For example,
+ * if *ptr* was initialized with 100 bytes and its
+ * offset was advanced by 40 bytes, then the size will be
+ * 60 bytes.
+ *
+ * *ptr* must be an initialized dynptr.
+ * Return
+ * The size of the dynptr on success, -EINVAL if the dynptr is
+ * invalid.
+ *
+ * long bpf_dynptr_get_offset(struct bpf_dynptr *ptr)
+ * Description
+ * Get the offset of the dynptr.
+ *
+ * *ptr* must be an initialized dynptr.
+ * Return
+ * The offset of the dynptr on success, -EINVAL if the dynptr is
+ * invalid.
*/
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
@@ -5703,6 +5726,8 @@ union bpf_attr {
FN(dynptr_trim), \
FN(dynptr_is_null), \
FN(dynptr_is_rdonly), \
+ FN(dynptr_get_size), \
+ FN(dynptr_get_offset), \
/* */
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 8729383d0966..62ed27444b73 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1418,7 +1418,7 @@ static enum bpf_dynptr_type bpf_dynptr_get_type(const struct bpf_dynptr_kern *pt
return (ptr->size & ~(DYNPTR_RDONLY_BIT)) >> DYNPTR_TYPE_SHIFT;
}
-static u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr)
+static u32 __bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr)
{
return ptr->size & DYNPTR_SIZE_MASK;
}
@@ -1451,7 +1451,7 @@ void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
static int bpf_dynptr_check_off_len(struct bpf_dynptr_kern *ptr, u32 offset, u32 len)
{
- u32 size = bpf_dynptr_get_size(ptr);
+ u32 size = __bpf_dynptr_get_size(ptr);
if (len > size || offset > size - len)
return -E2BIG;
@@ -1660,7 +1660,7 @@ BPF_CALL_2(bpf_dynptr_advance, struct bpf_dynptr_kern *, ptr, u32, len)
if (!ptr->data)
return -EINVAL;
- size = bpf_dynptr_get_size(ptr);
+ size = __bpf_dynptr_get_size(ptr);
if (len > size)
return -ERANGE;
@@ -1687,7 +1687,7 @@ BPF_CALL_2(bpf_dynptr_trim, struct bpf_dynptr_kern *, ptr, u32, len)
if (!ptr->data)
return -EINVAL;
- size = bpf_dynptr_get_size(ptr);
+ size = __bpf_dynptr_get_size(ptr);
if (len > size)
return -ERANGE;
@@ -1732,6 +1732,36 @@ static const struct bpf_func_proto bpf_dynptr_is_rdonly_proto = {
.arg1_type = ARG_PTR_TO_DYNPTR,
};
+BPF_CALL_1(bpf_dynptr_get_size, struct bpf_dynptr_kern *, ptr)
+{
+ if (!ptr->data)
+ return -EINVAL;
+
+ return __bpf_dynptr_get_size(ptr);
+}
+
+static const struct bpf_func_proto bpf_dynptr_get_size_proto = {
+ .func = bpf_dynptr_get_size,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_DYNPTR,
+};
+
+BPF_CALL_1(bpf_dynptr_get_offset, struct bpf_dynptr_kern *, ptr)
+{
+ if (!ptr->data)
+ return -EINVAL;
+
+ return ptr->offset;
+}
+
+static const struct bpf_func_proto bpf_dynptr_get_offset_proto = {
+ .func = bpf_dynptr_get_offset,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_DYNPTR,
+};
+
const struct bpf_func_proto bpf_get_current_task_proto __weak;
const struct bpf_func_proto bpf_get_current_task_btf_proto __weak;
const struct bpf_func_proto bpf_probe_read_user_proto __weak;
@@ -1812,6 +1842,10 @@ bpf_base_func_proto(enum bpf_func_id func_id)
return &bpf_dynptr_is_null_proto;
case BPF_FUNC_dynptr_is_rdonly:
return &bpf_dynptr_is_rdonly_proto;
+ case BPF_FUNC_dynptr_get_size:
+ return &bpf_dynptr_get_size_proto;
+ case BPF_FUNC_dynptr_get_offset:
+ return &bpf_dynptr_get_offset_proto;
default:
break;
}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 90b6d0744df2..4ca07cf500d2 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -5485,6 +5485,29 @@ union bpf_attr {
* Return
* True if the dynptr is read-only and a valid dynptr,
* else false.
+ *
+ * long bpf_dynptr_get_size(struct bpf_dynptr *ptr)
+ * Description
+ * Get the size of *ptr*.
+ *
+ * Size refers to the number of usable bytes. For example,
+ * if *ptr* was initialized with 100 bytes and its
+ * offset was advanced by 40 bytes, then the size will be
+ * 60 bytes.
+ *
+ * *ptr* must be an initialized dynptr.
+ * Return
+ * The size of the dynptr on success, -EINVAL if the dynptr is
+ * invalid.
+ *
+ * long bpf_dynptr_get_offset(struct bpf_dynptr *ptr)
+ * Description
+ * Get the offset of the dynptr.
+ *
+ * *ptr* must be an initialized dynptr.
+ * Return
+ * The offset of the dynptr on success, -EINVAL if the dynptr is
+ * invalid.
*/
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
@@ -5703,6 +5726,8 @@ union bpf_attr {
FN(dynptr_trim), \
FN(dynptr_is_null), \
FN(dynptr_is_rdonly), \
+ FN(dynptr_get_size), \
+ FN(dynptr_get_offset), \
/* */
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
--
2.30.2
next prev parent reply other threads:[~2022-09-08 0:07 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-08 0:02 [PATCH bpf-next v1 0/8] Dynptr convenience helpers Joanne Koong
2022-09-08 0:02 ` [PATCH bpf-next v1 1/8] bpf: Add bpf_dynptr_data_rdonly Joanne Koong
2022-09-09 15:29 ` Song Liu
2022-09-09 15:32 ` Alexei Starovoitov
2022-09-09 15:59 ` Song Liu
2022-09-09 15:51 ` Shmulik Ladkani
2022-09-08 0:02 ` [PATCH bpf-next v1 2/8] bpf: Add bpf_dynptr_trim and bpf_dynptr_advance Joanne Koong
2022-09-09 15:32 ` Song Liu
2022-09-09 16:16 ` Shmulik Ladkani
2022-09-28 22:14 ` Andrii Nakryiko
2022-09-08 0:02 ` [PATCH bpf-next v1 3/8] bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly Joanne Koong
2022-09-09 15:46 ` Song Liu
2022-09-09 21:28 ` Joanne Koong
2022-09-09 23:17 ` Song Liu
2022-09-08 0:02 ` Joanne Koong [this message]
2022-09-09 16:52 ` [PATCH bpf-next v1 4/8] bpf: Add bpf_dynptr_get_size and bpf_dynptr_get_offset Shmulik Ladkani
2022-09-09 20:37 ` Joanne Koong
2022-09-08 0:02 ` [PATCH bpf-next v1 5/8] bpf: Add bpf_dynptr_clone Joanne Koong
2022-09-09 16:41 ` Shmulik Ladkani
2022-09-09 22:18 ` Joanne Koong
2022-09-10 5:31 ` Shmulik Ladkani
2022-09-28 22:34 ` Andrii Nakryiko
2022-09-28 22:29 ` Andrii Nakryiko
2022-09-08 0:02 ` [PATCH bpf-next v1 6/8] bpf: Add verifier support for custom callback return range Joanne Koong
2022-09-08 0:02 ` [PATCH bpf-next v1 7/8] bpf: Add bpf_dynptr_iterator Joanne Koong
2022-09-19 0:07 ` Kumar Kartikeya Dwivedi
2022-09-28 22:47 ` Andrii Nakryiko
2022-09-28 22:41 ` Andrii Nakryiko
2022-09-29 0:31 ` Kumar Kartikeya Dwivedi
2022-09-29 0:43 ` Andrii Nakryiko
2022-10-02 16:45 ` Kumar Kartikeya Dwivedi
2022-10-03 18:39 ` Andrii Nakryiko
2022-09-08 0:02 ` [PATCH bpf-next v1 8/8] selftests/bpf: Tests for dynptr convenience helpers Joanne Koong
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=20220908000254.3079129-5-joannelkoong@gmail.com \
--to=joannelkoong@gmail.com \
--cc=Kernel-team@fb.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=martin.lau@kernel.org \
/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