xfs: remove xfs_buf_cache.bc_lock
PATCH 1/1 xfs: remove xfs_buf_cache.bc_lock
xfs_buf_cache.bc_lock serializes adding buffers to and removing them from the hashtable. But as the rhashtable code already uses fine grained internal locking for inserts and removals the extra protection isn't actually required.
It also happens to fix a lock order inversion vs b_lock added by the recent lookup race fix.
--- fs/xfs/xfs_buf.c | 20 ++++++++------------ fs/xfs/xfs_buf.h | 1 - 2 files changed, 8 insertions(+), 13 deletions(-)
fs/xfs/xfs_buf.c +8 -12
@@ -41,8 +41,7 @@ struct kmem_cache *xfs_buf_cache;* * xfs_buf_rele: * b_lock -* pag_buf_lock -* lru_lock +* lru_lock * * xfs_buftarg_drain_rele * lru_lock
@@ -502,7 +501,6 @@ intxfs_buf_cache_init ( struct xfs_buf_cache *bch) { -spin_lock_init (&bch->bc_lock);return rhashtable_init (&bch->bc_hash, &xfs_buf_hash_params); }
@@ -652,17 +650,20 @@ xfs_buf_find_insert(if (error) goto out_free_buf; -spin_lock (&bch->bc_lock); +/* The new buffer keeps the perag reference until it is freed. */ + new_bp->b_pag = pag; + +rcu_read_lock (); bp =rhashtable_lookup_get_insert_fast (&bch->bc_hash, &new_bp->b_rhash_head, xfs_buf_hash_params);if (IS_ERR (bp)) { +rcu_read_unlock (); error =PTR_ERR (bp); -spin_unlock (&bch->bc_lock); goto out_free_buf; }if (bp &&xfs_buf_try_hold (bp)) {/* found an existing buffer */ -spin_unlock (&bch->bc_lock); +rcu_read_unlock (); error =xfs_buf_find_lock (bp, flags);if (error)xfs_buf_rele (bp);
@@ -670,10 +671,8 @@ xfs_buf_find_insert( *bpp = bp; goto out_free_buf; } +rcu_read_unlock (); -/* The new buffer keeps the perag reference until it is freed. */ - new_bp->b_pag = pag; -spin_unlock (&bch->bc_lock); *bpp = new_bp;return 0 ;
@@ -1090,7 +1089,6 @@ xfs_buf_rele_cached( }/* we are asked to drop the last reference */ -spin_lock (&bch->bc_lock);__xfs_buf_ioacct_dec (bp);if (!(bp->b_flags &XBF_STALE ) &&atomic_read (&bp->b_lru_ref)) {/*
@@ -1102,7 +1100,6 @@ xfs_buf_rele_cached( bp->b_state &= ~XFS_BSTATE_DISPOSE ; else bp->b_hold--; -spin_unlock (&bch->bc_lock); }else { bp->b_hold--;/*
@@ -1120,7 +1117,6 @@ xfs_buf_rele_cached(ASSERT (!(bp->b_flags & _XBF_DELWRI_Q));rhashtable_remove_fast (&bch->bc_hash, &bp->b_rhash_head, xfs_buf_hash_params); -spin_unlock (&bch->bc_lock);if (pag)xfs_perag_put (pag); freebuf = true;
fs/xfs/xfs_buf.h +0 -1
@@ -80,7 +80,6 @@ typedef unsigned int xfs_buf_flags_t;#define XFS_BSTATE_IN_FLIGHT (1 << 1)/* I/O in flight */ struct xfs_buf_cache { -spinlock_t bc_lock;struct rhashtable bc_hash; };
Signature · 1 line
2.45.2
Quoted 11 lines · Christoph Hellwig
xfs_buf_cache.bc_lock serializes adding buffers to and removing them from the hashtable. But as the rhashtable code already uses fine grained internal locking for inserts and removals the extra protection isn't actually required.
It also happens to fix a lock order inversion vs
b_lockadded by the recent lookup race fix.Fixes ee10f6fcdb96 ("xfs: fix buffer lookup vs release race")Reported-by "Lai, Yi" <[email protected]>Signed-off-by Christoph Hellwig <[email protected]>
Looks good,
Quoted 102 lines
--- fs/xfs/xfs_buf.c | 20 ++++++++------------ fs/xfs/xfs_buf.h | 1 - 2 files changed, 8 insertions(+), 13 deletions(-)fs/xfs/xfs_buf.c +8
-12@@ -41,8 +41,7 @@ struct kmem_cache *xfs_buf_cache;* * xfs_buf_rele: * b_lock -* pag_buf_lock -* lru_lock +* lru_lock * * xfs_buftarg_drain_rele * lru_lock @@ -502,7 +501,6 @@ intxfs_buf_cache_init ( struct xfs_buf_cache *bch) { -spin_lock_init (&bch->bc_lock);return rhashtable_init (&bch->bc_hash, &xfs_buf_hash_params); }@@ -652,17 +650,20 @@ xfs_buf_find_insert(if (error) goto out_free_buf; -spin_lock (&bch->bc_lock); +/* The new buffer keeps the perag reference until it is freed. */ + new_bp->b_pag = pag; + +rcu_read_lock (); bp =rhashtable_lookup_get_insert_fast (&bch->bc_hash, &new_bp->b_rhash_head, xfs_buf_hash_params);if (IS_ERR (bp)) { +rcu_read_unlock (); error =PTR_ERR (bp); -spin_unlock (&bch->bc_lock); goto out_free_buf; }if (bp &&xfs_buf_try_hold (bp)) {/* found an existing buffer */ -spin_unlock (&bch->bc_lock); +rcu_read_unlock (); error =xfs_buf_find_lock (bp, flags);if (error)xfs_buf_rele (bp);@@ -670,10 +671,8 @@ xfs_buf_find_insert( *bpp = bp; goto out_free_buf; } +rcu_read_unlock (); -/* The new buffer keeps the perag reference until it is freed. */ - new_bp->b_pag = pag; -spin_unlock (&bch->bc_lock); *bpp = new_bp;return 0 ;@@ -1090,7 +1089,6 @@ xfs_buf_rele_cached( }/* we are asked to drop the last reference */ -spin_lock (&bch->bc_lock);__xfs_buf_ioacct_dec (bp);if (!(bp->b_flags &XBF_STALE ) &&atomic_read (&bp->b_lru_ref)) {/* @@ -1102,7 +1100,6 @@ xfs_buf_rele_cached( bp->b_state &= ~XFS_BSTATE_DISPOSE ; else bp->b_hold--; -spin_unlock (&bch->bc_lock); }else { bp->b_hold--;/* @@ -1120,7 +1117,6 @@ xfs_buf_rele_cached(ASSERT (!(bp->b_flags & _XBF_DELWRI_Q));rhashtable_remove_fast (&bch->bc_hash, &bp->b_rhash_head, xfs_buf_hash_params); -spin_unlock (&bch->bc_lock);if (pag)xfs_perag_put (pag); freebuf = true;fs/xfs/xfs_buf.h +0
-2@@ -80,7 +80,6 @@ typedef unsigned int xfs_buf_flags_t;#define XFS_BSTATE_IN_FLIGHT (1 << 1)/* I/O in flight */ struct xfs_buf_cache { -spinlock_t bc_lock;struct rhashtable bc_hash; }; --2.45.2
Quoted 52 lines · Christoph Hellwig
xfs_buf_cache.bc_lock serializes adding buffers to and removing them from the hashtable. But as the rhashtable code already uses fine grained internal locking for inserts and removals the extra protection isn't actually required.
It also happens to fix a lock order inversion vs
b_lockadded by the recent lookup race fix.Fixes ee10f6fcdb96 ("xfs: fix buffer lookup vs release race")Reported-by "Lai, Yi" <[email protected]>Signed-off-by Christoph Hellwig <[email protected]>--- fs/xfs/xfs_buf.c | 20 ++++++++------------ fs/xfs/xfs_buf.h | 1 - 2 files changed, 8 insertions(+), 13 deletions(-)fs/xfs/xfs_buf.c +6
-4@@ -41,8 +41,7 @@ struct kmem_cache *xfs_buf_cache;* * xfs_buf_rele: * b_lock -* pag_buf_lock -* lru_lock +* lru_lock * * xfs_buftarg_drain_rele * lru_lock @@ -502,7 +501,6 @@ intxfs_buf_cache_init ( struct xfs_buf_cache *bch) { -spin_lock_init (&bch->bc_lock);return rhashtable_init (&bch->bc_hash, &xfs_buf_hash_params); }@@ -652,17 +650,20 @@ xfs_buf_find_insert(if (error) goto out_free_buf; -spin_lock (&bch->bc_lock); +/* The new buffer keeps the perag reference until it is freed. */ + new_bp->b_pag = pag; + +rcu_read_lock (); bp =rhashtable_lookup_get_insert_fast (&bch->bc_hash, &new_bp->b_rhash_head, xfs_buf_hash_params);if (IS_ERR (bp)) { +rcu_read_unlock (); error =PTR_ERR (bp);
fs/xfs/xfs_buf.c xfs_buf_find_insert( · line 669
Quoted 10 lines
fs/xfs/xfs_buf.c +1
-2-spin_unlock (&bch->bc_lock); goto out_free_buf; }if (bp &&xfs_buf_try_hold (bp)) {/* found an existing buffer */ -spin_unlock (&bch->bc_lock); +rcu_read_unlock (); error =xfs_buf_find_lock (bp, flags);if (error)xfs_buf_rele (bp);
Ok, so now we can get racing inserts, which means this can find the buffer that has just been inserted by another thread in this same function. Or, indeed, and xfs_buf_lookup() call. What prevents those racing tasks from using this buffer before the task that inserted it can use it?
I think that the the buffer lock being initialised to "held" and b_hold being initialised to 1 make this all work correctly, but comments that explicitly spell out why RCU inserts are safe (both in xfs_buf_alloc() for the init values and here) would be appreciated.
fs/xfs/xfs_buf.h typedef unsigned int xfs_buf_flags_t; · line 84
Quoted 11 lines
fs/xfs/xfs_buf.h +0
-1@@ -80,7 +80,6 @@ typedef unsigned int xfs_buf_flags_t;#define XFS_BSTATE_IN_FLIGHT (1 << 1)/* I/O in flight */ struct xfs_buf_cache { -spinlock_t bc_lock;struct rhashtable bc_hash; };
At this point, the struct xfs_buf_cache structure can go away, right? (separate patch and all that...)
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Quoted 3 lines · Dave Chinner
Ok, so now we can get racing inserts, which means this can find the buffer that has just been inserted by another thread in this same function. Or, indeed, and
xfs_buf_lookup()call.
Yes.
Quoted 6 lines
What prevents
those racing tasks from using this buffer before the task that inserted it can use it?I think that the the buffer lock being initialised to "held" and
b_holdbeing initialised to 1 make this all work correctly,
Exactly, the buffer is inserted with the b_sema held and b_hold initializes 1, aka locked and held.
Quoted 4 lines
but
comments that explicitly spell out why RCU inserts are safe (both inxfs_buf_alloc()for the init values and here) would be appreciated.
Sure.
Quoted 7 lines
fs/xfs/xfs_buf.h typedef unsigned int xfs_buf_flags_t; · line 84
Quoted 4 lines
struct
xfs_buf_cache{fs/xfs/xfs_buf.h +0
-1-spinlock_t bc_lock;struct rhashtable bc_hash; };At this point, the struct
xfs_buf_cachestructure can go away, right? (separate patch and all that...)
Yes. And in fact I think the per-pag hash should also go away, as with the per-bucket locking there is no point in it. I've had this patch in my testing runs for a while, which I think is where we should be going:
Quoted 17 lines · Christoph Hellwig
Quoted 3 lines · Dave Chinner
Ok, so now we can get racing inserts, which means this can find the buffer that has just been inserted by another thread in this same function. Or, indeed, and
xfs_buf_lookup()call.Yes.
Quoted 6 lines
What prevents
those racing tasks from using this buffer before the task that inserted it can use it?I think that the the buffer lock being initialised to "held" and
b_holdbeing initialised to 1 make this all work correctly,Exactly, the buffer is inserted with the
b_semaheld andb_holdinitializes 1, aka locked and held.
Quoted 6 lines
Quoted 4 lines
but
comments that explicitly spell out why RCU inserts are safe (both inxfs_buf_alloc()for the init values and here) would be appreciated.Sure.
Thanks.
Quoted 8 lines
Quoted 7 lines
fs/xfs/xfs_buf.h typedef unsigned int xfs_buf_flags_t; · line 84
Quoted 4 lines
struct
xfs_buf_cache{fs/xfs/xfs_buf.h +0
-1-spinlock_t bc_lock;struct rhashtable bc_hash; };At this point, the struct
xfs_buf_cachestructure can go away, right? (separate patch and all that...)
Quoted 6 lines
Yes. And in fact I think the per-pag hash should also go away, as with the per-bucket locking there is no point in it. I've had this patch in my testing runs for a while, which I think is where we should be going:
*nod*
Code seems reasonable, but it'll need some benchmarking and scalability analysis before merging...
-Dave.
Signature · 2 lines
Dave Chinner [email protected]