Skip to content

Preserve the full counter when converting an eventfd to a host fd - #16627

Open
tamird wants to merge 1 commit into
google:masterfrom
tamird:review/eventfd-preserve-host-counter
Open

tamird wants to merge 1 commit into
google:masterfrom
tamird:review/eventfd-preserve-host-counter

Conversation

@tamird

@tamird tamird commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

A guest can write more than 32 bits to an eventfd before a device asks
for the underlying Linux file descriptor. The Gasket and VFIO interrupt
paths do this when registering an eventfd with the host. If a guest
creates an eventfd with 1 and writes 2^32, eventfd2's 32-bit initial
argument leaves the host descriptor with 1 instead of 4,294,967,297.

Create the descriptor with the low 32 bits, then write the rest before
handing it to the device. If the write or notifier registration fails,
close the private descriptor and keep the guest's counter. Counters
that already fit in the eventfd2 argument need no additional syscall.

The regression reads the actual descriptor in ordinary and semaphore
modes at UINT32_MAX, 2^32+1 and UINT64_MAX−1. To check a large
semaphore without draining it, read once, fill its predicted remaining
capacity, then check that writing one more returns EAGAIN.

A guest can write more than 32 bits to an eventfd before a device
needs the underlying Linux descriptor. eventfd2 takes an unsigned int,
so passing the accumulated counter as its initial value lost the high
bits. Seed the low bits at creation, write any remaining amount while
the descriptor is still private, and clear the guest counter only
after the transfer succeeds.
@tamird
tamird force-pushed the review/eventfd-preserve-host-counter branch from 57e4b37 to d94cb4a Compare October 28, 2026 21:55
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.

1 participant