Skip to content

Commit aaa2cac

Browse files
htejunaxboe
authored andcommitted
writeback: add lockdep annotation to inode_to_wb()
With the previous three patches, all operations which acquire wb from inode are either under one of inode->i_lock, mapping->tree_lock or wb->list_lock or protected by unlocked_inode_to_wb transaction. This will be depended upon by foreign inode wb switching. This patch adds lockdep assertion to inode_to_wb() so that usages outside the above list locks can be caught easily. There are three exceptions. * locked_inode_to_wb_and_lock_list() is holding wb->list_lock but the wb may not be the inode's. Ensuring that is the function's role after all. Updated to deref inode->i_wb directly. * inode_wb_stat_unlocked_begin() is usually protected by combination of !I_WB_SWITCH and rcu_read_lock(). Updated to deref inode->i_wb directly. * inode_congested() wants to test whether inode->i_wb is set before starting the transaction. Added inode_to_wb_is_valid() which tests inode->i_wb directly. v5: might_lock() removed. It annotates that the lock is grabbed w/ irq enabled which isn't the case and triggering lockdep warning spuriously. v4: might_lock() added to unlocked_inode_to_wb_begin(). v3: inode_congested() conversion added. v2: locked_inode_to_wb_and_lock_list() was missing in the first version. Signed-off-by: Tejun Heo <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Jan Kara <[email protected]> Cc: Wu Fengguang <[email protected]> Cc: Greg Thelen <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 5cb8b82 commit aaa2cac

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

fs/fs-writeback.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ locked_inode_to_wb_and_lock_list(struct inode *inode)
285285
spin_lock(&wb->list_lock);
286286
wb_put(wb); /* not gonna deref it anymore */
287287

288-
if (likely(wb == inode_to_wb(inode)))
288+
/* i_wb may have changed inbetween, can't use inode_to_wb() */
289+
if (likely(wb == inode->i_wb))
289290
return wb; /* @inode already has ref */
290291

291292
spin_unlock(&wb->list_lock);
@@ -622,7 +623,7 @@ int inode_congested(struct inode *inode, int cong_bits)
622623
* Once set, ->i_wb never becomes NULL while the inode is alive.
623624
* Start transaction iff ->i_wb is visible.
624625
*/
625-
if (inode && inode_to_wb(inode)) {
626+
if (inode && inode_to_wb_is_valid(inode)) {
626627
struct bdi_writeback *wb;
627628
bool locked, congested;
628629

include/linux/backing-dev.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,34 @@ wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
321321
return wb;
322322
}
323323

324+
/**
325+
* inode_to_wb_is_valid - test whether an inode has a wb associated
326+
* @inode: inode of interest
327+
*
328+
* Returns %true if @inode has a wb associated. May be called without any
329+
* locking.
330+
*/
331+
static inline bool inode_to_wb_is_valid(struct inode *inode)
332+
{
333+
return inode->i_wb;
334+
}
335+
324336
/**
325337
* inode_to_wb - determine the wb of an inode
326338
* @inode: inode of interest
327339
*
328-
* Returns the wb @inode is currently associated with.
340+
* Returns the wb @inode is currently associated with. The caller must be
341+
* holding either @inode->i_lock, @inode->i_mapping->tree_lock, or the
342+
* associated wb's list_lock.
329343
*/
330344
static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
331345
{
346+
#ifdef CONFIG_LOCKDEP
347+
WARN_ON_ONCE(debug_locks &&
348+
(!lockdep_is_held(&inode->i_lock) &&
349+
!lockdep_is_held(&inode->i_mapping->tree_lock) &&
350+
!lockdep_is_held(&inode->i_wb->list_lock)));
351+
#endif
332352
return inode->i_wb;
333353
}
334354

@@ -360,7 +380,12 @@ unlocked_inode_to_wb_begin(struct inode *inode, bool *lockedp)
360380

361381
if (unlikely(*lockedp))
362382
spin_lock_irq(&inode->i_mapping->tree_lock);
363-
return inode_to_wb(inode);
383+
384+
/*
385+
* Protected by either !I_WB_SWITCH + rcu_read_lock() or tree_lock.
386+
* inode_to_wb() will bark. Deref directly.
387+
*/
388+
return inode->i_wb;
364389
}
365390

366391
/**
@@ -459,6 +484,11 @@ wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
459484
return &bdi->wb;
460485
}
461486

487+
static inline bool inode_to_wb_is_valid(struct inode *inode)
488+
{
489+
return true;
490+
}
491+
462492
static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
463493
{
464494
return &inode_to_bdi(inode)->wb;

0 commit comments

Comments
 (0)