Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp3124320lqo; Tue, 14 May 2024 23:38:50 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWzDk7z3PT7Z7CVUfx9l1D5lI0WuMXcgRp0B4DorER1Vk42cdOVS1rZsY+OhZvGHVvWget1tVR6Xqmiq0NCVq38QqNXthuxxdkl1dpuqA== X-Google-Smtp-Source: AGHT+IFvskze73/M+cveXA3hzJiwTYkt45R52sP2nF9R4LwOSKSFpIOmGZXNX8GjmxYI3sQzKnSJ X-Received: by 2002:a05:6a00:2385:b0:6f3:c10a:7bf3 with SMTP id d2e1a72fcca58-6f4e01b9445mr17967065b3a.0.1715755130536; Tue, 14 May 2024 23:38:50 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715755130; cv=pass; d=google.com; s=arc-20160816; b=D86X1v6IbVSMUx8jalV7E1uv4kbBvWFb8g2+C6mLqgbFn5CKbQ1/CzR7FSenLe8L+S Fp9Jf2ZHvpq9HbivypgUkjyuo2zpRQ8r6qniSqhq4w9e+eVx5/nRlYW/WtIToEy7G4Ry vaApDMyl9+VJljxAfjGxHqpFKF5XmcaR5wNhW0oUjBqU5qJEThxEW182JzjBEH6cJ67H AMr69ljTUcgelrjwSIV7jGJMiXqazxLNkcDSX+CVtw0gLLX7BpbsyRUFc3UGkIcCd3lu yOXoUl3d5TowVUbqDuGLe9bRH4ik7Ia3uU0B6K2Jf10ATXSmabGbTRlvGWWBDv7lorFj HDKQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=o7jgdSrY+9B9aEOXq5wEUT5KkFZIpbQond31E/9LuBw=; fh=1D7eQ/0rc+iU6BAcs/KVKWP4G7WgjJ9VzUdfNwtKJ+I=; b=ORJ+K7MStYqop1pZ30SHeqPLY41lWcngMwsuT53HPc16kV+bI1ZBggVbBtEU303RJ4 DL18cw1YWpqwvdoaASqrjYiwLeg1peYKcsdxQxslKqZV46dcJF5ckhTNZkVIoP6LlsvA tLJoAqdKLYY/+g6IkM/smqJpUumJ6vpax5xrrvtCj4OCCXC2uYKelnQKsJI6IkE55yNL 4/7CU+OLNMZBVPRwcazir5DqLsJD5KJVd3r8MM1xFANyf2oropBpNYkkoFHW2VHT5rf1 mjR/N6RmTN95MDNak58s7OC0oSB+YLghtbDxJJBhWRIAxfgev5WYk8KDc6hVtuQzFdvA SxEg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kw38awke; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-179523-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179523-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d2e1a72fcca58-6f4d2a917c2si12905911b3a.98.2024.05.14.23.38.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 May 2024 23:38:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-179523-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kw38awke; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-179523-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179523-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D2EA128167C for ; Wed, 15 May 2024 06:38:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 273BF3D541; Wed, 15 May 2024 06:38:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kw38awke" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2295F3BBDE for ; Wed, 15 May 2024 06:38:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715755124; cv=none; b=YdlUwkujM7L7fsfNP7rdSlpJp47MmrxP/kMoP1jW1E6NeGjzyE/hFIpWqBCtflKMIe1eJ3THHkh+LROItosSkZrcKyIPtUsq0QCu6SPLxo1xFmsJFm0kqZ2OAg+zGRYD9FfSF9fbQFi2k6cS18YowUgGwisV7DqXZLPwrBPy9SI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715755124; c=relaxed/simple; bh=DUBK5FSNdWNxIcO5szuLJHHIpvf4ofWE4u9LBUSeSS0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Zei1EyFWvyJnkjBj5KzL0n6NiyzO8dM+TF14pzZHjuVd7WKc05aTuDpvhFo6/Kx2hLbYIpoF1sfmd3sVfRywyhuZoYF+UhkqUjeXwvetWJ4tQGeHFtCrtSdAk95C8nJ13Z3Q0rd0ZS8FrjL/d7lvHY9zPVXuJ31Xc6oDgNNoN4I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kw38awke; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A090DC116B1; Wed, 15 May 2024 06:38:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715755123; bh=DUBK5FSNdWNxIcO5szuLJHHIpvf4ofWE4u9LBUSeSS0=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=kw38awkeWt7DClqOHsChHLNDfetR+rpkBv+orOciCmrKvdkwDHPf7OujHxpx2EKAj 98n7e5GAEVyEPuQ4kPOaU7KbCmX3XVZfVe9MdfDgqUX5Jwh8RtKbV7TzGC5tV8yYCl QGgb/n8B3o8SXbNkOQiKagom5oRlwAHyuxtY/GZXLjpxsT0psyK/GgVVCsWmKQ/zAm viUXgxZZFYISGJgASO4r+dhIncFalX47rtSeXV0hN2cVrN/+qdWvdM6Xyz2ssN9bGR KBY+PmXmx/50GaUpkAiREXmC75q3ufVZbxCFjS3B9ZAPXIkAmVvV9kM85q0nIJJXG/ 9/9OIG00LuvQw== Message-ID: Date: Wed, 15 May 2024 14:38:39 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] f2fs: fix to avoid racing in between read and OPU dio write To: Jaegeuk Kim Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org References: <20240510023906.281700-1-chao@kernel.org> Content-Language: en-US From: Chao Yu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2024/5/15 12:42, Jaegeuk Kim wrote: > On 05/15, Chao Yu wrote: >> On 2024/5/15 0:09, Jaegeuk Kim wrote: >>> On 05/10, Chao Yu wrote: >>>> If lfs mode is on, buffered read may race w/ OPU dio write as below, >>>> it may cause buffered read hits unwritten data unexpectly, and for >>>> dio read, the race condition exists as well. >>>> >>>> Thread A Thread B >>>> - f2fs_file_write_iter >>>> - f2fs_dio_write_iter >>>> - __iomap_dio_rw >>>> - f2fs_iomap_begin >>>> - f2fs_map_blocks >>>> - __allocate_data_block >>>> - allocated blkaddr #x >>>> - iomap_dio_submit_bio >>>> - f2fs_file_read_iter >>>> - filemap_read >>>> - f2fs_read_data_folio >>>> - f2fs_mpage_readpages >>>> - f2fs_map_blocks >>>> : get blkaddr #x >>>> - f2fs_submit_read_bio >>>> IRQ >>>> - f2fs_read_end_io >>>> : read IO on blkaddr #x complete >>>> IRQ >>>> - iomap_dio_bio_end_io >>>> : direct write IO on blkaddr #x complete >>>> >>>> This patch introduces a new per-inode i_opu_rwsem lock to avoid >>>> such race condition. >>> >>> Wasn't this supposed to be managed by user-land? >> >> Actually, the test case is: >> >> 1. mount w/ lfs mode >> 2. touch file; >> 3. initialize file w/ 4k zeroed data; fsync; >> 4. continue triggering dio write 4k zeroed data to file; >> 5. and meanwhile, continue triggering buf/dio 4k read in file, >> use md5sum to verify the 4k data; >> >> It expects data is all zero, however it turned out it's not. > > Can we check outstanding write bios instead of abusing locks? I didn't figure out a way to solve this w/o lock, due to: - write bios can be issued after outstanding write bios check condition, result in the race. - once read() detects that there are outstanding write bios, we need to delay read flow rather than fail it, right? It looks using a lock is more proper here? Any suggestion? Thanks, > >> >> Thanks, >> >>> >>>> >>>> Fixes: f847c699cff3 ("f2fs: allow out-place-update for direct IO in LFS mode") >>>> Signed-off-by: Chao Yu >>>> --- >>>> v2: >>>> - fix to cover dio read path w/ i_opu_rwsem as well. >>>> fs/f2fs/f2fs.h | 1 + >>>> fs/f2fs/file.c | 28 ++++++++++++++++++++++++++-- >>>> fs/f2fs/super.c | 1 + >>>> 3 files changed, 28 insertions(+), 2 deletions(-) >>>> >>>> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h >>>> index 30058e16a5d0..91cf4b3d6bc6 100644 >>>> --- a/fs/f2fs/f2fs.h >>>> +++ b/fs/f2fs/f2fs.h >>>> @@ -847,6 +847,7 @@ struct f2fs_inode_info { >>>> /* avoid racing between foreground op and gc */ >>>> struct f2fs_rwsem i_gc_rwsem[2]; >>>> struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */ >>>> + struct f2fs_rwsem i_opu_rwsem; /* avoid racing between buf read and opu dio write */ >>>> >>>> int i_extra_isize; /* size of extra space located in i_addr */ >>>> kprojid_t i_projid; /* id for project quota */ >>>> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c >>>> index 72ce1a522fb2..4ec260af321f 100644 >>>> --- a/fs/f2fs/file.c >>>> +++ b/fs/f2fs/file.c >>>> @@ -4445,6 +4445,7 @@ static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to) >>>> const loff_t pos = iocb->ki_pos; >>>> const size_t count = iov_iter_count(to); >>>> struct iomap_dio *dio; >>>> + bool do_opu = f2fs_lfs_mode(sbi); >>>> ssize_t ret; >>>> >>>> if (count == 0) >>>> @@ -4457,8 +4458,14 @@ static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to) >>>> ret = -EAGAIN; >>>> goto out; >>>> } >>>> + if (do_opu && !f2fs_down_read_trylock(&fi->i_opu_rwsem)) { >>>> + f2fs_up_read(&fi->i_gc_rwsem[READ]); >>>> + ret = -EAGAIN; >>>> + goto out; >>>> + } >>>> } else { >>>> f2fs_down_read(&fi->i_gc_rwsem[READ]); >>>> + f2fs_down_read(&fi->i_opu_rwsem); >>>> } >>>> >>>> /* >>>> @@ -4477,6 +4484,7 @@ static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to) >>>> ret = iomap_dio_complete(dio); >>>> } >>>> >>>> + f2fs_up_read(&fi->i_opu_rwsem); >>>> f2fs_up_read(&fi->i_gc_rwsem[READ]); >>>> >>>> file_accessed(file); >>>> @@ -4523,7 +4531,13 @@ static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) >>>> if (f2fs_should_use_dio(inode, iocb, to)) { >>>> ret = f2fs_dio_read_iter(iocb, to); >>>> } else { >>>> + bool do_opu = f2fs_lfs_mode(F2FS_I_SB(inode)); >>>> + >>>> + if (do_opu) >>>> + f2fs_down_read(&F2FS_I(inode)->i_opu_rwsem); >>>> ret = filemap_read(iocb, to, 0); >>>> + if (do_opu) >>>> + f2fs_up_read(&F2FS_I(inode)->i_opu_rwsem); >>>> if (ret > 0) >>>> f2fs_update_iostat(F2FS_I_SB(inode), inode, >>>> APP_BUFFERED_READ_IO, ret); >>>> @@ -4748,14 +4762,22 @@ static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from, >>>> ret = -EAGAIN; >>>> goto out; >>>> } >>>> + if (do_opu && !f2fs_down_write_trylock(&fi->i_opu_rwsem)) { >>>> + f2fs_up_read(&fi->i_gc_rwsem[READ]); >>>> + f2fs_up_read(&fi->i_gc_rwsem[WRITE]); >>>> + ret = -EAGAIN; >>>> + goto out; >>>> + } >>>> } else { >>>> ret = f2fs_convert_inline_inode(inode); >>>> if (ret) >>>> goto out; >>>> >>>> f2fs_down_read(&fi->i_gc_rwsem[WRITE]); >>>> - if (do_opu) >>>> + if (do_opu) { >>>> f2fs_down_read(&fi->i_gc_rwsem[READ]); >>>> + f2fs_down_write(&fi->i_opu_rwsem); >>>> + } >>>> } >>>> >>>> /* >>>> @@ -4779,8 +4801,10 @@ static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from, >>>> ret = iomap_dio_complete(dio); >>>> } >>>> >>>> - if (do_opu) >>>> + if (do_opu) { >>>> + f2fs_up_write(&fi->i_opu_rwsem); >>>> f2fs_up_read(&fi->i_gc_rwsem[READ]); >>>> + } >>>> f2fs_up_read(&fi->i_gc_rwsem[WRITE]); >>>> >>>> if (ret < 0) >>>> diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c >>>> index daf2c4dbe150..b4ed3b094366 100644 >>>> --- a/fs/f2fs/super.c >>>> +++ b/fs/f2fs/super.c >>>> @@ -1428,6 +1428,7 @@ static struct inode *f2fs_alloc_inode(struct super_block *sb) >>>> init_f2fs_rwsem(&fi->i_gc_rwsem[READ]); >>>> init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]); >>>> init_f2fs_rwsem(&fi->i_xattr_sem); >>>> + init_f2fs_rwsem(&fi->i_opu_rwsem); >>>> >>>> /* Will be used by directory only */ >>>> fi->i_dir_level = F2FS_SB(sb)->dir_level; >>>> -- >>>> 2.40.1