git: e579e7e3281f - stable/12 - libthr pshared: correct a bug in allocation

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sun, 27 Aug 2023 07:11:42 UTC
The branch stable/12 has been updated by kib:

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(-)

diff --git a/lib/libthr/thread/thr_pshared.c b/lib/libthr/thread/thr_pshared.c
index 56def22f9ab9..3f303d52c2de 100644
--- a/lib/libthr/thread/thr_pshared.c
+++ b/lib/libthr/thread/thr_pshared.c
@@ -227,11 +227,16 @@ __thr_pshared_offpage(void *key, int doalloc)
 	int fd, ins_done;
 
 	curthread = _get_curthread();
-	pshared_rlock(curthread);
-	res = pshared_lookup(key);
-	pshared_unlock(curthread);
-	if (res != NULL)
-		return (res);
+	if (doalloc) {
+		pshared_destroy(curthread, key);
+		res = NULL;
+	} else {
+		pshared_rlock(curthread);
+		res = pshared_lookup(key);
+		pshared_unlock(curthread);
+		if (res != NULL)
+			return (res);
+	}
 	fd = _umtx_op(NULL, UMTX_OP_SHM, doalloc ? UMTX_SHM_CREAT :
 	    UMTX_SHM_LOOKUP, key, NULL);
 	if (fd == -1)