Skip to content

hugetlbfs: close race between MADV_DONTNEED and page fault

[email protected] linux-mm lkml 7 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v7 Latest4 patches 5 repliesIn review 3 of 4 applied
  2. v63 patches 5 repliesSuperseded
  3. v53 patches 7 repliesSuperseded
Earlier versions · 4
VersionPostedPatchesRepliesStatus
v43 patches4 repliesSuperseded
v33 patches3 repliesSuperseded
v23 patches3 repliesSuperseded
v12 patches11 repliesSuperseded

Patches in v7 3 of 4 applied

#Subject and filesSizeRepliesApplied
1/4hugetlbfs: clear resv_map pointer if mmap failsmm/hugetlb.c+4 -30 repliesApplied
2/4hugetlbfs: extend hugetlb_vma_lock to private VMAsinclude/linux/hugetlb.h mm/hugetlb.c+43 -42 repliesApplied
3/4hugetlbfs: close race between MADV_DONTNEED and page faultinclude/linux/hugetlb.h mm/hugetlb.c mm/memory.c+63 -191 replyApplied
4/4hugetlbfs: replace hugetlb_vma_lock with invalidate_lockfs/hugetlbfs/inode.c include/linux/fs.h include/linux/hugetlb.h +1 more+35 -3002 replies

Reviews trailers per patch

2 of 4 patches have a Reviewed-by or Acked-by.

PatchMike Kravetz
1/4
2/4R-b
3/4R-b
4/4

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v7 thread

Cover letter [PATCH v7 0/4] hugetlbfs: close race between MADV_DONTNEED and page fault

v7: fix !vma->vm_file and hfill2 cases
v6: move a fix from patch 3 to patch 2, more locking fixes
v5: somehow a __vma_private_lock(vma) test failed to make it from my tree into the v4 series, fix that
v4: fix unmap_vmas locking issue pointed out by Mike Kravetz, and resulting lockdep fallout
v3: fix compile error w/ lockdep and test case errors with patch 3
v2: fix the locking bug found with the libhugetlbfs tests.

Malloc libraries, like jemalloc and tcalloc, take decisions on when to call madvise independently from the code in the main application.

This sometimes results in the application page faulting on an address, right after the malloc library has shot down the backing memory with MADV_DONTNEED.

Usually this is harmless, because we always have some 4kB pages sitting around to satisfy a page fault. However, with hugetlbfs systems often allocate only the exact number of huge pages that the application wants.

Due to TLB batching, hugetlbfs MADV_DONTNEED will free pages outside of any lock taken on the page fault path, which can open up the following race condition:

CPU 1 CPU 2

MADV_DONTNEED
unmap page
shoot down TLB entry
page fault
fail to allocate a huge page killed with SIGBUS
free page

Fix that race by extending the hugetlb_vma_lock locking scheme to also cover private hugetlb mappings (with resv_map), and pulling the locking from __unmap_hugepage_final_range into helper functions called from zap_page_range_single. This ensures page faults stay locked out of the MADV_DONTNEED VMA until the huge pages have actually been freed.

The third patch in the series is more of an RFC. Using the invalidate_lock instead of the hugetlb_vma_lock greatly simplifies the code, but at the cost of turning a per-VMA lock into a lock per backing hugetlbfs file, which could slow things down when multiple processes are mapping the same hugetlbfs file.