gvisor/pkg/sentry/kernel
Nicolas Lacasse 4e6f0892c9 runsc: Support job control signals for the root container.
Now containers run with "docker run -it" support control characters like ^C and
^Z.

This required refactoring our signal handling a bit. Signals delivered to the
"runsc boot" process are turned into loader.Signal calls with the appropriate
delivery mode. Previously they were always sent directly to PID 1.

PiperOrigin-RevId: 217566770
Change-Id: I5b7220d9a0f2b591a56335479454a200c6de8732
2018-10-17 12:29:05 -07:00
..
auth Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
epoll sentry: make epoll.pollEntry wait for the file operation in restore. 2018-08-07 10:27:37 -07:00
eventfd Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
fasync Fix panic if FIOASYNC callback is registered and triggered without target 2018-10-03 20:22:31 -07:00
futex Implement shared futexes. 2018-10-08 10:20:38 -07:00
g3doc Use png for the run states diagram 2018-05-02 03:43:41 -07:00
kdefs stateify: support explicit annotation mode; convert refs and stack packages. 2018-07-27 10:17:21 -07:00
memevent Extend memory usage events to report mapped memory usage. 2018-09-13 15:16:47 -07:00
pipe Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
sched stateify: support explicit annotation mode; convert refs and stack packages. 2018-07-27 10:17:21 -07:00
semaphore Distinguish Element and Linker for ilist. 2018-09-04 09:19:11 -07:00
shm Add /proc/sys/kernel/shm[all,max,mni]. 2018-08-27 17:21:37 -07:00
time Implement POSIX per-process interval timers. 2018-08-23 16:32:36 -07:00
BUILD Move Unix transport out of netstack 2018-10-17 11:37:51 -07:00
README.md Format documentation 2018-07-12 10:37:21 -07:00
abstract_socket_namespace.go Move Unix transport out of netstack 2018-10-17 11:37:51 -07:00
context.go Check in gVisor. 2018-04-28 01:44:26 -04:00
fd_map.go Fix missing O_LARGEFILE from O_CREAT files 2018-08-09 16:50:37 -07:00
fd_map_test.go Fix format string type in test 2018-05-08 09:39:42 -07:00
fs_context.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
ipc_namespace.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
kernel.go runsc: Support job control signals for the root container. 2018-10-17 12:29:05 -07:00
kernel_state.go netstack: tcp socket connected state S/R support. 2018-07-10 09:23:35 -07:00
pending_signals.go Implement POSIX per-process interval timers. 2018-08-23 16:32:36 -07:00
pending_signals_state.go Implement POSIX per-process interval timers. 2018-08-23 16:32:36 -07:00
posixtimer.go Implement POSIX per-process interval timers. 2018-08-23 16:32:36 -07:00
ptrace.go Fix PTRACE_GETREGSET write size 2018-10-17 11:53:04 -07:00
rseq.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
seccomp.go Add seccomp(2) support. 2018-08-02 08:10:30 -07:00
sessions.go When creating a new process group, add it to the session. 2018-10-10 10:42:11 -07:00
signal.go Drop return from SendExternalSignal 2018-06-21 14:53:55 -07:00
signal_handlers.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
syscalls.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
syscalls_state.go Check in gVisor. 2018-04-28 01:44:26 -04:00
syslog.go Add 'Starting gVisor...' message to syslog 2018-09-07 16:59:27 -07:00
table_test.go Check in gVisor. 2018-04-28 01:44:26 -04:00
task.go Merge host.endpoint into host.ConnectedEndpoint 2018-10-15 17:48:11 -07:00
task_acct.go Check in gVisor. 2018-04-28 01:44:26 -04:00
task_block.go Allow kernel.(*Task).Block to accept an extract only channel 2018-09-17 13:35:54 -07:00
task_clone.go Implement shared futexes. 2018-10-08 10:20:38 -07:00
task_context.go Implement shared futexes. 2018-10-08 10:20:38 -07:00
task_exec.go Disintegrate kernel.TaskResources. 2018-08-31 13:58:04 -07:00
task_exit.go Implement shared futexes. 2018-10-08 10:20:38 -07:00
task_futex.go Implement shared futexes. 2018-10-08 10:20:38 -07:00
task_identity.go kernel: mutations on creds now require a copy. 2018-07-19 15:48:56 -07:00
task_log.go Disintegrate kernel.TaskResources. 2018-08-31 13:58:04 -07:00
task_net.go Check in gVisor. 2018-04-28 01:44:26 -04:00
task_run.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
task_sched.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
task_signals.go Fix interaction between rt_sigtimedwait and ignored signals. 2018-09-14 11:10:50 -07:00
task_start.go Implement 'runsc kill --all' 2018-09-27 15:00:58 -07:00
task_stop.go Check in gVisor. 2018-04-28 01:44:26 -04:00
task_syscall.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
task_test.go Check in gVisor. 2018-04-28 01:44:26 -04:00
task_usermem.go Call mm.CheckIORange() when copying in IOVecs. 2018-06-27 14:31:35 -07:00
thread_group.go Implement POSIX per-process interval timers. 2018-08-23 16:32:36 -07:00
threads.go Hold TaskSet.mu in Task.Parent. 2018-08-07 13:09:42 -07:00
timekeeper.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
timekeeper_state.go sentry: wait for restore clock instead of panicing in Timekeeper. 2018-07-12 15:09:02 -07:00
timekeeper_test.go sentry: wait for restore clock instead of panicing in Timekeeper. 2018-07-12 15:09:02 -07:00
timer.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
uts_namespace.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
vdso.go Automated rollback of changelist 207037226 2018-08-02 10:42:48 -07:00
version.go Check in gVisor. 2018-04-28 01:44:26 -04:00

README.md

This package contains:

  • A (partial) emulation of the "core Linux kernel", which governs task execution and scheduling, system call dispatch, and signal handling. See below for details.

  • The top-level interface for the sentry's Linux kernel emulation in general, used by the main function of all versions of the sentry. This interface revolves around the Env type (defined in kernel.go).

Background

In Linux, each schedulable context is referred to interchangeably as a "task" or "thread". Tasks can be divided into userspace and kernel tasks. In the sentry, scheduling is managed by the Go runtime, so each schedulable context is a goroutine; only "userspace" (application) contexts are referred to as tasks, and represented by Task objects. (From this point forward, "task" refers to the sentry's notion of a task unless otherwise specified.)

At a high level, Linux application threads can be thought of as repeating a "run loop":

  • Some amount of application code is executed in userspace.

  • A trap (explicit syscall invocation, hardware interrupt or exception, etc.) causes control flow to switch to the kernel.

  • Some amount of kernel code is executed in kernelspace, e.g. to handle the cause of the trap.

  • The kernel "returns from the trap" into application code.

Analogously, each task in the sentry is associated with a task goroutine that executes that task's run loop (Task.run in task_run.go). However, the sentry's task run loop differs in structure in order to support saving execution state to, and resuming execution from, checkpoints.

While in kernelspace, a Linux thread can be descheduled (cease execution) in a variety of ways:

  • It can yield or be preempted, becoming temporarily descheduled but still runnable. At present, the sentry delegates scheduling of runnable threads to the Go runtime.

  • It can exit, becoming permanently descheduled. The sentry's equivalent is returning from Task.run, terminating the task goroutine.

  • It can enter interruptible sleep, a state in which it can be woken by a caller-defined wakeup or the receipt of a signal. In the sentry, interruptible sleep (which is ambiguously referred to as blocking) is implemented by making all events that can end blocking (including signal notifications) communicated via Go channels and using select to multiplex wakeup sources; see task_block.go.

  • It can enter uninterruptible sleep, a state in which it can only be woken by a caller-defined wakeup. Killable sleep is a closely related variant in which the task can also be woken by SIGKILL. (These definitions also include Linux's "group-stopped" (TASK_STOPPED) and "ptrace-stopped" (TASK_TRACED) states.)

To maximize compatibility with Linux, sentry checkpointing appears as a spurious signal-delivery interrupt on all tasks; interrupted system calls return EINTR or are automatically restarted as usual. However, these semantics require that uninterruptible and killable sleeps do not appear to be interrupted. In other words, the state of the task, including its progress through the interrupted operation, must be preserved by checkpointing. For many such sleeps, the wakeup condition is application-controlled, making it infeasible to wait for the sleep to end before checkpointing. Instead, we must support checkpointing progress through sleeping operations.

Implementation

We break the task's control flow graph into states, delimited by:

  1. Points where uninterruptible and killable sleeps may occur. For example, there exists a state boundary between signal dequeueing and signal delivery because there may be an intervening ptrace signal-delivery-stop.

  2. Points where sleep-induced branches may "rejoin" normal execution. For example, the syscall exit state exists because it can be reached immediately following a synchronous syscall, or after a task that is sleeping in execve() or vfork() resumes execution.

  3. Points containing large branches. This is strictly for organizational purposes. For example, the state that processes interrupt-signaled conditions is kept separate from the main "app" state to reduce the size of the latter.

  4. SyscallReinvoke, which does not correspond to anything in Linux, and exists solely to serve the autosave feature.

dot -Tpng -Goverlap=false -orun_states.png run_states.dot

States before which a stop may occur are represented as implementations of the taskRunState interface named run(state), allowing them to be saved and restored. States that cannot be immediately preceded by a stop are simply Task methods named do(state).

Conditions that can require task goroutines to cease execution for unknown lengths of time are called stops. Stops are divided into internal stops, which are stops whose start and end conditions are implemented within the sentry, and external stops, which are stops whose start and end conditions are not known to the sentry. Hence all uninterruptible and killable sleeps are internal stops, and the existence of a pending checkpoint operation is an external stop. Internal stops are reified into instances of the TaskStop type, while external stops are merely counted. The task run loop alternates between checking for stops and advancing the task's state. This allows checkpointing to hold tasks in a stopped state while waiting for all tasks in the system to stop.