hugetlbfs: use i_mmap_rwsem for more synchronization
Subsystems (7)
v3 cover hugetlbfs: use i_mmap_rwsem for more synchronization
I'm kicking this back to RFC status as there may be a couple other options we want to explore. This expanded use of i_mmap_rwsem in hugetlbfs was recently pushed upstream and reverted due to locking issues.
http://lkml.kernel.org/r/[email protected]
http://lkml.kernel.org/r/[email protected]
The biggest problem with those patches was lock ordering. Recall, the two issues we are trying to address:
1) For shared pmds, huge PTE pointers returned by huge_pte_alloc can become invalid via a call to huge_pmd_unshare by another thread.
2) hugetlbfs page faults can race with truncation causing invalid global reserve counts and state.
To effectively use i_mmap_rwsem to address these issues it needs to be held (in read mode) during page fault processing. However, during fault processing we need to lock the page we will be adding. Lock ordering requires we take page lock before i_mmap_rwsem. Waiting until after taking the page lock is too late in the fault process for the synchronization we want to do.
To address this lock ordering issue, the following patches change the lock ordering for hugetlb pages. This is not difficult as hugetlbfs processing is done separate from core mm in many places. However, I don't really like this idea. Much ugliness is contained in the new routine hugetlb_page_mapping_lock_write() of patch 1.
If this approach of extending the usage of i_mmap_rwsem is not acceptable, there are two other options I can think of.
- Add a new rw-semaphore that lives in the hugetlbfs specific inode extension. The good thing is that this semaphore would be hugetlbfs specific and not directly exposed to the rest of the mm code. Therefore, interaction with other locking schemes is minimized.
- Don't really add any new synchronization, but notice and catch all the races. After catching the race, cleanup, backout, retry ... etc, as needed. This can get really ugly, especially for huge page reservations. At one time, I started writing some of the reservation backout code for page faults and it got so ugly and complicated I went down the path of adding synchronization to avoid the races.
Suggestions on how to proceed would be appreciated. If you think the following patches are not too ugly, comments on those would also be welcome.
Mike Kravetz (2):
hugetlbfs: use i_mmap_rwsem for more pmd sharing synchronization
hugetlbfs: Use i_mmap_rwsem to fix page fault/truncate race
fs/hugetlbfs/inode.c | 34 +++++--- include/linux/fs.h | 5 ++ include/linux/hugetlb.h | 8 ++ mm/hugetlb.c | 186 ++++++++++++++++++++++++++++++++++------ mm/memory-failure.c | 29 ++++++- mm/migrate.c | 24 +++++- mm/rmap.c | 17 +++- mm/userfaultfd.c | 11 ++- 8 files changed, 270 insertions(+), 44 deletions(-)
Signature · 1 line
2.17.2
RFC PATCH v3 1/2 hugetlbfs: use i_mmap_rwsem for more pmd sharing synchronization
While looking at BUGs associated with invalid huge page map counts, it was discovered and observed that a huge pte pointer could become 'invalid' and point to another task's page table. Consider the following:
A task takes a page fault on a shared hugetlbfs file and calls huge_pte_alloc to get a ptep. Suppose the returned ptep points to a shared pmd.
Now, another task truncates the hugetlbfs file. As part of truncation, it unmaps everyone who has the file mapped. If the range being truncated is covered by a shared pmd, huge_pmd_unshare will be called. For all but the last user of the shared pmd, huge_pmd_unshare will clear the pud pointing to the pmd. If the task in the middle of the page fault is not the last user, the ptep returned by huge_pte_alloc now points to another task's page table or worse. This leads to bad things such as incorrect page map/reference counts or invalid memory references.
To fix, expand the use of i_mmap_rwsem as follows:
- i_mmap_rwsem is held in read mode whenever huge_pmd_share is called. huge_pmd_share is only called via huge_pte_alloc, so callers of huge_pte_alloc take i_mmap_rwsem before calling. In addition, callers of huge_pte_alloc continue to hold the semaphore until finished with the ptep.
- i_mmap_rwsem is held in write mode whenever huge_pmd_unshare is called.
One problem with this scheme is that it requires taking i_mmap_rwsem before taking the page lock during page faults. This is not the order specified in the rest of mm code. Handling of hugetlbfs pages is mostly isolated today. Therefore, we use this alternative locking order for PageHuge() pages.
mapping->i_mmap_rwsem
hugetlb_fault_mutex (hugetlbfs specific page fault mutex)
page->flags PG_locked (lock_page)To help with lock ordering issues, hugetlb_page_mapping_lock_write() is introduced to write lock the i_mmap_rwsem associated with a page.
In most cases it is easy to get address_space via vma->vm_file->f_mapping. However, in the case of migration or memory errors for anon pages we do not have an associated vma. A new routine _get_hugetlb_page_mapping() will use anon_vma to get address_space in these cases.
--- fs/hugetlbfs/inode.c | 2 + include/linux/fs.h | 5 ++ include/linux/hugetlb.h | 8 ++ mm/hugetlb.c | 167 ++++++++++++++++++++++++++++++++++++---- mm/memory-failure.c | 29 ++++++- mm/migrate.c | 24 +++++- mm/rmap.c | 17 +++- mm/userfaultfd.c | 11 ++- 8 files changed, 239 insertions(+), 24 deletions(-)
fs/hugetlbfs/inode.c +2 -0
@@ -443,7 +443,9 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,if (unlikely (page_mapped (page))) {BUG_ON (truncate_op); +mutex_unlock (&hugetlb_fault_mutex_table[hash]);i_mmap_lock_write (mapping); +mutex_lock (&hugetlb_fault_mutex_table[hash]);hugetlb_vmdelete_list (&mapping->i_mmap, index *pages_per_huge_page (h), (index +1 ) *pages_per_huge_page (h));
include/linux/fs.h +5 -0
@@ -501,6 +501,11 @@ static inline void i_mmap_lock_write(struct address_space *mapping)down_write (&mapping->i_mmap_rwsem); } +static inline int i_mmap_trylock_write (struct address_space *mapping) +{ +return down_write_trylock (&mapping->i_mmap_rwsem); +} +static inline void i_mmap_unlock_write (struct address_space *mapping) {up_write (&mapping->i_mmap_rwsem);
include/linux/hugetlb.h +8 -0
@@ -130,6 +130,8 @@ u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm,pte_t *huge_pmd_share (struct mm_struct *mm,unsigned long addr,pud_t *pud); +struct address_space *hugetlb_page_mapping_lock_write (struct page *hpage); +extern int sysctl_hugetlb_shm_group;extern struct list_head huge_boot_pages;
@@ -172,6 +174,12 @@ static inline unsigned long hugetlb_total_pages(void)return 0 ; } +static inline struct address_space *hugetlb_page_mapping_lock_write ( +struct page *hpage) +{ +return NULL ; +} +static inline int huge_pmd_unshare (struct mm_struct *mm,unsigned long *addr,pte_t *ptep) {
mm/hugetlb.c +151 -16
260 lines not on this page · raw message
mm/memory-failure.c +27 -2
@@ -966,7 +966,7 @@ static bool hwpoison_user_mappings(struct page *p, unsigned long pfn,enum ttu_flags ttu =TTU_IGNORE_MLOCK |TTU_IGNORE_ACCESS ;struct address_space *mapping;LIST_HEAD (tokill); -bool unmap_success; +bool unmap_success = true;int kill =1 , forcekill;struct page *hpage = *hpagep;bool mlocked =PageMlocked (hpage);
@@ -1028,7 +1028,32 @@ static bool hwpoison_user_mappings(struct page *p, unsigned long pfn,if (kill)collect_procs (hpage, &tokill, flags &MF_ACTION_REQUIRED ); - unmap_success =try_to_unmap (hpage, ttu); +if (!PageHuge (hpage)) { + unmap_success =try_to_unmap (hpage, ttu); + }else { +/* +* For hugetlb pages, try_to_unmap could potentially call +* huge_pmd_unshare. Because of this, take semaphore in +* write mode here and set TTU_RMAP_LOCKED to indicate we +* have taken the lock at this higer level. +* +* Note that the call to hugetlb_page_mapping_lock_write +* is necessary even if mapping is already set. It handles +* ugliness of potentially having to drop page lock to obtain +* i_mmap_rwsem. +*/ + mapping =hugetlb_page_mapping_lock_write (hpage); + +if (mapping) { + unmap_success =try_to_unmap (hpage, + ttu|TTU_RMAP_LOCKED ); +i_mmap_unlock_write (mapping); + }else { +pr_info ("Memory failure: %#lx: could not find mapping for mapped huge page\n" , + pfn); + unmap_success = false; + } + }if (!unmap_success)pr_err ("Memory failure: %#lx: failed to unmap page (mapcount=%d)\n" , pfn,page_mapcount (hpage));
mm/migrate.c +21 -3
@@ -1266,6 +1266,7 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,int page_was_mapped =0 ;struct page *new_hpage;struct anon_vma *anon_vma =NULL ; +struct address_space *mapping =NULL ;/* * Movability of hugepages depends on architectures and hugepage size.
@@ -1303,17 +1304,34 @@ static int unmap_and_move_huge_page(new_page_t get_new_page, goto put_anon;if (page_mapped (hpage)) { +/* +* try_to_unmap could potentially call huge_pmd_unshare. +* Because of this, take semaphore in write mode here and +* set TTU_RMAP_LOCKED to let lower levels know we have +* taken the lock. +*/ + mapping =hugetlb_page_mapping_lock_write (hpage); +if (unlikely (!mapping)) + goto put_anon; +try_to_unmap (hpage, -TTU_MIGRATION |TTU_IGNORE_MLOCK |TTU_IGNORE_ACCESS ); +TTU_MIGRATION |TTU_IGNORE_MLOCK |TTU_IGNORE_ACCESS | +TTU_RMAP_LOCKED ); page_was_mapped =1 ; +/* +* Leave mapping locked until after subsequent call to +* remove_migration_ptes() +*/ }if (!page_mapped (hpage)) rc =move_to_new_page (new_hpage, hpage, mode); -if (page_was_mapped) +if (page_was_mapped) {remove_migration_ptes (hpage, - rc ==MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false); + rc ==MIGRATEPAGE_SUCCESS ? new_hpage : hpage, true); +i_mmap_unlock_write (mapping); + }unlock_page (new_hpage);
mm/rmap.c +16 -1
@@ -22,9 +22,10 @@* * inode->i_mutex (while writing or truncating, not reading or faulting) * mm->mmap_sem -* page->flags PG_locked (lock_page) +* page->flags PG_locked (lock_page) * (see huegtlbfs below) * hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share) * mapping->i_mmap_rwsem +* hugetlb_fault_mutex (hugetlbfs specific page fault mutex) * anon_vma->rwsem * mm->page_table_lock or pte_lock * zone_lru_lock (in mark_page_accessed, isolate_lru_page)
@@ -43,6 +44,11 @@* anon_vma->rwsem,mapping->i_mutex (memory_failure, collect_procs_anon) * ->tasklist_lock * pte map lock +* +* * hugetlbfs PageHuge() pages take locks in this order: +* mapping->i_mmap_rwsem +* hugetlb_fault_mutex (hugetlbfs specific page fault mutex) +* page->flags PG_locked (lock_page) */ #include <linux/mm.h>
@@ -1374,6 +1380,9 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,/* * If sharing is possible, start and end will be adjusted * accordingly. +* +* If called for a huge page, caller must hold i_mmap_rwsem +* in write mode as it is possible to call huge_pmd_unshare. */ adjust_range_if_pmd_sharing_possible (vma, &start, &end); }
@@ -1420,6 +1429,12 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma, address = pvmw.address;if (PageHuge (page)) { +/* +* To call huge_pmd_unshare, i_mmap_rwsem must be +* held in write mode. Caller needs to explicitly +* do this outside rmap routines. +*/ +VM_BUG_ON (!(flags &TTU_RMAP_LOCKED ));if (huge_pmd_unshare (mm, &address, pvmw.pte)) {/* * huge_pmd_unshare unmapped an entire PMD
mm/userfaultfd.c +9 -2
@@ -267,10 +267,14 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm,VM_BUG_ON (dst_addr & ~huge_page_mask (h));/* -* Serialize via hugetlb_fault_mutex +* Serialize via i_mmap_rwsem and hugetlb_fault_mutex. +* i_mmap_rwsem ensures the dst_pte remains valid even +* in the case of shared pmds. fault mutex prevents +* races with other faulting threads. */ - idx =linear_page_index (dst_vma, dst_addr); mapping = dst_vma->vm_file->f_mapping; +i_mmap_lock_read (mapping); + idx =linear_page_index (dst_vma, dst_addr); hash =hugetlb_fault_mutex_hash (h, dst_mm, dst_vma, mapping, idx, dst_addr);mutex_lock (&hugetlb_fault_mutex_table[hash]);
@@ -279,6 +283,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, dst_pte =huge_pte_alloc (dst_mm, dst_addr,huge_page_size (h));if (!dst_pte) {mutex_unlock (&hugetlb_fault_mutex_table[hash]); +i_mmap_unlock_read (mapping); goto out_unlock; }
@@ -286,6 +291,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, dst_pteval =huge_ptep_get (dst_pte);if (!huge_pte_none (dst_pteval)) {mutex_unlock (&hugetlb_fault_mutex_table[hash]); +i_mmap_unlock_read (mapping); goto out_unlock; }
@@ -293,6 +299,7 @@ static __always_inline ssize_t __mcopy_atomic_hugetlb(struct mm_struct *dst_mm, dst_addr, src_addr, &page);mutex_unlock (&hugetlb_fault_mutex_table[hash]); +i_mmap_unlock_read (mapping); vm_alloc_shared = vm_shared;cond_resched ();
Signature · 1 line
2.17.2
RFC PATCH v3 2/2 hugetlbfs: Use i_mmap_rwsem to fix page fault/truncate race
In reply to Mike Kravetzhugetlbfs page faults can race with truncate and hole punch operations. Current code in the page fault path attempts to handle this by 'backing out' operations if we encounter the race. One obvious omission in the current code is removing a page newly added to the page cache. This is pretty straight forward to address, but there is a more subtle and difficult issue of backing out hugetlb reservations. To handle this correctly, the 'reservation state' before page allocation needs to be noted so that it can be properly backed out. There are four distinct possibilities for reservation state: shared/reserved, shared/no-resv, private/reserved and private/no-resv. Backing out a reservation may require memory allocation which could fail so that needs to be taken into account as well.
Instead of writing the required complicated code for this rare occurrence, just eliminate the race. i_mmap_rwsem is now held in read mode for the duration of page fault processing. Hold i_mmap_rwsem in write mode when modifying i_size. In this way, truncation can not proceed when page faults are being processed. In addition, i_size will not change during fault processing so a single check can be made to ensure faults are not beyond (proposed) end of file. Faults can still race with hole punch, but that race is handled by existing code and the use of hugetlb_fault_mutex.
With this modification, checks for races with truncation in the page fault path can be simplified and removed. remove_inode_hugepages no longer needs to take hugetlb_fault_mutex in the case of truncation. Comments are expanded to explain reasoning behind locking.
--- fs/hugetlbfs/inode.c | 32 ++++++++++++++++++++++---------- mm/hugetlb.c | 23 +++++++++++------------ 2 files changed, 33 insertions(+), 22 deletions(-)
fs/hugetlbfs/inode.c +22 -10
@@ -384,10 +384,9 @@ hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end)* In this case, we first scan the range and release found pages. * After releasing pages, hugetlb_unreserve_pages cleans up region/reserv * maps and global counts. Page faults can not race with truncation -* in this routine. hugetlb_no_page() prevents page faults in the -* truncated range. It checks i_size before allocation, and again after -* with the page table lock for the page held. The same lock must be -* acquired to unmap a page. +* in this routine. hugetlb_no_page() holds i_mmap_rwsem and prevents +* page faults in the truncated range by checking i_size. i_size is +* modified while holding i_mmap_rwsem. * hole punch is indicated if end is not LLONG_MAX * In the hole punch case we scan the range and release found pages. * Only when releasing a page is the associated region/reserv map
@@ -425,11 +424,19 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,struct page *page = pvec.pages[i];u32 hash; - index = page->index; - hash =hugetlb_fault_mutex_hash (h, current->mm, +if (!truncate_op) { +/* +* Only need to hold the fault mutex in the +* hole punch case. This prevents races with +* page faults. Races are not possible in the +* case of truncation. +*/ + index = page->index; + hash =hugetlb_fault_mutex_hash (h, current->mm, &pseudo_vma, mapping, index,0 ); -mutex_lock (&hugetlb_fault_mutex_table[hash]); +mutex_lock (&hugetlb_fault_mutex_table[hash]); + }/* * If page is mapped, it was faulted in after being
@@ -472,7 +479,8 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart, }unlock_page (page); -mutex_unlock (&hugetlb_fault_mutex_table[hash]); +if (!truncate_op) +mutex_unlock (&hugetlb_fault_mutex_table[hash]); }huge_pagevec_release (&pvec);cond_resched ();
@@ -503,8 +511,8 @@ static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)BUG_ON (offset & ~huge_page_mask (h)); pgoff = offset >>PAGE_SHIFT ; -i_size_write (inode, offset);i_mmap_lock_write (mapping); +i_size_write (inode, offset);if (!RB_EMPTY_ROOT (&mapping->i_mmap.rb_root))hugetlb_vmdelete_list (&mapping->i_mmap, pgoff,0 );i_mmap_unlock_write (mapping);
@@ -626,7 +634,11 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,/* addr is the offset within the file (zero based) */ addr = index * hpage_size; -/* mutex taken here, fault path and hole punch */ +/* +* fault mutex taken here, protects against fault path +* and hole punch. inode_lock previously taken protects +* against truncation. +*/ hash =hugetlb_fault_mutex_hash (h, mm, &pseudo_vma, mapping, index, addr);mutex_lock (&hugetlb_fault_mutex_table[hash]);
mm/hugetlb.c +11 -12
@@ -3858,16 +3858,17 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm, }/* -* Use page lock to guard against racing truncation -* before we get page_table_lock. +* We can not race with truncation due to holding i_mmap_rwsem. +* i_size is modified when holding i_mmap_rwsem, so check here +* once for faults beyond end of file. */ + size =i_size_read (mapping->host) >>huge_page_shift (h); +if (idx >= size) + goto out; + retry: page =find_lock_page (mapping, idx);if (!page) { - size =i_size_read (mapping->host) >>huge_page_shift (h); -if (idx >= size) - goto out; -/* * Check for page in userfault range */
@@ -3957,10 +3958,6 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm, } ptl =huge_pte_lock (h, mm, ptep); - size =i_size_read (mapping->host) >>huge_page_shift (h); -if (idx >= size) - goto backout; - ret =0 ;if (!huge_pte_none (huge_ptep_get (ptep))) goto backout;
@@ -4062,8 +4059,10 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,/* * Acquire i_mmap_rwsem before calling huge_pte_alloc and hold -* until finished with ptep. This prevents huge_pmd_unshare from -* being called elsewhere and making the ptep no longer valid. +* until finished with ptep. This serves two purposes: +* 1) It prevents huge_pmd_unshare from being called elsewhere +* and making the ptep no longer valid. +* 2) It synchronizes us with i_size modifications during truncation. * * ptep could have already be assigned via huge_pte_offset. That * is OK, as huge_pte_alloc will return the same value unless
Signature · 1 line
2.17.2