History

Fix reference counting race on log buffers

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.

Dave Chinner committed 18 years ago

Commit 49641f11 parent 61ca9da1 file · +11 −4Browse files

Unified · Split

fs/xfs/xfs_log.c+11−4
@@ -2427,13 +2427,20 @@ restart: · 2426 unmodified lines
24272427 if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
24282428 xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
24292429
2430− /* If I'm the only one writing to this iclog, sync it to disk */
2431− if (atomic_read(&iclog->ic_refcnt) == 1) {
2430+ /*
2431+ * If I'm the only one writing to this iclog, sync it to disk.
2432+ * We need to do an atomic compare and decrement here to avoid
2433+ * racing with concurrent atomic_dec_and_lock() calls in
2434+ * xlog_state_release_iclog() when there is more than one
2435+ * reference to the iclog.
2436+ */
2437+ if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
2438+ /* we are the only one */
24322439 spin_unlock(&log->l_icloglock);
2433− if ((error = xlog_state_release_iclog(log, iclog)))
2440+ error = xlog_state_release_iclog(log, iclog);
2441+ if (error)
24342442 return error;
24352443 } else {
2436− atomic_dec(&iclog->ic_refcnt);
24372444 spin_unlock(&log->l_icloglock);
24382445 }
24392446 goto restart;