从内核模块触发用户线程的最佳方法是什么 [英] what is the best way to trigger a user thread from a kernel module

查看:95
本文介绍了从内核模块触发用户线程的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在内核中有一个模块,在该模块中,我想唤醒特定的事件/计数,该用户线程将通过proc fs从内核模块读取一些数据.

I have a module in kernel in which on a particular event/count i want to wake a user thread that will read some data from kernel module via proc fs.

我正在尝试将RT信号从内核模块发送到处于睡眠状态的用户线程.该信号将调用其处理程序,该处理程序将进一步从内核读取数据/缓冲区并将其存储到文件中.

I am trying to send a RT signal from the kernel module to the user thread that is in sleeping state. The signal will call its handler and that will further read the data/buffer from kernel and store it to the files.

使用这种方法,我面临着一个未知的问题,在发送信号后,系统被挂起,一切都停止了.

I am facing some unknown issue using this approach, after sending the signal the system gets hanged and everything stops.

请告诉我我是否使用正确的方法(使用信号)从内核模块触发用户线程.如果正确,那么可能是锁定问题的根本原因.

Please tell me if I am using correct method (using signals) to trigger a user thread from kernel module. If it is correct then what would be possible root cause of the lockup issue.

我可以用什么连击方式来实现这种功能...请提出建议/帮助.

is there any batter way that I can use to implement such functionality... Please suggest/help.

内核模块:

static int send_signal(int data)
{
    int ret;
    struct siginfo info;
    struct task_struct *t;

    /* send the signal */
    memset(&info, 0, sizeof(info));
    /* I have tried 44 and 30 but both are not working */
    info.si_signo = sig_num;
    info.si_code = SI_QUEUE;
    info.si_int = data;

    if (!g_user_pid) {
        printk("error seding signal, pid is not configured");
        return -EAGAIN;
    }

    rcu_read_lock();
    t = pid_task(find_pid_ns(g_user_pid, &init_pid_ns), PIDTYPE_PID);
    if (t == NULL) {
        printk("invalid pid\n");
        rcu_read_unlock();
        return -EAGAIN;
    }

    printk("sending value %u to pid %d\n", info.si_int, (int)t->pid);
    ret = send_sig_info(sig_num, &info, t); /* send the signal */
    rcu_read_unlock();

    if (ret < 0) {
        printk("error sending signal\n");
        return ret;
    }
}

PS :我正在中断上下文中调用此函数.

PS : I am calling this function from interrupt context.

推荐答案

另一种方法是通过内核模块创建设备文件.从该文件的读取被阻止,直到驱动程序放入数据为止.在这种情况下,用户空间应用程序可以打开和读取设备文件数据,而无需任何睡眠和捕获信号.

Another way is to create device file by your kernel module. Reading from this file is blocked until driver puts data. In this case, user space application can open and read from device file data without any sleeping and catching signals.

更新:

Update: Here you can find example for creating read-only device file. You should add waiting logic in hello_read_proc(), this will generate block for reading.

这篇关于从内核模块触发用户线程的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆