kernel-ark/mm
Andrea Arcangeli 70b50f94f1 mm: thp: tail page refcounting fix
Michel while working on the working set estimation code, noticed that
calling get_page_unless_zero() on a random pfn_to_page(random_pfn)
wasn't safe, if the pfn ended up being a tail page of a transparent
hugepage under splitting by __split_huge_page_refcount().

He then found the problem could also theoretically materialize with
page_cache_get_speculative() during the speculative radix tree lookups
that uses get_page_unless_zero() in SMP if the radix tree page is freed
and reallocated and get_user_pages is called on it before
page_cache_get_speculative has a chance to call get_page_unless_zero().

So the best way to fix the problem is to keep page_tail->_count zero at
all times.  This will guarantee that get_page_unless_zero() can never
succeed on any tail page.  page_tail->_mapcount is guaranteed zero and
is unused for all tail pages of a compound page, so we can simply
account the tail page references there and transfer them to
tail_page->_count in __split_huge_page_refcount() (in addition to the
head_page->_mapcount).

While debugging this s/_count/_mapcount/ change I also noticed get_page is
called by direct-io.c on pages returned by get_user_pages.  That wasn't
entirely safe because the two atomic_inc in get_page weren't atomic.  As
opposed to other get_user_page users like secondary-MMU page fault to
establish the shadow pagetables would never call any superflous get_page
after get_user_page returns.  It's safer to make get_page universally safe
for tail pages and to use get_page_foll() within follow_page (inside
get_user_pages()).  get_page_foll() is safe to do the refcounting for tail
pages without taking any locks because it is run within PT lock protected
critical sections (PT lock for pte and page_table_lock for
pmd_trans_huge).

The standard get_page() as invoked by direct-io instead will now take
the compound_lock but still only for tail pages.  The direct-io paths
are usually I/O bound and the compound_lock is per THP so very
finegrined, so there's no risk of scalability issues with it.  A simple
direct-io benchmarks with all lockdep prove locking and spinlock
debugging infrastructure enabled shows identical performance and no
overhead.  So it's worth it.  Ideally direct-io should stop calling
get_page() on pages returned by get_user_pages().  The spinlock in
get_page() is already optimized away for no-THP builds but doing
get_page() on tail pages returned by GUP is generally a rare operation
and usually only run in I/O paths.

This new refcounting on page_tail->_mapcount in addition to avoiding new
RCU critical sections will also allow the working set estimation code to
work without any further complexity associated to the tail page
refcounting with THP.

Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
Reported-by: Michel Lespinasse <walken@google.com>
Reviewed-by: Michel Lespinasse <walken@google.com>
Reviewed-by: Minchan Kim <minchan.kim@gmail.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Hugh Dickins <hughd@google.com>
Cc: Johannes Weiner <jweiner@redhat.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: David Gibson <david@gibson.dropbear.id.au>
Cc: <stable@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-11-02 16:06:57 -07:00
..
backing-dev.c mm-add-comment-explaining-task-state-setting-in-bdi_forker_thread-fix 2011-10-31 17:30:49 -07:00
bootmem.c
bounce.c
cleancache.c
compaction.c mm: compaction: make compact_zone_order() static 2011-10-31 17:30:49 -07:00
debug-pagealloc.c debug-pagealloc: add support for highmem pages 2011-10-31 17:30:48 -07:00
dmapool.c
fadvise.c
failslab.c fault-injection: add ability to export fault_attr in arbitrary directory 2011-08-03 14:25:20 -10:00
filemap_xip.c
filemap.c vfs: iov_iter: have iov_iter_advance decrement nr_segs appropriately 2011-10-28 13:55:08 +02:00
fremap.c
highmem.c mm: fix kunmap_high() comment 2011-10-31 17:30:49 -07:00
huge_memory.c mm: thp: tail page refcounting fix 2011-11-02 16:06:57 -07:00
hugetlb.c
hwpoison-inject.c
init-mm.c
internal.h mm: thp: tail page refcounting fix 2011-11-02 16:06:57 -07:00
Kconfig memblock: add NO_BOOTMEM config symbol 2011-10-31 17:30:47 -07:00
Kconfig.debug
kmemcheck.c
kmemleak-test.c
kmemleak.c
ksm.c oom: fix race while temporarily setting current's oom_score_adj 2011-10-31 17:30:45 -07:00
maccess.c
madvise.c
Makefile Cross Memory Attach 2011-10-31 17:30:44 -07:00
memblock.c mm/memblock.c: quiet sparse noise 2011-10-31 17:30:50 -07:00
memcontrol.c mm: change isolate mode from #define to bitwise type 2011-10-31 17:30:44 -07:00
memory_hotplug.c
memory-failure.c HWPOISON: convert pr_debug()s to pr_info()s 2011-10-31 17:30:49 -07:00
memory.c mm: thp: tail page refcounting fix 2011-11-02 16:06:57 -07:00
mempolicy.c mm/mempolicy.c: quiet sparse noise 2011-10-31 17:30:50 -07:00
mempool.c
migrate.c mm: migration: clean up unmap_and_move() 2011-10-31 17:30:45 -07:00
mincore.c mm: clarify the radix_tree exceptional cases 2011-08-03 14:25:24 -10:00
mlock.c mm: munlock use mapcount to avoid terrible overhead 2011-10-31 17:30:51 -07:00
mm_init.c
mmap.c mm/mmap.c: eliminate the ret variable from mm_take_all_locks() 2011-10-31 17:30:49 -07:00
mmu_context.c
mmu_notifier.c
mmzone.c
mprotect.c
mremap.c thp: mremap support and TLB optimization 2011-10-31 17:30:48 -07:00
msync.c
nobootmem.c
nommu.c
oom_kill.c oom: fix race while temporarily setting current's oom_score_adj 2011-10-31 17:30:45 -07:00
page_alloc.c mm: neaten warn_alloc_failed 2011-10-31 17:30:48 -07:00
page_cgroup.c mm: Convert vmalloc/memset to vzalloc 2011-09-15 13:56:56 +02:00
page_io.c
page_isolation.c
page-writeback.c mm/page-writeback.c: document bdi_min_ratio 2011-10-31 17:30:45 -07:00
pagewalk.c
percpu-km.c
percpu-vm.c
percpu.c
pgtable-generic.c
prio_tree.c
process_vm_access.c Cross Memory Attach 2011-10-31 17:30:44 -07:00
quicklist.c
readahead.c
rmap.c ksm: fix the comment of try_to_unmap_one() 2011-10-31 17:30:49 -07:00
shmem.c vmscan: add barrier to prevent evictable page in unevictable list 2011-10-31 17:30:50 -07:00
slab.c mm: restrict access to slab files under procfs and sysfs 2011-09-27 22:59:27 +03:00
slob.c
slub.c lib/string.c: introduce memchr_inv() 2011-10-31 17:30:47 -07:00
sparse-vmemmap.c
sparse.c
swap_state.c
swap.c mm: thp: tail page refcounting fix 2011-11-02 16:06:57 -07:00
swapfile.c oom: fix race while temporarily setting current's oom_score_adj 2011-10-31 17:30:45 -07:00
thrash.c mm/thrash.c: quiet sparse noise 2011-10-31 17:30:50 -07:00
truncate.c mm: a few small updates for radix-swap 2011-08-03 14:25:24 -10:00
util.c
vmalloc.c mm/vmalloc.c: report more vmalloc failures 2011-10-31 17:30:48 -07:00
vmscan.c vmscan: abort reclaim/compaction if compaction can proceed 2011-10-31 17:30:50 -07:00
vmstat.c mm/vmstat.c: cache align vm_stat 2011-10-31 17:30:51 -07:00