Kernel 内外のコンテキスト(Players inside/outside kernel)

Documentation/DocBook/kernel-hacking/basic-players.html に Linux において CPU が実行しているコンテキストについて書かれています。Documentation/DocBook/kernel-hacking/basic-players.html ではコンテキストを Players と呼んでいます。次の表は context (players) についてまとめたものです。

略称コンテキスト説明
User Space 実行中

running a process in user space
Userland のアプリケーションやコマンドを実行している状態です。system call、例外(trap)、プリエンプション、ハードウエア割り込みによって kernel 内の実行に移ります。kernel 内のコードは User 空間で実行されることはありません。
user contextUser Space と関連した kernel 内実行

running in kernel space, associated with a process (user context)
Userland のプロセスと関連した kernel 内の処理です。system call または例外によって kernel に実行が移った状態です。デバイスドライバを開発する立場であれば system call がきっかけで kernel 内に実行が移った状況だと考えて良いでしょう。system call の open(), close(), read(), write(), ioctl(), lseek(), mmap(), poll() などです。read(), write(), ioctl() system call に対応する処理で copy_from_user()copy_to_user() などでユーザーランドのアプリケーションと情報を交換できる状況です。
kthread contextUser Space と関連しない kernel 内実行

running in kernel space, not associated with a process
Userland のプロセスと関連していない kernel 内の処理です。kernel thread (/include/linux/kthread.h) 実行中、work queue (/include/linux/workqueue.h) に登録した関数を実行している最中です。work queue も __alloc_workqueue_key() にて kthread で実装されています。User Space と関連が無いのでユーザーランドのアプリケーションと情報は交換できません。
softirq contextsoftirq 実行中

not associated with any process,serving a softirq, tasklet, or timer
softirqソフトウエア割り込み処理中です。softirq が実行される状況複雑なので softirq 処理はいつ行われるか に別記しました。
softirq, tasklet, timer は bottom halves (単数形の bottom half が使われたり、省略されて bh) と総称されています。tasklet, timer もsoftirq にて実装されています(TASKLET_SOFTIRQ, TIMER_SOFTIRQ)。
Linux Kernel のドキュメントでは User Space と関連しない kernel 内実行(not associated with any process) と書かれています。このことを確かめるため softirq 処理の中から (task_struct*)current->comm を読むと何らかの process 名が入っています。多くの状況でハードウエア割り込みの最後の処理として softirq は実行されています。割り込みが発生した時点でたまたま実行していたコンテキスト上で 何か別のコンテキストと選べない・予測できない関連があると言うのが正しいです。
割り込み処理と同様に sleep できない制限があります。
tasklettasklet (/include/linux/interrupt.h) は softirq で実装されています。ソフトウエア割り込み番号は TASKLET_SOFTIRQ で処理関数は tasklet_action() です。
timertimer (/include/linux/timer.h) は softirq で実装されています。ソフトウエア割り込み番号は TIMER_SOFTIRQ で処理関数は run_timer_softirq() です。
interrupt contexthardware 割り込み実行中

not associated with any process, serving a hardware interrupt

Linux kernel 特有のフレーズ bottom halves の使われ方について

kernel API として bottom halves の範囲を定義しているのは spin_lock_bh(), spin_unlock_bh() とこれから派生・これに類似する API です。softirq (ソフトウエア割り込み)を禁止/解除する。という動作に対して接尾辞 _bh を付けています。
一方で「割り込み処理のうち後半処理全般」を指す意味として使われることがあります。irq handler を 対語の様に top halves と呼び irq handler から起床された何らかの context を全て総称するものとして使われます。ここでは「後半処理全般」を指すフレーズとして bottom halves は使いません。


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS