2 * Copyright (c) 2015 The FreeBSD Foundation
4 * This software was developed by Konstantin Belousov
5 * under sponsorship from the FreeBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
34 #include <sys/queue.h>
35 #include "namespace.h"
37 #include "un-namespace.h"
39 #include "thr_private.h"
47 LIST_HEAD(pshared_hash_head, psh);
49 static struct pshared_hash_head pshared_hash[HASH_SIZE];
50 #define PSHARED_KEY_HASH(key) (((unsigned long)(key) >> 8) % HASH_SIZE)
51 /* XXXKIB: lock could be split to per-hash chain, if appears contested */
52 static struct urwlock pshared_lock = DEFAULT_URWLOCK;
56 __thr_pshared_init(void)
60 page_size = getpagesize();
61 THR_ASSERT(page_size >= THR_PAGE_SIZE_MIN,
62 "THR_PAGE_SIZE_MIN is too large");
64 _thr_urwlock_init(&pshared_lock);
65 for (i = 0; i < HASH_SIZE; i++)
66 LIST_INIT(&pshared_hash[i]);
70 pshared_rlock(struct pthread *curthread)
73 curthread->locklevel++;
74 _thr_rwl_rdlock(&pshared_lock);
78 pshared_wlock(struct pthread *curthread)
81 curthread->locklevel++;
82 _thr_rwl_wrlock(&pshared_lock);
86 pshared_unlock(struct pthread *curthread)
89 _thr_rwl_unlock(&pshared_lock);
90 curthread->locklevel--;
95 * Among all processes sharing a lock only one executes
96 * pthread_lock_destroy(). Other processes still have the hash and
99 * Mitigate the problem by checking the liveness of all hashed keys
100 * periodically. Right now this is executed on each
101 * pthread_lock_destroy(), but may be done less often if found to be
102 * too time-consuming.
105 pshared_gc(struct pthread *curthread)
107 struct pshared_hash_head *hd;
111 pshared_wlock(curthread);
112 for (i = 0; i < HASH_SIZE; i++) {
113 hd = &pshared_hash[i];
114 LIST_FOREACH_SAFE(h, hd, link, h1) {
115 error = _umtx_op(NULL, UMTX_OP_SHM, UMTX_SHM_ALIVE,
119 LIST_REMOVE(h, link);
120 munmap(h->val, page_size);
124 pshared_unlock(curthread);
128 pshared_lookup(void *key)
130 struct pshared_hash_head *hd;
133 hd = &pshared_hash[PSHARED_KEY_HASH(key)];
134 LIST_FOREACH(h, hd, link) {
142 pshared_insert(void *key, void **val)
144 struct pshared_hash_head *hd;
147 hd = &pshared_hash[PSHARED_KEY_HASH(key)];
148 LIST_FOREACH(h, hd, link) {
150 * When the key already exists in the hash, we should
151 * return either the new (just mapped) or old (hashed)
152 * val, and the other val should be unmapped to avoid
153 * address space leak.
155 * If two threads perform lock of the same object
156 * which is not yet stored in the pshared_hash, then
157 * the val already inserted by the first thread should
158 * be returned, and the second val freed (order is by
159 * the pshared_lock()). Otherwise, if we unmap the
160 * value obtained from the hash, the first thread
161 * might operate on an unmapped off-page object.
163 * There is still an issue: if hashed key was unmapped
164 * and then other page is mapped at the same key
165 * address, the hash would return the old val. I
166 * decided to handle the race of simultaneous hash
167 * insertion, leaving the unlikely remap problem
171 if (h->val != *val) {
172 munmap(*val, page_size);
179 h = malloc(sizeof(*h));
184 LIST_INSERT_HEAD(hd, h, link);
189 pshared_remove(void *key)
191 struct pshared_hash_head *hd;
195 hd = &pshared_hash[PSHARED_KEY_HASH(key)];
196 LIST_FOREACH(h, hd, link) {
198 LIST_REMOVE(h, link);
208 pshared_clean(void *key, void *val)
212 munmap(val, page_size);
213 _umtx_op(NULL, UMTX_OP_SHM, UMTX_SHM_DESTROY, key, NULL);
217 __thr_pshared_offpage(void *key, int doalloc)
219 struct pthread *curthread;
223 curthread = _get_curthread();
224 pshared_rlock(curthread);
225 res = pshared_lookup(key);
226 pshared_unlock(curthread);
229 fd = _umtx_op(NULL, UMTX_OP_SHM, doalloc ? UMTX_SHM_CREAT :
230 UMTX_SHM_LOOKUP, key, NULL);
233 res = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
235 if (res == MAP_FAILED)
237 pshared_wlock(curthread);
238 ins_done = pshared_insert(key, &res);
239 pshared_unlock(curthread);
241 pshared_clean(key, res);
248 __thr_pshared_destroy(void *key)
250 struct pthread *curthread;
253 curthread = _get_curthread();
254 pshared_wlock(curthread);
255 val = pshared_remove(key);
256 pshared_unlock(curthread);
257 pshared_clean(key, val);
258 pshared_gc(curthread);
262 __thr_pshared_atfork_pre(void)
265 _thr_rwl_rdlock(&pshared_lock);
269 __thr_pshared_atfork_post(void)
272 _thr_rwl_unlock(&pshared_lock);