From patchwork Wed Aug 9 04:32:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 13347420 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 20804C001B0 for ; Wed, 9 Aug 2023 04:32:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B96406B0071; Wed, 9 Aug 2023 00:32:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B47308D0002; Wed, 9 Aug 2023 00:32:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E89F8D0001; Wed, 9 Aug 2023 00:32:27 -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 8FDAD6B0071 for ; Wed, 9 Aug 2023 00:32:27 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 68A518077A for ; Wed, 9 Aug 2023 04:32:27 +0000 (UTC) X-FDA: 81103294734.13.4BEB258 Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) by imf07.hostedemail.com (Postfix) with ESMTP id A3D9C4000D for ; Wed, 9 Aug 2023 04:32:25 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=5Alg+xen; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of hughd@google.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=hughd@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691555545; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fwvZIfkWxNDBAA2VDHz4cV0MGL3i8yqkmn2UYcR8Z9E=; b=1FEOx+ozZVV6JLxxX066JMwVZPXEHcAlc6csDTF+GoaABu4EQQqpDp7aOXKa/fsfOJG+Jz 6HsUexkJNXVJqhwpRfd0cVLcQ9I+ERh0XnJ4wxwT5cql029MmiLQZYHOMbduEEmO1EPnJR TwM2dbfPoH2PVKKVd6ZML+UL/FHyd4U= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=5Alg+xen; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of hughd@google.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=hughd@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691555545; a=rsa-sha256; cv=none; b=hJe3J51jJ3MBDA5wAar40oy/uXN1t7LAbCKcD8SgmPO7LkruuKbuvPOpkiEDmL3/hmMtw0 tbvWKouUJIU9aHuS4MisJgOxKlFQZTloEw1T5jRUld5y4UbwvdprlQooeXgAyh32YBhiNY wGjlcUpc2lGPthYIoSi+5xIpT/G8l7c= Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-583d63ca1e9so73852217b3.1 for ; Tue, 08 Aug 2023 21:32:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691555545; x=1692160345; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=fwvZIfkWxNDBAA2VDHz4cV0MGL3i8yqkmn2UYcR8Z9E=; b=5Alg+xenp1VtdRcTTiXegQmvVx8wy8aB4AnR3TYfFwxSSxxPNENMNSt97yEdGrUomF NVZb/9pCOJoouoToNfrQBTHhZBj1EesKjIvAxP6VCdurukvN0Q8leXUyfg2LOat9CP31 Q5FWQVPkNrMwaPA2S0kCOX/DeVEZl+iG2xcwrtiUoANuf8nGtYz5QN7xi0CTGFxuSqbN PSRfnYgOr2B3jxdIuMkYej0ebVCRGknFkxX6EnXyoSXhG5MW6xZasEahhgUijN4NRvYh V6baPuomPigc6GllJHR8SBcrJsJayUbJgFKFUTJSIvtzrUi6egvr428yal/Y3mjtHP4e Zetw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691555545; x=1692160345; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fwvZIfkWxNDBAA2VDHz4cV0MGL3i8yqkmn2UYcR8Z9E=; b=et0CGzo+9xzFWY2wCQUlrehYfvSKm2vHNM4TMxWjZFE8Rvqq2nEoACceKet0XnsBYp iJwYuM6qebtkFoZw6ysZJVeAOKOH8qiNulDCq0Tln98nJo3vQ1fjLRlUT098S7QKUgel y4u+urbckboPOGyx2XPd3ZNTjFDGbPMeM2M3NkPtdXQABhniOu4rVJqwC1cKbTrFInEW blfEKSOl5npdb+TSuOoGopU1QYec4N9iToUvrmMWjUOwTum2oa40f/UiTNl73t/zxbhX hIXL0aHBTZMQYF3QY+g/rRjjDTZA+4Z7jCIJ646q+eTNSXUw3A8U7UraDjqdWGPjR6Hw AHEg== X-Gm-Message-State: AOJu0Yw4hxGtpEiiT0RogMQbzRncTfJynhB3CeAO6iUY5gDoapHCF/UC ysXrB6OSWHogZYAhqmKYM9z4YQ== X-Google-Smtp-Source: AGHT+IFRq2+oQxP1jY8hLbXzDUaZNnkuiKMnohvmGHSRa3VzhKzvm1cl65r3lJF2CoEm8sENiDLztQ== X-Received: by 2002:a0d:dfd7:0:b0:571:bd3e:73ca with SMTP id i206-20020a0ddfd7000000b00571bd3e73camr1696829ywe.16.1691555544708; Tue, 08 Aug 2023 21:32:24 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id u62-20020a818441000000b0057a5302e2fesm3768585ywf.5.2023.08.08.21.32.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 21:32:24 -0700 (PDT) Date: Tue, 8 Aug 2023 21:32:21 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Christian Brauner cc: Andrew Morton , Oleksandr Tymoshenko , Carlos Maiolino , Jeff Layton , Chuck Lever , Jan Kara , Miklos Szeredi , Daniel Xu , Chris Down , Tejun Heo , Greg Kroah-Hartman , Matthew Wilcox , Christoph Hellwig , Pete Zaitcev , Helge Deller , Topi Miettinen , Yu Kuai , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH vfs.tmpfs 2/5] tmpfs: track free_ispace instead of free_inodes In-Reply-To: Message-ID: <4fe1739-d9e7-8dfd-5bce-12e7339711da@google.com> References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: A3D9C4000D X-Stat-Signature: 93fihad9eimyjts6iq3mf4n3mm3ezjby X-HE-Tag: 1691555545-149925 X-HE-Meta: U2FsdGVkX1+69VehxA3z5Sv6R4WfoFGkB1MrEQdiWjvZHw9THceI5kyd1KgCxsN4ffqxaXnnVemE3uVQVeMEi+z3SPDiwOODbNSaHq7tUVNeaGanYsfOIAZQjO2wQpGQg8mkulZS6mQ8yx7M0vitXnEMrae/XRliYYgzDJIKfMx1AOPU5YvvbVIF7zhSXgH7rY67nzLVMas6XG8FlD2U/yJSsPR7hmPrNLr59UrY8jJfH/9NVt5Q644arZzUlMf8LM/81Z+mrh6geT06BMFlv+mco8nAoVzPd4fdhHxjDtAmjy+9+yLfDhzLhgeAn2WhBbnR7yEX8PHEWE7gJmKrIiy5M5Uwk0OayF/ewXUE1r5SVxZDhBxm/TJOy6hgdglyLZkV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WyrbA81fmfFhmhD44WJMLTDHZUzL+e68PFw== 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: In preparation for assigning some inode space to extended attributes, keep track of free_ispace instead of number of free_inodes: as if one tmpfs inode (and accompanying dentry) occupies very approximately 1KiB. Unsigned long is large enough for free_ispace, on 64-bit and on 32-bit: but take care to enforce the maximum. And fix the nr_blocks maximum on 32-bit: S64_MAX would be too big for it there, so say LONG_MAX instead. Delete the incorrect limited<->unlimited blocks/inodes comment above shmem_reconfigure(): leave it to the error messages below to describe. Signed-off-by: Hugh Dickins Reviewed-by: Jan Kara Reviewed-by: Carlos Maiolino --- include/linux/shmem_fs.h | 2 +- mm/shmem.c | 33 +++++++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 9b2d2faff1d0..6b0c626620f5 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -54,7 +54,7 @@ struct shmem_sb_info { unsigned long max_blocks; /* How many blocks are allowed */ struct percpu_counter used_blocks; /* How many are allocated */ unsigned long max_inodes; /* How many inodes are allowed */ - unsigned long free_inodes; /* How many are left for allocation */ + unsigned long free_ispace; /* How much ispace left for allocation */ raw_spinlock_t stat_lock; /* Serialize shmem_sb_info changes */ umode_t mode; /* Mount mode for root directory */ unsigned char huge; /* Whether to try for hugepages */ diff --git a/mm/shmem.c b/mm/shmem.c index df3cabf54206..c39471384168 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -90,6 +90,9 @@ static struct vfsmount *shm_mnt; /* Pretend that each entry is of this size in directory's i_size */ #define BOGO_DIRENT_SIZE 20 +/* Pretend that one inode + its dentry occupy this much memory */ +#define BOGO_INODE_SIZE 1024 + /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ #define SHORT_SYMLINK_LEN 128 @@ -137,7 +140,8 @@ static unsigned long shmem_default_max_inodes(void) { unsigned long nr_pages = totalram_pages(); - return min(nr_pages - totalhigh_pages(), nr_pages / 2); + return min3(nr_pages - totalhigh_pages(), nr_pages / 2, + ULONG_MAX / BOGO_INODE_SIZE); } #endif @@ -331,11 +335,11 @@ static int shmem_reserve_inode(struct super_block *sb, ino_t *inop) if (!(sb->s_flags & SB_KERNMOUNT)) { raw_spin_lock(&sbinfo->stat_lock); if (sbinfo->max_inodes) { - if (!sbinfo->free_inodes) { + if (sbinfo->free_ispace < BOGO_INODE_SIZE) { raw_spin_unlock(&sbinfo->stat_lock); return -ENOSPC; } - sbinfo->free_inodes--; + sbinfo->free_ispace -= BOGO_INODE_SIZE; } if (inop) { ino = sbinfo->next_ino++; @@ -394,7 +398,7 @@ static void shmem_free_inode(struct super_block *sb) struct shmem_sb_info *sbinfo = SHMEM_SB(sb); if (sbinfo->max_inodes) { raw_spin_lock(&sbinfo->stat_lock); - sbinfo->free_inodes++; + sbinfo->free_ispace += BOGO_INODE_SIZE; raw_spin_unlock(&sbinfo->stat_lock); } } @@ -3155,7 +3159,7 @@ static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) } if (sbinfo->max_inodes) { buf->f_files = sbinfo->max_inodes; - buf->f_ffree = sbinfo->free_inodes; + buf->f_ffree = sbinfo->free_ispace / BOGO_INODE_SIZE; } /* else leave those fields 0 like simple_statfs */ @@ -3815,13 +3819,13 @@ static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) break; case Opt_nr_blocks: ctx->blocks = memparse(param->string, &rest); - if (*rest || ctx->blocks > S64_MAX) + if (*rest || ctx->blocks > LONG_MAX) goto bad_value; ctx->seen |= SHMEM_SEEN_BLOCKS; break; case Opt_nr_inodes: ctx->inodes = memparse(param->string, &rest); - if (*rest) + if (*rest || ctx->inodes > ULONG_MAX / BOGO_INODE_SIZE) goto bad_value; ctx->seen |= SHMEM_SEEN_INODES; break; @@ -4002,21 +4006,17 @@ static int shmem_parse_options(struct fs_context *fc, void *data) /* * Reconfigure a shmem filesystem. - * - * Note that we disallow change from limited->unlimited blocks/inodes while any - * are in use; but we must separately disallow unlimited->limited, because in - * that case we have no record of how much is already in use. */ static int shmem_reconfigure(struct fs_context *fc) { struct shmem_options *ctx = fc->fs_private; struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb); - unsigned long inodes; + unsigned long used_isp; struct mempolicy *mpol = NULL; const char *err; raw_spin_lock(&sbinfo->stat_lock); - inodes = sbinfo->max_inodes - sbinfo->free_inodes; + used_isp = sbinfo->max_inodes * BOGO_INODE_SIZE - sbinfo->free_ispace; if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) { if (!sbinfo->max_blocks) { @@ -4034,7 +4034,7 @@ static int shmem_reconfigure(struct fs_context *fc) err = "Cannot retroactively limit inodes"; goto out; } - if (ctx->inodes < inodes) { + if (ctx->inodes * BOGO_INODE_SIZE < used_isp) { err = "Too few inodes for current use"; goto out; } @@ -4080,7 +4080,7 @@ static int shmem_reconfigure(struct fs_context *fc) sbinfo->max_blocks = ctx->blocks; if (ctx->seen & SHMEM_SEEN_INODES) { sbinfo->max_inodes = ctx->inodes; - sbinfo->free_inodes = ctx->inodes - inodes; + sbinfo->free_ispace = ctx->inodes * BOGO_INODE_SIZE - used_isp; } /* @@ -4211,7 +4211,8 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) sb->s_flags |= SB_NOUSER; #endif sbinfo->max_blocks = ctx->blocks; - sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes; + sbinfo->max_inodes = ctx->inodes; + sbinfo->free_ispace = sbinfo->max_inodes * BOGO_INODE_SIZE; if (sb->s_flags & SB_KERNMOUNT) { sbinfo->ino_batch = alloc_percpu(ino_t); if (!sbinfo->ino_batch)