[PATCH v2 bpf-next] bpf: Add the missing types in the logs

Feng Yang posted 1 patch 1 week, 2 days ago
There is a newer version of this series
kernel/bpf/log.c | 53 +++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 48 insertions(+), 5 deletions(-)
[PATCH v2 bpf-next] bpf: Add the missing types in the logs
Posted by Feng Yang 1 week, 2 days ago
From: Feng Yang <yangfeng@kylinos.cn>

Add the missing types to avoid such uninformative errors as shown below:
R1 type=ptr_ expected=ptr_

Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
---
Changes in v2:
- Group together those for which only 0 or 1 can be selected. thanks, Mykyta Yatsenko.
- Link to v1: https://lore.kernel.org/all/20260128085842.145057-1-yangfeng59949@163.com/
---
 kernel/bpf/log.c | 53 +++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 48 insertions(+), 5 deletions(-)

diff --git a/kernel/bpf/log.c b/kernel/bpf/log.c
index a0c3b35de2ce..c7d08b1ba96f 100644
--- a/kernel/bpf/log.c
+++ b/kernel/bpf/log.c
@@ -432,6 +432,44 @@ static const char *btf_type_name(const struct btf *btf, u32 id)
 	return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off);
 }
 
+static const char *dynptr_reg_type(enum bpf_reg_type type)
+{
+	if (type & DYNPTR_TYPE_LOCAL)
+		return "dynptr_local_";
+	if (type & DYNPTR_TYPE_RINGBUF)
+		return "dynptr_ringbuf_";
+	if (type & DYNPTR_TYPE_SKB)
+		return "dynptr_skb_";
+	if (type & DYNPTR_TYPE_XDP)
+		return "dynptr_xdp_";
+	if (type & DYNPTR_TYPE_SKB_META)
+		return "dynptr_skb_meta_";
+	if (type & DYNPTR_TYPE_FILE)
+		return "dynptr_file_";
+
+	return "";
+}
+
+static const char *trusted_reg_type(enum bpf_reg_type type)
+{
+	if (type & PTR_TRUSTED)
+		return "trusted_";
+	if (type & PTR_UNTRUSTED)
+		return "untrusted_";
+
+	return "";
+}
+
+static const char *rw_reg_type(enum bpf_reg_type type)
+{
+	if (type & MEM_RDONLY)
+		return "rdonly_";
+	if (type & MEM_WRITE)
+		return "write_";
+
+	return "";
+}
+
 /* string representation of 'enum bpf_reg_type'
  *
  * Note that reg_type_str() can not appear more than once in a single verbose()
@@ -439,7 +477,7 @@ static const char *btf_type_name(const struct btf *btf, u32 id)
  */
 const char *reg_type_str(struct bpf_verifier_env *env, enum bpf_reg_type type)
 {
-	char postfix[16] = {0}, prefix[64] = {0};
+	char postfix[16] = {0}, prefix[128] = {0};
 	static const char * const str[] = {
 		[NOT_INIT]		= "?",
 		[SCALAR_VALUE]		= "scalar",
@@ -473,14 +511,19 @@ const char *reg_type_str(struct bpf_verifier_env *env, enum bpf_reg_type type)
 			strscpy(postfix, "_or_null");
 	}
 
-	snprintf(prefix, sizeof(prefix), "%s%s%s%s%s%s%s",
-		 type & MEM_RDONLY ? "rdonly_" : "",
+	snprintf(prefix, sizeof(prefix), "%s%s%s%s%s%s%s%s%s%s%s%s",
+		 rw_reg_type(type),
+		 trusted_reg_type(type),
+		 dynptr_reg_type(type),
 		 type & MEM_RINGBUF ? "ringbuf_" : "",
 		 type & MEM_USER ? "user_" : "",
 		 type & MEM_PERCPU ? "percpu_" : "",
 		 type & MEM_RCU ? "rcu_" : "",
-		 type & PTR_UNTRUSTED ? "untrusted_" : "",
-		 type & PTR_TRUSTED ? "trusted_" : ""
+		 type & MEM_UNINIT ? "uninit_" : "",
+		 type & MEM_FIXED_SIZE ? "fixed_size_" : "",
+		 type & MEM_ALLOC ? "alloc_" : "",
+		 type & NON_OWN_REF ? "non_own_ref_" : "",
+		 type & MEM_ALIGNED ? "aligned_" : ""
 	);
 
 	snprintf(env->tmp_str_buf, TMP_STR_BUF_LEN, "%s%s%s",
-- 
2.43.0