[RFC PATCH] mm: shm: round up tmpfs size to huge page size when huge=always

From: Yang Shi
Date: Fri Oct 06 2017 - 16:22:28 EST

When passing "huge=always" option for mounting tmpfs, THP is supposed to
be allocated all the time when it can fit, but when the available space is
smaller than the size of THP (2MB on x86), shmem fault handler still tries
to allocate huge page every time, then fallback to regular 4K page
allocation, i.e.:

# mount -t tmpfs -o huge,size=3000k tmpfs /tmp
# dd if=/dev/zero of=/tmp/test bs=1k count=2048
# dd if=/dev/zero of=/tmp/test1 bs=1k count=2048

The last dd command will handle 952 times page fault handler, then exit
with -ENOSPC.

Rounding up tmpfs size to THP size in order to use THP with "always"
more efficiently. And, it will not wast too much memory (just allocate
511 extra pages in worst case).

Signed-off-by: Yang Shi <yang.s@xxxxxxxxxxxxxxx>
mm/shmem.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/mm/shmem.c b/mm/shmem.c
index 07a1d22..b2b595d 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -3567,6 +3567,11 @@ static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
sbinfo->mpol = mpol;
+ /* Round up tmpfs size to huge page size */
+ if (sbinfo->max_blocks && sbinfo->huge == SHMEM_HUGE_ALWAYS)
+ sbinfo->max_blocks = round_up(sbinf->max_blocks, HPAGE_PMD_NR);
return 0;