提交 907ea17e 编写于 作者: M Matthew Wilcox (Oracle) 提交者: Andrew Morton

shmem: convert shmem_replace_page() to use folios throughout

Introduce folio_set_swap_entry() to abstract how both folio->private and
swp_entry_t work.  Use swap_address_space() directly instead of
indirecting through folio_mapping().  Include an assertion that the old
folio is not large as we only allocate a single-page folio to replace it. 
Use folio_put_refs() instead of calling folio_put() twice.

Link: https://lkml.kernel.org/r/20220902194653.1739778-9-willy@infradead.orgSigned-off-by: NMatthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
上级 4cd400fd
...@@ -355,6 +355,11 @@ static inline swp_entry_t folio_swap_entry(struct folio *folio) ...@@ -355,6 +355,11 @@ static inline swp_entry_t folio_swap_entry(struct folio *folio)
return entry; return entry;
} }
static inline void folio_set_swap_entry(struct folio *folio, swp_entry_t entry)
{
folio->private = (void *)entry.val;
}
/* linux/mm/workingset.c */ /* linux/mm/workingset.c */
void workingset_age_nonresident(struct lruvec *lruvec, unsigned long nr_pages); void workingset_age_nonresident(struct lruvec *lruvec, unsigned long nr_pages);
void *workingset_eviction(struct folio *folio, struct mem_cgroup *target_memcg); void *workingset_eviction(struct folio *folio, struct mem_cgroup *target_memcg);
......
...@@ -1560,12 +1560,6 @@ static struct folio *shmem_alloc_folio(gfp_t gfp, ...@@ -1560,12 +1560,6 @@ static struct folio *shmem_alloc_folio(gfp_t gfp,
return folio; return folio;
} }
static struct page *shmem_alloc_page(gfp_t gfp,
struct shmem_inode_info *info, pgoff_t index)
{
return &shmem_alloc_folio(gfp, info, index)->page;
}
static struct folio *shmem_alloc_and_acct_folio(gfp_t gfp, struct inode *inode, static struct folio *shmem_alloc_and_acct_folio(gfp_t gfp, struct inode *inode,
pgoff_t index, bool huge) pgoff_t index, bool huge)
{ {
...@@ -1617,51 +1611,49 @@ static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp) ...@@ -1617,51 +1611,49 @@ static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp)
static int shmem_replace_page(struct page **pagep, gfp_t gfp, static int shmem_replace_page(struct page **pagep, gfp_t gfp,
struct shmem_inode_info *info, pgoff_t index) struct shmem_inode_info *info, pgoff_t index)
{ {
struct page *oldpage, *newpage;
struct folio *old, *new; struct folio *old, *new;
struct address_space *swap_mapping; struct address_space *swap_mapping;
swp_entry_t entry; swp_entry_t entry;
pgoff_t swap_index; pgoff_t swap_index;
int error; int error;
oldpage = *pagep; old = page_folio(*pagep);
entry.val = page_private(oldpage); entry = folio_swap_entry(old);
swap_index = swp_offset(entry); swap_index = swp_offset(entry);
swap_mapping = page_mapping(oldpage); swap_mapping = swap_address_space(entry);
/* /*
* We have arrived here because our zones are constrained, so don't * We have arrived here because our zones are constrained, so don't
* limit chance of success by further cpuset and node constraints. * limit chance of success by further cpuset and node constraints.
*/ */
gfp &= ~GFP_CONSTRAINT_MASK; gfp &= ~GFP_CONSTRAINT_MASK;
newpage = shmem_alloc_page(gfp, info, index); VM_BUG_ON_FOLIO(folio_test_large(old), old);
if (!newpage) new = shmem_alloc_folio(gfp, info, index);
if (!new)
return -ENOMEM; return -ENOMEM;
get_page(newpage); folio_get(new);
copy_highpage(newpage, oldpage); folio_copy(new, old);
flush_dcache_page(newpage); flush_dcache_folio(new);
__SetPageLocked(newpage); __folio_set_locked(new);
__SetPageSwapBacked(newpage); __folio_set_swapbacked(new);
SetPageUptodate(newpage); folio_mark_uptodate(new);
set_page_private(newpage, entry.val); folio_set_swap_entry(new, entry);
SetPageSwapCache(newpage); folio_set_swapcache(new);
/* /*
* Our caller will very soon move newpage out of swapcache, but it's * Our caller will very soon move newpage out of swapcache, but it's
* a nice clean interface for us to replace oldpage by newpage there. * a nice clean interface for us to replace oldpage by newpage there.
*/ */
xa_lock_irq(&swap_mapping->i_pages); xa_lock_irq(&swap_mapping->i_pages);
error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage); error = shmem_replace_entry(swap_mapping, swap_index, old, new);
if (!error) { if (!error) {
old = page_folio(oldpage);
new = page_folio(newpage);
mem_cgroup_migrate(old, new); mem_cgroup_migrate(old, new);
__inc_lruvec_page_state(newpage, NR_FILE_PAGES); __lruvec_stat_mod_folio(new, NR_FILE_PAGES, 1);
__inc_lruvec_page_state(newpage, NR_SHMEM); __lruvec_stat_mod_folio(new, NR_SHMEM, 1);
__dec_lruvec_page_state(oldpage, NR_FILE_PAGES); __lruvec_stat_mod_folio(old, NR_FILE_PAGES, -1);
__dec_lruvec_page_state(oldpage, NR_SHMEM); __lruvec_stat_mod_folio(old, NR_SHMEM, -1);
} }
xa_unlock_irq(&swap_mapping->i_pages); xa_unlock_irq(&swap_mapping->i_pages);
...@@ -1671,18 +1663,17 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp, ...@@ -1671,18 +1663,17 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp,
* both PageSwapCache and page_private after getting page lock; * both PageSwapCache and page_private after getting page lock;
* but be defensive. Reverse old to newpage for clear and free. * but be defensive. Reverse old to newpage for clear and free.
*/ */
oldpage = newpage; old = new;
} else { } else {
lru_cache_add(newpage); folio_add_lru(new);
*pagep = newpage; *pagep = &new->page;
} }
ClearPageSwapCache(oldpage); folio_clear_swapcache(old);
set_page_private(oldpage, 0); old->private = NULL;
unlock_page(oldpage); folio_unlock(old);
put_page(oldpage); folio_put_refs(old, 2);
put_page(oldpage);
return error; return error;
} }
...@@ -2383,6 +2374,12 @@ static struct inode *shmem_get_inode(struct super_block *sb, struct inode *dir, ...@@ -2383,6 +2374,12 @@ static struct inode *shmem_get_inode(struct super_block *sb, struct inode *dir,
} }
#ifdef CONFIG_USERFAULTFD #ifdef CONFIG_USERFAULTFD
static struct page *shmem_alloc_page(gfp_t gfp,
struct shmem_inode_info *info, pgoff_t index)
{
return &shmem_alloc_folio(gfp, info, index)->page;
}
int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
pmd_t *dst_pmd, pmd_t *dst_pmd,
struct vm_area_struct *dst_vma, struct vm_area_struct *dst_vma,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册