函数源码

Linux Kernel

v5.5.9

Brick Technologies Co., Ltd

Source File:mm\gup.c Create Date:2022-07-27 16:01:37
首页 Copyright©Brick

1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
/*
 * Fast GUP
 *
 * get_user_pages_fast attempts to pin user pages by walking the page
 * tables directly and avoids taking locks. Thus the walker needs to be
 * protected from page table pages being freed from under it, and should
 * block any THP splits.
 *
 * One way to achieve this is to have the walker disable interrupts, and
 * rely on IPIs from the TLB flushing code blocking before the page table
 * pages are freed. This is unsuitable for architectures that do not need
 * to broadcast an IPI when invalidating TLBs.
 *
 * Another way to achieve this is to batch up page table containing pages
 * belonging to more than one mm_user, then rcu_sched a callback to free those
 * pages. Disabling interrupts will allow the fast_gup walker to both block
 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
 * (which is a relatively rare event). The code below adopts this strategy.
 *
 * Before activating this code, please be aware that the following assumptions
 * are currently made:
 *
 *  *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
 *  free pages containing page tables or TLB flushing requires IPI broadcast.
 *
 *  *) ptes can be read atomically by the architecture.
 *
 *  *) access_ok is sufficient to validate userspace address ranges.
 *
 * The last two assumptions can be relaxed by the addition of helper functions.
 *
 * This code is based heavily on the PowerPC implementation by Nick Piggin.
 */
#ifdef CONFIG_HAVE_FAST_GUP
#ifdef CONFIG_GUP_GET_PTE_LOW_HIGH
/*
 * WARNING: only to be used in the get_user_pages_fast() implementation.
 *
 * With get_user_pages_fast(), we walk down the pagetables without taking any
 * locks.  For this we would like to load the pointers atomically, but sometimes
 * that is not possible (e.g. without expensive cmpxchg8b on x86_32 PAE).  What
 * we do have is the guarantee that a PTE will only either go from not present
 * to present, or present to not present or both -- it will not switch to a
 * completely different present page without a TLB flush in between; something
 * that we are blocking by holding interrupts off.
 *
 * Setting ptes from not present to present goes:
 *
 *   ptep->pte_high = h;
 *   smp_wmb();
 *   ptep->pte_low = l;
 *
 * And present to not present goes:
 *
 *   ptep->pte_low = 0;
 *   smp_wmb();
 *   ptep->pte_high = 0;
 *
 * We must ensure here that the load of pte_low sees 'l' IFF pte_high sees 'h'.
 * We load pte_high *after* loading pte_low, which ensures we don't see an older
 * value of pte_high.  *Then* we recheck pte_low, which ensures that we haven't
 * picked up a changed pte high. We might have gotten rubbish values from
 * pte_low and pte_high, but we are guaranteed that pte_low will not have the
 * present bit set *unless* it is 'l'. Because get_user_pages_fast() only
 * operates on present ptes we're safe.
 */
static inline pte_t gup_get_pte(pte_t *ptep)
{
    pte_t pte;
 
    do {
        pte.pte_low = ptep->pte_low;
        smp_rmb();
        pte.pte_high = ptep->pte_high;
        smp_rmb();
    } while (unlikely(pte.pte_low != ptep->pte_low));
 
    return pte;
}
#else /* CONFIG_GUP_GET_PTE_LOW_HIGH */
/*
 * We require that the PTE can be read atomically.
 */
static inline pte_t gup_get_pte(pte_t *ptep)
{
    return READ_ONCE(*ptep);
}
#endif /* CONFIG_GUP_GET_PTE_LOW_HIGH */
 
static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start,
                        struct page **pages)
{
    while ((*nr) - nr_start) {
        struct page *page = pages[--(*nr)];
 
        ClearPageReferenced(page);
        put_page(page);
    }
}
 
/*
 * Return the compund head page with ref appropriately incremented,
 * or NULL if that failed.
 */
static inline struct page *try_get_compound_head(struct page *page, int refs)
{
    struct page *head = compound_head(page);
    if (WARN_ON_ONCE(page_ref_count(head) < 0))
        return NULL;
    if (unlikely(!page_cache_add_speculative(head, refs)))
        return NULL;
    return head;
}
 
#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
             unsigned int flags, struct page **pages, int *nr)
{
    struct dev_pagemap *pgmap = NULL;
    int nr_start = *nr, ret = 0;
    pte_t *ptep, *ptem;
 
    ptem = ptep = pte_offset_map(&pmd, addr);
    do {
        pte_t pte = gup_get_pte(ptep);
        struct page *head, *page;
 
        /*
         * Similar to the PMD case below, NUMA hinting must take slow
         * path using the pte_protnone check.
         */
        if (pte_protnone(pte))
            goto pte_unmap;
 
        if (!pte_access_permitted(pte, flags & FOLL_WRITE))
            goto pte_unmap;
 
        if (pte_devmap(pte)) {
            if (unlikely(flags & FOLL_LONGTERM))
                goto pte_unmap;
 
            pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
            if (unlikely(!pgmap)) {
                undo_dev_pagemap(nr, nr_start, pages);
                goto pte_unmap;
            }
        } else if (pte_special(pte))
            goto pte_unmap;
 
        VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
        page = pte_page(pte);
 
        head = try_get_compound_head(page, 1);
        if (!head)
            goto pte_unmap;
 
        if (unlikely(pte_val(pte) != pte_val(*ptep))) {
            put_page(head);
            goto pte_unmap;
        }
 
        VM_BUG_ON_PAGE(compound_head(page) != head, page);
 
        SetPageReferenced(page);
        pages[*nr] = page;
        (*nr)++;
 
    } while (ptep++, addr += PAGE_SIZE, addr != end);
 
    ret = 1;
 
pte_unmap:
    if (pgmap)
        put_dev_pagemap(pgmap);
    pte_unmap(ptem);
    return ret;
}
#else
 
/*
 * If we can't determine whether or not a pte is special, then fail immediately
 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
 * to be special.
 *
 * For a futex to be placed on a THP tail page, get_futex_key requires a
 * __get_user_pages_fast implementation that can pin pages. Thus it's still
 * useful to have gup_huge_pmd even if we can't operate on ptes.
 */
static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
             unsigned int flags, struct page **pages, int *nr)
{
    return 0;
}
#endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
 
#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
static int __gup_device_huge(unsigned long pfn, unsigned long addr,
        unsigned long end, struct page **pages, int *nr)
{
    int nr_start = *nr;
    struct dev_pagemap *pgmap = NULL;
 
    do {
        struct page *page = pfn_to_page(pfn);
 
        pgmap = get_dev_pagemap(pfn, pgmap);
        if (unlikely(!pgmap)) {
            undo_dev_pagemap(nr, nr_start, pages);
            return 0;
        }
        SetPageReferenced(page);
        pages[*nr] = page;
        get_page(page);
        (*nr)++;
        pfn++;
    } while (addr += PAGE_SIZE, addr != end);
 
    if (pgmap)
        put_dev_pagemap(pgmap);
    return 1;
}
 
static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
        unsigned long end, struct page **pages, int *nr)
{
    unsigned long fault_pfn;
    int nr_start = *nr;
 
    fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
    if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
        return 0;
 
    if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
        undo_dev_pagemap(nr, nr_start, pages);
        return 0;
    }
    return 1;
}
 
static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
        unsigned long end, struct page **pages, int *nr)
{
    unsigned long fault_pfn;
    int nr_start = *nr;
 
    fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
    if (!__gup_device_huge(fault_pfn, addr, end, pages, nr))
        return 0;
 
    if (unlikely(pud_val(orig) != pud_val(*pudp))) {
        undo_dev_pagemap(nr, nr_start, pages);
        return 0;
    }
    return 1;
}
#else
static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
        unsigned long end, struct page **pages, int *nr)
{
    BUILD_BUG();
    return 0;
}
 
static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr,
        unsigned long end, struct page **pages, int *nr)
{
    BUILD_BUG();
    return 0;
}
#endif
 
#ifdef CONFIG_ARCH_HAS_HUGEPD
static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
                      unsigned long sz)
{
    unsigned long __boundary = (addr + sz) & ~(sz-1);
    return (__boundary - 1 < end - 1) ? __boundary : end;
}
 
static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
               unsigned long end, unsigned int flags,
               struct page **pages, int *nr)
{
    unsigned long pte_end;
    struct page *head, *page;
    pte_t pte;
    int refs;
 
    pte_end = (addr + sz) & ~(sz-1);
    if (pte_end < end)
        end = pte_end;
 
    pte = READ_ONCE(*ptep);
 
    if (!pte_access_permitted(pte, flags & FOLL_WRITE))
        return 0;
 
    /* hugepages are never "special" */
    VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
 
    refs = 0;
    head = pte_page(pte);
 
    page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
    do {
        VM_BUG_ON(compound_head(page) != head);
        pages[*nr] = page;
        (*nr)++;
        page++;
        refs++;
    } while (addr += PAGE_SIZE, addr != end);
 
    head = try_get_compound_head(head, refs);
    if (!head) {
        *nr -= refs;
        return 0;
    }
 
    if (unlikely(pte_val(pte) != pte_val(*ptep))) {
        /* Could be optimized better */
        *nr -= refs;
        while (refs--)
            put_page(head);
        return 0;
    }
 
    SetPageReferenced(head);
    return 1;
}
 
static int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
        unsigned int pdshift, unsigned long end, unsigned int flags,
        struct page **pages, int *nr)
{
    pte_t *ptep;
    unsigned long sz = 1UL << hugepd_shift(hugepd);
    unsigned long next;
 
    ptep = hugepte_offset(hugepd, addr, pdshift);
    do {
        next = hugepte_addr_end(addr, end, sz);
        if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr))
            return 0;
    } while (ptep++, addr = next, addr != end);
 
    return 1;
}
#else
static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
        unsigned int pdshift, unsigned long end, unsigned int flags,
        struct page **pages, int *nr)
{
    return 0;
}
#endif /* CONFIG_ARCH_HAS_HUGEPD */
 
static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
            unsigned long end, unsigned int flags,
            struct page **pages, int *nr)
{
    struct page *head, *page;
    int refs;
 
    if (!pmd_access_permitted(orig, flags & FOLL_WRITE))
        return 0;
 
    if (pmd_devmap(orig)) {
        if (unlikely(flags & FOLL_LONGTERM))
            return 0;
        return __gup_device_huge_pmd(orig, pmdp, addr, end, pages, nr);
    }
 
    refs = 0;
    page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
    do {
        pages[*nr] = page;
        (*nr)++;
        page++;
        refs++;
    } while (addr += PAGE_SIZE, addr != end);
 
    head = try_get_compound_head(pmd_page(orig), refs);
    if (!head) {
        *nr -= refs;
        return 0;
    }
 
    if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
        *nr -= refs;
        while (refs--)
            put_page(head);
        return 0;
    }
 
    SetPageReferenced(head);
    return 1;
}
 
static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
        unsigned long end, unsigned int flags, struct page **pages, int *nr)
{
    struct page *head, *page;
    int refs;
 
    if (!pud_access_permitted(orig, flags & FOLL_WRITE))
        return 0;
 
    if (pud_devmap(orig)) {
        if (unlikely(flags & FOLL_LONGTERM))
            return 0;
        return __gup_device_huge_pud(orig, pudp, addr, end, pages, nr);
    }
 
    refs = 0;
    page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
    do {
        pages[*nr] = page;
        (*nr)++;
        page++;
        refs++;
    } while (addr += PAGE_SIZE, addr != end);
 
    head = try_get_compound_head(pud_page(orig), refs);
    if (!head) {
        *nr -= refs;
        return 0;
    }
 
    if (unlikely(pud_val(orig) != pud_val(*pudp))) {
        *nr -= refs;
        while (refs--)
            put_page(head);
        return 0;
    }
 
    SetPageReferenced(head);
    return 1;
}
 
static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
            unsigned long end, unsigned int flags,
            struct page **pages, int *nr)
{
    int refs;
    struct page *head, *page;
 
    if (!pgd_access_permitted(orig, flags & FOLL_WRITE))
        return 0;
 
    BUILD_BUG_ON(pgd_devmap(orig));
    refs = 0;
    page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
    do {
        pages[*nr] = page;
        (*nr)++;
        page++;
        refs++;
    } while (addr += PAGE_SIZE, addr != end);
 
    head = try_get_compound_head(pgd_page(orig), refs);
    if (!head) {
        *nr -= refs;
        return 0;
    }
 
    if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
        *nr -= refs;
        while (refs--)
            put_page(head);
        return 0;
    }
 
    SetPageReferenced(head);
    return 1;
}
 
static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
        unsigned int flags, struct page **pages, int *nr)
{
    unsigned long next;
    pmd_t *pmdp;
 
    pmdp = pmd_offset(&pud, addr);
    do {
        pmd_t pmd = READ_ONCE(*pmdp);
 
        next = pmd_addr_end(addr, end);
        if (!pmd_present(pmd))
            return 0;
 
        if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
                 pmd_devmap(pmd))) {
            /*
             * NUMA hinting faults need to be handled in the GUP
             * slowpath for accounting purposes and so that they
             * can be serialised against THP migration.
             */
            if (pmd_protnone(pmd))
                return 0;
 
            if (!gup_huge_pmd(pmd, pmdp, addr, next, flags,
                pages, nr))
                return 0;
 
        } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
            /*
             * architecture have different format for hugetlbfs
             * pmd format and THP pmd format
             */
            if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
                     PMD_SHIFT, next, flags, pages, nr))
                return 0;
        } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr))
            return 0;
    } while (pmdp++, addr = next, addr != end);
 
    return 1;
}
 
static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end,
             unsigned int flags, struct page **pages, int *nr)
{
    unsigned long next;
    pud_t *pudp;
 
    pudp = pud_offset(&p4d, addr);
    do {
        pud_t pud = READ_ONCE(*pudp);
 
        next = pud_addr_end(addr, end);
        if (pud_none(pud))
            return 0;
        if (unlikely(pud_huge(pud))) {
            if (!gup_huge_pud(pud, pudp, addr, next, flags,
                      pages, nr))
                return 0;
        } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
            if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
                     PUD_SHIFT, next, flags, pages, nr))
                return 0;
        } else if (!gup_pmd_range(pud, addr, next, flags, pages, nr))
            return 0;
    } while (pudp++, addr = next, addr != end);
 
    return 1;
}
 
static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end,
             unsigned int flags, struct page **pages, int *nr)
{
    unsigned long next;
    p4d_t *p4dp;
 
    p4dp = p4d_offset(&pgd, addr);
    do {
        p4d_t p4d = READ_ONCE(*p4dp);
 
        next = p4d_addr_end(addr, end);
        if (p4d_none(p4d))
            return 0;
        BUILD_BUG_ON(p4d_huge(p4d));
        if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) {
            if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr,
                     P4D_SHIFT, next, flags, pages, nr))
                return 0;
        } else if (!gup_pud_range(p4d, addr, next, flags, pages, nr))
            return 0;
    } while (p4dp++, addr = next, addr != end);
 
    return 1;
}
 
static void gup_pgd_range(unsigned long addr, unsigned long end,
        unsigned int flags, struct page **pages, int *nr)
{
    unsigned long next;
    pgd_t *pgdp;
 
    pgdp = pgd_offset(current->mm, addr);
    do {
        pgd_t pgd = READ_ONCE(*pgdp);
 
        next = pgd_addr_end(addr, end);
        if (pgd_none(pgd))
            return;
        if (unlikely(pgd_huge(pgd))) {
            if (!gup_huge_pgd(pgd, pgdp, addr, next, flags,
                      pages, nr))
                return;
        } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
            if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
                     PGDIR_SHIFT, next, flags, pages, nr))
                return;
        } else if (!gup_p4d_range(pgd, addr, next, flags, pages, nr))
            return;
    } while (pgdp++, addr = next, addr != end);
}
#else
static inline void gup_pgd_range(unsigned long addr, unsigned long end,
        unsigned int flags, struct page **pages, int *nr)
{
}