Received: by 2002:a05:7412:b130:b0:e2:908c:2ebd with SMTP id az48csp1968409rdb; Sun, 19 Nov 2023 19:20:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IF96m29ds/CkZsSZGOCp6mdXaE2svZQRaRRfs2sSe/Ev8biq00tFMNjaIV+CTeWgmEX8mbk X-Received: by 2002:a05:6808:a9c:b0:3b6:d617:a719 with SMTP id q28-20020a0568080a9c00b003b6d617a719mr6460026oij.41.1700450405657; Sun, 19 Nov 2023 19:20:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700450405; cv=none; d=google.com; s=arc-20160816; b=Vo8i/+KuC3ezYItrAGK8Yp0doAu6I7ZemzHeJcITV0S3ZJmQN9IIxtd9zFZoILKOTs tGYkh8OgHdFc/GFkRZUegN6du9RDiQMmBdDQHxmNXWrV/qXefKdzEY8LIv/igI4nZAFf EA48qzJbZ4oW3MozA8oEMywv9QT/1lJIpATqUJQbSotzAB0i+HtPsD8AWlfYgm+ePZzY JoP2XhqpEPeZy1TjVqw/PZzq9SCkr1sFLWEfYh4LHo+svzKf+nGf4nEaDq+IR/kbrkBr rTLqthZs8ZoIDG5sa5dWmqQ2oNvw+EejAi1VpVbxZG5n0Vk7Mmx3qJZ6RppDGy6+pU2c jaXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=htadz3AQoJ4Ze6NyzhsujKFld5EWVXe6VoVeHRjMA5I=; fh=8iuAhxKZ4zRmu4yJ8kyLM0sJOjlCiDjhtzZdjPnU8ps=; b=w+Ny5SWH1r3AjN5+mTk7g3dvrrCRDrpc59gLNzM6SoeD6C4yzR7SbW1t7fnZlcSib4 pzAKjO0bO7A0lkKZ14MVl3LZPixb/MFlSN/pKiuxGZqrNBzQ9J7ZgMH062dDJ2dLpbqJ dXB6YeQYwyg3G4tqvxh7uvXQ//WfsODJ+hbrI2R8yHJ075ay2/dlJIoSj/GsfkcvOZv5 INVOLvC5p7vfOtVTlmVj+qhHIS4i+KP/c8MTMLataUaEaOCfsvAWdJVz1pR8TKQapofV 8jOOHnxRjAZ/UzEtXzKRMfSjkGGmffJVsSuyeKAtrAITxzXht5bEaPxLi8ADqiSSpiMw BBDQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id h9-20020a656389000000b005b8f0ca2e5asi7021606pgv.79.2023.11.19.19.20.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Nov 2023 19:20:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 22795802C7CF; Sun, 19 Nov 2023 19:20:04 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231889AbjKTDT7 (ORCPT + 99 others); Sun, 19 Nov 2023 22:19:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231862AbjKTDTz (ORCPT ); Sun, 19 Nov 2023 22:19:55 -0500 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFC1F137 for ; Sun, 19 Nov 2023 19:19:50 -0800 (PST) Received: from kwepemm000013.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4SYXj30Chkz1P6j8; Mon, 20 Nov 2023 11:16:23 +0800 (CST) Received: from huawei.com (10.175.104.67) by kwepemm000013.china.huawei.com (7.193.23.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 20 Nov 2023 11:19:48 +0800 From: Zhihao Cheng To: , , , , CC: , Subject: [PATCH 2/3] ubifs: ubifs_tnc_locate: Drop TNC mutex lockless reading path Date: Mon, 20 Nov 2023 19:13:46 +0800 Message-ID: <20231120111347.2254153-3-chengzhihao1@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231120111347.2254153-1-chengzhihao1@huawei.com> References: <20231120111347.2254153-1-chengzhihao1@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.175.104.67] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemm000013.china.huawei.com (7.193.23.81) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.0 required=5.0 tests=BAYES_00,DATE_IN_FUTURE_06_12, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, 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-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 19 Nov 2023 19:20:04 -0800 (PST) This problem is described in [1], and Tao has proposed a solution in [2]. There exists a race between node reading and gc, which is shown as: P1 P2 ubifs_tnc_locate zbr.lnum = lnum_a GC // node is moved from lnum_a to lnum_b journal head switching // lnum_a becomes a bud ubifs_get_wbuf(c, zbr.lnum) // true ubifs_tnc_read_node ubifs_read_node_wbuf // read data from lnum_a check node failed ! There are two ways of reading node(See ubifs_tnc_read_node()): 1. Reading from wbuf. The node is written in wbuf(in mem), and wbuf is not written back to flash. 2. Otherwise, reading from flash. In most cases, ubifs_tnc_read_node() is invoked with TNC mutex locked, but except the lockless path in ubifs_tnc_locate() which is imported by commit 601c0bc46753("UBIFS: allow for racing between GC and TNC"). Function ubifs_tnc_locate() is mainly used for path lookup and file reading, VFS has inode/dentry/page cache for multiple times reading, the lockless optimization only works for first reading. Based on the discussion in [2], this patch simply drops the TNC mutex lockless reading path in ubifs_tnc_locate(). Fetch a reproducer in [3]. [1] https://lore.kernel.org/all/fda84926-09d1-1fc7-4b78-99e0d04508bc@huawei.com/T/ [2] https://lore.kernel.org/linux-mtd/20200305092205.127758-1-houtao1@huawei.com/ [3] https://bugzilla.kernel.org/show_bug.cgi?id=218163 Fixes: 601c0bc46753 ("UBIFS: allow for racing between GC and TNC") Fixes: 1e51764a3c2a ("UBIFS: add new flash file system") Reported-by: 李傲傲 (Carson Li1/9542) Analyzed-by: Hou Tao Signed-off-by: Zhihao Cheng --- fs/ubifs/tnc.c | 31 +++---------------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/fs/ubifs/tnc.c b/fs/ubifs/tnc.c index f4728e65d1bd..7b7d75ed3ec7 100644 --- a/fs/ubifs/tnc.c +++ b/fs/ubifs/tnc.c @@ -1478,11 +1478,10 @@ static int maybe_leb_gced(struct ubifs_info *c, int lnum, int gc_seq1) int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key, void *node, int *lnum, int *offs) { - int found, n, err, safely = 0, gc_seq1; + int found, n, err; struct ubifs_znode *znode; - struct ubifs_zbranch zbr, *zt; + struct ubifs_zbranch *zt; -again: mutex_lock(&c->tnc_mutex); found = ubifs_lookup_level0(c, key, &znode, &n); if (!found) { @@ -1505,31 +1504,7 @@ int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key, err = tnc_read_hashed_node(c, zt, node); goto out; } - if (safely) { - err = ubifs_tnc_read_node(c, zt, node); - goto out; - } - /* Drop the TNC mutex prematurely and race with garbage collection */ - zbr = znode->zbranch[n]; - gc_seq1 = c->gc_seq; - mutex_unlock(&c->tnc_mutex); - - if (ubifs_get_wbuf(c, zbr.lnum)) { - /* We do not GC journal heads */ - err = ubifs_tnc_read_node(c, &zbr, node); - return err; - } - - err = fallible_read_node(c, key, &zbr, node); - if (err <= 0 || maybe_leb_gced(c, zbr.lnum, gc_seq1)) { - /* - * The node may have been GC'ed out from under us so try again - * while keeping the TNC mutex locked. - */ - safely = 1; - goto again; - } - return 0; + err = ubifs_tnc_read_node(c, zt, node); out: mutex_unlock(&c->tnc_mutex); -- 2.39.2