[PATCH v3 1/6] kthread: Add the helper function kthread_run_on_cpu()
From: Cai Huoqing
Date: Thu Oct 21 2021 - 22:59:21 EST
the helper function kthread_run_on_cpu() includes
kthread_create_on_cpu/wake_up_process().
In some cases, use kthread_run_on_cpu() directly instead of
kthread_create_on_node/kthread_bind/wake_up_process() or
kthread_create_on_cpu/wake_up_process() or
kthreadd_create/kthread_bind/wake_up_process() to simplify the code.
Signed-off-by: Cai Huoqing <caihuoqing@xxxxxxxxx>
---
v1->v2:
*Remove cpu_to_node from kthread_create_on_cpu params.
*Updated the macro description comment.
v2->v3:
*Convert this helper macro to static inline function
*Fix typo in changelog
v1 link:
https://lore.kernel.org/lkml/20211021120135.3003-2-caihuoqing@xxxxxxxxx/
v2 link:
https://lore.kernel.org/lkml/20211021122758.3092-2-caihuoqing@xxxxxxxxx/
include/linux/kthread.h | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/include/linux/kthread.h b/include/linux/kthread.h
index 346b0f269161..db47aae7c481 100644
--- a/include/linux/kthread.h
+++ b/include/linux/kthread.h
@@ -56,6 +56,31 @@ bool kthread_is_per_cpu(struct task_struct *k);
__k; \
})
+/**
+ * kthread_run_on_cpu - create and wake a cpu bound thread.
+ * @threadfn: the function to run until signal_pending(current).
+ * @data: data ptr for @threadfn.
+ * @cpu: The cpu on which the thread should be bound,
+ * @namefmt: printf-style name for the thread. Format is restricted
+ * to "name.*%u". Code fills in cpu number.
+ *
+ * Description: Convenient wrapper for kthread_create_on_cpu()
+ * followed by wake_up_process(). Returns the kthread or
+ * ERR_PTR(-ENOMEM).
+ */
+static inline struct task_struct *
+kthread_run_on_cpu(int (*threadfn)(void *data), void *data,
+ unsigned int cpu, const char *namefmt)
+{
+ struct task_struct *p;
+
+ p = kthread_create_on_cpu(threadfn, data, cpu, namefmt);
+ if (!IS_ERR(p))
+ wake_up_process(p);
+
+ return p;
+}
+
void free_kthread_struct(struct task_struct *k);
void kthread_bind(struct task_struct *k, unsigned int cpu);
void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
--
2.25.1