From: Zijie Zhao 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 --- 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