- * [RFC v2 1/5] shmem: remove check for folio lock on writepage()
  2023-02-23  2:44 [RFC v2 0/5] tmpfs: add the option to disable swap Luis Chamberlain
@ 2023-02-23  2:44 ` Luis Chamberlain
  2023-02-23  2:44 ` [RFC v2 2/5] shmem: set shmem_writepage() variables early Luis Chamberlain
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Luis Chamberlain @ 2023-02-23  2:44 UTC (permalink / raw)
  To: hughd, akpm, willy
  Cc: linux-mm, p.raghav, dave, a.manzanares, yosryahmed, mcgrof,
	linux-kernel
Matthew notes we should not need to check the folio lock
on the writepage() callback so remove it. This sanity check
has been lingering since linux-history days. We remove this
as we tidy up the writepage() callback to make things a bit
clearer.
Suggested-by: Matthew Wilcox <willy@infradead.org>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
 mm/shmem.c | 1 -
 1 file changed, 1 deletion(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index 28f3c699c8ce..b3ad619328bf 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1351,7 +1351,6 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 		folio_clear_dirty(folio);
 	}
 
-	BUG_ON(!folio_test_locked(folio));
 	mapping = folio->mapping;
 	index = folio->index;
 	inode = mapping->host;
-- 
2.39.1
^ permalink raw reply related	[flat|nested] 10+ messages in thread
- * [RFC v2 2/5] shmem: set shmem_writepage() variables early
  2023-02-23  2:44 [RFC v2 0/5] tmpfs: add the option to disable swap Luis Chamberlain
  2023-02-23  2:44 ` [RFC v2 1/5] shmem: remove check for folio lock on writepage() Luis Chamberlain
@ 2023-02-23  2:44 ` Luis Chamberlain
  2023-02-23  2:44 ` [RFC v2 3/5] shmem: move reclaim check early on writepages() Luis Chamberlain
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Luis Chamberlain @ 2023-02-23  2:44 UTC (permalink / raw)
  To: hughd, akpm, willy
  Cc: linux-mm, p.raghav, dave, a.manzanares, yosryahmed, mcgrof,
	linux-kernel
shmem_writepage() sets up variables typically used *after* a possible
huge page split. However even if that does happen the address space
mapping should not change, and the inode does not change either. So it
should be safe to set that from the very beginning.
This commit makes no functional changes.
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
 mm/shmem.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index b3ad619328bf..1269482d0a5c 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1331,9 +1331,9 @@ int shmem_unuse(unsigned int type)
 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 {
 	struct folio *folio = page_folio(page);
-	struct shmem_inode_info *info;
-	struct address_space *mapping;
-	struct inode *inode;
+	struct address_space *mapping = folio->mapping;
+	struct inode *inode = mapping->host;
+	struct shmem_inode_info *info = SHMEM_I(inode);
 	swp_entry_t swap;
 	pgoff_t index;
 
@@ -1351,10 +1351,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 		folio_clear_dirty(folio);
 	}
 
-	mapping = folio->mapping;
 	index = folio->index;
-	inode = mapping->host;
-	info = SHMEM_I(inode);
 	if (info->flags & VM_LOCKED)
 		goto redirty;
 	if (!total_swap_pages)
-- 
2.39.1
^ permalink raw reply related	[flat|nested] 10+ messages in thread
- * [RFC v2 3/5] shmem: move reclaim check early on writepages()
  2023-02-23  2:44 [RFC v2 0/5] tmpfs: add the option to disable swap Luis Chamberlain
  2023-02-23  2:44 ` [RFC v2 1/5] shmem: remove check for folio lock on writepage() Luis Chamberlain
  2023-02-23  2:44 ` [RFC v2 2/5] shmem: set shmem_writepage() variables early Luis Chamberlain
@ 2023-02-23  2:44 ` Luis Chamberlain
  2023-02-23  2:44 ` [RFC v2 4/5] shmem: skip page split if we're not reclaiming Luis Chamberlain
  2023-02-23  2:44 ` [RFC v2 5/5] shmem: add support to ignore swap Luis Chamberlain
  4 siblings, 0 replies; 10+ messages in thread
From: Luis Chamberlain @ 2023-02-23  2:44 UTC (permalink / raw)
  To: hughd, akpm, willy
  Cc: linux-mm, p.raghav, dave, a.manzanares, yosryahmed, mcgrof,
	linux-kernel
i915_gem requires huge folios to be split when swapping.
However we have  check for usage of writepages() to ensure
it used only for swap purposes later. Avoid the splits if
we're not being called for reclaim, even if they should in
theory not happen.
This makes the conditions easier to follow on shem_writepage().
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
 mm/shmem.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index 1269482d0a5c..626eb1a0856c 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1337,6 +1337,18 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 	swp_entry_t swap;
 	pgoff_t index;
 
+	/*
+	 * Our capabilities prevent regular writeback or sync from ever calling
+	 * shmem_writepage; but a stacking filesystem might use ->writepage of
+	 * its underlying filesystem, in which case tmpfs should write out to
+	 * swap only in response to memory pressure, and not for the writeback
+	 * threads or sync.
+	 */
+	if (!wbc->for_reclaim) {
+		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
+		goto redirty;
+	}
+
 	/*
 	 * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "always" or
 	 * "force", drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages,
@@ -1357,18 +1369,6 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 	if (!total_swap_pages)
 		goto redirty;
 
-	/*
-	 * Our capabilities prevent regular writeback or sync from ever calling
-	 * shmem_writepage; but a stacking filesystem might use ->writepage of
-	 * its underlying filesystem, in which case tmpfs should write out to
-	 * swap only in response to memory pressure, and not for the writeback
-	 * threads or sync.
-	 */
-	if (!wbc->for_reclaim) {
-		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
-		goto redirty;
-	}
-
 	/*
 	 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
 	 * value into swapfile.c, the only way we can correctly account for a
-- 
2.39.1
^ permalink raw reply related	[flat|nested] 10+ messages in thread
- * [RFC v2 4/5] shmem: skip page split if we're not reclaiming
  2023-02-23  2:44 [RFC v2 0/5] tmpfs: add the option to disable swap Luis Chamberlain
                   ` (2 preceding siblings ...)
  2023-02-23  2:44 ` [RFC v2 3/5] shmem: move reclaim check early on writepages() Luis Chamberlain
@ 2023-02-23  2:44 ` Luis Chamberlain
  2023-02-23  2:44 ` [RFC v2 5/5] shmem: add support to ignore swap Luis Chamberlain
  4 siblings, 0 replies; 10+ messages in thread
From: Luis Chamberlain @ 2023-02-23  2:44 UTC (permalink / raw)
  To: hughd, akpm, willy
  Cc: linux-mm, p.raghav, dave, a.manzanares, yosryahmed, mcgrof,
	linux-kernel
In theory when info->flags & VM_LOCKED we should not be getting
shem_writepage() called so we should be verifying this with a
WARN_ON_ONCE(). Since we should not be swapping then best to ensure
we also don't do the folio split earlier too. So just move the check
early to avoid folio splits in case its a dubious call.
We also have a similar early bail when !total_swap_pages so just move
that earlier to avoid the possible folio split in the same situation.
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
 mm/shmem.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index 626eb1a0856c..a49b31d38627 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1349,6 +1349,12 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 		goto redirty;
 	}
 
+	if (WARN_ON_ONCE(info->flags & VM_LOCKED))
+		goto redirty;
+
+	if (!total_swap_pages)
+		goto redirty;
+
 	/*
 	 * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "always" or
 	 * "force", drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages,
@@ -1364,10 +1370,6 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 	}
 
 	index = folio->index;
-	if (info->flags & VM_LOCKED)
-		goto redirty;
-	if (!total_swap_pages)
-		goto redirty;
 
 	/*
 	 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
-- 
2.39.1
^ permalink raw reply related	[flat|nested] 10+ messages in thread
- * [RFC v2 5/5] shmem: add support to ignore swap
  2023-02-23  2:44 [RFC v2 0/5] tmpfs: add the option to disable swap Luis Chamberlain
                   ` (3 preceding siblings ...)
  2023-02-23  2:44 ` [RFC v2 4/5] shmem: skip page split if we're not reclaiming Luis Chamberlain
@ 2023-02-23  2:44 ` Luis Chamberlain
  2023-02-23 12:26   ` Christian Brauner
  4 siblings, 1 reply; 10+ messages in thread
From: Luis Chamberlain @ 2023-02-23  2:44 UTC (permalink / raw)
  To: hughd, akpm, willy
  Cc: linux-mm, p.raghav, dave, a.manzanares, yosryahmed, mcgrof,
	linux-kernel
In doing experimentations with shmem having the option to avoid swap
becomes a useful mechanism. One of the *raves* about brd over shmem is
you can avoid swap, but that's not really a good reason to use brd if
we can instead use shmem. Using brd has its own good reasons to exist,
but just because "tmpfs" doesn't let you do that is not a great reason
to avoid it if we can easily add support for it.
I don't add support for reconfiguring incompatible options, but if
we really wanted to we can add support for that.
To avoid swap we use mapping_set_unevictable() upon inode creation,
and put a WARN_ON_ONCE() stop-gap on writepages() for reclaim.
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
 Documentation/mm/unevictable-lru.rst |  2 ++
 include/linux/shmem_fs.h             |  1 +
 mm/shmem.c                           | 26 +++++++++++++++++++++++++-
 3 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/Documentation/mm/unevictable-lru.rst b/Documentation/mm/unevictable-lru.rst
index 53e59433497a..d7e11f492289 100644
--- a/Documentation/mm/unevictable-lru.rst
+++ b/Documentation/mm/unevictable-lru.rst
@@ -44,6 +44,8 @@ The unevictable list addresses the following classes of unevictable pages:
 
  * Those owned by ramfs.
 
+ * Those owned by tmpfs with the noswap option.
+
  * Those mapped into SHM_LOCK'd shared memory regions.
 
  * Those mapped into VM_LOCKED [mlock()ed] VMAs.
diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
index d09d54be4ffd..98a7d53f6cc5 100644
--- a/include/linux/shmem_fs.h
+++ b/include/linux/shmem_fs.h
@@ -45,6 +45,7 @@ struct shmem_sb_info {
 	kuid_t uid;		    /* Mount uid for root directory */
 	kgid_t gid;		    /* Mount gid for root directory */
 	bool full_inums;	    /* If i_ino should be uint or ino_t */
+	bool noswap;	    	    /* ingores VM relcaim / swap requests */
 	ino_t next_ino;		    /* The next per-sb inode number to use */
 	ino_t __percpu *ino_batch;  /* The next per-cpu inode number to use */
 	struct mempolicy *mpol;     /* default memory policy for mappings */
diff --git a/mm/shmem.c b/mm/shmem.c
index a49b31d38627..d2f34147fc66 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -116,10 +116,12 @@ struct shmem_options {
 	bool full_inums;
 	int huge;
 	int seen;
+	bool noswap;
 #define SHMEM_SEEN_BLOCKS 1
 #define SHMEM_SEEN_INODES 2
 #define SHMEM_SEEN_HUGE 4
 #define SHMEM_SEEN_INUMS 8
+#define SHMEM_SEEN_NOSWAP 16
 };
 
 #ifdef CONFIG_TMPFS
@@ -1334,6 +1336,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 	struct address_space *mapping = folio->mapping;
 	struct inode *inode = mapping->host;
 	struct shmem_inode_info *info = SHMEM_I(inode);
+	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
 	swp_entry_t swap;
 	pgoff_t index;
 
@@ -1349,7 +1352,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 		goto redirty;
 	}
 
-	if (WARN_ON_ONCE(info->flags & VM_LOCKED))
+	if (WARN_ON_ONCE((info->flags & VM_LOCKED) || sbinfo->noswap))
 		goto redirty;
 
 	if (!total_swap_pages)
@@ -2374,6 +2377,8 @@ static struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block
 			shmem_set_inode_flags(inode, info->fsflags);
 		INIT_LIST_HEAD(&info->shrinklist);
 		INIT_LIST_HEAD(&info->swaplist);
+		if (sbinfo->noswap)
+			mapping_set_unevictable(inode->i_mapping);
 		simple_xattrs_init(&info->xattrs);
 		cache_no_acl(inode);
 		mapping_set_large_folios(inode->i_mapping);
@@ -3461,6 +3466,7 @@ enum shmem_param {
 	Opt_uid,
 	Opt_inode32,
 	Opt_inode64,
+	Opt_noswap,
 };
 
 static const struct constant_table shmem_param_enums_huge[] = {
@@ -3482,6 +3488,7 @@ const struct fs_parameter_spec shmem_fs_parameters[] = {
 	fsparam_u32   ("uid",		Opt_uid),
 	fsparam_flag  ("inode32",	Opt_inode32),
 	fsparam_flag  ("inode64",	Opt_inode64),
+	fsparam_flag  ("noswap",	Opt_noswap),
 	{}
 };
 
@@ -3565,6 +3572,10 @@ static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param)
 		ctx->full_inums = true;
 		ctx->seen |= SHMEM_SEEN_INUMS;
 		break;
+	case Opt_noswap:
+		ctx->noswap = true;
+		ctx->seen |= SHMEM_SEEN_NOSWAP;
+		break;
 	}
 	return 0;
 
@@ -3663,6 +3674,14 @@ static int shmem_reconfigure(struct fs_context *fc)
 		err = "Current inum too high to switch to 32-bit inums";
 		goto out;
 	}
+	if ((ctx->seen & SHMEM_SEEN_NOSWAP) && ctx->noswap && !sbinfo->noswap) {
+		err = "Cannot disable swap on remount";
+		goto out;
+	}
+	if (!(ctx->seen & SHMEM_SEEN_NOSWAP) && !ctx->noswap && sbinfo->noswap) {
+		err = "Cannot enable swap on remount if it was disabled on first mount";
+		goto out;
+	}
 
 	if (ctx->seen & SHMEM_SEEN_HUGE)
 		sbinfo->huge = ctx->huge;
@@ -3683,6 +3702,10 @@ static int shmem_reconfigure(struct fs_context *fc)
 		sbinfo->mpol = ctx->mpol;	/* transfers initial ref */
 		ctx->mpol = NULL;
 	}
+
+	if (ctx->noswap)
+		sbinfo->noswap = true;
+
 	raw_spin_unlock(&sbinfo->stat_lock);
 	mpol_put(mpol);
 	return 0;
@@ -3780,6 +3803,7 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc)
 			ctx->inodes = shmem_default_max_inodes();
 		if (!(ctx->seen & SHMEM_SEEN_INUMS))
 			ctx->full_inums = IS_ENABLED(CONFIG_TMPFS_INODE64);
+		sbinfo->noswap = ctx->noswap;
 	} else {
 		sb->s_flags |= SB_NOUSER;
 	}
-- 
2.39.1
^ permalink raw reply related	[flat|nested] 10+ messages in thread
- * Re: [RFC v2 5/5] shmem: add support to ignore swap
  2023-02-23  2:44 ` [RFC v2 5/5] shmem: add support to ignore swap Luis Chamberlain
@ 2023-02-23 12:26   ` Christian Brauner
  2023-02-23 15:16     ` Christoph Hellwig
  0 siblings, 1 reply; 10+ messages in thread
From: Christian Brauner @ 2023-02-23 12:26 UTC (permalink / raw)
  To: Luis Chamberlain
  Cc: hughd, akpm, willy, linux-mm, p.raghav, dave, a.manzanares,
	yosryahmed, linux-kernel
On Wed, Feb 22, 2023 at 06:44:12PM -0800, Luis Chamberlain wrote:
> In doing experimentations with shmem having the option to avoid swap
> becomes a useful mechanism. One of the *raves* about brd over shmem is
> you can avoid swap, but that's not really a good reason to use brd if
> we can instead use shmem. Using brd has its own good reasons to exist,
> but just because "tmpfs" doesn't let you do that is not a great reason
> to avoid it if we can easily add support for it.
> 
> I don't add support for reconfiguring incompatible options, but if
> we really wanted to we can add support for that.
> 
> To avoid swap we use mapping_set_unevictable() upon inode creation,
> and put a WARN_ON_ONCE() stop-gap on writepages() for reclaim.
> 
> Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
> ---
We would have use-cases for this in systemd. We currently use ramfs for
systemd's credential logic since ramfs is unswappable. It'd be very neat
if we could use tmpfs instead,
Acked-by: Christian Brauner <brauner@kernel.org>
^ permalink raw reply	[flat|nested] 10+ messages in thread 
- * Re: [RFC v2 5/5] shmem: add support to ignore swap
  2023-02-23 12:26   ` Christian Brauner
@ 2023-02-23 15:16     ` Christoph Hellwig
  2023-02-23 16:09       ` Christian Brauner
  0 siblings, 1 reply; 10+ messages in thread
From: Christoph Hellwig @ 2023-02-23 15:16 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Luis Chamberlain, hughd, akpm, willy, linux-mm, p.raghav, dave,
	a.manzanares, yosryahmed, linux-kernel
On Thu, Feb 23, 2023 at 01:26:31PM +0100, Christian Brauner wrote:
> We would have use-cases for this in systemd. We currently use ramfs for
> systemd's credential logic since ramfs is unswappable. It'd be very neat
> if we could use tmpfs instead,
What is the advantage of using a swapless tmpfs over ramf?
^ permalink raw reply	[flat|nested] 10+ messages in thread 
- * Re: [RFC v2 5/5] shmem: add support to ignore swap
  2023-02-23 15:16     ` Christoph Hellwig
@ 2023-02-23 16:09       ` Christian Brauner
  2023-02-23 19:43         ` Luis Chamberlain
  0 siblings, 1 reply; 10+ messages in thread
From: Christian Brauner @ 2023-02-23 16:09 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Luis Chamberlain, hughd, akpm, willy, linux-mm, p.raghav, dave,
	a.manzanares, yosryahmed, linux-kernel
On Thu, Feb 23, 2023 at 07:16:09AM -0800, Christoph Hellwig wrote:
> On Thu, Feb 23, 2023 at 01:26:31PM +0100, Christian Brauner wrote:
> > We would have use-cases for this in systemd. We currently use ramfs for
> > systemd's credential logic since ramfs is unswappable. It'd be very neat
> > if we could use tmpfs instead,
> 
> What is the advantage of using a swapless tmpfs over ramf?
There are a few reasons we usually prefer tmpfs over ramfs. Iirc, ramfs
doesn't have limits and grows dynamically. So we currently only use it
from the most privileged process where we do our own accounting and
immediately remount the superblock read-only. Tmpfs on the other hand
offers various ways to restrict memory consumption.
Other reasons are that ramfs doesn't support selinux labels, xattrs, and
acls in general which come in quite handy. Starting with kernel v6.3
tmpfs does also support idmapped mounts. So we usually always prefer
ramfs over tmpfs unless we have a very specific need such as the memory
not being swapped out.
^ permalink raw reply	[flat|nested] 10+ messages in thread 
- * Re: [RFC v2 5/5] shmem: add support to ignore swap
  2023-02-23 16:09       ` Christian Brauner
@ 2023-02-23 19:43         ` Luis Chamberlain
  0 siblings, 0 replies; 10+ messages in thread
From: Luis Chamberlain @ 2023-02-23 19:43 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Christoph Hellwig, hughd, akpm, willy, linux-mm, p.raghav, dave,
	a.manzanares, yosryahmed, linux-kernel
On Thu, Feb 23, 2023 at 05:09:14PM +0100, Christian Brauner wrote:
> On Thu, Feb 23, 2023 at 07:16:09AM -0800, Christoph Hellwig wrote:
> > On Thu, Feb 23, 2023 at 01:26:31PM +0100, Christian Brauner wrote:
> > > We would have use-cases for this in systemd. We currently use ramfs for
> > > systemd's credential logic since ramfs is unswappable. It'd be very neat
> > > if we could use tmpfs instead,
> > 
> > What is the advantage of using a swapless tmpfs over ramf?
> 
> There are a few reasons we usually prefer tmpfs over ramfs. Iirc, ramfs
> doesn't have limits and grows dynamically. So we currently only use it
> from the most privileged process where we do our own accounting and
> immediately remount the superblock read-only. Tmpfs on the other hand
> offers various ways to restrict memory consumption.
Size limits is just one bell, in fact ramfs has no configurable options.
So in fact *all* options parsed on shmem_parse_options() are only
available with tmpfs, some of the options are:
  * size
  * number of blocks
  * number of inodes
  * NUMA memory allocation policy
  * huge pages
> Other reasons are that ramfs doesn't support selinux labels, xattrs, and
> acls in general which come in quite handy. Starting with kernel v6.3
> tmpfs does also support idmapped mounts. So we usually always prefer
> ramfs over tmpfs unless we have a very specific need such as the memory
> not being swapped out.
I guess its time to update Documentation/filesystems/tmpfs.rst.
  Luis
^ permalink raw reply	[flat|nested] 10+ messages in thread