Skip to content

Commit 41f70d8

Browse files
author
Frederic Weisbecker
committed
kthread: Unify kthread_create_on_cpu() and kthread_create_worker_on_cpu() automatic format
kthread_create_on_cpu() uses the CPU argument as an implicit and unique printf argument to add to the format whereas kthread_create_worker_on_cpu() still relies on explicitly passing the printf arguments. This difference in behaviour is error prone and doesn't help standardizing per-CPU kthread names. Unify the behaviours and convert kthread_create_worker_on_cpu() to use the printf behaviour of kthread_create_on_cpu(). Signed-off-by: Frederic Weisbecker <[email protected]>
1 parent db7ee3c commit 41f70d8

File tree

3 files changed

+52
-30
lines changed

3 files changed

+52
-30
lines changed

fs/erofs/zdata.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ static void erofs_destroy_percpu_workers(void)
320320
static struct kthread_worker *erofs_init_percpu_worker(int cpu)
321321
{
322322
struct kthread_worker *worker =
323-
kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
323+
kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u");
324324

325325
if (IS_ERR(worker))
326326
return worker;

include/linux/kthread.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,24 @@ extern void __kthread_init_worker(struct kthread_worker *worker,
187187

188188
int kthread_worker_fn(void *worker_ptr);
189189

190-
__printf(2, 3)
191-
struct kthread_worker *
192-
kthread_create_worker(unsigned int flags, const char namefmt[], ...);
190+
__printf(3, 4)
191+
struct kthread_worker *kthread_create_worker_on_node(unsigned int flags,
192+
int node,
193+
const char namefmt[], ...);
193194

194-
__printf(3, 4) struct kthread_worker *
195+
#define kthread_create_worker(flags, namefmt, ...) \
196+
({ \
197+
struct kthread_worker *__kw \
198+
= kthread_create_worker_on_node(flags, NUMA_NO_NODE, \
199+
namefmt, ## __VA_ARGS__); \
200+
if (!IS_ERR(__kw)) \
201+
wake_up_process(__kw->task); \
202+
__kw; \
203+
})
204+
205+
struct kthread_worker *
195206
kthread_create_worker_on_cpu(int cpu, unsigned int flags,
196-
const char namefmt[], ...);
207+
const char namefmt[]);
197208

198209
bool kthread_queue_work(struct kthread_worker *worker,
199210
struct kthread_work *work);

kernel/kthread.c

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,33 +1030,26 @@ int kthread_worker_fn(void *worker_ptr)
10301030
EXPORT_SYMBOL_GPL(kthread_worker_fn);
10311031

10321032
static __printf(3, 0) struct kthread_worker *
1033-
__kthread_create_worker(int cpu, unsigned int flags,
1034-
const char namefmt[], va_list args)
1033+
__kthread_create_worker_on_node(unsigned int flags, int node,
1034+
const char namefmt[], va_list args)
10351035
{
10361036
struct kthread_worker *worker;
10371037
struct task_struct *task;
1038-
int node = NUMA_NO_NODE;
10391038

10401039
worker = kzalloc(sizeof(*worker), GFP_KERNEL);
10411040
if (!worker)
10421041
return ERR_PTR(-ENOMEM);
10431042

10441043
kthread_init_worker(worker);
10451044

1046-
if (cpu >= 0)
1047-
node = cpu_to_node(cpu);
1048-
10491045
task = __kthread_create_on_node(kthread_worker_fn, worker,
1050-
node, namefmt, args);
1046+
node, namefmt, args);
10511047
if (IS_ERR(task))
10521048
goto fail_task;
10531049

1054-
if (cpu >= 0)
1055-
kthread_bind(task, cpu);
1056-
10571050
worker->flags = flags;
10581051
worker->task = task;
1059-
wake_up_process(task);
1052+
10601053
return worker;
10611054

10621055
fail_task:
@@ -1067,32 +1060,57 @@ __kthread_create_worker(int cpu, unsigned int flags,
10671060
/**
10681061
* kthread_create_worker - create a kthread worker
10691062
* @flags: flags modifying the default behavior of the worker
1063+
* @node: task structure for the thread is allocated on this node
10701064
* @namefmt: printf-style name for the kthread worker (task).
10711065
*
10721066
* Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
10731067
* when the needed structures could not get allocated, and ERR_PTR(-EINTR)
10741068
* when the caller was killed by a fatal signal.
10751069
*/
10761070
struct kthread_worker *
1077-
kthread_create_worker(unsigned int flags, const char namefmt[], ...)
1071+
kthread_create_worker_on_node(unsigned int flags, int node, const char namefmt[], ...)
1072+
{
1073+
struct kthread_worker *worker;
1074+
va_list args;
1075+
1076+
va_start(args, namefmt);
1077+
worker = __kthread_create_worker_on_node(flags, node, namefmt, args);
1078+
va_end(args);
1079+
1080+
if (worker)
1081+
wake_up_process(worker->task);
1082+
1083+
return worker;
1084+
}
1085+
EXPORT_SYMBOL(kthread_create_worker_on_node);
1086+
1087+
static __printf(3, 4) struct kthread_worker *
1088+
__kthread_create_worker_on_cpu(int cpu, unsigned int flags,
1089+
const char namefmt[], ...)
10781090
{
10791091
struct kthread_worker *worker;
10801092
va_list args;
10811093

10821094
va_start(args, namefmt);
1083-
worker = __kthread_create_worker(-1, flags, namefmt, args);
1095+
worker = __kthread_create_worker_on_node(flags, cpu_to_node(cpu),
1096+
namefmt, args);
10841097
va_end(args);
10851098

1099+
if (worker) {
1100+
kthread_bind(worker->task, cpu);
1101+
wake_up_process(worker->task);
1102+
}
1103+
10861104
return worker;
10871105
}
1088-
EXPORT_SYMBOL(kthread_create_worker);
10891106

10901107
/**
10911108
* kthread_create_worker_on_cpu - create a kthread worker and bind it
10921109
* to a given CPU and the associated NUMA node.
10931110
* @cpu: CPU number
10941111
* @flags: flags modifying the default behavior of the worker
1095-
* @namefmt: printf-style name for the kthread worker (task).
1112+
* @namefmt: printf-style name for the thread. Format is restricted
1113+
* to "name.*%u". Code fills in cpu number.
10961114
*
10971115
* Use a valid CPU number if you want to bind the kthread worker
10981116
* to the given CPU and the associated NUMA node.
@@ -1124,16 +1142,9 @@ EXPORT_SYMBOL(kthread_create_worker);
11241142
*/
11251143
struct kthread_worker *
11261144
kthread_create_worker_on_cpu(int cpu, unsigned int flags,
1127-
const char namefmt[], ...)
1145+
const char namefmt[])
11281146
{
1129-
struct kthread_worker *worker;
1130-
va_list args;
1131-
1132-
va_start(args, namefmt);
1133-
worker = __kthread_create_worker(cpu, flags, namefmt, args);
1134-
va_end(args);
1135-
1136-
return worker;
1147+
return __kthread_create_worker_on_cpu(cpu, flags, namefmt, cpu);
11371148
}
11381149
EXPORT_SYMBOL(kthread_create_worker_on_cpu);
11391150

0 commit comments

Comments
 (0)