kthread.c 9.1 KB

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