Skip to content

Commit 84a8f44

Browse files
committed
rcu: Defer rcu_tasks_kthread() creation till first call_rcu_tasks()
It is expected that many sites will have CONFIG_TASKS_RCU=y, but will never actually invoke call_rcu_tasks(). For such sites, creating rcu_tasks_kthread() at boot is wasteful. This commit therefore defers creation of this kthread until the time of the first call_rcu_tasks(). This of course means that the first call_rcu_tasks() must be invoked from process context after the scheduler is fully operational. Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
1 parent 37fe5f0 commit 84a8f44

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

‎kernel/rcu/update.c‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,12 @@ DEFINE_SRCU(tasks_rcu_exit_srcu);
375375
static int rcu_task_stall_timeout __read_mostly = HZ * 60 * 10;
376376
module_param(rcu_task_stall_timeout, int, 0644);
377377

378-
/* Post an RCU-tasks callback. */
378+
static void rcu_spawn_tasks_kthread(void);
379+
380+
/*
381+
* Post an RCU-tasks callback. First call must be from process context
382+
* after the scheduler if fully operational.
383+
*/
379384
void call_rcu_tasks(struct rcu_head *rhp, void (*func)(struct rcu_head *rhp))
380385
{
381386
unsigned long flags;
@@ -388,8 +393,10 @@ void call_rcu_tasks(struct rcu_head *rhp, void (*func)(struct rcu_head *rhp))
388393
*rcu_tasks_cbs_tail = rhp;
389394
rcu_tasks_cbs_tail = &rhp->next;
390395
raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
391-
if (needwake)
396+
if (needwake) {
397+
rcu_spawn_tasks_kthread();
392398
wake_up(&rcu_tasks_cbs_wq);
399+
}
393400
}
394401
EXPORT_SYMBOL_GPL(call_rcu_tasks);
395402

@@ -615,15 +622,27 @@ static int __noreturn rcu_tasks_kthread(void *arg)
615622
}
616623
}
617624

618-
/* Spawn rcu_tasks_kthread() at boot time. */
619-
static int __init rcu_spawn_tasks_kthread(void)
625+
/* Spawn rcu_tasks_kthread() at first call to call_rcu_tasks(). */
626+
static void rcu_spawn_tasks_kthread(void)
620627
{
621-
struct task_struct __maybe_unused *t;
628+
static DEFINE_MUTEX(rcu_tasks_kthread_mutex);
629+
static struct task_struct *rcu_tasks_kthread_ptr;
630+
struct task_struct *t;
622631

632+
if (ACCESS_ONCE(rcu_tasks_kthread_ptr)) {
633+
smp_mb(); /* Ensure caller sees full kthread. */
634+
return;
635+
}
636+
mutex_lock(&rcu_tasks_kthread_mutex);
637+
if (rcu_tasks_kthread_ptr) {
638+
mutex_unlock(&rcu_tasks_kthread_mutex);
639+
return;
640+
}
623641
t = kthread_run(rcu_tasks_kthread, NULL, "rcu_tasks_kthread");
624642
BUG_ON(IS_ERR(t));
625-
return 0;
643+
smp_mb(); /* Ensure others see full kthread. */
644+
ACCESS_ONCE(rcu_tasks_kthread_ptr) = t;
645+
mutex_unlock(&rcu_tasks_kthread_mutex);
626646
}
627-
early_initcall(rcu_spawn_tasks_kthread);
628647

629648
#endif /* #ifdef CONFIG_TASKS_RCU */

0 commit comments

Comments
 (0)