Skip to content

Commit 9b81af9

Browse files
urezkigregkh
authored andcommitted
rcu/tree: Defer kvfree_rcu() allocation to a clean context
[ Upstream commit 56292e8 ] The current memmory-allocation interface causes the following difficulties for kvfree_rcu(): a) If built with CONFIG_PROVE_RAW_LOCK_NESTING, the lockdep will complain about violation of the nesting rules, as in "BUG: Invalid wait context". This Kconfig option checks for proper raw_spinlock vs. spinlock nesting, in particular, it is not legal to acquire a spinlock_t while holding a raw_spinlock_t. This is a problem because kfree_rcu() uses raw_spinlock_t whereas the "page allocator" internally deals with spinlock_t to access to its zones. The code also can be broken from higher level of view: <snip> raw_spin_lock(&some_lock); kfree_rcu(some_pointer, some_field_offset); <snip> b) If built with CONFIG_PREEMPT_RT, spinlock_t is converted into sleeplock. This means that invoking the page allocator from atomic contexts results in "BUG: scheduling while atomic". c) Please note that call_rcu() is already invoked from raw atomic context, so it is only reasonable to expaect that kfree_rcu() and kvfree_rcu() will also be called from atomic raw context. This commit therefore defers page allocation to a clean context using the combination of an hrtimer and a workqueue. The hrtimer stage is required in order to avoid deadlocks with the scheduler. This deferred allocation is required only when kvfree_rcu()'s per-CPU page cache is empty. Link: https://lore.kernel.org/lkml/20200630164543.4mdcf6zb4zfclhln@linutronix.de/ Fixes: 3042f83 ("rcu: Support reclaim for head-less object") Reported-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 5cacd18 commit 9b81af9

1 file changed

Lines changed: 66 additions & 43 deletions

File tree

‎kernel/rcu/tree.c‎

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ module_param(rcu_unlock_delay, int, 0444);
177177
* per-CPU. Object size is equal to one page. This value
178178
* can be changed at boot time.
179179
*/
180-
static int rcu_min_cached_objs = 2;
180+
static int rcu_min_cached_objs = 5;
181181
module_param(rcu_min_cached_objs, int, 0444);
182182

183183
/* Retrieve RCU kthreads priority for rcutorture */
@@ -3087,6 +3087,9 @@ struct kfree_rcu_cpu_work {
30873087
* In order to save some per-cpu space the list is singular.
30883088
* Even though it is lockless an access has to be protected by the
30893089
* per-cpu lock.
3090+
* @page_cache_work: A work to refill the cache when it is empty
3091+
* @work_in_progress: Indicates that page_cache_work is running
3092+
* @hrtimer: A hrtimer for scheduling a page_cache_work
30903093
* @nr_bkv_objs: number of allocated objects at @bkvcache.
30913094
*
30923095
* This is a per-CPU structure. The reason that it is not included in
@@ -3103,6 +3106,11 @@ struct kfree_rcu_cpu {
31033106
bool monitor_todo;
31043107
bool initialized;
31053108
int count;
3109+
3110+
struct work_struct page_cache_work;
3111+
atomic_t work_in_progress;
3112+
struct hrtimer hrtimer;
3113+
31063114
struct llist_head bkvcache;
31073115
int nr_bkv_objs;
31083116
};
@@ -3220,10 +3228,10 @@ static void kfree_rcu_work(struct work_struct *work)
32203228
}
32213229
rcu_lock_release(&rcu_callback_map);
32223230

3223-
krcp = krc_this_cpu_lock(&flags);
3231+
raw_spin_lock_irqsave(&krcp->lock, flags);
32243232
if (put_cached_bnode(krcp, bkvhead[i]))
32253233
bkvhead[i] = NULL;
3226-
krc_this_cpu_unlock(krcp, flags);
3234+
raw_spin_unlock_irqrestore(&krcp->lock, flags);
32273235

32283236
if (bkvhead[i])
32293237
free_page((unsigned long) bkvhead[i]);
@@ -3350,6 +3358,57 @@ static void kfree_rcu_monitor(struct work_struct *work)
33503358
raw_spin_unlock_irqrestore(&krcp->lock, flags);
33513359
}
33523360

3361+
static enum hrtimer_restart
3362+
schedule_page_work_fn(struct hrtimer *t)
3363+
{
3364+
struct kfree_rcu_cpu *krcp =
3365+
container_of(t, struct kfree_rcu_cpu, hrtimer);
3366+
3367+
queue_work(system_highpri_wq, &krcp->page_cache_work);
3368+
return HRTIMER_NORESTART;
3369+
}
3370+
3371+
static void fill_page_cache_func(struct work_struct *work)
3372+
{
3373+
struct kvfree_rcu_bulk_data *bnode;
3374+
struct kfree_rcu_cpu *krcp =
3375+
container_of(work, struct kfree_rcu_cpu,
3376+
page_cache_work);
3377+
unsigned long flags;
3378+
bool pushed;
3379+
int i;
3380+
3381+
for (i = 0; i < rcu_min_cached_objs; i++) {
3382+
bnode = (struct kvfree_rcu_bulk_data *)
3383+
__get_free_page(GFP_KERNEL | __GFP_NOWARN);
3384+
3385+
if (bnode) {
3386+
raw_spin_lock_irqsave(&krcp->lock, flags);
3387+
pushed = put_cached_bnode(krcp, bnode);
3388+
raw_spin_unlock_irqrestore(&krcp->lock, flags);
3389+
3390+
if (!pushed) {
3391+
free_page((unsigned long) bnode);
3392+
break;
3393+
}
3394+
}
3395+
}
3396+
3397+
atomic_set(&krcp->work_in_progress, 0);
3398+
}
3399+
3400+
static void
3401+
run_page_cache_worker(struct kfree_rcu_cpu *krcp)
3402+
{
3403+
if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING &&
3404+
!atomic_xchg(&krcp->work_in_progress, 1)) {
3405+
hrtimer_init(&krcp->hrtimer, CLOCK_MONOTONIC,
3406+
HRTIMER_MODE_REL);
3407+
krcp->hrtimer.function = schedule_page_work_fn;
3408+
hrtimer_start(&krcp->hrtimer, 0, HRTIMER_MODE_REL);
3409+
}
3410+
}
3411+
33533412
static inline bool
33543413
kvfree_call_rcu_add_ptr_to_bulk(struct kfree_rcu_cpu *krcp, void *ptr)
33553414
{
@@ -3366,32 +3425,8 @@ kvfree_call_rcu_add_ptr_to_bulk(struct kfree_rcu_cpu *krcp, void *ptr)
33663425
if (!krcp->bkvhead[idx] ||
33673426
krcp->bkvhead[idx]->nr_records == KVFREE_BULK_MAX_ENTR) {
33683427
bnode = get_cached_bnode(krcp);
3369-
if (!bnode) {
3370-
/*
3371-
* To keep this path working on raw non-preemptible
3372-
* sections, prevent the optional entry into the
3373-
* allocator as it uses sleeping locks. In fact, even
3374-
* if the caller of kfree_rcu() is preemptible, this
3375-
* path still is not, as krcp->lock is a raw spinlock.
3376-
* With additional page pre-allocation in the works,
3377-
* hitting this return is going to be much less likely.
3378-
*/
3379-
if (IS_ENABLED(CONFIG_PREEMPT_RT))
3380-
return false;
3381-
3382-
/*
3383-
* NOTE: For one argument of kvfree_rcu() we can
3384-
* drop the lock and get the page in sleepable
3385-
* context. That would allow to maintain an array
3386-
* for the CONFIG_PREEMPT_RT as well if no cached
3387-
* pages are available.
3388-
*/
3389-
bnode = (struct kvfree_rcu_bulk_data *)
3390-
__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
3391-
}
3392-
33933428
/* Switch to emergency path. */
3394-
if (unlikely(!bnode))
3429+
if (!bnode)
33953430
return false;
33963431

33973432
/* Initialize the new block. */
@@ -3455,12 +3490,10 @@ void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
34553490
goto unlock_return;
34563491
}
34573492

3458-
/*
3459-
* Under high memory pressure GFP_NOWAIT can fail,
3460-
* in that case the emergency path is maintained.
3461-
*/
34623493
success = kvfree_call_rcu_add_ptr_to_bulk(krcp, ptr);
34633494
if (!success) {
3495+
run_page_cache_worker(krcp);
3496+
34643497
if (head == NULL)
34653498
// Inline if kvfree_rcu(one_arg) call.
34663499
goto unlock_return;
@@ -4452,24 +4485,14 @@ static void __init kfree_rcu_batch_init(void)
44524485

44534486
for_each_possible_cpu(cpu) {
44544487
struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
4455-
struct kvfree_rcu_bulk_data *bnode;
44564488

44574489
for (i = 0; i < KFREE_N_BATCHES; i++) {
44584490
INIT_RCU_WORK(&krcp->krw_arr[i].rcu_work, kfree_rcu_work);
44594491
krcp->krw_arr[i].krcp = krcp;
44604492
}
44614493

4462-
for (i = 0; i < rcu_min_cached_objs; i++) {
4463-
bnode = (struct kvfree_rcu_bulk_data *)
4464-
__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
4465-
4466-
if (bnode)
4467-
put_cached_bnode(krcp, bnode);
4468-
else
4469-
pr_err("Failed to preallocate for %d CPU!\n", cpu);
4470-
}
4471-
44724494
INIT_DELAYED_WORK(&krcp->monitor_work, kfree_rcu_monitor);
4495+
INIT_WORK(&krcp->page_cache_work, fill_page_cache_func);
44734496
krcp->initialized = true;
44744497
}
44754498
if (register_shrinker(&kfree_rcu_shrinker))

0 commit comments

Comments
 (0)