Received: by 2002:ab2:4a89:0:b0:1f4:a8b6:6e69 with SMTP id w9csp184835lqj; Wed, 10 Apr 2024 07:41:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVqocqAUhMUdTlyeKWIZMdBTneoNsnRQb5CvFsWX5Tp7RAd8i0CdAp3weHkK7EokswWXFCwSLZL1wg23HhaVlbZw3mb53aTwxeny3PYZQ== X-Google-Smtp-Source: AGHT+IHgbXxWw7CSf7wlhLKIFJSHSTYJMYneMjj1VQ/6BOL0jbJNdDJX20Hcg+ByLGLSIWTtLNak X-Received: by 2002:a17:906:b181:b0:a51:f7de:df6 with SMTP id w1-20020a170906b18100b00a51f7de0df6mr1610850ejy.16.1712760115717; Wed, 10 Apr 2024 07:41:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712760115; cv=pass; d=google.com; s=arc-20160816; b=VD6aK9zQb3gfY2cwYA5hTreRI8DUG/7IZWPrsq1YDChcBYLrC3hEgznahk3enTLr9a dGbS+Axj4UNN4vnGoIAE+kuFyTt7CSmSmF5vqhwUhSELqyYYUwk/P2hJvV+5X0fO1j0N TSDgJ53ns8IeGG3zjvPqnGVeoggCuR28K7NGLW23LA6a8FXq8Lh5Y8Jwl6+MA1xa5pZt SKbuRPd/EdhyH42geLj4hp8nIhtJbq+BkBFYeGulSGU9Gh9sUHUxEsgnTmcqEtdbD//K ilRkLYK6kLKwPn+IYfBpFGNtZhIDT7vOO3U7CvWTsRSpzqBHmR/L5RV3VTFny8HFcP8z P4Cw== 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=VCdyVZCUh9R8dltaA9/RWs/hxQzWhc63WX0fta2PQtE=; b=LKtIe3jeQEZSe/5tIEbH+sCxM2eef4LAc8aHDxe0RWwMo3/3LkFL+iQYmeEsCVZbEI 6f1PEq3mnVyqjm+RruES/RNOOsYZ/gS9NoVcs+NNwdPrrbcXnGGm0FYweLKGMvqkc2tE X0dJ/MZdbY35zSt/3VuZiHrJwzUAI0DShgQxyGMcr4ZQzhhZ0wCccFNnBblmiJ8ThAwJ nYvMlND9Ac5E4u5azzJX7cYFQ6MMqknCZdicvF2aU8OLXN+i+EXrU3yttG9IWjHtM9vr JaNh5VH2q0+ZbbHMu5+6NU5/ywHlhbUAs5ZmnWx4U4yGRmqIbAaUnYkDYKMePv0Wna7Z e2Hw==; 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-kernel+bounces-138769-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-138769-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id kg25-20020a17090776f900b00a47c39e82ffsi5763897ejc.643.2024.04.10.07.41.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 07:41:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-138769-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-138769-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-138769-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 9ACA01F2701C for ; Wed, 10 Apr 2024 14:41:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 98D5F171095; Wed, 10 Apr 2024 14:38:33 +0000 (UTC) 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 41A8216DEDE; Wed, 10 Apr 2024 14:38:30 +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=1712759912; cv=none; b=ZhKEjcSmQNz6d65/CcXL66z5czEhwdMM4KanoHdbjg9PtAo3l+U3VeGqB3SS0uLcPe+02+889fiwrwmbuAf4NTc/hWii3Vc/CELhgCRhSG35K7o17FnNjvUfKzOV5bOLAUrF2bNGES5zOhPAbKTbRjiZgr4rXlXmY1TxczbyoZ8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712759912; c=relaxed/simple; bh=c8/cXUrvFUJxUje/2tuH5VU8S66kB4iU4lyFh2BZH+Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mgf7HZ3FQ3uMvnjdOwbCGrLw29nuAnvYUBnSfFcFT9GKwlp7LTwaL5MqEOSzX01wPelwj1n4LD6QqQm+6Nob7QQ/9k9qsSnnBCogVXN2XoMzHEJMhMzh7ppVmHNc9Lzkn0pkvvCef8EfjxKc4pajohdIebPRPilus2bG4bOofbw= 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.163.216]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4VF56N1qDfz4f3khN; Wed, 10 Apr 2024 22:38:20 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 19E6C1A0C4F; Wed, 10 Apr 2024 22:38:27 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP1 (Coremail) with SMTP id cCh0CgAX6RFSpBZmcwR8Jg--.63000S6; Wed, 10 Apr 2024 22:38:26 +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, david@fromorbit.com, 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 22:29:16 +0800 Message-Id: <20240410142948.2817554-3-yi.zhang@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240410142948.2817554-1-yi.zhang@huaweicloud.com> References: <20240410142948.2817554-1-yi.zhang@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:cCh0CgAX6RFSpBZmcwR8Jg--.63000S6 X-Coremail-Antispam: 1UD129KBjvJXoWxurykJF17GFWxuF48Jw1DZFb_yoW5Grykpa 9xCF15Cr48Wwn7Wa93XF12vr1rWa1rJrWUKFZxKr1UZFZ5JFySg3Z0vF1aqFyftrs3J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o: d1lo6xhdqjqx5xdzvxpfor3voofrz/ From: Zhang Yi 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