gvisor/pkg/sentry/kernel
Andrei Vagin 9fff9469a2 kernel: print PID in addition to TID in task log messages
For multithreads processes, it is hard to read logs without knowing task pids.

And let's print a decimal return codeo for syscalls. A hex return code are
usefull for system calls that return addresses. For other syscalls, the decimal
form is more readable.

PiperOrigin-RevId: 400035449
2021-09-30 15:07:28 -07:00
..
auth [syserror] Remove pkg syserror. 2021-08-13 17:16:52 -07:00
contexttest Update package locations. 2020-01-27 15:31:32 -08:00
epoll Add POLLRDNORM/POLLWRNORM support. 2021-03-24 12:11:44 -07:00
eventfd [syserror] Remove pkg syserror. 2021-08-13 17:16:52 -07:00
fasync [syserror] Change syserror to linuxerr for E2BIG, EADDRINUSE, and EINVAL 2021-06-29 15:08:46 -07:00
futex [syserror] Remove pkg syserror. 2021-08-13 17:16:52 -07:00
g3doc Use png for the run states diagram 2018-05-02 03:43:41 -07:00
ipc Implement ipc.Object.Set and use it in ipc mechanisms. 2021-08-17 20:31:38 +02:00
memevent Standardize on tools directory. 2020-01-27 12:21:00 -08:00
msgqueue Fix race on msgrcv(MSG_COPY). 2021-09-14 16:53:30 -07:00
pipe [syserror] Remove pkg syserror. 2021-08-13 17:16:52 -07:00
sched
semaphore Merge pull request #6262 from sudo-sturbia:msgqueue/syscalls3 2021-08-17 17:44:26 -07:00
shm Merge pull request #6262 from sudo-sturbia:msgqueue/syscalls3 2021-08-17 17:44:26 -07:00
signalfd [syserror] Remove pkg syserror. 2021-08-13 17:16:52 -07:00
time [syserror] Change syserror to linuxerr for E2BIG, EADDRINUSE, and EINVAL 2021-06-29 15:08:46 -07:00
BUILD Add //pkg/sentry/seccheck. 2021-09-03 19:12:54 -07:00
README.md
abstract_socket_namespace.go Fix deadlock in procfs 2021-07-12 18:30:46 -07:00
aio.go Check for misuse of kernel.Task as context.Context. 2020-11-13 14:47:47 -08:00
cgroup.go Initial cgroupfs support for subcontainers 2021-08-11 17:21:37 -07:00
context.go
fd_table.go Merge pull request #6257 from zhlhahaha:2193-1 2021-07-30 14:43:13 -07:00
fd_table_test.go
fd_table_unsafe.go Merge pull request #6257 from zhlhahaha:2193-1 2021-07-30 14:43:13 -07:00
fs_context.go
ipc_namespace.go Create package msgqueue. 2021-07-13 22:12:02 +02:00
kcov.go [syserror] Update several syserror errors to linuxerr equivalents. 2021-07-01 12:05:19 -07:00
kcov_unsafe.go Fix reference counting on kcov mappings. 2020-10-19 18:09:39 -07:00
kernel.go Replace kernel.ExitStatus with linux.WaitStatus. 2021-07-08 13:39:15 -07:00
kernel_opts.go Add go:build directives as required by Go 1.17's gofmt. 2021-07-20 16:28:45 -07:00
kernel_state.go
pending_signals.go
pending_signals_state.go [op] Move SignalInfo to abi/linux package. 2021-06-10 10:26:36 -07:00
posixtimer.go [syserror] Update several syserror errors to linuxerr equivalents. 2021-07-01 12:05:19 -07:00
ptrace.go [syserror] Convert remaining syserror definitions to linuxerr. 2021-08-12 15:19:12 -07:00
ptrace_amd64.go [syserror] Convert remaining syserror definitions to linuxerr. 2021-08-12 15:19:12 -07:00
ptrace_arm64.go [syserror] Convert remaining syserror definitions to linuxerr. 2021-08-12 15:19:12 -07:00
rseq.go [syserror] Update syserror to linuxerr for more errors. 2021-07-12 15:26:20 -07:00
seccomp.go [syserror] Convert remaining syserror definitions to linuxerr. 2021-08-12 15:19:12 -07:00
sessions.go [syserror] Update syserror to linuxerr for more errors. 2021-07-12 15:26:20 -07:00
signal.go
signal_handlers.go
syscalls.go
syscalls_state.go
syslog.go Add a few syslog messages. 2020-11-18 11:46:23 -08:00
table_test.go
task.go Update the comment for Task.netns 2021-09-24 11:25:13 -07:00
task_acct.go [syserror] Change syserror to linuxerr for E2BIG, EADDRINUSE, and EINVAL 2021-06-29 15:08:46 -07:00
task_block.go [syserror] Remove pkg syserror. 2021-08-13 17:16:52 -07:00
task_cgroup.go Drop unnecessary checklocksignore. 2021-07-09 16:00:25 -07:00
task_clone.go kernel: allow to access Task.netns without taking Task.mu 2021-09-23 18:28:08 -07:00
task_context.go
task_exec.go Fix lock order violations: mm.mappingMu > Task.mu. 2021-08-27 13:18:49 -07:00
task_exit.go Fix lock order violations: mm.mappingMu > Task.mu. 2021-08-27 13:18:49 -07:00
task_futex.go
task_identity.go [syserror] Update syserror to linuxerr for EACCES, EBADF, and EPERM. 2021-06-30 08:18:59 -07:00
task_image.go Fix lock order violations: mm.mappingMu > Task.mu. 2021-08-27 13:18:49 -07:00
task_log.go kernel: print PID in addition to TID in task log messages 2021-09-30 15:07:28 -07:00
task_net.go kernel: allow to access Task.netns without taking Task.mu 2021-09-23 18:28:08 -07:00
task_run.go [syserror] Remove pkg syserror. 2021-08-13 17:16:52 -07:00
task_sched.go [syserror] Change syserror to linuxerr for E2BIG, EADDRINUSE, and EINVAL 2021-06-29 15:08:46 -07:00
task_signals.go [syserror] Remove pkg syserror. 2021-08-13 17:16:52 -07:00
task_start.go kernel: allow to access Task.netns without taking Task.mu 2021-09-23 18:28:08 -07:00
task_stop.go
task_syscall.go [syserror] Remove pkg syserror. 2021-08-13 17:16:52 -07:00
task_test.go
task_usermem.go [syserror] Convert remaining syserror definitions to linuxerr. 2021-08-12 15:19:12 -07:00
task_work.go
thread_group.go Merge pull request #6438 from gystemd:tcsetpgrp_SIGTTOU 2021-08-24 16:43:51 -07:00
threads.go
timekeeper.go Move tcpip.Clock impl to Timekeeper 2021-06-17 14:02:05 -07:00
timekeeper_state.go
timekeeper_test.go [syserror] Change syserror to linuxerr for E2BIG, EADDRINUSE, and EINVAL 2021-06-29 15:08:46 -07:00
tty.go Fix "unlock of unlocked mutex" crash when getting tty 2020-01-15 13:00:59 +08:00
uncaught_signal.proto
uts_namespace.go
vdso.go
version.go Change copyright notice to "The gVisor Authors" 2019-04-29 14:26:23 -07: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.