From nobody Tue Dec 19 15:34:03 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Svghq5Qkyz54QwK; Tue, 19 Dec 2023 15:34:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Svghq1vXhz3DXS; Tue, 19 Dec 2023 15:34:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703000043; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8jFzdUsyue/mQDWXdHniLO4miHCqQtM098LZes3S9XY=; b=vMXOs9rBsz1gR7vAJhlR6mAg9wUsevRsziWsFbO9JyXkE6clXoEzm5L1zQAiwPJgy4fAkg NwTZFSeJDVoebI4CsVQ//OnZ9bl9sbISuwjCqajRlZlGkfBzOYQZsNWwmXOiyqLjOyMtHy IldDl5kyLmk1WLw4K8xJe89km6CcIjf96GwaUMLgxYvMV0ZkviNN7i6w/sFmUFNHEwIeIY Lt2umtKgQTwVfvnADmhWd6FXQaz4mPcBzmy+PjfaqvbxmJvpRXZ78R/YpfjgMJfo3wVYxW nF13QTVTF4wYlenzDC1f+wjRpKiiSG8OjDaBFvPrwwmX8DoiOX7IN6/LKAZoPA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703000043; a=rsa-sha256; cv=none; b=lwL58mgqMJHRcVTmvVhsVz90qlEXlUy3V8cowIO3B8SMetGmkYGMmRvkMHYNp819z+qfef UhwR5MK7gL9v4AJnb/XGFkdGCUlPr3cJVdNQj+E1yZ32JO8mzaVgdDxiiUQ4brZ06B0ADg QIxlljqjtp+5EJL9KLTLHtqwaSIxAvYVv85RArp9FfOkxH7wl3pwlzWhnFswobuXD/i0DM d+f/h0IezB8U0z8ilqYGSDNpBzU/h6mtKJ0gXNCMi3E2/uNzJCROoC45jR0oQusj9P1STQ QYme3KfnyXxK9190O8KHfoSQh/lzmuNj4zbkBmGzH28V5D2F3yfT/tI+XKoftg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703000043; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8jFzdUsyue/mQDWXdHniLO4miHCqQtM098LZes3S9XY=; b=e/B+5bd5zFA7ZXCCF4qvevjZWYVUojTsWS18ryoV7v2m9C2/yIhBelIaeznjTq7FqrLUHc McemDKnsjnhb1vRrAjplMB8Vnz7VNt5sPVZ6Tw+RiLp5X929Q98T3TPT4tX8p3FpP0NoOb MlgznZJmtbyGtiOS3EE88t2DsZbHlauu3YkEtuivzEP0EU/lLAccuuUNypwtTBCyhDXJ1b 5Wh0iIEqu47ZIUyUDzydzIh0N9S7XO+NVZU0qKpUhVaD6C+MXqCyFOyNDrip4ejwxD2dgl wh2gsZ0h4kRfuulnj6xKtKctkXZbA69lf0qwaLhA3NzO9XtEgkoDc1NtWwyw1g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Svghq10Hpz19Bl; Tue, 19 Dec 2023 15:34:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BJFY3ux030911; Tue, 19 Dec 2023 15:34:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BJFY3fo030908; Tue, 19 Dec 2023 15:34:03 GMT (envelope-from git) Date: Tue, 19 Dec 2023 15:34:03 GMT Message-Id: <202312191534.3BJFY3fo030908@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mike Karels Subject: git: 636592343c3e - main - tmpfs: increase memory reserve to a percent of available memory + swap List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: karels X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 636592343c3ec0feb61a4d8043676381384420dd Auto-Submitted: auto-generated The branch main has been updated by karels: URL: https://cgit.FreeBSD.org/src/commit/?id=636592343c3ec0feb61a4d8043676381384420dd commit 636592343c3ec0feb61a4d8043676381384420dd Author: Mike Karels AuthorDate: 2023-12-19 15:33:33 +0000 Commit: Mike Karels 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);