diff --git a/fs/buffer.c b/fs/buffer.c index 22b43642ba574..dd2bd24126c09 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -2211,8 +2211,22 @@ void block_commit_write(struct folio *folio, size_t from, size_t to) if (!buffer_uptodate(bh)) partial = true; } else { + /* + * Per the contract documented at set_buffer_uptodate() + * in include/linux/buffer_head.h, callers must hold + * BH_Lock to serialize against concurrent clears of + * BH_Uptodate. Holding only the folio lock is not + * sufficient: a concurrent end_buffer_write_sync() on + * the write-error path clears BH_Uptodate while + * holding BH_Lock; without BH_Lock here the clear can + * land between set_buffer_uptodate() and + * mark_buffer_dirty(), tripping the WARN_ON_ONCE in + * mark_buffer_dirty(). + */ + lock_buffer(bh); set_buffer_uptodate(bh); mark_buffer_dirty(bh); + unlock_buffer(bh); } if (buffer_new(bh)) clear_buffer_new(bh);