Skip to content

Commit 0060ef3

Browse files
Matthew Wilcox (Oracle)torvalds
authored andcommitted
mm: support THPs in zero_user_segments
We can only kmap() one subpage of a THP at a time, so loop over all relevant subpages, skipping ones which don't need to be zeroed. This is too large to inline when THPs are enabled and we actually need highmem, so put it in highmem.c. [willy@infradead.org: start1 was allowed to be less than start2] Link: https://lkml.kernel.org/r/20201124041507.28996-1-willy@infradead.org Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Yang Shi <shy828301@gmail.com> Cc: Jan Kara <jack@suse.cz> Cc: Michal Hocko <mhocko@suse.com> Cc: Zi Yan <ziy@nvidia.com> Cc: Song Liu <songliubraving@fb.com> Cc: Mel Gorman <mgorman@suse.de> Cc: Naresh Kamboju <naresh.kamboju@linaro.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5e5dda8 commit 0060ef3

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

include/linux/highmem.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,22 @@ static inline void clear_highpage(struct page *page)
284284
kunmap_atomic(kaddr);
285285
}
286286

287+
/*
288+
* If we pass in a base or tail page, we can zero up to PAGE_SIZE.
289+
* If we pass in a head page, we can zero up to the size of the compound page.
290+
*/
291+
#if defined(CONFIG_HIGHMEM) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
292+
void zero_user_segments(struct page *page, unsigned start1, unsigned end1,
293+
unsigned start2, unsigned end2);
294+
#else /* !HIGHMEM || !TRANSPARENT_HUGEPAGE */
287295
static inline void zero_user_segments(struct page *page,
288-
unsigned start1, unsigned end1,
289-
unsigned start2, unsigned end2)
296+
unsigned start1, unsigned end1,
297+
unsigned start2, unsigned end2)
290298
{
291299
void *kaddr = kmap_atomic(page);
300+
unsigned int i;
292301

293-
BUG_ON(end1 > PAGE_SIZE || end2 > PAGE_SIZE);
302+
BUG_ON(end1 > page_size(page) || end2 > page_size(page));
294303

295304
if (end1 > start1)
296305
memset(kaddr + start1, 0, end1 - start1);
@@ -299,8 +308,10 @@ static inline void zero_user_segments(struct page *page,
299308
memset(kaddr + start2, 0, end2 - start2);
300309

301310
kunmap_atomic(kaddr);
302-
flush_dcache_page(page);
311+
for (i = 0; i < compound_nr(page); i++)
312+
flush_dcache_page(page + i);
303313
}
314+
#endif /* !HIGHMEM || !TRANSPARENT_HUGEPAGE */
304315

305316
static inline void zero_user_segment(struct page *page,
306317
unsigned start, unsigned end)

mm/highmem.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,58 @@ void kunmap_high(struct page *page)
369369
}
370370

371371
EXPORT_SYMBOL(kunmap_high);
372+
373+
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
374+
void zero_user_segments(struct page *page, unsigned start1, unsigned end1,
375+
unsigned start2, unsigned end2)
376+
{
377+
unsigned int i;
378+
379+
BUG_ON(end1 > page_size(page) || end2 > page_size(page));
380+
381+
for (i = 0; i < compound_nr(page); i++) {
382+
void *kaddr = NULL;
383+
384+
if (start1 < PAGE_SIZE || start2 < PAGE_SIZE)
385+
kaddr = kmap_atomic(page + i);
386+
387+
if (start1 >= PAGE_SIZE) {
388+
start1 -= PAGE_SIZE;
389+
end1 -= PAGE_SIZE;
390+
} else {
391+
unsigned this_end = min_t(unsigned, end1, PAGE_SIZE);
392+
393+
if (end1 > start1)
394+
memset(kaddr + start1, 0, this_end - start1);
395+
end1 -= this_end;
396+
start1 = 0;
397+
}
398+
399+
if (start2 >= PAGE_SIZE) {
400+
start2 -= PAGE_SIZE;
401+
end2 -= PAGE_SIZE;
402+
} else {
403+
unsigned this_end = min_t(unsigned, end2, PAGE_SIZE);
404+
405+
if (end2 > start2)
406+
memset(kaddr + start2, 0, this_end - start2);
407+
end2 -= this_end;
408+
start2 = 0;
409+
}
410+
411+
if (kaddr) {
412+
kunmap_atomic(kaddr);
413+
flush_dcache_page(page + i);
414+
}
415+
416+
if (!end1 && !end2)
417+
break;
418+
}
419+
420+
BUG_ON((start1 | start2 | end1 | end2) != 0);
421+
}
422+
EXPORT_SYMBOL(zero_user_segments);
423+
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
372424
#endif /* CONFIG_HIGHMEM */
373425

374426
#if defined(HASHED_PAGE_VIRTUAL)

0 commit comments

Comments
 (0)