Skip to content

Commit

Permalink
Fix reference counting race on log buffers
Browse files Browse the repository at this point in the history
When we release the iclog, we do an atomic_dec_and_lock to determine if
we are the last reference and need to trigger update of log headers and
writeout.  However, in xlog_state_get_iclog_space() we also need to
check if we have the last reference count there.  If we do, we release
the log buffer, otherwise we decrement the reference count.

But the compare and decrement in xlog_state_get_iclog_space() is not
atomic, so both places can see a reference count of 2 and neither will
release the iclog.  That leads to a filesystem hang.

Close the race by replacing the atomic_read() and atomic_dec() pair with
atomic_add_unless() to ensure that they are executed atomically.

Signed-off-by: Dave Chinner <[email protected]>
Reviewed-by: Tim Shimmin <[email protected]>
Tested-by: Eric Sandeen <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
dchinner authored and torvalds committed Jul 11, 2008
1 parent 61ca9da commit 49641f1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions fs/xfs/xfs_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -2427,13 +2427,20 @@ xlog_state_get_iclog_space(xlog_t *log,
if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
xlog_state_switch_iclogs(log, iclog, iclog->ic_size);

/* If I'm the only one writing to this iclog, sync it to disk */
if (atomic_read(&iclog->ic_refcnt) == 1) {
/*
* If I'm the only one writing to this iclog, sync it to disk.
* We need to do an atomic compare and decrement here to avoid
* racing with concurrent atomic_dec_and_lock() calls in
* xlog_state_release_iclog() when there is more than one
* reference to the iclog.
*/
if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
/* we are the only one */
spin_unlock(&log->l_icloglock);
if ((error = xlog_state_release_iclog(log, iclog)))
error = xlog_state_release_iclog(log, iclog);
if (error)
return error;
} else {
atomic_dec(&iclog->ic_refcnt);
spin_unlock(&log->l_icloglock);
}
goto restart;
Expand Down

0 comments on commit 49641f1

Please sign in to comment.