1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2023 Red Hat
6 #include "thread-registry.h"
8 #include <linux/rculist.h>
10 #include "permassert.h"
13 * We need to be careful when using other facilities that may use thread registry functions in
14 * their normal operation. For example, we do not want to invoke the logger while holding a lock.
17 void vdo_initialize_thread_registry(struct thread_registry *registry)
19 INIT_LIST_HEAD(®istry->links);
20 spin_lock_init(®istry->lock);
23 /* Register the current thread and associate it with a data pointer. */
24 void vdo_register_thread(struct thread_registry *registry,
25 struct registered_thread *new_thread, const void *pointer)
27 struct registered_thread *thread;
28 bool found_it = false;
30 INIT_LIST_HEAD(&new_thread->links);
31 new_thread->pointer = pointer;
32 new_thread->task = current;
34 spin_lock(®istry->lock);
35 list_for_each_entry(thread, ®istry->links, links) {
36 if (thread->task == current) {
37 /* There should be no existing entry. */
38 list_del_rcu(&thread->links);
43 list_add_tail_rcu(&new_thread->links, ®istry->links);
44 spin_unlock(®istry->lock);
46 ASSERT_LOG_ONLY(!found_it, "new thread not already in registry");
48 /* Ensure no RCU iterators see it before re-initializing. */
50 INIT_LIST_HEAD(&thread->links);
54 void vdo_unregister_thread(struct thread_registry *registry)
56 struct registered_thread *thread;
57 bool found_it = false;
59 spin_lock(®istry->lock);
60 list_for_each_entry(thread, ®istry->links, links) {
61 if (thread->task == current) {
62 list_del_rcu(&thread->links);
67 spin_unlock(®istry->lock);
69 ASSERT_LOG_ONLY(found_it, "thread found in registry");
71 /* Ensure no RCU iterators see it before re-initializing. */
73 INIT_LIST_HEAD(&thread->links);
77 const void *vdo_lookup_thread(struct thread_registry *registry)
79 struct registered_thread *thread;
80 const void *result = NULL;
83 list_for_each_entry_rcu(thread, ®istry->links, links) {
84 if (thread->task == current) {
85 result = thread->pointer;