成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Linux內核調試技術——進程D狀態死鎖檢測

系統 Linux 系統運維
所謂的D狀態就是Linux的進程存在多種狀態,如TASK_RUNNING的運行態、EXIT_DEAD的停止態和TASK_INTERRUPTIBLE的接收信號的等待狀態等等(可在include/linux/sched.h中查看)。其中有一種狀態等待為TASK_UNINTERRUPTIBLE,稱為D狀態。

Linux的進程存在多種狀態,如TASK_RUNNING的運行態、EXIT_DEAD的停止態和TASK_INTERRUPTIBLE的接收信號的等待狀態等等(可在include/linux/sched.h中查看)。其中有一種狀態等待為TASK_UNINTERRUPTIBLE,稱為D狀態,該種狀態下進程不接收信號,只能通過wake_up喚醒。處于這種狀態的情況有很多,例如mutex鎖就可能會設置進程于該狀態,有時候進程在等待某種IO資源就緒時(wait_event機制)會設置進程進入該狀態。一般情況下,進程處于該狀態的時間不會太久,但若IO設備出現故障或者出現進程死鎖等情況,進程就可能長期處于該狀態而無法再返回到TASK_RUNNING態。因此,內核為了便于發現這類情況設計出了hung task機制專門用于檢測長期處于D狀態的進程并發出告警。本文分析內核hung task機制的源碼并給出一個示例演示。

一、hung task機制分析

內核在很早的版本中就已經引入了hung task機制,本文以較新的Linux 4.1.15版本源碼為例進行分析,代碼量并不多,源代碼文件為kernel/hung_task.c。

首先給出整體流程框圖和設計思想:

圖 D狀態死鎖流程圖

其核心思想為創建一個內核監測進程循環監測處于D狀態的每一個進程(任務),統計它們在兩次檢測之間的調度次數,如果發現有任務在兩次監測之間沒有發生任何的調度則可判斷該進程一直處于D狀態,很有可能已經死鎖,因此觸發報警日志打印,輸出進程的基本信息,棧回溯以及寄存器保存信息以供內核開發人員定位。

下面詳細分析實現方式:

  1. [cpp] view plain copy  在CODE上查看代碼片派生到我的代碼片 
  2. static int __init hung_task_init(void)   
  3. {   
  4.     atomic_notifier_chain_register(&panic_notifier_list, &panic_block);   
  5.     watchdog_task = kthread_run(watchdog, NULL"khungtaskd");   
  6.    
  7.     return 0;   
  8. }   
  9. subsys_initcall(hung_task_init);   

首先,若在內核配置中啟用了該機制,在內核的subsys初始化階段就會調用hung_task_init()函數啟用功能,首先向內核的panic_notifier_list通知鏈注冊回調:

  1. [cpp] view plain copy  在CODE上查看代碼片派生到我的代碼片 
  2. static struct notifier_block panic_block = {   
  3.     .notifier_call = hung_task_panic,   
  4. };   

在內核觸發panic時就會調用該hung_task_panic()函數,這個函數的作用稍后再看。繼續往下初始化,調用kthread_run()函數創建了一個名為khungtaskd的線程,執行watchdog()函數,立即嘗試調度執行。該線程就是專用于檢測D狀態死鎖進程的后臺內核線程。

  1. [cpp] view plain copy  在CODE上查看代碼片派生到我的代碼片 
  2. /*  
  3.  * kthread which checks for tasks stuck in D state  
  4.  */   
  5. static int watchdog(void *dummy)   
  6. {   
  7.     set_user_nice(current, 0);   
  8.    
  9.     for ( ; ; ) {   
  10.         unsigned long timeout = sysctl_hung_task_timeout_secs;   
  11.    
  12.         while (schedule_timeout_interruptible(timeout_jiffies(timeout)))   
  13.             timeout = sysctl_hung_task_timeout_secs;   
  14.    
  15.         if (atomic_xchg(&reset_hung_task, 0))   
  16.             continue;   
  17.    
  18.         check_hung_uninterruptible_tasks(timeout);   
  19.     }   
  20.    
  21.     return 0;   
  22. }   

本進程首先設置優先級為0,即一般優先級,不影響其他進程。然后進入主循環(每隔timeout時間執行一次),首先讓進程睡眠,設置的睡眠時間為

CONFIG_DEFAULT_HUNG_TASK_TIMEOUT,可以通過內核配置選項修改,默認值為120s,睡眠結束被喚醒后判斷原子變量標識reset_hung_task,若被置位則跳過本輪監測,同時會清除該標識。該標識通過reset_hung_task_detector()函數設置(目前內核中尚無其他程序使用該接口):

  1. [cpp] view plain copy  在CODE上查看代碼片派生到我的代碼片 
  2. void reset_hung_task_detector(void)   
  3. {   
  4.     atomic_set(&reset_hung_task, 1);   
  5. }   
  6. EXPORT_SYMBOL_GPL(reset_hung_task_detector);   

接下來循環的***即為監測函數check_hung_uninterruptible_tasks(),函數入參為監測超時時間。

  1. [cpp] view plain copy  在CODE上查看代碼片派生到我的代碼片 
  2. /*  
  3.  * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for  
  4.  * a really long time (120 seconds). If that happens, print out  
  5.  * a warning.  
  6.  */   
  7. static void check_hung_uninterruptible_tasks(unsigned long timeout)   
  8. {   
  9.     int max_count = sysctl_hung_task_check_count;   
  10.     int batch_count = HUNG_TASK_BATCHING;   
  11.     struct task_struct *g, *t;   
  12.    
  13.     /*  
  14.      * If the system crashed already then all bets are off,  
  15.      * do not report extra hung tasks:  
  16.      */   
  17.     if (test_taint(TAINT_DIE) || did_panic)   
  18.         return;   
  19.    
  20.     rcu_read_lock();   
  21.     for_each_process_thread(g, t) {   
  22.         if (!max_count--)   
  23.             goto unlock;   
  24.         if (!--batch_count) {   
  25.             batch_count = HUNG_TASK_BATCHING;   
  26.             if (!rcu_lock_break(g, t))   
  27.                 goto unlock;   
  28.         }   
  29.         /* use "==" to skip the TASK_KILLABLE tasks waiting on NFS */   
  30.         if (t->state == TASK_UNINTERRUPTIBLE)   
  31.             check_hung_task(t, timeout);   
  32.     }   
  33.  unlock:   
  34.     rcu_read_unlock();   
  35. }   

首先檢測內核是否已經DIE了或者已經panic了,如果是則表明內核已經crash了,無需再進行監測了,直接返回即可。注意這里的did_panic標識在前文中的panic通知鏈回調函數中hung_task_panic()置位:

  1. [cpp] view plain copy  在CODE上查看代碼片派生到我的代碼片 
  2. static int   
  3. hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)   
  4. {   
  5.     did_panic = 1;   
  6.    
  7.     return NOTIFY_DONE;   
  8. }   

接下去若尚無觸發內核crash,則進入監測流程并逐一檢測內核中的所有進程(任務task),該過程在RCU加鎖的狀態下進行,因此為了避免在進程較多的情況下加鎖時間過長,這里設置了一個batch_count,一次最多檢測HUNG_TASK_BATCHING個進程。于此同時用戶也可以設定***的檢測個數max_count=sysctl_hung_task_check_count,默認值為***PID個數PID_MAX_LIMIT(通過sysctl命令設置)。

函數調用for_each_process_thread()函數輪詢內核中的所有進程(任務task),僅對狀態處于TASK_UNINTERRUPTIBLE狀態的進程進行超時判斷,調用check_hung_task()函數,入參為task_struct結構和超時時間(120s):

  1. [cpp] view plain copy  在CODE上查看代碼片派生到我的代碼片 
  2. static void check_hung_task(struct task_struct *t, unsigned long timeout)   
  3. {   
  4.     unsigned long switch_count = t->nvcsw + t->nivcsw;   
  5.    
  6.     /*  
  7.      * Ensure the task is not frozen.  
  8.      * Also, skip vfork and any other user process that freezer should skip.  
  9.      */   
  10.     if (unlikely(t->flags & (PF_FROZEN | PF_FREEZER_SKIP)))   
  11.         return;   
  12.    
  13.     /*  
  14.      * When a freshly created task is scheduled once, changes its state to  
  15.      * TASK_UNINTERRUPTIBLE without having ever been switched out once, it  
  16.      * musn't be checked.  
  17.      */   
  18.     if (unlikely(!switch_count))   
  19.         return;   
  20.    
  21.     if (switch_count != t->last_switch_count) {   
  22.         t->last_switch_count = switch_count;   
  23.         return;   
  24.     }   
  25.    
  26.     trace_sched_process_hang(t);   
  27.    
  28.     if (!sysctl_hung_task_warnings)   
  29.         return;   
  30.    
  31.     if (sysctl_hung_task_warnings > 0)   
  32.         sysctl_hung_task_warnings--;   

首先通過t->nvcsw和t->nivcsw的計數累加表示進程從創建開始至今的調度次數總和,其中t->nvcsw表示進程主動放棄CPU的次數,t->nivcsw表示被強制搶占的次數。隨后函數判斷幾個標識:(1)如果進程被frozen了那就跳過檢測;(2)調度次數為0的不檢測。

接下來判斷從上一次檢測時保存的進程調度次數和本次是否相同,若不相同則表明這輪timeout(120s)時間內進程發生了調度,則更新該調度值返回,否則則表明該進程已經有timeout(120s)時間沒有得到調度了,一直處于D狀態。接下來的trace_sched_process_hang()暫不清楚作用,然后判斷sysctl_hung_task_warnings標識,它表示需要觸發報警的次數,用戶也可以通過sysctl命令配置,默認值為10,即若當前檢測的進程一直處于D狀態,默認情況下此處每2分鐘發出一次告警,一共發出10次,之后不再發出告警。下面來看告警代碼:

  1. [cpp] view plain copy  在CODE上查看代碼片派生到我的代碼片 
  2. /*  
  3.  * Ok, the task did not get scheduled for more than 2 minutes,  
  4.  * complain:  
  5.  */   
  6. pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",   
  7.     t->comm, t->pid, timeout);   
  8. pr_err("      %s %s %.*s\n",   
  9.     print_tainted(), init_utsname()->release,   
  10.     (int)strcspn(init_utsname()->version, " "),   
  11.     init_utsname()->version);   
  12. pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""   
  13.     " disables this message.\n");   
  14. sched_show_task(t);   
  15. debug_show_held_locks(t);   
  16.    
  17. touch_nmi_watchdog();   

這里會在控制臺和日志中打印死鎖任務的名稱、PID號、超時時間、內核tainted信息、sysinfo、內核棧barktrace以及寄存器信息等。如果開啟了debug lock則打印鎖占用的情況,并touch nmi_watchdog以防止nmi_watchdog超時(對于我的ARM環境無需考慮nmi_watchdog)。

  1. [cpp] view plain copy  在CODE上查看代碼片派生到我的代碼片 
  2. if (sysctl_hung_task_panic) {   
  3.     trigger_all_cpu_backtrace();   
  4.     panic("hung_task: blocked tasks");   
  5. }   

***如果設置了sysctl_hung_task_panic標識則直接觸發panic(該值可通過內核配置文件配置也可以通過sysctl設置)。

二、示例演示

演示環境:樹莓派b(Linux 4.1.15)

1、首先確認內核配置選項以確認開啟hung stak機制

  1. [cpp] view plain copy  在CODE上查看代碼片派生到我的代碼片 
  2. #include      
  3. #include      
  4. #include      
  5. #include    
  6.    
  7. DEFINE_MUTEX(dlock);   
  8.    
  9. static int __init dlock_init(void)   
  10. {   
  11.     mutex_lock(&dlock);   
  12.     mutex_lock(&dlock);    
  13.        
  14.     return 0;   
  15. }   
  16.    
  17. static void __exit dlock_exit(void)    
  18. {   
  19.     return;   
  20. }   
  21.    
  22. module_init(dlock_init);     
  23. module_exit(dlock_exit);     
  24. MODULE_LICENSE("GPL");     

本示例程序定義了一個mutex鎖,然后在模塊的init函數中重復加鎖,人為造成死鎖現象(mutex_lock()函數會調用__mutex_lock_slowpath()將進程設置為TASK_UNINTERRUPTIBLE狀態),進程進入D狀態后是無法退出的。可以通過ps命令來查看:

 

  1. root@apple:~# busybox ps  
  2. PID USER TIME COMMAND  
  3. ...... 
  4.  
  5. 521 root 0:00 insmod dlock.ko  
  6. ...... 

然后查看該進程的狀態,可見已經進入了D狀態。

 

  1. root@apple:~# cat /proc/521/status  
  2. Name: insmod  
  3. State: D (disk sleep)  
  4. Tgid: 521  
  5. Ngid: 0  
  6. Pid: 521 

至此在等待兩分鐘后調試串口就會輸出以下信息,可見每兩分鐘就會輸出一次:

 

  1. [ 360.625466] INFO: task insmod:521 blocked for more than 120 seconds.  
  2. [ 360.631878] Tainted: G O 4.1.15 #5  
  3. [ 360.637042] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.  
  4. [ 360.644986] [] (__schedule) from [] (schedule+0x40/0xa4)  
  5. [ 360.652129] [] (schedule) from [] (schedule_preempt_disabled+0x18/0x1c)  
  6. [ 360.660570] [] (schedule_preempt_disabled) from [] (__mutex_lock_slowpath+0x6c/0xe4)  
  7. [ 360.670142] [] (__mutex_lock_slowpath) from [] (mutex_lock+0x44/0x48)  
  8. [ 360.678432] [] (mutex_lock) from [] (dlock_init+0x20/0x2c [dlock])  
  9. [ 360.686480] [] (dlock_init [dlock]) from [] (do_one_initcall+0x90/0x1e8)  
  10. [ 360.694976] [] (do_one_initcall) from [] (do_init_module+0x6c/0x1c0)  
  11. [ 360.703170] [] (do_init_module) from [] (load_module+0x1690/0x1d34)  
  12. [ 360.711284] [] (load_module) from [] (SyS_init_module+0xdc/0x130)  
  13. [ 360.719239] [] (SyS_init_module) from [] (ret_fast_syscall+0x0/0x54)  
  14. [ 480.725351] INFO: task insmod:521 blocked for more than 120 seconds.  
  15. [ 480.731759] Tainted: G O 4.1.15 #5  
  16. [ 480.736917] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.  
  17. [ 480.744842] [] (__schedule) from [] (schedule+0x40/0xa4)  
  18. [ 480.752029] [] (schedule) from [] (schedule_preempt_disabled+0x18/0x1c)  
  19. [ 480.760479] [] (schedule_preempt_disabled) from [] (__mutex_lock_slowpath+0x6c/0xe4)  
  20. [ 480.770066] [] (__mutex_lock_slowpath) from [] (mutex_lock+0x44/0x48)  
  21. [ 480.778363] [] (mutex_lock) from [] (dlock_init+0x20/0x2c [dlock])  
  22. [ 480.786402] [] (dlock_init [dlock]) from [] (do_one_initcall+0x90/0x1e8)  
  23. [ 480.794897] [] (do_one_initcall) from [] (do_init_module+0x6c/0x1c0)  
  24. [ 480.803085] [] (do_init_module) from [] (load_module+0x1690/0x1d34)  
  25. [ 480.811188] [] (load_module) from [] (SyS_init_module+0xdc/0x130)  
  26. [ 480.819113] [] (SyS_init_module) from [] (ret_fast_syscall+0x0/0x54)  
  27. [ 600.825353] INFO: task insmod:521 blocked for more than 120 seconds.  
  28. [ 600.831759] Tainted: G O 4.1.15 #5  
  29. [ 600.836916] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.  
  30. [ 600.844865] [] (__schedule) from [] (schedule+0x40/0xa4)  
  31. [ 600.852005] [] (schedule) from [] (schedule_preempt_disabled+0x18/0x1c)  
  32. [ 600.860445] [] (schedule_preempt_disabled) from [] (__mutex_lock_slowpath+0x6c/0xe4)  
  33. [ 600.870014] [] (__mutex_lock_slowpath) from [] (mutex_lock+0x44/0x48)  
  34. [ 600.878303] [] (mutex_lock) from [] (dlock_init+0x20/0x2c [dlock])  
  35. [ 600.886339] [] (dlock_init [dlock]) from [] (do_one_initcall+0x90/0x1e8)  
  36. [ 600.894835] [] (do_one_initcall) from [] (do_init_module+0x6c/0x1c0)  
  37. [ 600.903023] [] (do_init_module) from [] (load_module+0x1690/0x1d34)  
  38. [ 600.911133] [] (load_module) from [] (SyS_init_module+0xdc/0x130)  
  39. [ 600.919059] [] (SyS_init_module) from [] (ret_fast_syscall+0x0/0x54) 

三、總結

D狀態死鎖一般在驅動開發的過程中比較常見,且不太容易定位,內核提供這種hung task機制,開發人員只需要將這些輸出的定位信息抓取并保留下來就可以快速的進行定位。

責任編輯:武曉燕 來源: chinaunix
相關推薦

2017-01-12 19:15:03

Linux內核調試自構proc

2010-03-02 10:27:56

Linux進程狀態

2010-01-22 11:01:04

linux內核模塊

2014-08-28 15:08:35

Linux內核

2021-04-15 05:51:25

Linux

2010-07-06 10:08:57

SQL Server

2011-01-14 14:49:05

2019-04-10 13:43:19

Linux內核進程負載

2022-02-08 15:15:26

OpenHarmonlinux鴻蒙

2021-07-11 06:45:18

Linux內核靜態

2009-12-25 11:22:13

Linux進程技術

2017-09-29 10:49:30

2023-04-10 09:44:22

內核鼠標調試鴻蒙

2010-06-02 09:31:43

Linux core

2012-05-14 14:09:53

Linux內核調度系統

2009-11-18 09:05:36

Oracle死鎖進程

2021-11-15 04:00:07

Linux 內核動態

2021-11-14 07:29:55

Linux 內核靜態追蹤Linux 系統

2023-02-28 09:47:42

2022-03-03 18:18:53

BPF解釋器系統
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: av黄色在线 | 一区二区三区av | 国产一区二区三区久久久久久久久 | 国产日韩欧美一区二区 | 亚洲精品一区二区在线观看 | 天天综合网天天综合色 | 精品免费视频 | 91免费版在线观看 | 日韩国产中文字幕 | 91精品国产91久久久久久三级 | 久久精品欧美一区二区三区不卡 | 午夜国产一级片 | 欧美精品一二三区 | 99久久精品一区二区毛片吞精 | 欧美精品一区二区三区在线 | 久久九九影视 | 一区二区三区视频 | jizz亚洲人| 色综合色综合网色综合 | 黄色av网站在线免费观看 | 五月婷婷在线视频 | 国产福利视频网站 | 欧美激情精品久久久久久免费 | 日韩国产在线 | 久久久精品网站 | 中文字幕人成人 | 一级片免费网站 | av黄色在线播放 | 成人亚洲视频 | 精品国产乱码久久久 | 日韩三 | 最新国产精品 | 久久久久亚洲 | 欧美日韩一区在线播放 | 夜夜草 | 免费观看av | 成人一区二区三区在线观看 | 91精品国产一区二区在线观看 | 在线午夜 | 国产精品久久久亚洲 | 国内精品视频一区二区三区 |