抢占模式下,ksoftirqd进程的优先级和调度一点思考 【2.6.36】

ksoftirqd 进程由kthread_create创建,继承了init_task的优先级,和调度策略

下面是进程创建过程
static int __cpuinit cpu_callback(struct notifier_block *nfb,
                                  unsigned long action,
                                  void *hcpu)
{
        int hotcpu = (unsigned long)hcpu;
        struct task_struct *p;

        switch (action) {
        case CPU_UP_PREPARE:
        case CPU_UP_PREPARE_FROZEN:
                p = kthread_create(run_ksoftirqd, hcpu, "ksoftirqd/%d", hotcpu);
                if (IS_ERR(p)) {
                        printk("ksoftirqd for %i failed\n", hotcpu);
                        return notifier_from_errno(PTR_ERR(p));
                }
                kthread_bind(p, hotcpu);
                  per_cpu(ksoftirqd, hotcpu) = p;

下面是init_task的优先级和调度策略。
#define INIT_TASK(tsk)        \
{                                                                        \
        .state                = 0,                                                \
        .stack                = &init_thread_info,                                \
        .usage                = ATOMIC_INIT(2),                                \
        .flags                = PF_KTHREAD,                                        \
        .lock_depth        = -1,                                                \
        .prio                = MAX_PRIO-20,                                        \
        .static_prio        = MAX_PRIO-20,                                        \
        .normal_prio        = MAX_PRIO-20,                                        \
        .policy                = SCHED_NORMAL,                                        \

以前有人常说软中断优先级比其他进程优先级高,看来有点扯淡,当软中断和系统调用互斥的时候,一般情况下是,软中断使用spin_lock, 系统调用使用spin_lock_bh, 但这样并不能防止软中断进程优先级比系统调用进程优先级低的条件下,死锁的发生, 所以软中断中关闭了抢占,来避免问题发生。

static int run_ksoftirqd(void * __bind_cpu)
{
        set_current_state(TASK_INTERRUPTIBLE);

        while (!kthread_should_stop()) {
                preempt_disable();
                if (!local_softirq_pending()) {
                        preempt_enable_no_resched();
                        schedule();
                        preempt_disable();
                }

                __set_current_state(TASK_RUNNING);

                while (local_softirq_pending()) {
                        /* Preempt disable stops cpu going offline.
                           If already offline, we'll be on wrong CPU:
                           don't process */
                        if (cpu_is_offline((long)__bind_cpu))
                                goto wait_to_die;
                        do_softirq();
                        preempt_enable_no_resched();
                        cond_resched();
                        preempt_disable();
                        rcu_note_context_switch((long)__bind_cpu);
                }
                preempt_enable();
                set_current_state(TASK_INTERRUPTIBLE);
        }
        __set_current_state(TASK_RUNNING);
        return 0;

wait_to_die:
        preempt_enable();
        /* Wait for kthread_stop */
        set_current_state(TASK_INTERRUPTIBLE);
        while (!kthread_should_stop()) {
                schedule();
                set_current_state(TASK_INTERRUPTIBLE);
        }
        __set_current_state(TASK_RUNNING);
        return 0;
--------

软中断进程优先级并不高:120,这样的话面对优先级高的进程时候,就可能不合适了,必要时还是要自己调高他的优先级。

作者: tuibo   发布时间: 2011-01-20

学习了,守护进程的优先权都很低吧

作者: amarant   发布时间: 2011-01-20