电子产业一站式赋能平台

PCB联盟网

搜索
查看: 91|回复: 0
收起左侧

Linux内核调试之使用模块参数

[复制链接]

473

主题

473

帖子

5190

积分

四级会员

Rank: 4

积分
5190
发表于 2024-6-3 11:50:00 | 显示全部楼层 |阅读模式
开场白
  • 环境:处理器架构:arm64内核源码:linux-6.6.29ubuntu版本:20.04.1代码阅读工具:vim+ctags+cscope本文主要介绍内核开发中常用的模块传参手段,通过模块参数传递可以通过用户态来获取内核的一些信息,也可以通过用户态写入一些值来控制内核相关行为。一般内核开发者很喜欢使用模块传参来调试内核功能,如damon模块(数据访问监控器)。
    主要由以下部分组成:
  • 常用内核API
  • 支持的参数数据类型
  • 参数文件访问权限
  • 模块参数的读写
  • 示例代码
  • 参考资料[/ol]1.常用内核API1.1 module_param/**
    * module_param - typesafe helper for a module/cmdline parameter
    * @name: the variable to alter, and exposed parameter name.
    * @type: the type of the parameter
    * @perm: visibility in sysfs.
    *
    * @NAME becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
    * ".") the kernel commandline parameter.  Note that - is changed to _, so
    * the user can use "foo-bar=1" even for variable "foo_bar".
    *
    * @perm is 0 if the variable is not to appear in sysfs, or 0444
    * for world-readable, 0644 for root-writable, etc.  Note that if it
    * is writable, you may need to use kernel_param_lock() around
    * accesses (esp. charp, which can be kfreed when it changes).
    *
    * The @type is simply pasted to refer to a param_ops_##type and a
    * param_check_##type: for convenience many standard types are provided but
    * you can create your own by defining those variables.
    *
    * Standard types are:
    *      byte, hexint, short, ushort, int, uint, long, ulong
    *      charp: a character pointer
    *      bool: a bool, values 0/1, y/n, Y/N.
    *      invbool: the above, only sense-reversed (N = true).
    */
    #define module_param(name, type, perm)                          \
            module_param_named(name, name, type, perm)
    是最常规的传参方式,支持对普通数据类型的参数的读写。
  • name :表示模块参数名 (模块中定义和sysfs中显示的都是这个名字)
  • type:表示数据类型,如uint表示unsigned int
  • perm:sysfs文件中参数文件的访问权限 (一般8进制表示)
    例如:
    static unsigned int param_uint;
    module_param(param_uint, uint, 0600);
    MODULE_PARM_DESC(param_uint, "This is a uint parameter!");
    通过以下方式可以设置这个参数:
    1)加载模块时
    insmod module_param_test.ko param_uint=100
    2)cmdline传递
    cmdline中加入 module_param_test.param_uint=100 字段
    3)通过写sysfs节点
    echo 100 > /sys/module/module_param_test/parameters/param_uint
    通过sysfs查看模块参数:
    cat /sys/module/module_param_test/parameters/param_uint
    100
    1.2 module_param_array/**
    * module_param_array - a parameter which is an array of some type
    * @name: the name of the array variable
    * @type: the type, as per module_param()
    * @nump: optional pointer filled in with the number written
    * @perm: visibility in sysfs
    *
    * Input and output are as comma-separated values.  Commas inside values
    * don't work properly (eg. an array of charp).
    *
    * ARRAY_SIZE(@name) is used to determine the number of elements in the
    * array, so the definition must be visible.
    */
    #define module_param_array(name, type, nump, perm)              \
            module_param_array_named(name, name, type, nump, perm)
    即是数组类型支持。
  • name:表示数组名
  • type:数组元素类型
  • nump:一个整型变量,用于存储数组中元素的数量,可选(不关心可以写为NULL)
  • perm:sysfs文件中参数文件的权限 (一般8进制表示)
    例如:
    /* array: echo "1,2,3,4,4" > param_array */
    static int param_array[5];
    static int array_num;
    //module_param_array(param_char_array, int, NULL, 0600);
    module_param_array(param_array, int, &array_num, 0600);
    MODULE_PARM_DESC(param_bool, "This is a array parameter!");
    通过以下方式可以设置这个参数:
    1)加载模块时传递
    insmod module_param_test.ko param_array=1,2,3,4,4
    2)通过cmdline传递
    cmdline中加入 module_param_test.param_array=1,2,3,4,4 字段
    3)通过写sysfs节点
    echo 1,2,3,4,4 > /sys/module/module_param_test/parameters/param_array
    通过sysfs查看模块参数:
    cat /sys/module/module_param_test/parameters/param_array
    1,2,3,4,4
    1.3 module_param_cb/**
    * module_param_cb - general callback for a module/cmdline parameter
    * @name: a valid C identifier which is the parameter name.
    * @ops: the set & get operations for this parameter.
    * @arg: args for @ops
    * @perm: visibility in sysfs.
    *
    * The ops can have NULL set or get functions.
    */
    #define module_param_cb(name, ops, arg, perm)                                 \
            __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1, 0
    即是参数的回调函数支持。
  • name :表示模块参数名 (模块中定义和sysfs中显示的都是这个名字)
  • ops:参数的 set&get 操作集
  • arg:用于操作集的参数 perm:sysfs文件中参数文件的权限 (一般8进制表示)
    例如:
    static int param_int_cb;
    int param_int_cb_store(const char *val, const struct kernel_param *kp)
    {
            int value;
            int err;
            err = kstrtoint(val, 0, &value);
            if (err)
                    return err;
            if (value > 0)
                    pr_info("value:%d
    ", value);
            //return param_set_int(val, kp);
            return param_set_uint_minmax(val, kp, 0, 1000);
    }
    int param_int_cb_show(char *buffer, const struct kernel_param *kp)
    {
            int value = *((int *)kp->arg);
            if (value > 0)
                    return sprintf(buffer, "value:%d > 0
    ", value);
            else
                    return sprintf(buffer, "value:%d , value);
    }
    static const struct kernel_param_ops param_int_cb_ops = {
            .set = param_int_cb_store,
            //.get = param_get_int, /* default */
            .get = param_int_cb_show,
    };
    module_param_cb(param_int_cb, &param_int_cb_ops, &param_int_cb, 0600);
    MODULE_PARM_DESC(param_int_cb, "This is param_int_cb
    ");
    读写参数方式和上面介绍的类似,这里需要注意的是:当读参数param_int_cb时就会回调param_int_cb_show函数,写参数param_int_cb时就会回调param_int_cb_store,使得我们能有机会拦截参数来做一些操作。
    1.4 module_param_named/**
    * module_param_named - typesafe helper for a renamed module/cmdline parameter
    * @name: a valid C identifier which is the parameter name.
    * @value: the actual lvalue to alter.
    * @type: the type of the parameter
    * @perm: visibility in sysfs.
    *
    * Usually it's a good idea to have variable names and user-exposed names the
    * same, but that's harder if the variable must be non-static or is inside a
    * structure.  This allows exposure under a different name.
    */
    #define module_param_named(name, value, type, perm)                        \
            param_check_##type(name, &(value));                                \
            module_param_cb(name, &param_ops_##type, &value, perm);            \
            __MODULE_PARM_TYPE(name, #type)
    即是参数的重命名支持。
  • name:表示参数的别名/重命名,会在sysfs中显示
  • value:表示参数名,在模块中定义的变量名
  • type:表示数据类型
  • perm:sysfs文件中参数文件的权限
    例如:
    /* bool eg: echo 0/1/n/y/N/Y  > param_bool1_named */
    static bool param_bool1;
    module_param_named(param_bool1_named, param_bool1, bool, 0600);
    MODULE_PARM_DESC(param_bool1_named, "This is a bool parameter!");
    读写参数方式和上面介绍的类似,这里需要注意的是:模块中定义为param_bool1这个变量名,但是sysfs中使用的是这个param_bool1_named别名。
    注:都在include/linux/moduleparam.h文件中定义
    2.支持的参数数据类型内核支持的参数数据类型在定义module_param的时候有说明:
    include/linux/moduleparam.h
    Standard types are:
         byte, hexint, short, ushort, int, uint, long, ulong
         charp: a character pointer
         bool: a bool, values 0/1, y/n, Y/N.
         invbool: the above, only sense-reversed (N = true).
  • byte :表示字节大小,是无符号char类型 ,unsigned char
  • hexint:读的时候会显示16进制, 表示无符号的 int类型,即是 unsigned int
  • short:表示有符号的short类型,即是 short
  • ushort:表示无符号的short类型,即是 unsigned short
  • int:表示有符号的int类型,即是 int
  • uint:表示无符号的 int类型,即是 unsigned int
  • long:表示有符号的long类型,即是 long
  • ulong:表示无符号的long类型,即是 unsigned long
  • charp:char 指针类型,也就是字符串
  • bool:布尔类型
  • invbool:反布尔类型
  • 此外还支持llong (long long)和ullong (unsigned long long)类型。

    注:这些api的时候内核源码中有大量的例子,直接搜索即可知道内核开发者是如何使用。我们在实际内核开发中,如何在海量的源码中获得我们所需要的东西并在我们的优化代码中得以使用也是也是内核开发者需要具备的素养。
    3.参数文件访问权限常见权限如下:
  • 0 :无任何权限 ,在sysfs中不显示这个参数文件
  • 0666: -rwxrwxrwx 即是用户、组、其他 都可读可写 会编译错误,权限比较高,禁止使用。权限0666意味着任何用户都可以读写该文件。在内核模块中,通常需要保护模块的参数不被恶意修改,以避免潜在的安全风险。
  • 0444: -r--r--r--  -> 用户、组、其他都只读
  • 0600:-rw------- 用户可读可写,组、其他无权限
  • 0644:-rw-r--r-- 用户可读可写,组、其他只读
    当然也可以使用形如S_IRUSR这样的表示方法。
    4.模块参数的读写4.1 读对于内核态,直接读取定义的模块参数即可。
    而对于用户态,是通过sysfs来读取它的。
    读取格式:cat  /sys/module/xxx/parameters/param
    xxx表示想读取的模块 param表示具体的参数
    例如:示例中的module_param_test模块,读模块参数如下:cat  /sys/module/module_param_test/parameters/param_uint 100
    4.2 写对于内核态,直接读取定义的模块参数即可。
    而对于用户态,我们有三种方式来写模块参数。
    方法1:系统启动阶段通过cmdline传递一般用于buildin到内核的模块
    传参的方式为:module.param=val
    例如:module_param_test.param_charp=hello module_param_test.param_array=1,2,3,4,5
    方法2:加载模块时传递一般用于编译成模块的场景。
    传参的方式为:insmod xxx.ko param=val
    例如:insmod module_param_test.ko param_uint=100
    方法3:写sysfs中参数文件节点传参的方式为:echo xxx >/sys/module/xxx/parameters/param
    例如:echo 100 > /sys/module/module_param_test/parameters/param_uint
    5.示例代码#include
    #include
    #include
    /********** case 1: base type **********/
    /* bool eg: echo 0/1/n/y/N/Y  > param_bool*/
    static bool param_bool;
    //module_param(param_bool, bool, 0);//no permission, no file in sysfs
    //module_param(param_bool, bool, 0666);//-rwxrwxrwx -> forbit
    //module_param(param_bool, bool, 0644);//-rw-rw-rw-
    module_param(param_bool, bool, 0600);
    MODULE_PARM_DESC(param_bool, "This is a bool parameter!");
    /* bool eg: echo 0/1/n/y/N/Y  > param_bool1_named */
    static bool param_bool1;
    module_param_named(param_bool1_named, param_bool1, bool, 0600);
    MODULE_PARM_DESC(param_bool1_named, "This is a bool parameter!");
    /* byte  eg:  echo 0-255 > param_char */
    static unsigned char param_char;
    module_param(param_char, byte, 0600);
    MODULE_PARM_DESC(param_char, "This is a char parameter!");
    /* short eg: echo -100 > param_short */
    static short param_short;
    module_param(param_short, short, 0600);
    MODULE_PARM_DESC(param_short, "This is a short parameter!");
    /* unsigned short eg: echo 100 > param_short */
    static unsigned short param_ushort;
    module_param(param_ushort, ushort, 0600);
    MODULE_PARM_DESC(param_ushort, "This is a ushort parameter!");
    /* int eg: echo -100 > param_int */
    static int param_int;
    module_param(param_int, int, 0600);
    MODULE_PARM_DESC(param_int, "This is a int parameter!");
    /* unsigned int eg: echo 100 > param_unint */
    static unsigned int param_uint;
    module_param(param_uint, uint, 0600);
    MODULE_PARM_DESC(param_uint, "This is a uint parameter!");
    /* long eg: echo -100 > param_long*/
    static long param_long;
    module_param(param_long, long, 0600);
    MODULE_PARM_DESC(param_long, "This is a long parameter!");
    /* unsigned long eg: echo 100 > param_ulong */
    static unsigned long param_ulong;
    module_param(param_ulong, ulong, 0600);
    MODULE_PARM_DESC(param_ulong, "This is a ulong parameter!");
    /* unsigned long long eg: echo 100 > param_ullong */
    static unsigned long long param_ullong;
    module_param(param_ullong, ullong, 0600);
    MODULE_PARM_DESC(param_ullong, "This is a unsigned long long parameter!");
    /* character pointer : eg: echo hello > param_charp */
    static char *param_charp;
    module_param(param_charp, charp, 0600);
    MODULE_PARM_DESC(param_bool, "This is a charp parameter!");
    /********** case 2: array **********/
    /* array: echo "1,2,3,4,4" > param_array */
    static int param_array[5];
    static int array_num;
    //module_param_array(param_char_array, int, NULL, 0600);
    module_param_array(param_array, int, &array_num, 0600);
    MODULE_PARM_DESC(param_bool, "This is a array parameter!");
    /********** case 3: use call back **********/
    static int param_int_cb;
    int param_int_cb_store(const char *val, const struct kernel_param *kp)
    {
            int value;
            int err;        //把字符串转换为int类型
            err = kstrtoint(val, 0, &value);
            if (err)
                    return err;
            if (value > 0)
                    pr_info("value:%d
    ", value);
            //将用户态传过来的参数值设置到模块参数中,由于这里是基础的int类型,所以可以直接调用param_set_int api
            //param_set_uint_minmax 这个api会在设置时考虑最小和最大值
            //return param_set_int(val, kp);
            return param_set_uint_minmax(val, kp, 0, 1000);
    }
    int param_int_cb_show(char *buffer, const struct kernel_param *kp)
    {
            int value = *((int *)kp->arg);
            //用户态最终通过buffer来获得参数的信息,所以这里通过sprintf 做格式化操作写到buffer中
            if (value > 0)
                    return sprintf(buffer, "value:%d > 0
    ", value);
            else
                    return sprintf(buffer, "value:%d , value);
    }
    static const struct kernel_param_ops param_int_cb_ops = {
            .set = param_int_cb_store,
            //.get = param_get_int, /* default */
            .get = param_int_cb_show,
    };
    module_param_cb(param_int_cb, &param_int_cb_ops, &param_int_cb, 0600);
    MODULE_PARM_DESC(param_int_cb, "This is param_int_cb
    ");

    static int __init module_test_init(void)
    {
            pr_emerg("module_test_init
    ");
            return 0;
    }
    static void __exit module_test_exit(void)
    {
            pr_emerg("module_test_exit
    ");
    }

    module_init(module_test_init);
    module_exit(module_test_exit);
    MODULE_LICENSE("GPL");
    6.参考资料
  • include/linux/moduleparam.hkernel/params.cmm/damon/reclaim.cend

    一口Linux

    关注,回复【1024】海量Linux资料赠送
    精彩文章合集
    文章推荐
    ?【专辑】ARM?【专辑】粉丝问答?【专辑】所有原创?【专辑】linux入门?【专辑】计算机网络?【专辑】Linux驱动?【干货】嵌入式驱动工程师学习路线?【干货】Linux嵌入式所有知识点-思维导图
  • 回复

    使用道具 举报

    发表回复

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则


    联系客服 关注微信 下载APP 返回顶部 返回列表