Skip to content

Commit bb18924

Browse files
committed
jbd: Fix oops in journal_remove_journal_head()
journal_remove_journal_head() can oops when trying to access journal_head returned by bh2jh(). This is caused for example by the following race: TASK1 TASK2 journal_commit_transaction() ... processing t_forget list __journal_refile_buffer(jh); if (!jh->b_transaction) { jbd_unlock_bh_state(bh); journal_try_to_free_buffers() journal_grab_journal_head(bh) jbd_lock_bh_state(bh) __journal_try_to_free_buffer() journal_put_journal_head(jh) journal_remove_journal_head(bh); journal_put_journal_head() in TASK2 sees that b_jcount == 0 and buffer is not part of any transaction and thus frees journal_head before TASK1 gets to doing so. Note that even buffer_head can be released by try_to_free_buffers() after journal_put_journal_head() which adds even larger opportunity for oops (but I didn't see this happen in reality). Fix the problem by making transactions hold their own journal_head reference (in b_jcount). That way we don't have to remove journal_head explicitely via journal_remove_journal_head() and instead just remove journal_head when b_jcount drops to zero. The result of this is that [__]journal_refile_buffer(), [__]journal_unfile_buffer(), and __journal_remove_checkpoint() can free journal_head which needs modification of a few callers. Also we have to be careful because once journal_head is removed, buffer_head might be freed as well. So we have to get our own buffer_head reference where it matters. Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 2c2ea94 commit bb18924

5 files changed

Lines changed: 104 additions & 138 deletions

File tree

‎fs/jbd/checkpoint.c‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,14 @@ static int __try_to_free_cp_buf(struct journal_head *jh)
9696

9797
if (jh->b_jlist == BJ_None && !buffer_locked(bh) &&
9898
!buffer_dirty(bh) && !buffer_write_io_error(bh)) {
99+
/*
100+
* Get our reference so that bh cannot be freed before
101+
* we unlock it
102+
*/
103+
get_bh(bh);
99104
JBUFFER_TRACE(jh, "remove from checkpoint list");
100105
ret = __journal_remove_checkpoint(jh) + 1;
101106
jbd_unlock_bh_state(bh);
102-
journal_remove_journal_head(bh);
103107
BUFFER_TRACE(bh, "release");
104108
__brelse(bh);
105109
} else {
@@ -221,8 +225,8 @@ static int __wait_cp_io(journal_t *journal, transaction_t *transaction)
221225
spin_lock(&journal->j_list_lock);
222226
goto restart;
223227
}
228+
get_bh(bh);
224229
if (buffer_locked(bh)) {
225-
get_bh(bh);
226230
spin_unlock(&journal->j_list_lock);
227231
jbd_unlock_bh_state(bh);
228232
wait_on_buffer(bh);
@@ -241,7 +245,6 @@ static int __wait_cp_io(journal_t *journal, transaction_t *transaction)
241245
*/
242246
released = __journal_remove_checkpoint(jh);
243247
jbd_unlock_bh_state(bh);
244-
journal_remove_journal_head(bh);
245248
__brelse(bh);
246249
}
247250

@@ -305,12 +308,12 @@ static int __process_buffer(journal_t *journal, struct journal_head *jh,
305308
ret = 1;
306309
if (unlikely(buffer_write_io_error(bh)))
307310
ret = -EIO;
311+
get_bh(bh);
308312
J_ASSERT_JH(jh, !buffer_jbddirty(bh));
309313
BUFFER_TRACE(bh, "remove from checkpoint");
310314
__journal_remove_checkpoint(jh);
311315
spin_unlock(&journal->j_list_lock);
312316
jbd_unlock_bh_state(bh);
313-
journal_remove_journal_head(bh);
314317
__brelse(bh);
315318
} else {
316319
/*
@@ -526,9 +529,9 @@ int cleanup_journal_tail(journal_t *journal)
526529
/*
527530
* journal_clean_one_cp_list
528531
*
529-
* Find all the written-back checkpoint buffers in the given list and release them.
532+
* Find all the written-back checkpoint buffers in the given list and release
533+
* them.
530534
*
531-
* Called with the journal locked.
532535
* Called with j_list_lock held.
533536
* Returns number of bufers reaped (for debug)
534537
*/
@@ -635,8 +638,8 @@ int __journal_clean_checkpoint_list(journal_t *journal)
635638
* checkpoint lists.
636639
*
637640
* The function returns 1 if it frees the transaction, 0 otherwise.
641+
* The function can free jh and bh.
638642
*
639-
* This function is called with the journal locked.
640643
* This function is called with j_list_lock held.
641644
* This function is called with jbd_lock_bh_state(jh2bh(jh))
642645
*/
@@ -655,13 +658,14 @@ int __journal_remove_checkpoint(struct journal_head *jh)
655658
}
656659
journal = transaction->t_journal;
657660

661+
JBUFFER_TRACE(jh, "removing from transaction");
658662
__buffer_unlink(jh);
659663
jh->b_cp_transaction = NULL;
664+
journal_put_journal_head(jh);
660665

661666
if (transaction->t_checkpoint_list != NULL ||
662667
transaction->t_checkpoint_io_list != NULL)
663668
goto out;
664-
JBUFFER_TRACE(jh, "transaction has no more buffers");
665669

666670
/*
667671
* There is one special case to worry about: if we have just pulled the
@@ -672,10 +676,8 @@ int __journal_remove_checkpoint(struct journal_head *jh)
672676
* The locking here around t_state is a bit sleazy.
673677
* See the comment at the end of journal_commit_transaction().
674678
*/
675-
if (transaction->t_state != T_FINISHED) {
676-
JBUFFER_TRACE(jh, "belongs to running/committing transaction");
679+
if (transaction->t_state != T_FINISHED)
677680
goto out;
678-
}
679681

680682
/* OK, that was the last buffer for the transaction: we can now
681683
safely remove this transaction from the log */
@@ -687,7 +689,6 @@ int __journal_remove_checkpoint(struct journal_head *jh)
687689
wake_up(&journal->j_wait_logspace);
688690
ret = 1;
689691
out:
690-
JBUFFER_TRACE(jh, "exit");
691692
return ret;
692693
}
693694

@@ -706,6 +707,8 @@ void __journal_insert_checkpoint(struct journal_head *jh,
706707
J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
707708
J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
708709

710+
/* Get reference for checkpointing transaction */
711+
journal_grab_journal_head(jh2bh(jh));
709712
jh->b_cp_transaction = transaction;
710713

711714
if (!transaction->t_checkpoint_list) {

‎fs/jbd/commit.c‎

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,6 @@ static int journal_submit_data_buffers(journal_t *journal,
258258
jbd_unlock_bh_state(bh);
259259
if (locked)
260260
unlock_buffer(bh);
261-
journal_remove_journal_head(bh);
262-
/* One for our safety reference, other for
263-
* journal_remove_journal_head() */
264-
put_bh(bh);
265261
release_data_buffer(bh);
266262
}
267263

@@ -455,14 +451,9 @@ void journal_commit_transaction(journal_t *journal)
455451
}
456452
if (buffer_jbd(bh) && bh2jh(bh) == jh &&
457453
jh->b_transaction == commit_transaction &&
458-
jh->b_jlist == BJ_Locked) {
454+
jh->b_jlist == BJ_Locked)
459455
__journal_unfile_buffer(jh);
460-
jbd_unlock_bh_state(bh);
461-
journal_remove_journal_head(bh);
462-
put_bh(bh);
463-
} else {
464-
jbd_unlock_bh_state(bh);
465-
}
456+
jbd_unlock_bh_state(bh);
466457
release_data_buffer(bh);
467458
cond_resched_lock(&journal->j_list_lock);
468459
}
@@ -807,10 +798,16 @@ void journal_commit_transaction(journal_t *journal)
807798
while (commit_transaction->t_forget) {
808799
transaction_t *cp_transaction;
809800
struct buffer_head *bh;
801+
int try_to_free = 0;
810802

811803
jh = commit_transaction->t_forget;
812804
spin_unlock(&journal->j_list_lock);
813805
bh = jh2bh(jh);
806+
/*
807+
* Get a reference so that bh cannot be freed before we are
808+
* done with it.
809+
*/
810+
get_bh(bh);
814811
jbd_lock_bh_state(bh);
815812
J_ASSERT_JH(jh, jh->b_transaction == commit_transaction ||
816813
jh->b_transaction == journal->j_running_transaction);
@@ -868,28 +865,27 @@ void journal_commit_transaction(journal_t *journal)
868865
__journal_insert_checkpoint(jh, commit_transaction);
869866
if (is_journal_aborted(journal))
870867
clear_buffer_jbddirty(bh);
871-
JBUFFER_TRACE(jh, "refile for checkpoint writeback");
872-
__journal_refile_buffer(jh);
873-
jbd_unlock_bh_state(bh);
874868
} else {
875869
J_ASSERT_BH(bh, !buffer_dirty(bh));
876-
/* The buffer on BJ_Forget list and not jbddirty means
870+
/*
871+
* The buffer on BJ_Forget list and not jbddirty means
877872
* it has been freed by this transaction and hence it
878873
* could not have been reallocated until this
879874
* transaction has committed. *BUT* it could be
880875
* reallocated once we have written all the data to
881876
* disk and before we process the buffer on BJ_Forget
882-
* list. */
883-
JBUFFER_TRACE(jh, "refile or unfile freed buffer");
884-
__journal_refile_buffer(jh);
885-
if (!jh->b_transaction) {
886-
jbd_unlock_bh_state(bh);
887-
/* needs a brelse */
888-
journal_remove_journal_head(bh);
889-
release_buffer_page(bh);
890-
} else
891-
jbd_unlock_bh_state(bh);
877+
* list.
878+
*/
879+
if (!jh->b_next_transaction)
880+
try_to_free = 1;
892881
}
882+
JBUFFER_TRACE(jh, "refile or unfile freed buffer");
883+
__journal_refile_buffer(jh);
884+
jbd_unlock_bh_state(bh);
885+
if (try_to_free)
886+
release_buffer_page(bh);
887+
else
888+
__brelse(bh);
893889
cond_resched_lock(&journal->j_list_lock);
894890
}
895891
spin_unlock(&journal->j_list_lock);

‎fs/jbd/journal.c‎

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,10 +1803,9 @@ static void journal_free_journal_head(struct journal_head *jh)
18031803
* When a buffer has its BH_JBD bit set it is immune from being released by
18041804
* core kernel code, mainly via ->b_count.
18051805
*
1806-
* A journal_head may be detached from its buffer_head when the journal_head's
1807-
* b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1808-
* Various places in JBD call journal_remove_journal_head() to indicate that the
1809-
* journal_head can be dropped if needed.
1806+
* A journal_head is detached from its buffer_head when the journal_head's
1807+
* b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
1808+
* transaction (b_cp_transaction) hold their references to b_jcount.
18101809
*
18111810
* Various places in the kernel want to attach a journal_head to a buffer_head
18121811
* _before_ attaching the journal_head to a transaction. To protect the
@@ -1819,17 +1818,16 @@ static void journal_free_journal_head(struct journal_head *jh)
18191818
* (Attach a journal_head if needed. Increments b_jcount)
18201819
* struct journal_head *jh = journal_add_journal_head(bh);
18211820
* ...
1822-
* jh->b_transaction = xxx;
1823-
* journal_put_journal_head(jh);
1824-
*
1825-
* Now, the journal_head's b_jcount is zero, but it is safe from being released
1826-
* because it has a non-zero b_transaction.
1821+
* (Get another reference for transaction)
1822+
* journal_grab_journal_head(bh);
1823+
* jh->b_transaction = xxx;
1824+
* (Put original reference)
1825+
* journal_put_journal_head(jh);
18271826
*/
18281827

18291828
/*
18301829
* Give a buffer_head a journal_head.
18311830
*
1832-
* Doesn't need the journal lock.
18331831
* May sleep.
18341832
*/
18351833
struct journal_head *journal_add_journal_head(struct buffer_head *bh)
@@ -1893,61 +1891,29 @@ static void __journal_remove_journal_head(struct buffer_head *bh)
18931891
struct journal_head *jh = bh2jh(bh);
18941892

18951893
J_ASSERT_JH(jh, jh->b_jcount >= 0);
1896-
1897-
get_bh(bh);
1898-
if (jh->b_jcount == 0) {
1899-
if (jh->b_transaction == NULL &&
1900-
jh->b_next_transaction == NULL &&
1901-
jh->b_cp_transaction == NULL) {
1902-
J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1903-
J_ASSERT_BH(bh, buffer_jbd(bh));
1904-
J_ASSERT_BH(bh, jh2bh(jh) == bh);
1905-
BUFFER_TRACE(bh, "remove journal_head");
1906-
if (jh->b_frozen_data) {
1907-
printk(KERN_WARNING "%s: freeing "
1908-
"b_frozen_data\n",
1909-
__func__);
1910-
jbd_free(jh->b_frozen_data, bh->b_size);
1911-
}
1912-
if (jh->b_committed_data) {
1913-
printk(KERN_WARNING "%s: freeing "
1914-
"b_committed_data\n",
1915-
__func__);
1916-
jbd_free(jh->b_committed_data, bh->b_size);
1917-
}
1918-
bh->b_private = NULL;
1919-
jh->b_bh = NULL; /* debug, really */
1920-
clear_buffer_jbd(bh);
1921-
__brelse(bh);
1922-
journal_free_journal_head(jh);
1923-
} else {
1924-
BUFFER_TRACE(bh, "journal_head was locked");
1925-
}
1894+
J_ASSERT_JH(jh, jh->b_transaction == NULL);
1895+
J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1896+
J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
1897+
J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1898+
J_ASSERT_BH(bh, buffer_jbd(bh));
1899+
J_ASSERT_BH(bh, jh2bh(jh) == bh);
1900+
BUFFER_TRACE(bh, "remove journal_head");
1901+
if (jh->b_frozen_data) {
1902+
printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
1903+
jbd_free(jh->b_frozen_data, bh->b_size);
19261904
}
1905+
if (jh->b_committed_data) {
1906+
printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
1907+
jbd_free(jh->b_committed_data, bh->b_size);
1908+
}
1909+
bh->b_private = NULL;
1910+
jh->b_bh = NULL; /* debug, really */
1911+
clear_buffer_jbd(bh);
1912+
journal_free_journal_head(jh);
19271913
}
19281914

19291915
/*
1930-
* journal_remove_journal_head(): if the buffer isn't attached to a transaction
1931-
* and has a zero b_jcount then remove and release its journal_head. If we did
1932-
* see that the buffer is not used by any transaction we also "logically"
1933-
* decrement ->b_count.
1934-
*
1935-
* We in fact take an additional increment on ->b_count as a convenience,
1936-
* because the caller usually wants to do additional things with the bh
1937-
* after calling here.
1938-
* The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1939-
* time. Once the caller has run __brelse(), the buffer is eligible for
1940-
* reaping by try_to_free_buffers().
1941-
*/
1942-
void journal_remove_journal_head(struct buffer_head *bh)
1943-
{
1944-
jbd_lock_bh_journal_head(bh);
1945-
__journal_remove_journal_head(bh);
1946-
jbd_unlock_bh_journal_head(bh);
1947-
}
1948-
1949-
/*
1950-
* Drop a reference on the passed journal_head. If it fell to zero then try to
1916+
* Drop a reference on the passed journal_head. If it fell to zero then
19511917
* release the journal_head from the buffer_head.
19521918
*/
19531919
void journal_put_journal_head(struct journal_head *jh)
@@ -1957,11 +1923,12 @@ void journal_put_journal_head(struct journal_head *jh)
19571923
jbd_lock_bh_journal_head(bh);
19581924
J_ASSERT_JH(jh, jh->b_jcount > 0);
19591925
--jh->b_jcount;
1960-
if (!jh->b_jcount && !jh->b_transaction) {
1926+
if (!jh->b_jcount) {
19611927
__journal_remove_journal_head(bh);
1928+
jbd_unlock_bh_journal_head(bh);
19621929
__brelse(bh);
1963-
}
1964-
jbd_unlock_bh_journal_head(bh);
1930+
} else
1931+
jbd_unlock_bh_journal_head(bh);
19651932
}
19661933

19671934
/*

0 commit comments

Comments
 (0)