fix(libkernel): return EPERM when unlocking an rwlock that is not held #3615
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3615"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by karim200003 on 2026-10-11T14:15:35Z.
scePthreadRwlockUnlock and pthread_rwlock_unlock treated every caller that is not the write owner as a reader: they decremented the reader count and called unlock_shared on the host shared_timed_mutex. On a lock nobody holds, after a double unlock, or while another thread holds the write lock, that is undefined behaviour on the host, the call returned 0 and the lock was damaged afterwards (the reader count went negative and destroy reported EBUSY for an idle lock).
FreeBSD libthr's _pthread_rwlock_unlock returns EPERM when the write owner is another thread, and do_rw_unlock in kern_umtx.c returns EPERM when there is no write owner and the reader count is zero. Unlock now does the same: a write lock owned by another thread is refused, and a read unlock takes one count with a compare-exchange that fails at zero, before unlock_shared. As in libthr the count is per lock, so a thread that holds no read lock can still release one held by another.
guest_pthread_rwlock covers a never-locked rwlock, double unlocks after a write and after reads, unlocking while another thread holds the write lock, and that timed-out read attempts leave no count; it fails without the change. An 8 s stress of 12 readers and 4 writers mixing the blocking, try and timed calls (22 million lock operations) saw no false EPERM and no torn write.
Fixes #3096
AI-assisted: yes
What
Tested
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T14:15:46Z.
Thank you for your contribution! Authors with no pull requests merged into
maincan have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.Pull request closed