• Wei Fang's avatar
    vfs,mm: fix a dead loop in truncate_inode_pages_range() · 3d549dcf
    Wei Fang authored
    commit c2a9737f45e27d8263ff9643f994bda9bac0b944 upstream.
    We triggered a deadloop in truncate_inode_pages_range() on 32 bits
    architecture with the test case bellow:
    	fd = open();
    	write(fd, buf, 4096);
    	preadv64(fd, &iovec, 1, 0xffffffff000);
    	ftruncate(fd, 0);
    Then ftruncate() will not return forever.
    The filesystem used in this case is ubifs, but it can be triggered on
    many other filesystems.
    When preadv64() is called with offset=0xffffffff000, a page with
    index=0xffffffff will be added to the radix tree of ->mapping.  Then
    this page can be found in ->mapping with pagevec_lookup().  After that,
    truncate_inode_pages_range(), which is called in ftruncate(), will fall
    into an infinite loop:
     - find a page with index=0xffffffff, since index>=end, this page won't
       be truncated
     - index++, and index become 0
     - the page with index=0xffffffff will be found again
    The data type of index is unsigned long, so index won't overflow to 0 on
    64 bits architecture in this case, and the dead loop won't happen.
    Since truncate_inode_pages_range() is executed with holding lock of
    inode->i_rwsem, any operation related with this lock will be blocked,
    and a hung task will happen, e.g.:
      INFO: task truncate_test:3364 blocked for more than 120 seconds.
    The page with index=0xffffffff added to ->mapping is useless.  Fix this
    by checking the read position before allocating pages.
    Link: http://lkml.kernel.org/r/1475151010-40166-1-git-send-email-fangwei1@huawei.comSigned-off-by: default avatarWei Fang <fangwei1@huawei.com>
    Cc: Christoph Hellwig <hch@infradead.org>
    Cc: Dave Chinner <david@fromorbit.com>
    Cc: Al Viro <viro@zeniv.linux.org.uk>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
filemap.c 77.4 KB