Re: [PATCH v3 bpf-next 2/6] libbpf: Add BTF_KIND_FLOAT support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 2/19/21 7:49 PM, Ilya Leoshkevich wrote:
The logic follows that of BTF_KIND_INT most of the time. Sanitization
replaces BTF_KIND_FLOATs with BTF_KIND_CONSTs pointing to
equally-sized BTF_KIND_ARRAYs on older kernels.

Signed-off-by: Ilya Leoshkevich <iii@xxxxxxxxxxxxx>
---
  tools/lib/bpf/btf.c             | 44 ++++++++++++++++++++++++++++++
  tools/lib/bpf/btf.h             |  8 ++++++
  tools/lib/bpf/btf_dump.c        |  4 +++
  tools/lib/bpf/libbpf.c          | 48 ++++++++++++++++++++++++++++++++-
  tools/lib/bpf/libbpf.map        |  5 ++++
  tools/lib/bpf/libbpf_internal.h |  2 ++
  6 files changed, 110 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index fdfff544f59a..1ebfcc687dab 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -292,6 +292,7 @@ static int btf_type_size(const struct btf_type *t)
  	case BTF_KIND_PTR:
  	case BTF_KIND_TYPEDEF:
  	case BTF_KIND_FUNC:
+	case BTF_KIND_FLOAT:
  		return base_size;
  	case BTF_KIND_INT:
  		return base_size + sizeof(__u32);
@@ -339,6 +340,7 @@ static int btf_bswap_type_rest(struct btf_type *t)
  	case BTF_KIND_PTR:
  	case BTF_KIND_TYPEDEF:
  	case BTF_KIND_FUNC:
+	case BTF_KIND_FLOAT:
  		return 0;
  	case BTF_KIND_INT:
  		*(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
@@ -579,6 +581,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
  		case BTF_KIND_UNION:
  		case BTF_KIND_ENUM:
  		case BTF_KIND_DATASEC:
+		case BTF_KIND_FLOAT:
  			size = t->size;
  			goto done;
  		case BTF_KIND_PTR:
@@ -622,6 +625,7 @@ int btf__align_of(const struct btf *btf, __u32 id)
  	switch (kind) {
  	case BTF_KIND_INT:
  	case BTF_KIND_ENUM:
+	case BTF_KIND_FLOAT:
  		return min(btf_ptr_sz(btf), (size_t)t->size);
  	case BTF_KIND_PTR:
  		return btf_ptr_sz(btf);
@@ -2400,6 +2404,42 @@ int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz)
  	return btf_commit_type(btf, sz);
  }
[...]

I skipped deduplication part, Andrii can take a look later.

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index d43cc3f29dae..b79a6a2e6faa 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -178,6 +178,8 @@ enum kern_feature_id {
  	FEAT_PROG_BIND_MAP,
  	/* Kernel support for module BTFs */
  	FEAT_MODULE_BTF,
+	/* BTF_KIND_FLOAT support */
+	FEAT_BTF_FLOAT,
  	__FEAT_CNT,
  };
@@ -1935,6 +1937,7 @@ static const char *btf_kind_str(const struct btf_type *t)
  	case BTF_KIND_FUNC_PROTO: return "func_proto";
  	case BTF_KIND_VAR: return "var";
  	case BTF_KIND_DATASEC: return "datasec";
+	case BTF_KIND_FLOAT: return "float";
  	default: return "unknown";
  	}
  }
@@ -2384,18 +2387,22 @@ static bool btf_needs_sanitization(struct bpf_object *obj)
  {
  	bool has_func_global = kernel_supports(FEAT_BTF_GLOBAL_FUNC);
  	bool has_datasec = kernel_supports(FEAT_BTF_DATASEC);
+	bool has_float = kernel_supports(FEAT_BTF_FLOAT);
  	bool has_func = kernel_supports(FEAT_BTF_FUNC);
- return !has_func || !has_datasec || !has_func_global;
+	return !has_func || !has_datasec || !has_func_global || !has_float;
  }
static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
  {
  	bool has_func_global = kernel_supports(FEAT_BTF_GLOBAL_FUNC);
  	bool has_datasec = kernel_supports(FEAT_BTF_DATASEC);
+	bool has_float = kernel_supports(FEAT_BTF_FLOAT);
  	bool has_func = kernel_supports(FEAT_BTF_FUNC);
  	struct btf_type *t;
+	int t_uchar = 0;
  	int i, j, vlen;
+	int t_uint = 0;
for (i = 1; i <= btf__get_nr_types(btf); i++) {
  		t = (struct btf_type *)btf__type_by_id(btf, i);
@@ -2445,6 +2452,26 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
  		} else if (!has_func_global && btf_is_func(t)) {
  			/* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
  			t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);
+		} else if (!has_float && btf_is_float(t)) {
+			/* replace FLOAT with TYPEDEF(u8[]) */

replace FLOAT with CONST(u8[]).

+			int t_array;
+			__u32 size;
+
+			size = t->size;
+			if (!t_uchar)
+				t_uchar = btf__add_int(btf, "unsigned char", 1,
+						       0);
+			if (!t_uint)
+				t_uint = btf__add_int(btf, "unsigned int", 4,
+						      0);
+			t_array = btf__add_array(btf, t_uint, t_uchar, size);

Checking whether t_array is valid here or not?
We do not need to check t_uchar/t_uint. If they are invalid btf__add_array() will return -EINVAL.

+
+			/* adding new types may have invalidated t */
+			t = (struct btf_type *)btf__type_by_id(btf, i);
+
+			t->name_off = 0;
+			t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0);
+			t->type = t_array;
  		}
  	}
  }
@@ -3882,6 +3909,18 @@ static int probe_kern_btf_datasec(void)
  					     strs, sizeof(strs)));
  }
+static int probe_kern_btf_float(void)
+{
+	static const char strs[] = "\0float";
+	__u32 types[] = {
+		/* float */
+		BTF_TYPE_FLOAT_ENC(1, 4),
+	};
+
+	return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
+					     strs, sizeof(strs)));
+}
+
  static int probe_kern_array_mmap(void)
  {
  	struct bpf_create_map_attr attr = {
@@ -4061,6 +4100,9 @@ static struct kern_feature_desc {
  	[FEAT_MODULE_BTF] = {
  		"module BTF support", probe_module_btf,
  	},
+	[FEAT_BTF_FLOAT] = {
+		"BTF_KIND_FLOAT support", probe_kern_btf_float,
+	},
  };
static bool kernel_supports(enum kern_feature_id feat_id)
@@ -4940,6 +4982,8 @@ static int bpf_core_fields_are_compat(const struct btf *local_btf,
  		local_id = btf_array(local_type)->type;
  		targ_id = btf_array(targ_type)->type;
  		goto recur;
+	case BTF_KIND_FLOAT:
+		return local_type->size == targ_type->size;
  	default:
  		pr_warn("unexpected kind %d relocated, local [%d], target [%d]\n",
  			btf_kind(local_type), local_id, targ_id);
@@ -5122,6 +5166,8 @@ static int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id
  		skip_mods_and_typedefs(targ_btf, targ_type->type, &targ_id);
  		goto recur;
  	}
+	case BTF_KIND_FLOAT:
+		return local_type->size == targ_type->size;
  	default:
  		pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
  			btf_kind_str(local_type), local_id, targ_id);
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index 1c0fd2dd233a..ec898f464ab9 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -350,3 +350,8 @@ LIBBPF_0.3.0 {
  		xsk_setup_xdp_prog;
  		xsk_socket__update_xskmap;
  } LIBBPF_0.2.0;
+
+LIBBPF_0.4.0 {
+	global:
+		btf__add_float;
+} LIBBPF_0.3.0;
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index 969d0ac592ba..343f6eb05637 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -31,6 +31,8 @@
  #define BTF_MEMBER_ENC(name, type, bits_offset) (name), (type), (bits_offset)
  #define BTF_PARAM_ENC(name, type) (name), (type)
  #define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
+#define BTF_TYPE_FLOAT_ENC(name, sz) \
+	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
#ifndef likely
  #define likely(x) __builtin_expect(!!(x), 1)




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux