forked from intel/QAT_Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qae_mem_utils.c
1135 lines (1042 loc) · 36.4 KB
/
qae_mem_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ====================================================================
*
*
* BSD LICENSE
*
* Copyright(c) 2016-2019 Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* ====================================================================
*/
/*****************************************************************************
* @file qae_mem_utils.c
*
* This file provides an interface to the QAT Linux kernel memory
* allocation driver and manages the slabs in user space.
*
*****************************************************************************/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "qat_sys_call.h"
#include "qae_mem_utils.h"
#ifdef USE_QAT_CONTIG_MEM
# include "qat_contig_mem.h"
#endif
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <string.h>
#include <limits.h>
#include <pthread.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
/*
* Error from file descriptor operation
*/
#define FD_ERROR -1
#define unlikely(x) __builtin_expect (!!(x), 0)
/* flag for mutex lock */
static int crypto_inited = 0;
#define PAGE_SHIFT 12
#define PAGE_SIZE (1UL << PAGE_SHIFT)
#define PAGE_MASK (~(PAGE_SIZE-1))
#define MAX_PAGES_SHIFT 5
#define MAX_PAGES (1UL << MAX_PAGES_SHIFT)
#ifdef USE_QAT_CONTIG_MEM
/* qat_contig_mem ioctl open file descriptor */
static int crypto_qat_contig_memfd = FD_ERROR;
#endif
/* Big Slab Allocator Lock */
static pthread_mutex_t crypto_bsal = PTHREAD_MUTEX_INITIALIZER;
/*
* We allocate memory in slabs consisting of a number of slots to avoid
* fragmentation and also to reduce cost of allocation There are nine
* predefined slot sizes: 128 bytes, 256 bytes, 512 bytes, 1024 bytes,
* 2048 bytes, 4096 bytes, 8192 bytes, 16384 bytes and 32768 bytes.
* Slabs are 128KB in size. Each slot has an overhead of a qae_slot
* structure plus QAE_BYTE_ALIGNMENT bytes. The slab also has an
* overhead of a qae_slab structure plus QAE_BYTE_ALIGNMENT bytes
* so the full 128KB is not available for allocation or splitting into
* slots. For allocations bigger than 32KB but less than MAX_ALLOC
* we do not split the slab into slots but just allocate the whole slab.
*/
#define SLAB_SIZE 0x20000
/* Slot sizes */
#define NUM_SLOT_SIZE 10
#define SLOT_128_BYTES 0x0080
#define SLOT_256_BYTES 0x0100
#define SLOT_512_BYTES 0x0200
#define SLOT_1_KILOBYTES 0x0400
#define SLOT_2_KILOBYTES 0x0800
#define SLOT_4_KILOBYTES 0x1000
#define SLOT_8_KILOBYTES 0x2000
#define SLOT_16_KILOBYTES 0x4000
#define SLOT_32_KILOBYTES 0x8000
#define SLOT_DEFAULT_INIT -1
/* slot free signature */
#define SIG_FREE 0xF1F2F3F4
/* slot allocate signature */
#define SIG_ALLOC 0xA1A2A3A4
/* maxmium slot size */
#define MAX_ALLOC (SLAB_SIZE - sizeof(qae_slab) - QAE_BYTE_ALIGNMENT)
#define MAX_EMPTY_SLAB 128
#define IN_EMPTY_LIST 0
#define IN_AVAILABLE_LIST 1
#define IN_FULL_LIST 2
typedef struct _qae_slot {
struct _qae_slot *next;
int sig;
int pool_index;
/* pointer to the slab which contains this slot */
struct _qae_slab *slab;
char *file;
int line;
} qae_slot;
typedef struct _qae_slab {
qat_contig_mem_config memCfg;
/* this field has two meanings:
* in normal slab node, it means the size of the slot in current slab
* as a head slab node, it means the number of slabs in current list */
int slot_size;
int sig;
struct _qae_slab *next;
struct _qae_slab *prev;
struct _qae_slot *next_slot;
/* used slots in slab */
int used_slots;
/* total slots in slab */
int total_slots;
/* indicate which slab list is current slab in */
int list_index;
/* indicate which process alloc this slab */
pid_t pid;
} qae_slab;
static int slot_sizes_available[] = {
SLOT_128_BYTES + QAE_BYTE_ALIGNMENT + sizeof(qae_slot),
SLOT_256_BYTES + QAE_BYTE_ALIGNMENT + sizeof(qae_slot),
SLOT_512_BYTES + QAE_BYTE_ALIGNMENT + sizeof(qae_slot),
SLOT_1_KILOBYTES + QAE_BYTE_ALIGNMENT + sizeof(qae_slot),
SLOT_2_KILOBYTES + QAE_BYTE_ALIGNMENT + sizeof(qae_slot),
SLOT_4_KILOBYTES + QAE_BYTE_ALIGNMENT + sizeof(qae_slot),
SLOT_8_KILOBYTES + QAE_BYTE_ALIGNMENT + sizeof(qae_slot),
SLOT_16_KILOBYTES + QAE_BYTE_ALIGNMENT + sizeof(qae_slot),
SLOT_32_KILOBYTES + QAE_BYTE_ALIGNMENT + sizeof(qae_slot)
};
/* head of a cyclic doubly linked list, reused qae_slab data structure */
typedef qae_slab qae_slab_pool;
/* slab list containing full used slabs */
static qae_slab_pool full_slab_list;
/* array of slab lists containing empty slabs by slot size */
static qae_slab_pool empty_slab_list[NUM_SLOT_SIZE];
/* array of slab lists containing partially used slabs by slot size */
static qae_slab_pool available_slab_list[NUM_SLOT_SIZE];
/* init the head node of a linked list */
static void init_pool(qae_slab_pool *list)
{
memset(list,0,sizeof(qae_slab_pool));
list->next = (qae_slab *)list;
list->prev = (qae_slab *)list;
list->slot_size = 0;
list->pid = getpid();
}
/* fetch the head node from a list */
static qae_slab * get_node_from_head(qae_slab_pool *list)
{
qae_slab *ret = NULL;
if(list->slot_size <= 0)
return ret;
ret = list->next;
ret->next->prev = (qae_slab *)list;
list->next = ret->next;
ret->next = ret->prev = NULL;
list->slot_size--;
return ret;
}
/* remove the node from a list */
static unsigned int remove_node_from_list(qae_slab_pool *list, qae_slab *node)
{
if(!(node && list->slot_size > 0)) {
return 0;
}
node->prev->next = node->next;
node->next->prev = node->prev;
list->slot_size--;
node->next = node->prev = NULL;
return 1;
}
/* insert a node to the end of a list */
static void insert_node_at_end(qae_slab_pool *list, qae_slab *node)
{
qae_slab *tail = list->prev;
tail->next = node;
node->prev = tail;
node->next = (qae_slab *)list;
list->prev = node;
list->slot_size++;
}
/* insert a node at the head of a list */
static void insert_node_at_head(qae_slab_pool *list, qae_slab *node)
{
qae_slab *head = list->next;
head->prev = node;
node->next = head;
node->prev = (qae_slab *)list;
list->next = node;
list->slot_size++;
}
static void crypto_init(void);
/******************************************************************************
* function:
* copyAllocPinnedMemory(void *ptr, size_t size, const char *file,
* int line)
*
* @param ptr [IN] - Pointer to data to be copied
* @param size [IN] - Size of data to be copied
* @param[in] file, the C source filename of the call site
* @param[in] line, the line number within the C source file of the call site
*
* description:
* Internal API to allocate a pinned memory
* buffer and copy data to it.
*
* @retval NULL failed to allocate memory
* @retval non-NULL pointer to allocated memory
******************************************************************************/
void *copyAllocPinnedMemory(void *ptr, size_t size, const char *file,
int line)
{
void *nptr;
if (unlikely((ptr == NULL) ||
(size == 0) ||
(file == NULL) ||
((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL))) {
MEM_WARN("pinned memory allocation failure\n");
return NULL;
}
memcpy(nptr, ptr, size);
return nptr;
}
/******************************************************************************
* function:
* copyAllocPinnedMemoryClean(void *ptr, size_t size,
* size_t original_size,
* const char *file, int line)
*
* @param ptr [IN] - Pointer to data to be copied
* @param size [IN] - Size of data to be copied
* @param original_size [IN] - Original size
* @param[in] file, the C source filename of the call site
* @param[in] line, the line number within the C source file of the call site
*
* description:
* Internal API to allocate a pinned memory
* buffer and copy data to it.
*
* @retval NULL failed to allocate memory
* @retval non-NULL pointer to allocated memory
******************************************************************************/
void *copyAllocPinnedMemoryClean(void *ptr, size_t size, size_t original_size,
const char *file, int line)
{
void *nptr;
if (unlikely(ptr == NULL ||
size == 0 ||
original_size == 0 ||
file == NULL)) {
MEM_WARN("Invalid input params.\n");
return NULL;
}
if (original_size > size) {
MEM_WARN("original_size : %zd > size : %zd", original_size, size);
return NULL;
}
if ((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL) {
MEM_WARN("pinned memory allocation failure\n");
return NULL;
}
memcpy(nptr, ptr, original_size);
return nptr;
}
/******************************************************************************
* function:
* copyFreePinnedMemory(void *uptr, void *kptr, int size)
*
* @param uptr [IN] - Pointer to user data
* @param kptr [IN] - Pointer to pinned memory to be copied
* @param size [IN] - Size of data to be copied
*
* description:
* Internal API to allocate a pinned memory
* buffer and copy data to it.
*
******************************************************************************/
int copyFreePinnedMemory(void *uptr, void *kptr, int size)
{
if (unlikely(uptr == NULL || kptr == NULL || size <= 0)) {
MEM_WARN("Input pointers uptr or kptr are NULL, or size invalid.\n");
return 0;
}
if (size > MAX_ALLOC) {
MEM_WARN("Size greater than MAX_ALLOC\n");
return 0;
}
memcpy(uptr, kptr, size);
qaeCryptoMemFree(kptr);
return 1;
}
/*****************************************************************************
* function:
* crypto_create_slab(int size, int pool_index)
*
* @param[in] size, the size of the slots within the slab. Note that this is
* not the size of the slab itself
* @param[in] pool_index, the index of the slot pool
* @retval qae_slab*, a pointer to the new slab.
*
* @description
* create a new slab and add it to the global linked list
* retval pointer to the new slab
*
*****************************************************************************/
static qae_slab *crypto_create_slab(int size, int pool_index)
{
int i = 0;
int nslot = 0;
qat_contig_mem_config qmcfg = { 0, (uintptr_t) NULL, 0, (uintptr_t) NULL };
qae_slab *result = NULL;
qae_slab *slb = NULL;
qae_slot *slt = NULL;
QAE_UINT alignment;
qmcfg.length = SLAB_SIZE;
#ifdef USE_QAT_CONTIG_MEM
if (qat_ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_MALLOC, &qmcfg) == -1) {
static char errmsg[LINE_MAX];
snprintf(errmsg, LINE_MAX, "ioctl QAT_CONTIG_MEM_MALLOC(%d)",
qmcfg.length);
perror(errmsg);
goto exit;
}
if ((slb =
qat_mmap(NULL, qmcfg.length*QAT_CONTIG_MEM_MMAP_ADJUSTMENT,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED, crypto_qat_contig_memfd,
qmcfg.virtualAddress)) == MAP_FAILED) {
static char errmsg[LINE_MAX];
snprintf(errmsg, LINE_MAX, "mmap: %d %s", errno, strerror(errno));
perror(errmsg);
goto exit;
}
#endif
MEM_DEBUG("Splitting slab into slot size %d\n", size);
slb->slot_size = size;
slb->next_slot = NULL;
slb->sig = SIG_ALLOC;
slb->used_slots = 0;
slb->pid = getpid();
for (i = sizeof(qae_slab); SLAB_SIZE - i >= size; i += size) {
slt = (qae_slot *) ((unsigned char *)slb + i);
alignment =
QAE_BYTE_ALIGNMENT -
(((QAE_UINT) slt + sizeof(qae_slot)) % QAE_BYTE_ALIGNMENT);
slt = (qae_slot *) (((QAE_UINT) slt) + alignment);
slt->next = slb->next_slot;
slt->pool_index = pool_index;
slt->sig = SIG_FREE;
slt->file = NULL;
slt->line = 0;
slb->next_slot = slt;
nslot++;
slt->slab = slb;
}
slb->total_slots = nslot;
/*
* Make sure the update of the slab list is the last thing to be done.
* This means it is not necessary to lock against anyone iterating the
* list from the head
*/
result = slb;
MEM_DEBUG("slab %p slotsize is %d last slot is %p, count is %d\n", slb, size, slt,
nslot);
exit:
return result;
}
/*****************************************************************************
* function:
* crypto_get_empty_slab(int size, int pool_index)
*
* @param[in] size, the size of the slots within the slab. Note that this is
* not the size of the slab itself
* @param[in] pool_index, index of slot pools
* @retval qae_slab*, a pointer to the new slab.
*
* @description
* request a slab from the empty slab list, if empty slab list has no slab
* available, then create a new slab
* retval pointer to the new slab
*
******************************************************************************/
static qae_slab *crypto_get_empty_slab(int size, int pool_index)
{
qae_slab *result = NULL;
result = get_node_from_head(&empty_slab_list[pool_index]);
if(result == NULL) {
result = crypto_create_slab(size,pool_index);
}
return result;
}
/*****************************************************************************
* function:
* crypto_alloc_from_slab(int size, const char *file, int line)
*
* @param[in] size, the size of the memory block required
* @param[in] file, the C source filename of the call site
* @param[in] line, the line number within the C source file of the call site
*
* @description
* allocate a slot of memory from some slab
* retval pointer to the allocated block
*
*****************************************************************************/
static void *crypto_alloc_from_slab(int size, const char *file, int line)
{
qae_slab *slb = NULL;
qae_slot *slt;
int slot_size;
void *result = NULL;
int rc;
int i;
int internal_size = size + QAE_BYTE_ALIGNMENT + sizeof(qae_slot);
if (!crypto_inited)
crypto_init();
slot_size = SLOT_DEFAULT_INIT;
for (i = 0; i < sizeof(slot_sizes_available) / sizeof(int); i++) {
if (internal_size <= slot_sizes_available[i]) {
slot_size = slot_sizes_available[i];
break;
}
}
if (SLOT_DEFAULT_INIT == slot_size) {
if (size <= MAX_ALLOC) {
slot_size = MAX_ALLOC;
} else {
MEM_WARN("Allocation of %d bytes is too big, MAX_ALLOC %lu\n",
size, (long unsigned int)MAX_ALLOC);
goto exit;
}
}
if (available_slab_list[i].pid != getpid())
crypto_init();
MEM_DEBUG("pthread_mutex_lock\n");
if ((rc = pthread_mutex_lock(&crypto_bsal)) != 0) {
MEM_WARN("pthread_mutex_lock: %s\n", strerror(rc));
return result;
}
if(available_slab_list[i].slot_size > 0) {
slt = available_slab_list[i].next->next_slot;
} else {
/* no free slots need to allocate new slab */
slb = crypto_get_empty_slab(slot_size, i);
if (NULL == slb) {
MEM_WARN("error, create_slab failed - memory allocation error\n");
if ((rc = pthread_mutex_unlock(&crypto_bsal)) != 0) {
MEM_WARN("pthread_mutex_unlock: %s\n", strerror(rc));
}
MEM_DEBUG("pthread_mutex_unlock\n");
goto exit;
}
/*allocate a new slab, add it into the available slab list*/
slt = slb->next_slot;
slb->list_index = IN_AVAILABLE_LIST;
insert_node_at_head(&available_slab_list[i],slb);
}
slb = slt->slab;
if (slt->sig != SIG_FREE) {
MEM_WARN("error alloc slot that isn't free %p\n", slt);
goto exit;
}
slt->sig = SIG_ALLOC;
slt->file = strdup(file);
slt->line = line;
/* increase the reference couter */
slb->used_slots++;
/* get the available slot from the head of available slab list */
slb->next_slot = slt->next;
slt->next = NULL;
/* if current slab has no slot available, remove the slab from
* available slab list and add it to the full slab list */
if(slb->used_slots >= slb->total_slots) {
remove_node_from_list(&available_slab_list[i],slb);
insert_node_at_end(&full_slab_list,slb);
slb->list_index = IN_FULL_LIST;
}
result = (void *)((unsigned char *)slt + sizeof(qae_slot));
if ((rc = pthread_mutex_unlock(&crypto_bsal)) != 0) {
MEM_WARN("pthread_mutex_unlock: %s\n", strerror(rc));
}
MEM_DEBUG("pthread_mutex_unlock\n");
exit:
return result;
}
/*****************************************************************************
* function:
* crypto_free_slab(qae_slab *slb)
*
* @param[in] slb, pointer to the slab to be freed
*
* @description
* free a slab to kernel
*
******************************************************************************/
static void crypto_free_slab(qae_slab *slb)
{
qat_contig_mem_config qmcfg;
#ifdef USE_QAT_CONTIG_MEM
MEM_DEBUG("do munmap of %p\n", slb);
qmcfg = *((qat_contig_mem_config *) slb);
if (qat_munmap(slb, SLAB_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
MEM_DEBUG("ioctl free of %p\n", slb);
if (qat_ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_FREE, &qmcfg) == -1) {
perror("ioctl QAT_CONTIG_MEM_FREE");
exit(EXIT_FAILURE);
}
#endif
}
/*****************************************************************************
* function:
* crypto_free_to_slab(void *ptr)
*
* @param[in] ptr, pointer to the memory to be freed
*
* @description
* free a slot of memory back to its slab
*
*****************************************************************************/
static void crypto_free_to_slab(void *ptr)
{
qae_slot *slt = (qae_slot *)((unsigned char *)ptr - sizeof(qae_slot));
if (!slt) {
MEM_WARN("Error freeing memory - unknown address\n");
return;
}
qae_slab *slb = slt->slab;
int i = slt->pool_index;
int rc;
MEM_DEBUG("pthread_mutex_lock\n");
if ((rc = pthread_mutex_lock(&crypto_bsal)) != 0) {
MEM_WARN("pthread_mutex_lock: %s\n", strerror(rc));
return;
}
if (slt->sig != SIG_ALLOC) {
MEM_WARN("error trying to free slot that hasn't been alloc'd %p\n", slt);
goto exit;
}
free(slt->file);
slt->sig = SIG_FREE;
slt->file = NULL;
slt->line = 0;
/* insert the slot into the slab */
slt->next = slb->next_slot;
slb->next_slot = slt;
/* decrease the reference count */
slb->used_slots--;
/* if the used_slots is 0, this slab is empty, it should be
* processed properly */
if(slb->used_slots == 0) {
/* remove this slab from the slab list */
switch(slb->list_index) {
case IN_AVAILABLE_LIST:
remove_node_from_list(&available_slab_list[i],slb);
break;
case IN_FULL_LIST:
remove_node_from_list(&full_slab_list,slb);
break;
default:
break;
}
/* free slab or assign it to the head of the empty slab list */
if(empty_slab_list[i].slot_size >= MAX_EMPTY_SLAB) {
crypto_free_slab(slb);
slb = NULL;
} else {
insert_node_at_head(&empty_slab_list[i],slb);
slb->list_index = IN_EMPTY_LIST;
}
} else {
/* if current slab is in full slab list,
* remove it from the full_slab_list list and then
* append it at the end of the available list */
switch(slb->list_index) {
case IN_FULL_LIST:
remove_node_from_list(&full_slab_list,slb);
insert_node_at_end(&available_slab_list[i],slb);
slt->slab->list_index = IN_AVAILABLE_LIST;
break;
default:
break;
}
}
exit:
if ((rc = pthread_mutex_unlock(&crypto_bsal)) != 0) {
MEM_WARN("pthread_mutex_unlock: %s\n", strerror(rc));
}
MEM_DEBUG("pthread_mutex_unlock\n");
}
/*****************************************************************************
* function:
* crypto_slot_get_size(void *ptr)
*
* @param[in] ptr, pointer to the slot memory
* @retval int, the size of the slot in bytes
*
* @description
* get the slot memory size in bytes
*
*****************************************************************************/
static int crypto_slot_get_size(void *ptr)
{
if (NULL == ptr) {
MEM_WARN("error can't find %p\n", ptr);
return 0;
}
qae_slot *slt = (qae_slot *)((unsigned char *)ptr - sizeof(qae_slot));
if (slt->pool_index == (NUM_SLOT_SIZE - 1)) {
return MAX_ALLOC;
} else if (slt->pool_index >= 0 && slt->pool_index <= NUM_SLOT_SIZE - 2) {
return slot_sizes_available[slt->pool_index] - sizeof(qae_slot) -
QAE_BYTE_ALIGNMENT;
} else {
MEM_WARN("error invalid pool_index %d\n", slt->pool_index);
return 0;
}
}
/*****************************************************************************
* function:
* fork_slab_list(qae_slab* list)
* @param[in] list, pointer to a slab list
*
* @description
* allocate and remap memory following a fork
*
*****************************************************************************/
void fork_slab_list(qae_slab_pool * list)
{
int rc = 0;
int count = 0;
if ((rc = pthread_mutex_lock(&crypto_bsal)) != 0) {
MEM_WARN("pthread_mutex_lock: %s\n", strerror(rc));
return;
}
MEM_DEBUG("fork_slab_list.\n");
MEM_DEBUG("pthread_mutex_lock\n");
qae_slab *old_slb = list->next;
qae_slab *new_slb = NULL;
qat_contig_mem_config qmcfg =
{ 0, (uintptr_t) NULL, SLAB_SIZE, (uintptr_t) NULL };
while (count < list->slot_size) {
#ifdef USE_QAT_CONTIG_MEM
if (qat_ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_MALLOC, &qmcfg)
== -1) {
static char errmsg[LINE_MAX];
snprintf(errmsg, LINE_MAX, "ioctl QAT_CONTIG_MEM_MALLOC(%d)",
qmcfg.length);
perror(errmsg);
exit(EXIT_FAILURE);
}
if ((new_slb =
qat_mmap(NULL, qmcfg.length*QAT_CONTIG_MEM_MMAP_ADJUSTMENT,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED, crypto_qat_contig_memfd,
qmcfg.virtualAddress)) == MAP_FAILED) {
static char errmsg[LINE_MAX];
snprintf(errmsg, LINE_MAX, "mmap: %d %s", errno, strerror(errno));
perror(errmsg);
exit(EXIT_FAILURE);
}
memcpy((void *)new_slb + sizeof(qat_contig_mem_config),
(void *)old_slb + sizeof(qat_contig_mem_config),
SLAB_SIZE - sizeof(qat_contig_mem_config));
#endif
qae_slab *to_unmap = old_slb;
old_slb = old_slb->next;
if (qat_munmap(to_unmap, SLAB_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
qae_slab *remap = qat_mremap(new_slb, SLAB_SIZE, SLAB_SIZE,
MREMAP_FIXED | MREMAP_MAYMOVE, to_unmap);
if ((remap == MAP_FAILED) || (remap != to_unmap)) {
perror("mremap");
exit(EXIT_FAILURE);
}
count++;
}
if ((rc = pthread_mutex_unlock(&crypto_bsal)) != 0) {
MEM_WARN("pthread_mutex_unlock: %s\n", strerror(rc));
}
MEM_DEBUG("pthread_mutex_unlock\n");
}
/*****************************************************************************
* function:
* crypto_free_slab_list(qae_slab_pool *list)
* @param[in] list, pointer to a slab list
*
* @description
* Free all slabs in the supplied slab list.
*
******************************************************************************/
static void crypto_free_slab_list(qae_slab_pool *list)
{
qae_slab *slb, *s_next_slab;
int rc;
#ifdef USE_QAT_CONTIG_MEM
qat_contig_mem_config qmcfg;
#endif
MEM_DEBUG("pthread_mutex_lock\n");
if ((rc = pthread_mutex_lock(&crypto_bsal)) != 0) {
MEM_WARN("pthread_mutex_lock: %s\n", strerror(rc));
return;
}
/* cleanup all the empty slab */
for (slb = list->next; list->slot_size > 0 ; slb = s_next_slab) {
/* need to save this off before unmapping. This is why we can't have
slb = slb->next_slab in the for loop above. */
s_next_slab = slb->next;
#ifdef USE_QAT_CONTIG_MEM
MEM_DEBUG("do munmap of %p\n", slb);
qmcfg = *((qat_contig_mem_config *) slb);
if (qat_munmap(slb, SLAB_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
MEM_DEBUG("ioctl free of %p\n", slb);
if (qat_ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_FREE, &qmcfg)
== -1) {
perror("ioctl QAT_CONTIG_MEM_FREE");
exit(EXIT_FAILURE);
}
#endif
list->slot_size--;
}
MEM_DEBUG("done\n");
if ((rc = pthread_mutex_unlock(&crypto_bsal)) != 0) {
MEM_WARN("pthread_mutex_unlock: %s\n", strerror(rc));
}
MEM_DEBUG("pthread_mutex_unlock\n");
}
/*****************************************************************************
* function:
* crypto_free_empty_slab_list(void)
*
* @description
* Free all slabs in the empty slab list.
*
******************************************************************************/
void crypto_free_empty_slab_list()
{
int i;
for(i = 0; i < NUM_SLOT_SIZE; i++) {
crypto_free_slab_list(&empty_slab_list[i]);
}
}
/*****************************************************************************
* function:
* slab_list_stat(qae_slab * list)
* @param[in] list, pointer to a slab list
* @description
* print statistical information about a slab list.
*
******************************************************************************/
void slab_list_stat(qae_slab_pool * list)
{
qae_slab *slb;
int index;
if(0 == list->slot_size) {
MEM_DEBUG("The list is empty.\n");
return;
}
for (slb = list->next, index = 0; index < list->slot_size;
slb = slb->next) {
MEM_DEBUG("Slab index : %d\n",index);
index++;
MEM_DEBUG("Slab virtual addr : %p\n",
(void *)slb->memCfg.virtualAddress);
MEM_DEBUG("Slab physical addr: %p\n",
(void *)slb->memCfg.physicalAddress);
MEM_DEBUG("Slab slot size : %d\n",slb->slot_size);
MEM_DEBUG("Slab used slots : %d\n",slb->used_slots);
MEM_DEBUG("Slab total slots : %d\n",slb->total_slots);
}
return;
}
/*****************************************************************************
* function:
* crypto_cleanup_slabs(void)
*
* @description
* Free all memory managed by the slab allocator. This function is
* intended to be registered as an atexit() handler.
*
*****************************************************************************/
void crypto_cleanup_slabs(void)
{
crypto_free_empty_slab_list();
#ifdef QAT_MEM_DEBUG
int i;
/* stat of available slab list*/
for(i = 0; i < NUM_SLOT_SIZE; i++) {
MEM_DEBUG("available_slab_list[%d]:\n",i);
slab_list_stat(&available_slab_list[i]);
}
/*stat of full slab list*/
MEM_DEBUG("full_slab_list:\n");
slab_list_stat(&full_slab_list);
#endif
}
/******************************************************************************
* function:
* crypto_init(void)
*
* @description
* Initialise the user-space part of the QAT memory allocator.
*
******************************************************************************/
static void crypto_init(void)
{
int i = 0;
MEM_WARN("Memory Driver Warnings Enabled.\n");
MEM_DEBUG("Memory Driver Debug Enabled.\n");
for(i = 0 ; i < NUM_SLOT_SIZE ; i++) {
init_pool(&available_slab_list[i]);
init_pool(&empty_slab_list[i]);
}
init_pool(&full_slab_list);
#ifdef USE_QAT_CONTIG_MEM
if ((crypto_qat_contig_memfd = qat_open("/dev/qat_contig_mem", O_RDWR)) == FD_ERROR) {
perror("open qat_contig_mem");
exit(EXIT_FAILURE);
}
#endif
atexit(crypto_cleanup_slabs);
crypto_inited = 1;
}
/*****************************************************************************
* function:
* qaeCryptoAtFork()
*
* @description
* allocate and remap memory following a fork
*
*****************************************************************************/
void qaeCryptoAtFork()
{
MEM_DEBUG("qaeCryptoAtFork.\n");
int i;
fork_slab_list(&full_slab_list);
for(i = 0;i < NUM_SLOT_SIZE; i++) {
fork_slab_list(&empty_slab_list[i]);
fork_slab_list(&available_slab_list[i]);
}
}
/******************************************************************************
* function:
* qaeCryptoMemV2P(void *v)
*
* @param[in] v, virtual memory address pointer
* @retval CpaPhysicalAddress, the physical memory address pointer, it
* returns 0 if not found.
*
* description:
* map virtual memory address to physical memory address
*
******************************************************************************/
CpaPhysicalAddr qaeCryptoMemV2P(void *v)
{
qat_contig_mem_config *memCfg = NULL;
void *pVirtPageAddress = NULL;
ptrdiff_t offset = 0;
if (unlikely(v == NULL)) {
MEM_WARN("NULL address passed to function\n");
return (CpaPhysicalAddr)0;
}
/* Get the physical address contained in the slab
header using the fact the slabs are aligned in
virtual address space */
pVirtPageAddress = (void *)(((ptrdiff_t)v) &
(~(MAX_PAGES*PAGE_SIZE-1)));
offset = (ptrdiff_t)v &
(ptrdiff_t)(MAX_PAGES*PAGE_SIZE-1);
memCfg = (qat_contig_mem_config *)pVirtPageAddress;
if(memCfg->signature == QAT_CONTIG_MEM_ALLOC_SIG)
return (CpaPhysicalAddr)(memCfg->physicalAddress + offset);
MEM_WARN("Virtual to Physical memory lookup failure\n");
return (CpaPhysicalAddr)0;
}
/**************************************
* Memory functions
*************************************/
/******************************************************************************
* function:
* qaeCryptoMemAlloc(size_t memsize, const char *file, int line)
*