History

mm: introduce num_pages_contiguous()

Let's add a simple helper for determining the number of contiguous pages that represent contiguous PFNs. In an ideal world, this helper would be simpler or not even required. Unfortunately, on some configs we still have to maintain (SPARSEMEM without VMEMMAP), the memmap is allocated per memory section, and we might run into weird corner cases of false positives when blindly testing for contiguous pages only. One example of such false positives would be a memory section-sized hole that does not have a memmap. The surrounding memory sections might get "struct pages" that are contiguous, but the PFNs are actually not. This helper will, for example, be useful for determining contiguous PFNs in a GUP result, to batch further operations across returned "struct page"s. VFIO will utilize this interface to accelerate the VFIO DMA map process. Implementation based on Linus' suggestions to avoid new usage of nth_page() where avoidable.

Li Zhe committed 1 year ago

Commit 929bf011 parent fd946192 files · +42 −1Browse files

Showing the changes to include/linux (2 of 2 files). Show all

Unified · Split

2 files
  1. include/linux/mm.h +6 −1
  2. include/linux/mm_inline.h +36 −0
include/linux/mm.h+6−1
@@ -1833,7 +1833,12 @@ static inline unsigned long memdesc_section(memdesc_flags_t mdf) · 1832 unmodified lines
1833{1833{
1834 return (mdf.f >> SECTIONS_PGSHIFT) & SECTIONS_MASK;1834 return (mdf.f >> SECTIONS_PGSHIFT) & SECTIONS_MASK;
1835}1835}
1836#endif1836#else /* !SECTION_IN_PAGE_FLAGS */
1837static inline unsigned long memdesc_section(memdesc_flags_t mdf)
1838{
1839 return 0;
1840}
1841#endif /* SECTION_IN_PAGE_FLAGS */
1837 1842
1838/**1843/**
1839 * folio_pfn - Return the Page Frame Number of a folio.1844 * folio_pfn - Return the Page Frame Number of a folio.
include/linux/mm_inline.h+36−0
@@ -617,4 +617,40 @@ static inline bool vma_has_recency(const struct vm_area_struct *vma) · 616 unmodified lines
617 return true;617 return true;
618}618}
619 619
620/**
621 * num_pages_contiguous() - determine the number of contiguous pages
622 * that represent contiguous PFNs
623 * @pages: an array of page pointers
624 * @nr_pages: length of the array, at least 1
625 *
626 * Determine the number of contiguous pages that represent contiguous PFNs
627 * in @pages, starting from the first page.
628 *
629 * In some kernel configs contiguous PFNs will not have contiguous struct
630 * pages. In these configurations num_pages_contiguous() will return a num
631 * smaller than ideal number. The caller should continue to check for pfn
632 * contiguity after each call to num_pages_contiguous().
633 *
634 * Returns the number of contiguous pages.
635 */
636static inline size_t num_pages_contiguous(struct page **pages, size_t nr_pages)
637{
638 struct page *cur_page = pages[0];
639 unsigned long section = memdesc_section(cur_page->flags);
640 size_t i;
641
642 for (i = 1; i < nr_pages; i++) {
643 if (++cur_page != pages[i])
644 break;
645 /*
646 * In unproblematic kernel configs, page_to_section() == 0 and
647 * the whole check will get optimized out.
648 */
649 if (memdesc_section(cur_page->flags) != section)
650 break;
651 }
652
653 return i;
654}
655
620#endif656#endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t