From: Chris Down <chris@chrisdown.name>
To: linux-mm@kvack.org
Cc: Hugh Dickins <hughd@google.com>,
Andrew Morton <akpm@linux-foundation.org>,
Al Viro <viro@zeniv.linux.org.uk>,
Matthew Wilcox <willy@infradead.org>,
Amir Goldstein <amir73il@gmail.com>,
Jeff Layton <jlayton@kernel.org>,
Johannes Weiner <hannes@cmpxchg.org>, Tejun Heo <tj@kernel.org>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
kernel-team@fb.com
Subject: [PATCH v5 1/2] tmpfs: Add per-superblock i_ino support
Date: Sun, 5 Jan 2020 12:06:00 +0000 [thread overview]
Message-ID: <91b4ed6727712cb6d426cf60c740fe2f473f7638.1578225806.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1578225806.git.chris@chrisdown.name>
get_next_ino has a number of problems:
- It uses and returns a uint, which is susceptible to become overflowed
if a lot of volatile inodes that use get_next_ino are created.
- It's global, with no specificity per-sb or even per-filesystem. This
means it's not that difficult to cause inode number wraparounds on a
single device, which can result in having multiple distinct inodes
with the same inode number.
This patch adds a per-superblock counter that mitigates the second case.
This design also allows us to later have a specific i_ino size
per-device, for example, allowing users to choose whether to use 32- or
64-bit inodes for each tmpfs mount. This is implemented in the next
commit.
Signed-off-by: Chris Down <chris@chrisdown.name>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Jeff Layton <jlayton@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: linux-mm@kvack.org
Cc: linux-fsdevel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: kernel-team@fb.com
---
include/linux/shmem_fs.h | 1 +
mm/shmem.c | 30 +++++++++++++++++++++++++++++-
2 files changed, 30 insertions(+), 1 deletion(-)
v5: Nothing in code, just resending with correct linux-mm domain.
diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
index de8e4b71e3ba..7fac91f490dc 100644
--- a/include/linux/shmem_fs.h
+++ b/include/linux/shmem_fs.h
@@ -35,6 +35,7 @@ struct shmem_sb_info {
unsigned char huge; /* Whether to try for hugepages */
kuid_t uid; /* Mount uid for root directory */
kgid_t gid; /* Mount gid for root directory */
+ ino_t next_ino; /* The next per-sb inode number to use */
struct mempolicy *mpol; /* default memory policy for mappings */
spinlock_t shrinklist_lock; /* Protects shrinklist */
struct list_head shrinklist; /* List of shinkable inodes */
diff --git a/mm/shmem.c b/mm/shmem.c
index 8793e8cc1a48..9e97ba972225 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2236,6 +2236,12 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
return 0;
}
+/*
+ * shmem_get_inode - reserve, allocate, and initialise a new inode
+ *
+ * If this tmpfs is from kern_mount we use get_next_ino, which is global, since
+ * inum churn there is low and this avoids taking locks.
+ */
static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
umode_t mode, dev_t dev, unsigned long flags)
{
@@ -2248,7 +2254,28 @@ static struct inode *shmem_get_inode(struct super_block *sb, const struct inode
inode = new_inode(sb);
if (inode) {
- inode->i_ino = get_next_ino();
+ if (sb->s_flags & SB_KERNMOUNT) {
+ /*
+ * __shmem_file_setup, one of our callers, is lock-free:
+ * it doesn't hold stat_lock in shmem_reserve_inode
+ * since max_inodes is always 0, and is called from
+ * potentially unknown contexts. As such, use the global
+ * allocator which doesn't require the per-sb stat_lock.
+ */
+ inode->i_ino = get_next_ino();
+ } else {
+ spin_lock(&sbinfo->stat_lock);
+ if (unlikely(sbinfo->next_ino > UINT_MAX)) {
+ /*
+ * Emulate get_next_ino uint wraparound for
+ * compatibility
+ */
+ sbinfo->next_ino = 1;
+ }
+ inode->i_ino = sbinfo->next_ino++;
+ spin_unlock(&sbinfo->stat_lock);
+ }
+
inode_init_owner(inode, dir, mode);
inode->i_blocks = 0;
inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
@@ -3662,6 +3689,7 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc)
#else
sb->s_flags |= SB_NOUSER;
#endif
+ sbinfo->next_ino = 1;
sbinfo->max_blocks = ctx->blocks;
sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes;
sbinfo->uid = ctx->uid;
--
2.24.1
next prev parent reply other threads:[~2020-01-05 12:06 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-01-05 12:05 [PATCH v5 0/2] fs: inode: shmem: Reduce risk of inum overflow Chris Down
2020-01-05 12:06 ` Chris Down [this message]
2020-01-06 2:03 ` [PATCH v5 1/2] tmpfs: Add per-superblock i_ino support zhengbin (A)
2020-01-06 6:41 ` Amir Goldstein
2020-01-07 8:01 ` Hugh Dickins
2020-01-07 8:35 ` Amir Goldstein
2020-01-08 10:58 ` Hugh Dickins
2020-01-08 12:51 ` Amir Goldstein
2020-01-06 13:17 ` Chris Down
2020-01-05 12:06 ` [PATCH v5 2/2] tmpfs: Support 64-bit inums per-sb Chris Down
2020-01-07 0:10 ` Dave Chinner
2020-01-07 0:16 ` Chris Down
2020-01-07 0:39 ` Dave Chinner
2020-01-07 6:54 ` Amir Goldstein
2020-01-07 8:36 ` Hugh Dickins
2020-01-07 10:12 ` Amir Goldstein
2020-01-07 21:07 ` Dave Chinner
2020-01-07 21:37 ` Chris Mason
2020-01-08 11:24 ` Hugh Dickins
2020-01-09 0:43 ` Jeff Layton
2020-01-10 16:45 ` Chris Down
2020-01-13 7:36 ` Hugh Dickins
2020-01-20 15:11 ` Chris Down
2020-02-25 23:14 ` Hugh Dickins
2020-01-07 20:59 ` Dave Chinner
2020-01-08 14:37 ` Mikael Magnusson
2020-01-13 6:58 ` Hugh Dickins
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=91b4ed6727712cb6d426cf60c740fe2f473f7638.1578225806.git.chris@chrisdown.name \
--to=chris@chrisdown.name \
--cc=akpm@linux-foundation.org \
--cc=amir73il@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=jlayton@kernel.org \
--cc=kernel-team@fb.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=tj@kernel.org \
--cc=viro@zeniv.linux.org.uk \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).