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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cc74cb5 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 14.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.
[1b20da0]37 *
38 * Kernel futex objects are stored in a global hash table futex_ht
[ba368a6]39 * where the physical address of the futex variable (futex_t.paddr)
[1b20da0]40 * is used as the lookup key. As a result multiple address spaces
41 * may share the same futex variable.
42 *
[ba368a6]43 * A kernel futex object is created the first time a task accesses
[1b20da0]44 * the futex (having a futex variable at a physical address not
[ba368a6]45 * encountered before). Futex object's lifetime is governed by
46 * a reference count that represents the number of all the different
47 * user space virtual addresses from all tasks that map to the
48 * physical address of the futex variable. A futex object is freed
49 * when the last task having accessed the futex exits.
[1b20da0]50 *
[ba368a6]51 * Each task keeps track of the futex objects it accessed in a list
[1b20da0]52 * of pointers (futex_ptr_t, task->futex_list) to the different futex
[ba368a6]53 * objects.
[1b20da0]54 *
[ba368a6]55 * To speed up translation of futex variables' virtual addresses
56 * to their physical addresses, futex pointers accessed by the
57 * task are furthermore stored in a concurrent hash table (CHT,
58 * task->futexes->ht). A single lookup without locks or accesses
[1b20da0]59 * to the page table translates a futex variable's virtual address
60 * into its futex kernel object.
[9aa72b4]61 */
62
[63e27ef]63#include <assert.h>
[9aa72b4]64#include <synch/futex.h>
[ee42e43]65#include <synch/mutex.h>
[303c94c]66#include <synch/spinlock.h>
[669f3d32]67#include <synch/rcu.h>
[9aa72b4]68#include <mm/frame.h>
69#include <mm/page.h>
70#include <mm/slab.h>
[303c94c]71#include <proc/thread.h>
[4fded58]72#include <proc/task.h>
[9aa72b4]73#include <genarch/mm/page_pt.h>
74#include <genarch/mm/page_ht.h>
[669f3d32]75#include <adt/cht.h>
[82cbf8c6]76#include <adt/hash.h>
[9aa72b4]77#include <adt/hash_table.h>
78#include <adt/list.h>
79#include <arch.h>
80#include <align.h>
81#include <panic.h>
82#include <errno.h>
83
[669f3d32]84/** Task specific pointer to a global kernel futex object. */
85typedef struct futex_ptr {
86 /** CHT link. */
87 cht_link_t cht_link;
88 /** List of all futex pointers used by the task. */
89 link_t all_link;
90 /** Kernel futex object. */
91 futex_t *futex;
92 /** User space virtual address of the futex variable in the task. */
93 uintptr_t uaddr;
94} futex_ptr_t;
95
96static void destroy_task_cache(work_t *work);
97
98static void futex_initialize(futex_t *futex, uintptr_t paddr);
99static void futex_add_ref(futex_t *futex);
100static void futex_release_ref(futex_t *futex);
101static void futex_release_ref_locked(futex_t *futex);
102
103static futex_t *get_futex(uintptr_t uaddr);
104static futex_t *find_cached_futex(uintptr_t uaddr);
105static futex_t *get_and_cache_futex(uintptr_t phys_addr, uintptr_t uaddr);
106static bool find_futex_paddr(uintptr_t uaddr, uintptr_t *phys_addr);
[9aa72b4]107
[82cbf8c6]108static size_t futex_ht_hash(const ht_link_t *item);
109static size_t futex_ht_key_hash(void *key);
110static bool futex_ht_key_equal(void *key, const ht_link_t *item);
111static void futex_ht_remove_callback(ht_link_t *item);
[9aa72b4]112
[669f3d32]113static size_t task_fut_ht_hash(const cht_link_t *link);
114static size_t task_fut_ht_key_hash(void *key);
115static bool task_fut_ht_equal(const cht_link_t *item1, const cht_link_t *item2);
116static bool task_fut_ht_key_equal(void *key, const cht_link_t *item);
117
118/** Mutex protecting the global futex hash table.
[1b20da0]119 *
[669f3d32]120 * Acquire task specific TASK->futex_list_lock before this mutex.
[4fded58]121 */
[207e8880]122SPINLOCK_STATIC_INITIALIZE_NAME(futex_ht_lock, "futex-ht-lock");
[9aa72b4]123
[669f3d32]124/** Global kernel futex hash table. Lock futex_ht_lock before accessing.
[1b20da0]125 *
[669f3d32]126 * Physical address of the futex variable is the lookup key.
127 */
[9aa72b4]128static hash_table_t futex_ht;
129
[669f3d32]130/** Global kernel futex hash table operations. */
[82cbf8c6]131static hash_table_ops_t futex_ht_ops = {
[9aa72b4]132 .hash = futex_ht_hash,
[82cbf8c6]133 .key_hash = futex_ht_key_hash,
134 .key_equal = futex_ht_key_equal,
[9aa72b4]135 .remove_callback = futex_ht_remove_callback
136};
137
[669f3d32]138/** Task futex cache CHT operations. */
139static cht_ops_t task_futex_ht_ops = {
140 .hash = task_fut_ht_hash,
141 .key_hash = task_fut_ht_key_hash,
142 .equal = task_fut_ht_equal,
143 .key_equal = task_fut_ht_key_equal,
144 .remove_callback = NULL
145};
146
[9aa72b4]147/** Initialize futex subsystem. */
148void futex_init(void)
149{
[82cbf8c6]150 hash_table_create(&futex_ht, 0, 0, &futex_ht_ops);
[9aa72b4]151}
152
[669f3d32]153/** Initializes the futex structures for the new task. */
154void futex_task_init(struct task *task)
155{
[11b285d]156 task->futexes = nfmalloc(sizeof(struct futex_cache));
[a35b458]157
[c28413a9]158 cht_create(&task->futexes->ht, 0, 0, 0, true, &task_futex_ht_ops);
[a35b458]159
[3ac5086]160 list_initialize(&task->futexes->list);
[207e8880]161 spinlock_initialize(&task->futexes->list_lock, "futex-list-lock");
[669f3d32]162}
163
164/** Destroys the futex structures for the dying task. */
[3ac5086]165void futex_task_deinit(task_t *task)
[669f3d32]166{
167 /* Interrupts are disabled so we must not block (cannot run cht_destroy). */
168 if (interrupts_disabled()) {
169 /* Invoke the blocking cht_destroy in the background. */
[1b20da0]170 workq_global_enqueue_noblock(&task->futexes->destroy_work,
[3bacee1]171 destroy_task_cache);
[669f3d32]172 } else {
173 /* We can block. Invoke cht_destroy in this thread. */
174 destroy_task_cache(&task->futexes->destroy_work);
175 }
176}
177
178/** Deallocates a task's CHT futex cache (must already be empty). */
179static void destroy_task_cache(work_t *work)
180{
[1b20da0]181 struct futex_cache *cache =
[3bacee1]182 member_to_inst(work, struct futex_cache, destroy_work);
[a35b458]183
[1b20da0]184 /*
[3ac5086]185 * Destroy the cache before manually freeing items of the cache in case
186 * table resize is in progress.
187 */
188 cht_destroy_unsafe(&cache->ht);
[a35b458]189
[3ac5086]190 /* Manually free futex_ptr cache items. */
191 list_foreach_safe(cache->list, cur_link, next_link) {
192 futex_ptr_t *fut_ptr = member_to_inst(cur_link, futex_ptr_t, all_link);
193
194 list_remove(cur_link);
195 free(fut_ptr);
196 }
[a35b458]197
[669f3d32]198 free(cache);
199}
200
201/** Remove references from futexes known to the current task. */
202void futex_task_cleanup(void)
203{
[3ac5086]204 struct futex_cache *futexes = TASK->futexes;
[a35b458]205
[669f3d32]206 /* All threads of this task have terminated. This is the last thread. */
[207e8880]207 spinlock_lock(&futexes->list_lock);
[a35b458]208
[3ac5086]209 list_foreach_safe(futexes->list, cur_link, next_link) {
210 futex_ptr_t *fut_ptr = member_to_inst(cur_link, futex_ptr_t, all_link);
211
[669f3d32]212 /*
213 * The function is free to free the futex. All other threads of this
214 * task have already terminated, so they have also definitely
215 * exited their CHT futex cache protecting rcu reader sections.
[1b20da0]216 * Moreover release_ref() only frees the futex if this is the
[669f3d32]217 * last task referencing the futex. Therefore, only threads
218 * of this task may have referenced the futex if it is to be freed.
219 */
220 futex_release_ref_locked(fut_ptr->futex);
221 }
[a35b458]222
[207e8880]223 spinlock_unlock(&futexes->list_lock);
[669f3d32]224}
225
226/** Initialize the kernel futex structure.
[9aa72b4]227 *
[669f3d32]228 * @param futex Kernel futex structure.
229 * @param paddr Physical address of the futex variable.
[9aa72b4]230 */
[669f3d32]231static void futex_initialize(futex_t *futex, uintptr_t paddr)
[9aa72b4]232{
233 waitq_initialize(&futex->wq);
[669f3d32]234 futex->paddr = paddr;
[4fded58]235 futex->refcount = 1;
[9aa72b4]236}
237
[669f3d32]238/** Increments the counter of tasks referencing the futex. */
239static void futex_add_ref(futex_t *futex)
240{
[63e27ef]241 assert(spinlock_locked(&futex_ht_lock));
242 assert(0 < futex->refcount);
[669f3d32]243 ++futex->refcount;
244}
245
246/** Decrements the counter of tasks referencing the futex. May free the futex.*/
247static void futex_release_ref(futex_t *futex)
248{
[63e27ef]249 assert(spinlock_locked(&futex_ht_lock));
250 assert(0 < futex->refcount);
[a35b458]251
[669f3d32]252 --futex->refcount;
[a35b458]253
[669f3d32]254 if (0 == futex->refcount) {
[82cbf8c6]255 hash_table_remove(&futex_ht, &futex->paddr);
[669f3d32]256 }
257}
258
259/** Decrements the counter of tasks referencing the futex. May free the futex.*/
260static void futex_release_ref_locked(futex_t *futex)
261{
[207e8880]262 spinlock_lock(&futex_ht_lock);
[669f3d32]263 futex_release_ref(futex);
[207e8880]264 spinlock_unlock(&futex_ht_lock);
[669f3d32]265}
266
267/** Returns a futex for the virtual address @a uaddr (or creates one). */
268static futex_t *get_futex(uintptr_t uaddr)
269{
270 futex_t *futex = find_cached_futex(uaddr);
[a35b458]271
[1b20da0]272 if (futex)
[669f3d32]273 return futex;
274
275 uintptr_t paddr;
276
[207e8880]277 if (!find_futex_paddr(uaddr, &paddr)) {
[669f3d32]278 return 0;
[207e8880]279 }
[669f3d32]280
281 return get_and_cache_futex(paddr, uaddr);
282}
283
284/** Finds the physical address of the futex variable. */
285static bool find_futex_paddr(uintptr_t uaddr, uintptr_t *paddr)
286{
[207e8880]287 page_table_lock(AS, false);
[759ea0d]288 spinlock_lock(&futex_ht_lock);
[669f3d32]289
[38dc82d]290 bool success = false;
291
292 pte_t t;
293 bool found;
294
295 found = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE), true, &t);
296 if (found && PTE_VALID(&t) && PTE_PRESENT(&t)) {
297 success = true;
298 *paddr = PTE_GET_FRAME(&t) +
299 (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
[669f3d32]300 }
[a35b458]301
[207e8880]302 spinlock_unlock(&futex_ht_lock);
[759ea0d]303 page_table_unlock(AS, false);
[a35b458]304
[38dc82d]305 return success;
[669f3d32]306}
307
308/** Returns the futex cached in this task with the virtual address uaddr. */
309static futex_t *find_cached_futex(uintptr_t uaddr)
310{
311 cht_read_lock();
[a35b458]312
[669f3d32]313 futex_t *futex;
[c0c26ac]314 cht_link_t *futex_ptr_link = cht_find_lazy(&TASK->futexes->ht, &uaddr);
[669f3d32]315
316 if (futex_ptr_link) {
[3bacee1]317 futex_ptr_t *futex_ptr =
318 member_to_inst(futex_ptr_link, futex_ptr_t, cht_link);
[a35b458]319
[669f3d32]320 futex = futex_ptr->futex;
321 } else {
322 futex = NULL;
323 }
[a35b458]324
[669f3d32]325 cht_read_unlock();
[a35b458]326
[669f3d32]327 return futex;
328}
329
[1b20da0]330/**
331 * Returns a kernel futex for the physical address @a phys_addr and caches
[669f3d32]332 * it in this task under the virtual address @a uaddr (if not already cached).
333 */
334static futex_t *get_and_cache_futex(uintptr_t phys_addr, uintptr_t uaddr)
335{
[11b285d]336 futex_t *futex = malloc(sizeof(futex_t));
[7473807]337 if (!futex)
338 return NULL;
[a35b458]339
[1b20da0]340 /*
341 * Find the futex object in the global futex table (or insert it
[669f3d32]342 * if it is not present).
343 */
[207e8880]344 spinlock_lock(&futex_ht_lock);
[a35b458]345
[82cbf8c6]346 ht_link_t *fut_link = hash_table_find(&futex_ht, &phys_addr);
[a35b458]347
[669f3d32]348 if (fut_link) {
349 free(futex);
350 futex = member_to_inst(fut_link, futex_t, ht_link);
351 futex_add_ref(futex);
352 } else {
353 futex_initialize(futex, phys_addr);
[82cbf8c6]354 hash_table_insert(&futex_ht, &futex->ht_link);
[669f3d32]355 }
[a35b458]356
[207e8880]357 spinlock_unlock(&futex_ht_lock);
[a35b458]358
[1b20da0]359 /*
360 * Cache the link to the futex object for this task.
[669f3d32]361 */
[11b285d]362 futex_ptr_t *fut_ptr = malloc(sizeof(futex_ptr_t));
[7473807]363 if (!fut_ptr) {
364 spinlock_lock(&futex_ht_lock);
365 futex_release_ref(futex);
366 spinlock_unlock(&futex_ht_lock);
367 return NULL;
368 }
[669f3d32]369 cht_link_t *dup_link;
[a35b458]370
[669f3d32]371 fut_ptr->futex = futex;
372 fut_ptr->uaddr = uaddr;
[a35b458]373
[669f3d32]374 cht_read_lock();
[a35b458]375
[669f3d32]376 /* Cache the mapping from the virtual address to the futex for this task. */
377 if (cht_insert_unique(&TASK->futexes->ht, &fut_ptr->cht_link, &dup_link)) {
[207e8880]378 spinlock_lock(&TASK->futexes->list_lock);
[3ac5086]379 list_append(&fut_ptr->all_link, &TASK->futexes->list);
[207e8880]380 spinlock_unlock(&TASK->futexes->list_lock);
[669f3d32]381 } else {
382 /* Another thread of this task beat us to it. Use that mapping instead.*/
383 free(fut_ptr);
384 futex_release_ref_locked(futex);
[a35b458]385
[669f3d32]386 futex_ptr_t *dup = member_to_inst(dup_link, futex_ptr_t, cht_link);
[1b20da0]387 futex = dup->futex;
[669f3d32]388 }
389
390 cht_read_unlock();
[a35b458]391
[669f3d32]392 return futex;
393}
394
[b59318e]395/** Sleep in futex wait queue with a timeout.
396 * If the sleep times out or is interrupted, the next wakeup is ignored.
397 * The userspace portion of the call must handle this condition.
[9aa72b4]398 *
[b59318e]399 * @param uaddr Userspace address of the futex counter.
400 * @param timeout Maximum number of useconds to sleep. 0 means no limit.
[9aa72b4]401 *
[4774a32]402 * @return If there is no physical mapping for uaddr ENOENT is
[897fd8f1]403 * returned. Otherwise returns the return value of
404 * waitq_sleep_timeout().
[9aa72b4]405 */
[b59318e]406sys_errno_t sys_futex_sleep(uintptr_t uaddr, uintptr_t timeout)
[9aa72b4]407{
[669f3d32]408 futex_t *futex = get_futex(uaddr);
[a35b458]409
[1b20da0]410 if (!futex)
[b7fd2a0]411 return (sys_errno_t) ENOENT;
[741fd16]412
[496232e]413#ifdef CONFIG_UDEBUG
414 udebug_stoppable_begin();
415#endif
416
[b59318e]417 errno_t rc = waitq_sleep_timeout(&futex->wq, timeout,
418 SYNCH_FLAGS_INTERRUPTIBLE | SYNCH_FLAGS_FUTEX, NULL);
[207e8880]419
[496232e]420#ifdef CONFIG_UDEBUG
421 udebug_stoppable_end();
422#endif
423
[b7fd2a0]424 return (sys_errno_t) rc;
[9aa72b4]425}
426
427/** Wakeup one thread waiting in futex wait queue.
428 *
[4774a32]429 * @param uaddr Userspace address of the futex counter.
[9aa72b4]430 *
[4774a32]431 * @return ENOENT if there is no physical mapping for uaddr.
[9aa72b4]432 */
[b7fd2a0]433sys_errno_t sys_futex_wakeup(uintptr_t uaddr)
[9aa72b4]434{
[669f3d32]435 futex_t *futex = get_futex(uaddr);
[a35b458]436
[669f3d32]437 if (futex) {
438 waitq_wakeup(&futex->wq, WAKEUP_FIRST);
[897fd8f1]439 return EOK;
[669f3d32]440 } else {
[b7fd2a0]441 return (sys_errno_t) ENOENT;
[9aa72b4]442 }
[a9ef68b]443}
444
[82cbf8c6]445/** Return the hash of the key stored in the item */
446size_t futex_ht_hash(const ht_link_t *item)
[9aa72b4]447{
[82cbf8c6]448 futex_t *futex = hash_table_get_inst(item, futex_t, ht_link);
449 return hash_mix(futex->paddr);
[9aa72b4]450}
451
[82cbf8c6]452/** Return the hash of the key */
453size_t futex_ht_key_hash(void *key)
[9aa72b4]454{
[82cbf8c6]455 uintptr_t *paddr = (uintptr_t *) key;
456 return hash_mix(*paddr);
457}
[9aa72b4]458
[82cbf8c6]459/** Return true if the key is equal to the item's lookup key. */
460bool futex_ht_key_equal(void *key, const ht_link_t *item)
461{
462 uintptr_t *paddr = (uintptr_t *) key;
463 futex_t *futex = hash_table_get_inst(item, futex_t, ht_link);
464 return *paddr == futex->paddr;
[9aa72b4]465}
466
467/** Callback for removal items from futex hash table.
468 *
[4774a32]469 * @param item Item removed from the hash table.
[9aa72b4]470 */
[82cbf8c6]471void futex_ht_remove_callback(ht_link_t *item)
[9aa72b4]472{
473 futex_t *futex;
474
[82cbf8c6]475 futex = hash_table_get_inst(item, futex_t, ht_link);
[9aa72b4]476 free(futex);
477}
[e090e1bc]478
[669f3d32]479/*
[1b20da0]480 * Operations of a task's CHT that caches mappings of futex user space
[669f3d32]481 * virtual addresses to kernel futex objects.
482 */
483
484static size_t task_fut_ht_hash(const cht_link_t *link)
[e090e1bc]485{
[669f3d32]486 const futex_ptr_t *fut_ptr = member_to_inst(link, futex_ptr_t, cht_link);
487 return fut_ptr->uaddr;
488}
[9c1c6771]489
[669f3d32]490static size_t task_fut_ht_key_hash(void *key)
491{
[3bacee1]492 return *(uintptr_t *)key;
[669f3d32]493}
494
495static bool task_fut_ht_equal(const cht_link_t *item1, const cht_link_t *item2)
496{
497 const futex_ptr_t *fut_ptr1 = member_to_inst(item1, futex_ptr_t, cht_link);
498 const futex_ptr_t *fut_ptr2 = member_to_inst(item2, futex_ptr_t, cht_link);
[a35b458]499
[669f3d32]500 return fut_ptr1->uaddr == fut_ptr2->uaddr;
[e090e1bc]501}
[b45c443]502
[669f3d32]503static bool task_fut_ht_key_equal(void *key, const cht_link_t *item)
504{
505 const futex_ptr_t *fut_ptr = member_to_inst(item, futex_ptr_t, cht_link);
[3bacee1]506 uintptr_t uaddr = *(uintptr_t *)key;
[a35b458]507
[669f3d32]508 return fut_ptr->uaddr == uaddr;
[e090e1bc]509}
[b45c443]510
[cc73a8a1]511/** @}
[b45c443]512 */
Note: See TracBrowser for help on using the repository browser.