Make btf_parse_module() take the vmlinux BTF as an argument instead of
fetching it with bpf_get_btf_vmlinux(), and add a data_owned flag: when
set, the passed .BTF data is an already kvmalloc()ed copy that the new
btf takes ownership of on success (on failure the caller keeps it).

Factor the sysfs file creation out of the module notifier into
btf_module_sysfs_add() and the teardown into btf_module_free(), and set
btf_mod->module right after the allocation rather than under the mutex.

No functional change.  This prepares for CONFIG_DEBUG_INFO_BTF=m, where a
module can be loaded before the vmlinux BTF is available: its .BTF is
then copied and exposed in sysfs first and parsed later, at which point
the parser must take the copy as is so that the sysfs file keeps
pointing at valid data.

Signed-off-by: Jay Wang <[email protected]>
---
 kernel/bpf/btf.c | 105 +++++++++++++++++++++++++++++------------------
 1 file changed, 64 insertions(+), 41 deletions(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 4a1fa4fbdf4e..a6634237dc89 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -6516,16 +6516,20 @@ __u32 btf_relocate_id(const struct btf *btf, __u32 id)
 
 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
 
-static struct btf *btf_parse_module(const char *module_name, const void *data,
-                                   unsigned int data_size, void *base_data,
-                                   unsigned int base_data_size)
+/*
+ * Parse split module BTF against @vmlinux_btf.  @data is the module's .BTF
+ * section; if @data_owned, it is an already kvmalloc()ed copy that the new
+ * btf takes ownership of on success (on failure the caller keeps it).
+ */
+static struct btf *btf_parse_module(const char *module_name, struct btf 
*vmlinux_btf,
+                                   void *data, unsigned int data_size, bool 
data_owned,
+                                   void *base_data, unsigned int 
base_data_size)
 {
-       struct btf *btf = NULL, *vmlinux_btf, *base_btf = NULL;
+       struct btf *btf = NULL, *base_btf = NULL;
        struct btf_verifier_env *env = NULL;
        struct bpf_verifier_log *log;
        int err = 0;
 
-       vmlinux_btf = bpf_get_btf_vmlinux();
        if (IS_ERR(vmlinux_btf))
                return vmlinux_btf;
        if (!vmlinux_btf)
@@ -6562,7 +6566,10 @@ static struct btf *btf_parse_module(const char 
*module_name, const void *data,
        btf->named_start_id = 0;
        strscpy(btf->name, module_name);
 
-       btf->data = kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN);
+       if (data_owned)
+               btf->data = data;
+       else
+               btf->data = kvmemdup(data, data_size, GFP_KERNEL | 
__GFP_NOWARN);
        if (!btf->data) {
                err = -ENOMEM;
                goto errout;
@@ -6605,7 +6612,8 @@ static struct btf *btf_parse_module(const char 
*module_name, const void *data,
        if (!IS_ERR(base_btf) && base_btf != vmlinux_btf)
                btf_free(base_btf);
        if (btf) {
-               kvfree(btf->data);
+               if (!data_owned)
+                       kvfree(btf->data);
                kvfree(btf->types);
                kfree(btf);
        }
@@ -8610,6 +8618,48 @@ static DEFINE_MUTEX(btf_module_mutex);
 
 static void purge_cand_cache(struct btf *btf);
 
+static int btf_module_sysfs_add(struct btf_module *btf_mod, const char *name,
+                               void *data, size_t data_size)
+{
+       struct bin_attribute *attr;
+       int err;
+
+       if (!IS_ENABLED(CONFIG_SYSFS))
+               return 0;
+
+       attr = kzalloc_obj(*attr);
+       if (!attr)
+               return -ENOMEM;
+
+       sysfs_bin_attr_init(attr);
+       attr->attr.name = name;
+       attr->attr.mode = 0444;
+       attr->size = data_size;
+       attr->private = data;
+       attr->read = sysfs_bin_attr_simple_read;
+
+       err = sysfs_create_bin_file(btf_kobj, attr);
+       if (err) {
+               pr_warn("failed to register module [%s] BTF in sysfs: %d\n",
+                       name, err);
+               kfree(attr);
+               return err;
+       }
+
+       btf_mod->sysfs_attr = attr;
+       return 0;
+}
+
+static void btf_module_free(struct btf_module *btf_mod)
+{
+       if (btf_mod->sysfs_attr)
+               sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr);
+       purge_cand_cache(btf_mod->btf);
+       btf_put(btf_mod->btf);
+       kfree(btf_mod->sysfs_attr);
+       kfree(btf_mod);
+}
+
 static int btf_module_notify(struct notifier_block *nb, unsigned long op,
                             void *module)
 {
@@ -8630,7 +8680,10 @@ static int btf_module_notify(struct notifier_block *nb, 
unsigned long op,
                        err = -ENOMEM;
                        goto out;
                }
-               btf = btf_parse_module(mod->name, mod->btf_data, 
mod->btf_data_size,
+               btf_mod->module = module;
+
+               btf = btf_parse_module(mod->name, bpf_get_btf_vmlinux(),
+                                      mod->btf_data, mod->btf_data_size, false,
                                       mod->btf_base_data, 
mod->btf_base_data_size);
                if (IS_ERR(btf)) {
                        kfree(btf_mod);
@@ -8652,37 +8705,12 @@ static int btf_module_notify(struct notifier_block *nb, 
unsigned long op,
 
                purge_cand_cache(NULL);
                mutex_lock(&btf_module_mutex);
-               btf_mod->module = module;
                btf_mod->btf = btf;
                list_add(&btf_mod->list, &btf_modules);
                mutex_unlock(&btf_module_mutex);
 
-               if (IS_ENABLED(CONFIG_SYSFS)) {
-                       struct bin_attribute *attr;
-
-                       attr = kzalloc_obj(*attr);
-                       if (!attr)
-                               goto out;
-
-                       sysfs_bin_attr_init(attr);
-                       attr->attr.name = btf->name;
-                       attr->attr.mode = 0444;
-                       attr->size = btf->data_size;
-                       attr->private = btf->data;
-                       attr->read = sysfs_bin_attr_simple_read;
-
-                       err = sysfs_create_bin_file(btf_kobj, attr);
-                       if (err) {
-                               pr_warn("failed to register module [%s] BTF in 
sysfs: %d\n",
-                                       mod->name, err);
-                               kfree(attr);
-                               err = 0;
-                               goto out;
-                       }
-
-                       btf_mod->sysfs_attr = attr;
-               }
-
+               /* not fatal, the module BTF is usable without the sysfs file */
+               btf_module_sysfs_add(btf_mod, btf->name, btf->data, 
btf->data_size);
                break;
        case MODULE_STATE_LIVE:
                mutex_lock(&btf_module_mutex);
@@ -8709,12 +8737,7 @@ static int btf_module_notify(struct notifier_block *nb, 
unsigned long op,
                         */
                        btf_free_id(btf_mod->btf);
                        list_del(&btf_mod->list);
-                       if (btf_mod->sysfs_attr)
-                               sysfs_remove_bin_file(btf_kobj, 
btf_mod->sysfs_attr);
-                       purge_cand_cache(btf_mod->btf);
-                       btf_put(btf_mod->btf);
-                       kfree(btf_mod->sysfs_attr);
-                       kfree(btf_mod);
+                       btf_module_free(btf_mod);
                        break;
                }
                mutex_unlock(&btf_module_mutex);
-- 
2.47.3


Reply via email to