Re: [PATCH] fs: fix a data race in i_size_write/i_size_read

From: Qian Cai
Date: Thu Feb 20 2020 - 23:19:43 EST




> On Feb 19, 2020, at 4:21 AM, Marco Elver <elver@xxxxxxxxxx> wrote:
>
> Let's assume the WRITE_ONCE can be dropped.
>
> The load is a different story. While load tearing may not be an issue,
> it's more likely that other optimizations can break the code. For
> example load fusing can break code that expects repeated loads in a
> loop. E.g. I found these uses of i_size_read in loops:
>
> git grep -E '(for|while) \(.*i_size_read'
> fs/ocfs2/dir.c: while (ctx->pos < i_size_read(inode)) {
> fs/ocfs2/dir.c: for (i = 0; i < i_size_read(inode) &&
> i < offset; ) {
> fs/ocfs2/dir.c: while (ctx->pos < i_size_read(inode)) {
> fs/ocfs2/dir.c: while (ctx->pos < i_size_read(inode)
> fs/squashfs/dir.c: while (length < i_size_read(inode)) {
> fs/squashfs/namei.c: while (length < i_size_read(dir)) {
>
> Can i_size writes happen concurrently, and if so will these break if
> the compiler decides to just do i_size_read's load once, and keep the
> result in a register?

Al, is it more acceptable to add READ_ONCE() only then?