24
24
#include <linux/rbtree.h>
25
25
#include <linux/swap.h>
26
26
#include <linux/crypto.h>
27
+ #include <linux/scatterlist.h>
27
28
#include <linux/mempool.h>
28
29
#include <linux/zpool.h>
30
+ #include <crypto/acompress.h>
29
31
30
32
#include <linux/mm_types.h>
31
33
#include <linux/page-flags.h>
@@ -127,9 +129,17 @@ module_param_named(same_filled_pages_enabled, zswap_same_filled_pages_enabled,
127
129
* data structures
128
130
**********************************/
129
131
132
+ struct crypto_acomp_ctx {
133
+ struct crypto_acomp * acomp ;
134
+ struct acomp_req * req ;
135
+ struct crypto_wait wait ;
136
+ u8 * dstmem ;
137
+ struct mutex * mutex ;
138
+ };
139
+
130
140
struct zswap_pool {
131
141
struct zpool * zpool ;
132
- struct crypto_comp * __percpu * tfm ;
142
+ struct crypto_acomp_ctx __percpu * acomp_ctx ;
133
143
struct kref kref ;
134
144
struct list_head list ;
135
145
struct work_struct release_work ;
@@ -388,23 +398,43 @@ static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,
388
398
* per-cpu code
389
399
**********************************/
390
400
static DEFINE_PER_CPU (u8 * , zswap_dstmem ) ;
401
+ /*
402
+ * If users dynamically change the zpool type and compressor at runtime, i.e.
403
+ * zswap is running, zswap can have more than one zpool on one cpu, but they
404
+ * are sharing dtsmem. So we need this mutex to be per-cpu.
405
+ */
406
+ static DEFINE_PER_CPU (struct mutex * , zswap_mutex ) ;
391
407
392
408
static int zswap_dstmem_prepare (unsigned int cpu )
393
409
{
410
+ struct mutex * mutex ;
394
411
u8 * dst ;
395
412
396
413
dst = kmalloc_node (PAGE_SIZE * 2 , GFP_KERNEL , cpu_to_node (cpu ));
397
414
if (!dst )
398
415
return - ENOMEM ;
399
416
417
+ mutex = kmalloc_node (sizeof (* mutex ), GFP_KERNEL , cpu_to_node (cpu ));
418
+ if (!mutex ) {
419
+ kfree (dst );
420
+ return - ENOMEM ;
421
+ }
422
+
423
+ mutex_init (mutex );
400
424
per_cpu (zswap_dstmem , cpu ) = dst ;
425
+ per_cpu (zswap_mutex , cpu ) = mutex ;
401
426
return 0 ;
402
427
}
403
428
404
429
static int zswap_dstmem_dead (unsigned int cpu )
405
430
{
431
+ struct mutex * mutex ;
406
432
u8 * dst ;
407
433
434
+ mutex = per_cpu (zswap_mutex , cpu );
435
+ kfree (mutex );
436
+ per_cpu (zswap_mutex , cpu ) = NULL ;
437
+
408
438
dst = per_cpu (zswap_dstmem , cpu );
409
439
kfree (dst );
410
440
per_cpu (zswap_dstmem , cpu ) = NULL ;
@@ -415,30 +445,54 @@ static int zswap_dstmem_dead(unsigned int cpu)
415
445
static int zswap_cpu_comp_prepare (unsigned int cpu , struct hlist_node * node )
416
446
{
417
447
struct zswap_pool * pool = hlist_entry (node , struct zswap_pool , node );
418
- struct crypto_comp * tfm ;
419
-
420
- if (WARN_ON (* per_cpu_ptr (pool -> tfm , cpu )))
421
- return 0 ;
448
+ struct crypto_acomp_ctx * acomp_ctx = per_cpu_ptr (pool -> acomp_ctx , cpu );
449
+ struct crypto_acomp * acomp ;
450
+ struct acomp_req * req ;
451
+
452
+ acomp = crypto_alloc_acomp_node (pool -> tfm_name , 0 , 0 , cpu_to_node (cpu ));
453
+ if (IS_ERR (acomp )) {
454
+ pr_err ("could not alloc crypto acomp %s : %ld\n" ,
455
+ pool -> tfm_name , PTR_ERR (acomp ));
456
+ return PTR_ERR (acomp );
457
+ }
458
+ acomp_ctx -> acomp = acomp ;
422
459
423
- tfm = crypto_alloc_comp (pool -> tfm_name , 0 , 0 );
424
- if (IS_ERR (tfm )) {
425
- pr_err ("could not alloc crypto comp %s : %ld\n" ,
426
- pool -> tfm_name , PTR_ERR (tfm ));
460
+ req = acomp_request_alloc (acomp_ctx -> acomp );
461
+ if (!req ) {
462
+ pr_err ("could not alloc crypto acomp_request %s\n" ,
463
+ pool -> tfm_name );
464
+ crypto_free_acomp (acomp_ctx -> acomp );
427
465
return - ENOMEM ;
428
466
}
429
- * per_cpu_ptr (pool -> tfm , cpu ) = tfm ;
467
+ acomp_ctx -> req = req ;
468
+
469
+ crypto_init_wait (& acomp_ctx -> wait );
470
+ /*
471
+ * if the backend of acomp is async zip, crypto_req_done() will wakeup
472
+ * crypto_wait_req(); if the backend of acomp is scomp, the callback
473
+ * won't be called, crypto_wait_req() will return without blocking.
474
+ */
475
+ acomp_request_set_callback (req , CRYPTO_TFM_REQ_MAY_BACKLOG ,
476
+ crypto_req_done , & acomp_ctx -> wait );
477
+
478
+ acomp_ctx -> mutex = per_cpu (zswap_mutex , cpu );
479
+ acomp_ctx -> dstmem = per_cpu (zswap_dstmem , cpu );
480
+
430
481
return 0 ;
431
482
}
432
483
433
484
static int zswap_cpu_comp_dead (unsigned int cpu , struct hlist_node * node )
434
485
{
435
486
struct zswap_pool * pool = hlist_entry (node , struct zswap_pool , node );
436
- struct crypto_comp * tfm ;
487
+ struct crypto_acomp_ctx * acomp_ctx = per_cpu_ptr (pool -> acomp_ctx , cpu );
488
+
489
+ if (!IS_ERR_OR_NULL (acomp_ctx )) {
490
+ if (!IS_ERR_OR_NULL (acomp_ctx -> req ))
491
+ acomp_request_free (acomp_ctx -> req );
492
+ if (!IS_ERR_OR_NULL (acomp_ctx -> acomp ))
493
+ crypto_free_acomp (acomp_ctx -> acomp );
494
+ }
437
495
438
- tfm = * per_cpu_ptr (pool -> tfm , cpu );
439
- if (!IS_ERR_OR_NULL (tfm ))
440
- crypto_free_comp (tfm );
441
- * per_cpu_ptr (pool -> tfm , cpu ) = NULL ;
442
496
return 0 ;
443
497
}
444
498
@@ -561,8 +615,9 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
561
615
pr_debug ("using %s zpool\n" , zpool_get_type (pool -> zpool ));
562
616
563
617
strlcpy (pool -> tfm_name , compressor , sizeof (pool -> tfm_name ));
564
- pool -> tfm = alloc_percpu (struct crypto_comp * );
565
- if (!pool -> tfm ) {
618
+
619
+ pool -> acomp_ctx = alloc_percpu (* pool -> acomp_ctx );
620
+ if (!pool -> acomp_ctx ) {
566
621
pr_err ("percpu alloc failed\n" );
567
622
goto error ;
568
623
}
@@ -585,7 +640,8 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
585
640
return pool ;
586
641
587
642
error :
588
- free_percpu (pool -> tfm );
643
+ if (pool -> acomp_ctx )
644
+ free_percpu (pool -> acomp_ctx );
589
645
if (pool -> zpool )
590
646
zpool_destroy_pool (pool -> zpool );
591
647
kfree (pool );
@@ -596,14 +652,14 @@ static __init struct zswap_pool *__zswap_pool_create_fallback(void)
596
652
{
597
653
bool has_comp , has_zpool ;
598
654
599
- has_comp = crypto_has_comp (zswap_compressor , 0 , 0 );
655
+ has_comp = crypto_has_acomp (zswap_compressor , 0 , 0 );
600
656
if (!has_comp && strcmp (zswap_compressor ,
601
657
CONFIG_ZSWAP_COMPRESSOR_DEFAULT )) {
602
658
pr_err ("compressor %s not available, using default %s\n" ,
603
659
zswap_compressor , CONFIG_ZSWAP_COMPRESSOR_DEFAULT );
604
660
param_free_charp (& zswap_compressor );
605
661
zswap_compressor = CONFIG_ZSWAP_COMPRESSOR_DEFAULT ;
606
- has_comp = crypto_has_comp (zswap_compressor , 0 , 0 );
662
+ has_comp = crypto_has_acomp (zswap_compressor , 0 , 0 );
607
663
}
608
664
if (!has_comp ) {
609
665
pr_err ("default compressor %s not available\n" ,
@@ -639,7 +695,7 @@ static void zswap_pool_destroy(struct zswap_pool *pool)
639
695
zswap_pool_debug ("destroying" , pool );
640
696
641
697
cpuhp_state_remove_instance (CPUHP_MM_ZSWP_POOL_PREPARE , & pool -> node );
642
- free_percpu (pool -> tfm );
698
+ free_percpu (pool -> acomp_ctx );
643
699
zpool_destroy_pool (pool -> zpool );
644
700
kfree (pool );
645
701
}
@@ -723,7 +779,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp,
723
779
}
724
780
type = s ;
725
781
} else if (!compressor ) {
726
- if (!crypto_has_comp (s , 0 , 0 )) {
782
+ if (!crypto_has_acomp (s , 0 , 0 )) {
727
783
pr_err ("compressor %s not available\n" , s );
728
784
return - ENOENT ;
729
785
}
@@ -774,7 +830,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp,
774
830
* failed, maybe both compressor and zpool params were bad.
775
831
* Allow changing this param, so pool creation will succeed
776
832
* when the other param is changed. We already verified this
777
- * param is ok in the zpool_has_pool() or crypto_has_comp ()
833
+ * param is ok in the zpool_has_pool() or crypto_has_acomp ()
778
834
* checks above.
779
835
*/
780
836
ret = param_set_charp (s , kp );
@@ -876,8 +932,10 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
876
932
pgoff_t offset ;
877
933
struct zswap_entry * entry ;
878
934
struct page * page ;
879
- struct crypto_comp * tfm ;
880
- u8 * src , * dst ;
935
+ struct scatterlist input , output ;
936
+ struct crypto_acomp_ctx * acomp_ctx ;
937
+
938
+ u8 * src ;
881
939
unsigned int dlen ;
882
940
int ret ;
883
941
struct writeback_control wbc = {
@@ -916,14 +974,20 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
916
974
917
975
case ZSWAP_SWAPCACHE_NEW : /* page is locked */
918
976
/* decompress */
977
+ acomp_ctx = raw_cpu_ptr (entry -> pool -> acomp_ctx );
978
+
919
979
dlen = PAGE_SIZE ;
920
980
src = (u8 * )zhdr + sizeof (struct zswap_header );
921
- dst = kmap_atomic (page );
922
- tfm = * get_cpu_ptr (entry -> pool -> tfm );
923
- ret = crypto_comp_decompress (tfm , src , entry -> length ,
924
- dst , & dlen );
925
- put_cpu_ptr (entry -> pool -> tfm );
926
- kunmap_atomic (dst );
981
+
982
+ mutex_lock (acomp_ctx -> mutex );
983
+ sg_init_one (& input , src , entry -> length );
984
+ sg_init_table (& output , 1 );
985
+ sg_set_page (& output , page , PAGE_SIZE , 0 );
986
+ acomp_request_set_params (acomp_ctx -> req , & input , & output , entry -> length , dlen );
987
+ ret = crypto_wait_req (crypto_acomp_decompress (acomp_ctx -> req ), & acomp_ctx -> wait );
988
+ dlen = acomp_ctx -> req -> dlen ;
989
+ mutex_unlock (acomp_ctx -> mutex );
990
+
927
991
BUG_ON (ret );
928
992
BUG_ON (dlen != PAGE_SIZE );
929
993
@@ -1004,7 +1068,8 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset,
1004
1068
{
1005
1069
struct zswap_tree * tree = zswap_trees [type ];
1006
1070
struct zswap_entry * entry , * dupentry ;
1007
- struct crypto_comp * tfm ;
1071
+ struct scatterlist input , output ;
1072
+ struct crypto_acomp_ctx * acomp_ctx ;
1008
1073
int ret ;
1009
1074
unsigned int hlen , dlen = PAGE_SIZE ;
1010
1075
unsigned long handle , value ;
@@ -1074,12 +1139,32 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset,
1074
1139
}
1075
1140
1076
1141
/* compress */
1077
- dst = get_cpu_var (zswap_dstmem );
1078
- tfm = * get_cpu_ptr (entry -> pool -> tfm );
1079
- src = kmap_atomic (page );
1080
- ret = crypto_comp_compress (tfm , src , PAGE_SIZE , dst , & dlen );
1081
- kunmap_atomic (src );
1082
- put_cpu_ptr (entry -> pool -> tfm );
1142
+ acomp_ctx = raw_cpu_ptr (entry -> pool -> acomp_ctx );
1143
+
1144
+ mutex_lock (acomp_ctx -> mutex );
1145
+
1146
+ dst = acomp_ctx -> dstmem ;
1147
+ sg_init_table (& input , 1 );
1148
+ sg_set_page (& input , page , PAGE_SIZE , 0 );
1149
+
1150
+ /* zswap_dstmem is of size (PAGE_SIZE * 2). Reflect same in sg_list */
1151
+ sg_init_one (& output , dst , PAGE_SIZE * 2 );
1152
+ acomp_request_set_params (acomp_ctx -> req , & input , & output , PAGE_SIZE , dlen );
1153
+ /*
1154
+ * it maybe looks a little bit silly that we send an asynchronous request,
1155
+ * then wait for its completion synchronously. This makes the process look
1156
+ * synchronous in fact.
1157
+ * Theoretically, acomp supports users send multiple acomp requests in one
1158
+ * acomp instance, then get those requests done simultaneously. but in this
1159
+ * case, frontswap actually does store and load page by page, there is no
1160
+ * existing method to send the second page before the first page is done
1161
+ * in one thread doing frontswap.
1162
+ * but in different threads running on different cpu, we have different
1163
+ * acomp instance, so multiple threads can do (de)compression in parallel.
1164
+ */
1165
+ ret = crypto_wait_req (crypto_acomp_compress (acomp_ctx -> req ), & acomp_ctx -> wait );
1166
+ dlen = acomp_ctx -> req -> dlen ;
1167
+
1083
1168
if (ret ) {
1084
1169
ret = - EINVAL ;
1085
1170
goto put_dstmem ;
@@ -1103,7 +1188,7 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset,
1103
1188
memcpy (buf , & zhdr , hlen );
1104
1189
memcpy (buf + hlen , dst , dlen );
1105
1190
zpool_unmap_handle (entry -> pool -> zpool , handle );
1106
- put_cpu_var ( zswap_dstmem );
1191
+ mutex_unlock ( acomp_ctx -> mutex );
1107
1192
1108
1193
/* populate entry */
1109
1194
entry -> offset = offset ;
@@ -1131,7 +1216,7 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset,
1131
1216
return 0 ;
1132
1217
1133
1218
put_dstmem :
1134
- put_cpu_var ( zswap_dstmem );
1219
+ mutex_unlock ( acomp_ctx -> mutex );
1135
1220
zswap_pool_put (entry -> pool );
1136
1221
freepage :
1137
1222
zswap_entry_cache_free (entry );
@@ -1148,7 +1233,8 @@ static int zswap_frontswap_load(unsigned type, pgoff_t offset,
1148
1233
{
1149
1234
struct zswap_tree * tree = zswap_trees [type ];
1150
1235
struct zswap_entry * entry ;
1151
- struct crypto_comp * tfm ;
1236
+ struct scatterlist input , output ;
1237
+ struct crypto_acomp_ctx * acomp_ctx ;
1152
1238
u8 * src , * dst ;
1153
1239
unsigned int dlen ;
1154
1240
int ret ;
@@ -1175,11 +1261,16 @@ static int zswap_frontswap_load(unsigned type, pgoff_t offset,
1175
1261
src = zpool_map_handle (entry -> pool -> zpool , entry -> handle , ZPOOL_MM_RO );
1176
1262
if (zpool_evictable (entry -> pool -> zpool ))
1177
1263
src += sizeof (struct zswap_header );
1178
- dst = kmap_atomic (page );
1179
- tfm = * get_cpu_ptr (entry -> pool -> tfm );
1180
- ret = crypto_comp_decompress (tfm , src , entry -> length , dst , & dlen );
1181
- put_cpu_ptr (entry -> pool -> tfm );
1182
- kunmap_atomic (dst );
1264
+
1265
+ acomp_ctx = raw_cpu_ptr (entry -> pool -> acomp_ctx );
1266
+ mutex_lock (acomp_ctx -> mutex );
1267
+ sg_init_one (& input , src , entry -> length );
1268
+ sg_init_table (& output , 1 );
1269
+ sg_set_page (& output , page , PAGE_SIZE , 0 );
1270
+ acomp_request_set_params (acomp_ctx -> req , & input , & output , entry -> length , dlen );
1271
+ ret = crypto_wait_req (crypto_acomp_decompress (acomp_ctx -> req ), & acomp_ctx -> wait );
1272
+ mutex_unlock (acomp_ctx -> mutex );
1273
+
1183
1274
zpool_unmap_handle (entry -> pool -> zpool , entry -> handle );
1184
1275
BUG_ON (ret );
1185
1276
0 commit comments