From patchwork Fri May 26 07:55:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13256583 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4E1EFC7EE31 for ; Fri, 26 May 2023 07:56:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD5AD6B007B; Fri, 26 May 2023 03:56:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8C8F1900002; Fri, 26 May 2023 03:56:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 656E46B0078; Fri, 26 May 2023 03:56:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 494F26B007B for ; Fri, 26 May 2023 03:56:08 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 180F01A0CD1 for ; Fri, 26 May 2023 07:56:08 +0000 (UTC) X-FDA: 80831648016.08.2EA0439 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf20.hostedemail.com (Postfix) with ESMTP id 0868F1C0014 for ; Fri, 26 May 2023 07:56:05 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b="dc/TfmJ1"; dmarc=fail reason="No valid SPF, DKIM not aligned (relaxed)" header.from=kernel.org (policy=none); spf=none (imf20.hostedemail.com: domain of mcgrof@infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=mcgrof@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685087766; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=X5+iEoQSxU/Osj6DTwS+zZOMGO87395DKsZwMMPd0uo=; b=1Ui0L/1j1W5cHDAbNNPifBsppcDXxvtp21ihxUcMHD7w3ZkdaW6h8c6DSdpADAdMiVZ/kH ygkMbNIuB1JRoRcFIJA1AbH9Q9o5afPKbtqDMA72rAG1n2kQ+1hD7a6FqLEz2aOR9xoiBz R59WjKwp6yVH3jj7OCw5Fa/wv/qY/y4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b="dc/TfmJ1"; dmarc=fail reason="No valid SPF, DKIM not aligned (relaxed)" header.from=kernel.org (policy=none); spf=none (imf20.hostedemail.com: domain of mcgrof@infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=mcgrof@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685087766; a=rsa-sha256; cv=none; b=GxN/iFH8dTCgv0TG9huuEy2MM5iZVED3fGPN4zpGi4QNzNV0AycsEsS37lVuZSB7yqJKAM DeD/PPIBk+pjctdS/wJwzYY2JDeX2fsfkay5o1F/aWWvDuKqlmPiG27c39iUXumhUNdeVo kj657eVRJGMunuXJj4umNvuP49y/R+U= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=X5+iEoQSxU/Osj6DTwS+zZOMGO87395DKsZwMMPd0uo=; b=dc/TfmJ1je+wYNn6ZVghaEpsuV hNonEGR/rf9ZvbCAtb5luxqhaZt290ieMP7zWmNl7lupnAdO9iA0HyVxMj1G/RoRzCtvY/M6prI9u lYnSzpeG1xbU/2p54uZWSQ88A0ZYpHndBkZvInbp17tTfW9XSan71VRDhfNHlqoumaNtcu88Wp3k4 v5FcBIFu+40VmHsGhbu9Jdd5zPt1PTCK0tbUA93ZZq2etcKHzdK8JGRPp3spp0ExpIzmQerGEiH7Q xB3QY73a5kLbfciwvYDJY/W2Eb3WRU0xJdfAs6PfVoe8OpyRuSafffwUrlDb8+X5ApvoFmqa5E3Ld TAz2AXvA==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1q2SIj-001WZu-30; Fri, 26 May 2023 07:55:53 +0000 From: Luis Chamberlain To: hughd@google.com, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, djwong@kernel.org Cc: p.raghav@samsung.com, da.gomez@samsung.com, rohan.puri@samsung.com, rpuri.linux@gmail.com, a.manzanares@samsung.com, dave@stgolabs.net, yosryahmed@google.com, keescook@chromium.org, hare@suse.de, kbusch@kernel.org, mcgrof@kernel.org, patches@lists.linux.dev, linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC v2 5/8] shmem: account for larger blocks sizes for shmem_default_max_blocks() Date: Fri, 26 May 2023 00:55:49 -0700 Message-Id: <20230526075552.363524-6-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230526075552.363524-1-mcgrof@kernel.org> References: <20230526075552.363524-1-mcgrof@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0868F1C0014 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 6js1ungmf818iqctwiprsqzyiiim9bsk X-HE-Tag: 1685087765-241511 X-HE-Meta: U2FsdGVkX1+jav/O4jzrHNJu4dTEPO0CVhgFwgfvOxzcg6z2rGffhXFsdGczCZ4IXLUqAQvIndwbM+ysWGboSf3Mp1mcdFbUi3zjR2ncqJixMtEoSWSH5U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Cfd2yVO1rCdu8FNV2ZElKEbDhgs11d25TW6B6GWRaTrejh7QZ4PH6w61/Ot28MivW1WUWYA2047fKMMJEg9ZAXJdqmEf9RusgRIIAe4YZZvqCcjVWWQIuDbvy9jXB8wR4Gj54CbzqIgCamkgaEvmPrrR6fI8Rwb4AxGvsu5p5qGk0rbJJMydpY0e3pFwbKdiDCFYkuLWop5Z1goAUDca7MLoVCDs0R8nchEATBwEvUHSBW4kCAwL9Z8CLdilzr0LXH2BwXjJ9zq1sb/yrwUjdlQw0yxTQPRcUyMY/+9z8q/L/a5QoZIuI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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))