1- /* $NetBSD: uvm_swap.c,v 1.214 2026/02/13 03:44:49 kre Exp $ */
1+ /* $NetBSD: uvm_swap.c,v 1.215 2026/02/13 19:16:41 kre Exp $ */
22
33/*
44 * Copyright (c) 1995, 1996, 1997, 2009 Matthew R. Green
3030 */
3131
3232#include <sys/cdefs.h>
33- __KERNEL_RCSID (0 , "$NetBSD: uvm_swap.c,v 1.214 2026/02/13 03:44:49 kre Exp $" );
33+ __KERNEL_RCSID (0 , "$NetBSD: uvm_swap.c,v 1.215 2026/02/13 19:16:41 kre Exp $" );
3434
3535#include "opt_uvmhist.h"
3636#include "opt_compat_netbsd.h"
@@ -500,10 +500,11 @@ int (*uvm_swap_stats50)(const struct sys_swapctl_args *, register_t *) =
500500
501501/*
502502 * sys_swapctl: main entry point for swapctl(2) system call
503- * [with two helper functions: swap_on and swap_off ]
503+ * [with three helper functions: swap_on, swap_off and uvm_swap_stats ]
504504 */
505505int
506- sys_swapctl (struct lwp * l , const struct sys_swapctl_args * uap , register_t * retval )
506+ sys_swapctl (struct lwp * l , const struct sys_swapctl_args * uap ,
507+ register_t * retval )
507508{
508509 /* {
509510 syscallarg(int) cmd;
@@ -782,7 +783,7 @@ uvm_swap_stats(char *ptr, int misc,
782783 struct swapdev * sdp , * * sdps , * * sp ;
783784 struct swapent sep ;
784785 size_t sdpsize = 0 ;
785- struct swapdev * stackbuf [8 ];
786+ struct swapdev * stackbuf [8 ]; /* magic 8, any number >1 will do */
786787 int count , slots ;
787788 int error ;
788789
@@ -799,7 +800,7 @@ uvm_swap_stats(char *ptr, int misc,
799800 KASSERT (rw_lock_held (& swap_syscall_lock ));
800801
801802 /*
802- * Allocate space for pointers to all swapdevs
803+ * Allocate space (slots) for pointers to all swapdevs
803804 *
804805 * This needs to be done here (not earlier) (and so needs
805806 * the unlock/lock dance) because of the way the various
@@ -820,16 +821,18 @@ uvm_swap_stats(char *ptr, int misc,
820821 return E2BIG ;
821822
822823 /*
823- * One slot for each currently existing swap device,
824- * but limited no more than the request wants (misc).
824+ * One slot for each currently existing swap device, but
825+ * limited (above) to no more than the request wants (misc).
826+ * Each slot needs space for a pointer to a swapdev.
825827 */
826828 sdpsize = (size_t )slots * sizeof sdp ;
827829
828830 /*
829- * Borrow from kmem_tmpbuf_alloc() but don't use it
831+ * Borrow from kmem_tmpbuf_alloc(9 ) but don't use that
830832 * so we don't need to do the unlock dance unnecessarily
831833 */
832834 if (sdpsize <= sizeof stackbuf ) {
835+ /* Should be the common case */
833836 sdps = stackbuf ;
834837 } else {
835838 rw_exit (& swap_syscall_lock );
@@ -841,48 +844,43 @@ uvm_swap_stats(char *ptr, int misc,
841844 /*
842845 * At this point, 3 possibilities.
843846 *
844- * uvmexp.nswapdev has increased, as a
845- * new swap device got added. That's OK,
846- * just ignore the excess device, and
847- * return the first N (the number that
848- * were there when we started.
847+ * 1. uvmexp.nswapdev has increased.
849848 *
850- * uvmexp.nswapdev has decreased, a swap
851- * device was deleted. In this case we
852- * will return less devices than requested
853- * but that's OK. We will have more memory
854- * than is needed to save them all, but
855- * just a little more, and it gets freed
856- * just below.
849+ * A new swap device got added. That's OK, just ignore the
850+ * excess device(s), and return the first N (the number that
851+ * were there when we started).
857852 *
858- * uvmexp.nswapdev hasn't changed. This
859- * will be the usual case (no swapctl operations
860- * occurred while the lock was releases - or
861- * possibly a device was deleted and another
862- * added - that's irrelevant, at this point
863- * all that matters is the number of devices,
864- * we haven't looked at the lists yet.
853+ * 2. uvmexp.nswapdev has decreased.
865854 *
866- * So we never need to repeat this allocation,
867- * Once is quite enough.
855+ * A swap device was deleted. In this case we will return
856+ * less devices than requested but that's OK. We will have
857+ * more slot memory than is needed to save them all, but just
858+ * a little more, and it gets freed just below.
868859 *
869- * And we never need to look at uvmexp.nswapdev
870- * again!
860+ * 3. uvmexp.nswapdev hasn't changed.
861+ *
862+ * This will be the usual case; no swapctl operations occurred
863+ * while the lock was released, or possibly a device was
864+ * deleted and another added - that's irrelevant. At this
865+ * point all that matters is the number of devices, we haven't
866+ * looked at the lists yet.
867+ *
868+ * So we never need to adjust this allocation.
869+ *
870+ * And we don't need to look at uvmexp.nswapdev again!
871871 */
872872 }
873873
874874 KASSERT (rw_lock_held (& swap_syscall_lock ));
875875
876876 /*
877- * Collect all of the swap descriptors, while
878- * holding the data lock, so the lists cannot
879- * change. Then they can be used safely.
877+ * Collect all of the swap descriptors, while holding the data lock,
878+ * so the lists cannot change. Then they can be used safely.
880879 *
881- * They cannot be deleted, because swap_syscall_lock
882- * is held, but the lists holding them can be adjusted
883- * except in this small loop where we lock out that
884- * kind of activity. No processing happens here,
885- * this is fast, with no func calls, or anything which
880+ * Entries cannot be deleted, because swap_syscall_lock is held,
881+ * but the lists holding them can be reordered except in this small
882+ * loop where we lock out that kind of activity. No processing
883+ * happens here, this is fast, with no func calls, or anything which
886884 * might perform operations which might need the lock.
887885 */
888886 mutex_enter (& uvm_swap_data_lock );
@@ -893,11 +891,11 @@ uvm_swap_stats(char *ptr, int misc,
893891 if (++ count <= slots )
894892 * sp ++ = sdp ;
895893 /*
896- * don't bother with exiting the loops
897- * early, they tend to be very short,
898- * and not exhausting them is a very rare
899- * occurrence. So just loop and do nothing
900- * in the odd case we could break out early.
894+ * don't bother with exiting the loops early,
895+ * the lists tend to be very short, and not
896+ * exhausting them is a very rare occurrence.
897+ * So just loop and do nothing (but count) in
898+ * the odd case we could have broken out early.
901899 */
902900 }
903901 }
@@ -913,13 +911,16 @@ uvm_swap_stats(char *ptr, int misc,
913911 if (misc > count ) /* the number of devices now */
914912 misc = count ;
915913
914+ /*
915+ * This is the actual work of uvm_swap_stats() - above was bookkeeping.
916+ */
916917 error = 0 ;
917918 count = 0 ;
918919 sp = sdps ;
919920 while (misc -- > 0 ) {
920921 int inuse ;
921922
922- sdp = * sp ++ ;
923+ sdp = * sp ++ ; /* The next swapdev, from the next slot */
923924
924925 inuse = btodb ((uint64_t )sdp -> swd_npginuse <<
925926 PAGE_SHIFT );
@@ -937,8 +938,8 @@ uvm_swap_stats(char *ptr, int misc,
937938 out :;
938939 if (sdps != stackbuf ) {
939940 /*
940- * XXX should unlock & lock again here
941- * as well probably, but for now, no...
941+ * XXX should unlock & lock again here probably,
942+ * but for now, no...
942943 */
943944 kmem_free (sdps , sdpsize );
944945 }
@@ -1062,7 +1063,7 @@ swap_on(struct lwp *l, struct swapdev *sdp)
10621063 goto bad ;
10631064 }
10641065
1065- UVMHIST_LOG (pdhist , " dev=%#jx: size=%jd addr=%jd" , dev , size , addr , 0 );
1066+ UVMHIST_LOG (pdhist ," dev=%#jx: size=%jd addr=%jd" , dev , size , addr , 0 );
10661067
10671068 /*
10681069 * now we need to allocate an extent to manage this swap device
@@ -1181,7 +1182,8 @@ swap_off(struct lwp *l, struct swapdev *sdp)
11811182 int error = 0 ;
11821183
11831184 UVMHIST_FUNC (__func__ );
1184- UVMHIST_CALLARGS (pdhist , " dev=%#jx, npages=%jd" , sdp -> swd_dev ,npages , 0 , 0 );
1185+ UVMHIST_CALLARGS (pdhist ,
1186+ " dev=%#jx, npages=%jd" , sdp -> swd_dev ,npages , 0 , 0 );
11851187
11861188 KASSERT (rw_write_held (& swap_syscall_lock ));
11871189 KASSERT (mutex_owned (& uvm_swap_data_lock ));
@@ -1429,7 +1431,8 @@ static int
14291431swread (dev_t dev , struct uio * uio , int ioflag )
14301432{
14311433 UVMHIST_FUNC (__func__ );
1432- UVMHIST_CALLARGS (pdhist , " dev=%#jx offset=%#jx" , dev , uio -> uio_offset , 0 , 0 );
1434+ UVMHIST_CALLARGS (pdhist ,
1435+ " dev=%#jx offset=%#jx" , dev , uio -> uio_offset , 0 , 0 );
14331436
14341437 return (physio (swstrategy , NULL , dev , B_READ , minphys , uio ));
14351438}
@@ -1442,7 +1445,8 @@ static int
14421445swwrite (dev_t dev , struct uio * uio , int ioflag )
14431446{
14441447 UVMHIST_FUNC (__func__ );
1445- UVMHIST_CALLARGS (pdhist , " dev=%#jx offset=%#jx" , dev , uio -> uio_offset , 0 , 0 );
1448+ UVMHIST_CALLARGS (pdhist ,
1449+ " dev=%#jx offset=%#jx" , dev , uio -> uio_offset , 0 , 0 );
14461450
14471451 return (physio (swstrategy , NULL , dev , B_WRITE , minphys , uio ));
14481452}
@@ -1992,7 +1996,8 @@ uvm_swap_io(struct vm_page **pps, int startslot, int npages, int flags)
19921996 int error , mapinflags ;
19931997 bool write , async , swap_encrypt ;
19941998 UVMHIST_FUNC (__func__ );
1995- UVMHIST_CALLARGS (pdhist , "<- called, startslot=%jd, npages=%jd, flags=%#jx" ,
1999+ UVMHIST_CALLARGS (pdhist ,
2000+ "<- called, startslot=%jd, npages=%jd, flags=%#jx" ,
19962001 startslot , npages , flags , 0 );
19972002
19982003 write = (flags & B_READ ) == 0 ;
0 commit comments