Skip to contentSkip to conversation

ocfs2: add journal NULL check in ocfs2_checkpoint_inode()

PATCH Applied Joseph Qi 2 participants 5 messages May 31 – Jun 1 OCFS2

lore
Share & tools
lore

Joseph Qi

author maintainer
#0 lore raw

PATCH 1/1 ocfs2: add journal NULL check in ocfs2_checkpoint_inode()

During unmount, ocfs2_journal_shutdown() frees the journal and sets osb->journal to NULL. Later, when VFS evicts remaining cached inodes, ocfs2_evict_inode() -> ocfs2_clear_inode() -> ocfs2_checkpoint_inode() -> ocfs2_ci_fully_checkpointed() dereferences osb->journal, causing a NULL pointer dereference.

Fix this by adding a NULL check for osb->journal in ocfs2_checkpoint_inode(). If the journal is NULL, it has already been fully flushed and destroyed during shutdown, so there is nothing to checkpoint.

Fixes da5e7c87827e ("ocfs2: cleanup journal init and shutdown")
---
 fs/ocfs2/journal.h | 3 +++
 1 file changed, 3 insertions(+)
fs/ocfs2/journal.h +3 -0
index 6397170f302f..f8b3b2a3d630 100644
@@ -196,6 +196,9 @@ static inline void ocfs2_checkpoint_inode(struct inode *inode)
 	if (ocfs2_mount_local(osb))
 		return;
 
+	if (!osb->journal)
+		return;
+
 	if (!ocfs2_ci_fully_checkpointed(INODE_CACHE(inode))) {
 		/* WARNING: This only kicks off a single
 		 * checkpoint. If someone races you and adds more
Signature · 1 line
2.39.3
reviewer
#1 lore raw
Quoted 24 lines · Joseph Qi

During unmount, ocfs2_journal_shutdown() frees the journal and sets osb->journal to NULL. Later, when VFS evicts remaining cached inodes, ocfs2_evict_inode() -> ocfs2_clear_inode() -> ocfs2_checkpoint_inode() -> ocfs2_ci_fully_checkpointed() dereferences osb->journal, causing a NULL pointer dereference.

Fix this by adding a NULL check for osb->journal in ocfs2_checkpoint_inode(). If the journal is NULL, it has already been fully flushed and destroyed during shutdown, so there is nothing to checkpoint.

Fixes da5e7c87827e ("ocfs2: cleanup journal init and shutdown")
---
 fs/ocfs2/journal.h | 3 +++
 1 file changed, 3 insertions(+)
fs/ocfs2/journal.h +0 -0
index 6397170f302f..f8b3b2a3d630 100644

fs/ocfs2/journal.h static inline void ocfs2_checkpoint_inode(struct inode *inode) · line 201

Quoted 6 lines
fs/ocfs2/journal.h +3 -0
@@ -196,6 +196,9 @@ static inline void ocfs2_checkpoint_inode(struct inode *inode)
 	if (ocfs2_mount_local(osb))
 		return;
 
+	if (!osb->journal)
+		return;
+

In my view, the code is correct for this bug.
However, the if condition is insufficient if ocfs2_journal_shutdown() sets "journal = NULL" immediately after this line.

Thanks,
Heming

Quoted 6 lines
fs/ocfs2/journal.h +0 -0
 	if (!ocfs2_ci_fully_checkpointed(INODE_CACHE(inode))) {
 		/* WARNING: This only kicks off a single
 		 * checkpoint. If someone races you and adds more
Signature · 1 line
2.39.3
author maintainer
#2 lore raw
Quoted 28 lines · Heming Zhao
Quoted 27 lines · Joseph Qi

During unmount, ocfs2_journal_shutdown() frees the journal and sets osb->journal to NULL. Later, when VFS evicts remaining cached inodes, ocfs2_evict_inode() -> ocfs2_clear_inode() -> ocfs2_checkpoint_inode() -> ocfs2_ci_fully_checkpointed() dereferences osb->journal, causing a NULL pointer dereference.

Fix this by adding a NULL check for osb->journal in ocfs2_checkpoint_inode(). If the journal is NULL, it has already been fully flushed and destroyed during shutdown, so there is nothing to checkpoint.

Fixes da5e7c87827e ("ocfs2: cleanup journal init and shutdown")
---
 fs/ocfs2/journal.h | 3 +++
 1 file changed, 3 insertions(+)
fs/ocfs2/journal.h +0 -0
index 6397170f302f..f8b3b2a3d630 100644
@@ -196,6 +196,9 @@ static inline void ocfs2_checkpoint_inode(struct inode *inode)
 	if (ocfs2_mount_local(osb))
 		return;
 
Quoted 8 lines

fs/ocfs2/journal.h static inline void ocfs2_checkpoint_inode(struct inode *inode) · line 201

Quoted 3 lines
fs/ocfs2/journal.h +3 -0
+	if (!osb->journal)
+		return;
+

In my view, the code is correct for this bug.
However, the if condition is insufficient if ocfs2_journal_shutdown() sets "journal = NULL" immediately after this line.

During unmount, journal shutdown happens before final inode eviction is triggered by generic_shutdown_super() -> evict_inodes(). That means they run sequentially in the unmount path (same thread context).

Thanks,
Joseph

reviewer
#3 lore raw
Quoted 39 lines · Joseph Qi
Quoted 28 lines · Heming Zhao
Quoted 27 lines · Joseph Qi

During unmount, ocfs2_journal_shutdown() frees the journal and sets osb->journal to NULL. Later, when VFS evicts remaining cached inodes, ocfs2_evict_inode() -> ocfs2_clear_inode() -> ocfs2_checkpoint_inode() -> ocfs2_ci_fully_checkpointed() dereferences osb->journal, causing a NULL pointer dereference.

Fix this by adding a NULL check for osb->journal in ocfs2_checkpoint_inode(). If the journal is NULL, it has already been fully flushed and destroyed during shutdown, so there is nothing to checkpoint.

Fixes da5e7c87827e ("ocfs2: cleanup journal init and shutdown")
---
 fs/ocfs2/journal.h | 3 +++
 1 file changed, 3 insertions(+)
fs/ocfs2/journal.h +0 -0
index 6397170f302f..f8b3b2a3d630 100644
@@ -196,6 +196,9 @@ static inline void ocfs2_checkpoint_inode(struct inode *inode)
 	if (ocfs2_mount_local(osb))
 		return;
 
Quoted 8 lines

fs/ocfs2/journal.h static inline void ocfs2_checkpoint_inode(struct inode *inode) · line 201

Quoted 3 lines
fs/ocfs2/journal.h +3 -0
+	if (!osb->journal)
+		return;
+

In my view, the code is correct for this bug.
However, the if condition is insufficient if ocfs2_journal_shutdown() sets "journal = NULL" immediately after this line.

Quoted 7 lines

During unmount, journal shutdown happens before final inode eviction is triggered by generic_shutdown_super() -> evict_inodes(). That means they run sequentially in the unmount path (same thread context).

Thanks,
Joseph

Thanks for the explanation. I will provide my Reviewed-by tag.

Based on the call stack in Farhad's email attachment, it seems the test case triggered the if (osb->slot_num != OCFS2_INVALID_SLOT) condition in ocfs2_dismount_volume() to skip the released slot_inode.

Thanks,
Heming

reviewer Reviewed-by on v1 1/1
#4 lore raw
In reply to Joseph Qi
Quoted 11 lines · Joseph Qi

During unmount, ocfs2_journal_shutdown() frees the journal and sets osb->journal to NULL. Later, when VFS evicts remaining cached inodes, ocfs2_evict_inode() -> ocfs2_clear_inode() -> ocfs2_checkpoint_inode() -> ocfs2_ci_fully_checkpointed() dereferences osb->journal, causing a NULL pointer dereference.

Fix this by adding a NULL check for osb->journal in ocfs2_checkpoint_inode(). If the journal is NULL, it has already been fully flushed and destroyed during shutdown, so there is nothing to checkpoint.

Quoted 4 lines
Fixes da5e7c87827e ("ocfs2: cleanup journal init and shutdown")

LGTM.

Quoted 21 lines
---
 fs/ocfs2/journal.h | 3 +++
 1 file changed, 3 insertions(+)
fs/ocfs2/journal.h +3 -0
index 6397170f302f..f8b3b2a3d630 100644
@@ -196,6 +196,9 @@ static inline void ocfs2_checkpoint_inode(struct inode *inode)
 	if (ocfs2_mount_local(osb))
 		return;
 
+	if (!osb->journal)
+		return;
+
 	if (!ocfs2_ci_fully_checkpointed(INODE_CACHE(inode))) {
 		/* WARNING: This only kicks off a single
 		 * checkpoint. If someone races you and adds more
Signature · 1 line
2.39.3