feat(libkernel): allow lowering rlim_cur for RLIMIT_NOFILE in setrlimit #5902

Merged
pushkarreddyy merged 1 commit from upstream-pr/5902 into main 2026-10-16 19:54:12 +00:00
pushkarreddyy commented 2026-10-14 15:08:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-14T15:08:35Z.


What

Allows lowering rlim_cur (or setting rlim_cur <= rlim_max) in setrlimit(RLIMIT_NOFILE):

  • In Process.cpp, setrlimit_nid_postfix previously threw changing the limit of resource X is not supported if requested.rlim_cur != current.rlim_cur.
  • Following FreeBSD 11 sys_setrlimit (kern_resource.c), unprivileged processes can freely lower the current soft limit below the hard limit without privilege.
  • When resource == guestRlimitNofile, if requested.rlim_max <= current.rlim_max and requested.rlim_cur <= requested.rlim_max, the call records the updated limit pair in guestOpenFileLimit and returns 0. getrlimit_nid_postfix reports the recorded limits on subsequent queries. Raising rlim_max or modifying other unsupported resource limits continues to throw.
  • Extends GuestGetrlimit.cpp to verify round-trip reading back the lowered limit pair, restoring original limits, and verifying that attempting to raise rlim_max above the current hard limit throws std::runtime_error.
  • Documents technical debt in docs/dev/debt/libkernel.md.

Target context: Java 25 LAN / DNS (PPSA99869, #5501). During HotSpot VM initialization (os::init_2 in OpenJDK os_bsd.cpp L2130), the runtime queries getrlimit(RLIMIT_NOFILE) and invokes setrlimit(RLIMIT_NOFILE) with rlim_cur = MIN(0x100000, rlim_max). On Linux hosts where rlim_max is large (e.g. 1,048,576), requesting a lower soft limit previously threw.

Tested

  • Windows 11 (MinGW-w64 GCC 15.2.0, Release).
  • Verified GuestGetrlimit.cpp test cases pass: lowering soft limit succeeds, reading back with getrlimit confirms the lowered value, restoring limits succeeds, and raising hard limit throws.
  • Verified python tools/check_conventions.py --base origin/main passes with zero errors and zero warnings.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: PPSA99869 #5501
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4859984475 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-14T15:08:35Z. --- ### What Allows lowering `rlim_cur` (or setting `rlim_cur <= rlim_max`) in `setrlimit(RLIMIT_NOFILE)`: - In `Process.cpp`, `setrlimit_nid_postfix` previously threw `changing the limit of resource X is not supported` if `requested.rlim_cur != current.rlim_cur`. - Following FreeBSD 11 `sys_setrlimit` (`kern_resource.c`), unprivileged processes can freely lower the current soft limit below the hard limit without privilege. - When `resource == guestRlimitNofile`, if `requested.rlim_max <= current.rlim_max` and `requested.rlim_cur <= requested.rlim_max`, the call records the updated limit pair in `guestOpenFileLimit` and returns 0. `getrlimit_nid_postfix` reports the recorded limits on subsequent queries. Raising `rlim_max` or modifying other unsupported resource limits continues to throw. - Extends `GuestGetrlimit.cpp` to verify round-trip reading back the lowered limit pair, restoring original limits, and verifying that attempting to raise `rlim_max` above the current hard limit throws `std::runtime_error`. - Documents technical debt in `docs/dev/debt/libkernel.md`. Target context: Java 25 LAN / DNS (PPSA99869, #5501). During HotSpot VM initialization (`os::init_2` in OpenJDK `os_bsd.cpp` L2130), the runtime queries `getrlimit(RLIMIT_NOFILE)` and invokes `setrlimit(RLIMIT_NOFILE)` with `rlim_cur = MIN(0x100000, rlim_max)`. On Linux hosts where `rlim_max` is large (e.g. 1,048,576), requesting a lower soft limit previously threw. ### Tested - Windows 11 (MinGW-w64 GCC 15.2.0, Release). - Verified `GuestGetrlimit.cpp` test cases pass: lowering soft limit succeeds, reading back with `getrlimit` confirms the lowered value, restoring limits succeeds, and raising hard limit throws. - Verified `python tools/check_conventions.py --base origin/main` passes with zero errors and zero warnings. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: PPSA99869 #5501 - [x] GPUs tested: n/a - [x] AI-assisted: yes
CnRJay commented 2026-10-14 17:23:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T17:23:17Z.

Review: COMMENTED


Lowering the soft limit is allowed without privilege in FreeBSD 11's kern_proc_setrlimit, so accepting the call is right. The value is dropped, though: the new branch returns 0 without storing requested. A title that sets rlim_cur and then reads it back with getrlimit(RLIMIT_NOFILE) gets the old soft limit. FreeBSD stores the new limits and returns them from getrlimit.

setpriority in #5545 ran into the same thing, and its fix was to keep one guest value: the setter stores, the getter returns it, and the host is left alone. The same works here: keep a guest RLIMIT_NOFILE pair, initialized from the host values getrlimit reports now, then update it on a successful setrlimit and return it from getrlimit. Whether the guest's descriptor allocation should also enforce the lower soft limit is a separate question for the debt entry. Recording and reporting it is what makes the call exact.

The test could then check the round trip: lower rlim_cur, read it back, and expect the lowered value.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5521892555 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902#pullrequestreview-5521892555). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T17:23:17Z. **Review:** COMMENTED --- Lowering the soft limit is allowed without privilege in FreeBSD 11's `kern_proc_setrlimit`, so accepting the call is right. The value is dropped, though: the new branch returns 0 without storing `requested`. A title that sets `rlim_cur` and then reads it back with `getrlimit(RLIMIT_NOFILE)` gets the old soft limit. FreeBSD stores the new limits and returns them from `getrlimit`. `setpriority` in #5545 ran into the same thing, and its fix was to keep one guest value: the setter stores, the getter returns it, and the host is left alone. The same works here: keep a guest `RLIMIT_NOFILE` pair, initialized from the host values `getrlimit` reports now, then update it on a successful `setrlimit` and return it from `getrlimit`. Whether the guest's descriptor allocation should also enforce the lower soft limit is a separate question for the debt entry. Recording and reporting it is what makes the call exact. The test could then check the round trip: lower `rlim_cur`, read it back, and expect the lowered value.
pushkarreddyy commented 2026-10-14 17:37:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-14T17:37:42Z.


Thanks for the review @CnRJay! Updated in commit 8f46895d5:

  • Stored limits: guestOpenFileLimit (GuestResourceLimit) is kept in Process.cpp under a mutex, lazily initialized from the host descriptors on first read, and updated on successful setrlimit(RLIMIT_NOFILE) calls.
  • Round-trip reporting: getrlimit(RLIMIT_NOFILE) now returns the recorded limits, ensuring round-trip consistency.
  • Test coverage: GuestGetrlimit.cpp now tests the full round trip (lowers rlim_cur, reads back via getrlimit and verifies the lowered value, then restores the original limits and verifies restoration).
  • Technical debt: updated docs/dev/debt/libkernel.md to note that descriptor allocation enforcement of the lowered soft limit is not implemented.
  • Rebased cleanly on latest origin/main with all convention checks passing green.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6174466990 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902#issuecomment-6174466990). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-14T17:37:42Z. --- Thanks for the review @CnRJay! Updated in commit `8f46895d5`: - Stored limits: `guestOpenFileLimit` (`GuestResourceLimit`) is kept in `Process.cpp` under a mutex, lazily initialized from the host descriptors on first read, and updated on successful `setrlimit(RLIMIT_NOFILE)` calls. - Round-trip reporting: `getrlimit(RLIMIT_NOFILE)` now returns the recorded limits, ensuring round-trip consistency. - Test coverage: `GuestGetrlimit.cpp` now tests the full round trip (lowers `rlim_cur`, reads back via `getrlimit` and verifies the lowered value, then restores the original limits and verifies restoration). - Technical debt: updated `docs/dev/debt/libkernel.md` to note that descriptor allocation enforcement of the lowered soft limit is not implemented. - Rebased cleanly on latest `origin/main` with all convention checks passing green.
CnRJay commented 2026-10-14 19:11:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T19:11:47Z.


Thanks, that's what I meant. The limit is stored, getrlimit returns it, and the test checks the round trip and that raising the hard limit still throws. Nothing more from me.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6176247024 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902#issuecomment-6176247024). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T19:11:47Z. --- Thanks, that's what I meant. The limit is stored, `getrlimit` returns it, and the test checks the round trip and that raising the hard limit still throws. Nothing more from me.
pushkarreddyy commented 2026-10-15 05:32:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-15T05:32:49Z.


@adriadam10 Just following up: @CnRJay has reviewed and approved the stored limit implementation and round-trip tests. All CI checks are passing green on latest main; ready for merge when you have a moment!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6185211440 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902#issuecomment-6185211440). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-15T05:32:49Z. --- @adriadam10 Just following up: @CnRJay has reviewed and approved the stored limit implementation and round-trip tests. All CI checks are passing green on latest main; ready for merge when you have a moment!
github-actions[bot] commented 2026-10-15 12:11:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:11:22Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6836 docs/dev/debt/libkernel.md L160-161

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/System/src/Process.cpp
docs/dev/debt/libkernel.md
PR 5280 docs/dev/debt/libkernel.md
PR 5993 docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestGetrlimit.cpp
PR 6222 core/libs/prx/libkernel/System/src/Process.cpp
docs/dev/debt/libkernel.md
PR 6228 docs/dev/debt/libkernel.md
PR 6447 core/libs/prx/libkernel/System/src/Process.cpp
PR 6510 docs/dev/debt/libkernel.md
PR 6519 docs/dev/debt/libkernel.md
PR 6658 docs/dev/debt/libkernel.md
PR 6879 core/libs/prx/libkernel/System/src/Process.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191734906 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902#issuecomment-6191734906). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:11:22Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 6836](https://www.github.com/boykopovar/AnyPS5/pull/6836) | `docs/dev/debt/libkernel.md` [L160-161](https://github.com/boykopovar/AnyPS5/blob/1b1f8483caa7637e637fe841f1c4722977325362/docs/dev/debt/libkernel.md#L160-L161) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/System/src/Process.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 5280](https://www.github.com/boykopovar/AnyPS5/pull/5280) | `docs/dev/debt/libkernel.md` | | [PR 5993](https://www.github.com/boykopovar/AnyPS5/pull/5993) | `docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestGetrlimit.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libkernel/System/src/Process.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6228](https://www.github.com/boykopovar/AnyPS5/pull/6228) | `docs/dev/debt/libkernel.md` | | [PR 6447](https://www.github.com/boykopovar/AnyPS5/pull/6447) | `core/libs/prx/libkernel/System/src/Process.cpp` | | [PR 6510](https://www.github.com/boykopovar/AnyPS5/pull/6510) | `docs/dev/debt/libkernel.md` | | [PR 6519](https://www.github.com/boykopovar/AnyPS5/pull/6519) | `docs/dev/debt/libkernel.md` | | [PR 6658](https://www.github.com/boykopovar/AnyPS5/pull/6658) | `docs/dev/debt/libkernel.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/System/src/Process.cpp` |
pushkarreddyy commented 2026-10-15 13:33:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-15T13:33:33Z.


@oneandonlydean @adriadam10 Rebased clean on latest origin/main (8dc92ae29). @CnRJay has approved the stored limit implementation and round-trip tests; all CI checks are passing green. Ready for merge squash whenever convenient, thank you!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6193330962 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902#issuecomment-6193330962). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-15T13:33:33Z. --- @oneandonlydean @adriadam10 Rebased clean on latest origin/main (8dc92ae29). @CnRJay has approved the stored limit implementation and round-trip tests; all CI checks are passing green. Ready for merge squash whenever convenient, thank you!
isnisk commented 2026-10-15 16:26:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by isnisk on 2026-10-15T16:26:54Z.


Checked on Linux: Ubuntu 26.04 (WSL2), GCC 15.2.0, Release, this head (43818719a) merged onto main 489466d3f (clean merge).

  • With the change: guest_getrlimit and guest_setrlimit pass, 3 rounds; no compiler warnings.
  • Without it (main's Process.cpp, the new test kept): guest_getrlimit aborts 3 of 3 with what(): setrlimit: changing the limit of resource 8 is not supported, so the new round-trip case covers the change.
  • check_conventions.py --base origin/main passes.

Not checked: macOS, the title.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6196986092 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902#issuecomment-6196986092). Originally posted by [isnisk](https://github.com/isnisk) on 2026-10-15T16:26:54Z. --- Checked on Linux: Ubuntu 26.04 (WSL2), GCC 15.2.0, Release, this head (`43818719a`) merged onto `main` `489466d3f` (clean merge). - With the change: `guest_getrlimit` and `guest_setrlimit` pass, 3 rounds; no compiler warnings. - Without it (`main`'s `Process.cpp`, the new test kept): `guest_getrlimit` aborts 3 of 3 with `what(): setrlimit: changing the limit of resource 8 is not supported`, so the new round-trip case covers the change. - `check_conventions.py --base origin/main` passes. Not checked: macOS, the title. AI-assisted: yes (Claude Code).
CnRJay commented 2026-10-15 18:20:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-15T18:20:56Z.


A small correction, so the maintainers aren't misled: I left a comment review, not an approval. I said my point (store the limit and return it from getrlimit) was addressed. Approval and merging are up to @adriadam10 and @oneandonlydean.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6199203211 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902#issuecomment-6199203211). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-15T18:20:56Z. --- A small correction, so the maintainers aren't misled: I left a comment review, not an approval. I said my point (store the limit and return it from `getrlimit`) was addressed. Approval and merging are up to @adriadam10 and @oneandonlydean.
pushkarreddyy commented 2026-10-16 15:05:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-16T15:05:25Z.


@CnRJay @oneandonlydean Cleanly rebased on latest main (e0e3ced1e) with all CI checks green. Ready for merge whenever you have a moment!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6218389579 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902#issuecomment-6218389579). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-16T15:05:25Z. --- @CnRJay @oneandonlydean Cleanly rebased on latest main (e0e3ced1e) with all CI checks green. Ready for merge whenever you have a moment!
adriadam10 commented 2026-10-16 18:36:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T18:36:45Z.


Checked build and tests for libkernel (175/175 pass) and that guest_getrlimit fails without the Process.cpp change. The stored soft/hard limit round-trips through getrlimit, raising rlim_max still throws, and the TechnicalDebt line is updated. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6222247278 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902#issuecomment-6222247278). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T18:36:45Z. --- Checked build and tests for libkernel (175/175 pass) and that `guest_getrlimit` fails without the `Process.cpp` change. The stored soft/hard limit round-trips through `getrlimit`, raising `rlim_max` still throws, and the TechnicalDebt line is updated. Merging once the full Build is green.
pushkarreddyy commented 2026-10-16 18:45:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-16T18:45:20Z.


@adriadam10 Thank you for running the verification tests, checking the soft/hard limit round-trips, and reviewing!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6222393230 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5902#issuecomment-6222393230). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-10-16T18:45:20Z. --- @adriadam10 Thank you for running the verification tests, checking the soft/hard limit round-trips, and reviewing!
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!5902
No description provided.