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 2/8] bpf: Add bpf_dynptr_trim and bpf_dynptr_advance
Date: Wed, 7 Sep 2022 17:02:48 -0700 [thread overview]
Message-ID: <20220908000254.3079129-3-joannelkoong@gmail.com> (raw)
In-Reply-To: <20220908000254.3079129-1-joannelkoong@gmail.com>
Add two new helper functions: bpf_dynptr_trim and bpf_dynptr_advance.
bpf_dynptr_trim decreases the size of a dynptr by the specified
number of bytes (offset remains the same). bpf_dynptr_advance advances
the offset of the dynptr by the specified number of bytes (size
decreases correspondingly).
One example where trimming / advancing the dynptr may useful is for
hashing. If the dynptr points to a larger struct, it is possible to hash
an individual field within the struct through dynptrs by using
bpf_dynptr_advance+trim.
Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
---
include/uapi/linux/bpf.h | 16 +++++++++
kernel/bpf/helpers.c | 63 ++++++++++++++++++++++++++++++++++
tools/include/uapi/linux/bpf.h | 16 +++++++++
3 files changed, 95 insertions(+)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index cce3356765fc..3b054553be30 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -5453,6 +5453,20 @@ union bpf_attr {
* dynptr is invalid or if the offset and length is out of bounds
* or in a paged buffer for skb-type dynptrs or across fragments
* for xdp-type dynptrs.
+ *
+ * long bpf_dynptr_advance(struct bpf_dynptr *ptr, u32 len)
+ * Description
+ * Advance a dynptr by *len*.
+ * Return
+ * 0 on success, -EINVAL if the dynptr is invalid, -ERANGE if *len*
+ * exceeds the bounds of the dynptr.
+ *
+ * long bpf_dynptr_trim(struct bpf_dynptr *ptr, u32 len)
+ * Description
+ * Trim a dynptr by *len*.
+ * Return
+ * 0 on success, -EINVAL if the dynptr is invalid, -ERANGE if
+ * trying to trim more bytes than the size of the dynptr.
*/
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
@@ -5667,6 +5681,8 @@ union bpf_attr {
FN(dynptr_from_skb), \
FN(dynptr_from_xdp), \
FN(dynptr_data_rdonly), \
+ FN(dynptr_advance), \
+ FN(dynptr_trim), \
/* */
/* 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 30a59c9e5df3..9f356105ab49 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1423,6 +1423,13 @@ static u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr)
return ptr->size & DYNPTR_SIZE_MASK;
}
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+ u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+ ptr->size = new_size | metadata;
+}
+
int bpf_dynptr_check_size(u32 size)
{
return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -1646,6 +1653,58 @@ static const struct bpf_func_proto bpf_dynptr_data_proto = {
.arg3_type = ARG_CONST_ALLOC_SIZE_OR_ZERO,
};
+BPF_CALL_2(bpf_dynptr_advance, struct bpf_dynptr_kern *, ptr, u32, len)
+{
+ u32 size;
+
+ if (!ptr->data)
+ return -EINVAL;
+
+ size = bpf_dynptr_get_size(ptr);
+
+ if (len > size)
+ return -ERANGE;
+
+ ptr->offset += len;
+
+ bpf_dynptr_set_size(ptr, size - len);
+
+ return 0;
+}
+
+static const struct bpf_func_proto bpf_dynptr_advance_proto = {
+ .func = bpf_dynptr_advance,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_DYNPTR,
+ .arg2_type = ARG_ANYTHING,
+};
+
+BPF_CALL_2(bpf_dynptr_trim, struct bpf_dynptr_kern *, ptr, u32, len)
+{
+ u32 size;
+
+ if (!ptr->data)
+ return -EINVAL;
+
+ size = bpf_dynptr_get_size(ptr);
+
+ if (len > size)
+ return -ERANGE;
+
+ bpf_dynptr_set_size(ptr, size - len);
+
+ return 0;
+}
+
+static const struct bpf_func_proto bpf_dynptr_trim_proto = {
+ .func = bpf_dynptr_trim,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_DYNPTR,
+ .arg2_type = ARG_ANYTHING,
+};
+
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;
@@ -1718,6 +1777,10 @@ bpf_base_func_proto(enum bpf_func_id func_id)
return &bpf_dynptr_data_proto;
case BPF_FUNC_dynptr_data_rdonly:
return &bpf_dynptr_data_rdonly_proto;
+ case BPF_FUNC_dynptr_advance:
+ return &bpf_dynptr_advance_proto;
+ case BPF_FUNC_dynptr_trim:
+ return &bpf_dynptr_trim_proto;
default:
break;
}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index cce3356765fc..3b054553be30 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -5453,6 +5453,20 @@ union bpf_attr {
* dynptr is invalid or if the offset and length is out of bounds
* or in a paged buffer for skb-type dynptrs or across fragments
* for xdp-type dynptrs.
+ *
+ * long bpf_dynptr_advance(struct bpf_dynptr *ptr, u32 len)
+ * Description
+ * Advance a dynptr by *len*.
+ * Return
+ * 0 on success, -EINVAL if the dynptr is invalid, -ERANGE if *len*
+ * exceeds the bounds of the dynptr.
+ *
+ * long bpf_dynptr_trim(struct bpf_dynptr *ptr, u32 len)
+ * Description
+ * Trim a dynptr by *len*.
+ * Return
+ * 0 on success, -EINVAL if the dynptr is invalid, -ERANGE if
+ * trying to trim more bytes than the size of the dynptr.
*/
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
@@ -5667,6 +5681,8 @@ union bpf_attr {
FN(dynptr_from_skb), \
FN(dynptr_from_xdp), \
FN(dynptr_data_rdonly), \
+ FN(dynptr_advance), \
+ FN(dynptr_trim), \
/* */
/* 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:10 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 ` Joanne Koong [this message]
2022-09-09 15:32 ` [PATCH bpf-next v1 2/8] bpf: Add bpf_dynptr_trim and bpf_dynptr_advance 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 ` [PATCH bpf-next v1 4/8] bpf: Add bpf_dynptr_get_size and bpf_dynptr_get_offset Joanne Koong
2022-09-09 16:52 ` 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-3-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