diff options
Diffstat (limited to 'mm/filemap.c')
-rw-r--r-- | mm/filemap.c | 168 |
1 files changed, 71 insertions, 97 deletions
diff --git a/mm/filemap.c b/mm/filemap.c index 40667c2f3383..1146fcfa3215 100644 --- a/mm/filemap.c +++ b/mm/filemap.c | |||
@@ -126,7 +126,7 @@ static void page_cache_delete(struct address_space *mapping, | |||
126 | /* hugetlb pages are represented by a single entry in the xarray */ | 126 | /* hugetlb pages are represented by a single entry in the xarray */ |
127 | if (!PageHuge(page)) { | 127 | if (!PageHuge(page)) { |
128 | xas_set_order(&xas, page->index, compound_order(page)); | 128 | xas_set_order(&xas, page->index, compound_order(page)); |
129 | nr = 1U << compound_order(page); | 129 | nr = compound_nr(page); |
130 | } | 130 | } |
131 | 131 | ||
132 | VM_BUG_ON_PAGE(!PageLocked(page), page); | 132 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
@@ -203,8 +203,9 @@ static void unaccount_page_cache_page(struct address_space *mapping, | |||
203 | __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr); | 203 | __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr); |
204 | if (PageTransHuge(page)) | 204 | if (PageTransHuge(page)) |
205 | __dec_node_page_state(page, NR_SHMEM_THPS); | 205 | __dec_node_page_state(page, NR_SHMEM_THPS); |
206 | } else { | 206 | } else if (PageTransHuge(page)) { |
207 | VM_BUG_ON_PAGE(PageTransHuge(page), page); | 207 | __dec_node_page_state(page, NR_FILE_THPS); |
208 | filemap_nr_thps_dec(mapping); | ||
208 | } | 209 | } |
209 | 210 | ||
210 | /* | 211 | /* |
@@ -281,11 +282,11 @@ EXPORT_SYMBOL(delete_from_page_cache); | |||
281 | * @pvec: pagevec with pages to delete | 282 | * @pvec: pagevec with pages to delete |
282 | * | 283 | * |
283 | * The function walks over mapping->i_pages and removes pages passed in @pvec | 284 | * The function walks over mapping->i_pages and removes pages passed in @pvec |
284 | * from the mapping. The function expects @pvec to be sorted by page index. | 285 | * from the mapping. The function expects @pvec to be sorted by page index |
286 | * and is optimised for it to be dense. | ||
285 | * It tolerates holes in @pvec (mapping entries at those indices are not | 287 | * It tolerates holes in @pvec (mapping entries at those indices are not |
286 | * modified). The function expects only THP head pages to be present in the | 288 | * modified). The function expects only THP head pages to be present in the |
287 | * @pvec and takes care to delete all corresponding tail pages from the | 289 | * @pvec. |
288 | * mapping as well. | ||
289 | * | 290 | * |
290 | * The function expects the i_pages lock to be held. | 291 | * The function expects the i_pages lock to be held. |
291 | */ | 292 | */ |
@@ -294,40 +295,43 @@ static void page_cache_delete_batch(struct address_space *mapping, | |||
294 | { | 295 | { |
295 | XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index); | 296 | XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index); |
296 | int total_pages = 0; | 297 | int total_pages = 0; |
297 | int i = 0, tail_pages = 0; | 298 | int i = 0; |
298 | struct page *page; | 299 | struct page *page; |
299 | 300 | ||
300 | mapping_set_update(&xas, mapping); | 301 | mapping_set_update(&xas, mapping); |
301 | xas_for_each(&xas, page, ULONG_MAX) { | 302 | xas_for_each(&xas, page, ULONG_MAX) { |
302 | if (i >= pagevec_count(pvec) && !tail_pages) | 303 | if (i >= pagevec_count(pvec)) |
303 | break; | 304 | break; |
305 | |||
306 | /* A swap/dax/shadow entry got inserted? Skip it. */ | ||
304 | if (xa_is_value(page)) | 307 | if (xa_is_value(page)) |
305 | continue; | 308 | continue; |
306 | if (!tail_pages) { | 309 | /* |
307 | /* | 310 | * A page got inserted in our range? Skip it. We have our |
308 | * Some page got inserted in our range? Skip it. We | 311 | * pages locked so they are protected from being removed. |
309 | * have our pages locked so they are protected from | 312 | * If we see a page whose index is higher than ours, it |
310 | * being removed. | 313 | * means our page has been removed, which shouldn't be |
311 | */ | 314 | * possible because we're holding the PageLock. |
312 | if (page != pvec->pages[i]) { | 315 | */ |
313 | VM_BUG_ON_PAGE(page->index > | 316 | if (page != pvec->pages[i]) { |
314 | pvec->pages[i]->index, page); | 317 | VM_BUG_ON_PAGE(page->index > pvec->pages[i]->index, |
315 | continue; | 318 | page); |
316 | } | 319 | continue; |
317 | WARN_ON_ONCE(!PageLocked(page)); | 320 | } |
318 | if (PageTransHuge(page) && !PageHuge(page)) | 321 | |
319 | tail_pages = HPAGE_PMD_NR - 1; | 322 | WARN_ON_ONCE(!PageLocked(page)); |
323 | |||
324 | if (page->index == xas.xa_index) | ||
320 | page->mapping = NULL; | 325 | page->mapping = NULL; |
321 | /* | 326 | /* Leave page->index set: truncation lookup relies on it */ |
322 | * Leave page->index set: truncation lookup relies | 327 | |
323 | * upon it | 328 | /* |
324 | */ | 329 | * Move to the next page in the vector if this is a regular |
330 | * page or the index is of the last sub-page of this compound | ||
331 | * page. | ||
332 | */ | ||
333 | if (page->index + compound_nr(page) - 1 == xas.xa_index) | ||
325 | i++; | 334 | i++; |
326 | } else { | ||
327 | VM_BUG_ON_PAGE(page->index + HPAGE_PMD_NR - tail_pages | ||
328 | != pvec->pages[i]->index, page); | ||
329 | tail_pages--; | ||
330 | } | ||
331 | xas_store(&xas, NULL); | 335 | xas_store(&xas, NULL); |
332 | total_pages++; | 336 | total_pages++; |
333 | } | 337 | } |
@@ -408,7 +412,8 @@ int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, | |||
408 | .range_end = end, | 412 | .range_end = end, |
409 | }; | 413 | }; |
410 | 414 | ||
411 | if (!mapping_cap_writeback_dirty(mapping)) | 415 | if (!mapping_cap_writeback_dirty(mapping) || |
416 | !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) | ||
412 | return 0; | 417 | return 0; |
413 | 418 | ||
414 | wbc_attach_fdatawrite_inode(&wbc, mapping->host); | 419 | wbc_attach_fdatawrite_inode(&wbc, mapping->host); |
@@ -617,10 +622,13 @@ int filemap_fdatawait_keep_errors(struct address_space *mapping) | |||
617 | } | 622 | } |
618 | EXPORT_SYMBOL(filemap_fdatawait_keep_errors); | 623 | EXPORT_SYMBOL(filemap_fdatawait_keep_errors); |
619 | 624 | ||
625 | /* Returns true if writeback might be needed or already in progress. */ | ||
620 | static bool mapping_needs_writeback(struct address_space *mapping) | 626 | static bool mapping_needs_writeback(struct address_space *mapping) |
621 | { | 627 | { |
622 | return (!dax_mapping(mapping) && mapping->nrpages) || | 628 | if (dax_mapping(mapping)) |
623 | (dax_mapping(mapping) && mapping->nrexceptional); | 629 | return mapping->nrexceptional; |
630 | |||
631 | return mapping->nrpages; | ||
624 | } | 632 | } |
625 | 633 | ||
626 | int filemap_write_and_wait(struct address_space *mapping) | 634 | int filemap_write_and_wait(struct address_space *mapping) |
@@ -1516,7 +1524,7 @@ EXPORT_SYMBOL(page_cache_prev_miss); | |||
1516 | struct page *find_get_entry(struct address_space *mapping, pgoff_t offset) | 1524 | struct page *find_get_entry(struct address_space *mapping, pgoff_t offset) |
1517 | { | 1525 | { |
1518 | XA_STATE(xas, &mapping->i_pages, offset); | 1526 | XA_STATE(xas, &mapping->i_pages, offset); |
1519 | struct page *head, *page; | 1527 | struct page *page; |
1520 | 1528 | ||
1521 | rcu_read_lock(); | 1529 | rcu_read_lock(); |
1522 | repeat: | 1530 | repeat: |
@@ -1531,25 +1539,19 @@ repeat: | |||
1531 | if (!page || xa_is_value(page)) | 1539 | if (!page || xa_is_value(page)) |
1532 | goto out; | 1540 | goto out; |
1533 | 1541 | ||
1534 | head = compound_head(page); | 1542 | if (!page_cache_get_speculative(page)) |
1535 | if (!page_cache_get_speculative(head)) | ||
1536 | goto repeat; | 1543 | goto repeat; |
1537 | 1544 | ||
1538 | /* The page was split under us? */ | ||
1539 | if (compound_head(page) != head) { | ||
1540 | put_page(head); | ||
1541 | goto repeat; | ||
1542 | } | ||
1543 | |||
1544 | /* | 1545 | /* |
1545 | * Has the page moved? | 1546 | * Has the page moved or been split? |
1546 | * This is part of the lockless pagecache protocol. See | 1547 | * This is part of the lockless pagecache protocol. See |
1547 | * include/linux/pagemap.h for details. | 1548 | * include/linux/pagemap.h for details. |
1548 | */ | 1549 | */ |
1549 | if (unlikely(page != xas_reload(&xas))) { | 1550 | if (unlikely(page != xas_reload(&xas))) { |
1550 | put_page(head); | 1551 | put_page(page); |
1551 | goto repeat; | 1552 | goto repeat; |
1552 | } | 1553 | } |
1554 | page = find_subpage(page, offset); | ||
1553 | out: | 1555 | out: |
1554 | rcu_read_unlock(); | 1556 | rcu_read_unlock(); |
1555 | 1557 | ||
@@ -1646,7 +1648,7 @@ repeat: | |||
1646 | } | 1648 | } |
1647 | 1649 | ||
1648 | /* Has the page been truncated? */ | 1650 | /* Has the page been truncated? */ |
1649 | if (unlikely(page->mapping != mapping)) { | 1651 | if (unlikely(compound_head(page)->mapping != mapping)) { |
1650 | unlock_page(page); | 1652 | unlock_page(page); |
1651 | put_page(page); | 1653 | put_page(page); |
1652 | goto repeat; | 1654 | goto repeat; |
@@ -1731,7 +1733,6 @@ unsigned find_get_entries(struct address_space *mapping, | |||
1731 | 1733 | ||
1732 | rcu_read_lock(); | 1734 | rcu_read_lock(); |
1733 | xas_for_each(&xas, page, ULONG_MAX) { | 1735 | xas_for_each(&xas, page, ULONG_MAX) { |
1734 | struct page *head; | ||
1735 | if (xas_retry(&xas, page)) | 1736 | if (xas_retry(&xas, page)) |
1736 | continue; | 1737 | continue; |
1737 | /* | 1738 | /* |
@@ -1742,17 +1743,13 @@ unsigned find_get_entries(struct address_space *mapping, | |||
1742 | if (xa_is_value(page)) | 1743 | if (xa_is_value(page)) |
1743 | goto export; | 1744 | goto export; |
1744 | 1745 | ||
1745 | head = compound_head(page); | 1746 | if (!page_cache_get_speculative(page)) |
1746 | if (!page_cache_get_speculative(head)) | ||
1747 | goto retry; | 1747 | goto retry; |
1748 | 1748 | ||
1749 | /* The page was split under us? */ | 1749 | /* Has the page moved or been split? */ |
1750 | if (compound_head(page) != head) | ||
1751 | goto put_page; | ||
1752 | |||
1753 | /* Has the page moved? */ | ||
1754 | if (unlikely(page != xas_reload(&xas))) | 1750 | if (unlikely(page != xas_reload(&xas))) |
1755 | goto put_page; | 1751 | goto put_page; |
1752 | page = find_subpage(page, xas.xa_index); | ||
1756 | 1753 | ||
1757 | export: | 1754 | export: |
1758 | indices[ret] = xas.xa_index; | 1755 | indices[ret] = xas.xa_index; |
@@ -1761,7 +1758,7 @@ export: | |||
1761 | break; | 1758 | break; |
1762 | continue; | 1759 | continue; |
1763 | put_page: | 1760 | put_page: |
1764 | put_page(head); | 1761 | put_page(page); |
1765 | retry: | 1762 | retry: |
1766 | xas_reset(&xas); | 1763 | xas_reset(&xas); |
1767 | } | 1764 | } |
@@ -1803,33 +1800,27 @@ unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start, | |||
1803 | 1800 | ||
1804 | rcu_read_lock(); | 1801 | rcu_read_lock(); |
1805 | xas_for_each(&xas, page, end) { | 1802 | xas_for_each(&xas, page, end) { |
1806 | struct page *head; | ||
1807 | if (xas_retry(&xas, page)) | 1803 | if (xas_retry(&xas, page)) |
1808 | continue; | 1804 | continue; |
1809 | /* Skip over shadow, swap and DAX entries */ | 1805 | /* Skip over shadow, swap and DAX entries */ |
1810 | if (xa_is_value(page)) | 1806 | if (xa_is_value(page)) |
1811 | continue; | 1807 | continue; |
1812 | 1808 | ||
1813 | head = compound_head(page); | 1809 | if (!page_cache_get_speculative(page)) |
1814 | if (!page_cache_get_speculative(head)) | ||
1815 | goto retry; | 1810 | goto retry; |
1816 | 1811 | ||
1817 | /* The page was split under us? */ | 1812 | /* Has the page moved or been split? */ |
1818 | if (compound_head(page) != head) | ||
1819 | goto put_page; | ||
1820 | |||
1821 | /* Has the page moved? */ | ||
1822 | if (unlikely(page != xas_reload(&xas))) | 1813 | if (unlikely(page != xas_reload(&xas))) |
1823 | goto put_page; | 1814 | goto put_page; |
1824 | 1815 | ||
1825 | pages[ret] = page; | 1816 | pages[ret] = find_subpage(page, xas.xa_index); |
1826 | if (++ret == nr_pages) { | 1817 | if (++ret == nr_pages) { |
1827 | *start = xas.xa_index + 1; | 1818 | *start = xas.xa_index + 1; |
1828 | goto out; | 1819 | goto out; |
1829 | } | 1820 | } |
1830 | continue; | 1821 | continue; |
1831 | put_page: | 1822 | put_page: |
1832 | put_page(head); | 1823 | put_page(page); |
1833 | retry: | 1824 | retry: |
1834 | xas_reset(&xas); | 1825 | xas_reset(&xas); |
1835 | } | 1826 | } |
@@ -1874,7 +1865,6 @@ unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index, | |||
1874 | 1865 | ||
1875 | rcu_read_lock(); | 1866 | rcu_read_lock(); |
1876 | for (page = xas_load(&xas); page; page = xas_next(&xas)) { | 1867 | for (page = xas_load(&xas); page; page = xas_next(&xas)) { |
1877 | struct page *head; | ||
1878 | if (xas_retry(&xas, page)) | 1868 | if (xas_retry(&xas, page)) |
1879 | continue; | 1869 | continue; |
1880 | /* | 1870 | /* |
@@ -1884,24 +1874,19 @@ unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index, | |||
1884 | if (xa_is_value(page)) | 1874 | if (xa_is_value(page)) |
1885 | break; | 1875 | break; |
1886 | 1876 | ||
1887 | head = compound_head(page); | 1877 | if (!page_cache_get_speculative(page)) |
1888 | if (!page_cache_get_speculative(head)) | ||
1889 | goto retry; | 1878 | goto retry; |
1890 | 1879 | ||
1891 | /* The page was split under us? */ | 1880 | /* Has the page moved or been split? */ |
1892 | if (compound_head(page) != head) | ||
1893 | goto put_page; | ||
1894 | |||
1895 | /* Has the page moved? */ | ||
1896 | if (unlikely(page != xas_reload(&xas))) | 1881 | if (unlikely(page != xas_reload(&xas))) |
1897 | goto put_page; | 1882 | goto put_page; |
1898 | 1883 | ||
1899 | pages[ret] = page; | 1884 | pages[ret] = find_subpage(page, xas.xa_index); |
1900 | if (++ret == nr_pages) | 1885 | if (++ret == nr_pages) |
1901 | break; | 1886 | break; |
1902 | continue; | 1887 | continue; |
1903 | put_page: | 1888 | put_page: |
1904 | put_page(head); | 1889 | put_page(page); |
1905 | retry: | 1890 | retry: |
1906 | xas_reset(&xas); | 1891 | xas_reset(&xas); |
1907 | } | 1892 | } |
@@ -1937,7 +1922,6 @@ unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index, | |||
1937 | 1922 | ||
1938 | rcu_read_lock(); | 1923 | rcu_read_lock(); |
1939 | xas_for_each_marked(&xas, page, end, tag) { | 1924 | xas_for_each_marked(&xas, page, end, tag) { |
1940 | struct page *head; | ||
1941 | if (xas_retry(&xas, page)) | 1925 | if (xas_retry(&xas, page)) |
1942 | continue; | 1926 | continue; |
1943 | /* | 1927 | /* |
@@ -1948,26 +1932,21 @@ unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index, | |||
1948 | if (xa_is_value(page)) | 1932 | if (xa_is_value(page)) |
1949 | continue; | 1933 | continue; |
1950 | 1934 | ||
1951 | head = compound_head(page); | 1935 | if (!page_cache_get_speculative(page)) |
1952 | if (!page_cache_get_speculative(head)) | ||
1953 | goto retry; | 1936 | goto retry; |
1954 | 1937 | ||
1955 | /* The page was split under us? */ | 1938 | /* Has the page moved or been split? */ |
1956 | if (compound_head(page) != head) | ||
1957 | goto put_page; | ||
1958 | |||
1959 | /* Has the page moved? */ | ||
1960 | if (unlikely(page != xas_reload(&xas))) | 1939 | if (unlikely(page != xas_reload(&xas))) |
1961 | goto put_page; | 1940 | goto put_page; |
1962 | 1941 | ||
1963 | pages[ret] = page; | 1942 | pages[ret] = find_subpage(page, xas.xa_index); |
1964 | if (++ret == nr_pages) { | 1943 | if (++ret == nr_pages) { |
1965 | *index = xas.xa_index + 1; | 1944 | *index = xas.xa_index + 1; |
1966 | goto out; | 1945 | goto out; |
1967 | } | 1946 | } |
1968 | continue; | 1947 | continue; |
1969 | put_page: | 1948 | put_page: |
1970 | put_page(head); | 1949 | put_page(page); |
1971 | retry: | 1950 | retry: |
1972 | xas_reset(&xas); | 1951 | xas_reset(&xas); |
1973 | } | 1952 | } |
@@ -2562,12 +2541,12 @@ retry_find: | |||
2562 | goto out_retry; | 2541 | goto out_retry; |
2563 | 2542 | ||
2564 | /* Did it get truncated? */ | 2543 | /* Did it get truncated? */ |
2565 | if (unlikely(page->mapping != mapping)) { | 2544 | if (unlikely(compound_head(page)->mapping != mapping)) { |
2566 | unlock_page(page); | 2545 | unlock_page(page); |
2567 | put_page(page); | 2546 | put_page(page); |
2568 | goto retry_find; | 2547 | goto retry_find; |
2569 | } | 2548 | } |
2570 | VM_BUG_ON_PAGE(page->index != offset, page); | 2549 | VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page); |
2571 | 2550 | ||
2572 | /* | 2551 | /* |
2573 | * We have a locked page in the page cache, now we need to check | 2552 | * We have a locked page in the page cache, now we need to check |
@@ -2648,7 +2627,7 @@ void filemap_map_pages(struct vm_fault *vmf, | |||
2648 | pgoff_t last_pgoff = start_pgoff; | 2627 | pgoff_t last_pgoff = start_pgoff; |
2649 | unsigned long max_idx; | 2628 | unsigned long max_idx; |
2650 | XA_STATE(xas, &mapping->i_pages, start_pgoff); | 2629 | XA_STATE(xas, &mapping->i_pages, start_pgoff); |
2651 | struct page *head, *page; | 2630 | struct page *page; |
2652 | 2631 | ||
2653 | rcu_read_lock(); | 2632 | rcu_read_lock(); |
2654 | xas_for_each(&xas, page, end_pgoff) { | 2633 | xas_for_each(&xas, page, end_pgoff) { |
@@ -2657,24 +2636,19 @@ void filemap_map_pages(struct vm_fault *vmf, | |||
2657 | if (xa_is_value(page)) | 2636 | if (xa_is_value(page)) |
2658 | goto next; | 2637 | goto next; |
2659 | 2638 | ||
2660 | head = compound_head(page); | ||
2661 | |||
2662 | /* | 2639 | /* |
2663 | * Check for a locked page first, as a speculative | 2640 | * Check for a locked page first, as a speculative |
2664 | * reference may adversely influence page migration. | 2641 | * reference may adversely influence page migration. |
2665 | */ | 2642 | */ |
2666 | if (PageLocked(head)) | 2643 | if (PageLocked(page)) |
2667 | goto next; | 2644 | goto next; |
2668 | if (!page_cache_get_speculative(head)) | 2645 | if (!page_cache_get_speculative(page)) |
2669 | goto next; | 2646 | goto next; |
2670 | 2647 | ||
2671 | /* The page was split under us? */ | 2648 | /* Has the page moved or been split? */ |
2672 | if (compound_head(page) != head) | ||
2673 | goto skip; | ||
2674 | |||
2675 | /* Has the page moved? */ | ||
2676 | if (unlikely(page != xas_reload(&xas))) | 2649 | if (unlikely(page != xas_reload(&xas))) |
2677 | goto skip; | 2650 | goto skip; |
2651 | page = find_subpage(page, xas.xa_index); | ||
2678 | 2652 | ||
2679 | if (!PageUptodate(page) || | 2653 | if (!PageUptodate(page) || |
2680 | PageReadahead(page) || | 2654 | PageReadahead(page) || |