如果CPU可以響應中斷,但是在長時間內不能調度(比如禁止搶占時間太長),此時就需要一種機制(softlockup)來檢測這種情形。
本文基於3.14.25
記錄下第二種比較嚴重的“死鎖”:禁止搶占的時間太長,此時依舊可以相應中斷,但是本CPU上在長時間內沒有發生調度。
檢測機制:softlockup
reset_init |-->kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND); kernel_init |-->kernel_init_freeable(); |-->lockup_detector_init(); |-->watchdog_enable_all_cpus(false); |-->smpboot_register_percpu_thread(&watchdog_threads);
static struct smp_hotplug_thread watchdog_threads = { .store = &softlockup_watchdog, .thread_should_run = watchdog_should_run, .thread_fn = watchdog, .thread_comm = "watchdog/%u", .setup = watchdog_enable, }; smpboot_register_percpu_thread(&watchdog_threads) |-->__smpboot_create_thread(&watchdog_threads, cpu);
165 static int __smpboot_create_thread(struct smp_hotplug_thread *ht, unsigned int cpu) 166 { 167 struct task_struct *tsk = *per_cpu_ptr(ht->store, cpu); 168 struct smpboot_thread_data *td; 169 +-- 4 lines: if (tsk)--- 173 td = kzalloc_node(sizeof(*td), GFP_KERNEL, cpu_to_node(cpu)); 174 +--- 2 lines: if (!td)--- 176 td->cpu = cpu; 177 td->ht = ht; 178 +-- 8 lines: tsk = kthread_create_on_cpu(smpboot_thread_fn, td, cpu,---- 186 *per_cpu_ptr(ht->store, cpu) = tsk; 187 +-- 12 lines: if (ht->create) {--- 199 return 0; 200 }
創建了一個線程A task,該線程的主函數為 smpboot_thread_fun,該線程存儲於watchdog_threads.store中 過程(閱讀smpboot_thread_fn): 1、調用setup:watchdog_enable; 2、調用thread_should_run:watchdog_should_run判定是否需要運行線程主函數 3、線程主函數thread_fn:watchdog watchdog_enable |-->struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer); |-->hrtimer->function = watchdog_timer_fn; |-->hrtimer_start(hrtimer, ns_to_ktime(sample_period), HRTIMER_MODE_REL_PINNED); |-->watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);//實時優先級 這個函數比較重要,因為: A task中主函數為smpboot_thread_fun,其中將會檢測是否運行thread_fn:watchdog watchdog_should_run |-->return __this_cpu_read(hrtimer_interrupts) != __this_cpu_read(soft_lockup_hrtimer_cnt); 因此如果hrtimer_interrupts == soft_lockup_hrtimer_cnt成立則運行調度器(放棄CPU使用權),否則運行thread_fn:watchdog 我們看下thread_fn的職責: watchdog: |-->__this_cpu_write(soft_lockup_hrtimer_cnt, __this_cpu_read(hrtimer_interrupts)); |--> __this_cpu_write(watchdog_touch_ts, get_timestamp()); 再看下hrtimer定時的職責: static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer) |-->unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts); |-->__this_cpu_inc(hrtimer_interrupts); 記錄hrtimer中斷次數 |-->wake_up_process(__this_cpu_read(softlockup_watchdog));//喚醒創建的 A task,雖然被喚醒,但並不意味着就能得到調度 |-->hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period)); |-->duration = is_softlockup(touch_ts);//判定當前時間與touch_ts的時間差是否超過20s, | 如果超過,說明發生了softlockup,否則說明沒有發生。
總結:更新watchdog_touch_ts的是一個具有實時優先級的線程A task,既然是線程就存在是否得到調度的問題。如果在20s內,該線程沒有得到調度,則說
明發生了softlockup。因為按照原理來說,實時優先級線程A task一定比普通優先級線程先得到調度。如果一個線程B禁止調度長達20s,則無論如何
wake_up_process,A task都是不會被調度到的,即watchdog_touch_ts是不會得到更新的。而softlockup的檢測是放在一個hrtimer處理函數中的。關於該函數是在中斷上半部運行還是下半部運行,我還不清楚,但是由於其在中斷上下文運行,想必也是具有高”優先級“的。
至於hrtimer的運行周期為:watchdog_thresh * 2 / 5 = 4s,我認為只是加大了喚醒A task的次數(wake_up_process),畢竟在20s嘗試4次喚醒A task時,只要A task有一次得到調度的機會就可以更新 wathchdog_touch_ts,也就不會判定發生softlockup.