linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.cz>
To: Baokun Li <libaokun1@huawei.com>
Cc: Jan Kara <jack@suse.cz>,
	linux-mm@kvack.org, linux-ext4@vger.kernel.org, tytso@mit.edu,
	adilger.kernel@dilger.ca, willy@infradead.org,
	akpm@linux-foundation.org, david@fromorbit.com,
	hch@infradead.org, ritesh.list@gmail.com,
	linux-kernel@vger.kernel.org, yi.zhang@huawei.com,
	yangerkun@huawei.com, yukuai3@huawei.com, stable@kernel.org
Subject: Re: [RFC PATCH] mm/filemap: avoid buffered read/write race to read inconsistent data
Date: Tue, 12 Dec 2023 14:37:13 +0100	[thread overview]
Message-ID: <20231212133713.bihojdsnccmadcpg@quack3> (raw)
In-Reply-To: <9fdebd0a-ac10-e193-b245-7678fa708c82@huawei.com>

On Tue 12-12-23 21:16:16, Baokun Li wrote:
> On 2023/12/12 20:41, Jan Kara wrote:
> > On Tue 12-12-23 17:36:34, Baokun Li wrote:
> > > The following concurrency may cause the data read to be inconsistent with
> > > the data on disk:
> > > 
> > >               cpu1                           cpu2
> > > ------------------------------|------------------------------
> > >                                 // Buffered write 2048 from 0
> > >                                 ext4_buffered_write_iter
> > >                                  generic_perform_write
> > >                                   copy_page_from_iter_atomic
> > >                                   ext4_da_write_end
> > >                                    ext4_da_do_write_end
> > >                                     block_write_end
> > >                                      __block_commit_write
> > >                                       folio_mark_uptodate
> > > // Buffered read 4096 from 0          smp_wmb()
> > > ext4_file_read_iter                   set_bit(PG_uptodate, folio_flags)
> > >   generic_file_read_iter            i_size_write // 2048
> > >    filemap_read                     unlock_page(page)
> > >     filemap_get_pages
> > >      filemap_get_read_batch
> > >      folio_test_uptodate(folio)
> > >       ret = test_bit(PG_uptodate, folio_flags)
> > >       if (ret)
> > >        smp_rmb();
> > >        // Ensure that the data in page 0-2048 is up-to-date.
> > > 
> > >                                 // New buffered write 2048 from 2048
> > >                                 ext4_buffered_write_iter
> > >                                  generic_perform_write
> > >                                   copy_page_from_iter_atomic
> > >                                   ext4_da_write_end
> > >                                    ext4_da_do_write_end
> > >                                     block_write_end
> > >                                      __block_commit_write
> > >                                       folio_mark_uptodate
> > >                                        smp_wmb()
> > >                                        set_bit(PG_uptodate, folio_flags)
> > >                                     i_size_write // 4096
> > >                                     unlock_page(page)
> > > 
> > >     isize = i_size_read(inode) // 4096
> > >     // Read the latest isize 4096, but without smp_rmb(), there may be
> > >     // Load-Load disorder resulting in the data in the 2048-4096 range
> > >     // in the page is not up-to-date.
> > >     copy_page_to_iter
> > >     // copyout 4096
> > > 
> > > In the concurrency above, we read the updated i_size, but there is no read
> > > barrier to ensure that the data in the page is the same as the i_size at
> > > this point, so we may copy the unsynchronized page out. Hence adding the
> > > missing read memory barrier to fix this.
> > > 
> > > This is a Load-Load reordering issue, which only occurs on some weak
> > > mem-ordering architectures (e.g. ARM64, ALPHA), but not on strong
> > > mem-ordering architectures (e.g. X86). And theoretically the problem
> > AFAIK x86 can also reorder loads vs loads so the problem can in theory
> > happen on x86 as well.
> 
> According to what I read in the /perfbook /at the link below,
> 
>  Loads Reordered After Loads does not happen on x86.
> 
> pdf sheet 562 corresponds to page 550,
> 
>    Table 15.5: Summary of Memory Ordering
> 
> https://mirrors.edge.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook-1c.2023.06.11a.pdf

Indeed. I stand corrected! Thanks for the link.

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR


  reply	other threads:[~2023-12-12 13:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-12  9:36 Baokun Li
2023-12-12 12:41 ` Jan Kara
2023-12-12 13:16   ` Baokun Li
2023-12-12 13:37     ` Jan Kara [this message]
2023-12-12 13:21   ` Baokun Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231212133713.bihojdsnccmadcpg@quack3 \
    --to=jack@suse.cz \
    --cc=adilger.kernel@dilger.ca \
    --cc=akpm@linux-foundation.org \
    --cc=david@fromorbit.com \
    --cc=hch@infradead.org \
    --cc=libaokun1@huawei.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ritesh.list@gmail.com \
    --cc=stable@kernel.org \
    --cc=tytso@mit.edu \
    --cc=willy@infradead.org \
    --cc=yangerkun@huawei.com \
    --cc=yi.zhang@huawei.com \
    --cc=yukuai3@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox