Skip to content

register: clamp the raised RLIMIT_NOFILE to the hard limit - #1648

Open
ManoharPaturi wants to merge 1 commit into
axboe:masterfrom
ManoharPaturi:reg-rlimit-clamp
Open

ManoharPaturi wants to merge 1 commit into
axboe:masterfrom
ManoharPaturi:reg-rlimit-clamp

Conversation

@ManoharPaturi

Copy link
Copy Markdown

The kernel checks the number of files being registered against the soft RLIMIT_NOFILE and fails with -EMFILE when the count exceeds it. When io_uring_register_files() and the other file registration helpers hit that error, the library raises the soft limit on the caller's behalf by adding the file count to the current soft limit.

That sum can overshoot the hard limit. Raising the soft limit above the hard limit fails with EPERM, the setrlimit(2) result is not inspected, and the retried registration fails with -EMFILE again, even though the request itself would fit below the hard limit. Every window with cur < nr <= max and cur + nr > max hits this. For example with cur=200, max=350 and 300 files to register, 200 + 300 = 500 exceeds 350, so the raise fails although the hard limit alone would have been enough for the request.

Raise the soft limit to the number of files being registered, clamped to the hard limit, so the retry succeeds whenever the hard limit permits the request.

Testing: test/file-register.c gains test_rlimit_window(), which opens 300 descriptors, sets cur=200 and max=350, and registers them. On an Ubuntu 24.04 runner the old expression makes this return -EMFILE, while with the clamp the registration succeeds. Full CI matrix is green.

The kernel checks the number of registered files against the soft
RLIMIT_NOFILE and fails with -EMFILE if the count exceeds it. When the
library raises the soft limit on behalf of the caller, it adds the file
count to the current soft limit. That sum can exceed the hard limit, in
which case setrlimit(2) fails with EPERM and the registration ends up
failing with -EMFILE even though the request itself would fit below the
hard limit.

Raise the soft limit to the number of files being registered, clamped
to the hard limit, so the retry can succeed whenever the hard limit
permits it.

Add a test that opens a window where cur < nr <= max and cur + nr > max,
and verifies that registration goes through.
Copilot AI balanced review requested due to automatic review settings October 4, 2026 02:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants