How to call exported kernel module functions from another module?

后端 未结 3 1142
难免孤独
难免孤独 2020-11-29 05:29

I\'m writing an API as a kernel module that provides device drivers with various functions. I wrote three functions in mycode.c. I then built and loaded the

3条回答
  •  -上瘾入骨i
    2020-11-29 05:38

    Minimal QEMU + Buildroot example

    I have tested the following in a fully reproducible QEMU + Buildroot environment, so maybe having this working version version will help you find out what is wong with your code.

    GitHub upstream is centered on the files:

    • dep.c
    • dep2.c
    • Makefile

    dep.c

    #include  /* usleep_range */
    #include 
    #include 
    #include 
    
    MODULE_LICENSE("GPL");
    
    int lkmc_dep = 0;
    EXPORT_SYMBOL(lkmc_dep);
    static struct task_struct *kthread;
    
    static int work_func(void *data)
    {
        while (!kthread_should_stop()) {
            printk(KERN_INFO "%d\n", lkmc_dep);
            usleep_range(1000000, 1000001);
        }
        return 0;
    }
    
    static int myinit(void)
    {
        kthread = kthread_create(work_func, NULL, "mykthread");
        wake_up_process(kthread);
        return 0;
    }
    
    static void myexit(void)
    {
        kthread_stop(kthread);
    }
    
    module_init(myinit)
    module_exit(myexit)
    

    dep2.c

    #include  /* usleep_range */
    #include 
    #include 
    #include 
    
    MODULE_LICENSE("GPL");
    
    extern int lkmc_dep;
    static struct task_struct *kthread;
    
    static int work_func(void *data)
    {
        while (!kthread_should_stop()) {
            usleep_range(1000000, 1000001);
            lkmc_dep++;
        }
        return 0;
    }
    
    static int myinit(void)
    {
        kthread = kthread_create(work_func, NULL, "mykthread");
        wake_up_process(kthread);
        return 0;
    }
    
    static void myexit(void)
    {
        kthread_stop(kthread);
    }
    
    module_init(myinit)
    module_exit(myexit)
    

    And now you can do:

    insmod dep.ko
    insmod dep2.ko
    

    With that Buildroot setup, things are already configuring depmod /lib/module/*/depmod with the dependency, so just this is enough to load both:

    modprobe dep
    

    Also, if you built your kernel with CONFIG_KALLSYMS_ALL=y, then the exported symbol can be seen with:

    grep lkmc_dep /proc/kallsyms
    

    see also: Does kallsyms have all the symbol of kernel functions?

提交回复
热议问题