git: 636592343c3e - main - tmpfs: increase memory reserve to a percent of available memory + swap

From: Mike Karels <karels_at_FreeBSD.org>
Date: Tue, 19 Dec 2023 15:34:03 UTC
The branch main has been updated by karels:

URL: https://cgit.FreeBSD.org/src/commit/?id=636592343c3ec0feb61a4d8043676381384420dd

commit 636592343c3ec0feb61a4d8043676381384420dd
Author:     Mike Karels <karels@FreeBSD.org>
AuthorDate: 2023-12-19 15:33:33 +0000
Commit:     Mike Karels <karels@FreeBSD.org>
CommitDate: 2023-12-19 15:33:33 +0000

    tmpfs: increase memory reserve to a percent of available memory + swap
    
    The tmpfs memory reserve defaulted to 4 MB, and other than that,
    all of available memory + swap could be allocated to tmpfs files.
    This was dangerous, as the page daemon attempts to keep some memory
    free, using up swap, and then resulting in processes being killed.
    Increase the reserve to a fraction of available memory + swap at
    file system startup time.  The limit is expressed as a percentage
    of available memory + swap that can be used, and defaults to 95%.
    The percentage can be changed via the vfs.tmpfs.memory_percent sysctl,
    recomputing the reserve with the new percentage but the initial
    available memory + swap.  Note that the reserve can also be set
    directly with an existing sysctl, ignoring the percentage.  The
    previous behavior can be specified by setting vfs.tmpfs.memory_percent
    to 100.
    
    Add sysctl for vfs.tmpfs.memory_percent and the pre-existing
    vfs.tmpfs.memory_reserved to tmpfs(5).
    
    PR:             275436
    MFC after:      1 month
    Reviewed by:    rgrimes
    Differential Revision:  https://reviews.freebsd.org/D43011
---
 share/man/man5/tmpfs.5    | 18 ++++++++++++++++++
 sys/fs/tmpfs/tmpfs.h      |  8 ++++++++
 sys/fs/tmpfs/tmpfs_subr.c | 40 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/share/man/man5/tmpfs.5 b/share/man/man5/tmpfs.5
index c01aefd8550a..f3d6a2f6c2b5 100644
--- a/share/man/man5/tmpfs.5
+++ b/share/man/man5/tmpfs.5
@@ -164,6 +164,24 @@ The default is the mount point's UID.
 Refer to
 .Xr mount 8 .
 .El
+.Sh SYSCTL VARIABLES
+The following
+.Xr sysctl 8
+variables are available:
+.Bl -tag -width indent
+.It Va vfs.tmpfs.memory_percent
+The percentage of memory plus swap space available at kernel file system
+initialization that can be used by a file system with a size of 0.
+When this amount of space in use is reached, new files cannot be created
+and files cannot be extended.
+The default is 95%.
+Changing this value also changes
+.Va vfs.tmpfs.memory_reserved .
+.It Va vfs.tmpfs.memory_reserved
+The currently-reserved amount of memory plus swap space
+based on the memory percentage.
+The minimum is compiled into the system, and defaults to 4 MB.
+.El
 .Sh EXAMPLES
 Mount a
 .Nm
diff --git a/sys/fs/tmpfs/tmpfs.h b/sys/fs/tmpfs/tmpfs.h
index 7ebdffbec84f..5c86a386b9da 100644
--- a/sys/fs/tmpfs/tmpfs.h
+++ b/sys/fs/tmpfs/tmpfs.h
@@ -543,6 +543,14 @@ tmpfs_update(struct vnode *vp)
 #define TMPFS_PAGES_MINRESERVED		(4 * 1024 * 1024 / PAGE_SIZE)
 #endif
 
+/*
+ * Percent of available memory + swap available to use by tmpfs file systems
+ * without a size limit.
+ */
+#if !defined(TMPFS_MEM_PERCENT)
+#define TMPFS_MEM_PERCENT		95
+#endif
+
 /*
  * Amount of memory to reserve for extended attributes.
  */
diff --git a/sys/fs/tmpfs/tmpfs_subr.c b/sys/fs/tmpfs/tmpfs_subr.c
index e255c6488613..c8961a819e7b 100644
--- a/sys/fs/tmpfs/tmpfs_subr.c
+++ b/sys/fs/tmpfs/tmpfs_subr.c
@@ -73,6 +73,9 @@ SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
     "tmpfs file system");
 
 static long tmpfs_pages_reserved = TMPFS_PAGES_MINRESERVED;
+static long tmpfs_pages_avail_init;
+static int tmpfs_mem_percent = TMPFS_MEM_PERCENT;
+static void tmpfs_set_reserve_from_percent(void);
 
 MALLOC_DEFINE(M_TMPFSDIR, "tmpfs dir", "tmpfs dirent structure");
 static uma_zone_t tmpfs_node_pool;
@@ -367,6 +370,9 @@ tmpfs_subr_init(void)
 	    sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor,
 	    tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0);
 	VFS_SMR_ZONE_SET(tmpfs_node_pool);
+
+	tmpfs_pages_avail_init = tmpfs_mem_avail();
+	tmpfs_set_reserve_from_percent();
 	return (0);
 }
 
@@ -401,10 +407,42 @@ sysctl_mem_reserved(SYSCTL_HANDLER_ARGS)
 }
 
 SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_reserved,
-    CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RW, &tmpfs_pages_reserved, 0,
+    CTLTYPE_LONG | CTLFLAG_MPSAFE | CTLFLAG_RW, &tmpfs_pages_reserved, 0,
     sysctl_mem_reserved, "L",
     "Amount of available memory and swap below which tmpfs growth stops");
 
+static int
+sysctl_mem_percent(SYSCTL_HANDLER_ARGS)
+{
+	int error, percent;
+
+	percent = *(int *)arg1;
+	error = sysctl_handle_int(oidp, &percent, 0, req);
+	if (error || !req->newptr)
+		return (error);
+
+	if ((unsigned) percent > 100)
+		return (EINVAL);
+
+	*(long *)arg1 = percent;
+	tmpfs_set_reserve_from_percent();
+	return (0);
+}
+
+static void
+tmpfs_set_reserve_from_percent(void)
+{
+	size_t reserved;
+
+	reserved = tmpfs_pages_avail_init * (100 - tmpfs_mem_percent) / 100;
+	tmpfs_pages_reserved = max(reserved, TMPFS_PAGES_MINRESERVED);
+}
+
+SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_percent,
+    CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, &tmpfs_mem_percent, 0,
+    sysctl_mem_percent, "I",
+    "Percent of available memory that can be used if no size limit");
+
 static __inline int tmpfs_dirtree_cmp(struct tmpfs_dirent *a,
     struct tmpfs_dirent *b);
 RB_PROTOTYPE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);