Message ID | 20211021120135.3003-2-caihuoqing@baidu.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | kthread: Add the helper macro kthread_run_on_cpu() | expand |
On Thu, 21 Oct 2021 20:01:30 +0800 Cai Huoqing <caihuoqing@baidu.com> wrote: > the helper macro kthread_run_on_cpu() inculdes "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@baidu.com> > --- > include/linux/kthread.h | 22 ++++++++++++++++++++++ > 1 file changed, 22 insertions(+) > > diff --git a/include/linux/kthread.h b/include/linux/kthread.h > index 346b0f269161..dfd125523aa9 100644 > --- a/include/linux/kthread.h > +++ b/include/linux/kthread.h > @@ -56,6 +56,28 @@ 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_node() > + * followed by wake_up_process(). Returns the kthread or > + * ERR_PTR(-ENOMEM). > + */ > +#define kthread_run_on_cpu(threadfn, data, cpu, namefmt) \ Why is this a macro and not a static inline function? -- Steve > +({ \ > + struct task_struct *__k \ > + = kthread_create_on_cpu(threadfn, data, cpu_to_node(cpu), \ > + namefmt); \ > + if (!IS_ERR(__k)) \ > + wake_up_process(__k); \ > + __k; \ > +}) > + > 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);
On 21 10月 21 09:10:01, Steven Rostedt wrote: > On Thu, 21 Oct 2021 20:01:30 +0800 > Cai Huoqing <caihuoqing@baidu.com> wrote: > > > the helper macro kthread_run_on_cpu() inculdes > > "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@baidu.com> > > --- > > include/linux/kthread.h | 22 ++++++++++++++++++++++ > > 1 file changed, 22 insertions(+) > > > > diff --git a/include/linux/kthread.h b/include/linux/kthread.h > > index 346b0f269161..dfd125523aa9 100644 > > --- a/include/linux/kthread.h > > +++ b/include/linux/kthread.h > > @@ -56,6 +56,28 @@ 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_node() > > + * followed by wake_up_process(). Returns the kthread or > > + * ERR_PTR(-ENOMEM). > > + */ > > +#define kthread_run_on_cpu(threadfn, data, cpu, namefmt) \ > > Why is this a macro and not a static inline function? > > -- Steve Hi,Thanks for your feedback, I think using static inline function is nice, but here try to keep consistent with the other macros, sush as kthread_create/kthread_init_work... Thanks, Cai. > > > +({ \ > > + struct task_struct *__k \ > > + = kthread_create_on_cpu(threadfn, data, cpu_to_node(cpu), \ > > + namefmt); \ > > + if (!IS_ERR(__k)) \ > > + wake_up_process(__k); \ > > + __k; \ > > +}) > > + > > 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); >
On Thu, 21 Oct 2021 21:53:12 +0800 Cai Huoqing <caihuoqing@baidu.com> wrote: > > > +/** > > > + * 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_node() > > > + * followed by wake_up_process(). Returns the kthread or > > > + * ERR_PTR(-ENOMEM). > > > + */ > > > +#define kthread_run_on_cpu(threadfn, data, cpu, namefmt) \ > > > > Why is this a macro and not a static inline function? > > > > -- Steve > Hi,Thanks for your feedback, > > I think using static inline function is nice, but here try to keep > consistent with the other macros, > sush as kthread_create/kthread_init_work... Which they did because they didn't want to use va_list to have variable arguments, which you don't have. Which begs the question, should you? -- Steve
On 21 10月 21 09:58:58, Steven Rostedt wrote: > On Thu, 21 Oct 2021 21:53:12 +0800 > Cai Huoqing <caihuoqing@baidu.com> wrote: > > > > > +/** > > > > + * 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_node() > > > > + * followed by wake_up_process(). Returns the kthread or > > > > + * ERR_PTR(-ENOMEM). > > > > + */ > > > > +#define kthread_run_on_cpu(threadfn, data, cpu, namefmt) \ > > > > > > Why is this a macro and not a static inline function? > > > > > > -- Steve > > Hi,Thanks for your feedback, > > > > I think using static inline function is nice, but here try to keep > > consistent with the other macros, > > sush as kthread_create/kthread_init_work... > > Which they did because they didn't want to use va_list to have variable > arguments, which you don't have. > > Which begs the question, should you? > > -- Steve Hi, Thanks your reply. I get it, V3 here: https://lore.kernel.org/lkml/20211022025711.3673-2-caihuoqing@baidu.com/ Thanks, Cai
diff --git a/include/linux/kthread.h b/include/linux/kthread.h index 346b0f269161..dfd125523aa9 100644 --- a/include/linux/kthread.h +++ b/include/linux/kthread.h @@ -56,6 +56,28 @@ 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_node() + * followed by wake_up_process(). Returns the kthread or + * ERR_PTR(-ENOMEM). + */ +#define kthread_run_on_cpu(threadfn, data, cpu, namefmt) \ +({ \ + struct task_struct *__k \ + = kthread_create_on_cpu(threadfn, data, cpu_to_node(cpu), \ + namefmt); \ + if (!IS_ERR(__k)) \ + wake_up_process(__k); \ + __k; \ +}) + 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);
the helper macro kthread_run_on_cpu() inculdes 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@baidu.com> --- include/linux/kthread.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)