Message ID | 20231202091432.8349-3-libaokun1@huawei.com (mailing list archive) |
---|---|
State | New |
Headers | show
Return-Path: <owner-linux-mm@kvack.org> 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 <linux-mm@archiver.kernel.org>; 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 <linux-mm@kvack.org>; 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 <linux-mm@kvack.org>; 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 <linux-mm@kvack.org>; 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 <libaokun1@huawei.com> To: <linux-mm@kvack.org>, <linux-ext4@vger.kernel.org> CC: <tytso@mit.edu>, <adilger.kernel@dilger.ca>, <jack@suse.cz>, <willy@infradead.org>, <akpm@linux-foundation.org>, <ritesh.list@gmail.com>, <linux-kernel@vger.kernel.org>, <yi.zhang@huawei.com>, <yangerkun@huawei.com>, <yukuai3@huawei.com>, <libaokun1@huawei.com> 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain 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/3lZ6sWF86gW1v2vfkDBmmR7iJVvyOUs3WCxbAnxSmA0RDWU6+gj54UsulpStAyHAVgDqBwTSzh3C8Ki4ycFb8kGlmUD/Y8YNBLIo77i7Z7RqiT3ByQ3sGgKD1/zAM0m1tp8IRCM80XNxkJyuHv4a5D1jjfcZT9jSpJ7Wl/B5+0AYmXKqyVWId1B51KLLlSqLGURtYstDmG45tYX8MfySrTfMO8T8N/cXY7iJZnhLAeXGwZ3oYUEIKwgXAEB5oHKUqMF9YtzVhi5vCd1Aa/LJAf+25rnHzThRGSlZXI+xockqix1EeWWbqpwVTf7wcW6CV8gC70EJEnjFS1/Vg0GT9qHSMEofz3ql4cieWzhqJ1WZPhD4QD3Ufl1O8sXlD94sh2/3lc6xTQJftXFIPwgJqxWtaeGam3o92g5uz+XQdaDD66j0RjXCCP0y3Ue+LGo2EZ8Whc84ffrrp4Njgnux/d+4jE9k8AOtc1GpP5pyvNm+IbDGCl1NQAilqfLrM53eBGHkLSdMp98zfAHhcePx+ZUAzSuabOZR90kwN48FdzFO+m2d3ay4VhVztr95IdKxHfAJHiTCwS6UEnfj4HYqcfiJ9HSWXxT4p/mBcSo9BcgvF1n9v8l0xwsr54vf76m7hAQeKm88BuOs80oz5kQ8pgOjBL4zS7WMbSfZy7rHWHvisIpMcyJf9zbNAW4y7nSXqcarWVuIwKFMpjhHCIbToZAgoda3WG6GrOMi0mKqTl3Cy0MSW6r9qN2WbjnhzgzZttN35sK+Br1bo3LpItv0+925QtP31owGzFaKK4GtUpCTYtkVpSrm7qkp2futELWS48D4yHZVGg7VrIoZSuXlO0whbUkDnHKik3q R6/mkt5M 7UrsHEakjFYoTFINXaSIrDL8VO3Mx4IG300aziHj6RcLgxVnHxXiz7jnpb4vcxdL0zdP/chtiuZOJAwGffjfmxH7gi8WvtEiTGPy/ChMB0d+rIbbSTSHCtQoG61Qfqf7KT8uHnwkXJUfwwfYC61xXEIkh8OBSWAd5jrxuIsXjU5FGtshIwiC4nrvYGNOw4xXsjhgN9pSPR+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: <linux-mm.kvack.org> List-Subscribe: <mailto:majordomo@kvack.org> List-Unsubscribe: <mailto:majordomo@kvack.org> |
Series |
mm/ext4: avoid data corruption when extending DIO write race with buffered read
|
expand
|
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); }
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 <libaokun1@huawei.com> --- fs/ext4/file.c | 3 +++ 1 file changed, 3 insertions(+)