source: mainline/kernel/generic/src/synch/futex.c@ c28413a9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c28413a9 was c28413a9, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

cht: Added cht_create_simple(). cht_create() can now block until memory is available.

  • Property mode set to 100644
File size: 12.5 KB
RevLine 
[9aa72b4]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[669f3d32]3 * Copyright (c) 2012 Adam Hraska
[9aa72b4]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - 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.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[cc73a8a1]30/** @addtogroup sync
[b45c443]31 * @{
32 */
33
[9aa72b4]34/**
[b45c443]35 * @file
[cf26ba9]36 * @brief Kernel backend for futexes.
[9aa72b4]37 */
38
39#include <synch/futex.h>
[ee42e43]40#include <synch/mutex.h>
[303c94c]41#include <synch/spinlock.h>
[669f3d32]42#include <synch/rcu.h>
[9aa72b4]43#include <mm/frame.h>
44#include <mm/page.h>
45#include <mm/slab.h>
[303c94c]46#include <proc/thread.h>
[4fded58]47#include <proc/task.h>
[9aa72b4]48#include <genarch/mm/page_pt.h>
49#include <genarch/mm/page_ht.h>
[669f3d32]50#include <adt/cht.h>
[9aa72b4]51#include <adt/hash_table.h>
52#include <adt/list.h>
53#include <arch.h>
54#include <align.h>
55#include <panic.h>
56#include <errno.h>
57
58#define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */
59
[669f3d32]60/** Task specific pointer to a global kernel futex object. */
61typedef struct futex_ptr {
62 /** CHT link. */
63 cht_link_t cht_link;
64 /** List of all futex pointers used by the task. */
65 link_t all_link;
66 /** Kernel futex object. */
67 futex_t *futex;
68 /** User space virtual address of the futex variable in the task. */
69 uintptr_t uaddr;
70} futex_ptr_t;
71
72
73static void destroy_task_cache(work_t *work);
74
75static void futex_initialize(futex_t *futex, uintptr_t paddr);
76static void futex_add_ref(futex_t *futex);
77static void futex_release_ref(futex_t *futex);
78static void futex_release_ref_locked(futex_t *futex);
79
80static futex_t *get_futex(uintptr_t uaddr);
81static futex_t *find_cached_futex(uintptr_t uaddr);
82static futex_t *get_and_cache_futex(uintptr_t phys_addr, uintptr_t uaddr);
83static bool find_futex_paddr(uintptr_t uaddr, uintptr_t *phys_addr);
[9aa72b4]84
[96b02eb9]85static size_t futex_ht_hash(sysarg_t *key);
86static bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item);
[9aa72b4]87static void futex_ht_remove_callback(link_t *item);
88
[669f3d32]89static size_t task_fut_ht_hash(const cht_link_t *link);
90static size_t task_fut_ht_key_hash(void *key);
91static bool task_fut_ht_equal(const cht_link_t *item1, const cht_link_t *item2);
92static bool task_fut_ht_key_equal(void *key, const cht_link_t *item);
93
94
95/** Mutex protecting the global futex hash table.
96 *
97 * Acquire task specific TASK->futex_list_lock before this mutex.
[4fded58]98 */
[ee42e43]99static mutex_t futex_ht_lock;
[9aa72b4]100
[669f3d32]101/** Global kernel futex hash table. Lock futex_ht_lock before accessing.
102 *
103 * Physical address of the futex variable is the lookup key.
104 */
[9aa72b4]105static hash_table_t futex_ht;
106
[669f3d32]107/** Global kernel futex hash table operations. */
[9aa72b4]108static hash_table_operations_t futex_ht_ops = {
109 .hash = futex_ht_hash,
110 .compare = futex_ht_compare,
111 .remove_callback = futex_ht_remove_callback
112};
113
[669f3d32]114/** Task futex cache CHT operations. */
115static cht_ops_t task_futex_ht_ops = {
116 .hash = task_fut_ht_hash,
117 .key_hash = task_fut_ht_key_hash,
118 .equal = task_fut_ht_equal,
119 .key_equal = task_fut_ht_key_equal,
120 .remove_callback = NULL
121};
122
[9aa72b4]123/** Initialize futex subsystem. */
124void futex_init(void)
125{
[ee42e43]126 mutex_initialize(&futex_ht_lock, MUTEX_PASSIVE);
[9aa72b4]127 hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops);
128}
129
[669f3d32]130/** Initializes the futex structures for the new task. */
131void futex_task_init(struct task *task)
132{
133 task->futexes = malloc(sizeof(struct futex_cache), 0);
134
[c28413a9]135 cht_create(&task->futexes->ht, 0, 0, 0, true, &task_futex_ht_ops);
[669f3d32]136}
137
138/** Destroys the futex structures for the dying task. */
139void futex_task_deinit(struct task *task)
140{
141 /* Interrupts are disabled so we must not block (cannot run cht_destroy). */
142 if (interrupts_disabled()) {
143 /* Invoke the blocking cht_destroy in the background. */
144 workq_global_enqueue_noblock(&task->futexes->destroy_work,
145 destroy_task_cache);
146 } else {
147 /* We can block. Invoke cht_destroy in this thread. */
148 destroy_task_cache(&task->futexes->destroy_work);
149 }
150}
151
152/** Deallocates a task's CHT futex cache (must already be empty). */
153static void destroy_task_cache(work_t *work)
154{
155 struct futex_cache *cache =
156 member_to_inst(work, struct futex_cache, destroy_work);
157
158 cht_destroy(&cache->ht);
159 free(cache);
160}
161
162/** Remove references from futexes known to the current task. */
163void futex_task_cleanup(void)
164{
165 /* All threads of this task have terminated. This is the last thread. */
166 mutex_lock(&TASK->futex_list_lock);
167
168 list_foreach_safe(TASK->futex_list, cur_link, next_link) {
169 futex_ptr_t *fut_ptr = member_to_inst(cur_link, futex_ptr_t, cht_link);
170
171 /*
172 * The function is free to free the futex. All other threads of this
173 * task have already terminated, so they have also definitely
174 * exited their CHT futex cache protecting rcu reader sections.
175 * Moreover release_ref() only frees the futex if this is the
176 * last task referencing the futex. Therefore, only threads
177 * of this task may have referenced the futex if it is to be freed.
178 */
179 futex_release_ref_locked(fut_ptr->futex);
180
181 /*
182 * No need to remove the futex_ptr from the CHT cache of this task
183 * because futex_task_deinit() will destroy the CHT shortly.
184 */
185 list_remove(cur_link);
186 free(fut_ptr);
187 }
188
189 mutex_unlock(&TASK->futex_list_lock);
190}
191
192
193/** Initialize the kernel futex structure.
[9aa72b4]194 *
[669f3d32]195 * @param futex Kernel futex structure.
196 * @param paddr Physical address of the futex variable.
[9aa72b4]197 */
[669f3d32]198static void futex_initialize(futex_t *futex, uintptr_t paddr)
[9aa72b4]199{
200 waitq_initialize(&futex->wq);
201 link_initialize(&futex->ht_link);
[669f3d32]202 futex->paddr = paddr;
[4fded58]203 futex->refcount = 1;
[9aa72b4]204}
205
[669f3d32]206/** Increments the counter of tasks referencing the futex. */
207static void futex_add_ref(futex_t *futex)
208{
209 ASSERT(mutex_locked(&futex_ht_lock));
210 ASSERT(0 < futex->refcount);
211 ++futex->refcount;
212}
213
214/** Decrements the counter of tasks referencing the futex. May free the futex.*/
215static void futex_release_ref(futex_t *futex)
216{
217 ASSERT(mutex_locked(&futex_ht_lock));
218 ASSERT(0 < futex->refcount);
219
220 --futex->refcount;
221
222 if (0 == futex->refcount) {
223 hash_table_remove(&futex_ht, &futex->paddr, 1);
224 }
225}
226
227/** Decrements the counter of tasks referencing the futex. May free the futex.*/
228static void futex_release_ref_locked(futex_t *futex)
229{
230 mutex_lock(&futex_ht_lock);
231 futex_release_ref(futex);
232 mutex_unlock(&futex_ht_lock);
233}
234
235/** Returns a futex for the virtual address @a uaddr (or creates one). */
236static futex_t *get_futex(uintptr_t uaddr)
237{
238 futex_t *futex = find_cached_futex(uaddr);
239
240 if (futex)
241 return futex;
242
243 uintptr_t paddr;
244
245 if (!find_futex_paddr(uaddr, &paddr))
246 return 0;
247
248 return get_and_cache_futex(paddr, uaddr);
249}
250
251
252/** Finds the physical address of the futex variable. */
253static bool find_futex_paddr(uintptr_t uaddr, uintptr_t *paddr)
254{
255 page_table_lock(AS, true);
256
257 bool found = false;
258 pte_t *t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE), false);
259
260 if (t && PTE_VALID(t) && PTE_PRESENT(t)) {
261 found = true;
262 *paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
263 }
264
265 page_table_unlock(AS, true);
266
267 return found;
268}
269
270/** Returns the futex cached in this task with the virtual address uaddr. */
271static futex_t *find_cached_futex(uintptr_t uaddr)
272{
273 cht_read_lock();
274
275 futex_t *futex;
276 cht_link_t *futex_ptr_link = cht_find(&TASK->futexes->ht, &uaddr);
277
278 if (futex_ptr_link) {
279 futex_ptr_t *futex_ptr
280 = member_to_inst(futex_ptr_link, futex_ptr_t, cht_link);
281
282 futex = futex_ptr->futex;
283 } else {
284 futex = NULL;
285 }
286
287 cht_read_unlock();
288
289 return futex;
290}
291
292
293/**
294 * Returns a kernel futex for the physical address @a phys_addr and caches
295 * it in this task under the virtual address @a uaddr (if not already cached).
296 */
297static futex_t *get_and_cache_futex(uintptr_t phys_addr, uintptr_t uaddr)
298{
299 futex_t *futex = malloc(sizeof(futex_t), 0);
300
301 /*
302 * Find the futex object in the global futex table (or insert it
303 * if it is not present).
304 */
305 mutex_lock(&futex_ht_lock);
306
307 link_t *fut_link = hash_table_find(&futex_ht, &phys_addr);
308
309 if (fut_link) {
310 free(futex);
311 futex = member_to_inst(fut_link, futex_t, ht_link);
312 futex_add_ref(futex);
313 } else {
314 futex_initialize(futex, phys_addr);
315 hash_table_insert(&futex_ht, &phys_addr, &futex->ht_link);
316 }
317
318 mutex_unlock(&futex_ht_lock);
319
320 /*
321 * Cache the link to the futex object for this task.
322 */
323 futex_ptr_t *fut_ptr = malloc(sizeof(futex_ptr_t), 0);
324 cht_link_t *dup_link;
325
326 fut_ptr->futex = futex;
327 fut_ptr->uaddr = uaddr;
328
329 cht_read_lock();
330
331 /* Cache the mapping from the virtual address to the futex for this task. */
332 if (cht_insert_unique(&TASK->futexes->ht, &fut_ptr->cht_link, &dup_link)) {
333 mutex_lock(&TASK->futex_list_lock);
334 list_append(&fut_ptr->all_link, &TASK->futex_list);
335 mutex_unlock(&TASK->futex_list_lock);
336 } else {
337 /* Another thread of this task beat us to it. Use that mapping instead.*/
338 free(fut_ptr);
339 futex_release_ref_locked(futex);
340
341 futex_ptr_t *dup = member_to_inst(dup_link, futex_ptr_t, cht_link);
342 futex = dup->futex;
343 }
344
345 cht_read_unlock();
346
347 return futex;
348}
349
[9aa72b4]350/** Sleep in futex wait queue.
351 *
[4774a32]352 * @param uaddr Userspace address of the futex counter.
[9aa72b4]353 *
[4774a32]354 * @return If there is no physical mapping for uaddr ENOENT is
355 * returned. Otherwise returns a wait result as defined in
356 * synch.h.
[9aa72b4]357 */
[96b02eb9]358sysarg_t sys_futex_sleep(uintptr_t uaddr)
[9aa72b4]359{
[669f3d32]360 futex_t *futex = get_futex(uaddr);
[9aa72b4]361
[669f3d32]362 if (!futex)
[96b02eb9]363 return (sysarg_t) ENOENT;
[741fd16]364
365#ifdef CONFIG_UDEBUG
366 udebug_stoppable_begin();
367#endif
[669f3d32]368 int rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE);
[741fd16]369#ifdef CONFIG_UDEBUG
370 udebug_stoppable_end();
371#endif
[96b02eb9]372 return (sysarg_t) rc;
[9aa72b4]373}
374
375/** Wakeup one thread waiting in futex wait queue.
376 *
[4774a32]377 * @param uaddr Userspace address of the futex counter.
[9aa72b4]378 *
[4774a32]379 * @return ENOENT if there is no physical mapping for uaddr.
[9aa72b4]380 */
[96b02eb9]381sysarg_t sys_futex_wakeup(uintptr_t uaddr)
[9aa72b4]382{
[669f3d32]383 futex_t *futex = get_futex(uaddr);
[9aa72b4]384
[669f3d32]385 if (futex) {
386 waitq_wakeup(&futex->wq, WAKEUP_FIRST);
387 return 0;
388 } else {
[96b02eb9]389 return (sysarg_t) ENOENT;
[9aa72b4]390 }
[a9ef68b]391}
392
[9aa72b4]393
394/** Compute hash index into futex hash table.
395 *
[4774a32]396 * @param key Address where the key (i.e. physical address of futex
397 * counter) is stored.
[9aa72b4]398 *
[4774a32]399 * @return Index into futex hash table.
[9aa72b4]400 */
[96b02eb9]401size_t futex_ht_hash(sysarg_t *key)
[9aa72b4]402{
[98000fb]403 return (*key & (FUTEX_HT_SIZE - 1));
[9aa72b4]404}
405
406/** Compare futex hash table item with a key.
407 *
[4774a32]408 * @param key Address where the key (i.e. physical address of futex
409 * counter) is stored.
[9aa72b4]410 *
[4774a32]411 * @return True if the item matches the key. False otherwise.
[9aa72b4]412 */
[96b02eb9]413bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item)
[9aa72b4]414{
415 futex_t *futex;
416
417 ASSERT(keys == 1);
418
419 futex = hash_table_get_instance(item, futex_t, ht_link);
420 return *key == futex->paddr;
421}
422
423/** Callback for removal items from futex hash table.
424 *
[4774a32]425 * @param item Item removed from the hash table.
[9aa72b4]426 */
427void futex_ht_remove_callback(link_t *item)
428{
429 futex_t *futex;
430
431 futex = hash_table_get_instance(item, futex_t, ht_link);
432 free(futex);
433}
[e090e1bc]434
[669f3d32]435/*
436 * Operations of a task's CHT that caches mappings of futex user space
437 * virtual addresses to kernel futex objects.
438 */
439
440static size_t task_fut_ht_hash(const cht_link_t *link)
[e090e1bc]441{
[669f3d32]442 const futex_ptr_t *fut_ptr = member_to_inst(link, futex_ptr_t, cht_link);
443 return fut_ptr->uaddr;
444}
[9c1c6771]445
[669f3d32]446static size_t task_fut_ht_key_hash(void *key)
447{
448 return *(uintptr_t*)key;
449}
450
451static bool task_fut_ht_equal(const cht_link_t *item1, const cht_link_t *item2)
452{
453 const futex_ptr_t *fut_ptr1 = member_to_inst(item1, futex_ptr_t, cht_link);
454 const futex_ptr_t *fut_ptr2 = member_to_inst(item2, futex_ptr_t, cht_link);
[9c1c6771]455
[669f3d32]456 return fut_ptr1->uaddr == fut_ptr2->uaddr;
[e090e1bc]457}
[b45c443]458
[669f3d32]459static bool task_fut_ht_key_equal(void *key, const cht_link_t *item)
460{
461 const futex_ptr_t *fut_ptr = member_to_inst(item, futex_ptr_t, cht_link);
462 uintptr_t uaddr = *(uintptr_t*)key;
463
464 return fut_ptr->uaddr == uaddr;
465}
466
467
[cc73a8a1]468/** @}
[b45c443]469 */
Note: See TracBrowser for help on using the repository browser.