AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/09/20 00:13 flow patch-triage
0m Args:
null

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [],
  "KMSANReasoning": "",
  "NeedsKMSAN": false,
  "Reasoning": "All changes in this patch series are strictly confined to user-space directories (tools/lib/bpf and tools/testing/selftests/bpf), introducing manual load strategy APIs to libbpf and accompanying selftests. No kernel-space code (vmlinux or kernel modules) is modified.",
  "WorthFuzzing": false
}

1/1 2026/09/20 00:13 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 565b35bc1051fe8c593bdd93bf89266d0eb31d01\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Sun Sep 20 00:13:27 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c\nindex 613afae265199..3ac7319ed0f40 100644\n--- a/tools/lib/bpf/libbpf.c\n+++ b/tools/lib/bpf/libbpf.c\n@@ -494,8 +494,9 @@ struct bpf_program {\n \tstruct bpf_object *obj;\n \n \tint fd;\n-\tbool autoload;\n+\tenum bpf_prog_load_strategy load_strategy;\n \tbool autoattach;\n+\tbool saved_autoattach;\n \tbool sym_global;\n \tbool mark_btf_static;\n \tenum bpf_prog_type type;\n@@ -725,6 +726,7 @@ struct bpf_object {\n \n \tbool has_subcalls;\n \tbool has_rodata;\n+\tbool has_manual_progs;\n \n \tstruct bpf_gen *gen_loader;\n \n@@ -870,16 +872,25 @@ bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,\n \t/* libbpf's convention for SEC(\"?abc...\") is that it's just like\n \t * SEC(\"abc...\") but the corresponding bpf_program starts out with\n \t * autoload set to false.\n+\t *\n+\t * Similarly, SEC(\"!abc...\") marks the program for manual loading:\n+\t * it is skipped by the bulk auto-load pass and must be explicitly\n+\t * loaded later via bpf_program__load_manually().\n \t */\n \tif (sec_name[0] == '?') {\n-\t\tprog-\u003eautoload = false;\n+\t\tprog-\u003eload_strategy = BPF_PROG_LOAD_STRATEGY_DISABLED;\n \t\t/* from now on forget there was ? in section name */\n \t\tsec_name++;\n+\t} else if (sec_name[0] == '!') {\n+\t\tprog-\u003eload_strategy = BPF_PROG_LOAD_STRATEGY_MANUAL;\n+\t\t/* from now on forget there was ! in section name */\n+\t\tsec_name++;\n \t} else {\n-\t\tprog-\u003eautoload = true;\n+\t\tprog-\u003eload_strategy = BPF_PROG_LOAD_STRATEGY_AUTO;\n \t}\n \n-\tprog-\u003eautoattach = true;\n+\tprog-\u003esaved_autoattach = true;\n+\tprog-\u003eautoattach = prog-\u003eload_strategy != BPF_PROG_LOAD_STRATEGY_MANUAL;\n \n \t/* inherit object's log_level */\n \tprog-\u003elog_level = obj-\u003elog_level;\n@@ -1163,7 +1174,8 @@ static int bpf_object_adjust_struct_ops_autoload(struct bpf_object *obj)\n \t\t\t}\n \t\t}\n \t\tif (use_cnt)\n-\t\t\tprog-\u003eautoload = should_load;\n+\t\t\tprog-\u003eload_strategy = should_load ? BPF_PROG_LOAD_STRATEGY_AUTO\n+\t\t\t\t: BPF_PROG_LOAD_STRATEGY_DISABLED;\n \t}\n \n \treturn 0;\n@@ -1248,7 +1260,7 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map)\n \t\t\t\t * then bpf_object_adjust_struct_ops_autoload() will update its\n \t\t\t\t * autoload accordingly.\n \t\t\t\t */\n-\t\t\t\tst_ops-\u003eprogs[i]-\u003eautoload = false;\n+\t\t\t\tst_ops-\u003eprogs[i]-\u003eload_strategy = BPF_PROG_LOAD_STRATEGY_DISABLED;\n \t\t\t\tst_ops-\u003eprogs[i] = NULL;\n \t\t\t}\n \n@@ -1287,7 +1299,7 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map)\n \t\t\t * if user replaced it with another program or NULL\n \t\t\t */\n \t\t\tif (st_ops-\u003eprogs[i] \u0026\u0026 st_ops-\u003eprogs[i] != prog)\n-\t\t\t\tst_ops-\u003eprogs[i]-\u003eautoload = false;\n+\t\t\t\tst_ops-\u003eprogs[i]-\u003eload_strategy = BPF_PROG_LOAD_STRATEGY_DISABLED;\n \n \t\t\t/* Update the value from the shadow type */\n \t\t\tst_ops-\u003eprogs[i] = prog;\n@@ -3587,7 +3599,7 @@ static bool obj_needs_vmlinux_btf(const struct bpf_object *obj)\n \t}\n \n \tbpf_object__for_each_program(prog, obj) {\n-\t\tif (!prog-\u003eautoload)\n+\t\tif (prog-\u003eload_strategy == BPF_PROG_LOAD_STRATEGY_DISABLED)\n \t\t\tcontinue;\n \t\tif (prog_needs_vmlinux_btf(prog))\n \t\t\treturn true;\n@@ -6181,7 +6193,7 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)\n \t\t\t/* no need to apply CO-RE relocation if the program is\n \t\t\t * not going to be loaded\n \t\t\t */\n-\t\t\tif (!prog-\u003eautoload)\n+\t\t\tif (prog-\u003eload_strategy == BPF_PROG_LOAD_STRATEGY_DISABLED)\n \t\t\t\tcontinue;\n \n \t\t\t/* adjust insn_idx from section frame of reference to the local\n@@ -7525,7 +7537,7 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat\n \t\t */\n \t\tif (prog_is_subprog(obj, prog))\n \t\t\tcontinue;\n-\t\tif (!prog-\u003eautoload)\n+\t\tif (prog-\u003eload_strategy == BPF_PROG_LOAD_STRATEGY_DISABLED)\n \t\t\tcontinue;\n \n \t\terr = bpf_object__relocate_calls(obj, prog);\n@@ -7561,7 +7573,7 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat\n \t\tprog = \u0026obj-\u003eprograms[i];\n \t\tif (prog_is_subprog(obj, prog))\n \t\t\tcontinue;\n-\t\tif (!prog-\u003eautoload)\n+\t\tif (prog-\u003eload_strategy == BPF_PROG_LOAD_STRATEGY_DISABLED)\n \t\t\tcontinue;\n \n \t\t/* Process data relos for main programs */\n@@ -8057,7 +8069,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog\n \t\t\tlog_buf = prog-\u003elog_buf;\n \t\t\tlog_buf_size = prog-\u003elog_size;\n \t\t\town_log_buf = false;\n-\t\t} else if (obj-\u003elog_buf) {\n+\t\t} else if (obj-\u003elog_buf \u0026\u0026 prog-\u003eload_strategy != BPF_PROG_LOAD_STRATEGY_MANUAL) {\n \t\t\tlog_buf = obj-\u003elog_buf;\n \t\t\tlog_buf_size = obj-\u003elog_size;\n \t\t\town_log_buf = false;\n@@ -8400,10 +8412,11 @@ bpf_object__load_progs(struct bpf_object *obj, int log_level)\n \t\tprog = \u0026obj-\u003eprograms[i];\n \t\tif (prog_is_subprog(obj, prog))\n \t\t\tcontinue;\n-\t\tif (!prog-\u003eautoload) {\n-\t\t\tpr_debug(\"prog '%s': skipped loading\\n\", prog-\u003ename);\n+\t\tif (prog-\u003eload_strategy != BPF_PROG_LOAD_STRATEGY_AUTO) {\n+\t\t\tpr_debug(\"prog '%s': skipped auto-loading\\n\", prog-\u003ename);\n \t\t\tcontinue;\n \t\t}\n+\n \t\tprog-\u003elog_level |= log_level;\n \n \t\tif (obj-\u003egen_loader)\n@@ -8429,6 +8442,8 @@ static int bpf_object_prepare_progs(struct bpf_object *obj)\n \n \tfor (i = 0; i \u003c obj-\u003enr_programs; i++) {\n \t\tprog = \u0026obj-\u003eprograms[i];\n+\t\tif (prog-\u003eload_strategy == BPF_PROG_LOAD_STRATEGY_MANUAL)\n+\t\t\tobj-\u003ehas_manual_progs = true;\n \t\terr = bpf_object__sanitize_prog(obj, prog);\n \t\tif (err)\n \t\t\treturn err;\n@@ -9062,33 +9077,50 @@ static void bpf_object_unpin(struct bpf_object *obj)\n \t\t\tbpf_map__unpin(\u0026obj-\u003emaps[i], NULL);\n }\n \n-static void bpf_object_cleanup_btf(struct bpf_object *obj)\n+static void bpf_object_cleanup_btf(struct bpf_object *obj, bool force)\n {\n \tint i;\n \n-\t/* clean up module BTFs */\n-\tfor (i = 0; i \u003c obj-\u003ebtf_module_cnt; i++) {\n-\t\tclose(obj-\u003ebtf_modules[i].fd);\n-\t\tbtf__free(obj-\u003ebtf_modules[i].btf);\n-\t\tfree(obj-\u003ebtf_modules[i].name);\n+\t/*\n+\t * Module BTF fds may still be borrowed (via fd_array,\n+\t * attach_btf_obj_fd, or baked into relocated instructions) by\n+\t * programs that have not been manually loaded yet, so defer\n+\t * closing them in that case to the end of the object lifetime,\n+\t * unless the caller forces immediate cleanup.\n+\t */\n+\tif (force || !obj-\u003ehas_manual_progs) {\n+\t\tfor (i = 0; i \u003c obj-\u003ebtf_module_cnt; i++) {\n+\t\t\tclose(obj-\u003ebtf_modules[i].fd);\n+\t\t\tbtf__free(obj-\u003ebtf_modules[i].btf);\n+\t\t\tfree(obj-\u003ebtf_modules[i].name);\n+\t\t}\n+\t\tobj-\u003ebtf_module_cnt = 0;\n+\t\tobj-\u003ebtf_module_cap = 0;\n+\t\tobj-\u003ebtf_modules_loaded = false;\n+\t\tzfree(\u0026obj-\u003ebtf_modules);\n \t}\n-\tobj-\u003ebtf_module_cnt = 0;\n-\tobj-\u003ebtf_module_cap = 0;\n-\tobj-\u003ebtf_modules_loaded = false;\n-\tzfree(\u0026obj-\u003ebtf_modules);\n \n-\t/* clean up vmlinux BTF */\n-\tbtf__free(obj-\u003ebtf_vmlinux);\n-\tobj-\u003ebtf_vmlinux = NULL;\n+\t/*\n+\t * The btf_vmlinux data is needed for manually loaded programs,\n+\t * so defer freeing it in that case to the end of the object lifetime.\n+\t */\n+\tif (force || !obj-\u003ehas_manual_progs) {\n+\t\tbtf__free(obj-\u003ebtf_vmlinux);\n+\t\tobj-\u003ebtf_vmlinux = NULL;\n+\t}\n }\n \n-static void bpf_object_post_load_cleanup(struct bpf_object *obj)\n+static void bpf_object_post_load_cleanup(struct bpf_object *obj, bool force)\n {\n-\t/* clean up fd_array */\n-\tzfree(\u0026obj-\u003efd_array);\n+\t/*\n+\t * The fd array is needed for manually loaded programs,\n+\t * so defer freeing it in that case to the end of the object lifetime.\n+\t */\n+\tif (force || !obj-\u003ehas_manual_progs || !obj-\u003efd_array_cnt)\n+\t\tzfree(\u0026obj-\u003efd_array);\n \n \t/* clean up BTF */\n-\tbpf_object_cleanup_btf(obj);\n+\tbpf_object_cleanup_btf(obj, force);\n }\n \n static int bpf_object_prepare(struct bpf_object *obj, const char *target_btf_path)\n@@ -9163,7 +9195,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch\n \t\t\terr = bpf_gen__finish(obj-\u003egen_loader, obj-\u003enr_programs, obj-\u003enr_maps);\n \t}\n \n-\tbpf_object_post_load_cleanup(obj);\n+\tbpf_object_post_load_cleanup(obj, false);\n \tobj-\u003estate = OBJ_LOADED; /* doesn't matter if successfully or not */\n \n \tif (err) {\n@@ -9631,7 +9663,7 @@ void bpf_object__close(struct bpf_object *obj)\n \t * bpf_object__load(), we need to clean up stuff that is normally\n \t * cleaned up at the end of loading step\n \t */\n-\tbpf_object_post_load_cleanup(obj);\n+\tbpf_object_post_load_cleanup(obj, true);\n \n \tusdt_manager_free(obj-\u003eusdt_man);\n \tobj-\u003eusdt_man = NULL;\n@@ -9640,7 +9672,6 @@ void bpf_object__close(struct bpf_object *obj)\n \tbpf_object__elf_finish(obj);\n \tbpf_object_unload(obj);\n \tbtf__free(obj-\u003ebtf);\n-\tbtf__free(obj-\u003ebtf_vmlinux);\n \tbtf_ext__free(obj-\u003ebtf_ext);\n \n \tfor (i = 0; i \u003c obj-\u003enr_maps; i++)\n@@ -9721,11 +9752,29 @@ int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)\n int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts)\n {\n \tstruct bpf_gen *gen;\n+\tsize_t i;\n \n \tif (!opts)\n \t\treturn libbpf_err(-EFAULT);\n \tif (!OPTS_VALID(opts, gen_loader_opts))\n \t\treturn libbpf_err(-EINVAL);\n+\n+\t/*\n+\t * Manually-loaded programs are not visible to gen_loader (see\n+\t * bpf_program__set_load_strategy()'s MANUAL case), and marking a\n+\t * program MANUAL happens during bpf_object__open(), before this\n+\t * function can ever run, so that guard can never catch it here.\n+\t * Reject any pre-existing MANUAL program now, since this is the\n+\t * earliest point where both are known.\n+\t */\n+\tfor (i = 0; i \u003c obj-\u003enr_programs; i++) {\n+\t\tif (obj-\u003eprograms[i].load_strategy == BPF_PROG_LOAD_STRATEGY_MANUAL) {\n+\t\t\tpr_warn(\"prog '%s': gen_loader does not support manually-loaded programs\\n\",\n+\t\t\t\tobj-\u003eprograms[i].name);\n+\t\t\treturn libbpf_err(-EOPNOTSUPP);\n+\t\t}\n+\t}\n+\n \tgen = calloc(1, sizeof(*gen));\n \tif (!gen)\n \t\treturn libbpf_err(-ENOMEM);\n@@ -9802,16 +9851,13 @@ const char *bpf_program__section_name(const struct bpf_program *prog)\n \n bool bpf_program__autoload(const struct bpf_program *prog)\n {\n-\treturn prog-\u003eautoload;\n+\treturn prog-\u003eload_strategy == BPF_PROG_LOAD_STRATEGY_AUTO;\n }\n \n int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)\n {\n-\tif (prog-\u003eobj-\u003estate \u003e= OBJ_LOADED)\n-\t\treturn libbpf_err(-EINVAL);\n-\n-\tprog-\u003eautoload = autoload;\n-\treturn 0;\n+\treturn bpf_program__set_load_strategy(prog,\n+\t\tautoload ? BPF_PROG_LOAD_STRATEGY_AUTO : BPF_PROG_LOAD_STRATEGY_DISABLED);\n }\n \n bool bpf_program__autoattach(const struct bpf_program *prog)\n@@ -9819,9 +9865,13 @@ bool bpf_program__autoattach(const struct bpf_program *prog)\n \treturn prog-\u003eautoattach;\n }\n \n-void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach)\n+int bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach)\n {\n+\tif (prog-\u003eload_strategy == BPF_PROG_LOAD_STRATEGY_MANUAL)\n+\t\treturn libbpf_err(-EINVAL);\n+\n \tprog-\u003eautoattach = autoattach;\n+\treturn 0;\n }\n \n const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)\n@@ -12745,7 +12795,7 @@ static int collect_func_ids_by_glob(const struct bpf_program *prog, const char *\n \terr = collect_btf_func_ids_by_glob(btf, pattern, ids);\n \n cleanup:\n-\tbpf_object_cleanup_btf(obj);\n+\tbpf_object_cleanup_btf(obj, false);\n \treturn err;\n }\n \n@@ -15125,7 +15175,7 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)\n \t\tstruct bpf_program *prog = *prog_skel-\u003eprog;\n \t\tstruct bpf_link **link = prog_skel-\u003elink;\n \n-\t\tif (!prog-\u003eautoload || !prog-\u003eautoattach)\n+\t\tif (prog-\u003eload_strategy != BPF_PROG_LOAD_STRATEGY_AUTO || !prog-\u003eautoattach)\n \t\t\tcontinue;\n \n \t\t/* auto-attaching not supported for this program */\n@@ -15235,3 +15285,101 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)\n \tfree(s-\u003eprogs);\n \tfree(s);\n }\n+\n+int bpf_program__set_load_strategy(struct bpf_program *prog, enum bpf_prog_load_strategy strategy)\n+{\n+\tstruct bpf_object *obj = prog-\u003eobj;\n+\n+\tif (obj-\u003estate \u003e= OBJ_PREPARED)\n+\t\treturn libbpf_err(-EINVAL);\n+\n+\tif (strategy == prog-\u003eload_strategy)\n+\t\treturn 0;\n+\n+\tswitch (strategy) {\n+\tcase BPF_PROG_LOAD_STRATEGY_DISABLED:\n+\tcase BPF_PROG_LOAD_STRATEGY_AUTO:\n+\t\tif (prog-\u003eload_strategy == BPF_PROG_LOAD_STRATEGY_MANUAL)\n+\t\t\tprog-\u003eautoattach = prog-\u003esaved_autoattach;\n+\t\tprog-\u003eload_strategy = strategy;\n+\t\tbreak;\n+\tcase BPF_PROG_LOAD_STRATEGY_MANUAL:\n+\t\t/*\n+\t\t * Manually-loaded programs are not supported for gen_loader.\n+\t\t * This is because bpf_object_load_prog is not called for\n+\t\t * manually-loaded programs, so such programs are not visible\n+\t\t * to gen_loader. For this reason, prevent calling\n+\t\t * bpf_program__set_load_strategy(MANUAL) when gen_loader was\n+\t\t * used to generate a BPF object loader.\n+\t\t * A gen_loader implementation is being called for autoloaded\n+\t\t * programs and defines its own model for loading BPF programs.\n+\t\t * To pass a BPF program to gen_loader, set the program's load strategy\n+\t\t * to BPF_PROG_LOAD_STRATEGY_AUTO.\n+\t\t */\n+\t\tif (obj-\u003egen_loader)\n+\t\t\treturn libbpf_err(-EOPNOTSUPP);\n+\n+\t\tif (prog_is_subprog(obj, prog))\n+\t\t\treturn libbpf_err(-EINVAL);\n+\n+\t\tprog-\u003esaved_autoattach = prog-\u003eautoattach;\n+\t\tprog-\u003eload_strategy = BPF_PROG_LOAD_STRATEGY_MANUAL;\n+\t\tprog-\u003eautoattach = false;\n+\t\tbreak;\n+\tdefault:\n+\t\treturn libbpf_err(-EINVAL);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+enum bpf_prog_load_strategy bpf_program__load_strategy(const struct bpf_program *prog)\n+{\n+\treturn prog-\u003eload_strategy;\n+}\n+\n+/*\n+ * This function must be called after bpf_object__prepare (or\n+ * bpf_object__load, which calls bpf_object__prepare internally).\n+ * Manually-loaded program data is initialized on object prepare.\n+ * Post-prepare initialization is not supported.\n+ */\n+int\n+bpf_program__load_manually(struct bpf_program *prog)\n+{\n+\tint err;\n+\tstruct bpf_object *obj = prog-\u003eobj;\n+\n+\tif (obj-\u003estate \u003c OBJ_PREPARED)\n+\t\treturn libbpf_err(-EINVAL);\n+\n+\tif (prog_is_subprog(obj, prog) || prog-\u003eload_strategy != BPF_PROG_LOAD_STRATEGY_MANUAL)\n+\t\treturn libbpf_err(-EINVAL);\n+\n+\tif (prog-\u003efd \u003e= 0)\n+\t\treturn libbpf_err(-EBUSY);\n+\n+\terr = bpf_object_load_prog(obj, prog, prog-\u003einsns, prog-\u003einsns_cnt,\n+\t\t\t\t   obj-\u003elicense, obj-\u003ekern_version, \u0026prog-\u003efd);\n+\tif (err) {\n+\t\tpr_warn(\"prog '%s': failed to load manually: %d\\n\", prog-\u003ename, err);\n+\t\treturn libbpf_err(err);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+int bpf_program__unload_manually(struct bpf_program *prog)\n+{\n+\tint err;\n+\n+\tif (prog-\u003eload_strategy != BPF_PROG_LOAD_STRATEGY_MANUAL)\n+\t\treturn libbpf_err(-EINVAL);\n+\n+\t/*\n+\t * Close the file descriptor but retain the program's data to\n+\t * support reloading the program if it is required again.\n+\t */\n+\terr = zclose(prog-\u003efd);\n+\treturn err ? libbpf_err(-errno) : 0;\n+}\ndiff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h\nindex 48266e752223e..5914e5001b972 100644\n--- a/tools/lib/bpf/libbpf.h\n+++ b/tools/lib/bpf/libbpf.h\n@@ -378,7 +378,7 @@ LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog)\n LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);\n LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);\n LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);\n-LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);\n+LIBBPF_API int bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);\n \n struct bpf_insn;\n \n@@ -2100,6 +2100,44 @@ LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);\n  */\n LIBBPF_API int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts *opts);\n \n+/**\n+ * The program load strategy:\n+ *\n+ * - BPF_PROG_LOAD_STRATEGY_DISABLED: the program is not loaded.\n+ * - BPF_PROG_LOAD_STRATEGY_AUTO: the program is autoloaded when the bpf_object is loaded.\n+ * - BPF_PROG_LOAD_STRATEGY_MANUAL: the program is loaded and attached manually.\n+ */\n+enum bpf_prog_load_strategy {\n+\tBPF_PROG_LOAD_STRATEGY_DISABLED = 0,\n+\tBPF_PROG_LOAD_STRATEGY_AUTO,\n+\tBPF_PROG_LOAD_STRATEGY_MANUAL,\n+};\n+\n+/**\n+ * @brief **bpf_program__set_load_strategy()** sets the load strategy of a\n+ * BPF program, controlling whether and when it gets loaded into the kernel.\n+ *\n+ * Can only be called before the enclosing bpf_object is loaded.\n+ *\n+ * @param prog BPF program to update\n+ * @param strategy new load strategy for the program\n+ * @return 0 on success; negative error code if the object was already loaded\n+ */\n+LIBBPF_API int bpf_program__set_load_strategy(struct bpf_program *prog,\n+\t\t\t\t\t      enum bpf_prog_load_strategy strategy);\n+\n+/**\n+ * @brief **bpf_program__load_strategy()** returns the current load strategy\n+ * of a BPF program.\n+ *\n+ * @param prog BPF program to query\n+ * @return current load strategy of the program\n+ */\n+LIBBPF_API enum bpf_prog_load_strategy bpf_program__load_strategy(const struct bpf_program *prog);\n+\n+LIBBPF_API int bpf_program__load_manually(struct bpf_program *prog);\n+LIBBPF_API int bpf_program__unload_manually(struct bpf_program *prog);\n+\n #ifdef __cplusplus\n } /* extern \"C\" */\n #endif\ndiff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map\nindex a811a1b3a0857..401a8e268d335 100644\n--- a/tools/lib/bpf/libbpf.map\n+++ b/tools/lib/bpf/libbpf.map\n@@ -462,6 +462,10 @@ LIBBPF_1.8.0 {\n \t\tbpf_program__attach_tracing_multi;\n \t\tbpf_program__clear_flags;\n \t\tbpf_program__clone;\n+\t\tbpf_program__load_manually;\n+\t\tbpf_program__load_strategy;\n+\t\tbpf_program__set_load_strategy;\n+\t\tbpf_program__unload_manually;\n \t\tbtf__find_by_name_kind_own;\n \t\tbtf__new_empty_opts;\n } LIBBPF_1.7.0;\ndiff --git a/tools/testing/selftests/bpf/prog_tests/dynamicload.c b/tools/testing/selftests/bpf/prog_tests/dynamicload.c\nnew file mode 100644\nindex 0000000000000..979bdc14f4ffd\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/dynamicload.c\n@@ -0,0 +1,350 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003ctest_progs.h\u003e\n+#include \u003ctime.h\u003e\n+#include \"test_dynamicload.skel.h\"\n+\n+#define READ_SZ 456\n+\n+/*\n+ * prog4 is marked SEC(\"!...\") in the source instead of being set\n+ * imperatively; verify that an explicit bpf_program__set_load_strategy() call\n+ * before load overrides the declarative default.\n+ */\n+static void dynamicload_verify_override(void)\n+{\n+\tstruct test_dynamicload *skel;\n+\tint err;\n+\n+\tskel = test_dynamicload__open();\n+\tif (!ASSERT_OK_PTR(skel, \"skel_open\"))\n+\t\treturn;\n+\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.prog4, BPF_PROG_LOAD_STRATEGY_DISABLED);\n+\tif (!ASSERT_OK(err, \"set_load_strategy_disabled\"))\n+\t\tgoto cleanup;\n+\n+\tif (!ASSERT_EQ(bpf_program__load_strategy(skel-\u003eprogs.prog4),\n+\t\t       BPF_PROG_LOAD_STRATEGY_DISABLED, \"prog4_load_strategy_overridden\"))\n+\t\tgoto cleanup;\n+\n+\t/* keep the other autoload programs out of the way of this load */\n+\tbpf_program__set_load_strategy(skel-\u003eprogs.prog1, BPF_PROG_LOAD_STRATEGY_DISABLED);\n+\tbpf_program__set_load_strategy(skel-\u003eprogs.prog3, BPF_PROG_LOAD_STRATEGY_DISABLED);\n+\n+\terr = test_dynamicload__load(skel);\n+\tif (!ASSERT_OK(err, \"skel_load\"))\n+\t\tgoto cleanup;\n+\n+\t/* prog4 was never loaded, so it cannot be loaded manually either */\n+\terr = bpf_program__load_manually(skel-\u003eprogs.prog4);\n+\tASSERT_ERR(err, \"load_manually_after_override\");\n+\n+cleanup:\n+\ttest_dynamicload__destroy(skel);\n+}\n+\n+/*\n+ * prog4 is MANUAL via its SEC(\"!...\") marker; verify that\n+ * bpf_object__prepare() alone -- without ever calling bpf_object__load() --\n+ * is sufficient for bpf_program__load_manually() to succeed, since BTF\n+ * loading, map creation, and relocation of MANUAL programs are all\n+ * completed by prepare() already.\n+ */\n+static void dynamicload_verify_prepare_only(void)\n+{\n+\tstruct test_dynamicload *skel;\n+\tstruct bpf_link *link;\n+\tint err;\n+\n+\tskel = test_dynamicload__open();\n+\tif (!ASSERT_OK_PTR(skel, \"skel_open\"))\n+\t\treturn;\n+\n+\terr = bpf_object__prepare(skel-\u003eobj);\n+\tif (!ASSERT_OK(err, \"bpf_object__prepare\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_program__load_manually(skel-\u003eprogs.prog4);\n+\tif (!ASSERT_OK(err, \"load_manually_after_prepare\"))\n+\t\tgoto cleanup;\n+\n+\tif (!ASSERT_GE(bpf_program__fd(skel-\u003eprogs.prog4), 0, \"prog4_fd_after_prepare\"))\n+\t\tgoto cleanup;\n+\n+\tlink = bpf_program__attach(skel-\u003eprogs.prog4);\n+\tif (!ASSERT_OK_PTR(link, \"attach_after_prepare\"))\n+\t\tgoto cleanup;\n+\n+\tusleep(1);\n+\n+\tif (!ASSERT_TRUE(skel-\u003ebss-\u003eprog4_called, \"prog4_called_after_prepare\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_link__destroy(link);\n+\tif (!ASSERT_OK(err, \"link_destroy_after_prepare\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_program__unload_manually(skel-\u003eprogs.prog4);\n+\tASSERT_OK(err, \"unload_manually_after_prepare\");\n+\n+cleanup:\n+\ttest_dynamicload__destroy(skel);\n+}\n+\n+/*\n+ * prog5 is disabled at parse time; resolve its attach target against\n+ * module BTF via bpf_program__set_attach_target() before switching it\n+ * to MANUAL and deferring its load past the bulk bpf_object__load().\n+ * Regression test for the module BTF fd/array lifetime bug: without\n+ * deferring the module BTF fd/array close for MANUAL programs, the fd\n+ * cached in prog-\u003eattach_btf_obj_fd is closed by the bulk load's\n+ * cleanup before this deferred load runs, causing a deterministic\n+ * -EINVAL.\n+ */\n+static void dynamicload_verify_module_btf(void)\n+{\n+\tstruct test_dynamicload *skel;\n+\tstruct bpf_link *link;\n+\tint err;\n+\n+\tif (!env.has_testmod) {\n+\t\ttest__skip();\n+\t\treturn;\n+\t}\n+\n+\tskel = test_dynamicload__open();\n+\tif (!ASSERT_OK_PTR(skel, \"skel_open\"))\n+\t\treturn;\n+\n+\terr = bpf_program__set_attach_target(skel-\u003eprogs.prog5, 0,\n+\t\t\t\t\t     \"bpf_testmod:bpf_testmod_test_read\");\n+\tif (!ASSERT_OK(err, \"set_attach_target\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.prog5, BPF_PROG_LOAD_STRATEGY_MANUAL);\n+\tif (!ASSERT_OK(err, \"set_load_strategy_manual\"))\n+\t\tgoto cleanup;\n+\n+\t/* keep the other autoload programs out of the way of this load */\n+\tbpf_program__set_load_strategy(skel-\u003eprogs.prog1, BPF_PROG_LOAD_STRATEGY_DISABLED);\n+\tbpf_program__set_load_strategy(skel-\u003eprogs.prog2, BPF_PROG_LOAD_STRATEGY_DISABLED);\n+\tbpf_program__set_load_strategy(skel-\u003eprogs.prog3, BPF_PROG_LOAD_STRATEGY_DISABLED);\n+\n+\t/*\n+\t * bulk load: prog5 itself is skipped (MANUAL), but this is where\n+\t * module BTF gets torn down if not correctly deferred\n+\t */\n+\terr = test_dynamicload__load(skel);\n+\tif (!ASSERT_OK(err, \"skel_load\"))\n+\t\tgoto cleanup;\n+\n+\t/*\n+\t * deferred load must still succeed: the module BTF fd cached above\n+\t * by set_attach_target() must still be a valid, open fd here\n+\t */\n+\terr = bpf_program__load_manually(skel-\u003eprogs.prog5);\n+\tif (!ASSERT_OK(err, \"load_manually_module_btf\"))\n+\t\tgoto cleanup;\n+\n+\tlink = bpf_program__attach(skel-\u003eprogs.prog5);\n+\tif (!ASSERT_OK_PTR(link, \"attach\"))\n+\t\tgoto cleanup;\n+\n+\tASSERT_OK(trigger_module_test_read(READ_SZ), \"trigger_read\");\n+\tASSERT_EQ(skel-\u003ebss-\u003eprog5_sz, READ_SZ, \"prog5_sz\");\n+\n+\tbpf_link__destroy(link);\n+\n+cleanup:\n+\ttest_dynamicload__destroy(skel);\n+}\n+\n+void test_dynamicload(void)\n+{\n+\tstruct bpf_link *link;\n+\tstruct test_dynamicload *skel;\n+\tint err;\n+\n+\tskel = test_dynamicload__open();\n+\tif (!ASSERT_OK_PTR(skel, \"skel_open\"))\n+\t\treturn;\n+\n+\t/*\n+\t * the SEC(\"!...\") prefix alone, with no imperative call, must set\n+\t * prog4's load strategy before it is ever touched below\n+\t */\n+\tif (!ASSERT_EQ(bpf_program__load_strategy(skel-\u003eprogs.prog4),\n+\t\t       BPF_PROG_LOAD_STRATEGY_MANUAL, \"prog4_prefix_load_strategy\"))\n+\t\tgoto cleanup;\n+\tif (!ASSERT_FALSE(bpf_program__autoattach(skel-\u003eprogs.prog4), \"prog4_autoattach\"))\n+\t\tgoto cleanup;\n+\n+\t/* don't load prog1 */\n+\tbpf_program__set_load_strategy(skel-\u003eprogs.prog1, BPF_PROG_LOAD_STRATEGY_DISABLED);\n+\n+\t/* prog2 is autoload */\n+\tbpf_program__set_load_strategy(skel-\u003eprogs.prog2, BPF_PROG_LOAD_STRATEGY_AUTO);\n+\n+\t/* prog3 is manually loaded */\n+\tbpf_program__set_load_strategy(skel-\u003eprogs.prog3, BPF_PROG_LOAD_STRATEGY_MANUAL);\n+\n+\terr = test_dynamicload__load(skel);\n+\tif (!ASSERT_OK(err, \"skel_load\"))\n+\t\tgoto cleanup;\n+\n+\terr = test_dynamicload__attach(skel);\n+\tif (!ASSERT_OK(err, \"skel_attach\"))\n+\t\tgoto cleanup;\n+\n+\t/* trigger the BPF programs */\n+\tusleep(1);\n+\n+\tASSERT_FALSE(skel-\u003ebss-\u003eprog1_called, \"prog1_called\");\n+\tASSERT_TRUE(skel-\u003ebss-\u003eprog2_called, \"prog2_called\");\n+\tASSERT_FALSE(skel-\u003ebss-\u003eprog3_called, \"prog3_called\");\n+\tASSERT_FALSE(skel-\u003ebss-\u003eprog4_called, \"prog4_called\");\n+\n+\t/* prog1 is disabled for load */\n+\terr = bpf_program__load_manually(skel-\u003eprogs.prog1);\n+\tif (!ASSERT_ERR(err, \"load_manually_disabled\"))\n+\t\tgoto cleanup;\n+\n+\t/* prog1 is disabled for load */\n+\terr = bpf_program__unload_manually(skel-\u003eprogs.prog1);\n+\tif (!ASSERT_ERR(err, \"unload_manually_disabled\"))\n+\t\tgoto cleanup;\n+\n+\t/* prog2 is autoload */\n+\terr = bpf_program__load_manually(skel-\u003eprogs.prog2);\n+\tif (!ASSERT_ERR(err, \"load_manually_autoload\"))\n+\t\tgoto cleanup;\n+\n+\t/* prog2 is autoload */\n+\terr = bpf_program__unload_manually(skel-\u003eprogs.prog2);\n+\tif (!ASSERT_ERR(err, \"unload_manually_autoload\"))\n+\t\tgoto cleanup;\n+\n+\t/* reset the call flags */\n+\tskel-\u003ebss-\u003eprog2_called = false;\n+\tskel-\u003ebss-\u003eprog3_called = false;\n+\n+\tusleep(1);\n+\n+\tASSERT_FALSE(skel-\u003ebss-\u003eprog1_called, \"prog1_called\");\n+\tASSERT_TRUE(skel-\u003ebss-\u003eprog2_called, \"prog2_called\");\n+\tASSERT_FALSE(skel-\u003ebss-\u003eprog3_called, \"prog3_called\");\n+\n+\t/* load prog3 */\n+\terr = bpf_program__load_manually(skel-\u003eprogs.prog3);\n+\tif (!ASSERT_OK(err, \"load_manually\"))\n+\t\tgoto cleanup;\n+\n+\t/* attach prog3 */\n+\tlink = bpf_program__attach(skel-\u003eprogs.prog3);\n+\tif (!ASSERT_OK_PTR(link, \"attach\"))\n+\t\tgoto cleanup;\n+\n+\tusleep(1);\n+\n+\tif (!ASSERT_TRUE(skel-\u003ebss-\u003eprog3_called, \"prog3_called\"))\n+\t\tgoto cleanup;\n+\n+\t/* detach prog3 as test_dynamicload__destroy doesn't detach manually loaded programs */\n+\terr = bpf_link__destroy(link);\n+\tif (!ASSERT_OK(err, \"link_destroy\"))\n+\t\tgoto cleanup;\n+\n+\t/* reset the call flags after detach */\n+\tskel-\u003ebss-\u003eprog2_called = false;\n+\tskel-\u003ebss-\u003eprog3_called = false;\n+\n+\tusleep(1);\n+\n+\tASSERT_TRUE(skel-\u003ebss-\u003eprog2_called, \"prog2_called\");\n+\tASSERT_FALSE(skel-\u003ebss-\u003eprog3_called, \"prog3_called\");\n+\n+\t/* unload prog3 */\n+\terr = bpf_program__unload_manually(skel-\u003eprogs.prog3);\n+\tif (!ASSERT_OK(err, \"unload_manually\"))\n+\t\tgoto cleanup;\n+\n+\t/* reload prog3 */\n+\terr = bpf_program__load_manually(skel-\u003eprogs.prog3);\n+\tif (!ASSERT_OK(err, \"load_manually_reload\"))\n+\t\tgoto cleanup;\n+\n+\t/* reattach prog3 */\n+\tlink = bpf_program__attach(skel-\u003eprogs.prog3);\n+\tif (!ASSERT_OK_PTR(link, \"reattach\"))\n+\t\tgoto cleanup;\n+\n+\tusleep(1);\n+\n+\tif (!ASSERT_TRUE(skel-\u003ebss-\u003eprog3_called, \"prog3_called_reattach\"))\n+\t\tgoto cleanup;\n+\n+\t/* detach prog3 as test_dynamicload__destroy doesn't detach manually loaded programs */\n+\terr = bpf_link__destroy(link);\n+\tif (!ASSERT_OK(err, \"link_destroy_reattach\"))\n+\t\tgoto cleanup;\n+\n+\t/*\n+\t * verify regular unload for manually loaded program,\n+\t * unload prog3 as a regular program\n+\t */\n+\tbpf_program__unload(skel-\u003eprogs.prog3);\n+\n+\t/* reset the call flags after unload */\n+\tskel-\u003ebss-\u003eprog2_called = false;\n+\tskel-\u003ebss-\u003eprog3_called = false;\n+\n+\tusleep(1);\n+\n+\tASSERT_TRUE(skel-\u003ebss-\u003eprog2_called, \"prog2_called\");\n+\tASSERT_FALSE(skel-\u003ebss-\u003eprog3_called, \"prog3_called\");\n+\n+\t/* reloading prog3 must fail as it was unloaded as a regular program */\n+\terr = bpf_program__load_manually(skel-\u003eprogs.prog3);\n+\tASSERT_ERR(err, \"load_manually_after_regular_unload\");\n+\n+\t/*\n+\t * run prog4 (declaratively marked) through the same manual\n+\t * load/attach/trigger/detach/unload cycle as prog3\n+\t */\n+\terr = bpf_program__load_manually(skel-\u003eprogs.prog4);\n+\tif (!ASSERT_OK(err, \"prog4_load_manually\"))\n+\t\tgoto cleanup;\n+\n+\tlink = bpf_program__attach(skel-\u003eprogs.prog4);\n+\tif (!ASSERT_OK_PTR(link, \"prog4_attach\"))\n+\t\tgoto cleanup;\n+\n+\tusleep(1);\n+\n+\tif (!ASSERT_TRUE(skel-\u003ebss-\u003eprog4_called, \"prog4_called\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_link__destroy(link);\n+\tif (!ASSERT_OK(err, \"prog4_link_destroy\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_program__unload_manually(skel-\u003eprogs.prog4);\n+\tASSERT_OK(err, \"prog4_unload_manually\");\n+\n+\ttest_dynamicload__destroy(skel);\n+\n+\t/* separate scenario: imperative override of the declarative marker */\n+\tdynamicload_verify_override();\n+\n+\t/* separate scenario: prepare()-only is sufficient for manual load */\n+\tdynamicload_verify_prepare_only();\n+\n+\t/* separate scenario: deferred manual load with a module BTF attach target */\n+\tdynamicload_verify_module_btf();\n+\treturn;\n+\n+cleanup:\n+\ttest_dynamicload__destroy(skel);\n+}\n+\ndiff --git a/tools/testing/selftests/bpf/prog_tests/load_type.c b/tools/testing/selftests/bpf/prog_tests/load_type.c\nnew file mode 100644\nindex 0000000000000..55ca53685863c\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/load_type.c\n@@ -0,0 +1,157 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003ctest_progs.h\u003e\n+#include \u003ctime.h\u003e\n+#include \"test_load_type.skel.h\"\n+\n+void test_load_type(void)\n+{\n+\tstruct bpf_link *link;\n+\tstruct test_load_type *skel;\n+\tint err;\n+\n+\tskel = test_load_type__open();\n+\tif (!ASSERT_OK_PTR(skel, \"skel_open\"))\n+\t\treturn;\n+\n+\t/* don't load prog1 */\n+\tbpf_program__set_load_strategy(skel-\u003eprogs.prog1, BPF_PROG_LOAD_STRATEGY_DISABLED);\n+\n+\t/* load and attach prog2 */\n+\tbpf_program__set_load_strategy(skel-\u003eprogs.prog2, BPF_PROG_LOAD_STRATEGY_AUTO);\n+\tif (!ASSERT_TRUE(bpf_program__autoload(skel-\u003eprogs.prog2), \"prog2_autoload\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.prog3, BPF_PROG_LOAD_STRATEGY_MANUAL);\n+\tif (!ASSERT_OK(err, \"set_load_strategy_manual\"))\n+\t\tgoto cleanup;\n+\tif (!ASSERT_EQ(bpf_program__load_strategy(skel-\u003eprogs.prog3), BPF_PROG_LOAD_STRATEGY_MANUAL,\n+\t\t       \"prog3_load_strategy\"))\n+\t\tgoto cleanup;\n+\n+\t/*\n+\t * bpf_program__set_autoload() is a thin forwarder to\n+\t * set_load_strategy(), restricted to AUTO/DISABLED to preserve its\n+\t * original bool on/off meaning; it does change the load strategy of\n+\t * a program that isn't currently BPF_PROG_LOAD_STRATEGY_AUTO.\n+\t */\n+\terr = bpf_program__set_autoload(skel-\u003eprogs.prog3, false);\n+\tif (!ASSERT_OK(err, \"set_autoload_false\"))\n+\t\tgoto cleanup;\n+\n+\tif (!ASSERT_EQ(bpf_program__load_strategy(skel-\u003eprogs.prog3),\n+\t\t       BPF_PROG_LOAD_STRATEGY_DISABLED, \"prog3_load_strategy_after_false\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_program__set_autoload(skel-\u003eprogs.prog3, true);\n+\tif (!ASSERT_OK(err, \"set_autoload_true\"))\n+\t\tgoto cleanup;\n+\n+\tif (!ASSERT_EQ(bpf_program__load_strategy(skel-\u003eprogs.prog3), BPF_PROG_LOAD_STRATEGY_AUTO,\n+\t\t       \"prog3_load_strategy_after_true\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.prog3, BPF_PROG_LOAD_STRATEGY_MANUAL);\n+\tif (!ASSERT_OK(err, \"set_load_strategy_manual_enum\"))\n+\t\tgoto cleanup;\n+\n+\tif (!ASSERT_EQ(bpf_program__load_strategy(skel-\u003eprogs.prog3), BPF_PROG_LOAD_STRATEGY_MANUAL,\n+\t\t       \"prog3_load_strategy_after_manual_enum\"))\n+\t\tgoto cleanup;\n+\n+\t/*\n+\t * leaving MANUAL for AUTO must restore autoattach (regression test for\n+\t * the autoattach residue bug: set_load_strategy(MANUAL) clears autoattach, and\n+\t * nothing used to restore it on exit)\n+\t */\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.prog3, BPF_PROG_LOAD_STRATEGY_AUTO);\n+\tif (!ASSERT_OK(err, \"set_load_strategy_auto\"))\n+\t\tgoto cleanup;\n+\n+\tif (!ASSERT_EQ(bpf_program__load_strategy(skel-\u003eprogs.prog3), BPF_PROG_LOAD_STRATEGY_AUTO,\n+\t\t       \"prog3_load_strategy_auto\"))\n+\t\tgoto cleanup;\n+\n+\tif (!ASSERT_TRUE(bpf_program__autoattach(skel-\u003eprogs.prog3), \"prog3_autoattach_restored\"))\n+\t\tgoto cleanup;\n+\n+\t/*\n+\t * confirm the restore also holds across a MANUAL -\u003e DISABLED -\u003e AUTO\n+\t * round-trip: AUTO and DISABLED share one guard keyed off the source\n+\t * strategy being MANUAL, so autoattach is already restored at the\n+\t * MANUAL -\u003e DISABLED step, not by a separate DISABLED -\u003e AUTO one\n+\t */\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.prog3, BPF_PROG_LOAD_STRATEGY_MANUAL);\n+\tif (!ASSERT_OK(err, \"set_load_strategy_manual_again\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.prog3, BPF_PROG_LOAD_STRATEGY_DISABLED);\n+\tif (!ASSERT_OK(err, \"set_load_strategy_disabled\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.prog3, BPF_PROG_LOAD_STRATEGY_AUTO);\n+\tif (!ASSERT_OK(err, \"set_load_strategy_auto_via_disabled\"))\n+\t\tgoto cleanup;\n+\n+\tif (!ASSERT_TRUE(bpf_program__autoattach(skel-\u003eprogs.prog3),\n+\t\t\t \"prog3_autoattach_restored_via_disabled\"))\n+\t\tgoto cleanup;\n+\n+\t/* an out-of-range load strategy is rejected */\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.prog3, (enum bpf_prog_load_strategy)999);\n+\tif (!ASSERT_ERR(err, \"set_load_strategy_invalid\"))\n+\t\tgoto cleanup;\n+\n+\t/* change the strategy back to BPF_PROG_LOAD_STRATEGY_MANUAL for the rest of the test */\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.prog3, BPF_PROG_LOAD_STRATEGY_MANUAL);\n+\tif (!ASSERT_OK(err, \"set_load_strategy_manual_final\"))\n+\t\tgoto cleanup;\n+\n+\tif (!ASSERT_EQ(bpf_program__load_strategy(skel-\u003eprogs.prog3), BPF_PROG_LOAD_STRATEGY_MANUAL,\n+\t\t       \"prog3_load_strategy_final\"))\n+\t\tgoto cleanup;\n+\n+\terr = test_load_type__load(skel);\n+\tif (!ASSERT_OK(err, \"skel_load\"))\n+\t\tgoto cleanup;\n+\n+\tif (!ASSERT_TRUE(bpf_program__autoattach(skel-\u003eprogs.prog2), \"prog2_autoattach\"))\n+\t\tgoto cleanup;\n+\tif (!ASSERT_FALSE(bpf_program__autoattach(skel-\u003eprogs.prog3), \"prog3_autoattach\"))\n+\t\tgoto cleanup;\n+\n+\t/* loaded program strategy cannot be changed */\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.prog3, BPF_PROG_LOAD_STRATEGY_DISABLED);\n+\tASSERT_ERR(err, \"set_load_strategy_after_load\");\n+\n+\terr = test_load_type__attach(skel);\n+\tif (!ASSERT_OK(err, \"skel_attach\"))\n+\t\tgoto cleanup;\n+\n+\tusleep(1);\n+\n+\tASSERT_FALSE(skel-\u003ebss-\u003eprog1_called, \"prog1_called\");\n+\tASSERT_TRUE(skel-\u003ebss-\u003eprog2_called, \"prog2_called\");\n+\tASSERT_FALSE(skel-\u003ebss-\u003eprog3_called, \"prog3_called\");\n+\n+\terr = bpf_program__load_manually(skel-\u003eprogs.prog3);\n+\tif (!ASSERT_OK(err, \"load_manually\"))\n+\t\tgoto cleanup;\n+\n+\t/* attach prog3 */\n+\tlink = bpf_program__attach(skel-\u003eprogs.prog3);\n+\tif (!ASSERT_OK_PTR(link, \"attach\"))\n+\t\tgoto cleanup;\n+\n+\tusleep(1);\n+\n+\tif (!ASSERT_TRUE(skel-\u003ebss-\u003eprog3_called, \"prog3_called_again\"))\n+\t\tgoto cleanup;\n+\n+\t/* detach prog3 as test_load_type__destroy doesn't detach manually loaded programs */\n+\terr = bpf_link__destroy(link);\n+\tASSERT_OK(err, \"link_destroy\");\n+\n+cleanup:\n+\ttest_load_type__destroy(skel);\n+}\ndiff --git a/tools/testing/selftests/bpf/prog_tests/signed_loader.c b/tools/testing/selftests/bpf/prog_tests/signed_loader.c\nindex a0f93756e717b..0648816e3d50a 100644\n--- a/tools/testing/selftests/bpf/prog_tests/signed_loader.c\n+++ b/tools/testing/selftests/bpf/prog_tests/signed_loader.c\n@@ -2168,6 +2168,33 @@ static void signed_module_kfunc_rejected(void)\n \trun_setup(\"cleanup\", dir);\n }\n \n+/*\n+ * a program marked MANUAL is invisible to gen_loader's program count (it is\n+ * skipped by bpf_object_load_progs()), so bpf_object__gen_loader() must\n+ * reject the whole object up front instead of silently generating a\n+ * skeleton whose prog_fd slots no longer line up with the object's programs\n+ */\n+static void manual_prog_rejected(void)\n+{\n+\tLIBBPF_OPTS(gen_loader_opts, gopts, .gen_hash = true);\n+\tstruct test_signed_loader *skel;\n+\tint err;\n+\n+\tskel = test_signed_loader__open();\n+\tif (!ASSERT_OK_PTR(skel, \"skel_open\"))\n+\t\treturn;\n+\n+\terr = bpf_program__set_load_strategy(skel-\u003eprogs.probe, BPF_PROG_LOAD_STRATEGY_MANUAL);\n+\tif (!ASSERT_OK(err, \"set_load_strategy_manual\"))\n+\t\tgoto cleanup;\n+\n+\terr = bpf_object__gen_loader(skel-\u003eobj, \u0026gopts);\n+\tASSERT_ERR(err, \"gen_loader_rejected\");\n+\n+cleanup:\n+\ttest_signed_loader__destroy(skel);\n+}\n+\n enum subtest_boot {\n \tBOOT_ANY,\n \tBOOT_SEALED,\n@@ -2211,6 +2238,7 @@ static const struct {\n \t{ \"signed_map_by_fd_rejected\", signed_map_by_fd_rejected, BOOT_SEALED },\n \t{ \"signed_sparse_fd_array_rejected\", signed_sparse_fd_array_rejected, BOOT_SEALED },\n \t{ \"bpf_keyring_provisioned\", bpf_keyring_provisioned, BOOT_UNSEALED },\n+\t{ \"manual_prog_rejected\", manual_prog_rejected, BOOT_ANY },\n };\n \n void test_signed_loader(void)\ndiff --git a/tools/testing/selftests/bpf/progs/test_dynamicload.c b/tools/testing/selftests/bpf/progs/test_dynamicload.c\nnew file mode 100644\nindex 0000000000000..156c90afe73e9\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/test_dynamicload.c\n@@ -0,0 +1,54 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \"vmlinux.h\"\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf/bpf_tracing.h\u003e\n+\n+bool prog1_called = false;\n+bool prog2_called = false;\n+bool prog3_called = false;\n+bool prog4_called = false;\n+__u32 prog5_sz = 0;\n+\n+SEC(\"raw_tp/sys_enter\")\n+int prog1(const void *ctx)\n+{\n+\tprog1_called = true;\n+\treturn 0;\n+}\n+\n+SEC(\"raw_tp/sys_enter\")\n+int prog2(const void *ctx)\n+{\n+\tprog2_called = true;\n+\treturn 0;\n+}\n+\n+SEC(\"raw_tp/sys_enter\")\n+int prog3(const void *ctx)\n+{\n+\tprog3_called = true;\n+\treturn 0;\n+}\n+\n+SEC(\"!raw_tp/sys_enter\")\n+int prog4(const void *ctx)\n+{\n+\tprog4_called = true;\n+\treturn 0;\n+}\n+\n+/*\n+ * disabled at parse time; its attach target is resolved against module\n+ * BTF via bpf_program__set_attach_target() before it is switched to\n+ * MANUAL and loaded\n+ */\n+SEC(\"?fentry\")\n+int BPF_PROG(prog5, struct file *file, struct kobject *kobj,\n+\t     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)\n+{\n+\tprog5_sz = len;\n+\treturn 0;\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/test_load_type.c b/tools/testing/selftests/bpf/progs/test_load_type.c\nnew file mode 100644\nindex 0000000000000..3d9b81691d7ad\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/test_load_type.c\n@@ -0,0 +1,31 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \"vmlinux.h\"\n+#include \u003cbpf/bpf_helpers.h\u003e\n+\n+bool prog1_called = false;\n+bool prog2_called = false;\n+bool prog3_called = false;\n+\n+SEC(\"raw_tp/sys_enter\")\n+int prog1(const void *ctx)\n+{\n+\tprog1_called = true;\n+\treturn 0;\n+}\n+\n+SEC(\"raw_tp/sys_enter\")\n+int prog2(const void *ctx)\n+{\n+\tprog2_called = true;\n+\treturn 0;\n+}\n+\n+SEC(\"raw_tp/sys_enter\")\n+int prog3(const void *ctx)\n+{\n+\tprog3_called = true;\n+\treturn 0;\n+}\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n"
}

2/1 2026/09/20 00:13 agent patch-evaluator
0m Model:
core

Args:
null

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [],
  "Reasoning": "All changes in this patch series are strictly confined to user-space directories (tools/lib/bpf and tools/testing/selftests/bpf), introducing manual load strategy APIs to libbpf and accompanying selftests. No kernel-space code (vmlinux or kernel modules) is modified.",
  "WorthFuzzing": false
}

Instruction:
You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.

================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.

- REACHABILITY IS THE PRIMARY GATE:
  Fuzzing can only discover bugs in code that can actually execute in standard virtualized
  environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
  If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
  regardless of whether it adds assertions or complex logic.

- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
  Patch authors routinely label changes as "cleanups", "refactorings", or state
  "No functional change intended". Do NOT take these claims at face value.
  Code refactorings that rearrange logic, introduce helper functions, or alter state management
  in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
  If reachable executable code is modified or refactored, it MUST be fuzzed.

- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
  When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
  BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
  Even if the author believes the invariant always holds, fuzzing is essential to verify whether
  an unusual sequence of operations can violate it.

================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:

- Non-kernel and non-executable changes:
  * Modifications to Documentation/, comments, or spelling fixes.
  * User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
    that do not affect the compiled kernel image (vmlinux) or kernel modules.
  * Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
    that do not alter control flow or data structures.
  * Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
  * Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
    ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
  * Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
    physical PCI hot-unplug or manual sysfs driver unbinding.
  * Code paths exclusive to architectures other than the target architecture.

================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
  * Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
    BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
  * Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
  * Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
  * Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).

================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:

- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
  Do NOT list generic, ubiquitous functions called by almost every program in the corpus
  (including, but not limited to: general memory allocators and deallocators, page fault
  and trap handlers, or core synchronization primitives; this is not an exhaustive list).
  Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
  which severely dilutes fuzzing effort away from the actual changes.

- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
  List functions that specifically implement the logic being added or altered, or direct API entrypoints
  for the subsystem feature under review.

- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
  Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
  distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
  If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
  functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).

================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 565b35bc1051fe8c593bdd93bf89266d0eb31d01
Author: syz-cluster <triage@syzkaller.com>
Date:   Sun Sep 20 00:13:27 2026 +0000

    syz-cluster: applied patch under review

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 613afae265199..3ac7319ed0f40 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -494,8 +494,9 @@ struct bpf_program {
 	struct bpf_object *obj;
 
 	int fd;
-	bool autoload;
+	enum bpf_prog_load_strategy load_strategy;
 	bool autoattach;
+	bool saved_autoattach;
 	bool sym_global;
 	bool mark_btf_static;
 	enum bpf_prog_type type;
@@ -725,6 +726,7 @@ struct bpf_object {
 
 	bool has_subcalls;
 	bool has_rodata;
+	bool has_manual_progs;
 
 	struct bpf_gen *gen_loader;
 
@@ -870,16 +872,25 @@ bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
 	/* libbpf's convention for SEC("?abc...") is that it's just like
 	 * SEC("abc...") but the corresponding bpf_program starts out with
 	 * autoload set to false.
+	 *
+	 * Similarly, SEC("!abc...") marks the program for manual loading:
+	 * it is skipped by the bulk auto-load pass and must be explicitly
+	 * loaded later via bpf_program__load_manually().
 	 */
 	if (sec_name[0] == '?') {
-		prog->autoload = false;
+		prog->load_strategy = BPF_PROG_LOAD_STRATEGY_DISABLED;
 		/* from now on forget there was ? in section name */
 		sec_name++;
+	} else if (sec_name[0] == '!') {
+		prog->load_strategy = BPF_PROG_LOAD_STRATEGY_MANUAL;
+		/* from now on forget there was ! in section name */
+		sec_name++;
 	} else {
-		prog->autoload = true;
+		prog->load_strategy = BPF_PROG_LOAD_STRATEGY_AUTO;
 	}
 
-	prog->autoattach = true;
+	prog->saved_autoattach = true;
+	prog->autoattach = prog->load_strategy != BPF_PROG_LOAD_STRATEGY_MANUAL;
 
 	/* inherit object's log_level */
 	prog->log_level = obj->log_level;
@@ -1163,7 +1174,8 @@ static int bpf_object_adjust_struct_ops_autoload(struct bpf_object *obj)
 			}
 		}
 		if (use_cnt)
-			prog->autoload = should_load;
+			prog->load_strategy = should_load ? BPF_PROG_LOAD_STRATEGY_AUTO
+				: BPF_PROG_LOAD_STRATEGY_DISABLED;
 	}
 
 	return 0;
@@ -1248,7 +1260,7 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
 				 * then bpf_object_adjust_struct_ops_autoload() will update its
 				 * autoload accordingly.
 				 */
-				st_ops->progs[i]->autoload = false;
+				st_ops->progs[i]->load_strategy = BPF_PROG_LOAD_STRATEGY_DISABLED;
 				st_ops->progs[i] = NULL;
 			}
 
@@ -1287,7 +1299,7 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
 			 * if user replaced it with another program or NULL
 			 */
 			if (st_ops->progs[i] && st_ops->progs[i] != prog)
-				st_ops->progs[i]->autoload = false;
+				st_ops->progs[i]->load_strategy = BPF_PROG_LOAD_STRATEGY_DISABLED;
 
 			/* Update the value from the shadow type */
 			st_ops->progs[i] = prog;
@@ -3587,7 +3599,7 @@ static bool obj_needs_vmlinux_btf(const struct bpf_object *obj)
 	}
 
 	bpf_object__for_each_program(prog, obj) {
-		if (!prog->autoload)
+		if (prog->load_strategy == BPF_PROG_LOAD_STRATEGY_DISABLED)
 			continue;
 		if (prog_needs_vmlinux_btf(prog))
 			return true;
@@ -6181,7 +6193,7 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
 			/* no need to apply CO-RE relocation if the program is
 			 * not going to be loaded
 			 */
-			if (!prog->autoload)
+			if (prog->load_strategy == BPF_PROG_LOAD_STRATEGY_DISABLED)
 				continue;
 
 			/* adjust insn_idx from section frame of reference to the local
@@ -7525,7 +7537,7 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat
 		 */
 		if (prog_is_subprog(obj, prog))
 			continue;
-		if (!prog->autoload)
+		if (prog->load_strategy == BPF_PROG_LOAD_STRATEGY_DISABLED)
 			continue;
 
 		err = bpf_object__relocate_calls(obj, prog);
@@ -7561,7 +7573,7 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat
 		prog = &obj->programs[i];
 		if (prog_is_subprog(obj, prog))
 			continue;
-		if (!prog->autoload)
+		if (prog->load_strategy == BPF_PROG_LOAD_STRATEGY_DISABLED)
 			continue;
 
 		/* Process data relos for main programs */
@@ -8057,7 +8069,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
 			log_buf = prog->log_buf;
 			log_buf_size = prog->log_size;
 			own_log_buf = false;
-		} else if (obj->log_buf) {
+		} else if (obj->log_buf && prog->load_strategy != BPF_PROG_LOAD_STRATEGY_MANUAL) {
 			log_buf = obj->log_buf;
 			log_buf_size = obj->log_size;
 			own_log_buf = false;
@@ -8400,10 +8412,11 @@ bpf_object__load_progs(struct bpf_object *obj, int log_level)
 		prog = &obj->programs[i];
 		if (prog_is_subprog(obj, prog))
 			continue;
-		if (!prog->autoload) {
-			pr_debug("prog '%s': skipped loading\n", prog->name);
+		if (prog->load_strategy != BPF_PROG_LOAD_STRATEGY_AUTO) {
+			pr_debug("prog '%s': skipped auto-loading\n", prog->name);
 			continue;
 		}
+
 		prog->log_level |= log_level;
 
 		if (obj->gen_loader)
@@ -8429,6 +8442,8 @@ static int bpf_object_prepare_progs(struct bpf_object *obj)
 
 	for (i = 0; i < obj->nr_programs; i++) {
 		prog = &obj->programs[i];
+		if (prog->load_strategy == BPF_PROG_LOAD_STRATEGY_MANUAL)
+			obj->has_manual_progs = true;
 		err = bpf_object__sanitize_prog(obj, prog);
 		if (err)
 			return err;
@@ -9062,33 +9077,50 @@ static void bpf_object_unpin(struct bpf_object *obj)
 			bpf_map__unpin(&obj->maps[i], NULL);
 }
 
-static void bpf_object_cleanup_btf(struct bpf_object *obj)
+static void bpf_object_cleanup_btf(struct bpf_object *obj, bool force)
 {
 	int i;
 
-	/* clean up module BTFs */
-	for (i = 0; i < obj->btf_module_cnt; i++) {
-		close(obj->btf_modules[i].fd);
-		btf__free(obj->btf_modules[i].btf);
-		free(obj->btf_modules[i].name);
+	/*
+	 * Module BTF fds may still be borrowed (via fd_array,
+	 * attach_btf_obj_fd, or baked into relocated instructions) by
+	 * programs that have not been manually loaded yet, so defer
+	 * closing them in that case to the end of the object lifetime,
+	 * unless the caller forces immediate cleanup.
+	 */
+	if (force || !obj->has_manual_progs) {
+		for (i = 0; i < obj->btf_module_cnt; i++) {
+			close(obj->btf_modules[i].fd);
+			btf__free(obj->btf_modules[i].btf);
+			free(obj->btf_modules[i].name);
+		}
+		obj->btf_module_cnt = 0;
+		obj->btf_module_cap = 0;
+		obj->btf_modules_loaded = false;
+		zfree(&obj->btf_modules);
 	}
-	obj->btf_module_cnt = 0;
-	obj->btf_module_cap = 0;
-	obj->btf_modules_loaded = false;
-	zfree(&obj->btf_modules);
 
-	/* clean up vmlinux BTF */
-	btf__free(obj->btf_vmlinux);
-	obj->btf_vmlinux = NULL;
+	/*
+	 * The btf_vmlinux data is needed for manually loaded programs,
+	 * so defer freeing it in that case to the end of the object lifetime.
+	 */
+	if (force || !obj->has_manual_progs) {
+		btf__free(obj->btf_vmlinux);
+		obj->btf_vmlinux = NULL;
+	}
 }
 
-static void bpf_object_post_load_cleanup(struct bpf_object *obj)
+static void bpf_object_post_load_cleanup(struct bpf_object *obj, bool force)
 {
-	/* clean up fd_array */
-	zfree(&obj->fd_array);
+	/*
+	 * The fd array is needed for manually loaded programs,
+	 * so defer freeing it in that case to the end of the object lifetime.
+	 */
+	if (force || !obj->has_manual_progs || !obj->fd_array_cnt)
+		zfree(&obj->fd_array);
 
 	/* clean up BTF */
-	bpf_object_cleanup_btf(obj);
+	bpf_object_cleanup_btf(obj, force);
 }
 
 static int bpf_object_prepare(struct bpf_object *obj, const char *target_btf_path)
@@ -9163,7 +9195,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
 			err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps);
 	}
 
-	bpf_object_post_load_cleanup(obj);
+	bpf_object_post_load_cleanup(obj, false);
 	obj->state = OBJ_LOADED; /* doesn't matter if successfully or not */
 
 	if (err) {
@@ -9631,7 +9663,7 @@ void bpf_object__close(struct bpf_object *obj)
 	 * bpf_object__load(), we need to clean up stuff that is normally
 	 * cleaned up at the end of loading step
 	 */
-	bpf_object_post_load_cleanup(obj);
+	bpf_object_post_load_cleanup(obj, true);
 
 	usdt_manager_free(obj->usdt_man);
 	obj->usdt_man = NULL;
@@ -9640,7 +9672,6 @@ void bpf_object__close(struct bpf_object *obj)
 	bpf_object__elf_finish(obj);
 	bpf_object_unload(obj);
 	btf__free(obj->btf);
-	btf__free(obj->btf_vmlinux);
 	btf_ext__free(obj->btf_ext);
 
 	for (i = 0; i < obj->nr_maps; i++)
@@ -9721,11 +9752,29 @@ int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)
 int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts)
 {
 	struct bpf_gen *gen;
+	size_t i;
 
 	if (!opts)
 		return libbpf_err(-EFAULT);
 	if (!OPTS_VALID(opts, gen_loader_opts))
 		return libbpf_err(-EINVAL);
+
+	/*
+	 * Manually-loaded programs are not visible to gen_loader (see
+	 * bpf_program__set_load_strategy()'s MANUAL case), and marking a
+	 * program MANUAL happens during bpf_object__open(), before this
+	 * function can ever run, so that guard can never catch it here.
+	 * Reject any pre-existing MANUAL program now, since this is the
+	 * earliest point where both are known.
+	 */
+	for (i = 0; i < obj->nr_programs; i++) {
+		if (obj->programs[i].load_strategy == BPF_PROG_LOAD_STRATEGY_MANUAL) {
+			pr_warn("prog '%s': gen_loader does not support manually-loaded programs\n",
+				obj->programs[i].name);
+			return libbpf_err(-EOPNOTSUPP);
+		}
+	}
+
 	gen = calloc(1, sizeof(*gen));
 	if (!gen)
 		return libbpf_err(-ENOMEM);
@@ -9802,16 +9851,13 @@ const char *bpf_program__section_name(const struct bpf_program *prog)
 
 bool bpf_program__autoload(const struct bpf_program *prog)
 {
-	return prog->autoload;
+	return prog->load_strategy == BPF_PROG_LOAD_STRATEGY_AUTO;
 }
 
 int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)
 {
-	if (prog->obj->state >= OBJ_LOADED)
-		return libbpf_err(-EINVAL);
-
-	prog->autoload = autoload;
-	return 0;
+	return bpf_program__set_load_strategy(prog,
+		autoload ? BPF_PROG_LOAD_STRATEGY_AUTO : BPF_PROG_LOAD_STRATEGY_DISABLED);
 }
 
 bool bpf_program__autoattach(const struct bpf_program *prog)
@@ -9819,9 +9865,13 @@ bool bpf_program__autoattach(const struct bpf_program *prog)
 	return prog->autoattach;
 }
 
-void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach)
+int bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach)
 {
+	if (prog->load_strategy == BPF_PROG_LOAD_STRATEGY_MANUAL)
+		return libbpf_err(-EINVAL);
+
 	prog->autoattach = autoattach;
+	return 0;
 }
 
 const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)
@@ -12745,7 +12795,7 @@ static int collect_func_ids_by_glob(const struct bpf_program *prog, const char *
 	err = collect_btf_func_ids_by_glob(btf, pattern, ids);
 
 cleanup:
-	bpf_object_cleanup_btf(obj);
+	bpf_object_cleanup_btf(obj, false);
 	return err;
 }
 
@@ -15125,7 +15175,7 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
 		struct bpf_program *prog = *prog_skel->prog;
 		struct bpf_link **link = prog_skel->link;
 
-		if (!prog->autoload || !prog->autoattach)
+		if (prog->load_strategy != BPF_PROG_LOAD_STRATEGY_AUTO || !prog->autoattach)
 			continue;
 
 		/* auto-attaching not supported for this program */
@@ -15235,3 +15285,101 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
 	free(s->progs);
 	free(s);
 }
+
+int bpf_program__set_load_strategy(struct bpf_program *prog, enum bpf_prog_load_strategy strategy)
+{
+	struct bpf_object *obj = prog->obj;
+
+	if (obj->state >= OBJ_PREPARED)
+		return libbpf_err(-EINVAL);
+
+	if (strategy == prog->load_strategy)
+		return 0;
+
+	switch (strategy) {
+	case BPF_PROG_LOAD_STRATEGY_DISABLED:
+	case BPF_PROG_LOAD_STRATEGY_AUTO:
+		if (prog->load_strategy == BPF_PROG_LOAD_STRATEGY_MANUAL)
+			prog->autoattach = prog->saved_autoattach;
+		prog->load_strategy = strategy;
+		break;
+	case BPF_PROG_LOAD_STRATEGY_MANUAL:
+		/*
+		 * Manually-loaded programs are not supported for gen_loader.
+		 * This is because bpf_object_load_prog is not called for
+		 * manually-loaded programs, so such programs are not visible
+		 * to gen_loader. For this reason, prevent calling
+		 * bpf_program__set_load_strategy(MANUAL) when gen_loader was
+		 * used to generate a BPF object loader.
+		 * A gen_loader implementation is being called for autoloaded
+		 * programs and defines its own model for loading BPF programs.
+		 * To pass a BPF program to gen_loader, set the program's load strategy
+		 * to BPF_PROG_LOAD_STRATEGY_AUTO.
+		 */
+		if (obj->gen_loader)
+			return libbpf_err(-EOPNOTSUPP);
+
+		if (prog_is_subprog(obj, prog))
+			return libbpf_err(-EINVAL);
+
+		prog->saved_autoattach = prog->autoattach;
+		prog->load_strategy = BPF_PROG_LOAD_STRATEGY_MANUAL;
+		prog->autoattach = false;
+		break;
+	default:
+		return libbpf_err(-EINVAL);
+	}
+
+	return 0;
+}
+
+enum bpf_prog_load_strategy bpf_program__load_strategy(const struct bpf_program *prog)
+{
+	return prog->load_strategy;
+}
+
+/*
+ * This function must be called after bpf_object__prepare (or
+ * bpf_object__load, which calls bpf_object__prepare internally).
+ * Manually-loaded program data is initialized on object prepare.
+ * Post-prepare initialization is not supported.
+ */
+int
+bpf_program__load_manually(struct bpf_program *prog)
+{
+	int err;
+	struct bpf_object *obj = prog->obj;
+
+	if (obj->state < OBJ_PREPARED)
+		return libbpf_err(-EINVAL);
+
+	if (prog_is_subprog(obj, prog) || prog->load_strategy != BPF_PROG_LOAD_STRATEGY_MANUAL)
+		return libbpf_err(-EINVAL);
+
+	if (prog->fd >= 0)
+		return libbpf_err(-EBUSY);
+
+	err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt,
+				   obj->license, obj->kern_version, &prog->fd);
+	if (err) {
+		pr_warn("prog '%s': failed to load manually: %d\n", prog->name, err);
+		return libbpf_err(err);
+	}
+
+	return 0;
+}
+
+int bpf_program__unload_manually(struct bpf_program *prog)
+{
+	int err;
+
+	if (prog->load_strategy != BPF_PROG_LOAD_STRATEGY_MANUAL)
+		return libbpf_err(-EINVAL);
+
+	/*
+	 * Close the file descriptor but retain the program's data to
+	 * support reloading the program if it is required again.
+	 */
+	err = zclose(prog->fd);
+	return err ? libbpf_err(-errno) : 0;
+}
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 48266e752223e..5914e5001b972 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -378,7 +378,7 @@ LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog)
 LIBBPF_API bool bpf_program__autoload(const struct bpf_program *prog);
 LIBBPF_API int bpf_program__set_autoload(struct bpf_program *prog, bool autoload);
 LIBBPF_API bool bpf_program__autoattach(const struct bpf_program *prog);
-LIBBPF_API void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);
+LIBBPF_API int bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach);
 
 struct bpf_insn;
 
@@ -2100,6 +2100,44 @@ LIBBPF_API int libbpf_unregister_prog_handler(int handler_id);
  */
 LIBBPF_API int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts *opts);
 
+/**
+ * The program load strategy:
+ *
+ * - BPF_PROG_LOAD_STRATEGY_DISABLED: the program is not loaded.
+ * - BPF_PROG_LOAD_STRATEGY_AUTO: the program is autoloaded when the bpf_object is loaded.
+ * - BPF_PROG_LOAD_STRATEGY_MANUAL: the program is loaded and attached manually.
+ */
+enum bpf_prog_load_strategy {
+	BPF_PROG_LOAD_STRATEGY_DISABLED = 0,
+	BPF_PROG_LOAD_STRATEGY_AUTO,
+	BPF_PROG_LOAD_STRATEGY_MANUAL,
+};
+
+/**
+ * @brief **bpf_program__set_load_strategy()** sets the load strategy of a
+ * BPF program, controlling whether and when it gets loaded into the kernel.
+ *
+ * Can only be called before the enclosing bpf_object is loaded.
+ *
+ * @param prog BPF program to update
+ * @param strategy new load strategy for the program
+ * @return 0 on success; negative error code if the object was already loaded
+ */
+LIBBPF_API int bpf_program__set_load_strategy(struct bpf_program *prog,
+					      enum bpf_prog_load_strategy strategy);
+
+/**
+ * @brief **bpf_program__load_strategy()** returns the current load strategy
+ * of a BPF program.
+ *
+ * @param prog BPF program to query
+ * @return current load strategy of the program
+ */
+LIBBPF_API enum bpf_prog_load_strategy bpf_program__load_strategy(const struct bpf_program *prog);
+
+LIBBPF_API int bpf_program__load_manually(struct bpf_program *prog);
+LIBBPF_API int bpf_program__unload_manually(struct bpf_program *prog);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index a811a1b3a0857..401a8e268d335 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -462,6 +462,10 @@ LIBBPF_1.8.0 {
 		bpf_program__attach_tracing_multi;
 		bpf_program__clear_flags;
 		bpf_program__clone;
+		bpf_program__load_manually;
+		bpf_program__load_strategy;
+		bpf_program__set_load_strategy;
+		bpf_program__unload_manually;
 		btf__find_by_name_kind_own;
 		btf__new_empty_opts;
 } LIBBPF_1.7.0;
diff --git a/tools/testing/selftests/bpf/prog_tests/dynamicload.c b/tools/testing/selftests/bpf/prog_tests/dynamicload.c
new file mode 100644
index 0000000000000..979bdc14f4ffd
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/dynamicload.c
@@ -0,0 +1,350 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include <time.h>
+#include "test_dynamicload.skel.h"
+
+#define READ_SZ 456
+
+/*
+ * prog4 is marked SEC("!...") in the source instead of being set
+ * imperatively; verify that an explicit bpf_program__set_load_strategy() call
+ * before load overrides the declarative default.
+ */
+static void dynamicload_verify_override(void)
+{
+	struct test_dynamicload *skel;
+	int err;
+
+	skel = test_dynamicload__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	err = bpf_program__set_load_strategy(skel->progs.prog4, BPF_PROG_LOAD_STRATEGY_DISABLED);
+	if (!ASSERT_OK(err, "set_load_strategy_disabled"))
+		goto cleanup;
+
+	if (!ASSERT_EQ(bpf_program__load_strategy(skel->progs.prog4),
+		       BPF_PROG_LOAD_STRATEGY_DISABLED, "prog4_load_strategy_overridden"))
+		goto cleanup;
+
+	/* keep the other autoload programs out of the way of this load */
+	bpf_program__set_load_strategy(skel->progs.prog1, BPF_PROG_LOAD_STRATEGY_DISABLED);
+	bpf_program__set_load_strategy(skel->progs.prog3, BPF_PROG_LOAD_STRATEGY_DISABLED);
+
+	err = test_dynamicload__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto cleanup;
+
+	/* prog4 was never loaded, so it cannot be loaded manually either */
+	err = bpf_program__load_manually(skel->progs.prog4);
+	ASSERT_ERR(err, "load_manually_after_override");
+
+cleanup:
+	test_dynamicload__destroy(skel);
+}
+
+/*
+ * prog4 is MANUAL via its SEC("!...") marker; verify that
+ * bpf_object__prepare() alone -- without ever calling bpf_object__load() --
+ * is sufficient for bpf_program__load_manually() to succeed, since BTF
+ * loading, map creation, and relocation of MANUAL programs are all
+ * completed by prepare() already.
+ */
+static void dynamicload_verify_prepare_only(void)
+{
+	struct test_dynamicload *skel;
+	struct bpf_link *link;
+	int err;
+
+	skel = test_dynamicload__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	err = bpf_object__prepare(skel->obj);
+	if (!ASSERT_OK(err, "bpf_object__prepare"))
+		goto cleanup;
+
+	err = bpf_program__load_manually(skel->progs.prog4);
+	if (!ASSERT_OK(err, "load_manually_after_prepare"))
+		goto cleanup;
+
+	if (!ASSERT_GE(bpf_program__fd(skel->progs.prog4), 0, "prog4_fd_after_prepare"))
+		goto cleanup;
+
+	link = bpf_program__attach(skel->progs.prog4);
+	if (!ASSERT_OK_PTR(link, "attach_after_prepare"))
+		goto cleanup;
+
+	usleep(1);
+
+	if (!ASSERT_TRUE(skel->bss->prog4_called, "prog4_called_after_prepare"))
+		goto cleanup;
+
+	err = bpf_link__destroy(link);
+	if (!ASSERT_OK(err, "link_destroy_after_prepare"))
+		goto cleanup;
+
+	err = bpf_program__unload_manually(skel->progs.prog4);
+	ASSERT_OK(err, "unload_manually_after_prepare");
+
+cleanup:
+	test_dynamicload__destroy(skel);
+}
+
+/*
+ * prog5 is disabled at parse time; resolve its attach target against
+ * module BTF via bpf_program__set_attach_target() before switching it
+ * to MANUAL and deferring its load past the bulk bpf_object__load().
+ * Regression test for the module BTF fd/array lifetime bug: without
+ * deferring the module BTF fd/array close for MANUAL programs, the fd
+ * cached in prog->attach_btf_obj_fd is closed by the bulk load's
+ * cleanup before this deferred load runs, causing a deterministic
+ * -EINVAL.
+ */
+static void dynamicload_verify_module_btf(void)
+{
+	struct test_dynamicload *skel;
+	struct bpf_link *link;
+	int err;
+
+	if (!env.has_testmod) {
+		test__skip();
+		return;
+	}
+
+	skel = test_dynamicload__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	err = bpf_program__set_attach_target(skel->progs.prog5, 0,
+					     "bpf_testmod:bpf_testmod_test_read");
+	if (!ASSERT_OK(err, "set_attach_target"))
+		goto cleanup;
+
+	err = bpf_program__set_load_strategy(skel->progs.prog5, BPF_PROG_LOAD_STRATEGY_MANUAL);
+	if (!ASSERT_OK(err, "set_load_strategy_manual"))
+		goto cleanup;
+
+	/* keep the other autoload programs out of the way of this load */
+	bpf_program__set_load_strategy(skel->progs.prog1, BPF_PROG_LOAD_STRATEGY_DISABLED);
+	bpf_program__set_load_strategy(skel->progs.prog2, BPF_PROG_LOAD_STRATEGY_DISABLED);
+	bpf_program__set_load_strategy(skel->progs.prog3, BPF_PROG_LOAD_STRATEGY_DISABLED);
+
+	/*
+	 * bulk load: prog5 itself is skipped (MANUAL), but this is where
+	 * module BTF gets torn down if not correctly deferred
+	 */
+	err = test_dynamicload__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto cleanup;
+
+	/*
+	 * deferred load must still succeed: the module BTF fd cached above
+	 * by set_attach_target() must still be a valid, open fd here
+	 */
+	err = bpf_program__load_manually(skel->progs.prog5);
+	if (!ASSERT_OK(err, "load_manually_module_btf"))
+		goto cleanup;
+
+	link = bpf_program__attach(skel->progs.prog5);
+	if (!ASSERT_OK_PTR(link, "attach"))
+		goto cleanup;
+
+	ASSERT_OK(trigger_module_test_read(READ_SZ), "trigger_read");
+	ASSERT_EQ(skel->bss->prog5_sz, READ_SZ, "prog5_sz");
+
+	bpf_link__destroy(link);
+
+cleanup:
+	test_dynamicload__destroy(skel);
+}
+
+void test_dynamicload(void)
+{
+	struct bpf_link *link;
+	struct test_dynamicload *skel;
+	int err;
+
+	skel = test_dynamicload__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	/*
+	 * the SEC("!...") prefix alone, with no imperative call, must set
+	 * prog4's load strategy before it is ever touched below
+	 */
+	if (!ASSERT_EQ(bpf_program__load_strategy(skel->progs.prog4),
+		       BPF_PROG_LOAD_STRATEGY_MANUAL, "prog4_prefix_load_strategy"))
+		goto cleanup;
+	if (!ASSERT_FALSE(bpf_program__autoattach(skel->progs.prog4), "prog4_autoattach"))
+		goto cleanup;
+
+	/* don't load prog1 */
+	bpf_program__set_load_strategy(skel->progs.prog1, BPF_PROG_LOAD_STRATEGY_DISABLED);
+
+	/* prog2 is autoload */
+	bpf_program__set_load_strategy(skel->progs.prog2, BPF_PROG_LOAD_STRATEGY_AUTO);
+
+	/* prog3 is manually loaded */
+	bpf_program__set_load_strategy(skel->progs.prog3, BPF_PROG_LOAD_STRATEGY_MANUAL);
+
+	err = test_dynamicload__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto cleanup;
+
+	err = test_dynamicload__attach(skel);
+	if (!ASSERT_OK(err, "skel_attach"))
+		goto cleanup;
+
+	/* trigger the BPF programs */
+	usleep(1);
+
+	ASSERT_FALSE(skel->bss->prog1_called, "prog1_called");
+	ASSERT_TRUE(skel->bss->prog2_called, "prog2_called");
+	ASSERT_FALSE(skel->bss->prog3_called, "prog3_called");
+	ASSERT_FALSE(skel->bss->prog4_called, "prog4_called");
+
+	/* prog1 is disabled for load */
+	err = bpf_program__load_manually(skel->progs.prog1);
+	if (!ASSERT_ERR(err, "load_manually_disabled"))
+		goto cleanup;
+
+	/* prog1 is disabled for load */
+	err = bpf_program__unload_manually(skel->progs.prog1);
+	if (!ASSERT_ERR(err, "unload_manually_disabled"))
+		goto cleanup;
+
+	/* prog2 is autoload */
+	err = bpf_program__load_manually(skel->progs.prog2);
+	if (!ASSERT_ERR(err, "load_manually_autoload"))
+		goto cleanup;
+
+	/* prog2 is autoload */
+	err = bpf_program__unload_manually(skel->progs.prog2);
+	if (!ASSERT_ERR(err, "unload_manually_autoload"))
+		goto cleanup;
+
+	/* reset the call flags */
+	skel->bss->prog2_called = false;
+	skel->bss->prog3_called = false;
+
+	usleep(1);
+
+	ASSERT_FALSE(skel->bss->prog1_called, "prog1_called");
+	ASSERT_TRUE(skel->bss->prog2_called, "prog2_called");
+	ASSERT_FALSE(skel->bss->prog3_called, "prog3_called");
+
+	/* load prog3 */
+	err = bpf_program__load_manually(skel->progs.prog3);
+	if (!ASSERT_OK(err, "load_manually"))
+		goto cleanup;
+
+	/* attach prog3 */
+	link = bpf_program__attach(skel->progs.prog3);
+	if (!ASSERT_OK_PTR(link, "attach"))
+		goto cleanup;
+
+	usleep(1);
+
+	if (!ASSERT_TRUE(skel->bss->prog3_called, "prog3_called"))
+		goto cleanup;
+
+	/* detach prog3 as test_dynamicload__destroy doesn't detach manually loaded programs */
+	err = bpf_link__destroy(link);
+	if (!ASSERT_OK(err, "link_destroy"))
+		goto cleanup;
+
+	/* reset the call flags after detach */
+	skel->bss->prog2_called = false;
+	skel->bss->prog3_called = false;
+
+	usleep(1);
+
+	ASSERT_TRUE(skel->bss->prog2_called, "prog2_called");
+	ASSERT_FALSE(skel->bss->prog3_called, "prog3_called");
+
+	/* unload prog3 */
+	err = bpf_program__unload_manually(skel->progs.prog3);
+	if (!ASSERT_OK(err, "unload_manually"))
+		goto cleanup;
+
+	/* reload prog3 */
+	err = bpf_program__load_manually(skel->progs.prog3);
+	if (!ASSERT_OK(err, "load_manually_reload"))
+		goto cleanup;
+
+	/* reattach prog3 */
+	link = bpf_program__attach(skel->progs.prog3);
+	if (!ASSERT_OK_PTR(link, "reattach"))
+		goto cleanup;
+
+	usleep(1);
+
+	if (!ASSERT_TRUE(skel->bss->prog3_called, "prog3_called_reattach"))
+		goto cleanup;
+
+	/* detach prog3 as test_dynamicload__destroy doesn't detach manually loaded programs */
+	err = bpf_link__destroy(link);
+	if (!ASSERT_OK(err, "link_destroy_reattach"))
+		goto cleanup;
+
+	/*
+	 * verify regular unload for manually loaded program,
+	 * unload prog3 as a regular program
+	 */
+	bpf_program__unload(skel->progs.prog3);
+
+	/* reset the call flags after unload */
+	skel->bss->prog2_called = false;
+	skel->bss->prog3_called = false;
+
+	usleep(1);
+
+	ASSERT_TRUE(skel->bss->prog2_called, "prog2_called");
+	ASSERT_FALSE(skel->bss->prog3_called, "prog3_called");
+
+	/* reloading prog3 must fail as it was unloaded as a regular program */
+	err = bpf_program__load_manually(skel->progs.prog3);
+	ASSERT_ERR(err, "load_manually_after_regular_unload");
+
+	/*
+	 * run prog4 (declaratively marked) through the same manual
+	 * load/attach/trigger/detach/unload cycle as prog3
+	 */
+	err = bpf_program__load_manually(skel->progs.prog4);
+	if (!ASSERT_OK(err, "prog4_load_manually"))
+		goto cleanup;
+
+	link = bpf_program__attach(skel->progs.prog4);
+	if (!ASSERT_OK_PTR(link, "prog4_attach"))
+		goto cleanup;
+
+	usleep(1);
+
+	if (!ASSERT_TRUE(skel->bss->prog4_called, "prog4_called"))
+		goto cleanup;
+
+	err = bpf_link__destroy(link);
+	if (!ASSERT_OK(err, "prog4_link_destroy"))
+		goto cleanup;
+
+	err = bpf_program__unload_manually(skel->progs.prog4);
+	ASSERT_OK(err, "prog4_unload_manually");
+
+	test_dynamicload__destroy(skel);
+
+	/* separate scenario: imperative override of the declarative marker */
+	dynamicload_verify_override();
+
+	/* separate scenario: prepare()-only is sufficient for manual load */
+	dynamicload_verify_prepare_only();
+
+	/* separate scenario: deferred manual load with a module BTF attach target */
+	dynamicload_verify_module_btf();
+	return;
+
+cleanup:
+	test_dynamicload__destroy(skel);
+}
+
diff --git a/tools/testing/selftests/bpf/prog_tests/load_type.c b/tools/testing/selftests/bpf/prog_tests/load_type.c
new file mode 100644
index 0000000000000..55ca53685863c
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/load_type.c
@@ -0,0 +1,157 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include <time.h>
+#include "test_load_type.skel.h"
+
+void test_load_type(void)
+{
+	struct bpf_link *link;
+	struct test_load_type *skel;
+	int err;
+
+	skel = test_load_type__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	/* don't load prog1 */
+	bpf_program__set_load_strategy(skel->progs.prog1, BPF_PROG_LOAD_STRATEGY_DISABLED);
+
+	/* load and attach prog2 */
+	bpf_program__set_load_strategy(skel->progs.prog2, BPF_PROG_LOAD_STRATEGY_AUTO);
+	if (!ASSERT_TRUE(bpf_program__autoload(skel->progs.prog2), "prog2_autoload"))
+		goto cleanup;
+
+	err = bpf_program__set_load_strategy(skel->progs.prog3, BPF_PROG_LOAD_STRATEGY_MANUAL);
+	if (!ASSERT_OK(err, "set_load_strategy_manual"))
+		goto cleanup;
+	if (!ASSERT_EQ(bpf_program__load_strategy(skel->progs.prog3), BPF_PROG_LOAD_STRATEGY_MANUAL,
+		       "prog3_load_strategy"))
+		goto cleanup;
+
+	/*
+	 * bpf_program__set_autoload() is a thin forwarder to
+	 * set_load_strategy(), restricted to AUTO/DISABLED to preserve its
+	 * original bool on/off meaning; it does change the load strategy of
+	 * a program that isn't currently BPF_PROG_LOAD_STRATEGY_AUTO.
+	 */
+	err = bpf_program__set_autoload(skel->progs.prog3, false);
+	if (!ASSERT_OK(err, "set_autoload_false"))
+		goto cleanup;
+
+	if (!ASSERT_EQ(bpf_program__load_strategy(skel->progs.prog3),
+		       BPF_PROG_LOAD_STRATEGY_DISABLED, "prog3_load_strategy_after_false"))
+		goto cleanup;
+
+	err = bpf_program__set_autoload(skel->progs.prog3, true);
+	if (!ASSERT_OK(err, "set_autoload_true"))
+		goto cleanup;
+
+	if (!ASSERT_EQ(bpf_program__load_strategy(skel->progs.prog3), BPF_PROG_LOAD_STRATEGY_AUTO,
+		       "prog3_load_strategy_after_true"))
+		goto cleanup;
+
+	err = bpf_program__set_load_strategy(skel->progs.prog3, BPF_PROG_LOAD_STRATEGY_MANUAL);
+	if (!ASSERT_OK(err, "set_load_strategy_manual_enum"))
+		goto cleanup;
+
+	if (!ASSERT_EQ(bpf_program__load_strategy(skel->progs.prog3), BPF_PROG_LOAD_STRATEGY_MANUAL,
+		       "prog3_load_strategy_after_manual_enum"))
+		goto cleanup;
+
+	/*
+	 * leaving MANUAL for AUTO must restore autoattach (regression test for
+	 * the autoattach residue bug: set_load_strategy(MANUAL) clears autoattach, and
+	 * nothing used to restore it on exit)
+	 */
+	err = bpf_program__set_load_strategy(skel->progs.prog3, BPF_PROG_LOAD_STRATEGY_AUTO);
+	if (!ASSERT_OK(err, "set_load_strategy_auto"))
+		goto cleanup;
+
+	if (!ASSERT_EQ(bpf_program__load_strategy(skel->progs.prog3), BPF_PROG_LOAD_STRATEGY_AUTO,
+		       "prog3_load_strategy_auto"))
+		goto cleanup;
+
+	if (!ASSERT_TRUE(bpf_program__autoattach(skel->progs.prog3), "prog3_autoattach_restored"))
+		goto cleanup;
+
+	/*
+	 * confirm the restore also holds across a MANUAL -> DISABLED -> AUTO
+	 * round-trip: AUTO and DISABLED share one guard keyed off the source
+	 * strategy being MANUAL, so autoattach is already restored at the
+	 * MANUAL -> DISABLED step, not by a separate DISABLED -> AUTO one
+	 */
+	err = bpf_program__set_load_strategy(skel->progs.prog3, BPF_PROG_LOAD_STRATEGY_MANUAL);
+	if (!ASSERT_OK(err, "set_load_strategy_manual_again"))
+		goto cleanup;
+
+	err = bpf_program__set_load_strategy(skel->progs.prog3, BPF_PROG_LOAD_STRATEGY_DISABLED);
+	if (!ASSERT_OK(err, "set_load_strategy_disabled"))
+		goto cleanup;
+
+	err = bpf_program__set_load_strategy(skel->progs.prog3, BPF_PROG_LOAD_STRATEGY_AUTO);
+	if (!ASSERT_OK(err, "set_load_strategy_auto_via_disabled"))
+		goto cleanup;
+
+	if (!ASSERT_TRUE(bpf_program__autoattach(skel->progs.prog3),
+			 "prog3_autoattach_restored_via_disabled"))
+		goto cleanup;
+
+	/* an out-of-range load strategy is rejected */
+	err = bpf_program__set_load_strategy(skel->progs.prog3, (enum bpf_prog_load_strategy)999);
+	if (!ASSERT_ERR(err, "set_load_strategy_invalid"))
+		goto cleanup;
+
+	/* change the strategy back to BPF_PROG_LOAD_STRATEGY_MANUAL for the rest of the test */
+	err = bpf_program__set_load_strategy(skel->progs.prog3, BPF_PROG_LOAD_STRATEGY_MANUAL);
+	if (!ASSERT_OK(err, "set_load_strategy_manual_final"))
+		goto cleanup;
+
+	if (!ASSERT_EQ(bpf_program__load_strategy(skel->progs.prog3), BPF_PROG_LOAD_STRATEGY_MANUAL,
+		       "prog3_load_strategy_final"))
+		goto cleanup;
+
+	err = test_load_type__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto cleanup;
+
+	if (!ASSERT_TRUE(bpf_program__autoattach(skel->progs.prog2), "prog2_autoattach"))
+		goto cleanup;
+	if (!ASSERT_FALSE(bpf_program__autoattach(skel->progs.prog3), "prog3_autoattach"))
+		goto cleanup;
+
+	/* loaded program strategy cannot be changed */
+	err = bpf_program__set_load_strategy(skel->progs.prog3, BPF_PROG_LOAD_STRATEGY_DISABLED);
+	ASSERT_ERR(err, "set_load_strategy_after_load");
+
+	err = test_load_type__attach(skel);
+	if (!ASSERT_OK(err, "skel_attach"))
+		goto cleanup;
+
+	usleep(1);
+
+	ASSERT_FALSE(skel->bss->prog1_called, "prog1_called");
+	ASSERT_TRUE(skel->bss->prog2_called, "prog2_called");
+	ASSERT_FALSE(skel->bss->prog3_called, "prog3_called");
+
+	err = bpf_program__load_manually(skel->progs.prog3);
+	if (!ASSERT_OK(err, "load_manually"))
+		goto cleanup;
+
+	/* attach prog3 */
+	link = bpf_program__attach(skel->progs.prog3);
+	if (!ASSERT_OK_PTR(link, "attach"))
+		goto cleanup;
+
+	usleep(1);
+
+	if (!ASSERT_TRUE(skel->bss->prog3_called, "prog3_called_again"))
+		goto cleanup;
+
+	/* detach prog3 as test_load_type__destroy doesn't detach manually loaded programs */
+	err = bpf_link__destroy(link);
+	ASSERT_OK(err, "link_destroy");
+
+cleanup:
+	test_load_type__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/signed_loader.c b/tools/testing/selftests/bpf/prog_tests/signed_loader.c
index a0f93756e717b..0648816e3d50a 100644
--- a/tools/testing/selftests/bpf/prog_tests/signed_loader.c
+++ b/tools/testing/selftests/bpf/prog_tests/signed_loader.c
@@ -2168,6 +2168,33 @@ static void signed_module_kfunc_rejected(void)
 	run_setup("cleanup", dir);
 }
 
+/*
+ * a program marked MANUAL is invisible to gen_loader's program count (it is
+ * skipped by bpf_object_load_progs()), so bpf_object__gen_loader() must
+ * reject the whole object up front instead of silently generating a
+ * skeleton whose prog_fd slots no longer line up with the object's programs
+ */
+static void manual_prog_rejected(void)
+{
+	LIBBPF_OPTS(gen_loader_opts, gopts, .gen_hash = true);
+	struct test_signed_loader *skel;
+	int err;
+
+	skel = test_signed_loader__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	err = bpf_program__set_load_strategy(skel->progs.probe, BPF_PROG_LOAD_STRATEGY_MANUAL);
+	if (!ASSERT_OK(err, "set_load_strategy_manual"))
+		goto cleanup;
+
+	err = bpf_object__gen_loader(skel->obj, &gopts);
+	ASSERT_ERR(err, "gen_loader_rejected");
+
+cleanup:
+	test_signed_loader__destroy(skel);
+}
+
 enum subtest_boot {
 	BOOT_ANY,
 	BOOT_SEALED,
@@ -2211,6 +2238,7 @@ static const struct {
 	{ "signed_map_by_fd_rejected", signed_map_by_fd_rejected, BOOT_SEALED },
 	{ "signed_sparse_fd_array_rejected", signed_sparse_fd_array_rejected, BOOT_SEALED },
 	{ "bpf_keyring_provisioned", bpf_keyring_provisioned, BOOT_UNSEALED },
+	{ "manual_prog_rejected", manual_prog_rejected, BOOT_ANY },
 };
 
 void test_signed_loader(void)
diff --git a/tools/testing/selftests/bpf/progs/test_dynamicload.c b/tools/testing/selftests/bpf/progs/test_dynamicload.c
new file mode 100644
index 0000000000000..156c90afe73e9
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_dynamicload.c
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+bool prog1_called = false;
+bool prog2_called = false;
+bool prog3_called = false;
+bool prog4_called = false;
+__u32 prog5_sz = 0;
+
+SEC("raw_tp/sys_enter")
+int prog1(const void *ctx)
+{
+	prog1_called = true;
+	return 0;
+}
+
+SEC("raw_tp/sys_enter")
+int prog2(const void *ctx)
+{
+	prog2_called = true;
+	return 0;
+}
+
+SEC("raw_tp/sys_enter")
+int prog3(const void *ctx)
+{
+	prog3_called = true;
+	return 0;
+}
+
+SEC("!raw_tp/sys_enter")
+int prog4(const void *ctx)
+{
+	prog4_called = true;
+	return 0;
+}
+
+/*
+ * disabled at parse time; its attach target is resolved against module
+ * BTF via bpf_program__set_attach_target() before it is switched to
+ * MANUAL and loaded
+ */
+SEC("?fentry")
+int BPF_PROG(prog5, struct file *file, struct kobject *kobj,
+	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
+{
+	prog5_sz = len;
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/test_load_type.c b/tools/testing/selftests/bpf/progs/test_load_type.c
new file mode 100644
index 0000000000000..3d9b81691d7ad
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_load_type.c
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+bool prog1_called = false;
+bool prog2_called = false;
+bool prog3_called = false;
+
+SEC("raw_tp/sys_enter")
+int prog1(const void *ctx)
+{
+	prog1_called = true;
+	return 0;
+}
+
+SEC("raw_tp/sys_enter")
+int prog2(const void *ctx)
+{
+	prog2_called = true;
+	return 0;
+}
+
+SEC("raw_tp/sys_enter")
+int prog3(const void *ctx)
+{
+	prog3_called = true;
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";

3/2 2026/09/20 00:13 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 17579
							output: 95
							thoughts: 1426
						

Thoughts:
**Analysis of the Patch for Fuzzing Worthiness**

Okay, let's take a look at this patch diff. My initial impression is that it primarily touches files within the `tools/` directory, which immediately triggers a mental check against the "WorthFuzzing" criteria. I need to determine if these changes impact the kernel image.

The listed modified files are: `tools/lib/bpf/libbpf.c`, `tools/lib/bpf/libbpf.h`, `tools/lib/bpf/libbpf.map`, and a series of files within `tools/testing/selftests/bpf/`. My first instinct is to confirm that *every* modified file falls within `tools/`. I scan the provided diff details and confirm that is indeed the case.

Now, I consult the "NEGATIVE CRITERIA" – the conditions under which fuzzing is *not* warranted. Specifically, I'm looking at the rule: "Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories: [...] User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/) that do not affect the compiled kernel image (vmlinux) or kernel modules."

`tools/lib/bpf` is, as I know, the user-space libbpf library. Similarly, `tools/testing/selftests` contains user-space self-tests. Neither of these, by their nature, directly influence the compiled kernel image or modules. I know that the kernel uses `resolve_btfids` at build time, but it doesn't affect `libbpf` code.

To eliminate any doubt, I meticulously review the provided diff. The explicit "diff --git" lines confirm that *only* files under the `tools/` path have been modified.

Considering the "NEGATIVE CRITERIA" rule and the confirmed file locations, it's clear that these changes are contained within user-space directories and do not impact the core kernel. Therefore, my assessment is that `WorthFuzzing` is `false`.

Given this, I set `FocusSymbols` and `EnableConfigs` to empty arrays (`[]`) and provide the appropriate reasoning: "All modified files reside in user-space directories (`tools/lib/bpf` and `tools/testing/selftests/bpf`) and do not alter the compiled Linux kernel image (`vmlinux`) or kernel modules."

Finally, I verify that the result format is correct: `WorthFuzzing: false`, and call set-results.



4/2 2026/09/20 00:13 tool set-results
0m Args:
{
  "EnableConfigs": [],
  "FocusSymbols": [],
  "Reasoning": "All changes in this patch series are strictly confined to user-space directories (tools/lib/bpf and tools/testing/selftests/bpf), introducing manual load strategy APIs to libbpf and accompanying selftests. No kernel-space code (vmlinux or kernel modules) is modified.",
  "WorthFuzzing": false
}

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [],
  "Reasoning": "All changes in this patch series are strictly confined to user-space directories (tools/lib/bpf and tools/testing/selftests/bpf), introducing manual load strategy APIs to libbpf and accompanying selftests. No kernel-space code (vmlinux or kernel modules) is modified.",
  "WorthFuzzing": false
}

5/1 2026/09/20 00:13 action If
0m Args:
{
  "WorthFuzzing": false
}

Results:
null

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)