Commit 574907741599 ("mm/page_alloc: leave IRQs enabled for per-cpu page allocations") stopped disabling IRQs under the pcp lock. A softirq that interrupts the holder always fails pcp_spin_trylock() and falls back to taking zone->lock for each page, since the pcp lock is still held by the interrupted process. This is common when zone->lock is contended. free_pcppages_bulk() spins on it with IRQs disabled, so an interrupt is often pending when it unlocks, and happens with the pcp lock still held. Restore IRQs only after dropping the pcp lock, and return with the pcp unlocked. PREEMPT_RT keeps IRQs enabled. With zone->lock contended in a 128-vCPU guest, tlb_remove_table_rcu() callbacks hitting the fallback went from 93% to 7%, and the average callback duration dropped from 14ms to 1.5ms. Throughput was unchanged. Signed-off-by: Joshua Hahn --- mm/page_alloc.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index aa1ed599c7c6b..bc272f8d8ec75 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -141,6 +141,18 @@ static DEFINE_MUTEX(pcp_batch_high_lock); pcpu_task_unpin(); \ }) +/* + * Like pcp_spin_unlock(), for callers that disabled IRQs (except on PREEMPT_RT) + * before taking zone->lock. An interrupt that arrives in between runs only once + * the pcp lock is dropped, so its softirqs can use the pcp lists. Disable IRQs + * after taking the pcp lock as other holders may be waiting for an IPI. + */ +#define pcp_spin_unlock_irqrestore(ptr, flags) \ +({ \ + spin_unlock_irqrestore(&(ptr)->lock, flags); \ + pcpu_task_unpin(); \ +}) + /* * On CONFIG_SMP=n the UP implementation of spin_trylock() never fails and thus * is not compatible with our locking scheme. However we do not need pcp for @@ -153,6 +165,9 @@ static DEFINE_MUTEX(pcp_batch_high_lock); #define pcp_spin_unlock(ptr) \ BUG_ON(1) + +#define pcp_spin_unlock_irqrestore(ptr, flags) \ + BUG_ON(1) #endif /* @@ -2846,8 +2861,8 @@ static int nr_pcp_high(struct per_cpu_pages *pcp, struct zone *zone, * Tune pcp alloc factor and adjust count & free_count. Free pages to bring the * pcp's watermarks below high. * - * May return a freed pcp, if during page freeing the pcp spinlock cannot be - * reacquired. Return true if pcp is locked, false otherwise. + * May return with the pcp unlocked after freeing pages to the buddy allocator. + * Return true if pcp is locked, false otherwise. */ static bool free_frozen_page_commit(struct zone *zone, struct per_cpu_pages *pcp, struct page *page, int migratetype, @@ -2904,14 +2919,19 @@ static bool free_frozen_page_commit(struct zone *zone, to_free = nr_pcp_free(pcp, batch, high, free_high); while (to_free > 0 && pcp->count > 0) { + unsigned long flags = 0; + to_free_batched = min(to_free, batch); + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) + local_irq_save(flags); free_pcppages_bulk(zone, to_free_batched, pcp, pindex); to_free -= to_free_batched; + pcp_spin_unlock_irqrestore(pcp, flags); - if (to_free == 0 || pcp->count == 0) + if (!to_free) { + ret = false; break; - - pcp_spin_unlock(pcp); + } pcp = pcp_spin_trylock(zone->per_cpu_pageset); if (!pcp) { -- 2.53.1-Meta