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

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

略称コンテキストhardware 割り込み状態説明
user spaceuser space で実行中

running a process in user space
許可user space で process を実行している状態です。user space は仮想アドレス空間の構成でアクセスを許可された共有空間を除いて「異なる process は互いのアクセスが出来ない」、「kernel で使用している空間にアクセスできない」ように保護された空間です。身近な例として shell から起動した /bin, /usr/bin, /sbin, /usr/sbin に格納されている実行ファイルに格納されたコードを実行している空間です(もちろん shell それ自身の実行も含まれます)。system call、例外(trap)、プリエンプション、ハードウエア割り込みによって kernel 内の実行(次の user context)に移ります。
user contextuser space と関連した kernel 内実行

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

running in kernel space, not associated with a process
許可user space の process と関連していない kernel 内の処理です。kernel thread (/include/linux/kthread.h) 実行中、work queue (/include/linux/workqueue.h) に登録した関数を実行している最中です。work queue も __alloc_workqueue_key() にて kthread で実装されています。user space の process と関連が無く仮想アドレス空間に何らかの(あるいは特定可能な) user space process が占めている領域が無いので user space の process と情報は交換できません。
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
Last-modified: 2017-11-03 (金) 22:05:48 (2368d)