Bug 269277 - On 12.3, process-shared mutex may fail locking operations after usage of ANOTHER process-shared mutex.
Summary: On 12.3, process-shared mutex may fail locking operations after usage of ANOT...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 12.3-RELEASE
Hardware: amd64 Any
: --- Affects Some People
Assignee: Konstantin Belousov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-02-01 15:38 UTC by rau8344
Modified: 2023-08-27 07:12 UTC (History)
3 users (show)

See Also:


Attachments
Minimal code to recreate issue (4.91 KB, text/plain)
2023-02-01 15:38 UTC, rau8344
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description rau8344 2023-02-01 15:38:08 UTC
Created attachment 239841 [details]
Minimal code to recreate issue

On FreeBSD 12.3 amd64, a process-shared mutex may fail locking operations after usage of ANOTHER process-shared mutex. Original issue was seen running a particular sequence of unit tests against a complex codebase; sometimes saw two threads lock the same mutex at the same time, or fail on lock with EINVAL. Issue recreated 

Recreated the EINVAL issue with a minimal example on a single thread (see attached), both with gtest and without. Tested on two physical quad-core machines (a Beckhoff 2040 and a Beckhoff 2042).
Comment 1 Konstantin Belousov freebsd_committer freebsd_triage 2023-02-01 20:19:26 UTC
https://reviews.freebsd.org/D38345
Comment 2 commit-hook freebsd_committer freebsd_triage 2023-02-01 23:41:45 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=25c862ae503a1c99458f4e055fd50c878fadbea3

commit 25c862ae503a1c99458f4e055fd50c878fadbea3
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-02-01 20:12:45 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-02-01 22:59:27 +0000

    libthr pshared: correct a bug in allocation

    When __thr_pshared_offpage() is called for allocation, it must not use
    the cached offpage for the key.  Instead, the cached offpage must be
    unmapped and removed from the cache, if any.

    It is legitimate for the user code to unmap the shared lock object without
    destroying it, and then mapping something over the freed VA to carry
    another shared lock.  In this case the cached offpage must be un-cached.

    PR:     269277
    Reported by:    rau8344@gmail.com
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D38345

 lib/libthr/thread/thr_pshared.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
Comment 3 commit-hook freebsd_committer freebsd_triage 2023-02-08 00:27:56 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=c8452bdeed4fc1f1feadf36c6008367263292254

commit c8452bdeed4fc1f1feadf36c6008367263292254
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-02-01 20:12:45 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-02-08 00:26:59 +0000

    libthr pshared: correct a bug in allocation

    PR:     269277

    (cherry picked from commit 25c862ae503a1c99458f4e055fd50c878fadbea3)

 lib/libthr/thread/thr_pshared.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
Comment 4 Xin LI freebsd_committer freebsd_triage 2023-08-27 07:03:03 UTC
Could you please also backport 3cf37d1251bf71b2171e67af8f2ca43cbe3d874d and 25c862ae503a1c99458f4e055fd50c878fadbea3 to stable/12? (These will apply cleanly).
Comment 5 commit-hook freebsd_committer freebsd_triage 2023-08-27 07:12:25 UTC
A commit in branch stable/12 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=e579e7e3281f8f096d5a3ca341adcf26bb0df765

commit e579e7e3281f8f096d5a3ca341adcf26bb0df765
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-02-01 20:12:45 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-08-27 07:10:52 +0000

    libthr pshared: correct a bug in allocation

    PR:     269277

    (cherry picked from commit 25c862ae503a1c99458f4e055fd50c878fadbea3)

 lib/libthr/thread/thr_pshared.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)