Repository navigation
Allocate advisory file lock trees on first use - #16337
Conversation
a8f3789 to
97cd922
Compare
manninglucas
left a comment
There was a problem hiding this comment.
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:
- Keep the
// locks is ...field doc style and add the nil-until-first-lock note to it, instead of replacing the description. - Add a short comment at the allocation in
LockRegionthat references Linux'slocks_get_lock_context()(allocates lazily, never onF_UNLCK). - Keep a
Preconditions: r.Start <= r.End.line on the relocatedunlock, since it still panics otherwise. - Consider extending
TestUnusedFileLocksto also coverLockRegionon a zero-valueLocks(lock → conflictingTestRegion→ unlock). - Consider annotating the
locksfield with// +checklocks:muso 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
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
97cd922 to
710b3f2
Compare
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
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.