Received: by 2002:ab2:4a89:0:b0:1f4:a8b6:6e69 with SMTP id w9csp143458lqj; Wed, 10 Apr 2024 06:39:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX2KisKSkAqtuvINn+fqwOixoRyEmE8m2c+ep8v3/bUC3fSePZUUBCgBq+RbZ3aPU5algwbsxsoeXwqn5YIe5XBLDVTSFau0UcYK5pJoA== X-Google-Smtp-Source: AGHT+IG4/1YiPeePuA4seAqq0r34jl+JTPvR/7UX9OLUT5XpPVrOF8epNxa33U7rgHfdlf1Q+6vT X-Received: by 2002:a05:620a:24c2:b0:78d:65e7:3d44 with SMTP id m2-20020a05620a24c200b0078d65e73d44mr3671375qkn.78.1712756365730; Wed, 10 Apr 2024 06:39:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712756365; cv=pass; d=google.com; s=arc-20160816; b=DKY1jMOq1rJHN844Piyvv3S4LACtqB6x/lPqE9XS/OOsilsxa7NFGRm8fUauWGR6hz dBCWeWqzFmaqEnnyDT/Ylg9h6UfinJkNXAqunNYeddAHb/x/BhQAW3Wi72xnsT9mSGZm n4bxPXEitx8Utm4FJXBVW8PLtQdAJZSgwaAtLj2+dZ29mBrLXPrxXoWRCLXk/dQvXz8z HuKy9AKWjtFZ9SV985mMWiDqYnvStuMCTJM12aLGYYuokqfnQ7Omd87w+FXb9ZSHHY/T Uy+r/m0SDEWbDEW3wphhJn0cSC6Vbz3VzvZeUaGF2ipTIyhMDS/iB5rAcdOR0pgCVwbb fiwQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=+W5KEpg2kUY6jwif517hChT/8NlMh2IK3EzSMnHViHw=; fh=UhGiR19HeAIu8tzXykjZgtAKLLMPZj8YX6gPScUOsdE=; b=PJqwaagEki3CYbkCZ9hOlC6+6Jn9h4PDP/kxTHYAkLT7k3NWbZTR+nQpAL44aeKSlU rC6tWdTdADcNFt2yE78HC9DmX/pHYaI6IIe0EayZ2K57dgvHWDP1m3c5M+vh9xOURVxY 0UhtVVzz1gix7R8CIhVfGUvmkjo+hcmBozz0p0SdzwTXzxu/QNKhgPoJHC/x38nvwSvr szDHZvYVxftofg1vs1qGuXboNjvW58c4kEQjH3nt16XYYGh+eO9/9zEnQzdWWBFTU+8V 4Wteo8Ztv1Ad1i6QyvysrkSFq89g0w+W7dHrdL38EsEO6fWj8kob86iRQiRsG4YPClPj GdwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-ext4+bounces-1947-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-ext4+bounces-1947-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id u14-20020a05620a0c4e00b00789f0d4acdesi12802266qki.317.2024.04.10.06.39.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 06:39:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4+bounces-1947-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-ext4+bounces-1947-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-ext4+bounces-1947-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D0EF81C23FF7 for ; Wed, 10 Apr 2024 13:38:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D57E816D33A; Wed, 10 Apr 2024 13:37:00 +0000 (UTC) X-Original-To: linux-ext4@vger.kernel.org Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C44B15F3F2; Wed, 10 Apr 2024 13:36:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712756220; cv=none; b=swywaaPSA1Ucu3EktBDsLm1NrTCIUHzCiNUYc9fqRBukdVVRsD4s8rrs6pJzjIzdlqkOXpaAFPraoJuktiU1k0Thsj08t2wLpi9xvu5sDEydswfzsLuN6AYNs/oY3lw1Wobj3c0/J3j8mviQk5MzQ7M1uKA0m3aaJc3c7duD+Q8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712756220; c=relaxed/simple; bh=c8/cXUrvFUJxUje/2tuH5VU8S66kB4iU4lyFh2BZH+Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bk1Lq3KW4K3/Co/HzpHVDGPt8z5hNcqFqCc6RLoSpXsFIIz4XTeiY/ydzD2eyk7QYfHe3n1Yi8gh+jt6W0y8/YWI6r07uDgH+ivUSEvhRGPt5U9b257OtD2OBSPnp83lpFT/dTdqXXjC6T+Re0tAZuZb0U1Ii1zQ32x/Ol2G/pc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4VF3lG35znz4f3kF7; Wed, 10 Apr 2024 21:36:42 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 437651A016E; Wed, 10 Apr 2024 21:36:49 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP1 (Coremail) with SMTP id cCh0CgAn+RHolRZmeCl4Jg--.8806S6; Wed, 10 Apr 2024 21:36:49 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, hch@infradead.org, djwong@kernel.org, willy@infradead.org, zokeefe@google.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com, wangkefeng.wang@huawei.com Subject: [PATCH v4 02/34] ext4: check the extent status again before inserting delalloc block Date: Wed, 10 Apr 2024 21:27:46 +0800 Message-Id: <20240410132818.2812377-3-yi.zhang@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240410132818.2812377-1-yi.zhang@huaweicloud.com> References: <20240410132818.2812377-1-yi.zhang@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:cCh0CgAn+RHolRZmeCl4Jg--.8806S6 X-Coremail-Antispam: 1UD129KBjvJXoWxurykJF17GFWxuF48Jw1DZFb_yoW5Grykpa 9xCF15Cr48Wwn7Wa93XF12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Now we lookup extent status entry without holding the i_data_sem before inserting delalloc block, it works fine in buffered write path and because it holds i_rwsem and folio lock, and the mmap path holds folio lock, so the found extent locklessly couldn't be modified concurrently. But it could be raced by fallocate since it allocate block whitout holding i_rwsem and folio lock. ext4_page_mkwrite() ext4_fallocate() block_page_mkwrite() ext4_da_map_blocks() //find hole in extent status tree ext4_alloc_file_blocks() ext4_map_blocks() //allocate block and unwritten extent ext4_insert_delayed_block() ext4_da_reserve_space() //reserve one more block ext4_es_insert_delayed_block() //drop unwritten extent and add delayed extent by mistake Then, the delalloc extent is wrong until writeback, the one more reserved block can't be release any more and trigger below warning: EXT4-fs (pmem2): Inode 13 (00000000bbbd4d23): i_reserved_data_blocks(1) not cleared! Hold i_data_sem in write mode directly can fix the problem, but it's expansive, we should keep the lockless check and check the extent again once we need to add an new delalloc block. Cc: stable@vger.kernel.org Signed-off-by: Zhang Yi --- fs/ext4/inode.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 6a41172c06e1..118b0497a954 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -1737,6 +1737,7 @@ static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, if (ext4_es_is_hole(&es)) goto add_delayed; +found: /* * Delayed extent could be allocated by fallocate. * So we need to check it. @@ -1781,6 +1782,24 @@ static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, add_delayed: down_write(&EXT4_I(inode)->i_data_sem); + /* + * Lookup extents tree again under i_data_sem, make sure this + * inserting delalloc range haven't been delayed or allocated + * whitout holding i_rwsem and folio lock. + */ + if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { + if (!ext4_es_is_hole(&es)) { + up_write(&EXT4_I(inode)->i_data_sem); + goto found; + } + } else if (!ext4_has_inline_data(inode)) { + retval = ext4_map_query_blocks(NULL, inode, map); + if (retval) { + up_write(&EXT4_I(inode)->i_data_sem); + return retval; + } + } + retval = ext4_insert_delayed_block(inode, map->m_lblk); up_write(&EXT4_I(inode)->i_data_sem); if (retval) -- 2.39.2