Skip to content

Commit 75edd34

Browse files
Hugh Dickinstorvalds
authored andcommitted
tmpfs: preliminary minor tidyups
Make a few cleanups in mm/shmem.c, before going on to complicate it. shmem_alloc_page() will become more complicated: we can't afford to to have that complication duplicated between a CONFIG_NUMA version and a !CONFIG_NUMA version, so rearrange the #ifdef'ery there to yield a single shmem_swapin() and a single shmem_alloc_page(). Yes, it's a shame to inflict the horrid pseudo-vma on non-NUMA configurations, but eliminating it is a larger cleanup: I have an alloc_pages_mpol() patchset not yet ready - mpol handling is subtle and bug-prone, and changed yet again since my last version. Move __SetPageLocked, __SetPageSwapBacked from shmem_getpage_gfp() to shmem_alloc_page(): that SwapBacked flag will be useful in future, to help to distinguish different cases appropriately. And the SGP_DIRTY variant of SGP_CACHE is hard to understand and of little use (IIRC it dates back to when shmem_getpage() returned the page unlocked): kill it and do the necessary in shmem_file_read_iter(). But an arm64 build then complained that info may be uninitialized (where shmem_getpage_gfp() deletes a freshly alloced page beyond eof), and advancing to an "sgp <= SGP_CACHE" test jogged it back to reality. Signed-off-by: Hugh Dickins <hughd@google.com> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Andres Lagar-Cavilla <andreslc@google.com> Cc: Yang Shi <yang.shi@linaro.org> Cc: Ning Qu <quning@gmail.com> Cc: Mel Gorman <mgorman@techsingularity.net> Cc: Konstantin Khlebnikov <koct9i@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fa9949d commit 75edd34

2 files changed

Lines changed: 32 additions & 43 deletions

File tree

‎include/linux/mempolicy.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ static inline void mpol_free_shared_policy(struct shared_policy *p)
228228
{
229229
}
230230

231+
static inline struct mempolicy *
232+
mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
233+
{
234+
return NULL;
235+
}
236+
231237
#define vma_policy(vma) NULL
232238

233239
static inline int

‎mm/shmem.c‎

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ struct shmem_falloc {
101101
enum sgp_type {
102102
SGP_READ, /* don't exceed i_size, don't allocate page */
103103
SGP_CACHE, /* don't exceed i_size, may allocate page */
104-
SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
105104
SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
106105
SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
107106
};
@@ -169,7 +168,7 @@ static inline int shmem_reacct_size(unsigned long flags,
169168

170169
/*
171170
* ... whereas tmpfs objects are accounted incrementally as
172-
* pages are allocated, in order to allow huge sparse files.
171+
* pages are allocated, in order to allow large sparse files.
173172
* shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
174173
* so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
175174
*/
@@ -947,8 +946,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
947946
return 0;
948947
}
949948

950-
#ifdef CONFIG_NUMA
951-
#ifdef CONFIG_TMPFS
949+
#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
952950
static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
953951
{
954952
char buffer[64];
@@ -972,7 +970,18 @@ static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
972970
}
973971
return mpol;
974972
}
975-
#endif /* CONFIG_TMPFS */
973+
#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
974+
static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
975+
{
976+
}
977+
static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
978+
{
979+
return NULL;
980+
}
981+
#endif /* CONFIG_NUMA && CONFIG_TMPFS */
982+
#ifndef CONFIG_NUMA
983+
#define vm_policy vm_private_data
984+
#endif
976985

977986
static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
978987
struct shmem_inode_info *info, pgoff_t index)
@@ -1008,39 +1017,17 @@ static struct page *shmem_alloc_page(gfp_t gfp,
10081017
pvma.vm_ops = NULL;
10091018
pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
10101019

1011-
page = alloc_page_vma(gfp, &pvma, 0);
1020+
page = alloc_pages_vma(gfp, 0, &pvma, 0, numa_node_id(), false);
1021+
if (page) {
1022+
__SetPageLocked(page);
1023+
__SetPageSwapBacked(page);
1024+
}
10121025

10131026
/* Drop reference taken by mpol_shared_policy_lookup() */
10141027
mpol_cond_put(pvma.vm_policy);
10151028

10161029
return page;
10171030
}
1018-
#else /* !CONFIG_NUMA */
1019-
#ifdef CONFIG_TMPFS
1020-
static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1021-
{
1022-
}
1023-
#endif /* CONFIG_TMPFS */
1024-
1025-
static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1026-
struct shmem_inode_info *info, pgoff_t index)
1027-
{
1028-
return swapin_readahead(swap, gfp, NULL, 0);
1029-
}
1030-
1031-
static inline struct page *shmem_alloc_page(gfp_t gfp,
1032-
struct shmem_inode_info *info, pgoff_t index)
1033-
{
1034-
return alloc_page(gfp);
1035-
}
1036-
#endif /* CONFIG_NUMA */
1037-
1038-
#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
1039-
static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1040-
{
1041-
return NULL;
1042-
}
1043-
#endif
10441031

10451032
/*
10461033
* When a page is moved from swapcache to shmem filecache (either by the
@@ -1084,8 +1071,6 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp,
10841071
copy_highpage(newpage, oldpage);
10851072
flush_dcache_page(newpage);
10861073

1087-
__SetPageLocked(newpage);
1088-
__SetPageSwapBacked(newpage);
10891074
SetPageUptodate(newpage);
10901075
set_page_private(newpage, swap_index);
10911076
SetPageSwapCache(newpage);
@@ -1155,7 +1140,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
11551140
page = NULL;
11561141
}
11571142

1158-
if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
1143+
if (sgp <= SGP_CACHE &&
11591144
((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
11601145
error = -EINVAL;
11611146
goto unlock;
@@ -1275,9 +1260,6 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
12751260
error = -ENOMEM;
12761261
goto decused;
12771262
}
1278-
1279-
__SetPageLocked(page);
1280-
__SetPageSwapBacked(page);
12811263
if (sgp == SGP_WRITE)
12821264
__SetPageReferenced(page);
12831265

@@ -1321,12 +1303,10 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
13211303
flush_dcache_page(page);
13221304
SetPageUptodate(page);
13231305
}
1324-
if (sgp == SGP_DIRTY)
1325-
set_page_dirty(page);
13261306
}
13271307

13281308
/* Perhaps the file has been truncated since we checked */
1329-
if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
1309+
if (sgp <= SGP_CACHE &&
13301310
((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
13311311
if (alloced) {
13321312
ClearPageDirty(page);
@@ -1633,7 +1613,7 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
16331613
* and even mark them dirty, so it cannot exceed the max_blocks limit.
16341614
*/
16351615
if (!iter_is_iovec(to))
1636-
sgp = SGP_DIRTY;
1616+
sgp = SGP_CACHE;
16371617

16381618
index = *ppos >> PAGE_SHIFT;
16391619
offset = *ppos & ~PAGE_MASK;
@@ -1659,8 +1639,11 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
16591639
error = 0;
16601640
break;
16611641
}
1662-
if (page)
1642+
if (page) {
1643+
if (sgp == SGP_CACHE)
1644+
set_page_dirty(page);
16631645
unlock_page(page);
1646+
}
16641647

16651648
/*
16661649
* We must evaluate after, since reads (unlike writes)

0 commit comments

Comments
 (0)