• Dave Hansen's avatar
    mm: fix pin vs. gup mismatch with gate pages · 9fa2dd94
    Dave Hansen authored
    Gate pages were missed when converting from get to pin_user_pages().
    This can lead to refcount imbalances.  This is reliably and quickly
    reproducible running the x86 selftests when vsyscall=emulate is enabled
    (the default).  Fix by using try_grab_page() with appropriate flags
    passed.
    
    The long story:
    
    Today, pin_user_pages() and get_user_pages() are similar interfaces for
    manipulating page reference counts.  However, "pins" use a "bias" value
    and manipulate the actual reference count by 1024 instead of 1 used by
    plain "gets".
    
    That means that pin_user_pages() must be matched with unpin_user_pages()
    and can't be mixed with a plain put_user_pages() or put_page().
    
    Enter gate pages, like the vsyscall page.  They are pages usually in the
    kernel image, but which are mapped to userspace.  Userspace is allowed
    access to them, including interfaces using get/pin_user_pages().  The
    refcount of these kernel pages is manipulated just like a normal user
    page on the get/pin side so that the put/unpin side can work the same
    for normal user pages or gate pages.
    
    get_gate_page() uses try_get_page() which only bumps the refcount by
    1, not 1024, even if called in the pin_user_pages() path.  If someone
    pins a gate page, this happens:
    
    	pin_user_pages()
    		get_gate_page()
    			try_get_page() // bump refcount +1
    	... some time later
    	unpin_user_pages()
    		page_ref_sub_and_test(page, 1024))
    
    ... and boom, we get a refcount off by 1023.  This is reliably and
    quickly reproducible running the x86 selftests when booted with
    vsyscall=emulate (the default).  The selftests use ptrace(), but I
    suspect anything using pin_user_pages() on gate pages could hit this.
    
    To fix it, simply use try_grab_page() instead of try_get_page(), and
    pass 'gup_flags' in so that FOLL_PIN can be respected.
    
    This bug traces back to the very beginning of the FOLL_PIN support in
    commit 3faa52c0 ("mm/gup: track FOLL_PIN pages"), which showed up in
    the 5.7 release.
    Signed-off-by: default avatarDave Hansen <dave.hansen@linux.intel.com>
    Fixes: 3faa52c0 ("mm/gup: track FOLL_PIN pages")
    Reported-by: default avatarPeter Zijlstra <peterz@infradead.org>
    Reviewed-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
    Acked-by: default avatarAndy Lutomirski <luto@kernel.org>
    Cc: x86@kernel.org
    Cc: Jann Horn <jannh@google.com>
    Cc: Andrew Morton <akpm@linux-foundation.org>
    Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    9fa2dd94
gup.c 87.2 KB