From patchwork Sat Dec 2 09:14:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baokun Li X-Patchwork-Id: 13476819 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 BEB36C10DC3 for ; Sat, 2 Dec 2023 09:10:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 869BC6B044B; Sat, 2 Dec 2023 04:10:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D34F6B0458; Sat, 2 Dec 2023 04:10:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 523EF6B045A; Sat, 2 Dec 2023 04:10:53 -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 39D696B0455 for ; Sat, 2 Dec 2023 04:10:53 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 144CC40113 for ; Sat, 2 Dec 2023 09:10:53 +0000 (UTC) X-FDA: 81521308386.01.EB3394A Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf07.hostedemail.com (Postfix) with ESMTP id 8230440004 for ; Sat, 2 Dec 2023 09:10:50 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; spf=pass (imf07.hostedemail.com: domain of libaokun1@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=libaokun1@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701508251; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Mj/XL1oO1gw3kh4x6YCx5Fd8SUUjjdIvfUWmpksvH6k=; b=Apg6BidVIHwTHImf8hf918uOHgmQQa7Y6UfIA4cSs4FQgGwGcdR1KqB7cqT+sLAejf3W8U j1bY3/1Aln4XKwrJBblES92vpN775AfHDxludLxh4G/OiQjryYMu5wZ4WJeZXQ1fnF7y3F WY58Dl4ogql5vY3nc9MIyG3NfiDWtuw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701508251; a=rsa-sha256; cv=none; b=5EoJ5xETaVHbKGqMLW8sQorhDNvQSCR8TWN+vby8+zO1nOvQRApXQZV3JFCGeeZHwfxO61 izWyEf3EjwXksd1xjVI8d4t8n5pGvdOCYpGj0nia4Zg5j+eoLISsdS6QT6oGZQa97i7BwB eA2JB03yDBeLxtsCL6zanw7xAQeyYUc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; spf=pass (imf07.hostedemail.com: domain of libaokun1@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=libaokun1@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from dggpeml500021.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Sj3vP3VCHzShK4; Sat, 2 Dec 2023 17:06:25 +0800 (CST) Received: from huawei.com (10.175.127.227) by dggpeml500021.china.huawei.com (7.185.36.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Sat, 2 Dec 2023 17:10:45 +0800 From: Baokun Li To: , CC: , , , , , , , , , , Subject: [PATCH -RFC 1/2] mm: avoid data corruption when extending DIO write race with buffered read Date: Sat, 2 Dec 2023 17:14:31 +0800 Message-ID: <20231202091432.8349-2-libaokun1@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20231202091432.8349-1-libaokun1@huawei.com> References: <20231202091432.8349-1-libaokun1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500021.china.huawei.com (7.185.36.21) X-CFilter-Loop: Reflected X-Stat-Signature: qtqqrhcutoxc579jdc1ee68xp4s9cy8o X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 8230440004 X-Rspam-User: X-HE-Tag: 1701508250-278164 X-HE-Meta: U2FsdGVkX19+voz9rH07M5fqQn29bPzvH99M6OnhD6Mm/Nij+iJ9eEQvulCZEMyrGbIYtHkTwSKWJcztPRxQrjsr8L7IhOoH+m41CKJ78sN7Qf7GBPo3OZEBprr3nKID88AImBMpRhTd6Uvua+50uHoaI2fF2i+46NTNkOCdy/fuzkiHZoSkt9f87Ct1DIhenIKErCh5q+xA2FkBiBj4RZaR4wB+lUmtfYkMNAk1JfwRYg1WX78YhMivT6FWa38bRUFW+sMJWlwQKVXWfWHdOHGs5JCdQAN+5dw1+IguNzgTjVkAWdjWOfQ6OS7BAEI8mfBn06LHr4HkiyVjcM5+mmj9eKqY3snagqO1bJfvtZNWkk8G7dpbx74ttzflY/HD9ygImd36AIMDxWlA8FjwzRIvgCzexJz4dLchmBggkv2cqHIo7IhP4GgXRE47KATsFRcTIg1VTx7S8qGy/lwJ5joxjpQrGRYGutc2twSadksQKf6KbLyjYgm1Qimygmcx1pwGtDy7g0jMmJBELthoLXq6PcrcbHsEPvfiIkVKlMXQcxDmTZR3jQdGbdD7Od3RDpyooksXuGF55LSvoZFKJU5szxHMH5xXpl+UupcuXFLMmgxOydSU+I9GLnchaH4Q8YFqp1+qUXydDfKfKxJDHCObjC+eTsFP4kVWO+kVXTy7D/i7PVqd4HXE8LvTT8LFezeXa21tVEiFFCTYXdFLYEWVtmy7/emhvHsygt+TMTDU/ov3ZWVknHe5EZwIU88mO4RKboIPHTz6TEE9A9MzLrnxgs8XVWnRnvDCT7/eYpd5HB9hVjs1pLEcrACELvN7gdhsCxnhm8yvOf5oQJ/Wqimf/Sg6MQ7Kokf2I699DTxUkXMHrxZilInhbrWQjS5+9wlCb3GJvFpG8rBoFvm+ApGXQ4dXTrDoFG7a5+oe6Fg1ooktiPk0/cGRabyKuYYNF7d/PkfVyJZzQZJCQ9d I91YMdBQ BdxuH6hv2u8AUcgvgNZ7eFDvUWrqCDFw+jgcfTr9qZE9k95j/5cYkRx8YmCCw/mZNChXz0yw5il67PSSbPiQPqCHjH+PAsCVyWXtTbtR7Oaxdu4BPMx8Jf4QwhWH6G4medNL+GL0eUunAhLX13pwMcWXTKBPG6d91Fmjl5RDsfNXoUTc4ZJE5YP244l2jfgw/YcYSBJr2JKZJ8j6OG0j8UN1pzA== 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: When DIO write and buffered read are performed on the same file on two CPUs, the following race may occur: cpu1 cpu2 Direct write 1024 from 4096 | Buffered read 8192 from 0 -----------------------------|---------------------------- ... ... ext4_file_write_iter ext4_dio_write_iter iomap_dio_rw ... ext4_file_read_iter generic_file_read_iter filemap_read filemap_get_pages ... ext4_mpage_readpages ext4_readpage_limit(inode) i_size_read(inode) // 4096 ext4_dio_write_end_io i_size_write(inode, 5120) i_size_read(inode) // 5120 1. read alloc 8192 0 8192 |-------------------|-------------------| 2. read form disk (i_size 4096) 0 filled data 4096 filled zero 8192 |-------------------|-------------------| 3. copyout (i_size 5120) 0 copyout to uset buffer 5120 8192 |------------------------|--------------| |~~~~| Inconsistent data In the above race, because of the change of inode_size, the actual data read from the disk is only 4096 bytes, but copied to the user's buffer 5120 bytes, including 1024 bytes of zero-filled tail page, which results in the data read by the user is not consistent with the data on the disk. To solve this problem completely, we should take the lesser of the number of bytes actually read or the inode_size and use that as the final read size. The problem here is that we don't know how many bytes of valid data filemap_get_pages() reads, or how many bytes of valid data are in a page, so we have to rely on inode_size to determine the range of valid data. So we read the inode_size before and after filemap_get_pages(), and take the smaller of the two as the size of the copyout to reduce the probability of the above issue being triggered. Signed-off-by: Baokun Li --- mm/filemap.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 71f00539ac00..47c1729afbb4 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2587,7 +2587,8 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, if ((iocb->ki_flags & IOCB_WAITQ) && already_read) iocb->ki_flags |= IOCB_NOWAIT; - if (unlikely(iocb->ki_pos >= i_size_read(inode))) + isize = i_size_read(inode); + if (unlikely(iocb->ki_pos >= isize)) break; error = filemap_get_pages(iocb, iter->count, &fbatch, false); @@ -2602,7 +2603,7 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, * part of the page is not copied back to userspace (unless * another truncate extends the file - this is desired though). */ - isize = i_size_read(inode); + isize = min_t(loff_t, isize, i_size_read(inode)); if (unlikely(iocb->ki_pos >= isize)) goto put_folios; end_offset = min_t(loff_t, isize, iocb->ki_pos + iter->count);