2021-10-22 03:00:50

by Cai,Huoqing

[permalink] [raw]
Subject: [PATCH v3 0/6] kthread: Add the helper function kthread_run_on_cpu()

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.

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
*Update changelog

v1 link:
https://lore.kernel.org/lkml/[email protected]/
v2 link:
https://lore.kernel.org/lkml/[email protected]/

Cai Huoqing (6):
kthread: Add the helper function kthread_run_on_cpu()
RDMA/siw: Make use of the helper function kthread_run_on_cpu()
ring-buffer: Make use of the helper function kthread_run_on_cpu()
rcutorture: Make use of the helper function kthread_run_on_cpu()
trace/osnoise: Make use of the helper function kthread_run_on_cpu()
trace/hwlat: Make use of the helper function kthread_run_on_cpu()

drivers/infiniband/sw/siw/siw_main.c | 7 +++----
include/linux/kthread.h | 25 +++++++++++++++++++++++++
kernel/rcu/rcutorture.c | 7 ++-----
kernel/trace/ring_buffer.c | 7 ++-----
kernel/trace/trace_hwlat.c | 6 +-----
kernel/trace/trace_osnoise.c | 3 +--
6 files changed, 34 insertions(+), 21 deletions(-)

--
2.25.1


2021-10-22 03:00:57

by Cai,Huoqing

[permalink] [raw]
Subject: [PATCH v3 1/6] kthread: Add the helper function kthread_run_on_cpu()

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 <[email protected]>
---
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/[email protected]/
v2 link:
https://lore.kernel.org/lkml/[email protected]/

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

2021-10-22 03:01:07

by Cai,Huoqing

[permalink] [raw]
Subject: [PATCH v3 5/6] trace/osnoise: Make use of the helper function kthread_run_on_cpu()

Replace kthread_create_on_cpu/wake_up_process()
with kthread_run_on_cpu() to simplify the code.

Signed-off-by: Cai Huoqing <[email protected]>
---
kernel/trace/trace_osnoise.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/kernel/trace/trace_osnoise.c b/kernel/trace/trace_osnoise.c
index ce053619f289..cbd78fd5e491 100644
--- a/kernel/trace/trace_osnoise.c
+++ b/kernel/trace/trace_osnoise.c
@@ -1525,7 +1525,7 @@ static int start_kthread(unsigned int cpu)
#else
snprintf(comm, 24, "osnoise/%d", cpu);
#endif
- kthread = kthread_create_on_cpu(main, NULL, cpu, comm);
+ kthread = kthread_run_on_cpu(main, NULL, cpu, comm);

if (IS_ERR(kthread)) {
pr_err(BANNER "could not start sampling thread\n");
@@ -1534,7 +1534,6 @@ static int start_kthread(unsigned int cpu)
}

per_cpu(per_cpu_osnoise_var, cpu).kthread = kthread;
- wake_up_process(kthread);

return 0;
}
--
2.25.1

2021-10-22 03:01:07

by Cai,Huoqing

[permalink] [raw]
Subject: [PATCH v3 4/6] rcutorture: Make use of the helper function kthread_run_on_cpu()

Replace kthread_create_on_node/kthread_bind/wake_up_process()
with kthread_run_on_cpu() to simplify the code.

Signed-off-by: Cai Huoqing <[email protected]>
---
kernel/rcu/rcutorture.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
index 503e14e62e8f..6c3ba8c0d7ff 100644
--- a/kernel/rcu/rcutorture.c
+++ b/kernel/rcu/rcutorture.c
@@ -2025,9 +2025,8 @@ static int rcutorture_booster_init(unsigned int cpu)
mutex_lock(&boost_mutex);
rcu_torture_disable_rt_throttle();
VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
- boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
- cpu_to_node(cpu),
- "rcu_torture_boost");
+ boost_tasks[cpu] = kthread_run_on_cpu(rcu_torture_boost, NULL,
+ cpu, "rcu_torture_boost_%u");
if (IS_ERR(boost_tasks[cpu])) {
retval = PTR_ERR(boost_tasks[cpu]);
VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
@@ -2036,8 +2035,6 @@ static int rcutorture_booster_init(unsigned int cpu)
mutex_unlock(&boost_mutex);
return retval;
}
- kthread_bind(boost_tasks[cpu], cpu);
- wake_up_process(boost_tasks[cpu]);
mutex_unlock(&boost_mutex);
return 0;
}
--
2.25.1

2021-10-22 03:01:11

by Cai,Huoqing

[permalink] [raw]
Subject: [PATCH v3 6/6] trace/hwlat: Make use of the helper function kthread_run_on_cpu()

Replace kthread_create_on_cpu/wake_up_process()
with kthread_run_on_cpu() to simplify the code.

Signed-off-by: Cai Huoqing <[email protected]>
---
kernel/trace/trace_hwlat.c | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/kernel/trace/trace_hwlat.c b/kernel/trace/trace_hwlat.c
index 1b83d75eb103..0e555335f095 100644
--- a/kernel/trace/trace_hwlat.c
+++ b/kernel/trace/trace_hwlat.c
@@ -491,18 +491,14 @@ static void stop_per_cpu_kthreads(void)
static int start_cpu_kthread(unsigned int cpu)
{
struct task_struct *kthread;
- char comm[24];

- snprintf(comm, 24, "hwlatd/%d", cpu);
-
- kthread = kthread_create_on_cpu(kthread_fn, NULL, cpu, comm);
+ kthread = kthread_run_on_cpu(kthread_fn, NULL, cpu, "hwlatd/%u");
if (IS_ERR(kthread)) {
pr_err(BANNER "could not start sampling thread\n");
return -ENOMEM;
}

per_cpu(hwlat_per_cpu_data, cpu).kthread = kthread;
- wake_up_process(kthread);

return 0;
}
--
2.25.1

2021-10-22 03:01:52

by Cai,Huoqing

[permalink] [raw]
Subject: [PATCH v3 2/6] RDMA/siw: Make use of the helper function kthread_run_on_cpu()

Replace kthread_create/kthread_bind/wake_up_process()
with kthread_run_on_cpu() to simplify the code.

Signed-off-by: Cai Huoqing <[email protected]>
---
drivers/infiniband/sw/siw/siw_main.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/infiniband/sw/siw/siw_main.c b/drivers/infiniband/sw/siw/siw_main.c
index 9093e6a80b26..e5c586913d0b 100644
--- a/drivers/infiniband/sw/siw/siw_main.c
+++ b/drivers/infiniband/sw/siw/siw_main.c
@@ -98,15 +98,14 @@ static int siw_create_tx_threads(void)
continue;

siw_tx_thread[cpu] =
- kthread_create(siw_run_sq, (unsigned long *)(long)cpu,
- "siw_tx/%d", cpu);
+ kthread_run_on_cpu(siw_run_sq,
+ (unsigned long *)(long)cpu,
+ cpu, "siw_tx/%u");
if (IS_ERR(siw_tx_thread[cpu])) {
siw_tx_thread[cpu] = NULL;
continue;
}
- kthread_bind(siw_tx_thread[cpu], cpu);

- wake_up_process(siw_tx_thread[cpu]);
assigned++;
}
return assigned;
--
2.25.1

2021-10-26 10:31:58

by Cai,Huoqing

[permalink] [raw]
Subject: RE: [PATCH v3 1/6] kthread: Add the helper function kthread_run_on_cpu()

Hello,
Just a ping, to see if there are any more comments :-P
> -----Original Message-----
> From: Cai,Huoqing <[email protected]>
> Sent: 2021??10??22?? 10:57
> Subject: [PATCH v3 1/6] kthread: Add the helper function kthread_run_on_cpu()
>
> 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 <[email protected]>
> ---
> 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-
> [email protected]/
> v2 link:
> https://lore.kernel.org/lkml/20211021122758.3092-2-
> [email protected]/
>
> 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

2021-11-16 21:01:55

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH v3 1/6] kthread: Add the helper function kthread_run_on_cpu()

On Tue, 26 Oct 2021 08:32:30 +0000
"Cai,Huoqing" <[email protected]> wrote:

> Hello,
> Just a ping, to see if there are any more comments :-P

I have no real issue with this patch set. As it seems to be a generic clean
up, perhaps Andrew might like to take a look at it, and if he's fine with
it, he can take it through his tree?

-- Steve


> > -----Original Message-----
> > From: Cai,Huoqing <[email protected]>
> > Sent: 2021年10月22日 10:57
> > Subject: [PATCH v3 1/6] kthread: Add the helper function kthread_run_on_cpu()
> >
> > 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 <[email protected]>
> > ---
> > 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-
> > [email protected]/
> > v2 link:
> > https://lore.kernel.org/lkml/20211021122758.3092-2-
> > [email protected]/
> >
> > 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
>