Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4235896ioa; Tue, 26 Apr 2022 21:56:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRfgG4GatpNRsAYaU4ANwLKRXElfan4JKE0/MxrGpyr2RHgh/NyV0TlsUYj0mt4SEndij3 X-Received: by 2002:a17:907:1b19:b0:6f0:1022:1430 with SMTP id mp25-20020a1709071b1900b006f010221430mr25109547ejc.13.1651035384785; Tue, 26 Apr 2022 21:56:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651035384; cv=none; d=google.com; s=arc-20160816; b=M7ORhV2Mlf/gU4WW+SRkmidLxmyqdKOx/Um0Rr/d+eJ0lep2p3ZYZ3U5npwtsdJdzy O9xjpuybwiGUwk2dBFHzYc6P/Wgi2qAFg3JCkA40a9EwBVCCTdkgMj0zaPagwFCawqha NiGrKa1iR0lY1WQDI2IJBgYkh743BIBh7hmmk2G48oUpjYzNSNFr5eozfRallszEmNG3 lm097Gq1Hpp0uwGdUncy1FfP0/sWSoIX94yh/TiTw/0H+FsOc6PLndk4mDz68jQM+VDA /VQcgmc7RoXh82G31JmgrWtAtTxT2k+qvxlGkYFQP1p/HouQ9W/pZ1h6agAksAlAVJEM mv9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject; bh=rENGg8ppoM3PTrf4Vc0tHO+DhCTJ2Gj8OeDvKCHDBOA=; b=MWAg/zb9qTlc/sFjD4jYB5xtzE74bJGGNL6uTyUx6U4vmgbuE5Pt4/CjA4HXDO7XEI ibfE47Pc8FS+QzJvVqaDjPe/PA3DDmGx2a32khfUPCmFeVxqD1cFu9Lr8WVU/xFXbXhT 6KQGWxmllSRWfS66TKL4c1n8vjWDkN3JdCFCd5adFBURgp5Qs9UlwYmQcwrR+bLJrJ4f Jaw6dy8g4zGd6FBGPThH2YDSSab2Iwnxi9ZBjKvOMOeJTFRmzlvXNt9qNLpUI352iAf1 wntKEb0gieo8QGyAmKQWs6MpqIVhFymuQ4fLF/Ws/zMylfrAvos1R4uk5094FsqcBwRk Laeg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nb30-20020a1709071c9e00b006eff0ac4c7csi377411ejc.230.2022.04.26.21.55.52; Tue, 26 Apr 2022 21:56:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350791AbiD0D0k (ORCPT + 99 others); Tue, 26 Apr 2022 23:26:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350567AbiD0D0h (ORCPT ); Tue, 26 Apr 2022 23:26:37 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34BA327B33; Tue, 26 Apr 2022 20:23:25 -0700 (PDT) Received: from canpemm500010.china.huawei.com (unknown [172.30.72.56]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Kp3w55Lt8z1JBnc; Wed, 27 Apr 2022 11:22:29 +0800 (CST) Received: from [10.174.178.185] (10.174.178.185) by canpemm500010.china.huawei.com (7.192.105.118) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Wed, 27 Apr 2022 11:23:22 +0800 Subject: Re: [PATCH -next v2] jbd2: Fix null-ptr-deref when process reserved list in jbd2_journal_commit_transaction To: , , References: <20220317142137.1821590-1-yebin10@huawei.com> CC: , From: yebin Message-ID: <6268B72A.6080506@huawei.com> Date: Wed, 27 Apr 2022 11:23:22 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 In-Reply-To: <20220317142137.1821590-1-yebin10@huawei.com> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.178.185] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To canpemm500010.china.huawei.com (7.192.105.118) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-6.1 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Friendly ping... On 2022/3/17 22:21, Ye Bin wrote: > we got issue as follows: > [ 72.796117] EXT4-fs error (device sda): ext4_journal_check_start:83: comm fallocate: Detected aborted journal > [ 72.826847] EXT4-fs (sda): Remounting filesystem read-only > fallocate: fallocate failed: Read-only file system > [ 74.791830] jbd2_journal_commit_transaction: jh=0xffff9cfefe725d90 bh=0x0000000000000000 end delay > [ 74.793597] ------------[ cut here ]------------ > [ 74.794203] kernel BUG at fs/jbd2/transaction.c:2063! > [ 74.794886] invalid opcode: 0000 [#1] PREEMPT SMP PTI > [ 74.795533] CPU: 4 PID: 2260 Comm: jbd2/sda-8 Not tainted 5.17.0-rc8-next-20220315-dirty #150 > [ 74.798327] RIP: 0010:__jbd2_journal_unfile_buffer+0x3e/0x60 > [ 74.801971] RSP: 0018:ffffa828c24a3cb8 EFLAGS: 00010202 > [ 74.802694] RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000000 > [ 74.803601] RDX: 0000000000000001 RSI: ffff9cfefe725d90 RDI: ffff9cfefe725d90 > [ 74.804554] RBP: ffff9cfefe725d90 R08: 0000000000000000 R09: ffffa828c24a3b20 > [ 74.805471] R10: 0000000000000001 R11: 0000000000000001 R12: ffff9cfefe725d90 > [ 74.806385] R13: ffff9cfefe725d98 R14: 0000000000000000 R15: ffff9cfe833a4d00 > [ 74.807301] FS: 0000000000000000(0000) GS:ffff9d01afb00000(0000) knlGS:0000000000000000 > [ 74.808338] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > [ 74.809084] CR2: 00007f2b81bf4000 CR3: 0000000100056000 CR4: 00000000000006e0 > [ 74.810047] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > [ 74.810981] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 > [ 74.811897] Call Trace: > [ 74.812241] > [ 74.812566] __jbd2_journal_refile_buffer+0x12f/0x180 > [ 74.813246] jbd2_journal_refile_buffer+0x4c/0xa0 > [ 74.813869] jbd2_journal_commit_transaction.cold+0xa1/0x148 > [ 74.817550] kjournald2+0xf8/0x3e0 > [ 74.819056] kthread+0x153/0x1c0 > [ 74.819963] ret_from_fork+0x22/0x30 > > Above issue may happen as follows: > write truncate kjournald2 > generic_perform_write > ext4_write_begin > ext4_walk_page_buffers > do_journal_get_write_access ->add BJ_Reserved list > ext4_journalled_write_end > ext4_walk_page_buffers > write_end_fn > ext4_handle_dirty_metadata > ***************JBD2 ABORT************** > jbd2_journal_dirty_metadata > -> return -EROFS, jh in reserved_list > jbd2_journal_commit_transaction > while (commit_transaction->t_reserved_list) > jh = commit_transaction->t_reserved_list; > truncate_pagecache_range > do_invalidatepage > ext4_journalled_invalidatepage > jbd2_journal_invalidatepage > journal_unmap_buffer > __dispose_buffer > __jbd2_journal_unfile_buffer > jbd2_journal_put_journal_head ->put last ref_count > __journal_remove_journal_head > bh->b_private = NULL; > jh->b_bh = NULL; > jbd2_journal_refile_buffer(journal, jh); > bh = jh2bh(jh); > ->bh is NULL, later will trigger null-ptr-deref > journal_free_journal_head(jh); > > As after 96f1e0974575 commit, handle reserved list will not hold "journal->j_state_lock" > when kjournald2 commit transaction. So journal_unmap_buffer maybe free > journal_head when handle reserved list. And lead to null-ptr-deref or some > strange errors. > As reserved list almost time is empty. Use "journal->j_state_lock" to protect > handle reserved list can simply solve above issue. > > Fixes: 96f1e0974575("jbd2: avoid long hold times of j_state_lock while committing a transaction") > Signed-off-by: Ye Bin > --- > fs/jbd2/commit.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/fs/jbd2/commit.c b/fs/jbd2/commit.c > index 5b9408e3b370..ac7f067b7bdd 100644 > --- a/fs/jbd2/commit.c > +++ b/fs/jbd2/commit.c > @@ -488,7 +488,6 @@ void jbd2_journal_commit_transaction(journal_t *journal) > jbd2_journal_wait_updates(journal); > > commit_transaction->t_state = T_SWITCH; > - write_unlock(&journal->j_state_lock); > > J_ASSERT (atomic_read(&commit_transaction->t_outstanding_credits) <= > journal->j_max_transaction_buffers); > @@ -508,6 +507,8 @@ void jbd2_journal_commit_transaction(journal_t *journal) > * has reserved. This is consistent with the existing behaviour > * that multiple jbd2_journal_get_write_access() calls to the same > * buffer are perfectly permissible. > + * We use journal->j_state_lock here to serialize processing of > + * t_reserved_list with eviction of buffers from journal_unmap_buffer(). > */ > while (commit_transaction->t_reserved_list) { > jh = commit_transaction->t_reserved_list; > @@ -527,6 +528,7 @@ void jbd2_journal_commit_transaction(journal_t *journal) > jbd2_journal_refile_buffer(journal, jh); > } > > + write_unlock(&journal->j_state_lock); > /* > * Now try to drop any written-back buffers from the journal's > * checkpoint lists. We do this *before* commit because it potentially