postpone memory allocation for uvm swap encryption until it is turned on with
authorprovos <provos@openbsd.org>
Sat, 18 Mar 2000 20:51:32 +0000 (20:51 +0000)
committerprovos <provos@openbsd.org>
Sat, 18 Mar 2000 20:51:32 +0000 (20:51 +0000)
sysctl.

sys/uvm/uvm_meter.c
sys/uvm/uvm_swap.c
sys/uvm/uvm_swap.h

index 3c287ca..da160cf 100644 (file)
@@ -49,6 +49,7 @@
 #include <sys/exec.h>
 
 #ifdef UVM_SWAP_ENCRYPT
+#include <uvm/uvm_swap.h>
 #include <uvm/uvm_swap_encrypt.h>
 #endif
 
@@ -153,9 +154,23 @@ uvm_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
                return (sysctl_rdstruct(oldp, oldlenp, newp, &_ps,
                    sizeof(_ps)));
 #ifdef UVM_SWAP_ENCRYPT
-       case VM_SWAPENCRYPT:
-               return (sysctl_int(oldp, oldlenp, newp, newlen,
-                   &uvm_doswapencrypt));
+       case VM_SWAPENCRYPT: {
+               int doencrypt = uvm_doswapencrypt;
+               int result;
+
+               result = sysctl_int(oldp, oldlenp, newp, newlen, &doencrypt);
+               if (result)
+                       return result;
+
+               /* Swap Encryption has been turned on, we need to
+                * initalize state for swap devices that have been
+                * added 
+                */
+               if (doencrypt)
+                       uvm_swap_initcrypt_all();
+               uvm_doswapencrypt = doencrypt;
+               return (0);
+       }
 #endif
        default:
                return (EOPNOTSUPP);
index 21f73ef..c00ccc2 100644 (file)
@@ -282,6 +282,7 @@ boolean_t uvm_swap_allocpages __P((struct vm_page **, int));
 void uvm_swap_freepages __P((struct vm_page **, int));
 void uvm_swap_markdecrypt __P((struct swapdev *, int, int, int));
 boolean_t uvm_swap_needdecrypt __P((struct swapdev *, int));
+void uvm_swap_initcrypt __P((struct swapdev *, int));
 #endif
 
 /*
@@ -358,6 +359,39 @@ uvm_swap_init()
 }
 
 #ifdef UVM_SWAP_ENCRYPT
+void
+uvm_swap_initcrypt_all(void)
+{
+       struct swapdev *sdp;
+       struct swappri *spp;
+
+       simple_lock(&uvm.swap_data_lock);
+
+       for (spp = swap_priority.lh_first; spp != NULL;
+            spp = spp->spi_swappri.le_next) {
+               for (sdp = spp->spi_swapdev.cqh_first;
+                    sdp != (void *)&spp->spi_swapdev;
+                    sdp = sdp->swd_next.cqe_next)
+                       if (sdp->swd_decrypt == NULL)
+                               uvm_swap_initcrypt(sdp, sdp->swd_npages);
+       }
+       simple_unlock(&uvm.swap_data_lock);
+}
+
+void
+uvm_swap_initcrypt(struct swapdev *sdp, int npages)
+{
+       /*
+        * keep information if a page needs to be decrypted when we get it
+        * from the swap device.
+        * We cannot chance a malloc later, if we are doing ASYNC puts,
+        * we may not call malloc with M_WAITOK.  This consumes only
+        * 8KB memory for a 256MB swap partition.
+        */
+       sdp->swd_decrypt = malloc(SWD_DCRYPT_SIZE(npages), M_VMSWAP, M_WAITOK);
+       bzero(sdp->swd_decrypt, SWD_DCRYPT_SIZE(npages));
+}
+
 boolean_t
 uvm_swap_allocpages(struct vm_page **pps, int npages)
 {
@@ -1109,15 +1143,8 @@ swap_on(p, sdp)
        }
 
 #ifdef UVM_SWAP_ENCRYPT
-       /*
-        * keep information if a page needs to be decrypted when we get it
-        * from the swap device.
-        * We cannot chance a malloc later, if we are doing ASYNC puts,
-        * we may not call malloc with M_WAITOK.  This takes consumes only
-        * 8KB memory for a 256MB swap partition.
-        */
-       sdp->swd_decrypt = malloc(SWD_DCRYPT_SIZE(npages), M_VMSWAP, M_WAITOK);
-       bzero(sdp->swd_decrypt, SWD_DCRYPT_SIZE(npages));
+       if (uvm_doswapencrypt)
+               uvm_swap_initcrypt(sdp, npages);
 #endif
        /*
         * now add the new swapdev to the drum and enable.
index 008db98..42a6f4a 100644 (file)
@@ -39,4 +39,7 @@ int                   uvm_swap_put __P((int, struct vm_page **, int,
 int                    uvm_swap_alloc __P((int *wanted, boolean_t lessok));
 void                   uvm_swap_free __P((int startslot, int nslots));
 
+#ifdef UVM_SWAP_ENCRYPT
+void                   uvm_swap_initcrypt_all __P((void));
+#endif
 #endif /* _UVM_UVM_SWAP_H_ */