Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp801309rwd; Thu, 1 Jun 2023 06:48:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7oyr/vhdl4YBQQz10mtbPUIi5gW437P0/FiiVvBCq5OVoJodS9mGHoxIO6RQpvGSJyW/CV X-Received: by 2002:a05:6a00:1a91:b0:64d:61f2:ca88 with SMTP id e17-20020a056a001a9100b0064d61f2ca88mr7089641pfv.12.1685627328760; Thu, 01 Jun 2023 06:48:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685627328; cv=none; d=google.com; s=arc-20160816; b=jZk1Blb1ZiZ8N4itTBK53QKZNlajDvv7RwM40PF5L/PzjiId06CXIaxrS/5RSQQ/10 NIZSgW7N8T4JZNR3NConsosHxvKvtN3Budx4PX7MSW8wIjIyN87Xdz/7T+eneeY1/TGQ +k79D4N7mMJ7qFZFCb2IKjJXJPx3tWokpj811LtSQmTEDU0x8FCmwied05FOinEWCz7P bd22OBJyVLqRrnTa4sy1VTDSJfXtHPWnHNyDq7LcBNIA+CrOj7neHyIVG54PwxpALP7+ rk+ODw5NW11MQ3VC7uZ8Wnzga6ouDogWE0z67Ogf97FtWuzGd/5+lqE8NU1dS+Z4k/Kw o18w== 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:references:cc:to :subject; bh=fRltbS73YK+IJOd/TgCjA82cdv46Qb5QmnQEFtqKvzU=; b=E/brTzXMp0o4agQqJ9Gqjzh4Fg97ctti/S6nnLyAc1sSKHALbUEaNBMamw/vPLHbpW Hz8gYSPRLsmO9BkwLafObS9NXxS1cuvMp9vfLrBu5jxCyVuDOpMh6hBo8nUSmU4phFUO A21E1qVuJJmGi4WQ95g2w2Z+sc/DP3wotfN5QAtiKMwo7zZYhcmKTwb9lukAWaXLDU55 0Xf7LS89KGfzY/4tbQKGBl+D/zkWykjBZufrdzUnc1jPUmj02qTDTEhD7WbfcP0qL+jX QJs86ImKj35zPgTsfP+vOEExQTDmJnwxFMToJrTt27n7CncmvUEVvS524KbQK8ugGtwz UBlA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-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 c8-20020aa79528000000b00643c4d0d0f5si5443163pfp.39.2023.06.01.06.48.32; Thu, 01 Jun 2023 06:48:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-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 S232140AbjFANpH (ORCPT + 99 others); Thu, 1 Jun 2023 09:45:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233948AbjFANpF (ORCPT ); Thu, 1 Jun 2023 09:45:05 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6C4318F for ; Thu, 1 Jun 2023 06:45:00 -0700 (PDT) Received: from kwepemm600013.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4QX6hJ3dVYz18LXB; Thu, 1 Jun 2023 21:40:16 +0800 (CST) Received: from [10.174.178.46] (10.174.178.46) by kwepemm600013.china.huawei.com (7.193.23.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Thu, 1 Jun 2023 21:44:55 +0800 Subject: Re: [PATCH 4/5] jbd2: Fix wrongly judgement for buffer head removing while doing checkpoint To: Jan Kara , Zhang Yi CC: , , , , References: <20230531115100.2779605-1-yi.zhang@huaweicloud.com> <20230531115100.2779605-5-yi.zhang@huaweicloud.com> <20230601094156.m4b7rxntmaxc5zy7@quack3> From: Zhihao Cheng Message-ID: Date: Thu, 1 Jun 2023 21:44:44 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <20230601094156.m4b7rxntmaxc5zy7@quack3> Content-Type: text/plain; charset="gbk"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.178.46] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemm600013.china.huawei.com (7.193.23.68) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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-ext4@vger.kernel.org ?? 2023/6/1 17:41, Jan Kara ะด??: Hi, Jan > On Wed 31-05-23 19:50:59, Zhang Yi wrote: >> From: Zhihao Cheng >> >> Following process, >> >> jbd2_journal_commit_transaction >> // there are several dirty buffer heads in transaction->t_checkpoint_list >> P1 wb_workfn >> jbd2_log_do_checkpoint >> if (buffer_locked(bh)) // false >> __block_write_full_page >> trylock_buffer(bh) >> test_clear_buffer_dirty(bh) >> if (!buffer_dirty(bh)) >> __jbd2_journal_remove_checkpoint(jh) >> if (buffer_write_io_error(bh)) // false >> >> bh IO error occurs << >> jbd2_cleanup_journal_tail >> __jbd2_update_log_tail >> jbd2_write_superblock >> // The bh won't be replayed in next mount. >> , which could corrupt the ext4 image, fetch a reproducer in [Link]. >> >> Since writeback process clears buffer dirty after locking buffer head, >> we can fix it by checking buffer dirty firstly and then checking buffer >> locked, the buffer head can be removed if it is neither dirty nor locked. >> >> Link: https://bugzilla.kernel.org/show_bug.cgi?id=217490 >> Fixes: 470decc613ab ("[PATCH] jbd2: initial copy of files from jbd") >> Signed-off-by: Zhihao Cheng >> Signed-off-by: Zhang Yi > > OK, the analysis is correct but I'm afraid the fix won't be that easy. The > reordering of tests you did below doesn't really help because CPU or the > compiler are free to order the loads (and stores) in whatever way they > wish. You'd have to use memory barriers when reading and modifying bh flags > (although the modification side is implicitely handled by the bitlock > code) to make this work reliably. But that is IMHO too subtle for this > code. > Do you mean there might be a sequence like following: jbd2_log_do_checkpoint if (buffer_dirty(bh)) else if (buffer_locked(bh)) else __jbd2_journal_remove_checkpoint(jh) CPU re-arranges the order of getting buffer state. reg_1 = buffer_locked(bh) // false lock_buffer(bh) clear_buffer(bh) reg_2 = buffer_dirty(bh) // false Then, jbd2_log_do_checkpoint() could become: if (reg_2) else if (reg_1) else __jbd2_journal_remove_checkpoint(jh) // enter ! Am I understanding right? > What we should be doing to avoid these races is to lock the bh. So > something like: > > if (jh->b_transaction != NULL) { > do stuff > } > if (!trylock_buffer(bh)) { > buffer_locked() branch > } > ... Now we have the buffer locked and can safely check for dirtyness > > And we need to do a similar treatment for journal_clean_one_cp_list() and > journal_shrink_one_cp_list(). > > BTW, I think we could merge journal_clean_one_cp_list() and > journal_shrink_one_cp_list() into a single common function. I think we can > drop the nr_to_scan argument and just always cleanup the whole checkpoint > list and return the number of freed buffers. That way we have one less > function to deal with checkpoint list cleaning. > > Thinking about it some more maybe we can have a function like: > > int jbd2_try_remove_checkpoint(struct journal_head *jh) > { > struct buffer_head *bh = jh2bh(jh); > > if (!trylock_buffer(bh) || buffer_dirty(bh)) > return -EBUSY; > /* > * Buffer is clean and the IO has finished (we hold the buffer lock) so > * the checkpoint is done. We can safely remove the buffer from this > * transaction. > */ > unlock_buffer(bh); > return __jbd2_journal_remove_checkpoint(jh); > } > > and that can be used with a bit of care in the checkpointing functions as > well as in jbd2_journal_forget(), __journal_try_to_free_buffer(), > journal_unmap_buffer(). > > Honza >