thread-utils.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #include "thread-utils.h"
  6. #include <asm/current.h>
  7. #include <linux/delay.h>
  8. #include <linux/kthread.h>
  9. #include <linux/mutex.h>
  10. #include <linux/types.h>
  11. #include "errors.h"
  12. #include "logger.h"
  13. #include "memory-alloc.h"
  14. static struct hlist_head thread_list;
  15. static struct mutex thread_mutex;
  16. struct thread {
  17. void (*thread_function)(void *thread_data);
  18. void *thread_data;
  19. struct hlist_node thread_links;
  20. struct task_struct *thread_task;
  21. struct completion thread_done;
  22. };
  23. void vdo_initialize_threads_mutex(void)
  24. {
  25. mutex_init(&thread_mutex);
  26. }
  27. static int thread_starter(void *arg)
  28. {
  29. struct registered_thread allocating_thread;
  30. struct thread *thread = arg;
  31. thread->thread_task = current;
  32. mutex_lock(&thread_mutex);
  33. hlist_add_head(&thread->thread_links, &thread_list);
  34. mutex_unlock(&thread_mutex);
  35. vdo_register_allocating_thread(&allocating_thread, NULL);
  36. thread->thread_function(thread->thread_data);
  37. vdo_unregister_allocating_thread();
  38. complete(&thread->thread_done);
  39. return 0;
  40. }
  41. int vdo_create_thread(void (*thread_function)(void *), void *thread_data,
  42. const char *name, struct thread **new_thread)
  43. {
  44. char *name_colon = strchr(name, ':');
  45. char *my_name_colon = strchr(current->comm, ':');
  46. struct task_struct *task;
  47. struct thread *thread;
  48. int result;
  49. result = vdo_allocate(1, struct thread, __func__, &thread);
  50. if (result != VDO_SUCCESS) {
  51. vdo_log_warning("Error allocating memory for %s", name);
  52. return result;
  53. }
  54. thread->thread_function = thread_function;
  55. thread->thread_data = thread_data;
  56. init_completion(&thread->thread_done);
  57. /*
  58. * Start the thread, with an appropriate thread name.
  59. *
  60. * If the name supplied contains a colon character, use that name. This causes uds module
  61. * threads to have names like "uds:callbackW" and the main test runner thread to be named
  62. * "zub:runtest".
  63. *
  64. * Otherwise if the current thread has a name containing a colon character, prefix the name
  65. * supplied with the name of the current thread up to (and including) the colon character.
  66. * Thus when the "kvdo0:dedupeQ" thread opens an index session, all the threads associated
  67. * with that index will have names like "kvdo0:foo".
  68. *
  69. * Otherwise just use the name supplied. This should be a rare occurrence.
  70. */
  71. if ((name_colon == NULL) && (my_name_colon != NULL)) {
  72. task = kthread_run(thread_starter, thread, "%.*s:%s",
  73. (int) (my_name_colon - current->comm), current->comm,
  74. name);
  75. } else {
  76. task = kthread_run(thread_starter, thread, "%s", name);
  77. }
  78. if (IS_ERR(task)) {
  79. vdo_free(thread);
  80. return PTR_ERR(task);
  81. }
  82. *new_thread = thread;
  83. return VDO_SUCCESS;
  84. }
  85. void vdo_join_threads(struct thread *thread)
  86. {
  87. while (wait_for_completion_interruptible(&thread->thread_done))
  88. fsleep(1000);
  89. mutex_lock(&thread_mutex);
  90. hlist_del(&thread->thread_links);
  91. mutex_unlock(&thread_mutex);
  92. vdo_free(thread);
  93. }