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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f6372be9 was 11b285d, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Use standard signature for malloc() in kernel.

The remaining instances of blocking allocation are replaced with
a new separate function named nfmalloc (short for non-failing malloc).

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