From patchwork Sat Jan 27 01:58:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Yi X-Patchwork-Id: 13533895 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 8CE35C47422 for ; Sat, 27 Jan 2024 02:02:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 82C2C6B0087; Fri, 26 Jan 2024 21:02:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 68B316B0092; Fri, 26 Jan 2024 21:02:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B9046B008A; Fri, 26 Jan 2024 21:02:50 -0500 (EST) 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 041686B0092 for ; Fri, 26 Jan 2024 21:02:50 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 96D0F80821 for ; Sat, 27 Jan 2024 02:02:49 +0000 (UTC) X-FDA: 81723442458.28.2A4F4E4 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by imf07.hostedemail.com (Postfix) with ESMTP id 479D44000E for ; Sat, 27 Jan 2024 02:02:44 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf07.hostedemail.com: domain of yi.zhang@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=yi.zhang@huaweicloud.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706320967; 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=LDz98XSxYiJRQC1cVb6S82GBYBYojLb8QpdBTLPL8OI=; b=TUV+F+4hvwmkTiHmTKc0a1+NhYrJzDOJmXV7iPAoVINkN1WHLmjqiU/+JWWR95+/I0cNml c0hRrR30tt5dVBqsHqg8LfyeCq/uDjNYFCnWNA4DFgvHLVjBuebANmZGdbsZEc/IYPzdFr C9bVFbzF/W0PMzSZecghXTVwkvG2zjg= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf07.hostedemail.com: domain of yi.zhang@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=yi.zhang@huaweicloud.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706320967; a=rsa-sha256; cv=none; b=Ig4jU0EjDTGV7+Bt4C3J2OZzdz+51+NQkN52z7PfeatdHT8UpV77kGAigrkLzoE33yJHEV cAvDS2uHwjhSXmiRDzmOUPNK81j+DjHQWDbDpEwzeVvPO/zcKx4v0NJDm5Gh52YSLfH6Py QVSS12UlWEj2HHHti9BSPQ2qpT2sKr0= Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4TMHrX4dRhz4f3lwH for ; Sat, 27 Jan 2024 10:02:36 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id A1C621A0272 for ; Sat, 27 Jan 2024 10:02:40 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP1 (Coremail) with SMTP id cCh0CgAX5g40ZLRlGJtmCA--.7377S6; Sat, 27 Jan 2024 10:02:40 +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 v3 02/26] ext4: convert to exclusive lock while inserting delalloc extents Date: Sat, 27 Jan 2024 09:58:01 +0800 Message-Id: <20240127015825.1608160-3-yi.zhang@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240127015825.1608160-1-yi.zhang@huaweicloud.com> References: <20240127015825.1608160-1-yi.zhang@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: cCh0CgAX5g40ZLRlGJtmCA--.7377S6 X-Coremail-Antispam: 1UD129KBjvJXoWxJw4DZFy8uF4kXr1fZry3XFb_yoW5Zr4kpr ZIkFyfCr1UW3ykuayIqr17XF1xG3WUJFW7GFZxGF4UZFyUAFnaqF1jyF1aqFyftrZ7AF4Y qFW0qry5uayUCrDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPj14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v2 6r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_GFv_WrylIxkGc2 Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_ Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8Jw CI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjfUoSdgDUUU U X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ X-Rspamd-Queue-Id: 479D44000E X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 1fcxerc4xt681dizjob1gfa4xq95rfcj X-HE-Tag: 1706320964-712304 X-HE-Meta: U2FsdGVkX19hUdQS1XUjga/CvAnn3dLVu6ovFNkwTazMA6GbVy3fNXp9OYfSQatmQA8llq+nvrmlDd5QMPGX9kjSVqan8phSQh4O6H0pnz8EOcmLLHO9gGaBD+1jQ2RK8S8xc6YhNzFH8ISv1WYbaGOx3G6fnTuqlRtepDI/ZLnBnhDobw3AOnG94JH2UeWrOa51yf/G4SrkpCOs2rnSnyGuWqo8SXlZEAo8bVwlow2m+9aHbwLML6agixm8OVifLeB4ppb+u38e0jDdAvfY94QtD0BDv1vboGth8M+aahvQw6hiQ1Q4Uc/QbHMxvgoPjjuse+shB/7jOpcIvn/DPdRjWNHMqE4p4Wi9gkeFZvBndFI/adfQwxhw/6tw9Cm3LQFl2zntmKmpP6cLhWdlqIHBSbGM1+B6q45+Gr8zl7o9HKqFCECSFzs5Kmz9OpK4ujlYs8KCH33cn9sIk6IlLQYt9nkzQoC6xj7oVVsL0mvqNcSq95HwA8mCbGOD3T0+kM4oPfgxIrnjqpmvd+9HqPWSxmV+soCstT6vVQDS1KCvpWfz9pmJA/ttq0ogQOQKSVhuZL2M8byPuj/Fw1xf5eT9TYmDDieY5K7RnLEZbB0m5X93WQfRLUjqzDhUWhEvGE0CWl3SlhstVMy/B7saDeV/f8ZutZQ/Ljx7R7LLI9PVAqiJsAYWneq4YlN6gRH4tgoo4nQ+UhzqR6b8td7vX0BsAoslthBf19sp3svEXDev0w6f5ct4Rvf0ClBlOgxyLa5r8Ygdr+ccg/vnL8uto+30tOHOHs877dHW2Tjo4+K6/W1QeuA1qQRd/uJ8mQ30V6EkXVK7A5NkKUJau/XlXJDClHzLh3VUmPYz+K10AgPhXsd9//i90zZVXReTtV76koEhupoJuUcFhsJ+P+QuFO2OzIdQkCy7fW4HEbSzmXsv9Woykq3r3CsyRylZzQrEKLGSgmi7Se7H2LmDyg1 tSUhJtZ5 Sf8dCXtqIX8PcqfpddITKIiaHH1SsWqaprBGlOjbEL0pb8WPq6IH9XVoG2SQjheYy8tkuLwjibZan5jTBq/G3XI/ZwgGWMCDvoKvSfptDVq2HEgEjuPPMbfYb2fuudRaSB6eMx66sRY3BF15FVAyiaCskB/nzc8LjGavHKKlwtub589HY+fU730poJJAfM0jzXkPEE3QPumjTG6stb/U/lzXOuVCX0N0VcHlkBqawIQpTR3F7lnWcISvOQoWvYeHTkwKfrjqUocHQMLVvYV7gxgZD7jaYdgxyaipc9yMAW26vV9rOH2il02gVZA== 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 ext4_da_map_blocks() only hold i_data_sem in shared mode and i_rwsem when inserting delalloc extents, it could be raced by another querying path of ext4_map_blocks() without i_rwsem, .e.g buffered read path. Suppose we buffered read a file containing just a hole, and without any cached extents tree, then it is raced by another delayed buffered write to the same area or the near area belongs to the same hole, and the new delalloc extent could be overwritten to a hole extent. pread() pwrite() filemap_read_folio() ext4_mpage_readpages() ext4_map_blocks() down_read(i_data_sem) ext4_ext_determine_hole() //find hole ext4_ext_put_gap_in_cache() ext4_es_find_extent_range() //no delalloc extent ext4_da_map_blocks() down_read(i_data_sem) ext4_insert_delayed_block() //insert delalloc extent ext4_es_insert_extent() //overwrite delalloc extent to hole This race could lead to inconsistent delalloc extents tree and incorrect reserved space counter. Fix this by converting to hold i_data_sem in exclusive mode when adding a new delalloc extent in ext4_da_map_blocks(). Cc: stable@vger.kernel.org Signed-off-by: Zhang Yi Suggested-by: Jan Kara Reviewed-by: Jan Kara --- fs/ext4/inode.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 5b0d3075be12..142c67f5c7fc 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -1703,10 +1703,8 @@ static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, /* Lookup extent status tree firstly */ if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { - if (ext4_es_is_hole(&es)) { - down_read(&EXT4_I(inode)->i_data_sem); + if (ext4_es_is_hole(&es)) goto add_delayed; - } /* * Delayed extent could be allocated by fallocate. @@ -1748,8 +1746,10 @@ static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, retval = ext4_ext_map_blocks(NULL, inode, map, 0); else retval = ext4_ind_map_blocks(NULL, inode, map, 0); - if (retval < 0) - goto out_unlock; + if (retval < 0) { + up_read(&EXT4_I(inode)->i_data_sem); + return retval; + } if (retval > 0) { unsigned int status; @@ -1765,24 +1765,21 @@ static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; ext4_es_insert_extent(inode, map->m_lblk, map->m_len, map->m_pblk, status); - goto out_unlock; + up_read(&EXT4_I(inode)->i_data_sem); + return retval; } + up_read(&EXT4_I(inode)->i_data_sem); add_delayed: - /* - * XXX: __block_prepare_write() unmaps passed block, - * is it OK? - */ + down_write(&EXT4_I(inode)->i_data_sem); retval = ext4_insert_delayed_block(inode, map->m_lblk); + up_write(&EXT4_I(inode)->i_data_sem); if (retval) - goto out_unlock; + return retval; map_bh(bh, inode->i_sb, invalid_block); set_buffer_new(bh); set_buffer_delay(bh); - -out_unlock: - up_read((&EXT4_I(inode)->i_data_sem)); return retval; }