Skip to content

Commit 5d01bbd

Browse files
committed
rcu: Yield simpler
The rcu_yield() code is amazing. It's there to avoid starvation of the system when lots of (boosting) work is to be done. Now looking at the code it's functionality is: Make the thread SCHED_OTHER and very nice, i.e. get it out of the way Arm a timer with 2 ticks schedule() Now if the system goes idle the rcu task returns, regains SCHED_FIFO and plugs on. If the systems stays busy the timer fires and wakes a per node kthread which in turn makes the per cpu thread SCHED_FIFO and brings it back on the cpu. For the boosting thread the "make it FIFO" bit is missing and it just runs some magic boost checks. Now this is a lot of code with extra threads and complexity. It's way simpler to let the tasks when they detect overload schedule away for 2 ticks and defer the normal wakeup as long as they are in yielded state and the cpu is not idle. That solves the same problem and the only difference is that when the cpu goes idle it's not guaranteed that the thread returns right away, but it won't be longer out than two ticks, so no harm is done. If that's an issue than it is way simpler just to wake the task from idle as RCU has callbacks there anyway. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com> Cc: Rusty Russell <rusty@rustcorp.com.au> Cc: Namhyung Kim <namhyung@kernel.org> Reviewed-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Link: http://lkml.kernel.org/r/20120716103948.131256723@linutronix.de Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 3bf671a commit 5d01bbd

3 files changed

Lines changed: 41 additions & 184 deletions

File tree

‎kernel/rcutree.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ DEFINE_PER_CPU(char, rcu_cpu_has_work);
139139

140140
#endif /* #ifdef CONFIG_RCU_BOOST */
141141

142-
static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
142+
static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
143143
static void invoke_rcu_core(void);
144144
static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
145145

@@ -1469,7 +1469,7 @@ static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
14691469

14701470
/* Adjust any no-longer-needed kthreads. */
14711471
rcu_stop_cpu_kthread(cpu);
1472-
rcu_node_kthread_setaffinity(rnp, -1);
1472+
rcu_boost_kthread_setaffinity(rnp, -1);
14731473

14741474
/* Remove the dead CPU from the bitmasks in the rcu_node hierarchy. */
14751475

@@ -2594,11 +2594,11 @@ static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
25942594
break;
25952595
case CPU_ONLINE:
25962596
case CPU_DOWN_FAILED:
2597-
rcu_node_kthread_setaffinity(rnp, -1);
2597+
rcu_boost_kthread_setaffinity(rnp, -1);
25982598
rcu_cpu_kthread_setrt(cpu, 1);
25992599
break;
26002600
case CPU_DOWN_PREPARE:
2601-
rcu_node_kthread_setaffinity(rnp, cpu);
2601+
rcu_boost_kthread_setaffinity(rnp, cpu);
26022602
rcu_cpu_kthread_setrt(cpu, 0);
26032603
break;
26042604
case CPU_DYING:

‎kernel/rcutree.h‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,8 @@ static void invoke_rcu_callbacks_kthread(void);
491491
static bool rcu_is_callbacks_kthread(void);
492492
#ifdef CONFIG_RCU_BOOST
493493
static void rcu_preempt_do_callbacks(void);
494-
static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp,
495-
cpumask_var_t cm);
496494
static int __cpuinit rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
497-
struct rcu_node *rnp,
498-
int rnp_index);
499-
static void invoke_rcu_node_kthread(struct rcu_node *rnp);
500-
static void rcu_yield(void (*f)(unsigned long), unsigned long arg);
495+
struct rcu_node *rnp);
501496
#endif /* #ifdef CONFIG_RCU_BOOST */
502497
static void rcu_cpu_kthread_setrt(int cpu, int to_rt);
503498
static void __cpuinit rcu_prepare_kthreads(int cpu);

‎kernel/rcutree_plugin.h‎

Lines changed: 36 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,16 @@ static void rcu_initiate_boost_trace(struct rcu_node *rnp)
10691069

10701070
#endif /* #else #ifdef CONFIG_RCU_TRACE */
10711071

1072+
static void rcu_wake_cond(struct task_struct *t, int status)
1073+
{
1074+
/*
1075+
* If the thread is yielding, only wake it when this
1076+
* is invoked from idle
1077+
*/
1078+
if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
1079+
wake_up_process(t);
1080+
}
1081+
10721082
/*
10731083
* Carry out RCU priority boosting on the task indicated by ->exp_tasks
10741084
* or ->boost_tasks, advancing the pointer to the next task in the
@@ -1140,17 +1150,6 @@ static int rcu_boost(struct rcu_node *rnp)
11401150
ACCESS_ONCE(rnp->boost_tasks) != NULL;
11411151
}
11421152

1143-
/*
1144-
* Timer handler to initiate waking up of boost kthreads that
1145-
* have yielded the CPU due to excessive numbers of tasks to
1146-
* boost. We wake up the per-rcu_node kthread, which in turn
1147-
* will wake up the booster kthread.
1148-
*/
1149-
static void rcu_boost_kthread_timer(unsigned long arg)
1150-
{
1151-
invoke_rcu_node_kthread((struct rcu_node *)arg);
1152-
}
1153-
11541153
/*
11551154
* Priority-boosting kthread. One per leaf rcu_node and one for the
11561155
* root rcu_node.
@@ -1174,8 +1173,9 @@ static int rcu_boost_kthread(void *arg)
11741173
else
11751174
spincnt = 0;
11761175
if (spincnt > 10) {
1176+
rnp->boost_kthread_status = RCU_KTHREAD_YIELDING;
11771177
trace_rcu_utilization("End boost kthread@rcu_yield");
1178-
rcu_yield(rcu_boost_kthread_timer, (unsigned long)rnp);
1178+
schedule_timeout_interruptible(2);
11791179
trace_rcu_utilization("Start boost kthread@rcu_yield");
11801180
spincnt = 0;
11811181
}
@@ -1213,8 +1213,8 @@ static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
12131213
rnp->boost_tasks = rnp->gp_tasks;
12141214
raw_spin_unlock_irqrestore(&rnp->lock, flags);
12151215
t = rnp->boost_kthread_task;
1216-
if (t != NULL)
1217-
wake_up_process(t);
1216+
if (t)
1217+
rcu_wake_cond(t, rnp->boost_kthread_status);
12181218
} else {
12191219
rcu_initiate_boost_trace(rnp);
12201220
raw_spin_unlock_irqrestore(&rnp->lock, flags);
@@ -1231,8 +1231,10 @@ static void invoke_rcu_callbacks_kthread(void)
12311231
local_irq_save(flags);
12321232
__this_cpu_write(rcu_cpu_has_work, 1);
12331233
if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
1234-
current != __this_cpu_read(rcu_cpu_kthread_task))
1235-
wake_up_process(__this_cpu_read(rcu_cpu_kthread_task));
1234+
current != __this_cpu_read(rcu_cpu_kthread_task)) {
1235+
rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
1236+
__this_cpu_read(rcu_cpu_kthread_status));
1237+
}
12361238
local_irq_restore(flags);
12371239
}
12381240

@@ -1245,21 +1247,6 @@ static bool rcu_is_callbacks_kthread(void)
12451247
return __get_cpu_var(rcu_cpu_kthread_task) == current;
12461248
}
12471249

1248-
/*
1249-
* Set the affinity of the boost kthread. The CPU-hotplug locks are
1250-
* held, so no one should be messing with the existence of the boost
1251-
* kthread.
1252-
*/
1253-
static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp,
1254-
cpumask_var_t cm)
1255-
{
1256-
struct task_struct *t;
1257-
1258-
t = rnp->boost_kthread_task;
1259-
if (t != NULL)
1260-
set_cpus_allowed_ptr(rnp->boost_kthread_task, cm);
1261-
}
1262-
12631250
#define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
12641251

12651252
/*
@@ -1276,15 +1263,19 @@ static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
12761263
* Returns zero if all is well, a negated errno otherwise.
12771264
*/
12781265
static int __cpuinit rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
1279-
struct rcu_node *rnp,
1280-
int rnp_index)
1266+
struct rcu_node *rnp)
12811267
{
1268+
int rnp_index = rnp - &rsp->node[0];
12821269
unsigned long flags;
12831270
struct sched_param sp;
12841271
struct task_struct *t;
12851272

12861273
if (&rcu_preempt_state != rsp)
12871274
return 0;
1275+
1276+
if (!rcu_scheduler_fully_active || rnp->qsmaskinit == 0)
1277+
return 0;
1278+
12881279
rsp->boost = 1;
12891280
if (rnp->boost_kthread_task != NULL)
12901281
return 0;
@@ -1327,20 +1318,6 @@ static void rcu_kthread_do_work(void)
13271318
rcu_preempt_do_callbacks();
13281319
}
13291320

1330-
/*
1331-
* Wake up the specified per-rcu_node-structure kthread.
1332-
* Because the per-rcu_node kthreads are immortal, we don't need
1333-
* to do anything to keep them alive.
1334-
*/
1335-
static void invoke_rcu_node_kthread(struct rcu_node *rnp)
1336-
{
1337-
struct task_struct *t;
1338-
1339-
t = rnp->node_kthread_task;
1340-
if (t != NULL)
1341-
wake_up_process(t);
1342-
}
1343-
13441321
/*
13451322
* Set the specified CPU's kthread to run RT or not, as specified by
13461323
* the to_rt argument. The CPU-hotplug locks are held, so the task
@@ -1365,45 +1342,6 @@ static void rcu_cpu_kthread_setrt(int cpu, int to_rt)
13651342
sched_setscheduler_nocheck(t, policy, &sp);
13661343
}
13671344

1368-
/*
1369-
* Timer handler to initiate the waking up of per-CPU kthreads that
1370-
* have yielded the CPU due to excess numbers of RCU callbacks.
1371-
* We wake up the per-rcu_node kthread, which in turn will wake up
1372-
* the booster kthread.
1373-
*/
1374-
static void rcu_cpu_kthread_timer(unsigned long arg)
1375-
{
1376-
struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, arg);
1377-
struct rcu_node *rnp = rdp->mynode;
1378-
1379-
atomic_or(rdp->grpmask, &rnp->wakemask);
1380-
invoke_rcu_node_kthread(rnp);
1381-
}
1382-
1383-
/*
1384-
* Drop to non-real-time priority and yield, but only after posting a
1385-
* timer that will cause us to regain our real-time priority if we
1386-
* remain preempted. Either way, we restore our real-time priority
1387-
* before returning.
1388-
*/
1389-
static void rcu_yield(void (*f)(unsigned long), unsigned long arg)
1390-
{
1391-
struct sched_param sp;
1392-
struct timer_list yield_timer;
1393-
int prio = current->rt_priority;
1394-
1395-
setup_timer_on_stack(&yield_timer, f, arg);
1396-
mod_timer(&yield_timer, jiffies + 2);
1397-
sp.sched_priority = 0;
1398-
sched_setscheduler_nocheck(current, SCHED_NORMAL, &sp);
1399-
set_user_nice(current, 19);
1400-
schedule();
1401-
set_user_nice(current, 0);
1402-
sp.sched_priority = prio;
1403-
sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
1404-
del_timer(&yield_timer);
1405-
}
1406-
14071345
/*
14081346
* Handle cases where the rcu_cpu_kthread() ends up on the wrong CPU.
14091347
* This can happen while the corresponding CPU is either coming online
@@ -1476,7 +1414,7 @@ static int rcu_cpu_kthread(void *arg)
14761414
if (spincnt > 10) {
14771415
*statusp = RCU_KTHREAD_YIELDING;
14781416
trace_rcu_utilization("End CPU kthread@rcu_yield");
1479-
rcu_yield(rcu_cpu_kthread_timer, (unsigned long)cpu);
1417+
schedule_timeout_interruptible(2);
14801418
trace_rcu_utilization("Start CPU kthread@rcu_yield");
14811419
spincnt = 0;
14821420
}
@@ -1532,48 +1470,6 @@ static int __cpuinit rcu_spawn_one_cpu_kthread(int cpu)
15321470
return 0;
15331471
}
15341472

1535-
/*
1536-
* Per-rcu_node kthread, which is in charge of waking up the per-CPU
1537-
* kthreads when needed. We ignore requests to wake up kthreads
1538-
* for offline CPUs, which is OK because force_quiescent_state()
1539-
* takes care of this case.
1540-
*/
1541-
static int rcu_node_kthread(void *arg)
1542-
{
1543-
int cpu;
1544-
unsigned long flags;
1545-
unsigned long mask;
1546-
struct rcu_node *rnp = (struct rcu_node *)arg;
1547-
struct sched_param sp;
1548-
struct task_struct *t;
1549-
1550-
for (;;) {
1551-
rnp->node_kthread_status = RCU_KTHREAD_WAITING;
1552-
rcu_wait(atomic_read(&rnp->wakemask) != 0);
1553-
rnp->node_kthread_status = RCU_KTHREAD_RUNNING;
1554-
raw_spin_lock_irqsave(&rnp->lock, flags);
1555-
mask = atomic_xchg(&rnp->wakemask, 0);
1556-
rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
1557-
for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1) {
1558-
if ((mask & 0x1) == 0)
1559-
continue;
1560-
preempt_disable();
1561-
t = per_cpu(rcu_cpu_kthread_task, cpu);
1562-
if (!cpu_online(cpu) || t == NULL) {
1563-
preempt_enable();
1564-
continue;
1565-
}
1566-
per_cpu(rcu_cpu_has_work, cpu) = 1;
1567-
sp.sched_priority = RCU_KTHREAD_PRIO;
1568-
sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1569-
preempt_enable();
1570-
}
1571-
}
1572-
/* NOTREACHED */
1573-
rnp->node_kthread_status = RCU_KTHREAD_STOPPED;
1574-
return 0;
1575-
}
1576-
15771473
/*
15781474
* Set the per-rcu_node kthread's affinity to cover all CPUs that are
15791475
* served by the rcu_node in question. The CPU hotplug lock is still
@@ -1583,17 +1479,17 @@ static int rcu_node_kthread(void *arg)
15831479
* no outgoing CPU. If there are no CPUs left in the affinity set,
15841480
* this function allows the kthread to execute on any CPU.
15851481
*/
1586-
static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1482+
static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
15871483
{
1484+
struct task_struct *t = rnp->boost_kthread_task;
1485+
unsigned long mask = rnp->qsmaskinit;
15881486
cpumask_var_t cm;
15891487
int cpu;
1590-
unsigned long mask = rnp->qsmaskinit;
15911488

1592-
if (rnp->node_kthread_task == NULL)
1489+
if (!t)
15931490
return;
1594-
if (!alloc_cpumask_var(&cm, GFP_KERNEL))
1491+
if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
15951492
return;
1596-
cpumask_clear(cm);
15971493
for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1)
15981494
if ((mask & 0x1) && cpu != outgoingcpu)
15991495
cpumask_set_cpu(cpu, cm);
@@ -1603,50 +1499,17 @@ static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
16031499
cpumask_clear_cpu(cpu, cm);
16041500
WARN_ON_ONCE(cpumask_weight(cm) == 0);
16051501
}
1606-
set_cpus_allowed_ptr(rnp->node_kthread_task, cm);
1607-
rcu_boost_kthread_setaffinity(rnp, cm);
1502+
set_cpus_allowed_ptr(t, cm);
16081503
free_cpumask_var(cm);
16091504
}
16101505

1611-
/*
1612-
* Spawn a per-rcu_node kthread, setting priority and affinity.
1613-
* Called during boot before online/offline can happen, or, if
1614-
* during runtime, with the main CPU-hotplug locks held. So only
1615-
* one of these can be executing at a time.
1616-
*/
1617-
static int __cpuinit rcu_spawn_one_node_kthread(struct rcu_state *rsp,
1618-
struct rcu_node *rnp)
1619-
{
1620-
unsigned long flags;
1621-
int rnp_index = rnp - &rsp->node[0];
1622-
struct sched_param sp;
1623-
struct task_struct *t;
1624-
1625-
if (!rcu_scheduler_fully_active ||
1626-
rnp->qsmaskinit == 0)
1627-
return 0;
1628-
if (rnp->node_kthread_task == NULL) {
1629-
t = kthread_create(rcu_node_kthread, (void *)rnp,
1630-
"rcun/%d", rnp_index);
1631-
if (IS_ERR(t))
1632-
return PTR_ERR(t);
1633-
raw_spin_lock_irqsave(&rnp->lock, flags);
1634-
rnp->node_kthread_task = t;
1635-
raw_spin_unlock_irqrestore(&rnp->lock, flags);
1636-
sp.sched_priority = 99;
1637-
sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1638-
wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
1639-
}
1640-
return rcu_spawn_one_boost_kthread(rsp, rnp, rnp_index);
1641-
}
1642-
16431506
/*
16441507
* Spawn all kthreads -- called as soon as the scheduler is running.
16451508
*/
16461509
static int __init rcu_spawn_kthreads(void)
16471510
{
1648-
int cpu;
16491511
struct rcu_node *rnp;
1512+
int cpu;
16501513

16511514
rcu_scheduler_fully_active = 1;
16521515
for_each_possible_cpu(cpu) {
@@ -1655,10 +1518,10 @@ static int __init rcu_spawn_kthreads(void)
16551518
(void)rcu_spawn_one_cpu_kthread(cpu);
16561519
}
16571520
rnp = rcu_get_root(rcu_state);
1658-
(void)rcu_spawn_one_node_kthread(rcu_state, rnp);
1521+
(void)rcu_spawn_one_boost_kthread(rcu_state, rnp);
16591522
if (NUM_RCU_NODES > 1) {
16601523
rcu_for_each_leaf_node(rcu_state, rnp)
1661-
(void)rcu_spawn_one_node_kthread(rcu_state, rnp);
1524+
(void)rcu_spawn_one_boost_kthread(rcu_state, rnp);
16621525
}
16631526
return 0;
16641527
}
@@ -1672,8 +1535,7 @@ static void __cpuinit rcu_prepare_kthreads(int cpu)
16721535
/* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
16731536
if (rcu_scheduler_fully_active) {
16741537
(void)rcu_spawn_one_cpu_kthread(cpu);
1675-
if (rnp->node_kthread_task == NULL)
1676-
(void)rcu_spawn_one_node_kthread(rcu_state, rnp);
1538+
(void)rcu_spawn_one_boost_kthread(rcu_state, rnp);
16771539
}
16781540
}
16791541

@@ -1706,7 +1568,7 @@ static void rcu_stop_cpu_kthread(int cpu)
17061568

17071569
#endif /* #ifdef CONFIG_HOTPLUG_CPU */
17081570

1709-
static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1571+
static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
17101572
{
17111573
}
17121574

0 commit comments

Comments
 (0)