You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为自定义Linux内核模块配置configfs失败:返回-EINVAL问题求助

解决configfs_register_subsystem返回-EINVAL的问题

核心原因

configfs_register_subsystem()返回-22(-EINVAL)通常是因为子系统结构体及关联的configfs对象未正确初始化,尤其是以下几个关键字段缺失或错误:

  • 子系统名称未设置
  • config_item_type结构体未正确关联操作集和属性数组
  • 属性数组未以NULL结尾
  • 内核未启用CONFIGFS支持

解决步骤

1. 确认内核CONFIGFS配置

首先检查内核是否开启了CONFIGFS支持:

zcat /proc/config.gz | grep CONFIG_CONFIGFS_FS

如果输出为CONFIG_CONFIGFS_FS=m或y,说明已支持;如果是n或无输出,需要重新编译内核开启该选项,或手动加载configfs模块:

modprobe configfs

同时确保/sys/kernel/config已挂载:

mount | grep configfs

如果未挂载,执行:

mount -t configfs configfs /sys/kernel/config

2. 修正模块代码

以下是正确的最小化configfs模块示例,对比你现有代码检查缺失项:

#include <linux/module.h>
#include <linux/configfs.h>

// 子系统默认属性数组(必须以NULL结尾,无属性则只留NULL)
static struct configfs_attribute *mymod_attrs[] = {
    NULL,
};

// 子系统组操作集(无特殊操作可留空,但结构体必须存在)
static const struct configfs_group_operations mymod_group_ops = {
};

// 子系统item类型定义,关联操作集和属性
static const struct config_item_type mymod_item_type = {
    .ct_group_ops = &mymod_group_ops,
    .ct_attrs = mymod_attrs,
    .ct_owner = THIS_MODULE, // 必须指定模块所有者
};

// 核心子系统结构体,初始化名称和类型
static struct configfs_subsystem mymod_subsys = {
    .su_group = {
        .cg_item = {
            .ci_namebuf = "mymod", // 子系统名称,对应/sys/kernel/config/mymod
            .ci_type = &mymod_item_type,
        },
    },
};

static int __init mymod_init(void)
{
    int ret = configfs_register_subsystem(&mymod_subsys);
    if (ret) {
        pr_err("configfs subsystem register failed: %d\n", ret);
        return ret;
    }
    pr_info("mymod configfs subsystem loaded\n");
    return 0;
}

static void __exit mymod_exit(void)
{
    configfs_unregister_subsystem(&mymod_subsys);
    pr_info("mymod configfs subsystem unloaded\n");
}

module_init(mymod_init);
module_exit(mymod_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Minimal working configfs module");
MODULE_AUTHOR("Your Name");

3. 关键错误点排查

  • 未设置ci_namebuf:这是最常见的错误,必须给子系统指定唯一名称,否则注册时会因参数无效返回-EINVAL。
  • 属性数组未以NULL结尾:configfs会遍历属性数组,未以NULL结尾会导致越界,触发参数校验失败。
  • config_item_type未赋值:ci_type必须指向有效的config_item_type结构体,否则无法识别子系统的操作规则。

测试验证

编译模块后加载:

insmod mymod.ko

如果成功,执行以下命令确认目录生成:

ls /sys/kernel/config/mymod

卸载模块:

rmmod mymod

内容的提问来源于stack exchange,提问作者NRagot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 19:22:52