{"id":6855,"date":"2024-07-25T22:01:01","date_gmt":"2024-07-25T14:01:01","guid":{"rendered":""},"modified":"2024-07-25T22:01:01","modified_gmt":"2024-07-25T14:01:01","slug":"linux\u8fdb\u7a0b\u8c03\u5ea6\u91c7\u7528\u7684\u662f\u4ec0\u4e48\u8c03\u5ea6\u65b9\u5f0f_linux\u7ebf\u7a0b\u8c03\u5ea6\u7b56\u7565","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6855.html","title":{"rendered":"linux\u8fdb\u7a0b\u8c03\u5ea6\u91c7\u7528\u7684\u662f\u4ec0\u4e48\u8c03\u5ea6\u65b9\u5f0f_linux\u7ebf\u7a0b\u8c03\u5ea6\u7b56\u7565"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u524d\u8a00<\/h2>\n

\u672c\u7bc7\u6587\u7ae0\u662f\u8fdb\u7a0b\u7ba1\u7406\u4e0e\u8c03\u5ea6\u7684\u7b2c\u4e00\u7bc7\u6587\u7ae0\uff0c\u8fdb\u7a0b\u7ba1\u7406\u4e0e\u8c03\u5ea6\u5185\u5bb9\u8f83\u591a\uff0c\u540e\u9762\u4f1a\u9646\u7eed\u66f4\u65b0\uff0c\u4eca\u5929\u4e3b\u8981\u662f\u8bb2\u8fdb\u7a0b\uff08\u5bf9\u4e8e\u5185\u6838\u6765\u8bf4\uff0c\u53eb\u4efb\u52a1 task \u66f4\u597d\u4e00\u70b9\uff0c\u4e0b\u6587\u6211\u90fd\u7528\u4efb\u52a1\u6765\u4ee3\u66ff\u8fdb\u7a0b\uff09\u8c03\u5ea6\u65f6\u8981\u505a\u54ea\u4e00\u4e9b\u4e8b\u60c5\uff0c\u4e5f\u5c31\u662fschedule\u51fd\u6570\u3002<\/p>\n

schedule\u51fd\u6570\u662fLinux\u5185\u6838\u4e2d\u7684\u4e3b\u8c03\u5ea6\u5668\uff0c\u9664\u6b64\u4e4b\u5916\u8fd8\u6709\u5468\u671f\u6027\u8c03\u5ea6\u5668\u3002<\/p>\n

Linux\u5185\u6838\u7248\u672c\uff1a4.10.0
\u786c\u4ef6\u5e73\u53f0\uff1aIntel X86_64<\/p>\n

\u8003\u8651\u5230\u6587\u7ae0\u7bc7\u5e45\uff0c\u5728\u8fd9\u91cc\u6211\u53ea\u8ba8\u8bba\u666e\u901a\u8fdb\u7a0b\uff0c\u5176\u8c03\u5ea6\u7b97\u6cd5\u91c7\u7528\u7684\u662fCFS\uff08\u5b8c\u5168\u516c\u5e73\uff09\u8c03\u5ea6\u7b97\u6cd5\u3002
\u81f3\u4e8eCFS\u8c03\u5ea6\u7b97\u6cd5\u7684\u5b9e\u73b0\u540e\u9762\u540e\u4e13\u95e8\u5199\u4e00\u7bc7\u6587\u7ae0\uff0c\u8fd9\u91cc\u53ea\u8981\u8bb0\u4f4f\u8c03\u5ea6\u65f6\u9009\u62e9\u4e00\u4e2a\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u4efb\u52a1\u6267\u884c<\/strong><\/p>\n

\u4e00\u3001\u8c03\u5ea6\u5355\u4f4d\u7b80\u4ecb<\/h2>\n

1.1 task_struct \u7ed3\u6784\u4f53\u7b80\u4ecb<\/h3>\n

\u5bf9\u4e8eLinux\u5185\u6838\u6765\u8bf4\uff0c\u8c03\u5ea6\u7684\u57fa\u672c\u5355\u4f4d\u662f\u4efb\u52a1\uff0c\u7528 struct task_struct \u8868\u793a\uff0c\u5b9a\u4e49\u5728include\/linux\/sched.h\u6587\u4ef6\u4e2d\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u5305\u542b\u4e00\u4e2a\u4efb\u52a1\u7684\u6240\u6709\u4fe1\u606f\uff0c\u7ed3\u6784\u4f53\u6210\u5458\u5f88\u591a\uff0c\u5728\u8fd9\u91cc\u6211\u53ea\u5217\u51fa\u4e0e\u672c\u7ae0\u5185\u5bb9\u6709\u5173\u7684\u6210\u5458\uff1a<\/p>\n

struct<\/span> task_struct<\/span> { \n   <\/span> .<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span> (<\/span>1<\/span>)<\/span> volatile<\/span> long<\/span> state;<\/span> (<\/span>2<\/span>)<\/span> const<\/span> struct<\/span> sched_class<\/span> *<\/span>sched_class;<\/span> (<\/span>3<\/span>)<\/span> void<\/span> *<\/span>stack;<\/span> struct<\/span> thread_struct<\/span> thread;<\/span> struct<\/span> mm_struct<\/span> *<\/span>mm,<\/span> *<\/span>active_mm;<\/span> .<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span> }<\/span> <\/code><\/pre>\n

\uff081\uff09state \uff1a\u8868\u793a\u4efb\u52a1\u5f53\u524d\u7684\u72b6\u6001\uff0c\u5f53state\u4e3aTASK_RUNNING\u65f6\uff0c\u8868\u793a\u4efb\u52a1\u5904\u4e8e\u53ef\u8fd0\u884c\u7684\u72b6\u6001\uff0c\u5e76\u4e0d\u4e00\u5b9a\u8868\u793a\u76ee\u524d\u6b63\u5728\u5360\u6709CPU\uff0c\u4e5f\u8bb8\u5728\u7b49\u5f85\u8c03\u5ea6\uff0c\u8c03\u5ea6\u5668\u53ea\u4f1a\u9009\u62e9\u5728\u8be5\u72b6\u6001\u4e0b\u7684\u4efb\u52a1\u8fdb\u884c\u8c03\u5ea6\u3002\u8be5\u72b6\u6001\u786e\u4fdd\u4efb\u52a1\u53ef\u4ee5\u7acb\u5373\u8fd0\u884c\uff0c\u800c\u4e0d\u9700\u8981\u7b49\u5f85\u5916\u90e8\u4e8b\u4ef6\u3002
\u7b80\u5355\u6765\u8bf4\u5c31\u662f\uff1a\u4efb\u52a1\u8c03\u5ea6\u7684\u5bf9\u8c61\u662f\u5904\u4e8eTASK_RUNNING\u72b6\u6001\u7684\u4efb\u52a1\u3002
\u5904\u4e8eTASK_RUNNING\u72b6\u6001\u7684\u4efb\u52a1\uff0c\u53ef\u80fd\u6b63\u5728\u6267\u884c\u7528\u6237\u6001\u4ee3\u7801\uff0c\u4e5f\u53ef\u80fd\u6b63\u5728\u6267\u884c\u5185\u6838\u6001\u7684\u4ee3\u7801\u3002<\/p>\n

\uff082\uff09sched_class \uff1a\u8868\u793a\u4efb\u52a1\u6240\u5c5e\u7684\u8c03\u5ea6\u5668\u7c7b\uff0c\u6211\u4eec\u8fd9\u91cc\u53ea\u8bb2CFS\u8c03\u5ea6\u7c7b\u3002<\/p>\n

\/\/ kernel\/sched\/sched.h<\/span> struct<\/span> sched_class<\/span> { \n   <\/span> .<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span> \/\/\u5c06\u4efb\u52a1\u52a0\u5165\u53ef\u8fd0\u884c\u7684\u961f\u5217\u4e2d<\/span> void<\/span> (<\/span>*<\/span>enqueue_task)<\/span> (<\/span>struct<\/span> rq<\/span> *<\/span>rq,<\/span> struct<\/span> task_struct<\/span> *<\/span>p,<\/span> int<\/span> flags)<\/span>;<\/span> \/\/\u5c06\u4efb\u52a1\u79fb\u9664\u53ef\u8fd0\u884c\u7684\u961f\u5217\u4e2d<\/span> void<\/span> (<\/span>*<\/span>dequeue_task)<\/span> (<\/span>struct<\/span> rq<\/span> *<\/span>rq,<\/span> struct<\/span> task_struct<\/span> *<\/span>p,<\/span> int<\/span> flags)<\/span>;<\/span> \/\/\u9009\u62e9\u4e00\u4e0b\u5c06\u8981\u8fd0\u884c\u7684\u4efb\u52a1<\/span> struct<\/span> task_struct<\/span> *<\/span> (<\/span>*<\/span>pick_next_task)<\/span> (<\/span>struct<\/span> rq<\/span> *<\/span>rq,<\/span> struct<\/span> task_struct<\/span> *<\/span>prev,<\/span> struct<\/span> pin_cookie<\/span> cookie)<\/span>;<\/span> .<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span> }<\/span> extern<\/span> const<\/span> struct<\/span> sched_class<\/span> fair_sched_class;<\/span> <\/code><\/pre>\n
\/\/ kernel\/sched\/fair.c<\/span> const<\/span> struct<\/span> sched_class<\/span> fair_sched_class;<\/span> \/* * All the scheduling class methods: *\/<\/span> const<\/span> struct<\/span> sched_class<\/span> fair_sched_class =<\/span> { \n   <\/span> .<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span> .<\/span>enqueue_task =<\/span> enqueue_task_fair,<\/span> \/\/CFS \u7684 enqueue_task \u5b9e\u4f8b<\/span> .<\/span>dequeue_task =<\/span> dequeue_task_fair,<\/span> \/\/CFS \u7684 dequeue_task \u5b9e\u4f8b<\/span> .<\/span>pick_next_task =<\/span> pick_next_task_fair,<\/span> \/\/CFS \u7684 pick_next_task \u5b9e\u4f8b<\/span> .<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span> }<\/span> <\/code><\/pre>\n

\uff083\uff09\u4efb\u52a1\u4e0a\u4e0b\u6587\uff1a\u8868\u793a\u4efb\u52a1\u8c03\u5ea6\u65f6\u8981\u5207\u6362\u7684\u4efb\u52a1\u4e0a\u4e0b\u6587\uff08\u4efb\u52a1\u5207\u6362\u53ea\u53d1\u751f\u5728\u5185\u6838\u6001\uff09\u3002
stack\uff1a\u5f53\u524d\u4efb\u52a1\u7684\u5185\u6838\u6001\u5806\u6808\uff08\u7528\u6237\u6001\u7684sp\uff0c\u7528\u6237\u6001\u7684ip\uff0c\u5728\u5185\u6838\u6808\u9876\u90e8\u7684 pt_regs \u7ed3\u6784\u91cc\u9762\uff09
thread \uff1a\u4e5f\u53eb\u4efb\u52a1\u7684\u786c\u4ef6\u4e0a\u4e0b\u6587\uff0c\u4e3b\u8981\u5305\u542b\u4e86\u5927\u90e8\u5206CPU\u5bc4\u5b58\u5668\uff08\u5982\uff1a\u5185\u6838\u6808sp\uff09\u3002
struct mm_struct *mm \uff1a\u5207\u6362\u6bcf\u4e2a\u4efb\u52a1\u7528\u6237\u6001\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff08\u6bcf\u4e2a\u4efb\u52a1\u7684\u7528\u6237\u6808\u90fd\u662f\u72ec\u7acb\u7684\uff0c\u90fd\u5728\u5185\u5b58\u7a7a\u95f4\u91cc\u9762\uff0c\u5207\u6362\u4efb\u52a1\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u4e5f\u5c31\u5207\u6362\u4e86\u4efb\u52a1\u7684\u7528\u6237\u6808\uff09\u3002<\/p>\n

1.2 task_struct \u7ed3\u6784\u4f53\u7684\u4ea7\u751f<\/h3>\n

\u4efb\u52a1\uff08task_struct) \u7684\u6765\u6e90\u6709\u4e09\u5904\uff1a
\uff081\uff09 fork()\uff1a\u8be5\u51fd\u6570\u662f\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\uff0c\u53ef\u4ee5\u590d\u5236\u4e00\u4e2a\u73b0\u6709\u7684\u8fdb\u7a0b\u6765\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684\u8fdb\u7a0b\uff0c\u4ea7\u751f\u4e00\u4e2a task_struct\uff0c\u7136\u540e\u8c03\u7528wake_up_new_task()\u5524\u9192\u65b0\u7684\u8fdb\u7a0b\uff0c\u4f7f\u5176\u8fdb\u5165TASK_RUNNING\u72b6\u6001\u3002
\uff082\uff09 pthread_create()\uff1a\u8be5\u51fd\u6570\u662fGlibc\u4e2d\u7684\u51fd\u6570\uff0c\u7136\u540e\u8c03\u7528clone()\u7cfb\u7edf\u8c03\u7528\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\uff08\u53c8\u53eb\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\uff09\uff0c\u4ea7\u751f\u4e00\u4e2a task_struct\uff0c\u7136\u540e\u8c03\u7528wake_up_new_task()\u5524\u9192\u65b0\u7684\u7ebf\u7a0b\uff0c\u4f7f\u5176\u8fdb\u5165TASK_RUNNING\u72b6\u6001\u3002
\uff083\uff09kthread_create()\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5185\u6838\u7ebf\u7a0b\uff0c\u4ea7\u751f\u4e00\u4e2a task_struct\uff0c\u7136\u540ewake_up_new_task()\uff0c\u5524\u9192\u65b0\u7684\u5185\u6838\u7ebf\u7a0b\uff0c\u4f7f\u5176\u8fdb\u5165TASK_RUNNING\u72b6\u6001\u3002<\/p>\n

\u5176\u5b9e\u8fd9\u4e09\u4e2aAPI\u6700\u540e\u90fd\u4f1a\u8c03\u7528 _do_fork()\uff0c\u4e0d\u540c\u4e4b\u5904\u662f\u4f20\u5165\u7ed9 _do_fork() \u7684\u53c2\u6570\u4e0d\u540c\uff08clone_flags\uff09\uff0c\u6700\u7ec8\u7ed3\u679c\u5c31\u662f\u8fdb\u7a0b\u6709\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\u548c\u6808\uff0c\u800c\u7528\u6237\u7ebf\u7a0b\u53ef\u4ee5\u81ea\u5df1\u6307\u5b9a\u7528\u6237\u6808\uff0c\u5730\u5740\u7a7a\u95f4\u548c\u7236\u8fdb\u7a0b\u5171\u4eab\uff0c\u5185\u6838\u7ebf\u7a0b\u5219\u53ea\u6709\u548c\u5185\u6838\u5171\u4eab\u7684\u540c\u4e00\u4e2a\u6808\uff0c\u540c\u4e00\u4e2a\u5730\u5740\u7a7a\u95f4\u3002\u56e0\u6b64\u4e0a\u8ff0\u4e09\u4e2aAPI\u6700\u7ec8\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2atask_struct\u7ed3\u6784\u3002<\/p>\n

\u5907\u6ce8\uff1a\u8fd9\u91cc\u6ca1\u6709\u8ba8\u8bbavfok()\u3002<\/p>\n

\u603b\u7ed3\uff1a\u4e0a\u8ff0\u4e09\u4e2a\u65b9\u5f0f\u90fd\u4ea7\u751f\u4e86\u4e00\u4e2a\u4efb\u52a1 task_struct\uff0c\u7136\u540e\u5524\u9192\u8be5\u4efb\u52a1\u4f7f\u5176\u5904\u4e8eTASK_RUNNING\u72b6\u6001\uff0c\u7136\u540e\u8fd9\u6837\u8c03\u5ea6\u5668\u5c31\u53ef\u4ee5\u8c03\u5ea6 \u4efb\u52a1\uff08task_struct)\u4e86\u3002<\/p>\n

\u5173\u4e8e\u4e0a\u8ff0\u4e09\u8005\u7684\u533a\u522b\u4e5f\u5c31\u662f\u8fdb\u7a0b\uff0c\u7ebf\u7a0b\uff08\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\uff09\uff0c\u5185\u6838\u7ebf\u7a0b\u7684\u533a\u522b\u6211\u4f1a\u5728\u4e0b\u4e00\u7bc7\u6587\u7ae0\u4e2d\u8fdb\u884c\u89e3\u91ca\uff0c\u8fd9\u4e00\u90e8\u5206\u5185\u5bb9\u5f88\u591a\uff0c\u5728\u8fd9\u91cc\u89e3\u91ca\u5c31\u5360\u7528\u4e86\u592a\u591a\u7bc7\u5e45\u3002<\/strong><\/p>\n

\u8fd8\u6709\u4e00\u4e2a\u6765\u6e90\u5c31\u662f0\u53f7\u8fdb\u7a0b\uff08\u53c8\u53eb idle \u8fdb\u7a0b\uff09\uff0c\u6bcf\u4e2a\u903b\u8f91\u5904\u7406\u5668\u4e0a\u90fd\u6709\u4e00\u4e2a\uff0c\u5c5e\u4e8e\u5185\u6838\u6001\u7ebf\u7a0b\uff0c\u53ea\u6709\u5728\u6ca1\u6709\u5176\u4ed6\u7684\u4efb\u52a1\u5904\u4e8eTASK_RUNNING\u72b6\u6001\u65f6\uff08\u7cfb\u7edf\u6b64\u65f6\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001\uff09\uff0c\u4efb\u52a1\u8c03\u5ea6\u5668\u624d\u4f1a\u9009\u62e90\u53f7\u8fdb\u7a0b\uff0c\u7136\u540e\u91cd\u590d\u6267\u884c HLT \u6307\u4ee4\u3002
HLT \u6307\u4ee4 \uff1a\u505c\u6b62\u6307\u4ee4\u6267\u884c\uff0c\u5e76\u5c06\u5904\u7406\u5668\u7f6e\u4e8eHALT\u72b6\u6001\u3002\u7b80\u5355\u6765\u8bf4\u8ba9\u8be5CPU\u8fdb\u5165\u4f11\u7720\u72b6\u6001\uff0c\u4f4e\u529f\u8017\u72b6\u6001\u3002
\uff08\u8be5\u6307\u4ee4\u53ea\u80fd\u5728 privilege level 0\u6267\u884c\uff0c\u4e14CPU\u6307\u7684\u662f\u903b\u8f91CPU\u800c\u4e0d\u662f\u7269\u7406CPU\uff0c\u6bcf\u4e2a\u903b\u8f91CPU\u90fd\u6709\u4e00\u4e2aidle\u8fdb\u7a0b\uff09\u3002
\"linux\u8fdb\u7a0b\u8c03\u5ea6\u91c7\u7528\u7684\u662f\u4ec0\u4e48\u8c03\u5ea6\u65b9\u5f0f_linux\u7ebf\u7a0b\u8c03\u5ea6\u7b56\u7565<\/p>\n

1.3 struct rq \u7ed3\u6784\u4f53<\/h3>\n

\u76ee\u524d\u7684x86_64\u90fd\u6709\u591a\u4e2a\u5904\u7406\u5668\uff0c\u90a3\u4e48\u5bf9\u4e8e\u6240\u6709\u5904\u4e8eTASK_RUNNING\u72b6\u6001\u7684\u4efb\u52a1\u662f\u5e94\u8be5\u4f4d\u4e8e\u4e00\u4e2a\u961f\u5217\u8fd8\u6709\u6bcf\u4e2a\u5904\u7406\u5668\u90fd\u6709\u81ea\u5df1\u7684\u961f\u5217\uff1f
Linux\u91c7\u7528\u7684\u662f\u6bcf\u4e2aCPU\u90fd\u6709\u81ea\u5df1\u7684\u8fd0\u884c\u961f\u5217\uff0c\u8fd9\u6837\u505a\u7684\u597d\u5904\uff1a
\uff081\uff09\u6bcf\u4e2aCPU\u5728\u81ea\u5df1\u7684\u8fd0\u884c\u961f\u5217\u4e0a\u9009\u62e9\u4efb\u52a1\u964d\u4f4e\u4e86\u7ade\u4e89
\uff082\uff09\u67d0\u4e2a\u4efb\u52a1\u4f4d\u4e8e\u4e00\u4e2aCPU\u7684\u8fd0\u884c\u961f\u5217\u4e0a\uff0c\u7ecf\u8fc7\u591a\u6b21\u8c03\u5ea6\u540e\uff0c\u5185\u6838\u8d8b\u4e8e\u9009\u62e9\u76f8\u540c\u7684CPU\u6267\u884c\u8be5\u4efb\u52a1\uff0c\u90a3\u4e48\u4e0a\u6b21\u4efb\u52a1\u8fd0\u884c\u7684\u53d8\u91cf\u5f88\u53ef\u80fd\u4ecd\u7136\u5728\u8fd9\u4e2aCPU\u7f13\u5b58\u4e0a\uff0c\u63d0\u9ad8\u8fd0\u884c\u6548\u7387\u3002<\/p>\n

\u5728\u8fd9\u91cc\u6211\u53ea\u8ba8\u8bba\u666e\u901a\u4efb\u52a1\u7684\u8c03\u5ea6\uff0c\u56e0\u4e3alinux\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u90fd\u662f\u5728\u8fd0\u884c\u666e\u901a\u4efb\u52a1\uff0c\u666e\u901a\u4efb\u52a1\u9009\u62e9\u7684\u8c03\u5ea6\u5668\u662fCFS\u5b8c\u5168\u8c03\u5ea6\u3002<\/p>\n

\u5728\u8c03\u5ea6\u65f6\uff0c\u8c03\u5ea6\u5668\u53bb CFS \u8fd0\u884c\u961f\u5217\u627e\u662f\u5426\u6709\u4efb\u52a1\u9700\u8981\u8fd0\u884c\u3002<\/p>\n

DEFINE_PER_CPU_SHARED_ALIGNED<\/span>(<\/span>struct<\/span> rq<\/span>,<\/span> runqueues)<\/span>;<\/span> <\/code><\/pre>\n
struct<\/span> rq<\/span> { \n   <\/span> .<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span> unsigned<\/span> int<\/span> nr_running;<\/span> \/\/\u8fd0\u884c\u961f\u5217\u4e0a\u53ef\u8fd0\u884c\u4efb\u52a1\u7684\u4e2a\u6570<\/span> struct<\/span> cfs_rq<\/span> cfs;<\/span> \/\/ CFS \u8fd0\u884c\u961f\u5217<\/span> struct<\/span> task_struct<\/span> *<\/span>curr,<\/span> \/\/\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u4efb\u52a1\u7684 task_struct \u5b9e\u4f8b<\/span> struct<\/span> task_struct<\/span> *<\/span>idle,<\/span> \/\/\u6307\u5411idle\u4efb\u52a1\u7684\u5b9e\u4f8b\uff0c\u5728\u6ca1\u6709\u5176\u5b83\u53ef\u8fd0\u884c\u4efb\u52a1\u7684\u65f6\u5019\u6267\u884c<\/span> .<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span> }<\/span> <\/code><\/pre>\n

\u4e8c\u3001schedule\u51fd\u6570\u8be6\u89e3<\/h2>\n

2.1 schedule\u51fd\u6570\u7b80\u4ecb<\/h3>\n

\u4e0a\u6587\u8bf4\u5230\u4efb\u52a1\u8c03\u5ea6\u5668\u662f\u5bf9\u4e8e\u53ef\u8fd0\u884c\u72b6\u6001\uff08TASK_RUNNING\uff09\u7684\u4efb\u52a1\u8fdb\u884c\u8c03\u5ea6\uff0c\u5982\u679c\u4efb\u52a1\u7684\u72b6\u6001\u4e0d\u662fTASK_RUNNING\uff0c\u90a3\u4e48\u8be5\u4efb\u52a1\u662f\u4e0d\u4f1a\u88ab\u8c03\u5ea6\u7684\u3002<\/p>\n

\u8c03\u5ea6\u7684\u5165\u53e3\u70b9\u662fschedule()\u51fd\u6570\uff0c\u5b9a\u4e49\u5728 kernel\/sched\/core.c \u6587\u4ef6\u4e2d\uff0c\u8fd9\u91cc\u5220\u6389\u4e86\u5f88\u591a\u4ee3\u7801\uff0c\u53ea\u7559\u4e86\u91cd\u8981\u7684\u4ee3\u7801\uff1a<\/p>\n

asmlinkage __visible void<\/span> __sched schedule<\/span>(<\/span>void<\/span>)<\/span> { \n   <\/span> .<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span> preempt_disable<\/span>(<\/span>)<\/span>;<\/span> \/\/\u7981\u6b62\u5185\u6838\u62a2\u5360<\/span> __schedule<\/span>(<\/span>false)<\/span>;<\/span> \/\/\u4efb\u52a1\u5f00\u59cb\u8c03\u5ea6\uff0c\u8c03\u5ea6\u7684\u8fc7\u7a0b\u4e2d\u7981\u6b62\u5176\u5b83\u4efb\u52a1\u62a2\u5360<\/span> sched_preempt_enable_no_resched<\/span>(<\/span>)<\/span>;<\/span> \/\/\u5f00\u542f\u5185\u6838\u62a2\u5360<\/span> .<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span> }<\/span> <\/code><\/pre>\n

\u8fd9\u91cc\u6ce8\u610fschedule\u8c03\u7528__schedule\u51fd\u6570\u662f\u4f20\u5165\u7684\u662ffalse\u53c2\u6570\uff0c\u8868\u793aschedule\u51fd\u6570\u4e3b\u8c03\u5ea6\u5668\u3002
\u8c03\u5ea6\u5206\u4e3a\u4e3b\u52a8\u8c03\u5ea6\u548c\u62a2\u5360\u8c03\u5ea6\u3002<\/p>\n

__schedule\u7684\u53c2\u6570preempt\u662fbool\u7c7b\u578b\uff0c\u8868\u793a\u672c\u6b21\u8c03\u5ea6\u662f\u5426\u4e3a\u62a2\u5360\u8c03\u5ea6\uff1a<\/p>\n

__schedule\u7684\u53c2\u6570preempt\u7b49\u4e8e0\u8868\u793a\u4e0d\u662f\u62a2\u5360\u8c03\u5ea6\uff0c\u5373\u4e3b\u52a8\u8c03\u5ea6\uff0c\u4ee3\u8868\u6b64\u6b21\u8c03\u5ea6\u662f\u8be5\u8fdb\u7a0b\u4e3b\u52a8\u8bf7\u6c42\u8c03\u5ea6\uff0c\u4e3b\u52a8\u8c03\u7528\u4e86schedule\u51fd\u6570\uff08\u4efb\u52a1\u4e3b\u52a8\u8ba9\u51fa\u5904\u7406\u5668\uff09\uff0c\u6bd4\u5982\u8be5\u8fdb\u7a0b\u8fdb\u5165\u4e86\u963b\u585e\u6001\u3002
\u800cschedule\u51fd\u6570\u53c2\u6570\u56fa\u5b9a\u4f20\u5165\u7684\u53c2\u6570\u662ffalse\uff0c\u4e5f\u5c31\u662f0\uff0c\u5c31\u662f\u8c03\u7528schedule\u51fd\u6570\u5c31\u662f\u4e3b\u52a8\u53d1\u8d77\u8c03\u5ea6\uff0c\u4e0d\u662f\u62a2\u5360\u8c03\u5ea6\uff0c\u56e0\u6b64schedule\u51fd\u6570\u79f0\u4e3a\u4e3b\u8c03\u5ea6\u5668\u3002<\/p>\n

\u76f4\u63a5\u8c03\u7528\u4e3b\u8c03\u5ea6\u5668schdule\u51fd\u6570\u7684\u573a\u666f\u67093\u79cd\uff1a
\uff081\uff09\u5f53\u524d\u8fdb\u7a0b\u9700\u8981\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\u624d\u80fd\u7ee7\u7eed\u8fd0\u884c\u65f6\uff0c\u8c03\u7528\u4e00\u4e2await_event()\u7c7b\u51fd\u6570\u5c06\u81ea\u5df1\u7684\u72b6\u6001\u8bbe\u4e3aTASK_INTERRUPTIBLE\u6216\u8005TASK_UNINTERRUPTIBLE\uff0c\u6302\u63a5\u5230\u67d0\u4e2a\u7b49\u5f85\u961f\u5217\uff0c\u7136\u540e\u6839\u636e\u60c5\u51b5\u8bbe\u7f6e\u4e00\u4e2a\u5408\u9002\u7684\u5524\u9192\u5b9a\u65f6\u5668\uff0c\u6700\u540e\u8c03\u7528schedule()\u53d1\u8d77\u8c03\u5ea6\uff1b<\/p>\n

\/* * The below macro ___wait_event() has an explicit shadow of the __ret * variable when used from the wait_event_*() macros. * * This is so that both can use the ___wait_cond_timeout() construct * to wrap the condition. * * The type inconsistency of the wait_event_*() __ret variable is also * on purpose; we use long where we can return timeout values and int * otherwise. *\/<\/span> #<\/span>define<\/span> ___wait_event<\/span>(<\/span>wq,<\/span> condition,<\/span> state,<\/span> exclusive,<\/span> ret,<\/span> cmd)<\/span> <\/span>\\<\/span> (<\/span>{ \n     <\/span> <\/span>\\<\/span> __label__ __out;<\/span> <\/span>\\<\/span> wait_queue_t<\/span> __wait;<\/span> <\/span>\\<\/span> long<\/span> __ret =<\/span> ret;<\/span> <\/span>\/* explicit shadow *\/<\/span> \\<\/span> \\<\/span> init_wait_entry<\/span>(<\/span>&<\/span>__wait,<\/span> exclusive ?<\/span> WQ_FLAG_EXCLUSIVE :<\/span> 0<\/span>)<\/span>;<\/span> <\/span>\\<\/span> for<\/span> (<\/span>;<\/span>;<\/span>)<\/span> { \n     <\/span> <\/span>\\<\/span> long<\/span> __int =<\/span> prepare_to_wait_event<\/span>(<\/span>&<\/span>wq,<\/span> &<\/span>__wait,<\/span> state)<\/span>;<\/span><\/span>\\<\/span> \\<\/span> if<\/span> (<\/span>condition)<\/span> <\/span>\\<\/span> break<\/span>;<\/span> <\/span>\\<\/span> \\<\/span> if<\/span> (<\/span>___wait_is_interruptible<\/span>(<\/span>state)<\/span> &&<\/span> __int)<\/span> { \n     <\/span> <\/span>\\<\/span> __ret =<\/span> __int;<\/span> <\/span>\\<\/span> goto<\/span> __out;<\/span> <\/span>\\<\/span> }<\/span> <\/span>\\<\/span> \\<\/span> cmd;<\/span> <\/span>\\<\/span> }<\/span> <\/span>\\<\/span> finish_wait<\/span>(<\/span>&<\/span>wq,<\/span> &<\/span>__wait)<\/span>;<\/span> <\/span>\\<\/span> __out:<\/span> __ret;<\/span> <\/span>\\<\/span> }<\/span>)<\/span><\/span><\/span> #<\/span>define<\/span> __wait_event<\/span>(<\/span>wq,<\/span> condition)<\/span> <\/span>\\<\/span> (<\/span>void<\/span>)<\/span>___wait_event<\/span>(<\/span>wq,<\/span> condition,<\/span> TASK_UNINTERRUPTIBLE,<\/span> 0<\/span>,<\/span> 0<\/span>,<\/span> <\/span>\\<\/span> schedule<\/span>(<\/span>)<\/span>)<\/span><\/span><\/span> \/** * wait_event - sleep until a condition gets true * @wq: the waitqueue to wait on * @condition: a C expression for the event to wait for * * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the * @condition evaluates to true. The @condition is checked each time * the waitqueue @wq is woken up. * * wake_up() has to be called after changing any variable that could * change the result of the wait condition. *\/<\/span> #<\/span>define<\/span> wait_event<\/span>(<\/span>wq,<\/span> condition)<\/span> <\/span>\\<\/span> do<\/span> { \n     <\/span> <\/span>\\<\/span> might_sleep<\/span>(<\/span>)<\/span>;<\/span> <\/span>\\<\/span> if<\/span> (<\/span>condition)<\/span> <\/span>\\<\/span> break<\/span>;<\/span> <\/span>\\<\/span> __wait_event<\/span>(<\/span>wq,<\/span> condition)<\/span>;<\/span> <\/span>\\<\/span> }<\/span> while<\/span> (<\/span>0<\/span>)<\/span><\/span><\/span> <\/code><\/pre>\n

\uff082\uff09\u5f53\u524d\u8fdb\u7a0b\u9700\u8981\u7761\u7720\u4e00\u6bb5\u7279\u5b9a\u7684\u65f6\u95f4\uff08\u4e0d\u7b49\u5f85\u4efb\u4f55\u4e8b\u4ef6\uff09\u65f6\uff0c\u8c03\u7528\u4e00\u4e2asleep()\u7c7b\u51fd\u6570\u5c06\u81ea\u5df1\u7684\u72b6\u6001\u8bbe\u4e3aTASK_INTERRUPTIBLE\u6216\u8005TASK_UNINTERRUPTIBLE\u4f46\u4e0d\u8fdb\u5165\u4efb\u4f55\u7b49\u5f85\u961f\u5217\uff0c\u7136\u540e\u8bbe\u7f6e\u4e00\u4e2a\u5408\u9002\u7684\u5524\u9192\u5b9a\u65f6\u5668\uff0c\u6700\u540e\u8c03\u7528schedule()\u53d1\u8d77\u8c03\u5ea6\uff1b<\/p>\n

\uff083\uff09\u5f53\u524d\u8fdb\u7a0b\u5355\u7eaf\u5730\u60f3\u8981\u8ba9\u51faCPU\u63a7\u5236\u6743\u65f6\uff0c\u8c03\u7528yield()\u51fd\u6570\u5c06\u81ea\u5df1\u7684\u72b6\u6001\u8bbe\u4e3aTASK_RUNNING\u5e76\u4f9d\u65e7\u5904\u4e8e\u8fd0\u884c\u961f\u5217\uff0c\u7136\u540e\u6267\u884c\u7279\u5b9a\u8c03\u5ea6\u7c7b\u7684yield_task()\u64cd\u4f5c\uff0c\u6700\u540e\u8c03\u7528schedule()\u53d1\u8d77\u81ea\u613f\u8c03\u5ea6\u3002<\/p>\n

\/** * sys_sched_yield - yield the current processor to other threads. * * This function yields the current CPU to other tasks. If there are no * other threads running on this CPU then this function will return. * * Return: 0. *\/<\/span> SYSCALL_DEFINE0<\/span>(<\/span>sched_yield)<\/span> { \n   <\/span> struct<\/span> rq<\/span> *<\/span>rq =<\/span> this_rq_lock<\/span>(<\/span>)<\/span>;<\/span> schedstat_inc<\/span>(<\/span>rq-><\/span>yld_count)<\/span>;<\/span> current-><\/span>sched_class-><\/span>yield_task<\/span>(<\/span>rq)<\/span>;<\/span> \/* * Since we are going to call schedule() anyway, there's * no need to preempt or enable interrupts: *\/<\/span> __release<\/span>(<\/span>rq-><\/span>lock)<\/span>;<\/span> spin_release<\/span>(<\/span>&<\/span>rq-><\/span>lock.<\/span>dep_map,<\/span> 1<\/span>,<\/span> _THIS_IP_)<\/span>;<\/span> do_raw_spin_unlock<\/span>(<\/span>&<\/span>rq-><\/span>lock)<\/span>;<\/span> sched_preempt_enable_no_resched<\/span>(<\/span>)<\/span>;<\/span> schedule<\/span>(<\/span>)<\/span>;<\/span> return<\/span> 0<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

\u4f8b\u4e3e\u4e00\u5904\u4e3b\u52a8\u8c03\u5ea6\u7684\u5730\u65b9\uff08\u8fdb\u7a0b\u53d8\u6210\u963b\u585e\u6001\uff0c\u4e3b\u52a8\u8c03\u7528\u4e86schedule\u51fd\u6570\uff09\uff1a<\/p>\n

static<\/span> void<\/span> d_wait_lookup<\/span>(<\/span>struct<\/span> dentry<\/span> *<\/span>dentry)<\/span> { \n   <\/span> if<\/span> (<\/span>d_in_lookup<\/span>(<\/span>dentry)<\/span>)<\/span> { \n   <\/span> \/\/\u8c03\u7528DECLARE_WAITQUEUE\u9759\u6001\u521b\u5efa\u4e00\u4e2a\u7b49\u5f85\u961f\u5217<\/span> \/\/\u7b49\u5f85\u961f\u5217\u7531\u7b49\u5f85\u67d0\u4e9b\u4e8b\u4ef6\u53d1\u751f\u7684\u8fdb\u7a0b\u7ec4\u6210\u7684\u7b80\u5355\u94fe\u8868<\/span> DECLARE_WAITQUEUE<\/span>(<\/span>wait,<\/span> current)<\/span>;<\/span> \/\/\u5c06\u8fdb\u7a0b\u653e\u5165\u7b49\u5f85\u961f\u5217\u4e2d<\/span> add_wait_queue<\/span>(<\/span>dentry-><\/span>d_wait,<\/span> &<\/span>wait)<\/span>;<\/span> do<\/span> { \n   <\/span> \/\/\u5c06\u8fdb\u7a0b\u7684\u72b6\u6001\u8bbe\u7f6e\u4e3aTASK_UNINTERRUPTIBLE<\/span> set_current_state<\/span>(<\/span>TASK_UNINTERRUPTIBLE)<\/span>;<\/span> spin_unlock<\/span>(<\/span>&<\/span>dentry-><\/span>d_lock)<\/span>;<\/span> \/\/\u4e3b\u52a8\u8c03\u7528schedule()\u53d1\u8d77\u8c03\u5ea6\uff0c\u8c03\u7528deactivate_task\u5c06\u8fdb\u7a0b\u79fb\u9664\u5c31\u7eea\u961f\u5217<\/span> schedule<\/span>(<\/span>)<\/span>;<\/span> spin_lock<\/span>(<\/span>&<\/span>dentry-><\/span>d_lock)<\/span>;<\/span> }<\/span> while<\/span> (<\/span>d_in_lookup<\/span>(<\/span>dentry)<\/span>)<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

__schedule\u7684\u53c2\u6570preempt\u7b49\u4e8e1\u8868\u793a\u662f\u62a2\u5360\u8c03\u5ea6\uff0c\u6709\u5904\u4e8e\u8fd0\u884c\u6001\u7684\u4efb\u52a1\u53d1\u8d77\u7684\u62a2\u5360\u8c03\u5ea6\u3002
\u4f8b\u4e3e\u51e0\u5904\u53d1\u8d77\u62a2\u5360\u8c03\u5ea6\u7684\u5730\u65b9\uff1a<\/p>\n

static<\/span> void<\/span> __sched notrace preempt_schedule_common<\/span>(<\/span>void<\/span>)<\/span> { \n   <\/span> do<\/span> { \n   <\/span> \/* * Because the function tracer can trace preempt_count_sub() * and it also uses preempt_enable\/disable_notrace(), if * NEED_RESCHED is set, the preempt_enable_notrace() called * by the function tracer will call this function again and * cause infinite recursion. * * Preemption must be disabled here before the function * tracer can trace. Break up preempt_disable() into two * calls. One to disable preemption without fear of being * traced. The other to still record the preemption latency, * which can also be traced by the function tracer. *\/<\/span> preempt_disable_notrace<\/span>(<\/span>)<\/span>;<\/span> preempt_latency_start<\/span>(<\/span>1<\/span>)<\/span>;<\/span> \/\/\u62a2\u5360\u8c03\u5ea6<\/span> __schedule<\/span>(<\/span>true)<\/span>;<\/span> preempt_latency_stop<\/span>(<\/span>1<\/span>)<\/span>;<\/span> preempt_enable_no_resched_notrace<\/span>(<\/span>)<\/span>;<\/span> \/* * Check again in case we missed a preemption opportunity * between schedule and now. *\/<\/span> }<\/span> while<\/span> (<\/span>need_resched<\/span>(<\/span>)<\/span>)<\/span>;<\/span> }<\/span> #<\/span>ifdef<\/span> CONFIG_PREEMPT<\/span><\/span> \/* * this is the entry point to schedule() from in-kernel preemption * off of preempt_enable. Kernel preemptions off return from interrupt * occur there and call schedule directly. *\/<\/span> asmlinkage __visible void<\/span> __sched notrace preempt_schedule<\/span>(<\/span>void<\/span>)<\/span> { \n   <\/span> \/* * If there is a non-zero preempt_count or interrupts are disabled, * we do not want to preempt the current task. Just return.. *\/<\/span> if<\/span> (<\/span>likely<\/span>(<\/span>!<\/span>preemptible<\/span>(<\/span>)<\/span>)<\/span>)<\/span> return<\/span>;<\/span> preempt_schedule_common<\/span>(<\/span>)<\/span>;<\/span> }<\/span> NOKPROBE_SYMBOL<\/span>(<\/span>preempt_schedule)<\/span>;<\/span> EXPORT_SYMBOL<\/span>(<\/span>preempt_schedule)<\/span>;<\/span> \/** * preempt_schedule_notrace - preempt_schedule called by tracing * * The tracing infrastructure uses preempt_enable_notrace to prevent * recursion and tracing preempt enabling caused by the tracing * infrastructure itself. But as tracing can happen in areas coming * from userspace or just about to enter userspace, a preempt enable * can occur before user_exit() is called. This will cause the scheduler * to be called when the system is still in usermode. * * To prevent this, the preempt_enable_notrace will use this function * instead of preempt_schedule() to exit user context if needed before * calling the scheduler. *\/<\/span> asmlinkage __visible void<\/span> __sched notrace preempt_schedule_notrace<\/span>(<\/span>void<\/span>)<\/span> { \n   <\/span> enum<\/span> ctx_state<\/span> prev_ctx;<\/span> if<\/span> (<\/span>likely<\/span>(<\/span>!<\/span>preemptible<\/span>(<\/span>)<\/span>)<\/span>)<\/span> return<\/span>;<\/span> do<\/span> { \n   <\/span> \/* * Because the function tracer can trace preempt_count_sub() * and it also uses preempt_enable\/disable_notrace(), if * NEED_RESCHED is set, the preempt_enable_notrace() called * by the function tracer will call this function again and * cause infinite recursion. * * Preemption must be disabled here before the function * tracer can trace. Break up preempt_disable() into two * calls. One to disable preemption without fear of being * traced. The other to still record the preemption latency, * which can also be traced by the function tracer. *\/<\/span> preempt_disable_notrace<\/span>(<\/span>)<\/span>;<\/span> preempt_latency_start<\/span>(<\/span>1<\/span>)<\/span>;<\/span> \/* * Needs preempt disabled in case user_exit() is traced * and the tracer calls preempt_enable_notrace() causing * an infinite recursion. *\/<\/span> prev_ctx =<\/span> exception_enter<\/span>(<\/span>)<\/span>;<\/span> \/\/\u62a2\u5360\u8c03\u5ea6<\/span> __schedule<\/span>(<\/span>true)<\/span>;<\/span> exception_exit<\/span>(<\/span>prev_ctx)<\/span>;<\/span> preempt_latency_stop<\/span>(<\/span>1<\/span>)<\/span>;<\/span> preempt_enable_no_resched_notrace<\/span>(<\/span>)<\/span>;<\/span> }<\/span> while<\/span> (<\/span>need_resched<\/span>(<\/span>)<\/span>)<\/span>;<\/span> }<\/span> EXPORT_SYMBOL_GPL<\/span>(<\/span>preempt_schedule_notrace)<\/span>;<\/span> #<\/span>endif<\/span> \/* CONFIG_PREEMPT *\/<\/span><\/span> \/* * this is the entry point to schedule() from kernel preemption * off of irq context. * Note, that this is called and return with irqs disabled. This will * protect us against recursive calling from irq. *\/<\/span> asmlinkage __visible void<\/span> __sched preempt_schedule_irq<\/span>(<\/span>void<\/span>)<\/span> { \n   <\/span> enum<\/span> ctx_state<\/span> prev_state;<\/span> \/* Catch callers which need to be fixed *\/<\/span> BUG_ON<\/span>(<\/span>preempt_count<\/span>(<\/span>)<\/span> ||<\/span> !<\/span>irqs_disabled<\/span>(<\/span>)<\/span>)<\/span>;<\/span> prev_state =<\/span> exception_enter<\/span>(<\/span>)<\/span>;<\/span> do<\/span> { \n   <\/span> preempt_disable<\/span>(<\/span>)<\/span>;<\/span> local_irq_enable<\/span>(<\/span>)<\/span>;<\/span> \/\/\u62a2\u5360\u8c03\u5ea6<\/span> __schedule<\/span>(<\/span>true)<\/span>;<\/span> local_irq_disable<\/span>(<\/span>)<\/span>;<\/span> sched_preempt_enable_no_resched<\/span>(<\/span>)<\/span>;<\/span> }<\/span> while<\/span> (<\/span>need_resched<\/span>(<\/span>)<\/span>)<\/span>;<\/span> exception_exit<\/span>(<\/span>prev_state)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

__schedule()\u662f\u4e3b\u8c03\u5ea6\u5668\u7684\u4e3b\u8981\u51fd\u6570\uff0c__schedule\u5728\u5185\u6838\u6e90\u7801\u4e2d\u6709\u5f88\u591a\u6ce8\u91ca\uff0c\u5982\u4e0b\u6240\u793a\uff1a
\u9a71\u4f7f\u8c03\u5ea6\u5668\u5e76\u56e0\u6b64\u8fdb\u5165\u6b64\u51fd\u6570\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a
1.\u663e\u5f0f\u963b\u585e\uff1a\u4e92\u65a5\u3001\u4fe1\u53f7\u91cf\u3001\u7b49\u5f85\u961f\u5217\u7b49\u3002
2.\u4e2d\u65ad\u548c\u7528\u6237\u7a7a\u95f4\u8fd4\u56de\u8def\u5f84\u4e0a\u68c0\u67e5TIF_NEED_RESCHED\u6807\u5fd7\u3002\u4f8b\u5982\uff0c\u8bf7\u53c2\u8003arch\/x86\/entry_64.S\u3002
\u4e3a\u4e86\u9a71\u52a8\u4efb\u52a1\u4e4b\u95f4\u7684\u62a2\u5360\uff0c\u8c03\u5ea6\u7a0b\u5e8f\u5728\u5b9a\u65f6\u5668\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8fscheduler_tick()\u4e2d\u8bbe\u7f6e\u6807\u5fd7\u3002
3.\u5524\u9192\u4e0d\u4f1a\u771f\u6b63\u9a6c\u4e0a\u8c03\u7528schedule()\uff0c\u53ea\u662f\u5c06\u4e00\u4e2a\u4efb\u52a1\u6dfb\u52a0\u5230\u8fd0\u884c\u961f\u5217\u4e2d\uff0c\u8bbe\u7f6e\u4efb\u52a1\u6807\u5fd7\u4f4d\u4e3aTIF_NED_RESCHED\uff0c\u4e5f\u5c31\u662f\u5c06\u5524\u9192\u7684\u8fdb\u7a0b\u52a0\u5165\u7684CFS\u5c31\u7eea\u961f\u5217\u4e2d\uff08\u5c06\u5524\u9192\u7684\u8fdb\u7a0b\u8c03\u5ea6\u5b9e\u4f53\u52a0\u5165\u5230\u7ea2\u9ed1\u6811\u4e2d\uff09\uff0c\u4ec5\u6b64\u800c\u5df2\u3002<\/p>\n

\u73b0\u5728\uff0c\u5982\u679c\u6dfb\u52a0\u5230\u8fd0\u884c\u961f\u5217\u7684\u65b0\u4efb\u52a1\u62a2\u5360\u4e86\u5f53\u524d\u4efb\u52a1\uff0c\u5219\u8bbe\u7f6eTIF_NED_RESCHED\uff0c\u5e76\u5728\u4ee5\u4e0b\u7684\u53ef\u80fd\u60c5\u51b5\u4e0b\u8c03\u7528schedule\uff08\uff09\uff0c\u4e5f\u5c31\u662f\u5524\u9192\u7684\u8fdb\u7a0b\u4ec0\u4e48\u65f6\u5019\u8c03\u7528schedule()\u51fd\u6570\uff0c\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n

\uff081\uff09\u5982\u679c\u5185\u6838\u53ef\u62a2\u5360(CONFIG_PREMPT=y)\uff1a
\u5728\u7cfb\u7edf\u8c03\u7528\u6216\u5f02\u5e38\u4e0a\u4e0b\u6587\u4e2d\uff0c\u5728\u4e0b\u4e00\u6b21\u8c03\u7528preempt_enable()\u65f6\u68c0\u67e5\u662f\u5426\u9700\u8981\u62a2\u5360\u8c03\u5ea6\u3002
\u5728IRQ\u4e0a\u4e0b\u6587\u4e2d\uff0c\u4ece\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u8fd4\u56de\u5230\u53ef\u62a2\u5360\u4e0a\u4e0b\u6587\u3002\u786c\u4ef6\u4e2d\u65ad\u5904\u7406\u8fd4\u56de\u524d\u4f1a\u68c0\u67e5\u662f\u5426\u8981\u62a2\u5360\u5f53\u524d\u8fdb\u7a0b\u3002<\/p>\n

\uff082\uff09\u5982\u679c\u5185\u6838\u4e0d\u53ef\u62a2\u5360(\u672a\u8bbe\u7f6eCONFIG_PREMPT)
\u8c03\u7528cond_resched()\u3002
\u663e\u5f0f\u8c03\u7528schedule()\u3002
\u4ecesyscall\u6216\u5f02\u5e38\u8fd4\u56de\u5230\u7528\u6237\u7a7a\u95f4\u3002
\u4ece\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u8fd4\u56de\u5230\u7528\u6237\u7a7a\u95f4\u3002<\/p>\n

2.2 __schedule \u4ee3\u7801\u89e3\u56fe<\/h3>\n
\/\/__schedule() is the main scheduler function.<\/span> static<\/span> void<\/span> __sched notrace __schedule<\/span>(<\/span>bool preempt)<\/span> { \n   <\/span> (<\/span>1<\/span>)<\/span> struct<\/span> task_struct<\/span> *<\/span>prev,<\/span> *<\/span>next;<\/span> unsigned<\/span> long<\/span> *<\/span>switch_count;<\/span> struct<\/span> pin_cookie<\/span> cookie;<\/span> struct<\/span> rq<\/span> *<\/span>rq;<\/span> int<\/span> cpu;<\/span> (<\/span>2<\/span>)<\/span> cpu =<\/span> smp_processor_id<\/span>(<\/span>)<\/span>;<\/span> rq =<\/span> cpu_rq<\/span>(<\/span>cpu)<\/span>;<\/span> prev =<\/span> rq-><\/span>curr;<\/span> (<\/span>3<\/span>)<\/span> if<\/span> (<\/span>!<\/span>preempt &&<\/span> prev-><\/span>state)<\/span> { \n   <\/span> if<\/span> (<\/span>unlikely<\/span>(<\/span>signal_pending_state<\/span>(<\/span>prev-><\/span>state,<\/span> prev)<\/span>)<\/span>)<\/span> { \n   <\/span> prev-><\/span>state =<\/span> TASK_RUNNING;<\/span> }<\/span> else<\/span> { \n   <\/span> deactivate_task<\/span>(<\/span>rq,<\/span> prev,<\/span> DEQUEUE_SLEEP)<\/span>;<\/span> prev-><\/span>on_rq =<\/span> 0<\/span>;<\/span> }<\/span> }<\/span> (<\/span>4<\/span>)<\/span> next =<\/span> pick_next_task<\/span>(<\/span>rq,<\/span> prev,<\/span> cookie)<\/span>;<\/span> clear_tsk_need_resched<\/span>(<\/span>prev)<\/span>;<\/span> clear_preempt_need_resched<\/span>(<\/span>)<\/span>;<\/span> (<\/span>5<\/span>)<\/span> if<\/span> (<\/span>likely<\/span>(<\/span>prev !=<\/span> next)<\/span>)<\/span> { \n   <\/span> rq-><\/span>nr_switches++<\/span>;<\/span> rq-><\/span>curr =<\/span> next;<\/span> ++<\/span>*<\/span>switch_count;<\/span> rq =<\/span> context_switch<\/span>(<\/span>rq,<\/span> prev,<\/span> next,<\/span> cookie)<\/span>;<\/span> \/* unlocks the rq *\/<\/span> }<\/span> else<\/span> { \n   <\/span> lockdep_unpin_lock<\/span>(<\/span>&<\/span>rq-><\/span>lock,<\/span> cookie)<\/span>;<\/span> raw_spin_unlock_irq<\/span>(<\/span>&<\/span>rq-><\/span>lock)<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

\uff081\uff09prev\u5c40\u90e8\u53d8\u91cf\u8868\u793a\u8981\u5207\u6362\u51fa\u53bb\u7684\u4efb\u52a1\uff0cnext\u5c40\u90e8\u53d8\u91cf\u8868\u793a\u8981\u5207\u6362\u8fdb\u6765\u7684\u4efb\u52a1\u3002<\/p>\n

 struct<\/span> task_struct<\/span> *<\/span>prev,<\/span> *<\/span>next;<\/span> <\/code><\/pre>\n

\uff082\uff09\u627e\u5230\u5f53\u524dCPU\u7684\u8fd0\u884c\u961f\u5217 struct rq\uff0c\u628a\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u4efb\u52a1curr \u8d4b\u503c\u7ed9 prev\u3002<\/p>\n

 int<\/span> cpu =<\/span> smp_processor_id<\/span>(<\/span>)<\/span>;<\/span> struct<\/span> rq<\/span> =<\/span> cpu_rq<\/span>(<\/span>cpu)<\/span>;<\/span> struct<\/span> task_struct<\/span> *<\/span>prev =<\/span> rq-><\/span>curr;<\/span> <\/code><\/pre>\n

\uff083\uff09
preempt \u7528\u4e8e\u5224\u65ad\u672c\u6b21\u8c03\u5ea6\u662f\u5426\u4e3a\u62a2\u5360\u8c03\u5ea6\uff0c\u5982\u679c\u53d1\u751f\u4e86\u8c03\u5ea6\u62a2\u5360\uff08preempt =1\uff09\uff0c\u90a3\u4e48\u76f4\u63a5\u8df3\u8fc7\uff0c\u4e0d\u53c2\u4e0e\u5224\u65ad\uff0c\u76f4\u63a5\u8c03\u7528pick_next_task\u3002\u62a2\u5360\u8c03\u5ea6\u901a\u5e38\u90fd\u662f\u5904\u4e8e\u8fd0\u884c\u6001\u7684\u4efb\u52a1\u53d1\u8d77\u7684\u62a2\u5360\u8c03\u5ea6\u3002<\/p>\n

\u5982\u679c\u672c\u6b21\u8c03\u5ea6\u4e0d\u662f\u62a2\u5360\u8c03\u5ea6\uff08preempt = 0\uff09\uff0c\u5e76\u4e14\u8be5\u8fdb\u7a0b\u7684state\u4e0d\u7b49\u4e8e TASK_RUNNING \uff080\uff09\uff0c\u4e5f\u5c31\u662f\u4e0d\u662f\u8fd0\u884c\u6001\uff0c\u5904\u4e8e\u5176\u4ed6\u72b6\u6001\u3002\u4ee3\u8868\u6b64\u6b21\u8c03\u5ea6\u662f\u8be5\u8fdb\u7a0b\u4e3b\u52a8\u8bf7\u6c42\u8c03\u5ea6\uff0c\u4e3b\u52a8\u8c03\u7528\u4e86schedule\u51fd\u6570\uff0c\u6bd4\u5982\u8be5\u8fdb\u7a0b\u8fdb\u5165\u4e86\u963b\u585e\u6001\u3002
\u8fdb\u7a0b\u5728\u64cd\u4f5c\u5916\u90e8\u8bbe\u5907\u7684\u65f6\u5019\uff08\u7f51\u7edc\u548c\u5b58\u50a8\u5219\u591a\u662f\u548c\u5916\u90e8\u8bbe\u5907\u7684\u5408\u4f5c\uff09\uff0c\u5f80\u5f80\u9700\u8981\u8ba9\u51fa CPU\uff0c\u53d1\u8d77\u4e3b\u52a8\u8c03\u5ea6\u3002
\u7531\u4e8e\u8fdb\u7a0b\u4e0d\u662f\u8fd0\u884c\u6001\uff1aTASK_RUNNING\u4e86\uff0c\u90a3\u4e48\u5c31\u4e0d\u80fd\u5728CFS\u5c31\u7eea\u961f\u5217\u4e2d\u4e86\uff0c\u90a3\u4e48\u5c31\u8c03\u7528 deactivate_task \u5c06\u9677\u5165\u963b\u585e\u6001\u7684\u8fdb\u7a0b\u79fb\u9664CFS\u5c31\u7eea\u961f\u5217\uff0c\u5e76\u5c06\u8fdb\u7a0b\u8c03\u5ea6\u5b9e\u4f53\u7684 on_rq \u6210\u5458\u7f6e0\uff0c\u8868\u793a\u4e0d\u5728CFS\u5c31\u7eea\u961f\u5217\u4e2d\u4e86\u3002
\u901a\u5e38\u4e3b\u52a8\u8bf7\u6c42\u8c03\u7528\u4e4b\u524d\u4f1a\u63d0\u524d\u8bbe\u7f6e\u5f53\u524d\u8fdb\u7a0b\u7684\u8fd0\u884c\u72b6\u6001\u4e3a TASK_INTERRUPTIBLE \u6216\u8005 TASK_UNINTERRUPTIBLE\u3002<\/p>\n

#<\/span>define<\/span> TASK_RUNNING<\/span> 0<\/span><\/span><\/span> #<\/span>define<\/span> TASK_INTERRUPTIBLE<\/span> 1<\/span><\/span><\/span> #<\/span>define<\/span> TASK_UNINTERRUPTIBLE<\/span> 2<\/span><\/span><\/span> .<\/span>.<\/span>.<\/span>.<\/span>.<\/span>.<\/span> <\/code><\/pre>\n
 if<\/span> (<\/span>!<\/span>preempt &&<\/span> prev-><\/span>state)<\/span> { \n   <\/span> if<\/span> (<\/span>unlikely<\/span>(<\/span>signal_pending_state<\/span>(<\/span>prev-><\/span>state,<\/span> prev)<\/span>)<\/span>)<\/span> { \n   <\/span> prev-><\/span>state =<\/span> TASK_RUNNING;<\/span> }<\/span> else<\/span> { \n   <\/span> deactivate_task<\/span>(<\/span>rq,<\/span> prev,<\/span> DEQUEUE_SLEEP)<\/span>;<\/span> prev-><\/span>on_rq =<\/span> 0<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n
 deactivate_task<\/span>(<\/span>rq,<\/span> prev,<\/span> DEQUEUE_SLEEP)<\/span>;<\/span> --<\/span>><\/span>dequeue_task<\/span>(<\/span>rq,<\/span> p,<\/span> flags)<\/span>;<\/span> --<\/span>><\/span>p-><\/span>sched_class-><\/span>dequeue_task<\/span>(<\/span>rq,<\/span> p,<\/span> flags)<\/span>;<\/span> prev-><\/span>on_rq =<\/span> 0<\/span>;<\/span> <\/code><\/pre>\n

\uff084\uff09\u9009\u62e9\u4e0b\u4e00\u4e2a\u5c06\u8981\u6267\u884c\u7684\u4efb\u52a1\uff0c\u901a\u8fc7CFS\u8c03\u5ea6\u7b97\u6cd5\u9009\u62e9\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u4e00\u4e2a\u4efb\u52a1\u3002
clear_tsk_need_resched\u5c06\u88ab\u62a2\u5360\u7684\u4efb\u52a1prev\uff08\u4e5f\u5c31\u662f\u5f53\u524d\u7684\u4efb\u52a1\uff09\u9700\u8981\u88ab\u8c03\u5ea6\u7684\u6807\u5fd7\u4f4d\uff08TIF_NEED_RESCHED\uff09\u7ed9\u6e05\u9664\u6389\uff0c\u8868\u793a prev \u63a5\u4e0b\u6765\u4e0d\u4f1a\u88ab\u8c03\u5ea6\u3002
clear_preempt_need_resched \u88ab\u62a2\u5360\u7684\u4efb\u52a1prev\uff08\u4e5f\u5c31\u662f\u5f53\u524d\u7684\u4efb\u52a1\uff09\u7684 PREEMPT_NEED_RESCHED \u6807\u5fd7\u4f4d\u7ed9\u6e05\u9664\u6389\u3002<\/p>\n

 next =<\/span> pick_next_task<\/span>(<\/span>rq,<\/span> prev,<\/span> cookie)<\/span>;<\/span> --<\/span>><\/span> next =<\/span> fair_sched_class.<\/span>pick_next_task<\/span>(<\/span>rq,<\/span> prev,<\/span> cookie)<\/span>;<\/span> clear_tsk_need_resched<\/span>(<\/span>prev)<\/span>;<\/span> clear_preempt_need_resched<\/span>(<\/span>)<\/span>;<\/span> <\/code><\/pre>\n

\uff085\uff09\u5982\u679c\u9009\u62e9\u7684\u4efb\u52a1next\u548c \u539f\u4efb\u52a1prev\u4e0d\u662f\u540c\u4e00\u4e2a\u4efb\u52a1\uff0c\u5219\u8fdb\u884c\u4efb\u52a1\u4e0a\u4e0b\u6587\u5207\u6362
\u5982\u679c\u662f\u540c\u4e00\u4e2a\u4efb\u52a1\uff0c\u5219\u4e0d\u8fdb\u884c\u4e0a\u4e0b\u6587\u5207\u6362\u3002
\u6ce8\u610f\u8fd9\u91cc\u662f \u7528 likely()\u4fee\u9970\uff08\u8fd9\u662fgcc\u5185\u5efa\u7684\u4e00\u6761\u6307\u4ee4\u7528\u4e8e\u4f18\u5316\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u6839\u636e\u8fd9\u6761\u6307\u4ee4\u5bf9\u5206\u652f\u9009\u62e9\u8fdb\u884c\u4f18\u5316\uff09\uff0c\u8868\u793a\u6709\u5f88\u5927\u7684\u6982\u7387 \u9009\u62e9\u7684\u4efb\u52a1next \u548c \u539f\u4efb\u52a1prev\u4e0d\u662f\u540c\u4e00\u4e2a\u4efb\u52a1\u3002
\u7531\u6211\u4eec\u7a0b\u5e8f\u5458\u6765\u6307\u660e\u6307\u4ee4\u6700\u53ef\u80fd\u7684\u5206\u652f\u8d70\u5411\uff0c\u4ee5\u8fbe\u5230\u4f18\u5316\u6027\u80fd\u7684\u6548\u679c\u3002<\/p>\n

 if<\/span> (<\/span>likely<\/span>(<\/span>prev !=<\/span> next)<\/span>)<\/span> { \n   <\/span> \/\/\u5927\u6982\u7387\u4e8b\u4ef6\uff0c\u8fdb\u884c\u4efb\u52a1\u5207\u6362<\/span> rq-><\/span>nr_switches++<\/span>;<\/span> \/\/\u53ef\u8fd0\u884c\u961f\u5217\u5207\u6362\u6b21\u6570\u66f4\u65b0<\/span> rq-><\/span>curr =<\/span> next;<\/span> \/\/\u5c06\u5f53\u524d\u4efb\u52a1curr\u8bbe\u7f6e\u4e3a\u5c06\u8981\u8fd0\u884c\u7684\u4e0b\u4e00\u4e2a\u4efb\u52a1 next <\/span> ++<\/span>*<\/span>switch_count;<\/span> \/\/\u4efb\u52a1\u5207\u6362\u6b21\u6570\u66f4\u65b0<\/span> \/\/\u4efb\u52a1\u4e0a\u4e0b\u6587\u5207\u6362<\/span> rq =<\/span> context_switch<\/span>(<\/span>rq,<\/span> prev,<\/span> next,<\/span> cookie)<\/span>;<\/span> \/* unlocks the rq *\/<\/span> }<\/span> else<\/span> { \n   <\/span> \/\/\u5c0f\u6982\u7387\u4e8b\u4ef6\uff0c\u4e0d\u8fdb\u884c\u4efb\u52a1\u5207\u6362<\/span> lockdep_unpin_lock<\/span>(<\/span>&<\/span>rq-><\/span>lock,<\/span> cookie)<\/span>;<\/span> raw_spin_unlock_irq<\/span>(<\/span>&<\/span>rq-><\/span>lock)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

2.3 context_switch \u4ee3\u7801\u89e3\u8bfb<\/h3>\n

\u4efb\u52a1\u5207\u6362\u4e3b\u8981\u662f\u4efb\u52a1\u7a7a\u95f4\u5373\u865a\u62df\u5185\u5b58\uff08\u7528\u6237\u6001\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u5305\u62ec\u4e86\u7528\u6237\u6001\u7684\u5806\u6808\uff09\u3001CPU\u5bc4\u5b58\u5668\u3001\u5185\u6838\u6001\u5806\u6808\u3002<\/p>\n

\u540e\u9762context_switch \u8fd8\u4f1a\u4e13\u95e8\u4e00\u7bc7\u8fdb\u884c\u63cf\u8ff0\uff0c\u8fd9\u91cc\u9650\u4e8e\u7bc7\u5e45\uff0c\u53ea\u662f\u7b80\u5355\u63cf\u8ff0\u4e00\u4e0b\u3002<\/strong><\/p>\n

\u7528\u4f2a\u4ee3\u7801\u8868\u793a\uff1a<\/p>\n

 switch_mm<\/span>(<\/span>)<\/span>;<\/span> switch_to<\/span>(<\/span>)<\/span>{ \n   <\/span> switch_register<\/span>(<\/span>)<\/span>;<\/span> switch_stack<\/span>(<\/span>)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n
\/* * context_switch - switch to the new MM and the new thread's register state. *\/<\/span> static<\/span> __always_inline struct<\/span> rq<\/span> *<\/span> context_switch<\/span>(<\/span>struct<\/span> rq<\/span> *<\/span>rq,<\/span> struct<\/span> task_struct<\/span> *<\/span>prev,<\/span> struct<\/span> task_struct<\/span> *<\/span>next,<\/span> struct<\/span> pin_cookie<\/span> cookie)<\/span> { \n   <\/span> struct<\/span> mm_struct<\/span> *<\/span>mm,<\/span> *<\/span>oldmm;<\/span> (<\/span>1<\/span>)<\/span> prepare_task_switch<\/span>(<\/span>rq,<\/span> prev,<\/span> next)<\/span>;<\/span> (<\/span>2<\/span>)<\/span> mm =<\/span> next-><\/span>mm;<\/span> oldmm =<\/span> prev-><\/span>active_mm;<\/span> if<\/span> (<\/span>!<\/span>mm)<\/span> { \n   <\/span> next-><\/span>active_mm =<\/span> oldmm;<\/span> atomic_inc<\/span>(<\/span>&<\/span>oldmm-><\/span>mm_count)<\/span>;<\/span> enter_lazy_tlb<\/span>(<\/span>oldmm,<\/span> next)<\/span>;<\/span> }<\/span> else<\/span> switch_mm_irqs_off<\/span>(<\/span>oldmm,<\/span> mm,<\/span> next)<\/span>;<\/span> (<\/span>3<\/span>)<\/span> \/* Here we just switch the register state and the stack. *\/<\/span> switch_to<\/span>(<\/span>prev,<\/span> next,<\/span> prev)<\/span>;<\/span> (<\/span>4<\/span>)<\/span> return<\/span> finish_task_switch<\/span>(<\/span>prev)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

\uff081\uff09\u5f00\u59cb\u4efb\u52a1\u5207\u6362\u524d,\u9700\u8981\u505a\u7684\u51c6\u5907\u5de5\u4f5c\uff0c\u8fd9\u91cc\u4e3b\u8981\u662f\u63d0\u4f9b\u4e862\u4e2a\u63a5\u53e3\u7ed9\u6211\u4eec\u5185\u6838\u5f00\u53d1\u8005\uff0c\u5f53\u4efb\u52a1\u5207\u6362\u65f6\u6211\u4eec\u53ef\u4ee5\u81ea\u5df1\u6dfb\u52a0\u4e00\u4e9b\u64cd\u4f5c\u8fdb\u53bb\uff0c\u4efb\u52a1\u88ab\u91cd\u65b0\u8c03\u5ea6\u65f6\u6211\u4eec\u4e5f\u53ef\u4ee5\u81ea\u5df1\u6dfb\u52a0\u4e00\u4e9b\u64cd\u4f5c\u8fdb\u53bb\u3002
\u540c\u65f6\u901a\u77e5\u6211\u4eec\u4efb\u52a1\u88ab\u62a2\u5360\uff08sched_out\uff09\u3002<\/p>\n

 prepare_task_switch<\/span>(<\/span>rq,<\/span> prev,<\/span> next)<\/span>;<\/span> --<\/span>><\/span>fire_sched_out_preempt_notifiers<\/span>(<\/span>prev,<\/span> next)<\/span>;<\/span> --<\/span>><\/span>__fire_sched_out_preempt_notifiers<\/span>(<\/span>curr,<\/span> next)<\/span>;<\/span> --<\/span>><\/span>hlist_for_each_entry<\/span>(<\/span>notifier,<\/span> &<\/span>curr-><\/span>preempt_notifiers,<\/span> link)<\/span> notifier-><\/span>ops-><\/span>sched_out<\/span>(<\/span>notifier,<\/span> next)<\/span>;<\/span> <\/code><\/pre>\n

\u8fd9\u91cc\u7684 sched_in()\u51fd\u6570\u548c sched_out\u51fd\u6570\u662f\u5185\u6838\u63d0\u4f9b\u7ed9\u6211\u4eec\u5f00\u53d1\u8005\u7684\u63a5\u53e3\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u8fd9\u4e24\u4e2a\u63a5\u53e3\u91cc\u9762\u6dfb\u52a0\u4e00\u4e9b\u64cd\u4f5c\u3002
sched_in \uff1a\u4efb\u52a1\u91cd\u65b0\u8c03\u5ea6\u65f6\u4f1a\u901a\u77e5\u6211\u4eec\u3002
sched_out\uff1a\u4efb\u52a1\u88ab\u62a2\u5360\u65f6\u4f1a\u901a\u77e5\u6211\u4eec\u3002<\/p>\n

\u5907\u6ce8\uff1a\u8c03\u5ea6\u5668\u8fd0\u884c\u8c03\u5ea6\u76f8\u5173\u7684\u4ee3\u7801\uff0c\u4f46\u5176\u81ea\u8eab\u5e76\u4e0d\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684 process \u5b58\u5728\uff0c\u5728\u8fdb\u7a0b\u5207\u6362\u65f6\uff0c\u6267\u884c switch out \u7684\u4ee3\u7801\u5c31\u662f\u5728\u88ab\u6362\u51fa\u7684 process \u4e2d\uff0c\u6267\u884c switch in \u7684\u4ee3\u7801\u5c31\u662f\u5728\u88ab\u6362\u5165\u7684 process \u4e2d\uff0c\u56e0\u6b64 scheduler \u6ca1\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684 PID\u3002<\/p>\n

\u5177\u4f53\u8bf7\u53c2\u8003\uff1aLinux \u8fdb\u7a0b\u8c03\u5ea6\u901a\u77e5\u673a\u5236<\/p>\n

struct<\/span> preempt_ops<\/span> { \n   <\/span> void<\/span> (<\/span>*<\/span>sched_in)<\/span>(<\/span>struct<\/span> preempt_notifier<\/span> *<\/span>notifier,<\/span> int<\/span> cpu)<\/span>;<\/span> void<\/span> (<\/span>*<\/span>sched_out)<\/span>(<\/span>struct<\/span> preempt_notifier<\/span> *<\/span>notifier,<\/span> struct<\/span> task_struct<\/span> *<\/span>next)<\/span>;<\/span> }<\/span>;<\/span> struct<\/span> preempt_notifier<\/span> { \n   <\/span> struct<\/span> hlist_node<\/span> link;<\/span> struct<\/span> preempt_ops<\/span> *<\/span>ops;<\/span> }<\/span>;<\/span> <\/code><\/pre>\n

(2) \u5207\u6362\u4efb\u52a1\u7684\u7528\u6237\u865a\u62df\u6001\u5730\u5740\uff08\u4e0d\u5207\u6362\u5185\u6838\u6001\u7684\u865a\u62df\u5730\u5740\uff09\uff0c\u4e5f\u5305\u62ec\u4e86\u7528\u6237\u6001\u7684\u6808\uff0c\u4e3b\u8981\u5c31\u662f\u5207\u6362\u4e86\u4efb\u52a1\u7684CR3\uff0c CR3\u5bc4\u5b58\u5668\u653e\u7684\u662f \u9875\u76ee\u5f55\u8868\u7269\u7406\u5185\u5b58\u57fa\u5730\u5740\u3002<\/p>\n

 mm =<\/span> next-><\/span>mm;<\/span> oldmm =<\/span> prev-><\/span>active_mm;<\/span> if<\/span> (<\/span>!<\/span>mm)<\/span> { \n   <\/span> \/\/ mm == NULL,\u4ee3\u8868\u4efb\u52a1\u662f\u5185\u6838\u7ebf\u7a0b<\/span> \/\/ \u76f4\u63a5\u7528 \u88ab\u5207\u6362\u8fdb\u7a0bprev\u7684active_mm<\/span> next-><\/span>active_mm =<\/span> oldmm;<\/span> atomic_inc<\/span>(<\/span>&<\/span>oldmm-><\/span>mm_count)<\/span>;<\/span> \/\/\u901a\u77e5\u5904\u7406\u5668\u4e0d\u9700\u8981\u5207\u6362\u865a\u62df\u5730\u5740\u7a7a\u95f4\u7684\u7528\u6237\u7a7a\u95f4\u90e8\u5206\uff0c\u7528\u6765\u52a0\u901f\u4e0a\u4e0b\u6587\u5207\u6362<\/span> enter_lazy_tlb<\/span>(<\/span>oldmm,<\/span> next)<\/span>;<\/span> }<\/span> else<\/span> \/\/\u4e0d\u662f\u5185\u6838\u7ebf\u7a0b\uff0c\u90a3\u4e48\u5c31\u8981\u5207\u6362\u7528\u6237\u6001\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u4e5f\u5c31\u662f\u5207\u6362\u4efb\u52a1\u7684CR3<\/span> switch_mm_irqs_off<\/span>(<\/span>oldmm,<\/span> mm,<\/span> next)<\/span>;<\/span> --<\/span>><\/span>load_cr3<\/span>(<\/span>next-><\/span>pgd)<\/span>;<\/span> \/\/\u52a0\u8f7d\u4e0b\u4e00\u4e2a\u4efb\u52a1\u7684CR3<\/span> <\/code><\/pre>\n

\"linux\u8fdb\u7a0b\u8c03\u5ea6\u91c7\u7528\u7684\u662f\u4ec0\u4e48\u8c03\u5ea6\u65b9\u5f0f_linux\u7ebf\u7a0b\u8c03\u5ea6\u7b56\u7565<\/p>\n

\uff083\uff09\u5207\u6362\u4efb\u52a1\u7684\u5bc4\u5b58\u5668\u548c\u5185\u6838\u6001\u5806\u6808\uff0c\u4fdd\u5b58\u539f\u4efb\u52a1\uff08prev\uff09\u7684\u6240\u6709\u5bc4\u5b58\u5668\u4fe1\u606f\uff0c\u6062\u590d\u65b0\u4efb\u52a1\uff08next\uff09\u7684\u6240\u6709\u5bc4\u5b58\u5668\u4fe1\u606f\uff0c\u5f53\u5207\u6362\u5b8c\u4e4b\u540e\uff0c\u5e76\u6267\u884c\u65b0\u7684\u4efb\u52a1\u3002<\/p>\n

 switch_to<\/span>(<\/span>prev,<\/span> next,<\/span> prev)<\/span>;<\/span> --<\/span>><\/span>__switch_to_asm<\/span>(<\/span>(<\/span>prev)<\/span>,<\/span> (<\/span>next)<\/span>)<\/span>)<\/span> --<\/span>><\/span>ENTRY<\/span>(<\/span>__switch_to_asm)<\/span> \/* switch stack *\/<\/span> movq %<\/span>rsp,<\/span> TASK_threadsp<\/span>(<\/span>%<\/span>rdi)<\/span> movq TASK_threadsp<\/span>(<\/span>%<\/span>rsi)<\/span>,<\/span> %<\/span>rsp jmp __switch_to END<\/span>(<\/span>__switch_to_asm)<\/span> --<\/span>><\/span>__switch_to<\/span>(<\/span>)<\/span> <\/code><\/pre>\n

(4) \u5b8c\u6210\u4e00\u4e9b\u6e05\u7406\u5de5\u4f5c\uff0c\u4f7f\u5176\u80fd\u591f\u6b63\u786e\u7684\u91ca\u653e\u9501\u3002\u8fd9\u4e2a\u6e05\u7406\u5de5\u4f5c\u7684\u5b8c\u6210\u662f\u7b2c\u4e09\u4e2a\u4efb\u52a1\uff0c\u7cfb\u7edf\u4e2d\u968f\u673a\u7684\u67d0\u4e2a\u5176\u5b83\u4efb\u52a1\u3002\u540c\u65f6\u901a\u77e5\u6211\u4eec\u4efb\u52a1\u88ab\u91cd\u65b0\u8c03\u5ea6\uff08sched_in\uff09\u3002
\u8fd9\u91cc\u5176\u5b9e\u4e5f\u6709\u70b9\u590d\u6742\uff0c\u6211\u4eec\u540e\u9762\u5728 context_switch \u7bc7\u91cd\u70b9\u63cf\u8ff0\u3002<\/p>\n

 finish_task_switch<\/span>(<\/span>prev)<\/span> --<\/span>><\/span>fire_sched_in_preempt_notifiers<\/span>(<\/span>current)<\/span>;<\/span> --<\/span>><\/span>__fire_sched_in_preempt_notifiers<\/span>(<\/span>curr)<\/span> --<\/span>><\/span>hlist_for_each_entry<\/span>(<\/span>notifier,<\/span> &<\/span>curr-><\/span>preempt_notifiers,<\/span> link)<\/span> notifier-><\/span>ops-><\/span>sched_in<\/span>(<\/span>notifier,<\/span> raw_smp_processor_id<\/span>(<\/span>)<\/span>)<\/span>;<\/span> <\/code><\/pre>\n

\u603b\u7ed3<\/h2>\n

\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u4e86schdule\u4e3b\u8c03\u5ea6\u5668\u7684\u5de5\u4f5c\u6d41\u7a0b\uff0c\u5373\u4e00\u4e2a\u8fd0\u884c\u4e2d\u7684\u8fdb\u7a0b\u4e3b\u52a8\u8c03\u7528 _schedule \u8ba9\u51fa CPU\uff0c\u603b\u7ed3\u6765\u8bf4\u5c31\u662f\uff1a
\uff081\uff09\u901a\u8fc7\u8c03\u5ea6\u7b97\u6cd5\u9009\u62e9\u4e00\u4e2a\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u4efb\u52a1\u3002
\uff082\uff09\u8fdb\u884c\u4efb\u52a1\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u4e0a\u4e0b\u6587\u5207\u6362\u53c8\u5206\u7528\u6237\u6001\u8fdb\u7a0b\u7a7a\u95f4\u7684\u5207\u6362\u548c\u5185\u6838\u6001\u7684\u5207\u6362\u3002
\u4fdd\u5b58\u539f\u4efb\u52a1\u7684\u6240\u6709\u5bc4\u5b58\u5668\u4fe1\u606f\uff0c\u6062\u590d\u65b0\u4efb\u52a1\u7684\u6240\u6709\u5bc4\u5b58\u5668\u4fe1\u606f\uff0c\u5e76\u6267\u884c\u65b0\u7684\u4efb\u52a1\u3002
\uff083\uff09\u4e0a\u4e0b\u6587\u5207\u6362\u5b8c\u540e\uff0c\u65b0\u7684\u4efb\u52a1\u6295\u5165\u8fd0\u884c\u3002
\u5982\u4e0b\u56fe\u6240\u793a\uff1a
\"linux\u8fdb\u7a0b\u8c03\u5ea6\u91c7\u7528\u7684\u662f\u4ec0\u4e48\u8c03\u5ea6\u65b9\u5f0f_linux\u7ebf\u7a0b\u8c03\u5ea6\u7b56\u7565
\u56fe\u7247\u6765\u81ea\u4e8e\u6781\u5ba2\u65f6\u95f4\uff1a\u8da3\u8c08Linux\u64cd\u4f5c\u7cfb\u7edf<\/p>\n

\u53c2\u8003\u8d44\u6599<\/h2>\n

Linux\u5185\u6838\u6e90\u7801 4.10.0<\/p>\n

\u6781\u5ba2\u65f6\u95f4\uff1a\u8da3\u8c08Linux\u64cd\u4f5c\u7cfb\u7edf
Linux \u8fdb\u7a0b\u8c03\u5ea6\u4e4bschdule\u4e3b\u8c03\u5ea6\u5668
Linux \u8fdb\u7a0b\u8c03\u5ea6\u4e4bschdule\u4e3b\u8c03\u5ea6\u5668
https:\/\/mp.weixin..com\/s\/L06_XqWk2pA9vMTVGiHFbg<\/p>\n","protected":false},"excerpt":{"rendered":"linux\u8fdb\u7a0b\u8c03\u5ea6\u91c7\u7528\u7684\u662f\u4ec0\u4e48\u8c03\u5ea6\u65b9\u5f0f_linux\u7ebf\u7a0b\u8c03\u5ea6\u7b56\u7565\u8be5\u6587\u7ae0\u63cf\u8ff0\u4e86\u8fdb\u7a0b\u8c03\u5ea6\u6240\u9700\u8981\u505a\u7684\u4e00\u4e9b\u4e8b\u60c5\uff0c\u4e3b\u8981\u5206\u6790\u4e86schdule\u51fd\u6570\u7684\u91cd...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6855"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6855"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6855\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}