【问题标题】:kernel hang when not doing tasklet_kill不执行 tasklet_kill 时内核挂起
【发布时间】:2015-10-11 11:50:11
【问题描述】:

我编写了简单的代码来测试tasklet的功能。

当我不做tasklet_kill时,在insmod命令使用后内核将被挂起。由于没有日志,我不知道会发生什么。

以下是我的代码。

void work_fcn(unsigned long a)
{
    printk("this is tasklet work function\n");
}

void tasklet_test(void)
{
    struct tasklet_struct task;
    tasklet_init(&task, work_fcn, 0);
    tasklet_schedule(&task);
    //if I don't do the following line, then kernel hang
    tasklet_kill(&task); 
}

static int __init hello_init(void)
{
     tasklet_test();
     return 0;
}

module_init(hello_init);

谢谢。

【问题讨论】:

  • Tasklets 是一个专门用于在下半部分(AKA 软 IRQ 模式)运行的小例程,您在这里显然没有。最好阅读可用的文档甚至源代码。除此之外,您正在堆栈上使用变量。想象一下当你离开函数时他们会发生什么。
  • OK~我去查一下相关文件。堆栈变量真的是一个错误!谢谢!我知道 tasklet 和 softirq 在下半部分运行。当我们有中断时,中断处理程序可能会使用 tasklet 来推迟一些工作。所以在这里我只是做同样的事情,推迟工作,这不应该有效吗?
  • 这是有效的,尽管(一些维护者/开发人员)更喜欢使用线程 IRQ 句柄机制。

标签: linux-kernel tasklet


【解决方案1】:
static void tasklet_action_common(struct softirq_action *a,
                  struct tasklet_head *tl_head,
                  unsigned int softirq_nr)
{
...

    while (list) {
        struct tasklet_struct *t = list;

        list = list->next;

        if (tasklet_trylock(t)) {
            if (!atomic_read(&t->count)) {
                if (!test_and_clear_bit(TASKLET_STATE_SCHED,
                            &t->state)) //<===========(1)
                    BUG();
                t->func(t->data);
                tasklet_unlock(t);
                continue;
            }
            tasklet_unlock(t);
        }

...
    }
}

在注释(1)中,它检查tasklet状态是否为TASKLET_STATE_SCHED,如果是,它会panic。

void tasklet_kill(struct tasklet_struct *t)
{
    if (in_interrupt())
        pr_notice("Attempt to kill tasklet from interrupt\n");

    while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
        do {
            yield();
        } while (test_bit(TASKLET_STATE_SCHED, &t->state));
    }
    tasklet_unlock_wait(t);
    clear_bit(TASKLET_STATE_SCHED, &t->state); //<=========(2)
}
EXPORT_SYMBOL(tasklet_kill);

在注释(2)中会清除TASKLET_STATE_SCHED位,不会panic。

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 1970-01-01
    • 2015-01-17
    • 2012-12-16
    • 2015-01-27
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    相关资源
    最近更新 更多