Skip to content

lkl: mmu: fix shm leak and add shmem_cleanup host operation - #649

Open
clingfei wants to merge 1 commit into
lkl:masterfrom
clingfei:fix
Open

clingfei wants to merge 1 commit into
lkl:masterfrom
clingfei:fix

Conversation

@clingfei

@clingfei clingfei commented Oct 10, 2026 •

Copy link
Copy Markdown

In MMU mode, shmem_init() allocates POSIX shared memory via shm_open() to back LKL physical memory. Two issues currently exist:

  1. The shared memory object is never unlinked, so it persists after the host process exits or crashes, potentially exhausting /dev/shm space during repeated testing or fuzzing.

  2. The backing file descriptor is not closed when LKL shuts down. Reinitialization within the same host process overwrites shared_mem_fd without closing the previous descriptor, leaking file descriptors.

Fix these by:

  • Calling shm_unlink() immediately after shm_open(), keeping the descriptor available for subsequent mappings.
  • Adding an optional shmem_cleanup host operation, called from free_mem() in MMU mode, to close shared_mem_fd and reset it to -1 on shutdown.
  • Executing shm_unlink() and ftruncate() outside assert() so both calls are preserved when NDEBUG is defined.

Closes: #629

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Test Results

 87 files  ±0   87 suites  ±0   10m 23s ⏱️ -45s
194 tests ±0  183 ✅ ±0  11 💤 ±0  0 ❌ ±0 
710 runs  ±0  666 ✅ ±0  44 💤 ±0  0 ❌ ±0 

Results for commit 3d59b01. ± Comparison against base commit 9ad978b.

♻️ This comment has been updated with latest results.

@tavip tavip 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.

Sorry for taking so long to review, and thanks for the fix! A couple of things before merging:

  1. Commit message mismatch: Please update the commit message and PR description to match the diff — it currently mentions setting FD_CLOEXEC (already guaranteed by POSIX shm_open()) and closing an existing shared_mem_fd in shmem_init(), neither of which is in the patch.

  2. Avoid putting shm_unlink() inside assert() (tools/lkl/lib/posix-host.c): -DNDEBUG compiles it out completely. Please capture the return value first (e.g. ret = shm_unlink(...); assert(ret == 0);, and ideally the same for ftruncate()).

In MMU mode, the POSIX shared memory object persists after process exit
because it is never unlinked. Its backing file descriptor is also left
open when LKL shuts down.

Unlink the object immediately after shm_open(), keeping the descriptor
available for subsequent mappings. Add an optional shmem_cleanup host
operation, called from free_mem() in MMU mode, to close shared_mem_fd
and reset it to -1 on shutdown.

Execute shm_unlink() and ftruncate() outside assert() so both calls are
preserved when NDEBUG is defined.

Closes: lkl#629

Signed-off-by: Cheng Lingfei <chenglingfei@foxmail.com>
@clingfei

Copy link
Copy Markdown
Author

Sorry for taking so long to review, and thanks for the fix! A couple of things before merging:

  1. Commit message mismatch: Please update the commit message and PR description to match the diff — it currently mentions setting FD_CLOEXEC (already guaranteed by POSIX shm_open()) and closing an existing shared_mem_fd in shmem_init(), neither of which is in the patch.
  2. Avoid putting shm_unlink() inside assert() (tools/lkl/lib/posix-host.c): -DNDEBUG compiles it out completely. Please capture the return value first (e.g. ret = shm_unlink(...); assert(ret == 0);, and ideally the same for ftruncate()).

Fixed. Thanks!

Btw, the Windows CI build is failing because flex is missing from the MSYS2 environment. I'll send a separate PR to explicitly install flex and bison.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MMU lkl_phys_mem shm leak

2 participants