kthread.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include <common/kthread.h>
  2. #include <common/glib.h>
  3. #include <common/spinlock.h>
  4. #include <sched/sched.h>
  5. #include <debug/bug.h>
  6. #include <time/sleep.h>
  7. static spinlock_t __kthread_create_lock; // kthread创建过程的锁
  8. static struct List kthread_create_list; // kthread创建任务的链表
  9. struct process_control_block *kthreadd_pcb = NULL; // kthreadd守护线程的pcb
  10. // 枚举各个标志位是在第几位
  11. enum KTHREAD_BITS
  12. {
  13. KTHREAD_IS_PER_CPU = 0,
  14. KTHREAD_SHOULD_STOP,
  15. KTHREAD_SHOULD_PARK,
  16. };
  17. /**
  18. * @brief kthread的创建信息(仅在创建过程中存在)
  19. *
  20. */
  21. struct kthread_create_info_t
  22. {
  23. // 传递给kthread的信息
  24. int (*thread_fn)(void *data);
  25. void *data;
  26. int node;
  27. // kthreadd守护进程传递给kthread_create的结果, 成功则返回PCB,不成功则该值为负数错误码。若该值为NULL,意味着创建过程尚未完成
  28. struct process_control_block *result;
  29. struct List list;
  30. };
  31. /**
  32. * @brief kthread信息
  33. * 该结构体将会绑定到pcb的worker_private中
  34. */
  35. struct kthread_info_t
  36. {
  37. uint64_t flags;
  38. uint32_t cpu;
  39. int result;
  40. int (*thread_fn)(void *);
  41. void *data;
  42. // todo: 将这里改为completion机制
  43. bool exited; // 是否已退出
  44. char *full_name; // 内核线程的名称
  45. };
  46. /**
  47. * @brief 获取pcb中的kthread结构体
  48. *
  49. * @param pcb pcb
  50. * @return struct kthread* kthread信息结构体
  51. */
  52. static inline struct kthread_info_t *to_kthread(struct process_control_block *pcb)
  53. {
  54. WARN_ON(!(pcb->flags & PF_KTHREAD));
  55. return pcb->worker_private;
  56. }
  57. static struct process_control_block *__kthread_create_on_node(int (*thread_fn)(void *data), void *data,
  58. int node,
  59. const char name_fmt[], va_list args)
  60. {
  61. struct process_control_block *pcb = NULL;
  62. struct kthread_create_info_t *create = kzalloc(sizeof(struct kthread_create_info_t), 0);
  63. if (create == NULL)
  64. return ERR_PTR(-ENOMEM);
  65. create->thread_fn = thread_fn;
  66. create->data = data;
  67. create->node = node;
  68. create->result = NULL;
  69. list_init(&create->list);
  70. spin_lock(&__kthread_create_lock);
  71. list_append(&kthread_create_list, &create->list);
  72. spin_unlock(&__kthread_create_lock);
  73. kdebug("to wakeup kthread daemon..., current preempt=%d, rflags=%#018lx", current_pcb->preempt_count, get_rflags());
  74. while (kthreadd_pcb == NULL) // 若kthreadd未初始化,则等待kthreadd启动
  75. ;
  76. // 唤醒kthreadd守护进程
  77. process_wakeup_immediately(kthreadd_pcb);
  78. // 等待创建完成
  79. // todo: 使用completion机制以降低忙等时间
  80. while (create->result == NULL)
  81. pause();
  82. // 获取结果
  83. pcb = create->result;
  84. if (!IS_ERR(create->result))
  85. {
  86. // todo: 为内核线程设置名字
  87. }
  88. kfree(create);
  89. return pcb;
  90. }
  91. /**
  92. * @brief 让当前内核线程退出,并返回result参数给kthread_stop()函数
  93. *
  94. * @param result 返回值
  95. */
  96. void kthread_exit(long result)
  97. {
  98. struct kthread_info_t *kt = to_kthread(current_pcb);
  99. kt->result = result;
  100. kt->exited = true;
  101. process_do_exit(0);
  102. }
  103. /**
  104. * @brief 在当前结点上创建一个内核线程
  105. *
  106. * @param thread_fn 该内核线程要执行的函数
  107. * @param data 传递给 thread_fn 的参数数据
  108. * @param node 线程的任务和线程结构都分配在这个节点上
  109. * @param name_fmt printf-style format string for the thread name
  110. * @param arg name_fmt的参数
  111. * @return 返回一个pcb或者是ERR_PTR(-ENOMEM)
  112. *
  113. * 请注意,该宏会创建一个内核线程,并将其设置为停止状态。您可以使用wake_up_process来启动这个线程。
  114. * 新的线程的调度策略为SCHED_NORMAL,并且能在所有的cpu上运行
  115. *
  116. * 当内核线程被唤醒时,会运行thread_fn函数,并将data作为参数传入。
  117. * 内核线程可以直接返回,也可以在kthread_should_stop为真时返回。
  118. */
  119. struct process_control_block *kthread_create_on_node(int (*thread_fn)(void *data), void *data,
  120. int node,
  121. const char name_fmt[], ...)
  122. {
  123. struct process_control_block *pcb;
  124. va_list args;
  125. va_start(args, name_fmt);
  126. pcb = __kthread_create_on_node(thread_fn, data, node, name_fmt, args);
  127. va_end(args);
  128. return pcb;
  129. }
  130. /**
  131. * @brief 内核线程的包裹程序
  132. * 当内核线程被运行后,从kernel_thread_func跳转到这里。
  133. * @param _create 内核线程的创建信息
  134. * @return int 内核线程的退出返回值
  135. */
  136. static int kthread(void *_create)
  137. {
  138. struct kthread_create_info_t *create = _create;
  139. // 将这几个信息从kthread_create_info中拷贝过来。以免在kthread_create_info被free后,数据丢失从而导致错误。
  140. int (*thread_fn)(void *data) = create->thread_fn;
  141. void *data = create->data;
  142. int retval = 0;
  143. struct kthread_info_t *self = to_kthread(current_pcb);
  144. self->thread_fn = thread_fn;
  145. self->data = data;
  146. // todo: 增加调度参数设定
  147. // todo: 当前内核线程继承了kthreadd的优先级以及调度策略,需要在这里进行更新
  148. // 设置当前进程为不可被打断
  149. current_pcb->state = PROC_UNINTERRUPTIBLE;
  150. // 将当前pcb返回给创建者
  151. create->result = current_pcb;
  152. current_pcb->state &= ~PROC_RUNNING; // 设置当前进程不是RUNNING态
  153. // 发起调度,使得当前内核线程休眠。直到创建者通过process_wakeup将当前内核线程唤醒
  154. sched();
  155. retval = -EINTR;
  156. // 如果发起者没有调用kthread_stop(),则该kthread的功能函数开始执行
  157. if (!(self->flags & (1 << KTHREAD_SHOULD_STOP)))
  158. {
  159. retval = thread_fn(data);
  160. }
  161. kthread_exit(retval);
  162. }
  163. static void __create_kthread(struct kthread_create_info_t *create)
  164. {
  165. pid_t pid = kernel_thread(kthread, create, CLONE_FS | CLONE_SIGNAL);
  166. if (IS_ERR((void *)pid))
  167. {
  168. // todo: 使用complete机制完善这里
  169. create->result = (struct process_control_block *)pid;
  170. }
  171. }
  172. /**
  173. * @brief kthread守护线程
  174. *
  175. * @param unused
  176. * @return int 不应当退出
  177. */
  178. int kthreadd(void *unused)
  179. {
  180. kinfo("kthread daemon started!");
  181. struct process_control_block *pcb = current_pcb;
  182. kthreadd_pcb = current_pcb;
  183. current_pcb->flags |= PF_NOFREEZE;
  184. for (;;)
  185. {
  186. current_pcb->state = PROC_INTERRUPTIBLE;
  187. // 所有的创建任务都被处理完了
  188. if (list_empty(&kthread_create_list))
  189. sched();
  190. spin_lock(&__kthread_create_lock);
  191. // 循环取出链表中的任务
  192. while (!list_empty(&kthread_create_list))
  193. {
  194. // 从链表中取出第一个要创建的内核线程任务
  195. struct kthread_create_info_t *create = container_of(kthread_create_list.next, struct kthread_create_info_t, list);
  196. list_del_init(&create->list);
  197. spin_unlock(&__kthread_create_lock);
  198. __create_kthread(create);
  199. spin_lock(&__kthread_create_lock);
  200. }
  201. spin_unlock(&__kthread_create_lock);
  202. }
  203. }
  204. /**
  205. * @brief 内核线程调用该函数,检查自身的标志位,判断自己是否应该执行完任务后退出
  206. *
  207. * @return true 内核线程应该退出
  208. * @return false 无需退出
  209. */
  210. bool kthread_should_stop(void)
  211. {
  212. struct kthread_info_t *self = to_kthread(current_pcb);
  213. if (self->flags & (1 << KTHREAD_SHOULD_STOP))
  214. return true;
  215. return false;
  216. }
  217. /**
  218. * @brief 向kthread发送停止信号,请求其结束
  219. *
  220. * @param pcb 内核线程的pcb
  221. * @return int 错误码
  222. */
  223. int kthread_stop(struct process_control_block *pcb)
  224. {
  225. int retval;
  226. struct kthread_info_t *target = to_kthread(pcb);
  227. target->flags |= (1 << KTHREAD_SHOULD_STOP);
  228. process_wakeup(pcb);
  229. // 等待指定的内核线程退出
  230. // todo: 使用completion机制改进这里
  231. while (target->exited == false)
  232. usleep(5000);
  233. retval = target->result;
  234. // 释放内核线程的页表
  235. process_exit_mm(pcb);
  236. process_release_pcb(pcb);
  237. return retval;
  238. }
  239. /**
  240. * @brief 设置pcb中的worker_private字段(只应被设置一次)
  241. *
  242. * @param pcb pcb
  243. * @return bool 成功或失败
  244. */
  245. bool kthread_set_worker_private(struct process_control_block *pcb)
  246. {
  247. if (WARN_ON_ONCE(to_kthread(pcb)))
  248. return false;
  249. struct kthread_info_t *kt = kzalloc(sizeof(struct kthread_info_t), 0);
  250. if (kt == NULL)
  251. return false;
  252. pcb->worker_private = kt;
  253. return true;
  254. }
  255. /**
  256. * @brief 初始化kthread机制(只应被process_init调用)
  257. *
  258. * @return int 错误码
  259. */
  260. int kthread_mechanism_init()
  261. {
  262. kinfo("Initializing kthread mechanism...");
  263. spin_init(&__kthread_create_lock);
  264. list_init(&kthread_create_list);
  265. // 创建kthreadd守护进程
  266. kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_SIGNAL);
  267. return 0;
  268. }