Skip to content

Commit 53c72b5

Browse files
urezkipaulmckrcu
authored andcommitted
rcu/tree: cache specified number of objects
In order to reduce the dynamic need for pages in kfree_rcu(), pre-allocate a configurable number of pages per CPU and link them in a list. When kfree_rcu() reclaims objects, the object's container page is cached into a list instead of being released to the low-level page allocator. Such an approach provides O(1) access to free pages while also reducing the number of requests to the page allocator. It also makes the kfree_rcu() code to have free pages available during a low memory condition. A read-only sysfs parameter (rcu_min_cached_objs) reflects the minimum number of allowed cached pages per CPU. Signed-off-by: Uladzislau Rezki (Sony) <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent 69f08d3 commit 53c72b5

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4038,6 +4038,14 @@
40384038
latencies, which will choose a value aligned
40394039
with the appropriate hardware boundaries.
40404040

4041+
rcutree.rcu_min_cached_objs= [KNL]
4042+
Minimum number of objects which are cached and
4043+
maintained per one CPU. Object size is equal
4044+
to PAGE_SIZE. The cache allows to reduce the
4045+
pressure to page allocator, also it makes the
4046+
whole algorithm to behave better in low memory
4047+
condition.
4048+
40414049
rcutree.jiffies_till_first_fqs= [KNL]
40424050
Set delay from grace-period initialization to
40434051
first attempt to force quiescent states.

kernel/rcu/tree.c

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ module_param(gp_init_delay, int, 0444);
175175
static int gp_cleanup_delay;
176176
module_param(gp_cleanup_delay, int, 0444);
177177

178+
/*
179+
* This rcu parameter is runtime-read-only. It reflects
180+
* a minimum allowed number of objects which can be cached
181+
* per-CPU. Object size is equal to one page. This value
182+
* can be changed at boot time.
183+
*/
184+
static int rcu_min_cached_objs = 2;
185+
module_param(rcu_min_cached_objs, int, 0444);
186+
178187
/* Retrieve RCU kthreads priority for rcutorture */
179188
int rcu_get_gp_kthreads_prio(void)
180189
{
@@ -2997,7 +3006,6 @@ struct kfree_rcu_cpu_work {
29973006
* struct kfree_rcu_cpu - batch up kfree_rcu() requests for RCU grace period
29983007
* @head: List of kfree_rcu() objects not yet waiting for a grace period
29993008
* @bhead: Bulk-List of kfree_rcu() objects not yet waiting for a grace period
3000-
* @bcached: Keeps at most one object for later reuse when build chain blocks
30013009
* @krw_arr: Array of batches of kfree_rcu() objects waiting for a grace period
30023010
* @lock: Synchronize access to this structure
30033011
* @monitor_work: Promote @head to @head_free after KFREE_DRAIN_JIFFIES
@@ -3013,13 +3021,22 @@ struct kfree_rcu_cpu_work {
30133021
struct kfree_rcu_cpu {
30143022
struct rcu_head *head;
30153023
struct kfree_rcu_bulk_data *bhead;
3016-
struct kfree_rcu_bulk_data *bcached;
30173024
struct kfree_rcu_cpu_work krw_arr[KFREE_N_BATCHES];
30183025
raw_spinlock_t lock;
30193026
struct delayed_work monitor_work;
30203027
bool monitor_todo;
30213028
bool initialized;
30223029
int count;
3030+
3031+
/*
3032+
* A simple cache list that contains objects for
3033+
* reuse purpose. In order to save some per-cpu
3034+
* space the list is singular. Even though it is
3035+
* lockless an access has to be protected by the
3036+
* per-cpu lock.
3037+
*/
3038+
struct llist_head bkvcache;
3039+
int nr_bkv_objs;
30233040
};
30243041

30253042
static DEFINE_PER_CPU(struct kfree_rcu_cpu, krc) = {
@@ -3056,6 +3073,31 @@ krc_this_cpu_unlock(struct kfree_rcu_cpu *krcp, unsigned long flags)
30563073
local_irq_restore(flags);
30573074
}
30583075

3076+
static inline struct kfree_rcu_bulk_data *
3077+
get_cached_bnode(struct kfree_rcu_cpu *krcp)
3078+
{
3079+
if (!krcp->nr_bkv_objs)
3080+
return NULL;
3081+
3082+
krcp->nr_bkv_objs--;
3083+
return (struct kfree_rcu_bulk_data *)
3084+
llist_del_first(&krcp->bkvcache);
3085+
}
3086+
3087+
static inline bool
3088+
put_cached_bnode(struct kfree_rcu_cpu *krcp,
3089+
struct kfree_rcu_bulk_data *bnode)
3090+
{
3091+
// Check the limit.
3092+
if (krcp->nr_bkv_objs >= rcu_min_cached_objs)
3093+
return false;
3094+
3095+
llist_add((struct llist_node *) bnode, &krcp->bkvcache);
3096+
krcp->nr_bkv_objs++;
3097+
return true;
3098+
3099+
}
3100+
30593101
/*
30603102
* This function is invoked in workqueue context after a grace period.
30613103
* It frees all the objects queued on ->bhead_free or ->head_free.
@@ -3091,7 +3133,12 @@ static void kfree_rcu_work(struct work_struct *work)
30913133
kfree_bulk(bhead->nr_records, bhead->records);
30923134
rcu_lock_release(&rcu_callback_map);
30933135

3094-
if (cmpxchg(&krcp->bcached, NULL, bhead))
3136+
krcp = krc_this_cpu_lock(&flags);
3137+
if (put_cached_bnode(krcp, bhead))
3138+
bhead = NULL;
3139+
krc_this_cpu_unlock(krcp, flags);
3140+
3141+
if (bhead)
30953142
free_page((unsigned long) bhead);
30963143

30973144
cond_resched_tasks_rcu_qs();
@@ -3224,7 +3271,7 @@ kfree_call_rcu_add_ptr_to_bulk(struct kfree_rcu_cpu *krcp,
32243271
/* Check if a new block is required. */
32253272
if (!krcp->bhead ||
32263273
krcp->bhead->nr_records == KFREE_BULK_MAX_ENTR) {
3227-
bnode = xchg(&krcp->bcached, NULL);
3274+
bnode = get_cached_bnode(krcp);
32283275
if (!bnode) {
32293276
WARN_ON_ONCE(sizeof(struct kfree_rcu_bulk_data) > PAGE_SIZE);
32303277

@@ -4277,12 +4324,23 @@ static void __init kfree_rcu_batch_init(void)
42774324

42784325
for_each_possible_cpu(cpu) {
42794326
struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
4327+
struct kfree_rcu_bulk_data *bnode;
42804328

42814329
for (i = 0; i < KFREE_N_BATCHES; i++) {
42824330
INIT_RCU_WORK(&krcp->krw_arr[i].rcu_work, kfree_rcu_work);
42834331
krcp->krw_arr[i].krcp = krcp;
42844332
}
42854333

4334+
for (i = 0; i < rcu_min_cached_objs; i++) {
4335+
bnode = (struct kfree_rcu_bulk_data *)
4336+
__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
4337+
4338+
if (bnode)
4339+
put_cached_bnode(krcp, bnode);
4340+
else
4341+
pr_err("Failed to preallocate for %d CPU!\n", cpu);
4342+
}
4343+
42864344
INIT_DELAYED_WORK(&krcp->monitor_work, kfree_rcu_monitor);
42874345
krcp->initialized = true;
42884346
}

0 commit comments

Comments
 (0)