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); From patchwork Sat Dec 2 09:14:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baokun Li X-Patchwork-Id: 13476820 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 C874BC4167B for ; Sat, 2 Dec 2023 09:10:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C046A6B0458; Sat, 2 Dec 2023 04:10:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BB57F6B045A; 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 A32446B045D; Sat, 2 Dec 2023 04:10:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6F9BF6B045A for ; Sat, 2 Dec 2023 04:10:53 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4E0DF1A0109 for ; Sat, 2 Dec 2023 09:10:53 +0000 (UTC) X-FDA: 81521308386.25.18382C5 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf24.hostedemail.com (Postfix) with ESMTP id B8308180020 for ; Sat, 2 Dec 2023 09:10:50 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of libaokun1@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=libaokun1@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=iif1XoXNzkKrShWPU5sx4DaXx2NraIUn+NNfm0O+3Mo=; b=U3Ao5+JnuRrRUXYEX9QB19TagH2E7+r9Xi2TKrtetskaCy4voymDgdPHFoBbnV7IOCM5TO uRUsTB+IFCHQKq/SwqZ0H3IQYTNYv80yoLh77QUU7iKmm3mb/fDOzI5pZQULNg0Rz8CKCG yJbRWV+6e6eRYdzwULi68SA7Z0jEUiw= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf24.hostedemail.com: domain of libaokun1@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=libaokun1@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701508251; a=rsa-sha256; cv=none; b=JS56l0F5FgLElUGsYr6Y9mhdZ89QMc0TALFKXin7cBV3MwMR+Bwuzi8GhcOTSIZnUYlkHs sLHT5ZWsdrlWeVCHqVk+mQaeL7hVbRljPSVYt7u2x0ukFHYK+fvzJyU5FKObsEWbCCp6Mc HqQwgofuwouHPG5ICSWZM7aFphqHv/Q= Received: from dggpeml500021.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Sj3tm01XNzMnZj; Sat, 2 Dec 2023 17:05:52 +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 2/2] ext4: avoid data corruption when extending DIO write race with buffered read Date: Sat, 2 Dec 2023 17:14:32 +0800 Message-ID: <20231202091432.8349-3-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-Rspam-User: X-Stat-Signature: 8d7ztxcxuowtknu4gh6m495p5gm75mh9 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B8308180020 X-HE-Tag: 1701508250-963316 X-HE-Meta: U2FsdGVkX1+jeLbww2aF9gqp3PXT6cRUqpLgaFMVN/9su7E99/f3aCdWgq3Kp9zUlG0unUlhlSCf7J2xY2h7jWzzU9i/O9P/50tFtvSwlC1odcG/3lZ6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PR+ezglBFxwYkUsmwlA== 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: The following race between extending DIO write and buffered read may result in reading a stale page cache: cpu1 cpu2 ------------------------------|----------------------------- // Direct write 1024 from 4096 // Buffer 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 i_size_read(inode) // 4096 filemap_get_pages ... ext4_mpage_readpages ext4_readpage_limit(inode) i_size_read(inode) // 4096 // read 4096, zero-filled 4096 ext4_dio_write_end_io i_size_write(inode, 5120) i_size_read(inode) // 5120 copyout 4096 // new read 4096 from 4096 ext4_file_read_iter generic_file_read_iter filemap_read i_size_read(inode) // 5120 filemap_get_pages // stale page is uptodata i_size_read(inode) // 5120 copyout 5120 dio invalidate stale page cache In the above race, after DIO write updates the inode size, but before invalidate stale page cache, buffered read sees that the last read page chche is still uptodata, and does not re-read it from the disk to copy it directly to the user space, which results in the data in the tail of 1024 bytes is not the same as the data on the disk. To get around this, we wait for the existing DIO write to invalidate the stale page cache before each new buffered read. Signed-off-by: Baokun Li --- fs/ext4/file.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/ext4/file.c b/fs/ext4/file.c index 0166bb9ca160..99e92ddef97d 100644 --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -144,6 +144,9 @@ static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to) if (iocb->ki_flags & IOCB_DIRECT) return ext4_dio_read_iter(iocb, to); + /* wait for stale page cache to be invalidated */ + inode_dio_wait(inode); + return generic_file_read_iter(iocb, to); }