Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] ocfs2: don't free a NULL chunk in ocfs2_local_quota_add_chunk()
@ 2026-11-30 23:56 Zijie Zhao via B4 Relay
  2026-12-01 10:36 ` Jan Kara
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Zijie Zhao via B4 Relay @ 2026-11-30 23:56 UTC (permalink / raw)
  To: Mark Fasheh, Joel Becker, Joseph Qi
  Cc: Heming Zhao, Jan Kara, ocfs2-devel, stable, Zijie Zhao

From: Zijie Zhao <zzjas98@gmail.com>

ocfs2_local_quota_add_chunk() allocates the in-memory chunk only after
the local quota file has been extended. If ocfs2_extend_no_holes() or
ocfs2_simple_size_update() fails, for example with -ENOSPC on a full
filesystem, or if kmem_cache_alloc() fails, the function jumps to the
out label with chunk still NULL. The out label then calls
kmem_cache_free(ocfs2_qf_chunk_cachep, NULL).

Unlike kfree(), kmem_cache_free() does not accept NULL. It calls
virt_to_slab() on the pointer and reads the struct page derived from
address 0. This can oops, or warn about freeing an object that is not
from a slab page.

Allocate the chunk before the file is extended and return right away if
the allocation fails. Every jump to the out label then has a valid
chunk to free. A failed allocation also no longer leaves the local
quota file grown by two unused blocks.

Found by a static analysis tool.

The fix and this changelog were drafted with AI assistance (Claude Code,
with a second review by OpenAI Codex), then reviewed by the submitter.

Fixes: 9e33d69f553a ("ocfs2: Implementation of local and global quota file handling")
Cc: stable@vger.kernel.org
Signed-off-by: Zijie Zhao <zzjas98@gmail.com>
---
An alternative was to keep the allocation where it was and guard the free
with "if (chunk)". Allocating first also stops a failed allocation from
leaving the local quota file two blocks larger.

Two related issues on this error path predate the patch and are left out of
it. If ocfs2_simple_size_update() or the chunk initialisation fails, the
extended local quota file is not shrunk back. Also, ocfs2_acquire_dquot()
does not undo the dq_use_count it has already committed when
ocfs2_create_local_dquot() fails. Happy to send separate patches if wanted.

Testing:
- Compile-tested on x86_64 (allmodconfig, W=1): fs/ocfs2/quota_local.o;
  no new warnings.
- checkpatch.pl --strict: no errors or warnings.
---
 fs/ocfs2/quota_local.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/fs/ocfs2/quota_local.c b/fs/ocfs2/quota_local.c
index 8be76fa784ac..5a241d465f46 100644
--- a/fs/ocfs2/quota_local.c
+++ b/fs/ocfs2/quota_local.c
@@ -1003,6 +1003,13 @@ static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
 	struct buffer_head *bh = NULL, *dbh = NULL;
 	u64 p_blkno;
 
+	chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
+	if (!chunk) {
+		status = -ENOMEM;
+		mlog_errno(status);
+		return ERR_PTR(status);
+	}
+
 	/* We are protected by dqio_sem so no locking needed */
 	status = ocfs2_extend_no_holes(lqinode, NULL,
 				       i_size_read(lqinode) + 2 * sb->s_blocksize,
@@ -1018,12 +1025,6 @@ static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
 		goto out;
 	}
 
-	chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
-	if (!chunk) {
-		status = -ENOMEM;
-		mlog_errno(status);
-		goto out;
-	}
 	/* Local quota info and two new blocks we initialize */
 	handle = ocfs2_start_trans(OCFS2_SB(sb),
 			OCFS2_LOCAL_QINFO_WRITE_CREDITS +

---
base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
change-id: 20261130-kbug-l1059-ocfs2-quota-b402c6eeb200

Best regards,
--  
Zijie Zhao <zzjas98@gmail.com>



^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-02  3:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 23:56 [PATCH] ocfs2: don't free a NULL chunk in ocfs2_local_quota_add_chunk() Zijie Zhao via B4 Relay
2026-12-01 10:36 ` Jan Kara
2026-12-01 11:00   ` Joseph Qi
2026-12-01 20:20 ` + ocfs2-dont-free-a-null-chunk-in-ocfs2_local_quota_add_chunk.patch added to mm-nonmm-hotfixes-unstable branch Andrew Morton
2026-12-02  3:51 ` [merged mm-nonmm-hotfixes-stable] ocfs2-dont-free-a-null-chunk-in-ocfs2_local_quota_add_chunk.patch removed from -mm tree Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox