Skip to content

Commit bd72205

Browse files
VMoolaakpm00
authored andcommitted
hugetlb: convert hugetlb_wp() to use struct vm_fault
hugetlb_wp() can use the struct vm_fault passed in from hugetlb_fault(). This alleviates the stack by consolidating 5 variables into a single struct. [[email protected]: simplify hugetlb_wp() arguments] Link: https://lkml.kernel.org/r/ZhQtoFNZBNwBCeXn@fedora Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Vishal Moola (Oracle) <[email protected]> Reviewed-by: Oscar Salvador <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Cc: Muchun Song <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 7b6ec18 commit bd72205

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

mm/hugetlb.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5918,19 +5918,18 @@ static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
59185918
* cannot race with other handlers or page migration.
59195919
* Keep the pte_same checks anyway to make transition from the mutex easier.
59205920
*/
5921-
static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
5922-
unsigned long address, pte_t *ptep, unsigned int flags,
5923-
struct folio *pagecache_folio, spinlock_t *ptl,
5921+
static vm_fault_t hugetlb_wp(struct folio *pagecache_folio,
59245922
struct vm_fault *vmf)
59255923
{
5926-
const bool unshare = flags & FAULT_FLAG_UNSHARE;
5927-
pte_t pte = huge_ptep_get(ptep);
5924+
struct vm_area_struct *vma = vmf->vma;
5925+
struct mm_struct *mm = vma->vm_mm;
5926+
const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
5927+
pte_t pte = huge_ptep_get(vmf->pte);
59285928
struct hstate *h = hstate_vma(vma);
59295929
struct folio *old_folio;
59305930
struct folio *new_folio;
59315931
int outside_reserve = 0;
59325932
vm_fault_t ret = 0;
5933-
unsigned long haddr = address & huge_page_mask(h);
59345933
struct mmu_notifier_range range;
59355934

59365935
/*
@@ -5953,7 +5952,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
59535952

59545953
/* Let's take out MAP_SHARED mappings first. */
59555954
if (vma->vm_flags & VM_MAYSHARE) {
5956-
set_huge_ptep_writable(vma, haddr, ptep);
5955+
set_huge_ptep_writable(vma, vmf->address, vmf->pte);
59575956
return 0;
59585957
}
59595958

@@ -5972,7 +5971,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
59725971
SetPageAnonExclusive(&old_folio->page);
59735972
}
59745973
if (likely(!unshare))
5975-
set_huge_ptep_writable(vma, haddr, ptep);
5974+
set_huge_ptep_writable(vma, vmf->address, vmf->pte);
59765975

59775976
delayacct_wpcopy_end();
59785977
return 0;
@@ -5999,8 +5998,8 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
59995998
* Drop page table lock as buddy allocator may be called. It will
60005999
* be acquired again before returning to the caller, as expected.
60016000
*/
6002-
spin_unlock(ptl);
6003-
new_folio = alloc_hugetlb_folio(vma, haddr, outside_reserve);
6001+
spin_unlock(vmf->ptl);
6002+
new_folio = alloc_hugetlb_folio(vma, vmf->address, outside_reserve);
60046003

60056004
if (IS_ERR(new_folio)) {
60066005
/*
@@ -6025,19 +6024,21 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
60256024
*
60266025
* Reacquire both after unmap operation.
60276026
*/
6028-
idx = vma_hugecache_offset(h, vma, haddr);
6027+
idx = vma_hugecache_offset(h, vma, vmf->address);
60296028
hash = hugetlb_fault_mutex_hash(mapping, idx);
60306029
hugetlb_vma_unlock_read(vma);
60316030
mutex_unlock(&hugetlb_fault_mutex_table[hash]);
60326031

6033-
unmap_ref_private(mm, vma, &old_folio->page, haddr);
6032+
unmap_ref_private(mm, vma, &old_folio->page,
6033+
vmf->address);
60346034

60356035
mutex_lock(&hugetlb_fault_mutex_table[hash]);
60366036
hugetlb_vma_lock_read(vma);
6037-
spin_lock(ptl);
6038-
ptep = hugetlb_walk(vma, haddr, huge_page_size(h));
6039-
if (likely(ptep &&
6040-
pte_same(huge_ptep_get(ptep), pte)))
6037+
spin_lock(vmf->ptl);
6038+
vmf->pte = hugetlb_walk(vma, vmf->address,
6039+
huge_page_size(h));
6040+
if (likely(vmf->pte &&
6041+
pte_same(huge_ptep_get(vmf->pte), pte)))
60416042
goto retry_avoidcopy;
60426043
/*
60436044
* race occurs while re-acquiring page table
@@ -6059,50 +6060,51 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
60596060
if (unlikely(ret))
60606061
goto out_release_all;
60616062

6062-
if (copy_user_large_folio(new_folio, old_folio, address, vma)) {
6063+
if (copy_user_large_folio(new_folio, old_folio, vmf->real_address, vma)) {
60636064
ret = VM_FAULT_HWPOISON_LARGE;
60646065
goto out_release_all;
60656066
}
60666067
__folio_mark_uptodate(new_folio);
60676068

6068-
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, haddr,
6069-
haddr + huge_page_size(h));
6069+
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, vmf->address,
6070+
vmf->address + huge_page_size(h));
60706071
mmu_notifier_invalidate_range_start(&range);
60716072

60726073
/*
60736074
* Retake the page table lock to check for racing updates
60746075
* before the page tables are altered
60756076
*/
6076-
spin_lock(ptl);
6077-
ptep = hugetlb_walk(vma, haddr, huge_page_size(h));
6078-
if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {
6077+
spin_lock(vmf->ptl);
6078+
vmf->pte = hugetlb_walk(vma, vmf->address, huge_page_size(h));
6079+
if (likely(vmf->pte && pte_same(huge_ptep_get(vmf->pte), pte))) {
60796080
pte_t newpte = make_huge_pte(vma, &new_folio->page, !unshare);
60806081

60816082
/* Break COW or unshare */
6082-
huge_ptep_clear_flush(vma, haddr, ptep);
6083+
huge_ptep_clear_flush(vma, vmf->address, vmf->pte);
60836084
hugetlb_remove_rmap(old_folio);
6084-
hugetlb_add_new_anon_rmap(new_folio, vma, haddr);
6085+
hugetlb_add_new_anon_rmap(new_folio, vma, vmf->address);
60856086
if (huge_pte_uffd_wp(pte))
60866087
newpte = huge_pte_mkuffd_wp(newpte);
6087-
set_huge_pte_at(mm, haddr, ptep, newpte, huge_page_size(h));
6088+
set_huge_pte_at(mm, vmf->address, vmf->pte, newpte,
6089+
huge_page_size(h));
60886090
folio_set_hugetlb_migratable(new_folio);
60896091
/* Make the old page be freed below */
60906092
new_folio = old_folio;
60916093
}
6092-
spin_unlock(ptl);
6094+
spin_unlock(vmf->ptl);
60936095
mmu_notifier_invalidate_range_end(&range);
60946096
out_release_all:
60956097
/*
60966098
* No restore in case of successful pagetable update (Break COW or
60976099
* unshare)
60986100
*/
60996101
if (new_folio != old_folio)
6100-
restore_reserve_on_error(h, vma, haddr, new_folio);
6102+
restore_reserve_on_error(h, vma, vmf->address, new_folio);
61016103
folio_put(new_folio);
61026104
out_release_old:
61036105
folio_put(old_folio);
61046106

6105-
spin_lock(ptl); /* Caller expects lock to be held */
6107+
spin_lock(vmf->ptl); /* Caller expects lock to be held */
61066108

61076109
delayacct_wpcopy_end();
61086110
return ret;
@@ -6369,8 +6371,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
63696371
hugetlb_count_add(pages_per_huge_page(h), mm);
63706372
if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
63716373
/* Optimization, do the COW without a second fault */
6372-
ret = hugetlb_wp(mm, vma, vmf->real_address, vmf->pte,
6373-
vmf->flags, folio, vmf->ptl, vmf);
6374+
ret = hugetlb_wp(folio, vmf);
63746375
}
63756376

63766377
spin_unlock(vmf->ptl);
@@ -6583,8 +6584,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
65836584

65846585
if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) {
65856586
if (!huge_pte_write(vmf.orig_pte)) {
6586-
ret = hugetlb_wp(mm, vma, address, vmf.pte, flags,
6587-
pagecache_folio, vmf.ptl, &vmf);
6587+
ret = hugetlb_wp(pagecache_folio, &vmf);
65886588
goto out_put_page;
65896589
} else if (likely(flags & FAULT_FLAG_WRITE)) {
65906590
vmf.orig_pte = huge_pte_mkdirty(vmf.orig_pte);

0 commit comments

Comments
 (0)