folio_zero_user() splits a folio into three page ranges around the faulting page and clears them in order, so that the faulting page and its neighbours are cleared last and stay cache hot. Copying a folio to be mapped at a fault has the same locality requirement, and an upcoming patch converts copy_user_large_folio() to the same scheme. Move the range computation into folio_user_ranges() so that both can share it. Rename FOLIO_ZERO_LOCALITY_RADIUS to FOLIO_LOCALITY_RADIUS and generalise the comment above it, as it now describes both. No functional change. Signed-off-by: Shivank Garg --- mm/memory.c | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index dfa307a9228c..bd2c276e83aa 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -7767,31 +7767,35 @@ static void clear_contig_highpages(struct page *page, unsigned long addr, } /* - * When zeroing a folio, we want to differentiate between pages in the - * vicinity of the faulting address where we have spatial and temporal + * When zeroing or copying a folio, we want to differentiate between pages in + * the vicinity of the faulting address where we have spatial and temporal * locality, and those far away where we don't. * * Use a radius of 2 for determining the local neighbourhood. */ -#define FOLIO_ZERO_LOCALITY_RADIUS 2 +#define FOLIO_LOCALITY_RADIUS 2 -/** - * folio_zero_user - Zero a folio which will be mapped to userspace. - * @folio: The folio to zero. - * @addr_hint: The address accessed by the user or the base address. +/* + * Split a folio into the page index ranges to process, in order: + * + * r[0]: pages to the right of the faulting page's neighbourhood + * r[1]: pages to the left of the faulting page's neighbourhood + * r[2]: the faulting page and its immediate neighbourhood, to be processed + * last to keep its cachelines hot + * + * A range is empty if its length, range_len(), is 0. */ -void folio_zero_user(struct folio *folio, unsigned long addr_hint) +static void folio_user_ranges(struct folio *folio, unsigned long addr_hint, + struct range r[3]) { const unsigned long base_addr = ALIGN_DOWN(addr_hint, folio_size(folio)); const long fault_idx = (addr_hint - base_addr) / PAGE_SIZE; const struct range pg = DEFINE_RANGE(0, folio_nr_pages(folio) - 1); - const long radius = FOLIO_ZERO_LOCALITY_RADIUS; - struct range r[3]; - int i; + const long radius = FOLIO_LOCALITY_RADIUS; /* - * Faulting page and its immediate neighbourhood. Will be cleared at the - * end to keep its cachelines hot. + * Faulting page and its immediate neighbourhood. Will be processed at + * the end to keep its cachelines hot. */ r[2] = DEFINE_RANGE(fault_idx - radius < (long)pg.start ? pg.start : fault_idx - radius, fault_idx + radius > (long)pg.end ? pg.end : fault_idx + radius); @@ -7802,6 +7806,20 @@ void folio_zero_user(struct folio *folio, unsigned long addr_hint) /* Region to the right of the fault: always valid for the common fault_idx=0 case. */ r[0] = DEFINE_RANGE(r[2].end + 1, pg.end); +} + +/** + * folio_zero_user - Zero a folio which will be mapped to userspace. + * @folio: The folio to zero. + * @addr_hint: The address accessed by the user or the base address. + */ +void folio_zero_user(struct folio *folio, unsigned long addr_hint) +{ + const unsigned long base_addr = ALIGN_DOWN(addr_hint, folio_size(folio)); + struct range r[3]; + int i; + + folio_user_ranges(folio, addr_hint, r); for (i = 0; i < ARRAY_SIZE(r); i++) { const unsigned long addr = base_addr + r[i].start * PAGE_SIZE; -- 2.53.0