bcachefs: Fix journal_buf_realloc()
authorKent Overstreet <kent.overstreet@gmail.com>
Mon, 4 Jan 2021 20:46:57 +0000 (15:46 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:08:50 +0000 (17:08 -0400)
It used to be safe to reallocate a buf that the write path owns without
holding the journal lock, but now this can trigger an assertion in
journal_seq_to_buf().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/journal_io.c

index 25010aa42af6544bcc8e9d9f5b793360a753fa66..cba420565248ac4f200ce20401b2571b51b3a01b 100644 (file)
@@ -1051,9 +1051,13 @@ static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)
                return;
 
        memcpy(new_buf, buf->data, buf->buf_size);
-       kvpfree(buf->data, buf->buf_size);
-       buf->data       = new_buf;
-       buf->buf_size   = new_size;
+
+       spin_lock(&j->lock);
+       swap(buf->data,         new_buf);
+       swap(buf->buf_size,     new_size);
+       spin_unlock(&j->lock);
+
+       kvpfree(new_buf, new_size);
 }
 
 static inline struct journal_buf *journal_last_unwritten_buf(struct journal *j)