Jak zatrzymać wątki jądra Linuksa na rmmod?

Napisałem następujący kod, aby utworzyć wątek jądra:

#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);

Po podaniu polecenia insmod, jestem w stanie utworzyć wątek jądra o nazwie "pradeep" i mogę zobaczyć nowy wątek za pomocą ps -ef polecenie w następujący sposób

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

A jego rodzicem jest kthreadd, którego PID wynosi 2. Ale nie jestem w stanie zatrzymać tego wątku po podaniu komendy rmmod. Daje następujące wyjście:

ERROR: Removing 'pradeep': Device or resource busy.
Czy ktoś może mi powiedzieć jak zabić ten wątek?

1 answers

Należy używać tylko jednego z kthread_create() lub 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;                                                               \
})

Więc tworzysz dwa wątki i wyciekasz jeden z nich:

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

Ponadto funkcja wątku może brakować niektórych szczegółów:

/**
 * 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).
 */

Myślę, że dwie opcje zakończenia wątku to:

    Zadzwoń kiedy skończysz.
  1. Zwraca wartość, gdy inny wątek wywoła kthread_stop().

Mam nadzieję, że po naprawieniu tych dwóch małych problemów, będziesz miał funkcjonalny wątek twórca / Żniwiarz.

 39
Author: ,
Warning: date() expects parameter 2 to be long, string given in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54