Skip to content

Commit 243418e

Browse files
minchanktorvalds
authored andcommitted
mm: fs: invalidate bh_lrus for only cold path
The kernel test robot reported the regression of fio.write_iops[1] with commit 8cc621d ("mm: fs: invalidate BH LRU during page migration"). Since lru_add_drain is called frequently, invalidate bh_lrus there could increase bh_lrus cache miss ratio, which needs more IO in the end. This patch moves the bh_lrus invalidation from the hot path( e.g., zap_page_range, pagevec_release) to cold path(i.e., lru_add_drain_all, lru_cache_disable). Zhengjun Xing confirmed "I test the patch, the regression reduced to -2.9%" [1] https://lore.kernel.org/lkml/20210520083144.GD14190@xsang-OptiPlex-9020/ [2] 8cc621d, mm: fs: invalidate BH LRU during page migration Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Minchan Kim <[email protected]> Reported-by: kernel test robot <[email protected]> Reviewed-by: Chris Goldsworthy <[email protected]> Tested-by: "Xing, Zhengjun" <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent b7cd9fa commit 243418e

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

fs/buffer.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,12 +1425,16 @@ void invalidate_bh_lrus(void)
14251425
}
14261426
EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
14271427

1428-
void invalidate_bh_lrus_cpu(int cpu)
1428+
/*
1429+
* It's called from workqueue context so we need a bh_lru_lock to close
1430+
* the race with preemption/irq.
1431+
*/
1432+
void invalidate_bh_lrus_cpu(void)
14291433
{
14301434
struct bh_lru *b;
14311435

14321436
bh_lru_lock();
1433-
b = per_cpu_ptr(&bh_lrus, cpu);
1437+
b = this_cpu_ptr(&bh_lrus);
14341438
__invalidate_bh_lrus(b);
14351439
bh_lru_unlock();
14361440
}

include/linux/buffer_head.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void __breadahead_gfp(struct block_device *, sector_t block, unsigned int size,
194194
struct buffer_head *__bread_gfp(struct block_device *,
195195
sector_t block, unsigned size, gfp_t gfp);
196196
void invalidate_bh_lrus(void);
197-
void invalidate_bh_lrus_cpu(int cpu);
197+
void invalidate_bh_lrus_cpu(void);
198198
bool has_bh_in_lru(int cpu, void *dummy);
199199
struct buffer_head *alloc_buffer_head(gfp_t gfp_flags);
200200
void free_buffer_head(struct buffer_head * bh);
@@ -408,7 +408,7 @@ static inline int inode_has_buffers(struct inode *inode) { return 0; }
408408
static inline void invalidate_inode_buffers(struct inode *inode) {}
409409
static inline int remove_inode_buffers(struct inode *inode) { return 1; }
410410
static inline int sync_mapping_buffers(struct address_space *mapping) { return 0; }
411-
static inline void invalidate_bh_lrus_cpu(int cpu) {}
411+
static inline void invalidate_bh_lrus_cpu(void) {}
412412
static inline bool has_bh_in_lru(int cpu, void *dummy) { return false; }
413413
#define buffer_heads_over_limit 0
414414

mm/swap.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,6 @@ void lru_add_drain_cpu(int cpu)
620620
pagevec_lru_move_fn(pvec, lru_lazyfree_fn);
621621

622622
activate_page_drain(cpu);
623-
invalidate_bh_lrus_cpu(cpu);
624623
}
625624

626625
/**
@@ -703,6 +702,20 @@ void lru_add_drain(void)
703702
local_unlock(&lru_pvecs.lock);
704703
}
705704

705+
/*
706+
* It's called from per-cpu workqueue context in SMP case so
707+
* lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
708+
* the same cpu. It shouldn't be a problem in !SMP case since
709+
* the core is only one and the locks will disable preemption.
710+
*/
711+
static void lru_add_and_bh_lrus_drain(void)
712+
{
713+
local_lock(&lru_pvecs.lock);
714+
lru_add_drain_cpu(smp_processor_id());
715+
local_unlock(&lru_pvecs.lock);
716+
invalidate_bh_lrus_cpu();
717+
}
718+
706719
void lru_add_drain_cpu_zone(struct zone *zone)
707720
{
708721
local_lock(&lru_pvecs.lock);
@@ -717,7 +730,7 @@ static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
717730

718731
static void lru_add_drain_per_cpu(struct work_struct *dummy)
719732
{
720-
lru_add_drain();
733+
lru_add_and_bh_lrus_drain();
721734
}
722735

723736
/*
@@ -858,7 +871,7 @@ void lru_cache_disable(void)
858871
*/
859872
__lru_add_drain_all(true);
860873
#else
861-
lru_add_drain();
874+
lru_add_and_bh_lrus_drain();
862875
#endif
863876
}
864877

0 commit comments

Comments
 (0)