Re: Swap encrypt under memory pressure

2022-03-15 Thread Stuart Henderson
On 2022/03/12 11:35, Martin Pieuchot wrote:
> Try to allocate the buffer before doing the encryption, if it fails we
> do not spend time doing the encryption.  This reduce the pressure when
> swapping with low memory.

Done a couple of ports bulk builds with this on i386, no problems seen.



Re: Swap encrypt under memory pressure

2022-03-13 Thread Mark Kettenis
> Date: Sat, 12 Mar 2022 11:35:09 +0100
> From: Martin Pieuchot 
> 
> Try to allocate the buffer before doing the encryption, if it fails we
> do not spend time doing the encryption.  This reduce the pressure when
> swapping with low memory.
> 
> ok?

Makes sense to me; ok kettenis@

> Index: uvm/uvm_swap.c
> ===
> RCS file: /cvs/src/sys/uvm/uvm_swap.c,v
> retrieving revision 1.153
> diff -u -p -r1.153 uvm_swap.c
> --- uvm/uvm_swap.c22 Feb 2022 01:15:02 -  1.153
> +++ uvm/uvm_swap.c12 Mar 2022 10:30:26 -
> @@ -1690,6 +1690,26 @@ uvm_swap_io(struct vm_page **pps, int st
>   }
>   }
>  
> + /*
> +  * now allocate a buf for the i/o.
> +  * [make sure we don't put the pagedaemon to sleep...]
> +  */
> + pflag = (async || curproc == uvm.pagedaemon_proc) ? PR_NOWAIT :
> + PR_WAITOK;
> + bp = pool_get(, pflag | PR_ZERO);
> +
> + /*
> +  * if we failed to get a swapbuf, return "try again"
> +  */
> + if (bp == NULL) {
> + if (bounce) {
> + uvm_pagermapout(bouncekva, npages);
> + uvm_swap_freepages(tpps, npages);
> + }
> + uvm_pagermapout(kva, npages);
> + return (VM_PAGER_AGAIN);
> + }
> +
>   /* encrypt to swap */
>   if (write && bounce) {
>   int i, opages;
> @@ -1729,35 +1749,6 @@ uvm_swap_io(struct vm_page **pps, int st
> PGO_PDFREECLUST);
>  
>   kva = bouncekva;
> - }
> -
> - /*
> -  * now allocate a buf for the i/o.
> -  * [make sure we don't put the pagedaemon to sleep...]
> -  */
> - pflag = (async || curproc == uvm.pagedaemon_proc) ? PR_NOWAIT :
> - PR_WAITOK;
> - bp = pool_get(, pflag | PR_ZERO);
> -
> - /*
> -  * if we failed to get a swapbuf, return "try again"
> -  */
> - if (bp == NULL) {
> - if (write && bounce) {
> -#ifdef UVM_SWAP_ENCRYPT
> - int i;
> -
> - /* swap encrypt needs cleanup */
> - if (encrypt)
> - for (i = 0; i < npages; i++)
> - SWAP_KEY_PUT(sdp, SWD_KEY(sdp,
> - startslot + i));
> -#endif
> -
> - uvm_pagermapout(kva, npages);
> - uvm_swap_freepages(tpps, npages);
> - }
> - return (VM_PAGER_AGAIN);
>   }
>  
>   /*
> 
> 



Swap encrypt under memory pressure

2022-03-12 Thread Martin Pieuchot
Try to allocate the buffer before doing the encryption, if it fails we
do not spend time doing the encryption.  This reduce the pressure when
swapping with low memory.

ok?

Index: uvm/uvm_swap.c
===
RCS file: /cvs/src/sys/uvm/uvm_swap.c,v
retrieving revision 1.153
diff -u -p -r1.153 uvm_swap.c
--- uvm/uvm_swap.c  22 Feb 2022 01:15:02 -  1.153
+++ uvm/uvm_swap.c  12 Mar 2022 10:30:26 -
@@ -1690,6 +1690,26 @@ uvm_swap_io(struct vm_page **pps, int st
}
}
 
+   /*
+* now allocate a buf for the i/o.
+* [make sure we don't put the pagedaemon to sleep...]
+*/
+   pflag = (async || curproc == uvm.pagedaemon_proc) ? PR_NOWAIT :
+   PR_WAITOK;
+   bp = pool_get(, pflag | PR_ZERO);
+
+   /*
+* if we failed to get a swapbuf, return "try again"
+*/
+   if (bp == NULL) {
+   if (bounce) {
+   uvm_pagermapout(bouncekva, npages);
+   uvm_swap_freepages(tpps, npages);
+   }
+   uvm_pagermapout(kva, npages);
+   return (VM_PAGER_AGAIN);
+   }
+
/* encrypt to swap */
if (write && bounce) {
int i, opages;
@@ -1729,35 +1749,6 @@ uvm_swap_io(struct vm_page **pps, int st
  PGO_PDFREECLUST);
 
kva = bouncekva;
-   }
-
-   /*
-* now allocate a buf for the i/o.
-* [make sure we don't put the pagedaemon to sleep...]
-*/
-   pflag = (async || curproc == uvm.pagedaemon_proc) ? PR_NOWAIT :
-   PR_WAITOK;
-   bp = pool_get(, pflag | PR_ZERO);
-
-   /*
-* if we failed to get a swapbuf, return "try again"
-*/
-   if (bp == NULL) {
-   if (write && bounce) {
-#ifdef UVM_SWAP_ENCRYPT
-   int i;
-
-   /* swap encrypt needs cleanup */
-   if (encrypt)
-   for (i = 0; i < npages; i++)
-   SWAP_KEY_PUT(sdp, SWD_KEY(sdp,
-   startslot + i));
-#endif
-
-   uvm_pagermapout(kva, npages);
-   uvm_swap_freepages(tpps, npages);
-   }
-   return (VM_PAGER_AGAIN);
}
 
/*