From patchwork Wed Apr 10 14:29:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Yi X-Patchwork-Id: 13624701 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 CA445CD1299 for ; Wed, 10 Apr 2024 14:38:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B26896B0093; Wed, 10 Apr 2024 10:38:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A40BA6B0096; Wed, 10 Apr 2024 10:38:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 891866B009B; Wed, 10 Apr 2024 10:38:37 -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 3D0D76B0095 for ; Wed, 10 Apr 2024 10:38:37 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E650C1C08A7 for ; Wed, 10 Apr 2024 14:38:36 +0000 (UTC) X-FDA: 81993878232.14.7061CCE Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by imf08.hostedemail.com (Postfix) with ESMTP id 6C4EE160012 for ; Wed, 10 Apr 2024 14:38:31 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; spf=pass (imf08.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712759914; a=rsa-sha256; cv=none; b=Jn+1+DiJet/bS3s4pXWr7gKKFRaNxnaqxS6UMXbcMyzuT1Abdx9mY1X9LjCXSahhLERZ1R FomdbUGhreNnn2UOb7kTeAe5/KDoWY1tmxzYdN2cPvNj8CDXyapeZYNgnGoeCJAnomnTgK vKlNj7MYom9EIZFTRQHpH7nCgZAHRkY= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; spf=pass (imf08.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=1712759914; 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=E+LCOdveXV8/72NAEsC9lRrHF1ySpcdmke82VncMaCkSDDssoyX3QBB2Ul7GtxBqG/SCP5 PlkDwMxFjv/qidoHlWCVowncYJgF5KspbBQ1di4bE1YPaB0C8+GNzyOSYImSSU2jUAcbtb jZpr4XcMGjxzFMSS7oaoYqsNjp3JOzc= Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4VF56Q4Rmyz4f3jqP for ; Wed, 10 Apr 2024 22:38:22 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 0C7231A0568 for ; 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> MIME-Version: 1.0 X-CM-TRANSID: cCh0CgAX6RFSpBZmcwR8Jg--.63000S6 X-Coremail-Antispam: 1UD129KBjvJXoWxurykJF17GFWxuF48Jw1DZFb_yoW5Grykpa 9xCF15Cr48Wwn7Wa93XF12vr1rWa1rJrWUKFZxKr1UZFZ5JFySg3Z0vF1aqFyftrs3JFsY qFWjqry8ua1UKrDanT9S1TB71UUUUUJqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUH214x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAa w2AFwI0_Jrv_JF1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrV C2j2WlYx0E2Ix0cI8IcVAFwI0_Jrv_JF1lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE 7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lFIxGxc IEc7CjxVA2Y2ka0xkIwI1lc7CjxVAaw2AFwI0_GFv_Wryl42xK82IYc2Ij64vIr41l4I8I 3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_Jrv_JF1lx2IqxVAqx4xG67AKxVWUJV WUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r4a6rW5MIIYrxkI7VAK I48JMIIF0xvE2Ix0cI8IcVAFwI0_Ar0_tr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4UJV WxJr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r4UJVWxJr1l IxAIcVC2z280aVCY1x0267AKxVWxJr0_GcJvcSsGvfC2KfnxnUUI43ZEXa7sRieOJ5UUUU U== X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 6C4EE160012 X-Stat-Signature: drze47zs5fkw8rscidfdkwuzr9mfprso X-HE-Tag: 1712759911-464358 X-HE-Meta: U2FsdGVkX19zQF3qKmah+1N+UMcO+lv+CzotBim2HsS3Of0bM+0KhrBb1g+Mygr7Fi+ii05Cm9Oc3KztKh69dA0tnh+v75cjIFY58/V9fdVPfv3535JdOgk4r9k+KMvAAmzDQTDL85ucmQEFye1to1dalfdjlfGbpSAdny3RwW+O6WjohHd6UzsyQcDmDYOPEmuXoD0m13+ToSi5CFY0a4QMo/5qZf12K2IgGPSvW1a5xf62pmSC1/JEv3i1WnY8xzZ7BLwR/I26mkD+PSpaoR/YToaHziY2tsqEFY3munBXe33UlZ1GHU8qGeQ5rszyT1RBOTIOOn0tvi1cu5/pENY+FwvvsOQoz87Ls9amVnHEmzyPWrLhjnc3zA3YATPXmXi04tW/cgCgx7eb+NMf37ICKxf1X2LJGkdwYmLLX4GBIIG8VGVJhOIzQQ0xJsr2iYgl8BPlm4EZ6ME6q2jinJs41bgWjwcjABi5/RQpSwcZlckS9DRnUSOhSpAenzLNCkrsJSVeI8EgQRh1/4QQnzx6cFUZBld2rCYHrslOaGFh05hVIOfJOXpXDSK2R0N9TKJWL2zYkBQM967Je86KUmyfJmt4s8LJVbx8gr79mey8cNMQAyVOqiXy+KqybuwU+aYVjmecbWklD9ew9ut57X9QjG7ORa0qoMZuIN6uLG3BOgNOXmpS6NxwvKwtHwYCR3/OtoTLKHCjoN+EV1BT2aMxahoAnZqteWOHV5sub3z/xBv4CPeBzAZYr+EqaOes4BFV9N0B8RPvIEKJyNDFwjJYdBsoVjSHHmROjU1z6w844ElqS/NaxilvlC7lsU9+4X7CbVaCjU49lLs9haZ7l9QJEwIjO6eHuvWiNEC/j3iy0eetIaJ28meBpj3bikStfGxVrKGUXtfjSq94r6gxuy6niKAfzdqA+rFUhy2M4DGXuzW3J075z4Y/UmmBUeofaGxgWfif5zBSdV1mTKD Cz1OXzUD mnMas 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)