blob: ec08478dd01397cdc0e159fc3b49c9bf6a88e0a7 [file] [log] [blame]
Matthew Sakai89f9b702023-11-16 19:47:35 -05001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright 2023 Red Hat
4 */
5
Mike Snitzerc2f54aa2024-02-09 10:10:03 -06006#include "thread-utils.h"
Matthew Sakai89f9b702023-11-16 19:47:35 -05007
Mike Snitzer20be4662024-02-13 10:51:19 -05008#include <asm/current.h>
Mike Snitzercae3816d2024-02-14 10:25:39 -05009#include <linux/delay.h>
Matthew Sakai89f9b702023-11-16 19:47:35 -050010#include <linux/kthread.h>
Mike Snitzercb6f8b72024-02-09 13:17:05 -060011#include <linux/mutex.h>
12#include <linux/types.h>
Matthew Sakai89f9b702023-11-16 19:47:35 -050013
14#include "errors.h"
15#include "logger.h"
16#include "memory-alloc.h"
17
18static struct hlist_head thread_list;
19static struct mutex thread_mutex;
Matthew Sakai89f9b702023-11-16 19:47:35 -050020
21struct thread {
22 void (*thread_function)(void *thread_data);
23 void *thread_data;
24 struct hlist_node thread_links;
25 struct task_struct *thread_task;
26 struct completion thread_done;
27};
28
Matthew Sakai7eb30fe2024-03-01 18:29:05 -050029void vdo_initialize_threads_mutex(void)
Matthew Sakai89f9b702023-11-16 19:47:35 -050030{
31 mutex_init(&thread_mutex);
32}
33
34static int thread_starter(void *arg)
35{
36 struct registered_thread allocating_thread;
37 struct thread *thread = arg;
38
39 thread->thread_task = current;
Matthew Sakai89f9b702023-11-16 19:47:35 -050040 mutex_lock(&thread_mutex);
41 hlist_add_head(&thread->thread_links, &thread_list);
42 mutex_unlock(&thread_mutex);
Mike Snitzer0eea6b62024-02-13 10:55:50 -060043 vdo_register_allocating_thread(&allocating_thread, NULL);
Matthew Sakai89f9b702023-11-16 19:47:35 -050044 thread->thread_function(thread->thread_data);
Mike Snitzer0eea6b62024-02-13 10:55:50 -060045 vdo_unregister_allocating_thread();
Matthew Sakai89f9b702023-11-16 19:47:35 -050046 complete(&thread->thread_done);
47 return 0;
48}
49
Mike Snitzer650e3102024-02-09 12:08:09 -060050int vdo_create_thread(void (*thread_function)(void *), void *thread_data,
Matthew Sakai89f9b702023-11-16 19:47:35 -050051 const char *name, struct thread **new_thread)
52{
53 char *name_colon = strchr(name, ':');
54 char *my_name_colon = strchr(current->comm, ':');
55 struct task_struct *task;
56 struct thread *thread;
57 int result;
58
Mike Snitzer0eea6b62024-02-13 10:55:50 -060059 result = vdo_allocate(1, struct thread, __func__, &thread);
Mike Snitzer2de70382024-02-13 12:06:53 -060060 if (result != VDO_SUCCESS) {
Mike Snitzer35842402024-02-14 09:22:04 -060061 vdo_log_warning("Error allocating memory for %s", name);
Matthew Sakai89f9b702023-11-16 19:47:35 -050062 return result;
63 }
64
65 thread->thread_function = thread_function;
66 thread->thread_data = thread_data;
67 init_completion(&thread->thread_done);
68 /*
69 * Start the thread, with an appropriate thread name.
70 *
71 * If the name supplied contains a colon character, use that name. This causes uds module
72 * threads to have names like "uds:callbackW" and the main test runner thread to be named
73 * "zub:runtest".
74 *
75 * Otherwise if the current thread has a name containing a colon character, prefix the name
76 * supplied with the name of the current thread up to (and including) the colon character.
77 * Thus when the "kvdo0:dedupeQ" thread opens an index session, all the threads associated
78 * with that index will have names like "kvdo0:foo".
79 *
80 * Otherwise just use the name supplied. This should be a rare occurrence.
81 */
82 if ((name_colon == NULL) && (my_name_colon != NULL)) {
83 task = kthread_run(thread_starter, thread, "%.*s:%s",
84 (int) (my_name_colon - current->comm), current->comm,
85 name);
86 } else {
87 task = kthread_run(thread_starter, thread, "%s", name);
88 }
89
90 if (IS_ERR(task)) {
Mike Snitzer0eea6b62024-02-13 10:55:50 -060091 vdo_free(thread);
Matthew Sakai89f9b702023-11-16 19:47:35 -050092 return PTR_ERR(task);
93 }
94
95 *new_thread = thread;
Mike Snitzer34edf9e2024-02-13 13:18:35 -060096 return VDO_SUCCESS;
Matthew Sakai89f9b702023-11-16 19:47:35 -050097}
98
Mike Snitzer650e3102024-02-09 12:08:09 -060099void vdo_join_threads(struct thread *thread)
Matthew Sakai89f9b702023-11-16 19:47:35 -0500100{
Mike Snitzercae3816d2024-02-14 10:25:39 -0500101 while (wait_for_completion_interruptible(&thread->thread_done))
102 fsleep(1000);
Matthew Sakai89f9b702023-11-16 19:47:35 -0500103
104 mutex_lock(&thread_mutex);
105 hlist_del(&thread->thread_links);
106 mutex_unlock(&thread_mutex);
Mike Snitzer0eea6b62024-02-13 10:55:50 -0600107 vdo_free(thread);
Matthew Sakai89f9b702023-11-16 19:47:35 -0500108}