Commit c5bf121e authored by Vineeth Remanan Pillai's avatar Vineeth Remanan Pillai Committed by Linus Torvalds

mm: refactor swap-in logic out of shmem_getpage_gfp

swapin logic can be reused independently without rest of the logic in
shmem_getpage_gfp.  So lets refactor it out as an independent function.

Link: http://lkml.kernel.org/r/20190114153129.4852-1-vpillai@digitalocean.comSigned-off-by: default avatarVineeth Remanan Pillai <vpillai@digitalocean.com>
Reviewed-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Cc: Huang Ying <ying.huang@intel.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Kelley Nielsen <kelleynnn@gmail.com>
Cc: Rik van Riel <riel@surriel.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent a9e7c39f
...@@ -123,6 +123,10 @@ static unsigned long shmem_default_max_inodes(void) ...@@ -123,6 +123,10 @@ static unsigned long shmem_default_max_inodes(void)
static bool shmem_should_replace_page(struct page *page, gfp_t gfp); static bool shmem_should_replace_page(struct page *page, 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);
static int shmem_swapin_page(struct inode *inode, pgoff_t index,
struct page **pagep, enum sgp_type sgp,
gfp_t gfp, struct vm_area_struct *vma,
vm_fault_t *fault_type);
static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
struct page **pagep, enum sgp_type sgp, struct page **pagep, enum sgp_type sgp,
gfp_t gfp, struct vm_area_struct *vma, gfp_t gfp, struct vm_area_struct *vma,
...@@ -1576,75 +1580,28 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp, ...@@ -1576,75 +1580,28 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp,
} }
/* /*
* shmem_getpage_gfp - find page in cache, or get from swap, or allocate * Swap in the page pointed to by *pagep.
* * Caller has to make sure that *pagep contains a valid swapped page.
* If we allocate a new one we do not mark it dirty. That's up to the * Returns 0 and the page in pagep if success. On failure, returns the
* vm. If we swap it in we mark it dirty since we also free the swap * the error code and NULL in *pagep.
* entry since a page cannot live in both the swap and page cache.
*
* fault_mm and fault_type are only supplied by shmem_fault:
* otherwise they are NULL.
*/ */
static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, static int shmem_swapin_page(struct inode *inode, pgoff_t index,
struct page **pagep, enum sgp_type sgp, gfp_t gfp, struct page **pagep, enum sgp_type sgp,
struct vm_area_struct *vma, struct vm_fault *vmf, gfp_t gfp, struct vm_area_struct *vma,
vm_fault_t *fault_type) vm_fault_t *fault_type)
{ {
struct address_space *mapping = inode->i_mapping; struct address_space *mapping = inode->i_mapping;
struct shmem_inode_info *info = SHMEM_I(inode); struct shmem_inode_info *info = SHMEM_I(inode);
struct shmem_sb_info *sbinfo; struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm;
struct mm_struct *charge_mm;
struct mem_cgroup *memcg; struct mem_cgroup *memcg;
struct page *page; struct page *page;
swp_entry_t swap; swp_entry_t swap;
enum sgp_type sgp_huge = sgp;
pgoff_t hindex = index;
int error; int error;
int once = 0;
int alloced = 0;
if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
return -EFBIG;
if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
sgp = SGP_CACHE;
repeat:
swap.val = 0;
page = find_lock_entry(mapping, index);
if (xa_is_value(page)) {
swap = radix_to_swp_entry(page);
page = NULL;
}
if (sgp <= SGP_CACHE &&
((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
error = -EINVAL;
goto unlock;
}
if (page && sgp == SGP_WRITE)
mark_page_accessed(page);
/* fallocated page? */ VM_BUG_ON(!*pagep || !xa_is_value(*pagep));
if (page && !PageUptodate(page)) { swap = radix_to_swp_entry(*pagep);
if (sgp != SGP_READ) *pagep = NULL;
goto clear;
unlock_page(page);
put_page(page);
page = NULL;
}
if (page || (sgp == SGP_READ && !swap.val)) {
*pagep = page;
return 0;
}
/*
* Fast cache lookup did not find it:
* bring it back from swap or allocate.
*/
sbinfo = SHMEM_SB(inode->i_sb);
charge_mm = vma ? vma->vm_mm : current->mm;
if (swap.val) {
/* Look it up and read it in.. */ /* Look it up and read it in.. */
page = lookup_swap_cache(swap, NULL, 0); page = lookup_swap_cache(swap, NULL, 0);
if (!page) { if (!page) {
...@@ -1666,7 +1623,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, ...@@ -1666,7 +1623,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
lock_page(page); lock_page(page);
if (!PageSwapCache(page) || page_private(page) != swap.val || if (!PageSwapCache(page) || page_private(page) != swap.val ||
!shmem_confirm_swap(mapping, index, swap)) { !shmem_confirm_swap(mapping, index, swap)) {
error = -EEXIST; /* try again */ error = -EEXIST;
goto unlock; goto unlock;
} }
if (!PageUptodate(page)) { if (!PageUptodate(page)) {
...@@ -1695,8 +1652,6 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, ...@@ -1695,8 +1652,6 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
* shmem_undo_range() will have done some of the * shmem_undo_range() will have done some of the
* unaccounting, now delete_from_swap_cache() will do * unaccounting, now delete_from_swap_cache() will do
* the rest. * the rest.
* Reset swap.val? No, leave it so "failed" goes back to
* "repeat": reading a hole and writing should succeed.
*/ */
if (error) { if (error) {
mem_cgroup_cancel_charge(page, memcg, false); mem_cgroup_cancel_charge(page, memcg, false);
...@@ -1720,7 +1675,92 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, ...@@ -1720,7 +1675,92 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
set_page_dirty(page); set_page_dirty(page);
swap_free(swap); swap_free(swap);
} else { *pagep = page;
return 0;
failed:
if (!shmem_confirm_swap(mapping, index, swap))
error = -EEXIST;
unlock:
if (page) {
unlock_page(page);
put_page(page);
}
return error;
}
/*
* shmem_getpage_gfp - find page in cache, or get from swap, or allocate
*
* If we allocate a new one we do not mark it dirty. That's up to the
* vm. If we swap it in we mark it dirty since we also free the swap
* entry since a page cannot live in both the swap and page cache.
*
* fault_mm and fault_type are only supplied by shmem_fault:
* otherwise they are NULL.
*/
static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
struct page **pagep, enum sgp_type sgp, gfp_t gfp,
struct vm_area_struct *vma, struct vm_fault *vmf,
vm_fault_t *fault_type)
{
struct address_space *mapping = inode->i_mapping;
struct shmem_inode_info *info = SHMEM_I(inode);
struct shmem_sb_info *sbinfo;
struct mm_struct *charge_mm;
struct mem_cgroup *memcg;
struct page *page;
enum sgp_type sgp_huge = sgp;
pgoff_t hindex = index;
int error;
int once = 0;
int alloced = 0;
if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
return -EFBIG;
if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
sgp = SGP_CACHE;
repeat:
if (sgp <= SGP_CACHE &&
((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
return -EINVAL;
}
sbinfo = SHMEM_SB(inode->i_sb);
charge_mm = vma ? vma->vm_mm : current->mm;
page = find_lock_entry(mapping, index);
if (xa_is_value(page)) {
error = shmem_swapin_page(inode, index, &page,
sgp, gfp, vma, fault_type);
if (error == -EEXIST)
goto repeat;
*pagep = page;
return error;
}
if (page && sgp == SGP_WRITE)
mark_page_accessed(page);
/* fallocated page? */
if (page && !PageUptodate(page)) {
if (sgp != SGP_READ)
goto clear;
unlock_page(page);
put_page(page);
page = NULL;
}
if (page || sgp == SGP_READ) {
*pagep = page;
return 0;
}
/*
* Fast cache lookup did not find it:
* bring it back from swap or allocate.
*/
if (vma && userfaultfd_missing(vma)) { if (vma && userfaultfd_missing(vma)) {
*fault_type = handle_userfault(vmf, VM_UFFD_MISSING); *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
return 0; return 0;
...@@ -1755,28 +1795,31 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, ...@@ -1755,28 +1795,31 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
alloc_huge: alloc_huge:
page = shmem_alloc_and_acct_page(gfp, inode, index, true); page = shmem_alloc_and_acct_page(gfp, inode, index, true);
if (IS_ERR(page)) { if (IS_ERR(page)) {
alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode, alloc_nohuge:
page = shmem_alloc_and_acct_page(gfp, inode,
index, false); index, false);
} }
if (IS_ERR(page)) { if (IS_ERR(page)) {
int retry = 5; int retry = 5;
error = PTR_ERR(page); error = PTR_ERR(page);
page = NULL; page = NULL;
if (error != -ENOSPC) if (error != -ENOSPC)
goto failed; goto unlock;
/* /*
* Try to reclaim some spece by splitting a huge page * Try to reclaim some space by splitting a huge page
* beyond i_size on the filesystem. * beyond i_size on the filesystem.
*/ */
while (retry--) { while (retry--) {
int ret; int ret;
ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
if (ret == SHRINK_STOP) if (ret == SHRINK_STOP)
break; break;
if (ret) if (ret)
goto alloc_nohuge; goto alloc_nohuge;
} }
goto failed; goto unlock;
} }
if (PageTransHuge(page)) if (PageTransHuge(page))
...@@ -1850,7 +1893,6 @@ alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode, ...@@ -1850,7 +1893,6 @@ alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode,
} }
SetPageUptodate(head); SetPageUptodate(head);
} }
}
/* Perhaps the file has been truncated since we checked */ /* Perhaps the file has been truncated since we checked */
if (sgp <= SGP_CACHE && if (sgp <= SGP_CACHE &&
...@@ -1879,9 +1921,6 @@ alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode, ...@@ -1879,9 +1921,6 @@ alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode,
put_page(page); put_page(page);
goto alloc_nohuge; goto alloc_nohuge;
} }
failed:
if (swap.val && !shmem_confirm_swap(mapping, index, swap))
error = -EEXIST;
unlock: unlock:
if (page) { if (page) {
unlock_page(page); unlock_page(page);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment