[RFC v2 5/8] shmem: account for larger blocks sizes for shmem_default_max_blocks()

From: Luis Chamberlain
Date: Fri May 26 2023 - 03:56:36 EST


If we end up supporting a larger block size than PAGE_SIZE the
calculations in shmem_default_max_blocks() need to be modified to take
into account the fact that multiple pages would be required for a
single block.

Today the max number of blocks is computed based on the fact that we
will by default use half of the available memory and each block is of
PAGE_SIZE.

And so we end up with:

totalram_pages() / 2

That's because blocksize == PAGE_SIZE. When blocksize > PAGE_SIZE
we need to consider how how many blocks fit into totalram_pages() first,
then just divide by 2. This ends up being:

totalram_pages * PAGE_SIZE / blocksize / 2
totalram_pages * 2^PAGE_SHIFT / 2^bbits / 2
totalram_pages * 2^(PAGE_SHIFT - bbits - 1)

We know bbits > PAGE_SHIFT so we'll end up with a negative
power of 2. 2^(-some_val). We can factor the -1 out by changing
this to a division of power of 2 and flipping the values for
the signs:

-1 * (PAGE_SHIFT - bbits -1) = (-PAGE_SHIFT + bbits + 1)
= (bbits - PAGE_SHIFT + 1)

And so we end up with:

totalram_pages / 2^(bbits - PAGE_SHIFT + 1)

The bbits is just the block order.

Signed-off-by: Luis Chamberlain <mcgrof@xxxxxxxxxx>
---
mm/shmem.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/mm/shmem.c b/mm/shmem.c
index c124997f8d93..179fde04f57f 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -138,9 +138,11 @@ static u64 shmem_sb_blocksize(struct shmem_sb_info *sbinfo)
return 1UL << sbinfo->block_order;
}

-static unsigned long shmem_default_max_blocks(void)
+static unsigned long shmem_default_max_blocks(unsigned char block_order)
{
- return totalram_pages() / 2;
+ if (block_order == shmem_default_block_order())
+ return totalram_pages() / 2;
+ return totalram_pages() >> (block_order - PAGE_SHIFT + 1);
}

static unsigned long shmem_default_max_inodes(void)
@@ -3905,7 +3907,7 @@ static int shmem_show_options(struct seq_file *seq, struct dentry *root)
{
struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);

- if (sbinfo->max_blocks != shmem_default_max_blocks())
+ if (sbinfo->max_blocks != shmem_default_max_blocks(shmem_default_block_order()))
seq_printf(seq, ",size=%luk",
sbinfo->max_blocks << (PAGE_SHIFT - 10));
if (sbinfo->max_inodes != shmem_default_max_inodes())
@@ -3987,7 +3989,7 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc)
*/
if (!(sb->s_flags & SB_KERNMOUNT)) {
if (!(ctx->seen & SHMEM_SEEN_BLOCKS))
- ctx->blocks = shmem_default_max_blocks();
+ ctx->blocks = shmem_default_max_blocks(shmem_default_block_order());
if (!(ctx->seen & SHMEM_SEEN_INODES))
ctx->inodes = shmem_default_max_inodes();
if (!(ctx->seen & SHMEM_SEEN_INUMS))
--
2.39.2