Bug 238343 - [FEATURE REQUEST] auto run trim for swap devices before swapon
Summary: [FEATURE REQUEST] auto run trim for swap devices before swapon
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Doug Moore
Depends on:
Reported: 2019-06-05 16:41 UTC by Ivan Rozhuk
Modified: 2020-12-23 01:27 UTC (History)
3 users (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Ivan Rozhuk 2019-06-05 16:41:46 UTC
We have trim command that can send TRIM for device/partition/file.
SWAP partitions have no fs and never get TRIM command on "data delete", this may decrease some SSD~s performance.

trim command may be executed before swapon (after dumpon) to fix this.
Comment 1 Mark Johnston freebsd_committer 2019-06-23 19:35:30 UTC
We now have this in -CURRENT as of r349286, thanks to Doug Moore.  I can't see any reason not to MFC the change to stable/12 for 12.1.
Comment 2 sigsys 2019-07-19 18:32:08 UTC
Small patch to make it work with geli.  Trimming triggered detach-on-last-close.

Index: sbin/swapon/swapon.c
--- sbin/swapon/swapon.c	(revision 350115)
+++ sbin/swapon/swapon.c	(working copy)
@@ -436,7 +436,8 @@
 	struct stat sb;
 	char *dname, *args;
-	int error;
+	const char *ret;
+	int error, fd;
 	error = stat(name, &sb);
@@ -469,7 +470,13 @@
 	} while (0);
-	return (swap_on_off_sfile(name, doingall));
+	/* Hold geli device open to avoid triggering detach on last close. */
+	fd = open(name, O_WRONLY);
+	if (fd < 0)
+		errx(1, "Cannot open %s", name);
+	ret = swap_on_off_sfile(name, doingall);
+	close(fd);
+	return ret;
 static const char *
Comment 3 Ivan Rozhuk 2020-12-23 01:25:35 UTC
trimonce - feature.

/dev/ada0p1.eli none swap sw,trimonce 0 0
Comment 4 Ivan Rozhuk 2020-12-23 01:27:59 UTC
(In reply to sigsys from comment #2)

Is your patch in source tree?
Why it is so important to "Hold geli device open to avoid triggering detach on last close" ? (what this fixes?)