函数源码

Linux Kernel

v5.5.9

Brick Technologies Co., Ltd

Source File:mm\memory.c Create Date:2022-07-27 16:10:19
首页 Copyright©Brick

3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
#ifdef CONFIG_DEBUG_FS
static int fault_around_bytes_get(void *data, u64 *val)
{
    *val = fault_around_bytes;
    return 0;
}
 
/*
 * fault_around_bytes must be rounded down to the nearest page order as it's
 * what do_fault_around() expects to see.
 */
static int fault_around_bytes_set(void *data, u64 val)
{
    if (val / PAGE_SIZE > PTRS_PER_PTE)
        return -EINVAL;
    if (val > PAGE_SIZE)
        fault_around_bytes = rounddown_pow_of_two(val);
    else
        fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
    return 0;
}
DEFINE_DEBUGFS_ATTRIBUTE(fault_around_bytes_fops,
        fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
 
static int __init fault_around_debugfs(void)
{
    debugfs_create_file_unsafe("fault_around_bytes", 0644, NULL, NULL,
                   &fault_around_bytes_fops);
    return 0;
}
late_initcall(fault_around_debugfs);
#endif
 
/*
 * do_fault_around() tries to map few pages around the fault address. The hope
 * is that the pages will be needed soon and this will lower the number of
 * faults to handle.
 *
 * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
 * not ready to be mapped: not up-to-date, locked, etc.
 *
 * This function is called with the page table lock taken. In the split ptlock
 * case the page table lock only protects only those entries which belong to
 * the page table corresponding to the fault address.
 *
 * This function doesn't cross the VMA boundaries, in order to call map_pages()
 * only once.
 *
 * fault_around_bytes defines how many bytes we'll try to map.
 * do_fault_around() expects it to be set to a power of two less than or equal
 * to PTRS_PER_PTE.
 *
 * The virtual address of the area that we map is naturally aligned to
 * fault_around_bytes rounded down to the machine page size
 * (and therefore to page order).  This way it's easier to guarantee
 * that we don't cross page table boundaries.
 */
static vm_fault_t do_fault_around(struct vm_fault *vmf)
{
    unsigned long address = vmf->address, nr_pages, mask;
    pgoff_t start_pgoff = vmf->pgoff;
    pgoff_t end_pgoff;
    int off;
    vm_fault_t ret = 0;
 
    nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
    mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
 
    vmf->address = max(address & mask, vmf->vma->vm_start);
    off = ((address - vmf->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
    start_pgoff -= off;
 
    /*
     *  end_pgoff is either the end of the page table, the end of
     *  the vma or nr_pages from start_pgoff, depending what is nearest.
     */
    end_pgoff = start_pgoff -
        ((vmf->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
        PTRS_PER_PTE - 1;
    end_pgoff = min3(end_pgoff, vma_pages(vmf->vma) + vmf->vma->vm_pgoff - 1,
            start_pgoff + nr_pages - 1);
 
    if (pmd_none(*vmf->pmd)) {
        vmf->prealloc_pte = pte_alloc_one(vmf->vma->vm_mm);
        if (!vmf->prealloc_pte)
            goto out;
        smp_wmb(); /* See comment in __pte_alloc() */
    }
 
    vmf->vma->vm_ops->map_pages(vmf, start_pgoff, end_pgoff);
 
    /* Huge page is mapped? Page fault is solved */
    if (pmd_trans_huge(*vmf->pmd)) {
        ret = VM_FAULT_NOPAGE;
        goto out;
    }
 
    /* ->map_pages() haven't done anything useful. Cold page cache? */
    if (!vmf->pte)
        goto out;
 
    /* check if the page fault is solved */
    vmf->pte -= (vmf->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT);
    if (!pte_none(*vmf->pte))
        ret = VM_FAULT_NOPAGE;
    pte_unmap_unlock(vmf->pte, vmf->ptl);
out:
    vmf->address = address;
    vmf->pte = NULL;
    return ret;
}