[PATCH v2 3/5] mm/swap: move bh draining into a separate workqueue

Marcelo Tosatti
2026-03-02T15:53:30Z

Separate the bh draining into a separate workqueue
(from the mm lru draining), so that its possible to switch
the mm lru draining to QPW.

To switch bh draining to QPW, it would be necessary to add
a spinlock to addition of bhs to percpu cache, and that is a
very hot path.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
---
mm/swap.c | 52 +++++++++++++++++++++++++++++++++++++---------------
1 file changed, 37 insertions(+), 15 deletions(-)

Index: linux/mm/swap.c
===================================================================
--- linux.orig/mm/swap.c
+++ linux/mm/swap.c
@@ -745,12 +745,11 @@ void lru_add_drain(void)
* the same cpu. It shouldn't be a problem in !SMP case since
* the core is only one and the locks will disable preemption.
*/
-static void lru_add_and_bh_lrus_drain(void)
+static void lru_add_mm_drain(void)
{
local_lock(&cpu_fbatches.lock);
lru_add_drain_cpu(smp_processor_id());
local_unlock(&cpu_fbatches.lock);
- invalidate_bh_lrus_cpu();
mlock_drain_local();
}

@@ -769,10 +768,17 @@ static DEFINE_PER_CPU(struct work_struct

static void lru_add_drain_per_cpu(struct work_struct *dummy)
{
- lru_add_and_bh_lrus_drain();
+ lru_add_mm_drain();
}

-static bool cpu_needs_drain(unsigned int cpu)
+static DEFINE_PER_CPU(struct work_struct, bh_add_drain_work);
+
+static void bh_add_drain_per_cpu(struct work_struct *dummy)
+{
+ invalidate_bh_lrus_cpu();
+}
+
+static bool cpu_needs_mm_drain(unsigned int cpu)
{
struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu);

@@ -783,8 +789,12 @@ static bool cpu_needs_drain(unsigned int
folio_batch_count(&fbatches->lru_deactivate) ||
folio_batch_count(&fbatches->lru_lazyfree) ||
folio_batch_count(&fbatches->lru_activate) ||
- need_mlock_drain(cpu) ||
- has_bh_in_lru(cpu, NULL);
+ need_mlock_drain(cpu);
+}
+
+static bool cpu_needs_bh_drain(unsigned int cpu)
+{
+ return has_bh_in_lru(cpu, NULL);
}

/*
@@ -807,7 +817,7 @@ static inline void __lru_add_drain_all(b
* each CPU.
*/
static unsigned int lru_drain_gen;
- static struct cpumask has_work;
+ static struct cpumask has_mm_work, has_bh_work;
static DEFINE_MUTEX(lock);
unsigned cpu, this_gen;

@@ -870,20 +880,31 @@ static inline void __lru_add_drain_all(b
WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
smp_mb();

- cpumask_clear(&has_work);
+ cpumask_clear(&has_mm_work);
+ cpumask_clear(&has_bh_work);
for_each_online_cpu(cpu) {
- struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
+ struct work_struct *mm_work = &per_cpu(lru_add_drain_work, cpu);
+ struct work_struct *bh_work = &per_cpu(bh_add_drain_work, cpu);
+
+ if (cpu_needs_mm_drain(cpu)) {
+ INIT_WORK(mm_work, lru_add_drain_per_cpu);
+ queue_work_on(cpu, mm_percpu_wq, mm_work);
+ __cpumask_set_cpu(cpu, &has_mm_work);
+ }

- if (cpu_needs_drain(cpu)) {
- INIT_WORK(work, lru_add_drain_per_cpu);
- queue_work_on(cpu, mm_percpu_wq, work);
- __cpumask_set_cpu(cpu, &has_work);
+ if (cpu_needs_bh_drain(cpu)) {
+ INIT_WORK(bh_work, bh_add_drain_per_cpu);
+ queue_work_on(cpu, mm_percpu_wq, bh_work);
+ __cpumask_set_cpu(cpu, &has_bh_work);
}
}

- for_each_cpu(cpu, &has_work)
+ for_each_cpu(cpu, &has_mm_work)
flush_work(&per_cpu(lru_add_drain_work, cpu));

+ for_each_cpu(cpu, &has_bh_work)
+ flush_work(&per_cpu(bh_add_drain_work, cpu));
+
done:
mutex_unlock(&lock);
}
@@ -929,7 +950,8 @@ void lru_cache_disable(void)
#ifdef CONFIG_SMP
__lru_add_drain_all(true);
#else
- lru_add_and_bh_lrus_drain();
+ lru_add_mm_drain();
+ invalidate_bh_lrus_cpu();
#endif
}