Skip to content

Commit 61ef186

Browse files
Matthew Wilcox (Oracle)torvalds
authored andcommitted
mm: factor find_get_incore_page out of mincore_page
Patch series "Return head pages from find_*_entry", v2. This patch series started out as part of the THP patch set, but it has some nice effects along the way and it seems worth splitting it out and submitting separately. Currently find_get_entry() and find_lock_entry() return the page corresponding to the requested index, but the first thing most callers do is find the head page, which we just threw away. As part of auditing all the callers, I found some misuses of the APIs and some plain inefficiencies that I've fixed. The diffstat is unflattering, but I added more kernel-doc and a new wrapper. This patch (of 8); Provide this functionality from the swap cache. It's useful for more than just mincore(). Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Hugh Dickins <hughd@google.com> Cc: William Kucharski <william.kucharski@oracle.com> Cc: Jani Nikula <jani.nikula@linux.intel.com> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Chris Wilson <chris@chris-wilson.co.uk> Cc: Matthew Auld <matthew.auld@intel.com> Cc: Huang Ying <ying.huang@intel.com> Link: https://lkml.kernel.org/r/20200910183318.20139-1-willy@infradead.org Link: https://lkml.kernel.org/r/20200910183318.20139-2-willy@infradead.org Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent bac3cf4 commit 61ef186

3 files changed

Lines changed: 41 additions & 26 deletions

File tree

‎include/linux/swap.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ extern void free_pages_and_swap_cache(struct page **, int);
427427
extern struct page *lookup_swap_cache(swp_entry_t entry,
428428
struct vm_area_struct *vma,
429429
unsigned long addr);
430+
struct page *find_get_incore_page(struct address_space *mapping, pgoff_t index);
430431
extern struct page *read_swap_cache_async(swp_entry_t, gfp_t,
431432
struct vm_area_struct *vma, unsigned long addr,
432433
bool do_poll);
@@ -570,6 +571,12 @@ static inline struct page *lookup_swap_cache(swp_entry_t swp,
570571
return NULL;
571572
}
572573

574+
static inline
575+
struct page *find_get_incore_page(struct address_space *mapping, pgoff_t index)
576+
{
577+
return find_get_page(mapping, index);
578+
}
579+
573580
static inline int add_to_swap(struct page *page)
574581
{
575582
return 0;

‎mm/mincore.c‎

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr,
4848
* and is up to date; i.e. that no page-in operation would be required
4949
* at this time if an application were to map and access this page.
5050
*/
51-
static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff)
51+
static unsigned char mincore_page(struct address_space *mapping, pgoff_t index)
5252
{
5353
unsigned char present = 0;
5454
struct page *page;
@@ -59,31 +59,7 @@ static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff)
5959
* any other file mapping (ie. marked !present and faulted in with
6060
* tmpfs's .fault). So swapped out tmpfs mappings are tested here.
6161
*/
62-
#ifdef CONFIG_SWAP
63-
if (shmem_mapping(mapping)) {
64-
page = find_get_entry(mapping, pgoff);
65-
/*
66-
* shmem/tmpfs may return swap: account for swapcache
67-
* page too.
68-
*/
69-
if (xa_is_value(page)) {
70-
swp_entry_t swp = radix_to_swp_entry(page);
71-
struct swap_info_struct *si;
72-
73-
/* Prevent swap device to being swapoff under us */
74-
si = get_swap_device(swp);
75-
if (si) {
76-
page = find_get_page(swap_address_space(swp),
77-
swp_offset(swp));
78-
put_swap_device(si);
79-
} else
80-
page = NULL;
81-
}
82-
} else
83-
page = find_get_page(mapping, pgoff);
84-
#else
85-
page = find_get_page(mapping, pgoff);
86-
#endif
62+
page = find_get_incore_page(mapping, index);
8763
if (page) {
8864
present = PageUptodate(page);
8965
put_page(page);

‎mm/swap_state.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/vmalloc.h>
2222
#include <linux/swap_slots.h>
2323
#include <linux/huge_mm.h>
24+
#include <linux/shmem_fs.h>
2425
#include "internal.h"
2526

2627
/*
@@ -414,6 +415,37 @@ struct page *lookup_swap_cache(swp_entry_t entry, struct vm_area_struct *vma,
414415
return page;
415416
}
416417

418+
/**
419+
* find_get_incore_page - Find and get a page from the page or swap caches.
420+
* @mapping: The address_space to search.
421+
* @index: The page cache index.
422+
*
423+
* This differs from find_get_page() in that it will also look for the
424+
* page in the swap cache.
425+
*
426+
* Return: The found page or %NULL.
427+
*/
428+
struct page *find_get_incore_page(struct address_space *mapping, pgoff_t index)
429+
{
430+
swp_entry_t swp;
431+
struct swap_info_struct *si;
432+
struct page *page = find_get_entry(mapping, index);
433+
434+
if (!xa_is_value(page))
435+
return page;
436+
if (!shmem_mapping(mapping))
437+
return NULL;
438+
439+
swp = radix_to_swp_entry(page);
440+
/* Prevent swapoff from happening to us */
441+
si = get_swap_device(swp);
442+
if (!si)
443+
return NULL;
444+
page = find_get_page(swap_address_space(swp), swp_offset(swp));
445+
put_swap_device(si);
446+
return page;
447+
}
448+
417449
struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
418450
struct vm_area_struct *vma, unsigned long addr,
419451
bool *new_page_allocated)

0 commit comments

Comments
 (0)