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

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

Fixed kernel futex cleanup: now walks list via correct links and frees items once it is sure cht is not resizing.

  • Property mode set to 100644
File size: 14.0 KB
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
3 * Copyright (c) 2012 Adam Hraska
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
30/** @addtogroup sync
31 * @{
32 */
33
34/**
35 * @file
36 * @brief Kernel backend for futexes.
37 *
38 * Kernel futex objects are stored in a global hash table futex_ht
39 * where the physical address of the futex variable (futex_t.paddr)
40 * is used as the lookup key. As a result multiple address spaces
41 * may share the same futex variable.
42 *
43 * A kernel futex object is created the first time a task accesses
44 * the futex (having a futex variable at a physical address not
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.
50 *
51 * Each task keeps track of the futex objects it accessed in a list
52 * of pointers (futex_ptr_t, task->futex_list) to the different futex
53 * objects.
54 *
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
59 * to the page table translates a futex variable's virtual address
60 * into its futex kernel object.
61 */
62
63#include <synch/futex.h>
64#include <synch/mutex.h>
65#include <synch/spinlock.h>
66#include <synch/rcu.h>
67#include <mm/frame.h>
68#include <mm/page.h>
69#include <mm/slab.h>
70#include <proc/thread.h>
71#include <proc/task.h>
72#include <genarch/mm/page_pt.h>
73#include <genarch/mm/page_ht.h>
74#include <adt/cht.h>
75#include <adt/hash_table.h>
76#include <adt/list.h>
77#include <arch.h>
78#include <align.h>
79#include <panic.h>
80#include <errno.h>
81
82#define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */
83
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);
108
109static size_t futex_ht_hash(sysarg_t *key);
110static bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item);
111static void futex_ht_remove_callback(link_t *item);
112
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
119/** Mutex protecting the global futex hash table.
120 *
121 * Acquire task specific TASK->futex_list_lock before this mutex.
122 */
123static mutex_t futex_ht_lock;
124
125/** Global kernel futex hash table. Lock futex_ht_lock before accessing.
126 *
127 * Physical address of the futex variable is the lookup key.
128 */
129static hash_table_t futex_ht;
130
131/** Global kernel futex hash table operations. */
132static hash_table_operations_t futex_ht_ops = {
133 .hash = futex_ht_hash,
134 .compare = futex_ht_compare,
135 .remove_callback = futex_ht_remove_callback
136};
137
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
147/** Initialize futex subsystem. */
148void futex_init(void)
149{
150 mutex_initialize(&futex_ht_lock, MUTEX_PASSIVE);
151 hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops);
152}
153
154/** Initializes the futex structures for the new task. */
155void futex_task_init(struct task *task)
156{
157 task->futexes = malloc(sizeof(struct futex_cache), 0);
158
159 cht_create(&task->futexes->ht, 0, 0, 0, true, &task_futex_ht_ops);
160
161 list_initialize(&task->futexes->list);
162 mutex_initialize(&task->futexes->list_lock, MUTEX_PASSIVE);
163}
164
165/** Destroys the futex structures for the dying task. */
166void futex_task_deinit(task_t *task)
167{
168 /* Interrupts are disabled so we must not block (cannot run cht_destroy). */
169 if (interrupts_disabled()) {
170 /* Invoke the blocking cht_destroy in the background. */
171 workq_global_enqueue_noblock(&task->futexes->destroy_work,
172 destroy_task_cache);
173 } else {
174 /* We can block. Invoke cht_destroy in this thread. */
175 destroy_task_cache(&task->futexes->destroy_work);
176 }
177}
178
179/** Deallocates a task's CHT futex cache (must already be empty). */
180static void destroy_task_cache(work_t *work)
181{
182 struct futex_cache *cache =
183 member_to_inst(work, struct futex_cache, destroy_work);
184
185 /*
186 * Destroy the cache before manually freeing items of the cache in case
187 * table resize is in progress.
188 */
189 cht_destroy_unsafe(&cache->ht);
190
191 /* Manually free futex_ptr cache items. */
192 list_foreach_safe(cache->list, cur_link, next_link) {
193 futex_ptr_t *fut_ptr = member_to_inst(cur_link, futex_ptr_t, all_link);
194
195 list_remove(cur_link);
196 free(fut_ptr);
197 }
198
199 free(cache);
200}
201
202/** Remove references from futexes known to the current task. */
203void futex_task_cleanup(void)
204{
205 struct futex_cache *futexes = TASK->futexes;
206
207 /* All threads of this task have terminated. This is the last thread. */
208 mutex_lock(&futexes->list_lock);
209
210 list_foreach_safe(futexes->list, cur_link, next_link) {
211 futex_ptr_t *fut_ptr = member_to_inst(cur_link, futex_ptr_t, all_link);
212
213 /*
214 * The function is free to free the futex. All other threads of this
215 * task have already terminated, so they have also definitely
216 * exited their CHT futex cache protecting rcu reader sections.
217 * Moreover release_ref() only frees the futex if this is the
218 * last task referencing the futex. Therefore, only threads
219 * of this task may have referenced the futex if it is to be freed.
220 */
221 futex_release_ref_locked(fut_ptr->futex);
222 }
223
224 mutex_unlock(&futexes->list_lock);
225}
226
227
228/** Initialize the kernel futex structure.
229 *
230 * @param futex Kernel futex structure.
231 * @param paddr Physical address of the futex variable.
232 */
233static void futex_initialize(futex_t *futex, uintptr_t paddr)
234{
235 waitq_initialize(&futex->wq);
236 link_initialize(&futex->ht_link);
237 futex->paddr = paddr;
238 futex->refcount = 1;
239}
240
241/** Increments the counter of tasks referencing the futex. */
242static void futex_add_ref(futex_t *futex)
243{
244 ASSERT(mutex_locked(&futex_ht_lock));
245 ASSERT(0 < futex->refcount);
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{
252 ASSERT(mutex_locked(&futex_ht_lock));
253 ASSERT(0 < futex->refcount);
254
255 --futex->refcount;
256
257 if (0 == futex->refcount) {
258 hash_table_remove(&futex_ht, &futex->paddr, 1);
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{
265 mutex_lock(&futex_ht_lock);
266 futex_release_ref(futex);
267 mutex_unlock(&futex_ht_lock);
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);
274
275 if (futex)
276 return futex;
277
278 uintptr_t paddr;
279
280 if (!find_futex_paddr(uaddr, &paddr))
281 return 0;
282
283 return get_and_cache_futex(paddr, uaddr);
284}
285
286
287/** Finds the physical address of the futex variable. */
288static bool find_futex_paddr(uintptr_t uaddr, uintptr_t *paddr)
289{
290 page_table_lock(AS, true);
291
292 bool found = false;
293 pte_t *t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE), false);
294
295 if (t && PTE_VALID(t) && PTE_PRESENT(t)) {
296 found = true;
297 *paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
298 }
299
300 page_table_unlock(AS, true);
301
302 return found;
303}
304
305/** Returns the futex cached in this task with the virtual address uaddr. */
306static futex_t *find_cached_futex(uintptr_t uaddr)
307{
308 cht_read_lock();
309
310 futex_t *futex;
311 cht_link_t *futex_ptr_link = cht_find_lazy(&TASK->futexes->ht, &uaddr);
312
313 if (futex_ptr_link) {
314 futex_ptr_t *futex_ptr
315 = member_to_inst(futex_ptr_link, futex_ptr_t, cht_link);
316
317 futex = futex_ptr->futex;
318 } else {
319 futex = NULL;
320 }
321
322 cht_read_unlock();
323
324 return futex;
325}
326
327
328/**
329 * Returns a kernel futex for the physical address @a phys_addr and caches
330 * it in this task under the virtual address @a uaddr (if not already cached).
331 */
332static futex_t *get_and_cache_futex(uintptr_t phys_addr, uintptr_t uaddr)
333{
334 futex_t *futex = malloc(sizeof(futex_t), 0);
335
336 /*
337 * Find the futex object in the global futex table (or insert it
338 * if it is not present).
339 */
340 mutex_lock(&futex_ht_lock);
341
342 link_t *fut_link = hash_table_find(&futex_ht, &phys_addr);
343
344 if (fut_link) {
345 free(futex);
346 futex = member_to_inst(fut_link, futex_t, ht_link);
347 futex_add_ref(futex);
348 } else {
349 futex_initialize(futex, phys_addr);
350 hash_table_insert(&futex_ht, &phys_addr, &futex->ht_link);
351 }
352
353 mutex_unlock(&futex_ht_lock);
354
355 /*
356 * Cache the link to the futex object for this task.
357 */
358 futex_ptr_t *fut_ptr = malloc(sizeof(futex_ptr_t), 0);
359 cht_link_t *dup_link;
360
361 fut_ptr->futex = futex;
362 fut_ptr->uaddr = uaddr;
363
364 cht_read_lock();
365
366 /* Cache the mapping from the virtual address to the futex for this task. */
367 if (cht_insert_unique(&TASK->futexes->ht, &fut_ptr->cht_link, &dup_link)) {
368 mutex_lock(&TASK->futexes->list_lock);
369 list_append(&fut_ptr->all_link, &TASK->futexes->list);
370 mutex_unlock(&TASK->futexes->list_lock);
371 } else {
372 /* Another thread of this task beat us to it. Use that mapping instead.*/
373 free(fut_ptr);
374 futex_release_ref_locked(futex);
375
376 futex_ptr_t *dup = member_to_inst(dup_link, futex_ptr_t, cht_link);
377 futex = dup->futex;
378 }
379
380 cht_read_unlock();
381
382 return futex;
383}
384
385/** Sleep in futex wait queue.
386 *
387 * @param uaddr Userspace address of the futex counter.
388 *
389 * @return If there is no physical mapping for uaddr ENOENT is
390 * returned. Otherwise returns a wait result as defined in
391 * synch.h.
392 */
393sysarg_t sys_futex_sleep(uintptr_t uaddr)
394{
395 futex_t *futex = get_futex(uaddr);
396
397 if (!futex)
398 return (sysarg_t) ENOENT;
399
400#ifdef CONFIG_UDEBUG
401 udebug_stoppable_begin();
402#endif
403 int rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE);
404#ifdef CONFIG_UDEBUG
405 udebug_stoppable_end();
406#endif
407 return (sysarg_t) rc;
408}
409
410/** Wakeup one thread waiting in futex wait queue.
411 *
412 * @param uaddr Userspace address of the futex counter.
413 *
414 * @return ENOENT if there is no physical mapping for uaddr.
415 */
416sysarg_t sys_futex_wakeup(uintptr_t uaddr)
417{
418 futex_t *futex = get_futex(uaddr);
419
420 if (futex) {
421 waitq_wakeup(&futex->wq, WAKEUP_FIRST);
422 return 0;
423 } else {
424 return (sysarg_t) ENOENT;
425 }
426}
427
428
429/** Compute hash index into futex hash table.
430 *
431 * @param key Address where the key (i.e. physical address of futex
432 * counter) is stored.
433 *
434 * @return Index into futex hash table.
435 */
436size_t futex_ht_hash(sysarg_t *key)
437{
438 return (*key & (FUTEX_HT_SIZE - 1));
439}
440
441/** Compare futex hash table item with a key.
442 *
443 * @param key Address where the key (i.e. physical address of futex
444 * counter) is stored.
445 *
446 * @return True if the item matches the key. False otherwise.
447 */
448bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item)
449{
450 futex_t *futex;
451
452 ASSERT(keys == 1);
453
454 futex = hash_table_get_instance(item, futex_t, ht_link);
455 return *key == futex->paddr;
456}
457
458/** Callback for removal items from futex hash table.
459 *
460 * @param item Item removed from the hash table.
461 */
462void futex_ht_remove_callback(link_t *item)
463{
464 futex_t *futex;
465
466 futex = hash_table_get_instance(item, futex_t, ht_link);
467 free(futex);
468}
469
470/*
471 * Operations of a task's CHT that caches mappings of futex user space
472 * virtual addresses to kernel futex objects.
473 */
474
475static size_t task_fut_ht_hash(const cht_link_t *link)
476{
477 const futex_ptr_t *fut_ptr = member_to_inst(link, futex_ptr_t, cht_link);
478 return fut_ptr->uaddr;
479}
480
481static size_t task_fut_ht_key_hash(void *key)
482{
483 return *(uintptr_t*)key;
484}
485
486static bool task_fut_ht_equal(const cht_link_t *item1, const cht_link_t *item2)
487{
488 const futex_ptr_t *fut_ptr1 = member_to_inst(item1, futex_ptr_t, cht_link);
489 const futex_ptr_t *fut_ptr2 = member_to_inst(item2, futex_ptr_t, cht_link);
490
491 return fut_ptr1->uaddr == fut_ptr2->uaddr;
492}
493
494static bool task_fut_ht_key_equal(void *key, const cht_link_t *item)
495{
496 const futex_ptr_t *fut_ptr = member_to_inst(item, futex_ptr_t, cht_link);
497 uintptr_t uaddr = *(uintptr_t*)key;
498
499 return fut_ptr->uaddr == uaddr;
500}
501
502
503/** @}
504 */
Note: See TracBrowser for help on using the repository browser.