如何在rmmod上停止Linux内核线程? [英] How to stop Linux kernel threads on rmmod?

查看:259
本文介绍了如何在rmmod上停止Linux内核线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码来创建内核线程:

I wrote the following code to create a kernel thread:

#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/kthread.h>
#include<linux/sched.h>

struct task_struct *task;
int data;
int ret;
int thread_function(void *data)
{
    int var;
    var = 10;
    return var;
}

static int kernel_init(void)
{
    data = 20;
    printk(KERN_INFO"--------------------------------------------");
    task = kthread_create(&thread_function,(void *)data,"pradeep");
    task = kthread_run(&thread_function,(void *)data,"pradeep");
    printk(KERN_INFO"Kernel Thread : %s\n",task->comm);
    return 0;
}

static void kernel_exit(void)
{
    ret = kthread_stop(task);
}

module_init(kernel_init);
module_exit(kernel_exit);

给出insmod命令后,我可以创建一个名为"pradeep"的内核线程,并且可以使用 ps -ef命令如下

On giving the insmod command, I am able to create a kernel thread named "pradeep" and I can see the new thread using the ps -ef command as follows

root      6071     2  0 10:21 ?        00:00:00 [pradeep]

并且其父级是kthreadd,其PID为2. 但是我无法在发出rmmod命令时停止此线程.它给出以下输出:

and its parent is kthreadd whose PID is 2. But I am not able to stop this thread on giving rmmod command. It is giving the following output:

ERROR: Removing 'pradeep': Device or resource busy.

有人可以告诉我如何杀死该线程吗?

Can somebody please tell me how to kill this thread?

推荐答案

您应仅使用kthread_create()kthread_run()之一:

/**
 * kthread_run - create and wake a thread.
 * @threadfn: the function to run until signal_pending(current).
 * @data: data ptr for @threadfn.
 * @namefmt: printf-style name for the thread.
 *
 * Description: Convenient wrapper for kthread_create() followed by
 * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
 */
#define kthread_run(threadfn, data, namefmt, ...)                      \
({                                                                     \
    struct task_struct *__k                                            \
            = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
    if (!IS_ERR(__k))                                                  \
            wake_up_process(__k);                                      \
    __k;                                                               \
})

因此,您要创建两个线程并泄漏其中一个线程:

So you're creating two threads and leaking one of them:

task = kthread_create(&thread_function,(void*) &data,"pradeep");
task = kthread_run(&thread_function,(void*) &data,"pradeep");

此外,您的线程函数可能缺少一些详细信息:

Furthermore, your thread function might be missing some details:

/**
 * kthread_create - create a kthread.
 * @threadfn: the function to run until signal_pending(current).
 * @data: data ptr for @threadfn.
 * @namefmt: printf-style name for the thread.
 *
 * Description: This helper function creates and names a kernel
 * thread.  The thread will be stopped: use wake_up_process() to start
 * it.  See also kthread_run().
 *
 * When woken, the thread will run @threadfn() with @data as its
 * argument. @threadfn() can either call do_exit() directly if it is a
 * standalone thread for which noone will call kthread_stop(), or
 * return when 'kthread_should_stop()' is true (which means
 * kthread_stop() has been called).  The return value should be zero
 * or a negative error number; it will be passed to kthread_stop().
 *
 * Returns a task_struct or ERR_PTR(-ENOMEM).
 */

我认为终止线程的两个选择是:

I think the two choices for terminating a thread are:

  1. 完成后致电do_exit().
  2. 当另一个线程调用kthread_stop()时返回一个值.
  1. Call do_exit() when you're done.
  2. Return a value when another thread calls kthread_stop().

希望在解决了这两个小问题之后,您将拥有一个功能强大的线程创建者/收割者.

Hopefully after fixing these two small problems, you'll have a functional thread creator / reaper.

这篇关于如何在rmmod上停止Linux内核线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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