117 Commits
Author SHA1 Message Date
Jing ChenandgVisor bot b6e233b071 Fix ptrace dumpability bypass check on exit.
Fix #13226

PiperOrigin-RevId: 949272248
2026-07-16 17:48:25 -07:00
Shailend ChandandgVisor bot e3b85191b4 Support cgroup namespaces
cgroup namespaces are non hierarchical and they are defined primarily by a
"root" cgroup node. The init cgroup namespaces's root node is the true root of
the (only) cgroup2fs tree. The root node of other cgroup namespaces is the
cgroup node of the task at the time of namespace creation.

Residing in a cgroup namespace affects tasks this way:

1) The views into /proc/[pid]/cgroup and the "root" field under
   /proc/[pid]/mountinfo are virtualized: they are rendered relative to the
   reading task's cgroupns.

2) mount()ing a cgroup2 fs furnishes only a subtree of the full cgroup2fs
   tree, rooted at the "root" cgroup of the hosting cgroupns.

Fixes #11989

PiperOrigin-RevId: 948882249
2026-07-16 04:04:38 -07:00
Ryan El KochtaandgVisor bot 2b543c1db5 Stub support for /proc/[pid]/coredump_filter
The value has no effect, but it is stored and inherited across fork().
Unlike Linux, our default value is 0.

Elasticsearch >=v9.3.0 requires this.

PiperOrigin-RevId: 944228419
2026-07-07 19:54:23 -07:00
Shailend ChandandgVisor bot dde347d9fc Add initial cgroup2 support.
The cgroup v2 fs is initialized early in the boot process, so that every task
in the sandbox resides in at least the root v2 cgroup. A new runsc flag
--mount-cgroup-v2 has been added: this controls whether runsc mounts a
cgroupv2 filesystem at /sys/fs/cgroup automatically (currently it always
mounts a v1 hierarchy at this path). This flag also gates whether userspace
can mount a cgroup2 fs. When this flag is false, which is the default, all
tasks in the sandbox reside forever in the root cgroup.

Like in Linux, the v2 hierarchy is deemed the default owner of a controller,
that is, until the corresponding v1 hierarchy is mounted: then, the v1 tree
steals ownership of that controller and it remains this way till as long as
a v1 mount exists. In other words, hybrid mode is supported, even if such
workloads are rare these days.

The pids controller is the only controller implemented in full: it serves
as validation for the unified hierarchy. The memory controller can be used
to learn the usage stats of a subtree: but like the current v1 implementation,
and unlike Linux, it doesn't achieve this through charging up the hierarchy
during allocation, instead it queries pgalloc for every active controller's
id. And it does no enforcing. The cpu and cpuset controllers are mere stubs,
like in the current v1 implementation.

The following are supported:
* General matters of topology: cgroup.subtree_control, cgroup.controllers,
                               cgroup.procs etc
* CLONE_INTO_CGROUP
* cgroup.kill
* cgroup.events
* A working pids controller and its events files
* A minimal memory controller that just reports mem usage
* Stubbed cpu and cpuset controllers

The following are NOT supported:
* threaded cgroups
* cgroup namespaces
* BPF_PROG_TYPE_CGROUP_DEVICE
* cgroup.freeze

Updates #13007

PiperOrigin-RevId: 943519748
2026-07-06 15:17:54 -07:00
Ryan El KochtaandgVisor bot 6d0da34f53 stub implementations for schedattr and ioprio syscalls
Adds stub implementations for:
- sched_setattr(2)
- sched_getattr(2)
- ioprio_set(2)
- ioprio_get(2)

"Support" (with no effect) is included for SCHED_OTHER (referred to in Linux
as SCHED_NORMAL), SCHED_BATCH, and SCHED_IDLE. sched_getscheduler(2) and
sched_setscheduler(2) are updated to support all three as well.

For ioprio_set, the configured I/O priority has no effect.

PiperOrigin-RevId: 938270335
2026-06-25 17:07:37 -07:00
Etienne PerotandgVisor bot 6b795d59fe Implement personality(2) for no-op personality bits.
PiperOrigin-RevId: 928825123
2026-06-08 16:13:59 -07:00
Shailend ChandandgVisor bot 329cdabbf5 Implement pidfds
This patch introduces pidfd support roughly up to Linux 6.8. Linux selftests
at this version pass, and are now run as an //image_test target. Some features,
like PIDFD_THREAD, that occur later than 6.8 in Linux, are included.

It includes:
- These new syscalls:
  - pidfd_open()
  - pidfd_getfd()
  - pidfd_send_signal()
- New aspects to these existing syscalls:
  - waitid() in its support of P_PIDFD
  - clone()/clone3() in their support of CLONE_PIDFD
  - poll()/epoll()/select() in their support for pidfds
  - setns() in its support for pidfds
  - pid introspection via /proc/[pid]/fdinfo/[pidfd]

It excludes:
- pidfdfs: pidfds are backed by the anon inode right now
- ioctls to retrieve namespace fds using a pidfd
- process_madvise()

Updates #5839.

PiperOrigin-RevId: 908341658
2026-04-30 13:22:58 -07:00
Shailend ChandandgVisor bot 1b546ce933 Fix race between unshare(2) and execve(2).
There are two issues being fixed: 1) checkAndPreventSharingOutsideTG() was
accessing t.fsContext without grabbing t.mu and thus causing a data race with
a concurrent unshare and 2) the concurrent unshare might present an incomplete
view of the shared fscontext and cause execve to incorrectly conclude that it
was shared outside the thread group.

Reported-by: [email protected]
Reported-by: [email protected]
PiperOrigin-RevId: 800327461
2025-08-27 23:52:21 -07:00
Shailend ChandandgVisor bot 2c6185b9d1 Support privileged executables inside the sandbox
There is already some support for privileged executables before this change:
file capabilities were taken into account when creating new processes inside
the sandbox from external instigation (but not during execve(2)). set-user-id
and set-group-id bits were never taken into account, and the Sentry behaved
as if NO_NEW_PRIVS is always true. After this change, execve(2) and the
external Exec entry point both take the set-user(group)-id bits and file
capabilities into account.

Because NO_NEW_PRIVS can no longer be assumed to be always set, a couple of
extra checks to prevent privilege elevation during execve(2) are needed:
the presence of a ptracer and the use of a shared fsContext. Moreover,
execve had to be serialized with seccomp(2) when it is syncing filters to
all threads in a group. Serializing execve with PTRACE_ATTACH and seccomp
required some Task runState gymnastics.

Task dumpability is reset when privilege elevation takes place and privilege
elevation is denied for binaries living on nosuid mounts. To fix an old bug
where some aux vectors were populated using data from the old pre-execve
creds, the computation of the (file-privilege-augmented) creds during execve
now takes place before the stack for the new program is set up.

gvisor users who thus far have exec'd into the sandbox with UID as (sandbox)
root will lose the caps an all-powerful namespace root'd have if: 1) the binary
is a suid/guid binary 2) the owner of the binary is host root and there is
no mapping for host root in the host user namespace of the sandbox. To avoid
this surprise, the suid/sgid bits are ignored by default. Users can opt-in
via the --allow-suid flag.

One result of this change is that sudo now works inside the sandbox.

PiperOrigin-RevId: 794414101
2025-08-12 23:07:38 -07:00
Jamie LiuandgVisor bot c38bbe4dee sentry/kernel: don't use Task.mu to protect Task.creds
Task.creds is documented as being "owned by the task goroutine", i.e. it can
only be mutated by the task goroutine, precluding the need for writer
synchronization. (AFAIK, no current Linux feature permits one task to change
another's credentials; capset(2) did before the introduction of VFS
capabilities, but gVisor doesn't support this. Libcs implement functions like
setresuid() by signaling every thread to execute the setresuid() syscall to
change their own credentials.) Prior to cl/185803179 and cl/205315612, the
mutex was needed to avoid data races on auth.Credentials fields; then prior to
cl/254989492, the mutex was needed to avoid data races on the auth.Credentials
pointer; now the mutex is not needed at all.

PiperOrigin-RevId: 777692758
2025-06-30 13:21:50 -07:00
Nicolas LacasseandgVisor bot 0179221ee4 Protect Task.ptraceSeized with TaskSet mutex and Signal mutex.
Previously Task.ptraceSeized was protected only by TaskSet mutex, but some
pathways incorrectly modified the field without holding the lock, leading to
data races.

This CL changes the locking so that TaskSet and Signal mutexes are required to
write Task.ptraceSeized, but only one of them must be held to read
Task.ptraceSeized.

Reported-by: [email protected]
PiperOrigin-RevId: 742317413
2025-03-31 10:51:35 -07:00
Andrei VaginandgVisor bot 03044d7bea proc: invalidate task inodes when tasks are destroyed
PiperOrigin-RevId: 705785809
2024-12-13 00:58:08 -08:00
Jamie LiuandgVisor bot b58ec99982 kernel: drive all CPU timers in CPU clock ticker
gVisor currently implements CPU clocks as follows:

- A per-sentry "CPU clock ticker goroutine"
  (task_sched.go:Kernel.runCPUClockTicker()) periodically advances
  Kernel.cpuClock, causing it to serve as a very coarse but inexpensive
  monotonic wall clock (that happens to be suspended when no tasks are
  running).

- Task goroutines observe the most recent value of Kernel.cpuClock when
  changing state (Task.gosched.Timestamp), and use it to compute the number of
  CPU clock ticks that have elapsed in a given state. Thus, task CPU clocks are
  approximately based on the wall time during which they were marked as
  running.

- ITIMER_VIRTUAL, ITIMER_PROF, and RLIMIT_CPU are checked by the CPU clock
  ticker goroutine after advancing Kernel.cpuClock. POSIX interval timers and
  timerfds check CPU clocks (taskClock/tgClock) in ktime.SampledTimer
  goroutines.

This has three major problems:

- ktime.SampledTimer goroutines for CPU clock timers run concurrently with the
  CPU clock ticker, and are not informed as to when corresponding tasks start
  or stop running (due to overhead on the task execution critical path), so
  they can't determine when CPU clocks have/will advance; instead, they simply
  poll CPU clocks on a period equal to that of the represented timer, resulting
  in significant overhead for CPU-clock-based POSIX interval timers and
  timerfds.

- For the same reason, CPU clock interval timers and timerfds may expire much
  later than when the CPU clock is actually incremented; in the interval timer
  case, this can result in notification signals being sent long after tasks
  have stopped running. (This is the same problem as in b/116538398, which
  motivated the special-casing of ITIMER_VIRTUAL and ITIMER_PROF described
  above, but applied to POSIX interval timers.)

- The sentry does not impose a limit on the number of tasks that may be
  concurrently marked running, so if more tasks are marked running than the
  number of CPUs advertised to applications, application CPU utilization can
  appear to exceed 100%.

This CL fixes these problems by introducing explicit per-Task and ThreadGroup
CPU clocks, directly advancing (up to Kernel.applicationCores of) them in the
CPU clock ticker, and directly expiring CPU timers when doing so. Itimer and
RLIMIT_CPU timers lose their special-casing and instead behave like other CPU
timers (see task_acct.go). Kernel.cpuClock is still required, but only for the
sentry watchdog.

Minor cleanup changes:

- Gather all stateify hooks in kernel_state.go.

- Replace kernel.randInt31n() with math/rand/v2, which fixes the same problem
  (https://go.dev/blog/randv2#problem.rand).

Test workload:

```
#include <err.h>
#include <signal.h>
#include <time.h>
#include <chrono>
#include <thread>

constexpr int kNumTimers = 1000;
constexpr long kTimerPeriodNS = 10000000;

int main(int argc, char** argv) {
  for (int i = 0; i < kNumTimers; i++) {
    struct sigevent sev = {.sigev_notify = SIGEV_NONE};
    timer_t timerid;
    if (timer_create(CLOCK_THREAD_CPUTIME_ID, &sev, &timerid) < 0) {
      err(1, "timer_create failed");
    }
    struct itimerspec it = {
      .it_interval = {0, kTimerPeriodNS},
      .it_value = {0, kTimerPeriodNS},
    };
    if (timer_settime(timerid, 0, &it, nullptr) < 0) {
      err(1, "timer_settime failed");
    }
  }
  std::this_thread::sleep_for(std::chrono::seconds(5));
  return 0;
}
```

Before this CL:
```
# /usr/bin/time ./runsc --ignore-cgroups --platform kvm --network none do $(pwd)/workloads/threadcputimers
1.50user 0.17system 0:05.25elapsed 31%CPU (0avgtext+0avgdata 35792maxresident)k
0inputs+184outputs (10major+20889minor)pagefaults 0swaps
```

After this CL:
```
# /usr/bin/time ./runsc --ignore-cgroups --platform kvm --network none do $(pwd)/workloads/threadcputimers
0.10user 0.12system 0:05.22elapsed 4%CPU (0avgtext+0avgdata 34040maxresident)k
0inputs+192outputs (6major+20929minor)pagefaults 0swaps
```

PiperOrigin-RevId: 695198313
2024-11-10 22:19:30 -08:00
Jamie LiuandgVisor bot 5850096133 ktime: support varying Timer implementations
- Rename Timer to SampledTimer.

- Move all Clock methods except Now to new interface SampledClock.

- Move SampledTimer's exported methods (except SetClock) to new interface
  Timer. Combine Swap and SwapAnd into Set to reduce the number of redundant
  methods that must be implemented.

- Add interface method Clock.NewTimer.

This is in preparation for cl/693856539, which adds a second Timer
implementation.

PiperOrigin-RevId: 694299679
2024-11-07 17:19:25 -08:00
Jamie LiuandgVisor bot 19f12e64c8 Move //pkg/sentry/kernel/time to //pkg/sentry/ktime.
This avoids needing to rename it everywhere it's imported.

PiperOrigin-RevId: 693930089
2024-11-06 18:13:51 -08:00
Jamie LiuandgVisor bot 17e070f24f kernel: only lock TaskSet.mu in Task.unstopVforkParent() if necessary
PiperOrigin-RevId: 687421394
2024-10-18 14:17:05 -07:00
Jamie LiuandgVisor bot 76a011743d kernel: add ThreadGroup.signalLock()
This allows "remote" locking of ThreadGroup.signalHandlers.mu without needing
to lock TaskSet.mu, analogously to Linux's lock_task_sighand().

This reveals a bug: kernel.Task.sendSignal[Timer]Locked() unintentionally
requires TaskSet.mu to be locked since it reads Task.exitState. To fix this,
use atomic memory operations on Task.exitState when required.

PiperOrigin-RevId: 681128063
2024-10-01 12:48:10 -07:00
Etienne PerotandgVisor bot a8eecc9746 Metrics: Refactor uint64 metric constructor, allow non-cumulative gauges.
This turns the uint64 metric constructor arguments into a struct, making it
more explicit as to what each part means. It also allows the creation of
non-cumulative uint64 (gauge) metrics, and adds methods to decrement or set
them.

PiperOrigin-RevId: 647134245
2024-06-26 17:41:24 -07:00
Fabricio VoznikaandgVisor bot 4e2d8d73ae Track exec'ed processes and kill them after restore
Processes that are exec'ed into a container cannot be properly
restored because the caller is no longer present. This change
tracks processes that are exec'ed and kill them upon restore.

Updates #1956

PiperOrigin-RevId: 623644184
2024-04-10 16:53:49 -07:00
Ayush RanjanandgVisor bot 533c7b5188 Plumb restore context to load*() methods.
This allows for external information to be passed to restore code.
Similar to a9af2d309b ("Plumb restore context to afterLoad()").

Updates #1956.

PiperOrigin-RevId: 614125262
2024-03-08 20:28:02 -08:00
gVisor bot f7e052496a Merge pull request #10102 from NymanRobin:change-atomic-value-to-pointer
PiperOrigin-RevId: 613612663
2024-03-07 09:52:08 -08:00
NymanRobinandrobin.nyman b6001724e6 Convert atomic.Value to atomic.Pointer[T] 2024-03-05 11:09:23 +02:00
Fabricio VoznikaandgVisor bot a9af2d309b Plumb restore context to afterLoad()
This allows for external information to be passed to restore code, like
host FDs to be remapped.

Updates #1956

PiperOrigin-RevId: 612540749
2024-03-04 12:21:50 -08:00
Etienne PerotandgVisor bot 4071080282 Replace Task.ptraceTracer with atomic.Pointer.
This removes the compare-and-branch involved in casting
`ptraceTracer.Load()` to `*Task`.

This is on the hot syscall path, and in the overwhelmingly-likely case that
the task is not being traced, we should be as fast as possible.

Shaves off about 20 nanoseconds from the syscall hot path:

```
                                 │   before    │               after                │
                                 │   sec/op    │   sec/op     vs base               │
Syscallbench/syscall.getpidopt-8   1.355µ ± 2%   1.334µ ± 2%  -1.59% (p=0.030 n=64)
```

PiperOrigin-RevId: 611277514
2024-02-28 17:06:55 -08:00
Etienne PerotandgVisor bot 3330aa4a72 gVisor seccomp: Implement in-Sentry seccomp cache.
This adds a per-task cache of seccomp actions to take for syscall numbers
where the filters return an action without depending on anything other than
the syscall number and the architecture code of the seccomp program input.

This avoids evaluating seccomp-bpf programs in the syscall hot path, for
programs that use seccomp *within* gVisor (aka on themselves).

Benchmarks show that this removes about 50ns from the syscall hot path
for a trivial filter like the one in the benchmark.
Real-world filters are much longer, and the benefit is magnified the more
complex the filter is.

```
                    │ not_cached  │              cached               │
                    │   sec/op    │   sec/op     vs base              │
SyscallUnderSeccomp   1.282µ ± 3%   1.230µ ± 1%  -4.06% (p=0.002 n=6)
```

PiperOrigin-RevId: 586522068
2023-11-29 20:00:16 -08:00
Ayush RanjanandgVisor bot 92f9e9c2bc Call FileDescription.OnClose() for newfd being replaced in dup2 and dup3.
dup(2) man page specifies:
       If the file descriptor newfd was previously open, it is closed
       before being reused; the close is performed silently (i.e., any
       errors during the close are not reported by dup2()).

Even though we were DecRef-ing and hence releasing the replaced FD, we were
not calling OnClose(). Compare fs/file.c:do_dup2() -> filp_close(tofree), which
in turn calls filp_flush(). In gVisor, FileDescription.OnClose() analogously
does such flush operations.
in turn

PiperOrigin-RevId: 583147682
2023-11-16 13:38:17 -08:00
Fabricio VoznikaandgVisor bot d550fb884b Fix up container ID after restore
Restore of a pod can be done using the same container ID (e.g.
save to continue the same execution) or using a different
container ID (e.g. cloning the pod into a new execution). If
a different container ID is used, we need to make sure the new
container ID is used in all places.

PiperOrigin-RevId: 582829466
2023-11-15 15:44:17 -08:00
Ayush RanjanandgVisor bot 10f98de123 Fix linter warning in kernel.go.
- Added docstrings for exported functions.
- Exported kernel.userCounters since it is used by another package (testutil)
  and is an exported field of an exported type `kernel.TaskConfig`.

PiperOrigin-RevId: 580615070
2023-11-08 12:20:17 -08:00
Andrei VaginandgVisor bot f1d8fe35fe inet: allow to create abstract unix sockets in non-root namespaces
PiperOrigin-RevId: 573253619
2023-10-13 10:20:56 -07:00
Konstantin BogomolovandgVisor bot 26d4fa13c1 Enable easy creation of independent profiling metrics.
When using conditionally compiled metrics there was only one go_tag to rule them
all. That's kind of a shame because when profiling something specific it's
nicer to know that only your metrics that you care about are compiled in and
metrics that you don't care about aren't.

This CL makes it easy to create new profiling go_tags, by abstracting the
profiling metric init functions under two builder structs: RealMetricBuilder
and FakeMetricBuilder; these builder structs are the only things that need
to change between the metrics-ON or OFF versions of conditional metric files
(aside from the go_tag controlling conditional compilation of course).
Ideally having to create these files manually wouldn't be necessary at all,
and we could use a go_template build rule instead, but that doesn't support
replacing go:build tags AFAIK.

PiperOrigin-RevId: 571153131
2023-10-05 15:56:37 -07:00
Etienne PerotandgVisor bot 3f85ee26ac Implement a subset of keyctl(2) and keyrings(7) for better Docker support.
The intention of this change is to cover a sufficient surface to accommodate
the use of running Docker within gVisor, rather than a full implementation.

This implements the following features:

  - Keys as a first-class concept in the kernel.
  - Tracking keys in user namespaces.
  - Task session keyrings: possession, inheritance.
  - Key permission enforcement.
  - The following `keyctl(2)` operations:
    - `KEYCTL_GET_KEYRING_ID`
    - `KEYCTL_DESCRIBE`
    - `KEYCTL_JOIN_SESSION_KEYRING`
    - `KEYCTL_SETPERM`

Notably, this does not implement:

  - The ability to actually add any keys other than the session keyring
    (which does not hold any cryptographic key data).
  - Other special keyrings (thread keyring, process keyring, user session
    keyring, etc.).
  - Lots of `keyctl(2)` operations.
  - Key expiration.
  - Key garbage collection. Keys live until their user namespace is destroyed.
    However, each user namespace is limited to 200 keys, so memory growth is
    bounded.
  - `add_key(2)`
  - `request_key(2)`

... However, this makes design choices that seem odd given the limited scope
of this change, but make sense when taking into account the desire to
eventually accommodate them in the future. For example, there are many
`switch` statements with only one option for session keyrings, which would get
more options when adding support for other special keyrings. Similarly, the
signature of `PossessedKeys` takes in all 3 special "possessed" keyrings, but
currently only ever gets the session keyring as non-nil.

PiperOrigin-RevId: 567047896
2023-09-20 12:38:39 -07:00
Andrei VaginandgVisor bot f556917137 vfs: MountNamespace.Root() has to return a top mount of /
A few mounts can be mounted on top of `/`.

PiperOrigin-RevId: 558264274
2023-08-18 15:35:47 -07:00
Andrei VaginandgVisor bot 988a2981dc kernel: GetMountNamespace has to check that mntns isn't nil
Reported-by: [email protected]
PiperOrigin-RevId: 553568085
2023-08-03 13:14:04 -07:00
Andrei VaginandgVisor bot 2d37d59eeb kernel: don't use atomic pointers for task.netns
task.netns is always changed from a task goroutine under task.mu.

It means that we can access it without any locks from a task goroutine
we don't need to increment a reference counter in such cases.

In all other cases, we need to take task.mu.

PiperOrigin-RevId: 552913323
2023-08-01 14:04:53 -07:00
Andrei VaginandgVisor bot 4af420d420 Implement setns for mount namespaces
PiperOrigin-RevId: 552859231
2023-08-01 11:12:29 -07:00
Andrei VaginandgVisor bot c5493106c6 Implement the setns syscall
This change introduces the nsfs file system. Each new namespace allocates
a new nsfs inode.

Here are reasons why we need these inodes:
* each namespace has to have an unique id.
* proc/pid/ns/ contains one entry for each namespace. Bind mounting one of
  the files in this directory to somewhere else in the filesystem keeps the
  corresponding namespace alive even if all processes currently in
  the namespace terminate.
* setns() allows the calling process to join an existing namespace specified
  by a file descriptor.

PiperOrigin-RevId: 550694515
2023-07-24 15:45:08 -07:00
Nayana BidariandgVisor bot 14bd67edab Pass memory cgroup id in pgalloc.Allocate()
For memory accounting per task, the memory cgroup id of the task is required.
This CL retrieves the cgroup id from the task context and stores it in the
AllocOpts struct which will later be used to account for memory usage.

PiperOrigin-RevId: 546996195
2023-07-10 15:16:47 -07:00
Andrei VaginandgVisor bot 4a554c0583 Enable lockdep for more mutexes
PiperOrigin-RevId: 533162054
2023-05-18 09:59:15 -07:00
Fabricio VoznikaandgVisor bot 11154ce7f1 Fix crash with large FD value
There were 2 problems when trying to allocate a high FD value:
- Rlimit is stored as uint64 and could be truncated when converting to int32
  to calculate the max value allowed for the FD.
- While trying to double the FD table size, the new length for the table could
  end up short due to invalid type convertion again.

Reported-by: [email protected]
PiperOrigin-RevId: 518362257
2023-03-21 13:21:27 -07:00
Ayush RanjanandgVisor bot 1003ae8e93 Delete VFS1 completely.
- Delete pkg/sentry/fs/*.
- Move pkg/sentry/fs/fsutil out of VFS1 directory and remove VFS1 components.
- Remove remaining unused references to VFS1 from remaining codebase.
- Rename/refactor code to avoid even referencing VFS2, unless necessary.
- Rewrite VFS1-only tests to VFS2.

Updates #1624

PiperOrigin-RevId: 490064269
2022-11-21 13:57:52 -08:00
Ayush RanjanandgVisor bot 7cac4e2732 Start cleaning up VFS1.
PiperOrigin-RevId: 486586072
2022-11-07 00:39:54 -08:00
Konstantin BogomolovandgVisor bot a111d38fe4 Introduce conditionally-compiled metrics.
This change introduces metrics which are disabled at compile time by default,
but can be enabled using a specified go tag. To demonstrate this, it replaces
the metrics used to profile the KVM platform to these new ones.

These profiling metrics can be enabled with the "condmetric_profiling" tag
when building runsc.

The motivation behind this is twofold:
- We have observed that using metrics in hot-paths can be quite expensive
  (i.e. syscall counter, timer metrics on KVM platform machine.Get()).
- There are classes of metrics that we generally care about using only on
  specific occasions, and don't need to have on by default.

PiperOrigin-RevId: 464636315
2022-08-01 15:21:48 -07:00
Andrei VaginandgVisor bot e7e65a9436 kernel: use lockdep mutexes
PiperOrigin-RevId: 449877248
2022-05-19 18:33:59 -07:00
Konstantin BogomolovandgVisor bot e9a817bbd5 Add KVM specific metrics.
This change adds counter and timer metrics useful for analyzing the KVM
platform.

PiperOrigin-RevId: 447043888
2022-05-06 12:21:49 -07:00
Kevin KrakauerandgVisor bot 3e7fcef1df switch fsimpl/ from sync/atomic to atomicbitops for 32 bit values
PiperOrigin-RevId: 443535714
2022-04-21 18:32:04 -07:00
Fabricio VoznikaandgVisor bot 93023f5e60 Use proto structs for seccheck points
Given that in most cases points are serialized to another process,
point data is now created diretly into protos.

As part of this change, infrastructure to track optional and context
fields was created to facilitate addition of lots of Points which is
needed for upcomming of changes.

Updates #4805

Currently the SST code is converting seccheck protos into SST protos
in the sentry before sending it to the API. After this change, SST
checker will be changed to send seccheck protos to the API and the
API then converts these into SST on the way to pubsub.

PiperOrigin-RevId: 442688320
2022-04-18 19:03:15 -07:00
Kevin KrakauerandgVisor bot b8dd528b97 prohibit direct use of sync/atomic (u)int64 functions
All atomic 64 bit ints are changed to atomicbitops.(Ui|I)nt64. A nogo checker
enforces that sync/atomic 64 bit functions are not called.

For reviewers: the interesting changes are in the atomicbitops and checkaligned
packages.

Why do this?
- It is very easy to accidentally use atomic values without sync/atomic funcs.
- We have checkatomics, but this is optional and is forgotten in several places.
  - Using a type+checker to enforce this seems less error prone and simpler.
- We get NoCopy protection.
- Use of 64 bit atomics can break 32 bit builds. We have types to handle this
  without any runtime cost, so we might as well use them.

PiperOrigin-RevId: 440473398
2022-04-08 16:06:26 -07:00
Adin ScannellandgVisor bot 33a9bf8e30 Refactor CPUID to allow for use in KVM and ring0.
Updates #5039

PiperOrigin-RevId: 421696994
2022-01-13 17:24:28 -08:00
Andrei Vagin 3ba699a85f Implement RLIMIT_NPROC
This is simplified version without supporting hierarchical
user-namespace counters.

Signed-off-by: Andrei Vagin <[email protected]>
2021-12-23 13:52:05 -08:00
Jamie LiuandgVisor bot 319a5364e5 Remove state:"nosave"/"zerovalue" annotations from all waiter.Queues.
Prior to cl/318010298, //pkg/state couldn't handle pointers to struct fields,
which meant that it couldn't handle intrusive linked lists, which meant that it
couldn't handle waiter.Queue, which meant that it couldn't handle epoll. As a
result, VFS1 unregisters all epoll waiters before saving and re-registers them
after loading, and waitable VFS1 file implementations tag their waiter.Queues
state:"nosave" (causing them to be skipped by the save/restore machinery) or
state:"zerovalue" (causing them to only be checked for zero-value-equality on
save).

VFS2 required cl/318010298 to support save/restore (due to the Impl inheritance
pattern used by vfs.FileDescription, vfs.Dentry, etc.); correspondingly, VFS2
epoll assumes that waiter.Queues *will be* saved and loaded correctly, and VFS2
file implementations do not tag waiter.Queues.

Some waiter.Queues, e.g. pipe.Pipe.Queue and kernel.Task.signalQueue, are used
by both VFS1 and VFS2 (the latter via signalfd); as a result of the above,
tagging these Queues state:"nosave" or state:"zerovalue" breaks VFS2 epoll.
Remove VFS1 epoll unregistration before saving (bringing it in line with VFS2),
and remove these tags from all waiter.Queues.

Also clean up after the epoll test added by cl/402323053, which implied this
issue (by instantiating DisableSave in the new test) without reporting it.

PiperOrigin-RevId: 402596216
2021-10-12 10:25:30 -07:00