From patchwork Wed Apr 10 13:27:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Yi X-Patchwork-Id: 13624536 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91D7FCD128A for ; Wed, 10 Apr 2024 13:37:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CCDAD6B008C; Wed, 10 Apr 2024 09:36:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B54036B0089; Wed, 10 Apr 2024 09:36:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 957E66B0095; Wed, 10 Apr 2024 09:36:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3C3516B0088 for ; Wed, 10 Apr 2024 09:36:57 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C052B1C099E for ; Wed, 10 Apr 2024 13:36:56 +0000 (UTC) X-FDA: 81993722832.02.F94CD54 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by imf16.hostedemail.com (Postfix) with ESMTP id 38CD018000F for ; Wed, 10 Apr 2024 13:36:52 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; spf=pass (imf16.hostedemail.com: domain of yi.zhang@huaweicloud.com designates 45.249.212.51 as permitted sender) smtp.mailfrom=yi.zhang@huaweicloud.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712756215; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+W5KEpg2kUY6jwif517hChT/8NlMh2IK3EzSMnHViHw=; b=wsyRqEPZmyUgfqgkXiFmmPNnmRjew3QJdedz7/yL7LeE98Pc2Qg3+CcsuIDot5HL7HrPwR DrOQAxsoI7kiEZd6stt0+ONZgPBT4s0drD/ZtGxNAop6jTPX3Df9PH8l2ZpW6XjGgl5pZq rNAUrj2pTtIrKCYAdBU8cnFJTylapGg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712756215; a=rsa-sha256; cv=none; b=wZfz8uVsoYbcsw+fJ2DW0KUYXQW5y/HU9u2/P5qxhnh1SfBE1h6y6CEKTtBbeYIzg/7DHJ jJM+WU18QKFXNb+m+mhHsSFigvTzWM9nxabP2PLalNQAATJB1bsZmZnG6lDGFvAqiipklV eJGpZZq0YcjqtBidOK9zw84i/1ui7dE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; spf=pass (imf16.hostedemail.com: domain of yi.zhang@huaweicloud.com designates 45.249.212.51 as permitted sender) smtp.mailfrom=yi.zhang@huaweicloud.com; dmarc=none Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4VF3lD3mlxz4f3m7L for ; Wed, 10 Apr 2024 21:36:40 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 315281A0D65 for ; 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> MIME-Version: 1.0 X-CM-TRANSID: cCh0CgAn+RHolRZmeCl4Jg--.8806S6 X-Coremail-Antispam: 1UD129KBjvJXoWxurykJF17GFWxuF48Jw1DZFb_yoW5Grykpa 9xCF15Cr48Wwn7Wa93XF12vr1rWa1rJrWUKFZxKr1UZFZ5JFySg3Z0vF1aqFyftrs3JFsY qFWjqry8ua1UKrDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUP014x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v2 6r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_GFv_WrylIxkGc2 Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_ Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJw CI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUvcSsGvfC2KfnxnUUI43ZEXa7VU1489tUU UUU== X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ X-Stat-Signature: kcw5zhwjj6p6smpp5dxy5iw49yk4gk48 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 38CD018000F X-Rspam-User: X-HE-Tag: 1712756212-177529 X-HE-Meta: U2FsdGVkX1+1hc6IL0OUqLSkYl7qVQ0qXyIhTHPTxouaG81dCstbVrql3ulLM/NcVa2o1BtSaNPWw4N20g83PT2e7z+8h2WqN8RDcB/xqp09Mo2us1JNjzqWWmcg1wIXRF9fycGTowwHCG1REVV/jiFIl4IL/1rLvaUXJt6znhSUxUyZ0eeOiZqZnT+MtPFqpM0LRNDBlhHAAdGOkZolOottneO64zZZV1aarNUcj7+nFEsf/q3M4aj2yudeX982vZL+b4JvZ537APom9mXQOS99i2ruxoVPbtY983hA9uWY1oo+4MwJYe41cd4pYd0RpOl3/luX5yASIMYsyJIyOqgbcMO+PZ7jXS0kIUBsVPthk2uiv5gEUnR0DBABVPcF6gqV8zjpMpI0oeZe8tZwtK5HzNNYNWCNtDhIe3iT1r2ezW37MJOoMKEQAroT5Lf60YYeXkvtwMMt/OZM1OIBIhRpe9NbP+YKpTu3aSavJKU37UThw4rNfvuoqMaYverAY/vRtrOBScwU+R0YIVa2zceCO5s+4f3GjjmJOVMBWNVlXXinbaQ34dJTHs8lcb4QG2lLeeeHIroQmH5OeQthv8CAOnM/prNcBO+2+WYPyASaqEYfV4UCMQ0iaqyTatyWsApR+PmA3/0RuFm4sTFS3AykcAeh0bhhDimmUmeTly56i/s9RhhR0MeQkrw9qfHX1H4L9d96a6gAmx0PVYy4HUX885taHhWampzHt8lLLWA8M2UtsGAvqNwkP/nwLVmg5q/+Tznf4HGkICkbh6/8Cp1vvT9HFTnZi9Sx5jagxayWLLClYR4dBlOUDY311yNgr5BmUKASZRc+ZTobYkYAgkEHAmn8Dauv9TAsOSPjzv30Pdawch67ydmU43l41iS0oZ6KevcDo1KIouMSoX8Q5OkKmso+2JF9Z4QLppKEd4kGqdGN9wwQcgnmZSGis8q5Ssb6yW4JHbhH6g6EcOV WC2+yPbv /O5+mFleAw66vbydoFm7UVGHn5wjwaLlGPs2JDmaszYQ/FbdmllLZNFJzRvr6nEuPhUyyWKEnDVivORZzEDdysJjISEF64hKzewGvXRBLfowAfgeklumHFwn5NAV4UJsZFfKgrQm5/H6BYMusERss9N5H8PtHnr6YV0gcoTIDouyK4r6CP/7OmeV0LkjKuAePKC7P8jnnw8TZQqZeZkG2P0x9nSPg1mu6Q8hmC42XZyOiM7GhJ2n8b16F1/iF1YDnqrFMn4D07S0XAitLlrp+CoOiHsjpIt6HO+/4E9P5aj693xV+hEY6m2dK3A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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)