Skip to content

Commit 657e303

Browse files
kiryltorvalds
authored andcommitted
shmem, thp: respect MADV_{NO,}HUGEPAGE for file mappings
Let's wire up existing madvise() hugepage hints for file mappings. MADV_HUGEPAGE advise shmem to allocate huge page on page fault in the VMA. It only has effect if the filesystem is mounted with huge=advise or huge=within_size. MADV_NOHUGEPAGE prevents hugepage from being allocated on page fault in the VMA. It doesn't prevent a huge page from being allocated by other means, i.e. page fault into different mapping or write(2) into file. Link: http://lkml.kernel.org/r/1466021202-61880-31-git-send-email-kirill.shutemov@linux.intel.com Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 800d8c6 commit 657e303

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

‎mm/huge_memory.c‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
18301830
return NULL;
18311831
}
18321832

1833-
#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
1833+
#define VM_NO_KHUGEPAGED (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
18341834

18351835
int hugepage_madvise(struct vm_area_struct *vma,
18361836
unsigned long *vm_flags, int advice)
@@ -1846,27 +1846,18 @@ int hugepage_madvise(struct vm_area_struct *vma,
18461846
if (mm_has_pgste(vma->vm_mm))
18471847
return 0;
18481848
#endif
1849-
/*
1850-
* Be somewhat over-protective like KSM for now!
1851-
*/
1852-
if (*vm_flags & VM_NO_THP)
1853-
return -EINVAL;
18541849
*vm_flags &= ~VM_NOHUGEPAGE;
18551850
*vm_flags |= VM_HUGEPAGE;
18561851
/*
18571852
* If the vma become good for khugepaged to scan,
18581853
* register it here without waiting a page fault that
18591854
* may not happen any time soon.
18601855
*/
1861-
if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
1856+
if (!(*vm_flags & VM_NO_KHUGEPAGED) &&
1857+
khugepaged_enter_vma_merge(vma, *vm_flags))
18621858
return -ENOMEM;
18631859
break;
18641860
case MADV_NOHUGEPAGE:
1865-
/*
1866-
* Be somewhat over-protective like KSM for now!
1867-
*/
1868-
if (*vm_flags & VM_NO_THP)
1869-
return -EINVAL;
18701861
*vm_flags &= ~VM_HUGEPAGE;
18711862
*vm_flags |= VM_NOHUGEPAGE;
18721863
/*
@@ -1974,7 +1965,7 @@ int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
19741965
* page fault if needed.
19751966
*/
19761967
return 0;
1977-
if (vma->vm_ops || (vm_flags & VM_NO_THP))
1968+
if (vma->vm_ops || (vm_flags & VM_NO_KHUGEPAGED))
19781969
/* khugepaged not yet working on file or special mappings */
19791970
return 0;
19801971
hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
@@ -2366,7 +2357,7 @@ static bool hugepage_vma_check(struct vm_area_struct *vma)
23662357
return false;
23672358
if (is_vma_temporary_stack(vma))
23682359
return false;
2369-
return !(vma->vm_flags & VM_NO_THP);
2360+
return !(vma->vm_flags & VM_NO_KHUGEPAGED);
23702361
}
23712362

23722363
/*

‎mm/shmem.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ struct shmem_falloc {
101101
enum sgp_type {
102102
SGP_READ, /* don't exceed i_size, don't allocate page */
103103
SGP_CACHE, /* don't exceed i_size, may allocate page */
104+
SGP_NOHUGE, /* like SGP_CACHE, but no huge pages */
105+
SGP_HUGE, /* like SGP_CACHE, huge pages preferred */
104106
SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
105107
SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
106108
};
@@ -1409,13 +1411,16 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
14091411
struct mem_cgroup *memcg;
14101412
struct page *page;
14111413
swp_entry_t swap;
1414+
enum sgp_type sgp_huge = sgp;
14121415
pgoff_t hindex = index;
14131416
int error;
14141417
int once = 0;
14151418
int alloced = 0;
14161419

14171420
if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
14181421
return -EFBIG;
1422+
if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1423+
sgp = SGP_CACHE;
14191424
repeat:
14201425
swap.val = 0;
14211426
page = find_lock_entry(mapping, index);
@@ -1534,7 +1539,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
15341539
/* shmem_symlink() */
15351540
if (mapping->a_ops != &shmem_aops)
15361541
goto alloc_nohuge;
1537-
if (shmem_huge == SHMEM_HUGE_DENY)
1542+
if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
15381543
goto alloc_nohuge;
15391544
if (shmem_huge == SHMEM_HUGE_FORCE)
15401545
goto alloc_huge;
@@ -1551,7 +1556,9 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
15511556
goto alloc_huge;
15521557
/* fallthrough */
15531558
case SHMEM_HUGE_ADVISE:
1554-
/* TODO: wire up fadvise()/madvise() */
1559+
if (sgp_huge == SGP_HUGE)
1560+
goto alloc_huge;
1561+
/* TODO: implement fadvise() hints */
15551562
goto alloc_nohuge;
15561563
}
15571564

@@ -1680,6 +1687,7 @@ static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
16801687
{
16811688
struct inode *inode = file_inode(vma->vm_file);
16821689
gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
1690+
enum sgp_type sgp;
16831691
int error;
16841692
int ret = VM_FAULT_LOCKED;
16851693

@@ -1741,7 +1749,13 @@ static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
17411749
spin_unlock(&inode->i_lock);
17421750
}
17431751

1744-
error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, SGP_CACHE,
1752+
sgp = SGP_CACHE;
1753+
if (vma->vm_flags & VM_HUGEPAGE)
1754+
sgp = SGP_HUGE;
1755+
else if (vma->vm_flags & VM_NOHUGEPAGE)
1756+
sgp = SGP_NOHUGE;
1757+
1758+
error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
17451759
gfp, vma->vm_mm, &ret);
17461760
if (error)
17471761
return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);

0 commit comments

Comments
 (0)