Linux 内核线程:如何将Linux 模块写入函数作为线程必须执行的函数传递?

Linux Kernel Threads : How to pass the Linux module write function as the function that the thread has to execute?

我正在开发与用户 space 程序通信的 Linux 内核模块。此模块等待从用户 space 发送的消息,以便在内核模式下打印它。

这是模块:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>  
#include <linux/kernel.h>
#include <linux/uaccess.h>

MODULE_LICENSE("GPL");      
MODULE_AUTHOR("Gaston");  
MODULE_DESCRIPTION("A simple Linux char driver"); 
MODULE_VERSION("0.1"); 

#define MAX 256
static char message[MAX] ="";      ///< Memory for the string that is passed from userspace

ssize_t exer_open(struct inode *pinode, struct file *pfile) {

    printk(KERN_INFO "Device has been opened\n");
    return 0;
}


ssize_t exer_read(struct file *pfile, char __user *buffer, size_t length, loff_t *offset) {

    return 0;
}


ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset) {
    if (length > MAX)
        return -EINVAL;

    if (copy_from_user(message, buffer, length) != 0)
        return -EFAULT;

    printk(KERN_INFO "Received %s characters from the user\n", message);
    return 0;

}   


ssize_t exer_close(struct inode *pinode, struct file *pfile) {

    printk(KERN_INFO "Device successfully closed\n");
    return 0;
}


struct file_operations exer_file_operations = { 
    .owner = THIS_MODULE,
    .open = exer_open,
    .read = exer_read,
    .write = exer_write,
    .release = exer_close,
};


int exer_simple_module_init(void) {

    printk(KERN_INFO "Initializing the LKM\n");
    register_chrdev(240, "Simple Char Drv", &exer_file_operations);
    return 0;
}


void exer_simple_module_exit(void) {

    unregister_chrdev(240, "Simple Char Drv");
}


module_init(exer_simple_module_init);
module_exit(exer_simple_module_exit);

这是用户-space程序:

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>


#define BUFFER_LENGTH 256 

int main()

{

int ret, fd;
char stringToSend[BUFFER_LENGTH];


fd = open("/dev/char_device", O_RDWR);             // Open the device with read/write access

if (fd < 0)
    {
            perror("Failed to open the device...");
            return errno;
    }


printf("Type in a short string to send to the kernel module:\n");

scanf("%s", stringToSend);                // Read in a string (with spaces)

printf("Writing message to the device [%s].\n", stringToSend);

ret = write(fd, stringToSend, strlen(stringToSend)); // Send the string to the LKM

if (ret < 0)
    {
            perror("Failed to write the message to the device.");
            return errno;
    }

return 0;

}

这工作正常,我从 user-space 输入的消息打印在内核 space.

现在,我正在使用这个例子来学习内核线程编程

我正在尝试将 LKM write 函数 作为内核线程必须执行的函数,所以我修改了我的模块:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>  
#include <linux/kernel.h>
#include <linux/uaccess.h>
#include <linux/kthread.h>

MODULE_LICENSE("GPL");      
MODULE_AUTHOR("Gaston");  
MODULE_DESCRIPTION("A simple Linux char driver"); 
MODULE_VERSION("0.1"); 


#define MAX 256
static struct task_struct *thread1;
static char message[MAX] ="";           ///< Memory for the string that is passed from userspace


ssize_t exer_open(struct inode *pinode, struct file *pfile) {

    printk(KERN_INFO "Device has been opened\n");
    return 0;
}



ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset) {

    if (length > MAX)
        return -EINVAL;

    printk("Thread_fuction is running ...\n");

    if (copy_from_user(message, buffer, length) != 0)
        return -EFAULT;

    printk(KERN_INFO "Received this message : %s ,from the user\n", message);

    return 0;

}   




struct file_operations exer_file_operations = { 
    .owner = THIS_MODULE,
    .open = exer_open,
    .write = exer_write,    
};




int exer_simple_module_init(void) {

    char our_thread[8]="thread1";

    printk(KERN_INFO "Initializing the LKM\n");
    register_chrdev(240, "Simple Char Drv", &exer_file_operations);

    thread1 = kthread_create(exer_write,NULL,our_thread);
    if((thread1))
            {
                printk(KERN_INFO "Thread is created");
                wake_up_process(thread1);
            }

    return 0;
}



void exer_simple_module_exit(void) {

    int ret;    

    unregister_chrdev(240, "Simple Char Drv");

    ret = kthread_stop(thread1);
    if(!ret)
        printk(KERN_INFO "Thread stopped");
}


module_init(exer_simple_module_init);
module_exit(exer_simple_module_exit);

问题在这里,我在编译模块的时候,出现了错误:

/exer_simple_char_drv.c:63:27: error: passing argument 1 of ‘kthread_create_on_node’ from incompatible pointer type [-Werror=incompatible-pointer-types] thread1 = kthread_create(exer_write,NULL,our_thread);

在网上搜索,我发现一个线程是通过调用函数创建的:

struct task_struct *kthread_create(int (*function)(void *data), void *data, const char name[], ...)

此函数将线程必须执行的函数作为第一个参数,并将其作为参数 (void *data),但是,我编写的内核函数没有相同的参数:

ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)

请问有办法解决这个问题吗?如何修改我的 Linux 内核以便将 write 函数作为线程执行?

谢谢。

你不能。您的线程函数必须使用这种格式:

int my_thread_function(void *data)

您可以随心所欲地调用它 - 它不必被称为 my_thread_function - 并且参数不必被称为 data 但它必须是 void *.

这行不通:

ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)

我建议编写一个新函数作为线程函数:

int exer_write_in_thread(void *data) {
    exer_write(???, ???, ???, ???);
    return 0;
}

显然你必须弄清楚你想用什么参数调用 exer_write