how about this?
Subsystems (5)
cover how about this?
This is combines basically the folio approach to serialize
along with doing that hack idea in the comment for the __find_get_block() shit. Then by adding the flag we can come up with a nifty locking scheme that satisfies both these issues as well as potential performance improvement by avoiding the i_private_lock.
What do you think?
Compile tested only :D
Davidlohr Bueso (3):
fs/buffer: rely on the folio lock for lookups when possible mm/migrate: fix blocking in atomic for large folios and bh fs/buffer: introduce __find_get_block_nonatomic()
fs/buffer.c | 76 +++++++++++++++++++++++++++---------- fs/jbd2/revoke.c | 8 ++-- fs/ocfs2/journal.c | 2 +- include/linux/buffer_head.h | 3 ++ mm/migrate.c | 8 +++- 5 files changed, 71 insertions(+), 26 deletions(-)
Signature · 1 line
2.39.5
PATCH 1/3 fs/buffer: rely on the folio lock for lookups when possible
Callers of __find_get_block() may or may not allow for blocking semantics, and is currently assumed that it will not. Layout two paths for which ultimately the i_private_lock scheme will be used as a fallback. The suggested trylock idea is implemented.
No change in semantics.
--- fs/buffer.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-)
fs/buffer.c +29 -16
@@ -176,18 +176,8 @@ void end_buffer_write_sync(struct buffer_head *bh, int uptodate) }EXPORT_SYMBOL (end_buffer_write_sync); -/* -* Various filesystems appear to want __find_get_block to be non-blocking. -* But it's the page lock which protects the buffers. To get around this, -* we get exclusion from try_to_free_buffers with the blockdev mapping's -* i_private_lock. -* -* Hack idea: for the blockdev mapping, i_private_lock contention -* may be quite high. This code could TryLock the page, and if that -* succeeds, there is no need to take i_private_lock. -*/ static struct buffer_head * -__find_get_block_slow (struct block_device *bdev,sector_t block) +__find_get_block_slow (struct block_device *bdev,sector_t block,bool atomic) {struct address_space *bd_mapping = bdev->bd_mapping;const int blkbits = bd_mapping->host->i_blkbits;
@@ -197,6 +187,7 @@ __find_get_block_slow(struct block_device *bdev, sector_t block)struct buffer_head *head;struct folio *folio;int all_mapped =1 ; +bool folio_locked = true; static DEFINE_RATELIMIT_STATE(last_warned, HZ, 1); index = ((loff_t )block << blkbits) /PAGE_SIZE ;
@@ -204,7 +195,19 @@ __find_get_block_slow(struct block_device *bdev, sector_t block)if (IS_ERR (folio)) goto out; -spin_lock (&bd_mapping->i_private_lock); +/* +* Folio lock protects the buffers. Callers that cannot block +* will fallback to serializing vs try_to_free_buffers() via +* the i_private_lock. +*/ +if (!folio_trylock (folio)) { +if (atomic) { +spin_lock (&bd_mapping->i_private_lock); + folio_locked = false; + }else +folio_lock (folio); + } + head =folio_buffers (folio);if (!head) goto out_unlock;
@@ -236,7 +239,10 @@ __find_get_block_slow(struct block_device *bdev, sector_t block) 1 << blkbits); } out_unlock: -spin_unlock (&bd_mapping->i_private_lock); +if (folio_locked) +folio_unlock (folio); +else +spin_unlock (&bd_mapping->i_private_lock);folio_put (folio); out:return ret;
@@ -1388,14 +1394,15 @@ lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)* it in the LRU and mark it as accessed. If it is not present then return * NULL */ -struct buffer_head * -__find_get_block (struct block_device *bdev,sector_t block,unsigned size) +static struct buffer_head * +__find_get_block_common (struct block_device *bdev,sector_t block, +unsigned size,bool atomic) {struct buffer_head *bh =lookup_bh_lru (bdev, block, size);if (bh ==NULL ) {/* __find_get_block_slow will mark the page accessed */ - bh =__find_get_block_slow (bdev, block); + bh =__find_get_block_slow (bdev, block, atomic);if (bh)bh_lru_install (bh); }else
@@ -1403,6 +1410,12 @@ __find_get_block(struct block_device *bdev, sector_t block, unsigned size)return bh; } + +struct buffer_head * +__find_get_block (struct block_device *bdev,sector_t block,unsigned size) +{ +return __find_get_block_common (bdev, block, size, true); +}EXPORT_SYMBOL (__find_get_block);/**
Signature · 1 line
2.39.5
PATCH 2/3 mm/migrate: fix blocking in atomic for large folios and bh
In reply to Davidlohr BuesoThe large folio + buffer head noref migration scenarios have been caught being naughty and blocking while holding a spinlock.
Fix this by replacing the scope of the current i_private_lock in the buffer migration with the folio lock, and reducing the spinlock's critical region to only checking the count. Further a new BH_Migrate flag is added to serialize entirely across __buffer_migrate_folio().
Currently there are no blocking users of pagecache lookups.
--- fs/buffer.c | 6 ++++++ include/linux/buffer_head.h | 1 + mm/migrate.c | 8 ++++++-- 3 files changed, 13 insertions(+), 2 deletions(-)
fs/buffer.c +6 -0
@@ -211,6 +211,12 @@ __find_get_block_slow(struct block_device *bdev, sector_t block, bool atomic) head =folio_buffers (folio);if (!head) goto out_unlock; +/* +* Upon a noref migration, the folio lock serialize here; +* otherwise bail. +*/ +if (!folio_locked &&test_bit (BH_Migrate, &head->b_state)) + goto out_unlock; bh = head; do { if (!buffer_mapped(bh))
include/linux/buffer_head.h +1 -0
@@ -34,6 +34,7 @@ enum bh_state_bits { BH_Meta,/* Buffer contains metadata */ BH_Prio,/* Buffer should be submitted with REQ_PRIO */ BH_Defer_Completion,/* Defer AIO completion to workqueue */ + BH_Migrate,/* Buffer is being migrated */ BH_PrivateStart,/* not a state bit, but the first bit available* for private allocation by other entities
mm/migrate.c +6 -2
@@ -844,10 +844,13 @@ static int __buffer_migrate_folio(struct address_space *mapping,if (!buffer_migrate_lock_buffers (head, mode))return -EAGAIN ; +set_bit (BH_Migrate, &head->b_state); +if (check_refs) {bool busy;bool invalidated = false; +folio_lock (src); recheck_buffers: busy = false;spin_lock (&mapping->i_private_lock);
@@ -859,12 +862,12 @@ static int __buffer_migrate_folio(struct address_space *mapping, } bh = bh->b_this_page; }while (bh != head); +spin_unlock (&mapping->i_private_lock);if (busy) {if (invalidated) { rc = -EAGAIN ; goto unlock_buffers; } -spin_unlock (&mapping->i_private_lock);invalidate_bh_lrus (); invalidated = true; goto recheck_buffers;
@@ -882,8 +885,9 @@ static int __buffer_migrate_folio(struct address_space *mapping, }while (bh != head); unlock_buffers: +clear_bit (BH_Migrate, &head->b_state);if (check_refs) -spin_unlock (&mapping->i_private_lock); +folio_unlock (src); bh = head; do { unlock_buffer(bh);
Signature · 1 line
2.39.5
PATCH 3/3 fs/buffer: introduce __find_get_block_nonatomic()
In reply to Davidlohr BuesoCallers that do not require atomic context for pagecache lookups can use this flavor to avoid the unnencesary contention on the blockdev mapping i_private_lock as well as waiting on noref migration.
Sprinkle some conversions. This could be separated into different patches I suppose.
--- fs/buffer.c | 25 +++++++++++++++++++++---- fs/jbd2/revoke.c | 8 +++++--- fs/ocfs2/journal.c | 2 +- include/linux/buffer_head.h | 2 ++ 4 files changed, 29 insertions(+), 8 deletions(-)
fs/buffer.c +21 -4
@@ -668,7 +668,8 @@ EXPORT_SYMBOL(generic_buffers_fsync);void write_boundary_block (struct block_device *bdev,sector_t bblock,unsigned blocksize) { -struct buffer_head *bh =__find_get_block (bdev, bblock +1 , blocksize); +struct buffer_head *bh =__find_get_block_nonatomic (bdev, bblock +1 , + blocksize);if (bh) {if (buffer_dirty (bh))write_dirty_buffer (bh,0 );
@@ -1135,8 +1136,10 @@ __getblk_slow(struct block_device *bdev, sector_t block,for (;;) {struct buffer_head *bh; - - bh =__find_get_block (bdev, block, size); +if (gfpflags_allow_blocking (gfp)) + bh =__find_get_block_nonatomic (bdev, block, size); +else + bh =__find_get_block (bdev, block, size);if (bh)return bh;
@@ -1424,6 +1427,15 @@ __find_get_block(struct block_device *bdev, sector_t block, unsigned size) }EXPORT_SYMBOL (__find_get_block); +/* same as __find_get_block() but can sleep */ +struct buffer_head * +__find_get_block_nonatomic (struct block_device *bdev,sector_t block, +unsigned size) +{ +return __find_get_block_common (bdev, block, size, false); +} +EXPORT_SYMBOL (__find_get_block_nonatomic); +/** * bdev_getblk - Get a buffer_head in a block device's buffer cache. * @bdev: The block device.
@@ -1441,7 +1453,12 @@ EXPORT_SYMBOL(__find_get_block);struct buffer_head *bdev_getblk (struct block_device *bdev,sector_t block,unsigned size,gfp_t gfp) { -struct buffer_head *bh =__find_get_block (bdev, block, size); +struct buffer_head *bh; + +if (gfpflags_allow_blocking (gfp)) + bh =__find_get_block_nonatomic (bdev, block, size); +else + bh =__find_get_block (bdev, block, size);might_alloc (gfp);if (bh)
fs/jbd2/revoke.c +5 -3
@@ -345,7 +345,8 @@ int jbd2_journal_revoke(handle_t *handle, unsigned long long blocknr, bh = bh_in;if (!bh) { - bh =__find_get_block (bdev, blocknr, journal->j_blocksize); + bh =__find_get_block_nonatomic (bdev, blocknr, + journal->j_blocksize);if (bh)BUFFER_TRACE (bh,"found on hash" ); }
@@ -464,7 +465,8 @@ void jbd2_journal_cancel_revoke(handle_t *handle, struct journal_head *jh)* state machine will get very upset later on. */ if (need_cancel) {struct buffer_head *bh2; - bh2 =__find_get_block (bh->b_bdev, bh->b_blocknr, bh->b_size); + bh2 =__find_get_block_nonatomic (bh->b_bdev, bh->b_blocknr, + bh->b_size);if (bh2) {if (bh2 != bh)clear_buffer_revoked (bh2);
@@ -492,7 +494,7 @@ void jbd2_clear_buffer_revoked_flags(journal_t *journal)struct jbd2_revoke_record_s *record;struct buffer_head *bh; record = (struct jbd2_revoke_record_s *)list_entry; - bh =__find_get_block (journal->j_fs_dev, + bh =__find_get_block_nonatomic (journal->j_fs_dev, record->blocknr, journal->j_blocksize);if (bh) {
fs/ocfs2/journal.c +1 -1
@@ -1249,7 +1249,7 @@ static int ocfs2_force_read_journal(struct inode *inode) }for (i =0 ; i < p_blocks; i++, p_blkno++) { - bh =__find_get_block (osb->sb->s_bdev, p_blkno, + bh =__find_get_block_nonatomic (osb->sb->s_bdev, p_blkno, osb->sb->s_blocksize);/* block not cached. */ if (!bh)
include/linux/buffer_head.h +2 -0
@@ -223,6 +223,8 @@ void __wait_on_buffer(struct buffer_head *);wait_queue_head_t *bh_waitq_head (struct buffer_head *bh);struct buffer_head *__find_get_block (struct block_device *bdev,sector_t block,unsigned size); +struct buffer_head *__find_get_block_nonatomic (struct block_device *bdev, +sector_t block,unsigned size);struct buffer_head *bdev_getblk (struct block_device *bdev,sector_t block,unsigned size,gfp_t gfp);void __brelse (struct buffer_head *);
Signature · 1 line
2.39.5