xref: /linux/drivers/md/dm-vdo/thread-utils.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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 
21c2f54aa2SMike Snitzer struct thread {
22c2f54aa2SMike Snitzer 	void (*thread_function)(void *thread_data);
23c2f54aa2SMike Snitzer 	void *thread_data;
24c2f54aa2SMike Snitzer 	struct hlist_node thread_links;
25c2f54aa2SMike Snitzer 	struct task_struct *thread_task;
26c2f54aa2SMike Snitzer 	struct completion thread_done;
27c2f54aa2SMike Snitzer };
28c2f54aa2SMike Snitzer 
vdo_initialize_threads_mutex(void)29*7eb30fe1SMatthew Sakai void vdo_initialize_threads_mutex(void)
30c2f54aa2SMike Snitzer {
31c2f54aa2SMike Snitzer 	mutex_init(&thread_mutex);
32c2f54aa2SMike Snitzer }
33c2f54aa2SMike Snitzer 
thread_starter(void * arg)34c2f54aa2SMike Snitzer static int thread_starter(void *arg)
35c2f54aa2SMike Snitzer {
36c2f54aa2SMike Snitzer 	struct registered_thread allocating_thread;
37c2f54aa2SMike Snitzer 	struct thread *thread = arg;
38c2f54aa2SMike Snitzer 
39c2f54aa2SMike Snitzer 	thread->thread_task = current;
40c2f54aa2SMike Snitzer 	mutex_lock(&thread_mutex);
41c2f54aa2SMike Snitzer 	hlist_add_head(&thread->thread_links, &thread_list);
42c2f54aa2SMike Snitzer 	mutex_unlock(&thread_mutex);
430eea6b6eSMike Snitzer 	vdo_register_allocating_thread(&allocating_thread, NULL);
44c2f54aa2SMike Snitzer 	thread->thread_function(thread->thread_data);
450eea6b6eSMike Snitzer 	vdo_unregister_allocating_thread();
46c2f54aa2SMike Snitzer 	complete(&thread->thread_done);
47c2f54aa2SMike Snitzer 	return 0;
48c2f54aa2SMike Snitzer }
49c2f54aa2SMike Snitzer 
vdo_create_thread(void (* thread_function)(void *),void * thread_data,const char * name,struct thread ** new_thread)50650e3107SMike Snitzer int vdo_create_thread(void (*thread_function)(void *), void *thread_data,
51c2f54aa2SMike Snitzer 		      const char *name, struct thread **new_thread)
52c2f54aa2SMike Snitzer {
53c2f54aa2SMike Snitzer 	char *name_colon = strchr(name, ':');
54c2f54aa2SMike Snitzer 	char *my_name_colon = strchr(current->comm, ':');
55c2f54aa2SMike Snitzer 	struct task_struct *task;
56c2f54aa2SMike Snitzer 	struct thread *thread;
57c2f54aa2SMike Snitzer 	int result;
58c2f54aa2SMike Snitzer 
590eea6b6eSMike Snitzer 	result = vdo_allocate(1, struct thread, __func__, &thread);
602de70388SMike Snitzer 	if (result != VDO_SUCCESS) {
613584240bSMike Snitzer 		vdo_log_warning("Error allocating memory for %s", name);
62c2f54aa2SMike Snitzer 		return result;
63c2f54aa2SMike Snitzer 	}
64c2f54aa2SMike Snitzer 
65c2f54aa2SMike Snitzer 	thread->thread_function = thread_function;
66c2f54aa2SMike Snitzer 	thread->thread_data = thread_data;
67c2f54aa2SMike Snitzer 	init_completion(&thread->thread_done);
68c2f54aa2SMike Snitzer 	/*
69c2f54aa2SMike Snitzer 	 * Start the thread, with an appropriate thread name.
70c2f54aa2SMike Snitzer 	 *
71c2f54aa2SMike Snitzer 	 * If the name supplied contains a colon character, use that name. This causes uds module
72c2f54aa2SMike Snitzer 	 * threads to have names like "uds:callbackW" and the main test runner thread to be named
73c2f54aa2SMike Snitzer 	 * "zub:runtest".
74c2f54aa2SMike Snitzer 	 *
75c2f54aa2SMike Snitzer 	 * Otherwise if the current thread has a name containing a colon character, prefix the name
76c2f54aa2SMike Snitzer 	 * supplied with the name of the current thread up to (and including) the colon character.
77c2f54aa2SMike Snitzer 	 * Thus when the "kvdo0:dedupeQ" thread opens an index session, all the threads associated
78c2f54aa2SMike Snitzer 	 * with that index will have names like "kvdo0:foo".
79c2f54aa2SMike Snitzer 	 *
80c2f54aa2SMike Snitzer 	 * Otherwise just use the name supplied. This should be a rare occurrence.
81c2f54aa2SMike Snitzer 	 */
82c2f54aa2SMike Snitzer 	if ((name_colon == NULL) && (my_name_colon != NULL)) {
83c2f54aa2SMike Snitzer 		task = kthread_run(thread_starter, thread, "%.*s:%s",
84c2f54aa2SMike Snitzer 				   (int) (my_name_colon - current->comm), current->comm,
85c2f54aa2SMike Snitzer 				   name);
86c2f54aa2SMike Snitzer 	} else {
87c2f54aa2SMike Snitzer 		task = kthread_run(thread_starter, thread, "%s", name);
88c2f54aa2SMike Snitzer 	}
89c2f54aa2SMike Snitzer 
90c2f54aa2SMike Snitzer 	if (IS_ERR(task)) {
910eea6b6eSMike Snitzer 		vdo_free(thread);
92c2f54aa2SMike Snitzer 		return PTR_ERR(task);
93c2f54aa2SMike Snitzer 	}
94c2f54aa2SMike Snitzer 
95c2f54aa2SMike Snitzer 	*new_thread = thread;
9634edf9e2SMike Snitzer 	return VDO_SUCCESS;
97c2f54aa2SMike Snitzer }
98c2f54aa2SMike Snitzer 
vdo_join_threads(struct thread * thread)99650e3107SMike Snitzer void vdo_join_threads(struct thread *thread)
100c2f54aa2SMike Snitzer {
101c2f54aa2SMike Snitzer 	while (wait_for_completion_interruptible(&thread->thread_done))
102c2f54aa2SMike Snitzer 		fsleep(1000);
103c2f54aa2SMike Snitzer 
104c2f54aa2SMike Snitzer 	mutex_lock(&thread_mutex);
105c2f54aa2SMike Snitzer 	hlist_del(&thread->thread_links);
106c2f54aa2SMike Snitzer 	mutex_unlock(&thread_mutex);
1070eea6b6eSMike Snitzer 	vdo_free(thread);
108c2f54aa2SMike Snitzer }
109