• Nick Piggin's avatar
    [PATCH] Fix read() vs truncate race · 4bd9607e
    Nick Piggin authored
    do_generic_mapping_read()
    {
    	isize1 = i_size_read();
    	...
    	readpage
    	copy_to_user up to isize1;
    }
    
    readpage()
    {
    	isize2 = i_size_read();
    	...
    	read blocks
    	...
    	zero-fill all blocks past isize2
    }
    
    If a second thread runs truncate and shrinks i_size, so isize1 and isize2 are
    different, the read can return up to a page of zero-fill that shouldn't really
    exist.
    
    The trick is to read isize1 after doing the readpage.  I realised this is the
    right way to do it without having to change the readpage API.
    
    The patch should not cost any cycles when reading from pagecache.
    Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
    4bd9607e
filemap.c 51.5 KB