Skip to content

Commit 51824b7

Browse files
urezkipaulmckrcu
authored andcommitted
rcu/kvfree: Update KFREE_DRAIN_JIFFIES interval
Currently the monitor work is scheduled with a fixed interval of HZ/20, which is roughly 50 milliseconds. The drawback of this approach is low utilization of the 512 page slots in scenarios with infrequence kvfree_rcu() calls. For example on an Android system: <snip> kworker/3:3-507 [003] .... 470.286305: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x00000000d0f0dde5 nr_records=6 kworker/6:1-76 [006] .... 470.416613: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x00000000ea0d6556 nr_records=1 kworker/6:1-76 [006] .... 470.416625: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x000000003e025849 nr_records=9 kworker/3:3-507 [003] .... 471.390000: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x00000000815a8713 nr_records=48 kworker/1:1-73 [001] .... 471.725785: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x00000000fda9bf20 nr_records=3 kworker/1:1-73 [001] .... 471.725833: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x00000000a425b67b nr_records=76 kworker/0:4-1411 [000] .... 472.085673: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x000000007996be9d nr_records=1 kworker/0:4-1411 [000] .... 472.085728: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x00000000d0f0dde5 nr_records=5 kworker/6:1-76 [006] .... 472.260340: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x0000000065630ee4 nr_records=102 <snip> In many cases, out of 512 slots, fewer than 10 were actually used. In order to improve batching and make utilization more efficient this commit sets a drain interval to a fixed 5-seconds interval. Floods are detected when a page fills quickly, and in that case, the reclaim work is re-scheduled for the next scheduling-clock tick (jiffy). After this change: <snip> kworker/7:1-371 [007] .... 5630.725708: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x000000005ab0ffb3 nr_records=121 kworker/7:1-371 [007] .... 5630.989702: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x0000000060c84761 nr_records=47 kworker/7:1-371 [007] .... 5630.989714: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x000000000babf308 nr_records=510 kworker/7:1-371 [007] .... 5631.553790: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x00000000bb7bd0ef nr_records=169 kworker/7:1-371 [007] .... 5631.553808: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x0000000044c78753 nr_records=510 kworker/5:6-9428 [005] .... 5631.746102: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x00000000d98519aa nr_records=123 kworker/4:7-9434 [004] .... 5632.001758: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x00000000526c9d44 nr_records=322 kworker/4:7-9434 [004] .... 5632.002073: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x000000002c6a8afa nr_records=185 kworker/7:1-371 [007] .... 5632.277515: rcu_invoke_kfree_bulk_callback: rcu_preempt bulk=0x000000007f4a962f nr_records=510 <snip> Here, all but one of the cases, more than one hundreds slots were used, representing an order-of-magnitude improvement. Signed-off-by: Uladzislau Rezki (Sony) <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent 3826909 commit 51824b7

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

kernel/rcu/tree.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,7 +2832,7 @@ EXPORT_SYMBOL_GPL(call_rcu);
28322832

28332833

28342834
/* Maximum number of jiffies to wait before draining a batch. */
2835-
#define KFREE_DRAIN_JIFFIES (HZ / 50)
2835+
#define KFREE_DRAIN_JIFFIES (5 * HZ)
28362836
#define KFREE_N_BATCHES 2
28372837
#define FREE_N_CHANNELS 2
28382838

@@ -3093,6 +3093,21 @@ need_offload_krc(struct kfree_rcu_cpu *krcp)
30933093
return !!krcp->head;
30943094
}
30953095

3096+
static void
3097+
schedule_delayed_monitor_work(struct kfree_rcu_cpu *krcp)
3098+
{
3099+
long delay, delay_left;
3100+
3101+
delay = READ_ONCE(krcp->count) >= KVFREE_BULK_MAX_ENTR ? 1:KFREE_DRAIN_JIFFIES;
3102+
if (delayed_work_pending(&krcp->monitor_work)) {
3103+
delay_left = krcp->monitor_work.timer.expires - jiffies;
3104+
if (delay < delay_left)
3105+
mod_delayed_work(system_wq, &krcp->monitor_work, delay);
3106+
return;
3107+
}
3108+
queue_delayed_work(system_wq, &krcp->monitor_work, delay);
3109+
}
3110+
30963111
/*
30973112
* This function is invoked after the KFREE_DRAIN_JIFFIES timeout.
30983113
*/
@@ -3150,7 +3165,7 @@ static void kfree_rcu_monitor(struct work_struct *work)
31503165
// work to repeat an attempt. Because previous batches are
31513166
// still in progress.
31523167
if (need_offload_krc(krcp))
3153-
schedule_delayed_work(&krcp->monitor_work, KFREE_DRAIN_JIFFIES);
3168+
schedule_delayed_monitor_work(krcp);
31543169

31553170
raw_spin_unlock_irqrestore(&krcp->lock, flags);
31563171
}
@@ -3339,7 +3354,7 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
33393354

33403355
// Set timer to drain after KFREE_DRAIN_JIFFIES.
33413356
if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING)
3342-
schedule_delayed_work(&krcp->monitor_work, KFREE_DRAIN_JIFFIES);
3357+
schedule_delayed_monitor_work(krcp);
33433358

33443359
unlock_return:
33453360
krc_this_cpu_unlock(krcp, flags);
@@ -3415,7 +3430,7 @@ void __init kfree_rcu_scheduler_running(void)
34153430

34163431
raw_spin_lock_irqsave(&krcp->lock, flags);
34173432
if (need_offload_krc(krcp))
3418-
schedule_delayed_work_on(cpu, &krcp->monitor_work, KFREE_DRAIN_JIFFIES);
3433+
schedule_delayed_monitor_work(krcp);
34193434
raw_spin_unlock_irqrestore(&krcp->lock, flags);
34203435
}
34213436
}

0 commit comments

Comments
 (0)