Bug 238343

Summary: [FEATURE REQUEST] auto run trim for swap devices before swapon
Product: Base System Reporter: rozhuk.im
Component: binAssignee: Doug Moore <dougm>
Status: In Progress ---    
Severity: Affects Some People CC: eugen, markj, rozhuk.im, sigsys
Priority: ---    
Version: CURRENT   
Hardware: Any   
OS: Any   

Description rozhuk.im 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 *