Skip to content

Commit 34c8817

Browse files
urezkipaulmckrcu
authored andcommitted
rcu: Support kfree_bulk() interface in kfree_rcu()
The kfree_rcu() logic can be improved further by using kfree_bulk() interface along with "basic batching support" introduced earlier. The are at least two advantages of using "bulk" interface: - in case of large number of kfree_rcu() requests kfree_bulk() reduces the per-object overhead caused by calling kfree() per-object. - reduces the number of cache-misses due to "pointer chasing" between objects which can be far spread between each other. This approach defines a new kfree_rcu_bulk_data structure that stores pointers in an array with a specific size. Number of entries in that array depends on PAGE_SIZE making kfree_rcu_bulk_data structure to be exactly one page. Since it deals with "block-chain" technique there is an extra need in dynamic allocation when a new block is required. Memory is allocated with GFP_NOWAIT | __GFP_NOWARN flags, i.e. that allows to skip direct reclaim under low memory condition to prevent stalling and fails silently under high memory pressure. The "emergency path" gets maintained when a system is run out of memory. In that case objects are linked into regular list. The "rcuperf" was run to analyze this change in terms of memory consumption and kfree_bulk() throughput. 1) Testing on the Intel(R) Xeon(R) W-2135 CPU @ 3.70GHz, 12xCPUs with following parameters: kfree_loops=200000 kfree_alloc_num=1000 kfree_rcu_test=1 kfree_vary_obj_size=1 dev.2020.01.10a branch Default / CONFIG_SLAB 53607352517 ns, loops: 200000, batches: 1885, memory footprint: 1248MB 53529637912 ns, loops: 200000, batches: 1921, memory footprint: 1193MB 53570175705 ns, loops: 200000, batches: 1929, memory footprint: 1250MB Patch / CONFIG_SLAB 23981587315 ns, loops: 200000, batches: 810, memory footprint: 1219MB 23879375281 ns, loops: 200000, batches: 822, memory footprint: 1190MB 24086841707 ns, loops: 200000, batches: 794, memory footprint: 1380MB Default / CONFIG_SLUB 51291025022 ns, loops: 200000, batches: 1713, memory footprint: 741MB 51278911477 ns, loops: 200000, batches: 1671, memory footprint: 719MB 51256183045 ns, loops: 200000, batches: 1719, memory footprint: 647MB Patch / CONFIG_SLUB 50709919132 ns, loops: 200000, batches: 1618, memory footprint: 456MB 50736297452 ns, loops: 200000, batches: 1633, memory footprint: 507MB 50660403893 ns, loops: 200000, batches: 1628, memory footprint: 429MB in case of CONFIG_SLAB there is double increase in performance and slightly higher memory usage. As for CONFIG_SLUB, the performance figures are better together with lower memory usage. 2) Testing on the HiKey-960, arm64, 8xCPUs with below parameters: CONFIG_SLAB=y kfree_loops=200000 kfree_alloc_num=1000 kfree_rcu_test=1 102898760401 ns, loops: 200000, batches: 5822, memory footprint: 158MB 89947009882 ns, loops: 200000, batches: 6715, memory footprint: 115MB rcuperf shows approximately ~12% better throughput in case of using "bulk" interface. The "drain logic" or its RCU callback does the work faster that leads to better throughput. Signed-off-by: Uladzislau Rezki (Sony) <[email protected]> Tested-by: Joel Fernandes (Google) <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent bb6d3fb commit 34c8817

File tree

1 file changed

+169
-35
lines changed

1 file changed

+169
-35
lines changed

kernel/rcu/tree.c

Lines changed: 169 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,22 +2689,47 @@ EXPORT_SYMBOL_GPL(call_rcu);
26892689
#define KFREE_DRAIN_JIFFIES (HZ / 50)
26902690
#define KFREE_N_BATCHES 2
26912691

2692+
/*
2693+
* This macro defines how many entries the "records" array
2694+
* will contain. It is based on the fact that the size of
2695+
* kfree_rcu_bulk_data structure becomes exactly one page.
2696+
*/
2697+
#define KFREE_BULK_MAX_ENTR ((PAGE_SIZE / sizeof(void *)) - 3)
2698+
2699+
/**
2700+
* struct kfree_rcu_bulk_data - single block to store kfree_rcu() pointers
2701+
* @nr_records: Number of active pointers in the array
2702+
* @records: Array of the kfree_rcu() pointers
2703+
* @next: Next bulk object in the block chain
2704+
* @head_free_debug: For debug, when CONFIG_DEBUG_OBJECTS_RCU_HEAD is set
2705+
*/
2706+
struct kfree_rcu_bulk_data {
2707+
unsigned long nr_records;
2708+
void *records[KFREE_BULK_MAX_ENTR];
2709+
struct kfree_rcu_bulk_data *next;
2710+
struct rcu_head *head_free_debug;
2711+
};
2712+
26922713
/**
26932714
* struct kfree_rcu_cpu_work - single batch of kfree_rcu() requests
26942715
* @rcu_work: Let queue_rcu_work() invoke workqueue handler after grace period
26952716
* @head_free: List of kfree_rcu() objects waiting for a grace period
2717+
* @bhead_free: Bulk-List of kfree_rcu() objects waiting for a grace period
26962718
* @krcp: Pointer to @kfree_rcu_cpu structure
26972719
*/
26982720

26992721
struct kfree_rcu_cpu_work {
27002722
struct rcu_work rcu_work;
27012723
struct rcu_head *head_free;
2724+
struct kfree_rcu_bulk_data *bhead_free;
27022725
struct kfree_rcu_cpu *krcp;
27032726
};
27042727

27052728
/**
27062729
* struct kfree_rcu_cpu - batch up kfree_rcu() requests for RCU grace period
27072730
* @head: List of kfree_rcu() objects not yet waiting for a grace period
2731+
* @bhead: Bulk-List of kfree_rcu() objects not yet waiting for a grace period
2732+
* @bcached: Keeps at most one object for later reuse when build chain blocks
27082733
* @krw_arr: Array of batches of kfree_rcu() objects waiting for a grace period
27092734
* @lock: Synchronize access to this structure
27102735
* @monitor_work: Promote @head to @head_free after KFREE_DRAIN_JIFFIES
@@ -2718,6 +2743,8 @@ struct kfree_rcu_cpu_work {
27182743
*/
27192744
struct kfree_rcu_cpu {
27202745
struct rcu_head *head;
2746+
struct kfree_rcu_bulk_data *bhead;
2747+
struct kfree_rcu_bulk_data *bcached;
27212748
struct kfree_rcu_cpu_work krw_arr[KFREE_N_BATCHES];
27222749
spinlock_t lock;
27232750
struct delayed_work monitor_work;
@@ -2727,14 +2754,24 @@ struct kfree_rcu_cpu {
27272754

27282755
static DEFINE_PER_CPU(struct kfree_rcu_cpu, krc);
27292756

2757+
static __always_inline void
2758+
debug_rcu_head_unqueue_bulk(struct rcu_head *head)
2759+
{
2760+
#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2761+
for (; head; head = head->next)
2762+
debug_rcu_head_unqueue(head);
2763+
#endif
2764+
}
2765+
27302766
/*
27312767
* This function is invoked in workqueue context after a grace period.
2732-
* It frees all the objects queued on ->head_free.
2768+
* It frees all the objects queued on ->bhead_free or ->head_free.
27332769
*/
27342770
static void kfree_rcu_work(struct work_struct *work)
27352771
{
27362772
unsigned long flags;
27372773
struct rcu_head *head, *next;
2774+
struct kfree_rcu_bulk_data *bhead, *bnext;
27382775
struct kfree_rcu_cpu *krcp;
27392776
struct kfree_rcu_cpu_work *krwp;
27402777

@@ -2744,22 +2781,41 @@ static void kfree_rcu_work(struct work_struct *work)
27442781
spin_lock_irqsave(&krcp->lock, flags);
27452782
head = krwp->head_free;
27462783
krwp->head_free = NULL;
2784+
bhead = krwp->bhead_free;
2785+
krwp->bhead_free = NULL;
27472786
spin_unlock_irqrestore(&krcp->lock, flags);
27482787

2749-
// List "head" is now private, so traverse locklessly.
2788+
/* "bhead" is now private, so traverse locklessly. */
2789+
for (; bhead; bhead = bnext) {
2790+
bnext = bhead->next;
2791+
2792+
debug_rcu_head_unqueue_bulk(bhead->head_free_debug);
2793+
2794+
rcu_lock_acquire(&rcu_callback_map);
2795+
kfree_bulk(bhead->nr_records, bhead->records);
2796+
rcu_lock_release(&rcu_callback_map);
2797+
2798+
if (cmpxchg(&krcp->bcached, NULL, bhead))
2799+
free_page((unsigned long) bhead);
2800+
2801+
cond_resched_tasks_rcu_qs();
2802+
}
2803+
2804+
/*
2805+
* Emergency case only. It can happen under low memory
2806+
* condition when an allocation gets failed, so the "bulk"
2807+
* path can not be temporary maintained.
2808+
*/
27502809
for (; head; head = next) {
27512810
unsigned long offset = (unsigned long)head->func;
27522811

27532812
next = head->next;
2754-
// Potentially optimize with kfree_bulk in future.
27552813
debug_rcu_head_unqueue(head);
27562814
rcu_lock_acquire(&rcu_callback_map);
27572815
trace_rcu_invoke_kfree_callback(rcu_state.name, head, offset);
27582816

2759-
if (!WARN_ON_ONCE(!__is_kfree_rcu_offset(offset))) {
2760-
/* Could be optimized with kfree_bulk() in future. */
2817+
if (!WARN_ON_ONCE(!__is_kfree_rcu_offset(offset)))
27612818
kfree((void *)head - offset);
2762-
}
27632819

27642820
rcu_lock_release(&rcu_callback_map);
27652821
cond_resched_tasks_rcu_qs();
@@ -2774,26 +2830,48 @@ static void kfree_rcu_work(struct work_struct *work)
27742830
*/
27752831
static inline bool queue_kfree_rcu_work(struct kfree_rcu_cpu *krcp)
27762832
{
2833+
struct kfree_rcu_cpu_work *krwp;
2834+
bool queued = false;
27772835
int i;
2778-
struct kfree_rcu_cpu_work *krwp = NULL;
27792836

27802837
lockdep_assert_held(&krcp->lock);
2781-
for (i = 0; i < KFREE_N_BATCHES; i++)
2782-
if (!krcp->krw_arr[i].head_free) {
2783-
krwp = &(krcp->krw_arr[i]);
2784-
break;
2785-
}
27862838

2787-
// If a previous RCU batch is in progress, we cannot immediately
2788-
// queue another one, so return false to tell caller to retry.
2789-
if (!krwp)
2790-
return false;
2839+
for (i = 0; i < KFREE_N_BATCHES; i++) {
2840+
krwp = &(krcp->krw_arr[i]);
27912841

2792-
krwp->head_free = krcp->head;
2793-
krcp->head = NULL;
2794-
INIT_RCU_WORK(&krwp->rcu_work, kfree_rcu_work);
2795-
queue_rcu_work(system_wq, &krwp->rcu_work);
2796-
return true;
2842+
/*
2843+
* Try to detach bhead or head and attach it over any
2844+
* available corresponding free channel. It can be that
2845+
* a previous RCU batch is in progress, it means that
2846+
* immediately to queue another one is not possible so
2847+
* return false to tell caller to retry.
2848+
*/
2849+
if ((krcp->bhead && !krwp->bhead_free) ||
2850+
(krcp->head && !krwp->head_free)) {
2851+
/* Channel 1. */
2852+
if (!krwp->bhead_free) {
2853+
krwp->bhead_free = krcp->bhead;
2854+
krcp->bhead = NULL;
2855+
}
2856+
2857+
/* Channel 2. */
2858+
if (!krwp->head_free) {
2859+
krwp->head_free = krcp->head;
2860+
krcp->head = NULL;
2861+
}
2862+
2863+
/*
2864+
* One work is per one batch, so there are two "free channels",
2865+
* "bhead_free" and "head_free" the batch can handle. It can be
2866+
* that the work is in the pending state when two channels have
2867+
* been detached following each other, one by one.
2868+
*/
2869+
queue_rcu_work(system_wq, &krwp->rcu_work);
2870+
queued = true;
2871+
}
2872+
}
2873+
2874+
return queued;
27972875
}
27982876

27992877
static inline void kfree_rcu_drain_unlock(struct kfree_rcu_cpu *krcp,
@@ -2830,19 +2908,65 @@ static void kfree_rcu_monitor(struct work_struct *work)
28302908
spin_unlock_irqrestore(&krcp->lock, flags);
28312909
}
28322910

2911+
static inline bool
2912+
kfree_call_rcu_add_ptr_to_bulk(struct kfree_rcu_cpu *krcp,
2913+
struct rcu_head *head, rcu_callback_t func)
2914+
{
2915+
struct kfree_rcu_bulk_data *bnode;
2916+
2917+
if (unlikely(!krcp->initialized))
2918+
return false;
2919+
2920+
lockdep_assert_held(&krcp->lock);
2921+
2922+
/* Check if a new block is required. */
2923+
if (!krcp->bhead ||
2924+
krcp->bhead->nr_records == KFREE_BULK_MAX_ENTR) {
2925+
bnode = xchg(&krcp->bcached, NULL);
2926+
if (!bnode) {
2927+
WARN_ON_ONCE(sizeof(struct kfree_rcu_bulk_data) > PAGE_SIZE);
2928+
2929+
bnode = (struct kfree_rcu_bulk_data *)
2930+
__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
2931+
}
2932+
2933+
/* Switch to emergency path. */
2934+
if (unlikely(!bnode))
2935+
return false;
2936+
2937+
/* Initialize the new block. */
2938+
bnode->nr_records = 0;
2939+
bnode->next = krcp->bhead;
2940+
bnode->head_free_debug = NULL;
2941+
2942+
/* Attach it to the head. */
2943+
krcp->bhead = bnode;
2944+
}
2945+
2946+
#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2947+
head->func = func;
2948+
head->next = krcp->bhead->head_free_debug;
2949+
krcp->bhead->head_free_debug = head;
2950+
#endif
2951+
2952+
/* Finally insert. */
2953+
krcp->bhead->records[krcp->bhead->nr_records++] =
2954+
(void *) head - (unsigned long) func;
2955+
2956+
return true;
2957+
}
2958+
28332959
/*
2834-
* Queue a request for lazy invocation of kfree() after a grace period.
2960+
* Queue a request for lazy invocation of kfree_bulk()/kfree() after a grace
2961+
* period. Please note there are two paths are maintained, one is the main one
2962+
* that uses kfree_bulk() interface and second one is emergency one, that is
2963+
* used only when the main path can not be maintained temporary, due to memory
2964+
* pressure.
28352965
*
28362966
* Each kfree_call_rcu() request is added to a batch. The batch will be drained
2837-
* every KFREE_DRAIN_JIFFIES number of jiffies. All the objects in the batch
2838-
* will be kfree'd in workqueue context. This allows us to:
2839-
*
2840-
* 1. Batch requests together to reduce the number of grace periods during
2841-
* heavy kfree_rcu() load.
2842-
*
2843-
* 2. It makes it possible to use kfree_bulk() on a large number of
2844-
* kfree_rcu() requests thus reducing cache misses and the per-object
2845-
* overhead of kfree().
2967+
* every KFREE_DRAIN_JIFFIES number of jiffies. All the objects in the batch will
2968+
* be free'd in workqueue context. This allows us to: batch requests together to
2969+
* reduce the number of grace periods during heavy kfree_rcu() load.
28462970
*/
28472971
void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
28482972
{
@@ -2861,9 +2985,16 @@ void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
28612985
__func__, head);
28622986
goto unlock_return;
28632987
}
2864-
head->func = func;
2865-
head->next = krcp->head;
2866-
krcp->head = head;
2988+
2989+
/*
2990+
* Under high memory pressure GFP_NOWAIT can fail,
2991+
* in that case the emergency path is maintained.
2992+
*/
2993+
if (unlikely(!kfree_call_rcu_add_ptr_to_bulk(krcp, head, func))) {
2994+
head->func = func;
2995+
head->next = krcp->head;
2996+
krcp->head = head;
2997+
}
28672998

28682999
// Set timer to drain after KFREE_DRAIN_JIFFIES.
28693000
if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING &&
@@ -3769,8 +3900,11 @@ static void __init kfree_rcu_batch_init(void)
37693900
struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
37703901

37713902
spin_lock_init(&krcp->lock);
3772-
for (i = 0; i < KFREE_N_BATCHES; i++)
3903+
for (i = 0; i < KFREE_N_BATCHES; i++) {
3904+
INIT_RCU_WORK(&krcp->krw_arr[i].rcu_work, kfree_rcu_work);
37733905
krcp->krw_arr[i].krcp = krcp;
3906+
}
3907+
37743908
INIT_DELAYED_WORK(&krcp->monitor_work, kfree_rcu_monitor);
37753909
krcp->initialized = true;
37763910
}

0 commit comments

Comments
 (0)