Skip to content

Allocate advisory file lock trees on first use - #16337

Merged
copybara-service[bot] merged 1 commit into
gvisor:masterfrom
tamird:lazy-file-lock-trees
Oct 26, 2026
Merged

copybara-service[bot] merged 1 commit into
gvisor:masterfrom
tamird:lazy-file-lock-trees

Conversation

@tamird

@tamird tamird commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Each tmpfs inode reserves 640 bytes for POSIX and BSD advisory lock
trees, whether or not a process locks the file. Store a pointer to each
tree and allocate it under the existing mutex on the first lock
request. Querying or releasing a lock on an untouched file allocates
nothing.

On AMD64 this reduces a regular tmpfs file from 1,392 to 768 bytes and
a directory from 1,048 to 424 bytes. Each tree that is actually used
costs one 320-byte allocation. A checkpoint of a warmed Bazel server
contained 9,130 tmpfs inodes and just one separately allocated lock
tree across the entire restored system 1.

@manninglucas manninglucas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This is a small, well-measured memory reduction (about 624 B per inode for every vfs.FileLocks user), and it follows the same approach Linux uses for inode->i_flctx in locks_get_lock_context(). All accesses to the pointer happen under Locks.mu, the nil-tree paths are handled correctly, and the PR merges cleanly with current master. These nits are all optional:

  1. Keep the // locks is ... field doc style and add the nil-until-first-lock note to it, instead of replacing the description.
  2. Add a short comment at the allocation in LockRegion that references Linux's locks_get_lock_context() (allocates lazily, never on F_UNLCK).
  3. Keep a Preconditions: r.Start <= r.End. line on the relocated unlock, since it still panics otherwise.
  4. Consider extending TestUnusedFileLocks to also cover LockRegion on a zero-value Locks (lock → conflicting TestRegion → unlock).
  5. Consider annotating the locks field with // +checklocks:mu so checklocks enforces the protection that the comment describes.

Security: I reviewed this against the SECURITY.md taxonomy and found no security impact. The change stays inside the Sentry and adds no host interactions or new validation paths. Every access to the lazily allocated tree happens under Locks.mu. Unlock and query on a nil tree get a terminal iterator, so they can't panic. Worst-case memory per inode is the same as before, so a sandboxed app gains no new DoS or memory-amplification lever.


[🤖] This review is part of an experimental auto-review process to help speed up gVisor review velocity. It is a work in progress. Assisted-by: Claude

Comment thread pkg/sentry/fsimpl/lock/lock.go Resolved
Comment thread pkg/sentry/fsimpl/lock/lock.go Resolved
Comment thread pkg/sentry/fsimpl/lock/lock.go Resolved
Comment thread pkg/sentry/fsimpl/lock/lock_test.go Resolved
Each tmpfs inode reserves 640 bytes for POSIX and BSD advisory lock
trees, whether or not a process locks the file. Store a pointer to each
tree and allocate it under the existing mutex on the first lock
request. Querying or releasing a lock on an untouched file allocates
nothing.

On AMD64 this reduces a regular tmpfs file from 1,392 to 768 bytes and
a directory from 1,048 to 424 bytes. Each tree that is actually used
costs one 320-byte allocation. A checkpoint of a warmed Bazel server
contained 9,130 tmpfs inodes and just one separately allocated lock
tree across the entire restored system [1][2].

[1]: https://app.buildbuddy.io/invocation/22e8c550-7d76-4fcd-91b9-7e76a33064a7
[2]: https://app.buildbuddy.io/invocation/044f2b57-f53e-4b90-a0c4-f0a5381bd40b
@tamird
tamird force-pushed the lazy-file-lock-trees branch from 97cd922 to 710b3f2 Compare October 26, 2026 20:33

@tamird tamird left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replies from Codex on Tamir’s behalf for 710b3f2.

Comment thread pkg/sentry/fsimpl/lock/lock.go Resolved
Comment thread pkg/sentry/fsimpl/lock/lock.go Resolved
Comment thread pkg/sentry/fsimpl/lock/lock.go Resolved
Comment thread pkg/sentry/fsimpl/lock/lock_test.go Resolved
@manninglucas manninglucas added the ready to pull Causes the PR to be imported into Google. Will be removed soon. label Oct 26, 2026
copybara-service Bot pushed a commit that referenced this pull request Oct 26, 2026
Each tmpfs inode reserves 640 bytes for POSIX and BSD advisory lock
trees, whether or not a process locks the file. Store a pointer to each
tree and allocate it under the existing mutex on the first lock
request. Querying or releasing a lock on an untouched file allocates
nothing.

On AMD64 this reduces a regular tmpfs file from 1,392 to 768 bytes and
a directory from 1,048 to 424 bytes. Each tree that is actually used
costs one 320-byte allocation. A checkpoint of a warmed Bazel server
contained 9,130 tmpfs inodes and just one separately allocated lock
tree across the entire restored system [1][2].

[1]: https://app.buildbuddy.io/invocation/22e8c550-7d76-4fcd-91b9-7e76a33064a7
[2]: https://app.buildbuddy.io/invocation/044f2b57-f53e-4b90-a0c4-f0a5381bd40b

FUTURE_COPYBARA_INTEGRATE_REVIEW=#16337 from tamird:lazy-file-lock-trees 710b3f2
PiperOrigin-RevId: 1007577427
@copybara-service
copybara-service Bot merged commit eb97bf6 into gvisor:master Oct 26, 2026
14 checks passed
@tamird
tamird deleted the lazy-file-lock-trees branch November 18, 2026 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready to pull Causes the PR to be imported into Google. Will be removed soon.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants