]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
libbpf: Allow use of feature cache for non-token cases
authorAlan Maguire <alan.maguire@oracle.com>
Wed, 8 Apr 2026 16:57:34 +0000 (17:57 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 10 Apr 2026 19:34:36 +0000 (12:34 -0700)
Allow bpf object feat_cache assignment in BPF selftests
to simulate missing features via inclusion of libbpf_internal.h
and use of bpf_object_set_feat_cache() and bpf_object__sanitize_btf() to
test BTF sanitization for cases where missing features are simulated.

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Link: https://lore.kernel.org/r/20260408165735.843763-2-alan.maguire@oracle.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf_internal.h

index 42bdba4efd0c67696c9f9b0623dccd93ddf47a06..8b0c3246097f5c0f06b94c5dac5d11f5c8240a30 100644 (file)
@@ -3145,7 +3145,7 @@ static bool btf_needs_sanitization(struct bpf_object *obj)
               !has_layout;
 }
 
-static struct btf *bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *orig_btf)
+struct btf *bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *orig_btf)
 {
        bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC);
        bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
@@ -5203,12 +5203,20 @@ bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
                 */
                return true;
 
-       if (obj->token_fd)
+       if (obj->feat_cache)
                return feat_supported(obj->feat_cache, feat_id);
 
        return feat_supported(NULL, feat_id);
 }
 
+/* Used in testing to simulate missing features. */
+void bpf_object_set_feat_cache(struct bpf_object *obj, struct kern_feature_cache *cache)
+{
+       if (obj->feat_cache)
+               free(obj->feat_cache);
+       obj->feat_cache = cache;
+}
+
 static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
 {
        struct bpf_map_info map_info;
index cabdaef790982946931bba69ef5ed71fcd9b31c5..3781c45b46d3bfcd372ecc8bcc47447f26742730 100644 (file)
@@ -414,6 +414,7 @@ struct kern_feature_cache {
 
 bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id);
 bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id);
+void bpf_object_set_feat_cache(struct bpf_object *obj, struct kern_feature_cache *cache);
 
 int probe_kern_syscall_wrapper(int token_fd);
 int probe_memcg_account(int token_fd);
@@ -427,7 +428,7 @@ int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
 int libbpf__load_raw_btf_hdr(const struct btf_header *hdr,
                             const char *raw_types, const char *str_sec,
                             const char *layout_sec, int token_fd);
-
+struct btf *bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *orig_btf);
 int btf_load_into_kernel(struct btf *btf,
                         char *log_buf, size_t log_sz, __u32 log_level,
                         int token_fd);