@@ -5553,6 +5553,24 @@ union bpf_attr {
* Return
* 0 on success, -EINVAL if the dynptr is invalid, -ERANGE if
* trying to trim more bytes than the size of the dynptr.
+ *
+ * bool bpf_dynptr_is_null(struct bpf_dynptr *ptr)
+ * Description
+ * Determine whether a dynptr is null / invalid.
+ *
+ * *ptr* must be an initialized dynptr.
+ * Return
+ * True if the dynptr is null, else false.
+ *
+ * bool bpf_dynptr_is_rdonly(struct bpf_dynptr *ptr)
+ * Description
+ * Determine whether a dynptr is read-only.
+ *
+ * *ptr* must be an initialized dynptr. If *ptr*
+ * is a null dynptr, this will return false.
+ * Return
+ * True if the dynptr is read-only and a valid dynptr,
+ * else false.
*/
#define ___BPF_FUNC_MAPPER(FN, ctx...) \
FN(unspec, 0, ##ctx) \
@@ -5771,6 +5789,8 @@ union bpf_attr {
FN(dynptr_from_xdp, 213, ##ctx) \
FN(dynptr_advance, 214, ##ctx) \
FN(dynptr_trim, 215, ##ctx) \
+ FN(dynptr_is_null, 216, ##ctx) \
+ FN(dynptr_is_rdonly, 217, ##ctx) \
/* */
/* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
@@ -1404,7 +1404,7 @@ static const struct bpf_func_proto bpf_kptr_xchg_proto = {
#define DYNPTR_SIZE_MASK 0xFFFFFF
#define DYNPTR_RDONLY_BIT BIT(31)
-static bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
+static bool __bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
{
return ptr->size & DYNPTR_RDONLY_BIT;
}
@@ -1547,7 +1547,7 @@ BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *,
enum bpf_dynptr_type type;
int err;
- if (!dst->data || bpf_dynptr_is_rdonly(dst))
+ if (!dst->data || __bpf_dynptr_is_rdonly(dst))
return -EINVAL;
err = bpf_dynptr_check_off_len(dst, offset, len);
@@ -1605,7 +1605,7 @@ BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len
switch (type) {
case BPF_DYNPTR_TYPE_LOCAL:
case BPF_DYNPTR_TYPE_RINGBUF:
- if (bpf_dynptr_is_rdonly(ptr))
+ if (__bpf_dynptr_is_rdonly(ptr))
return 0;
data = ptr->data;
@@ -1703,6 +1703,33 @@ static const struct bpf_func_proto bpf_dynptr_trim_proto = {
.arg2_type = ARG_ANYTHING,
};
+BPF_CALL_1(bpf_dynptr_is_null, struct bpf_dynptr_kern *, ptr)
+{
+ return !ptr->data;
+}
+
+static const struct bpf_func_proto bpf_dynptr_is_null_proto = {
+ .func = bpf_dynptr_is_null,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_DYNPTR,
+};
+
+BPF_CALL_1(bpf_dynptr_is_rdonly, struct bpf_dynptr_kern *, ptr)
+{
+ if (!ptr->data)
+ return false;
+
+ return __bpf_dynptr_is_rdonly(ptr);
+}
+
+static const struct bpf_func_proto bpf_dynptr_is_rdonly_proto = {
+ .func = bpf_dynptr_is_rdonly,
+ .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;
@@ -1811,6 +1838,10 @@ bpf_base_func_proto(enum bpf_func_id func_id)
return &bpf_dynptr_advance_proto;
case BPF_FUNC_dynptr_trim:
return &bpf_dynptr_trim_proto;
+ case BPF_FUNC_dynptr_is_null:
+ return &bpf_dynptr_is_null_proto;
+ case BPF_FUNC_dynptr_is_rdonly:
+ return &bpf_dynptr_is_rdonly_proto;
#ifdef CONFIG_CGROUPS
case BPF_FUNC_cgrp_storage_get:
return &bpf_cgrp_storage_get_proto;
@@ -710,6 +710,7 @@ class PrinterHelpers(Printer):
'int',
'long',
'unsigned long',
+ 'bool',
'__be16',
'__be32',
@@ -781,6 +782,8 @@ class PrinterHelpers(Printer):
header = '''\
/* This is auto-generated file. See bpf_doc.py for details. */
+#include <stdbool.h>
+
/* Forward declarations of BPF structs */'''
print(header)
@@ -5553,6 +5553,24 @@ union bpf_attr {
* Return
* 0 on success, -EINVAL if the dynptr is invalid, -ERANGE if
* trying to trim more bytes than the size of the dynptr.
+ *
+ * bool bpf_dynptr_is_null(struct bpf_dynptr *ptr)
+ * Description
+ * Determine whether a dynptr is null / invalid.
+ *
+ * *ptr* must be an initialized dynptr.
+ * Return
+ * True if the dynptr is null, else false.
+ *
+ * bool bpf_dynptr_is_rdonly(struct bpf_dynptr *ptr)
+ * Description
+ * Determine whether a dynptr is read-only.
+ *
+ * *ptr* must be an initialized dynptr. If *ptr*
+ * is a null dynptr, this will return false.
+ * Return
+ * True if the dynptr is read-only and a valid dynptr,
+ * else false.
*/
#define ___BPF_FUNC_MAPPER(FN, ctx...) \
FN(unspec, 0, ##ctx) \
@@ -5771,6 +5789,8 @@ union bpf_attr {
FN(dynptr_from_xdp, 213, ##ctx) \
FN(dynptr_advance, 214, ##ctx) \
FN(dynptr_trim, 215, ##ctx) \
+ FN(dynptr_is_null, 216, ##ctx) \
+ FN(dynptr_is_rdonly, 217, ##ctx) \
/* */
/* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
Add two new helper functions: bpf_dynptr_is_null and bpf_dynptr_is_rdonly. bpf_dynptr_is_null returns true if the dynptr is null / invalid (determined by whether ptr->data is NULL), else false if the dynptr is a valid dynptr. bpf_dynptr_is_rdonly returns true if the dynptr is read-only, else false if the dynptr is read-writable. Signed-off-by: Joanne Koong <joannelkoong@gmail.com> --- include/uapi/linux/bpf.h | 20 ++++++++++++++++++ kernel/bpf/helpers.c | 37 +++++++++++++++++++++++++++++++--- scripts/bpf_doc.py | 3 +++ tools/include/uapi/linux/bpf.h | 20 ++++++++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-)