1c2f54aa2SMike Snitzer // SPDX-License-Identifier: GPL-2.0-only 2c2f54aa2SMike Snitzer /* 3c2f54aa2SMike Snitzer * Copyright 2023 Red Hat 4c2f54aa2SMike Snitzer */ 5c2f54aa2SMike Snitzer 6c2f54aa2SMike Snitzer #include "thread-utils.h" 7c2f54aa2SMike Snitzer 820be466cSMike Snitzer #include <asm/current.h> 9c2f54aa2SMike Snitzer #include <linux/delay.h> 10c2f54aa2SMike Snitzer #include <linux/kthread.h> 11cb6f8b75SMike Snitzer #include <linux/mutex.h> 12cb6f8b75SMike Snitzer #include <linux/types.h> 13c2f54aa2SMike Snitzer 14c2f54aa2SMike Snitzer #include "errors.h" 15c2f54aa2SMike Snitzer #include "logger.h" 16c2f54aa2SMike Snitzer #include "memory-alloc.h" 17c2f54aa2SMike Snitzer 18c2f54aa2SMike Snitzer static struct hlist_head thread_list; 19c2f54aa2SMike Snitzer static struct mutex thread_mutex; 20c2f54aa2SMike Snitzer static atomic_t thread_once = ATOMIC_INIT(0); 21c2f54aa2SMike Snitzer 22c2f54aa2SMike Snitzer struct thread { 23c2f54aa2SMike Snitzer void (*thread_function)(void *thread_data); 24c2f54aa2SMike Snitzer void *thread_data; 25c2f54aa2SMike Snitzer struct hlist_node thread_links; 26c2f54aa2SMike Snitzer struct task_struct *thread_task; 27c2f54aa2SMike Snitzer struct completion thread_done; 28c2f54aa2SMike Snitzer }; 29c2f54aa2SMike Snitzer 30*6008d526SBruce Johnston #define ONCE_NOT_DONE 0 31*6008d526SBruce Johnston #define ONCE_IN_PROGRESS 1 32*6008d526SBruce Johnston #define ONCE_COMPLETE 2 33c2f54aa2SMike Snitzer 34c2f54aa2SMike Snitzer /* Run a function once only, and record that fact in the atomic value. */ 35650e3107SMike Snitzer void vdo_perform_once(atomic_t *once, void (*function)(void)) 36c2f54aa2SMike Snitzer { 37c2f54aa2SMike Snitzer for (;;) { 38c2f54aa2SMike Snitzer switch (atomic_cmpxchg(once, ONCE_NOT_DONE, ONCE_IN_PROGRESS)) { 39c2f54aa2SMike Snitzer case ONCE_NOT_DONE: 40c2f54aa2SMike Snitzer function(); 41c2f54aa2SMike Snitzer atomic_set_release(once, ONCE_COMPLETE); 42c2f54aa2SMike Snitzer return; 43c2f54aa2SMike Snitzer case ONCE_IN_PROGRESS: 44c2f54aa2SMike Snitzer cond_resched(); 45c2f54aa2SMike Snitzer break; 46c2f54aa2SMike Snitzer case ONCE_COMPLETE: 47c2f54aa2SMike Snitzer return; 48c2f54aa2SMike Snitzer default: 49c2f54aa2SMike Snitzer return; 50c2f54aa2SMike Snitzer } 51c2f54aa2SMike Snitzer } 52c2f54aa2SMike Snitzer } 53c2f54aa2SMike Snitzer 54c2f54aa2SMike Snitzer static void thread_init(void) 55c2f54aa2SMike Snitzer { 56c2f54aa2SMike Snitzer mutex_init(&thread_mutex); 57c2f54aa2SMike Snitzer } 58c2f54aa2SMike Snitzer 59c2f54aa2SMike Snitzer static int thread_starter(void *arg) 60c2f54aa2SMike Snitzer { 61c2f54aa2SMike Snitzer struct registered_thread allocating_thread; 62c2f54aa2SMike Snitzer struct thread *thread = arg; 63c2f54aa2SMike Snitzer 64c2f54aa2SMike Snitzer thread->thread_task = current; 65650e3107SMike Snitzer vdo_perform_once(&thread_once, thread_init); 66c2f54aa2SMike Snitzer mutex_lock(&thread_mutex); 67c2f54aa2SMike Snitzer hlist_add_head(&thread->thread_links, &thread_list); 68c2f54aa2SMike Snitzer mutex_unlock(&thread_mutex); 69c2f54aa2SMike Snitzer uds_register_allocating_thread(&allocating_thread, NULL); 70c2f54aa2SMike Snitzer thread->thread_function(thread->thread_data); 71c2f54aa2SMike Snitzer uds_unregister_allocating_thread(); 72c2f54aa2SMike Snitzer complete(&thread->thread_done); 73c2f54aa2SMike Snitzer return 0; 74c2f54aa2SMike Snitzer } 75c2f54aa2SMike Snitzer 76650e3107SMike Snitzer int vdo_create_thread(void (*thread_function)(void *), void *thread_data, 77c2f54aa2SMike Snitzer const char *name, struct thread **new_thread) 78c2f54aa2SMike Snitzer { 79c2f54aa2SMike Snitzer char *name_colon = strchr(name, ':'); 80c2f54aa2SMike Snitzer char *my_name_colon = strchr(current->comm, ':'); 81c2f54aa2SMike Snitzer struct task_struct *task; 82c2f54aa2SMike Snitzer struct thread *thread; 83c2f54aa2SMike Snitzer int result; 84c2f54aa2SMike Snitzer 85c2f54aa2SMike Snitzer result = uds_allocate(1, struct thread, __func__, &thread); 86c2f54aa2SMike Snitzer if (result != UDS_SUCCESS) { 87c2f54aa2SMike Snitzer uds_log_warning("Error allocating memory for %s", name); 88c2f54aa2SMike Snitzer return result; 89c2f54aa2SMike Snitzer } 90c2f54aa2SMike Snitzer 91c2f54aa2SMike Snitzer thread->thread_function = thread_function; 92c2f54aa2SMike Snitzer thread->thread_data = thread_data; 93c2f54aa2SMike Snitzer init_completion(&thread->thread_done); 94c2f54aa2SMike Snitzer /* 95c2f54aa2SMike Snitzer * Start the thread, with an appropriate thread name. 96c2f54aa2SMike Snitzer * 97c2f54aa2SMike Snitzer * If the name supplied contains a colon character, use that name. This causes uds module 98c2f54aa2SMike Snitzer * threads to have names like "uds:callbackW" and the main test runner thread to be named 99c2f54aa2SMike Snitzer * "zub:runtest". 100c2f54aa2SMike Snitzer * 101c2f54aa2SMike Snitzer * Otherwise if the current thread has a name containing a colon character, prefix the name 102c2f54aa2SMike Snitzer * supplied with the name of the current thread up to (and including) the colon character. 103c2f54aa2SMike Snitzer * Thus when the "kvdo0:dedupeQ" thread opens an index session, all the threads associated 104c2f54aa2SMike Snitzer * with that index will have names like "kvdo0:foo". 105c2f54aa2SMike Snitzer * 106c2f54aa2SMike Snitzer * Otherwise just use the name supplied. This should be a rare occurrence. 107c2f54aa2SMike Snitzer */ 108c2f54aa2SMike Snitzer if ((name_colon == NULL) && (my_name_colon != NULL)) { 109c2f54aa2SMike Snitzer task = kthread_run(thread_starter, thread, "%.*s:%s", 110c2f54aa2SMike Snitzer (int) (my_name_colon - current->comm), current->comm, 111c2f54aa2SMike Snitzer name); 112c2f54aa2SMike Snitzer } else { 113c2f54aa2SMike Snitzer task = kthread_run(thread_starter, thread, "%s", name); 114c2f54aa2SMike Snitzer } 115c2f54aa2SMike Snitzer 116c2f54aa2SMike Snitzer if (IS_ERR(task)) { 117c2f54aa2SMike Snitzer uds_free(thread); 118c2f54aa2SMike Snitzer return PTR_ERR(task); 119c2f54aa2SMike Snitzer } 120c2f54aa2SMike Snitzer 121c2f54aa2SMike Snitzer *new_thread = thread; 122c2f54aa2SMike Snitzer return UDS_SUCCESS; 123c2f54aa2SMike Snitzer } 124c2f54aa2SMike Snitzer 125650e3107SMike Snitzer void vdo_join_threads(struct thread *thread) 126c2f54aa2SMike Snitzer { 127c2f54aa2SMike Snitzer while (wait_for_completion_interruptible(&thread->thread_done)) 128c2f54aa2SMike Snitzer fsleep(1000); 129c2f54aa2SMike Snitzer 130c2f54aa2SMike Snitzer mutex_lock(&thread_mutex); 131c2f54aa2SMike Snitzer hlist_del(&thread->thread_links); 132c2f54aa2SMike Snitzer mutex_unlock(&thread_mutex); 133c2f54aa2SMike Snitzer uds_free(thread); 134c2f54aa2SMike Snitzer } 135