Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4473583ioa; Wed, 27 Apr 2022 04:46:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw08xokH8QFalW/vM/7IkVgISIAUJ58vw8WGX9RZfof8UMwgvqkjrI9QMInrn1YZvRCxYAB X-Received: by 2002:a17:90b:3e83:b0:1d2:edd3:5639 with SMTP id rj3-20020a17090b3e8300b001d2edd35639mr43218132pjb.183.1651060009573; Wed, 27 Apr 2022 04:46:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651060009; cv=none; d=google.com; s=arc-20160816; b=bwzlKzy0A/PTljWRPaONaxsD7EyWC/yHYCoPSRKil8m1UUAp3yWrSx9NWMXpUZv7JZ Sfa8+b+gmsYP0AyRLfGJEYpFmOBRm3Bb25ymvVZaNjOiyHET8OkouKgCV9cYuhpWBVbY xEcQf9yl21GBvEZzsIvX+kJtQ7KGanGOfUkHFGmy735zuS8MflLWNaRSHRN6sFh6LdfV yGXNFid4XP6nFRJ72v6DfueLcVFfdaaeBMsj+VK5ir0wEqbWPdeAwk3AQApQA1tBDqXB /zI/TbgjT9sbMOTxliEfNn6whZdlY9Bqm563OlJcEKBqs2ssjxSxM9JJ0By29XnJI398 Lqxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :dkim-signature; bh=TuraDcwXXKi2cRnXfRzWlMeYV5vNom2TtBxlGkwTBtM=; b=QUCjLJjmp5jmys5jnc/pJ5SVI9CQ0lVFODwerVHlL2hT8SfxNKsEW3owGhntik7Q7p OUMxCMK7ZCbZ1QbqhT//wVmQFW2ZU48j7GIWz3FOpvKb1LYiDtgNxS2bsFO5gjfxeC4W UKGcfhuKB0anRhi2yop88g85jNIhX5Yed19qreSdLjbftGxoiKXEn00Yg0YFU54nTrAZ zTCFGA7fV3k7qvQ9Z7nOId6UVeLIjiRoGc3xZ+tSp388PK4pYj8Km4BEcEDE6JJLS27N nIkjaRnOsjwQtuRp1mQh3pWFlSXTZE2w6q03Q1dM1EJj/bbxlGnG/lwTcmsJJ8dmx7fl 1XEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=OhaACAbn; dkim=neutral (no key) header.i=@suse.cz; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id f78-20020a623851000000b0050aaf7a1bbcsi1162147pfa.37.2022.04.27.04.46.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 04:46:49 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=OhaACAbn; dkim=neutral (no key) header.i=@suse.cz; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5BF1244752; Wed, 27 Apr 2022 04:07:29 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232039AbiD0LKB (ORCPT + 99 others); Wed, 27 Apr 2022 07:10:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232179AbiD0LHo (ORCPT ); Wed, 27 Apr 2022 07:07:44 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C61E222A6; Wed, 27 Apr 2022 03:59:32 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 3B736210E8; Wed, 27 Apr 2022 09:41:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1651052467; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=TuraDcwXXKi2cRnXfRzWlMeYV5vNom2TtBxlGkwTBtM=; b=OhaACAbnzlQzYj3kadKxn23D86fCwFIVEw4Ilz4OvUjPxnWYYZoRpMM19uCnkAo75xHO9t RmjHWD+I/rlxXWuBzEJ8jM9Q2jqGaFgKx4CtngGJ0k1evb/AYzMRmvfVNj9IotZYxhpUJ0 rfxU/ckvRGf7BXSWiaN/nHxbU8TTtpM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1651052467; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=TuraDcwXXKi2cRnXfRzWlMeYV5vNom2TtBxlGkwTBtM=; b=Q08CHfZpCFR6XTWhfxUoKV+sZm6RsUm2xPblimsRIua3OuMo5hPBSE80QRvW4lvAJW6+ve uQNYjh4+jK650LAw== Received: from quack3.suse.cz (unknown [10.100.224.230]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id E998D2C141; Wed, 27 Apr 2022 09:41:06 +0000 (UTC) Received: by quack3.suse.cz (Postfix, from userid 1000) id 79407A0620; Wed, 27 Apr 2022 11:41:06 +0200 (CEST) Date: Wed, 27 Apr 2022 11:41:06 +0200 From: Jan Kara To: yebin Cc: tytso@mit.edu, adilger.kernel@dilger.ca, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, jack@suse.cz Subject: Re: [PATCH -next v2] jbd2: Fix null-ptr-deref when process reserved list in jbd2_journal_commit_transaction Message-ID: <20220427094106.5lmyockklpckkbs3@quack3.lan> References: <20220317142137.1821590-1-yebin10@huawei.com> <6268B72A.6080506@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6268B72A.6080506@huawei.com> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE autolearn=no 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 On Wed 27-04-22 11:23:22, yebin wrote: > Friendly ping... Ted probably forgot to send his standard email but your patch is already in with a slightly edited message: 23e3d7f7061f86 ("jbd2: fix a potential race while discarding reserved buffers after an abort") Honza > > 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 > -- Jan Kara SUSE Labs, CR