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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
|
// SPDX-License-Identifier: MIT
/*
* Copyright © 2021 Intel Corporation
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include "igt.h"
#include "igt_map.h"
#include "intel_allocator.h"
#include "intel_allocator_msgchannel.h"
#include "intel_pat.h"
#include "xe/xe_query.h"
#include "xe/xe_util.h"
#ifdef ALLOCDBG
#define alloc_info igt_info
#define alloc_debug igt_debug
static const char *reqtype_str[] = {
[REQ_STOP] = "stop",
[REQ_OPEN] = "open",
[REQ_CLOSE] = "close",
[REQ_ADDRESS_RANGE] = "address range",
[REQ_ALLOC] = "alloc",
[REQ_FREE] = "free",
[REQ_IS_ALLOCATED] = "is allocated",
[REQ_RESERVE] = "reserve",
[REQ_UNRESERVE] = "unreserve",
[REQ_RESERVE_IF_NOT_ALLOCATED] = "reserve-ina",
[REQ_IS_RESERVED] = "is reserved",
};
static inline const char *reqstr(enum reqtype request_type)
{
igt_assert(request_type >= REQ_STOP && request_type <= REQ_IS_RESERVED);
return reqtype_str[request_type];
}
#else
#define alloc_info(...) {}
#define alloc_debug(...) {}
#endif
#ifdef ALLOCBINDDBG
#define bind_info igt_info
#define bind_debug igt_debug
#else
#define bind_info(...) {}
#define bind_debug(...) {}
#endif
/*
* We limit allocator space to avoid hang when batch would be
* pinned in the last page.
*/
#define RESERVED 4096
struct allocator {
int fd;
uint32_t ctx;
uint32_t vm;
_Atomic(int32_t) refcount;
struct intel_allocator *ial;
};
struct handle_entry {
uint64_t handle;
struct allocator *al;
};
/* For tracking alloc()/free() for Xe */
struct ahnd_info {
int fd;
uint64_t ahnd;
uint32_t vm;
enum intel_driver driver;
struct igt_map *bind_map;
pthread_mutex_t bind_map_mutex;
};
enum allocator_bind_op {
BOUND,
TO_BIND,
TO_UNBIND,
};
struct allocator_object {
uint32_t handle;
uint64_t offset;
uint64_t size;
uint8_t pat_index;
enum allocator_bind_op bind_op;
};
struct intel_allocator *
intel_allocator_reloc_create(int fd, uint64_t start, uint64_t end);
struct intel_allocator *
intel_allocator_random_create(int fd, uint64_t start, uint64_t end);
struct intel_allocator *
intel_allocator_simple_create(int fd, uint64_t start, uint64_t end,
enum allocator_strategy strategy);
/*
* Instead of trying to find first empty handle just get new one. Assuming
* our counter is incremented 2^32 times per second (4GHz clock and handle
* assignment takes single clock) 64-bit counter would wrap around after
* ~68 years.
*
* allocator
* handles <fd, ctx> intel allocator
* +-----+ +--------+ +-------------+
* | 1 +---------->+ fd: 3 +--------->+ data: ... |
* +-----+ +---->+ ctx: 1 | | refcount: 2 |
* | 2 +-----+ | ref: 2 | +-------------+
* +-----+ +--------+
* | 3 +--+ +--------+ intel allocator
* +-----+ | | fd: 3 | +-------------+
* | ... | +------->| ctx: 2 +--------->+ data: ... |
* +-----+ | ref: 1 | | refcount: 1 |
* | n +--------+ +--------+ +-------------+
* +-----+ |
* | ... +-----+ | allocator
* +-----+ | | <fd, vm> intel allocator
* | ... +--+ | | +--------+ +-------------+
* + + | | +->+ fd: 3 +-----+--->+ data: ... |
* | +---->+ vm: 1 | | | refcount: 3 |
* | | ref: 2 | | +-------------+
* | +--------+ |
* | +--------+ |
* | | fd: 3 | |
* +------->+ vm: 2 +-----+
* | ref: 1 |
* +--------+
*/
static _Atomic(uint64_t) next_handle;
static struct igt_map *handles;
static struct igt_map *ctx_map;
static struct igt_map *vm_map;
static pthread_mutex_t map_mutex = PTHREAD_MUTEX_INITIALIZER;
#define GET_MAP(vm) ((vm) ? vm_map : ctx_map)
static bool multiprocess;
static pthread_t allocator_thread;
static bool allocator_thread_running;
static bool warn_if_not_empty;
/* For allocator purposes we need to track pid/tid */
static pid_t allocator_pid = -1;
extern pid_t child_pid;
extern __thread pid_t child_tid;
/*
* Track alloc()/free() requires storing in local process which has
* an access to real drm fd it can work on.
*/
extern struct igt_map *ahnd_map;
extern pthread_mutex_t ahnd_map_mutex;
/*
* - for parent process we have child_pid == -1
* - for child which calls intel_allocator_init() allocator_pid == child_pid
*/
static inline bool is_same_process(void)
{
return child_pid == -1 || allocator_pid == child_pid;
}
static struct msg_channel *channel;
static int send_alloc_stop(struct msg_channel *msgchan)
{
struct alloc_req req = {0};
req.request_type = REQ_STOP;
return msgchan->send_req(msgchan, &req);
}
static int send_req(struct msg_channel *msgchan, pid_t tid,
struct alloc_req *request)
{
request->tid = tid;
return msgchan->send_req(msgchan, request);
}
static int recv_req(struct msg_channel *msgchan, struct alloc_req *request)
{
return msgchan->recv_req(msgchan, request);
}
static int send_resp(struct msg_channel *msgchan,
pid_t tid, struct alloc_resp *response)
{
response->tid = tid;
return msgchan->send_resp(msgchan, response);
}
static int recv_resp(struct msg_channel *msgchan,
pid_t tid, struct alloc_resp *response)
{
response->tid = tid;
return msgchan->recv_resp(msgchan, response);
}
static inline void map_entry_free_func(struct igt_map_entry *entry)
{
free(entry->data);
}
static bool can_report_gtt_size(int fd)
{
struct drm_i915_gem_context_param p = {
.param = I915_CONTEXT_PARAM_GTT_SIZE
};
return (__gem_context_get_param(fd, &p) == 0);
}
static uint64_t __handle_create(struct allocator *al)
{
struct handle_entry *h = malloc(sizeof(*h));
igt_assert(h);
h->handle = atomic_fetch_add(&next_handle, 1);
h->al = al;
igt_map_insert(handles, h, h);
return h->handle;
}
static void __handle_destroy(uint64_t handle)
{
struct handle_entry he = { .handle = handle };
igt_map_remove(handles, &he, map_entry_free_func);
}
static struct allocator *__allocator_find(int fd, uint32_t ctx, uint32_t vm)
{
struct allocator al = { .fd = fd, .ctx = ctx, .vm = vm };
struct igt_map *map = GET_MAP(vm);
return igt_map_search(map, &al);
}
static struct allocator *__allocator_find_by_handle(uint64_t handle)
{
struct handle_entry *h, he = { .handle = handle };
h = igt_map_search(handles, &he);
if (!h)
return NULL;
return h->al;
}
static struct allocator *__allocator_create(int fd, uint32_t ctx, uint32_t vm,
struct intel_allocator *ial)
{
struct igt_map *map = GET_MAP(vm);
struct allocator *al = malloc(sizeof(*al));
igt_assert(al);
igt_assert(fd == ial->fd);
al->fd = fd;
al->ctx = ctx;
al->vm = vm;
atomic_init(&al->refcount, 0);
al->ial = ial;
igt_map_insert(map, al, al);
return al;
}
static void __allocator_destroy(struct allocator *al)
{
struct igt_map *map = GET_MAP(al->vm);
igt_map_remove(map, al, map_entry_free_func);
}
static int __allocator_get(struct allocator *al)
{
struct intel_allocator *ial = al->ial;
int refcount;
atomic_fetch_add(&al->refcount, 1);
refcount = atomic_fetch_add(&ial->refcount, 1);
igt_assert(refcount >= 0);
return refcount;
}
static bool __allocator_put(struct allocator *al)
{
struct intel_allocator *ial = al->ial;
bool released = false;
int refcount, al_refcount;
al_refcount = atomic_fetch_sub(&al->refcount, 1);
refcount = atomic_fetch_sub(&ial->refcount, 1);
igt_assert(refcount >= 1);
if (refcount == 1) {
if (!ial->is_empty(ial) && warn_if_not_empty)
igt_warn("Allocator not clear before destroy!\n");
/* Check allocator has also refcount == 1 */
igt_assert_eq(al_refcount, 1);
released = true;
}
return released;
}
static struct intel_allocator *intel_allocator_create(int fd,
uint64_t start, uint64_t end,
uint8_t allocator_type,
uint8_t allocator_strategy,
uint64_t default_alignment)
{
struct intel_allocator *ial = NULL;
switch (allocator_type) {
/*
* Few words of explanation is required here.
*
* INTEL_ALLOCATOR_NONE allows keeping information in the code (intel-bb
* is an example) we're not using IGT allocator itself and likely
* we rely on relocations.
* So trying to create NONE allocator doesn't makes sense and below
* assertion catches such invalid usage.
*/
case INTEL_ALLOCATOR_NONE:
igt_assert_f(allocator_type != INTEL_ALLOCATOR_NONE,
"We cannot use NONE allocator\n");
break;
case INTEL_ALLOCATOR_RELOC:
ial = intel_allocator_reloc_create(fd, start, end);
break;
case INTEL_ALLOCATOR_SIMPLE:
ial = intel_allocator_simple_create(fd, start, end,
allocator_strategy);
break;
default:
igt_assert_f(ial, "Allocator type %d not implemented\n",
allocator_type);
break;
}
igt_assert(ial);
ial->type = allocator_type;
ial->strategy = allocator_strategy;
ial->default_alignment = default_alignment;
pthread_mutex_init(&ial->mutex, NULL);
return ial;
}
static void intel_allocator_destroy(struct intel_allocator *ial)
{
alloc_info("Destroying allocator (empty: %d)\n", ial->is_empty(ial));
ial->destroy(ial);
}
static struct allocator *allocator_open(int fd, uint32_t ctx, uint32_t vm,
uint64_t start, uint64_t end,
uint8_t allocator_type,
uint8_t allocator_strategy,
uint64_t default_alignment,
uint64_t *ahndp)
{
struct intel_allocator *ial;
struct allocator *al;
const char *idstr = vm ? "vm" : "ctx";
igt_assert(ahndp);
al = __allocator_find(fd, ctx, vm);
if (!al) {
alloc_info("Allocator fd: %d, ctx: %u, vm: %u, <0x%llx : 0x%llx>, "
"default alignment: 0x%llx "
"not found, creating one\n",
fd, ctx, vm, (long long) start, (long long) end,
(long long) default_alignment);
ial = intel_allocator_create(fd, start, end, allocator_type,
allocator_strategy,
default_alignment);
al = __allocator_create(fd, ctx, vm, ial);
}
ial = al->ial;
igt_assert_f(ial->type == allocator_type,
"Allocator type must be same for fd/%s\n", idstr);
igt_assert_f(ial->strategy == allocator_strategy,
"Allocator strategy must be same or fd/%s\n", idstr);
igt_assert_f(ial->default_alignment == default_alignment,
"Allocator default alignment must be same or fd/%s\n", idstr);
__allocator_get(al);
*ahndp = __handle_create(al);
return al;
}
static bool allocator_close(uint64_t ahnd)
{
struct allocator *al;
bool released, is_empty = false;
al = __allocator_find_by_handle(ahnd);
if (!al) {
igt_warn("Cannot find handle: %llx\n", (long long) ahnd);
return false;
}
released = __allocator_put(al);
if (released) {
is_empty = al->ial->is_empty(al->ial);
intel_allocator_destroy(al->ial);
}
if (!atomic_load(&al->refcount))
__allocator_destroy(al);
__handle_destroy(ahnd);
return is_empty;
}
static int send_req_recv_resp(struct msg_channel *msgchan,
struct alloc_req *request,
struct alloc_resp *response)
{
int ret;
ret = send_req(msgchan, child_tid, request);
if (ret < 0) {
igt_warn("Error sending request [type: %d]: err = %d [%s]\n",
request->request_type, errno, strerror(errno));
return ret;
}
ret = recv_resp(msgchan, child_tid, response);
if (ret < 0)
igt_warn("Error receiving response [type: %d]: err = %d [%s]\n",
request->request_type, errno, strerror(errno));
/*
* This is main assumption - we receive message which size must be > 0.
* If this is fulfilled we return 0 as a success.
*/
if (ret > 0)
ret = 0;
return ret;
}
static int handle_request(struct alloc_req *req, struct alloc_resp *resp)
{
int ret;
long refcnt;
memset(resp, 0, sizeof(*resp));
if (is_same_process()) {
struct intel_allocator *ial;
struct allocator *al;
uint64_t start, end, size, ahnd;
uint32_t ctx, vm;
bool allocated, reserved, unreserved;
/* Used when debug is on, so avoid compilation warnings */
(void) ctx;
(void) vm;
(void) refcnt;
/*
* Mutex only work on allocator instance, not stop/open/close
*/
if (req->request_type > REQ_CLOSE) {
/*
* We have to lock map mutex because concurrent open
* can lead to resizing the map.
*/
pthread_mutex_lock(&map_mutex);
al = __allocator_find_by_handle(req->allocator_handle);
pthread_mutex_unlock(&map_mutex);
igt_assert(al);
ial = al->ial;
igt_assert(ial);
pthread_mutex_lock(&ial->mutex);
}
switch (req->request_type) {
case REQ_STOP:
alloc_info("<stop>\n");
break;
case REQ_OPEN:
pthread_mutex_lock(&map_mutex);
al = allocator_open(req->open.fd,
req->open.ctx, req->open.vm,
req->open.start, req->open.end,
req->open.allocator_type,
req->open.allocator_strategy,
req->open.default_alignment,
&ahnd);
refcnt = atomic_load(&al->refcount);
ret = atomic_load(&al->ial->refcount);
pthread_mutex_unlock(&map_mutex);
resp->response_type = RESP_OPEN;
resp->open.allocator_handle = ahnd;
alloc_info("<open> [tid: %ld] fd: %d, ahnd: %" PRIx64
", ctx: %u, vm: %u"
", alloc_type: %u, defalign: %llx"
", al->refcnt: %ld->%ld"
", refcnt: %d->%d\n",
(long) req->tid, req->open.fd, ahnd,
req->open.ctx, req->open.vm,
req->open.allocator_type,
(long long) req->open.default_alignment,
refcnt - 1, refcnt, ret - 1, ret);
break;
case REQ_CLOSE:
pthread_mutex_lock(&map_mutex);
al = __allocator_find_by_handle(req->allocator_handle);
resp->response_type = RESP_CLOSE;
if (!al) {
alloc_info("<close> [tid: %ld] ahnd: %" PRIx64
" -> no handle\n",
(long) req->tid, req->allocator_handle);
pthread_mutex_unlock(&map_mutex);
break;
}
resp->response_type = RESP_CLOSE;
ctx = al->ctx;
vm = al->vm;
refcnt = atomic_load(&al->refcount);
ret = atomic_load(&al->ial->refcount);
resp->close.is_empty = allocator_close(req->allocator_handle);
pthread_mutex_unlock(&map_mutex);
alloc_info("<close> [tid: %ld] ahnd: %" PRIx64
", ctx: %u, vm: %u"
", is_empty: %d, al->refcount: %ld->%ld"
", refcnt: %d->%d\n",
(long) req->tid, req->allocator_handle,
ctx, vm, resp->close.is_empty,
refcnt, refcnt - 1, ret, ret - 1);
break;
case REQ_ADDRESS_RANGE:
resp->response_type = RESP_ADDRESS_RANGE;
ial->get_address_range(ial, &start, &end);
resp->address_range.start = start;
resp->address_range.end = end;
alloc_info("<address range> [tid: %ld] ahnd: %" PRIx64
", ctx: %u, vm: %u"
", start: 0x%" PRIx64 ", end: 0x%" PRId64 "\n",
(long) req->tid, req->allocator_handle,
al->ctx, al->vm, start, end);
break;
case REQ_ALLOC:
req->alloc.alignment = max(ial->default_alignment,
req->alloc.alignment);
resp->response_type = RESP_ALLOC;
resp->alloc.offset = ial->alloc(ial,
req->alloc.handle,
req->alloc.size,
req->alloc.alignment,
req->alloc.pat_index,
req->alloc.strategy);
alloc_info("<alloc> [tid: %ld] ahnd: %" PRIx64
", ctx: %u, vm: %u, handle: %u"
", size: 0x%" PRIx64 ", offset: 0x%" PRIx64
", alignment: 0x%" PRIx64 ", pat_index: %u, strategy: %u\n",
(long) req->tid, req->allocator_handle,
al->ctx, al->vm,
req->alloc.handle, req->alloc.size,
resp->alloc.offset, req->alloc.alignment,
req->alloc.pat_index, req->alloc.strategy);
break;
case REQ_FREE:
resp->response_type = RESP_FREE;
resp->free.freed = ial->free(ial, req->free.handle);
alloc_info("<free> [tid: %ld] ahnd: %" PRIx64
", ctx: %u, vm: %u"
", handle: %u, freed: %d\n",
(long) req->tid, req->allocator_handle,
al->ctx, al->vm,
req->free.handle, resp->free.freed);
break;
case REQ_IS_ALLOCATED:
resp->response_type = RESP_IS_ALLOCATED;
allocated = ial->is_allocated(ial,
req->is_allocated.handle,
req->is_allocated.size,
req->is_allocated.offset);
resp->is_allocated.allocated = allocated;
alloc_info("<is allocated> [tid: %ld] ahnd: %" PRIx64
", ctx: %u, vm: %u"
", offset: 0x%" PRIx64
", allocated: %d\n", (long) req->tid,
req->allocator_handle, al->ctx, al->vm,
req->is_allocated.offset, allocated);
break;
case REQ_RESERVE:
resp->response_type = RESP_RESERVE;
reserved = ial->reserve(ial,
req->reserve.handle,
req->reserve.start,
req->reserve.end);
resp->reserve.reserved = reserved;
alloc_info("<reserve> [tid: %ld] ahnd: %" PRIx64
", ctx: %u, vm: %u, handle: %u"
", start: 0x%" PRIx64 ", end: 0x%" PRIx64
", reserved: %d\n",
(long) req->tid, req->allocator_handle,
al->ctx, al->vm, req->reserve.handle,
req->reserve.start, req->reserve.end, reserved);
break;
case REQ_UNRESERVE:
resp->response_type = RESP_UNRESERVE;
unreserved = ial->unreserve(ial,
req->unreserve.handle,
req->unreserve.start,
req->unreserve.end);
resp->unreserve.unreserved = unreserved;
alloc_info("<unreserve> [tid: %ld] ahnd: %" PRIx64
", ctx: %u, vm: %u, handle: %u"
", start: 0x%" PRIx64 ", end: 0x%" PRIx64
", unreserved: %d\n",
(long) req->tid, req->allocator_handle,
al->ctx, al->vm, req->unreserve.handle,
req->unreserve.start, req->unreserve.end,
unreserved);
break;
case REQ_IS_RESERVED:
resp->response_type = RESP_IS_RESERVED;
reserved = ial->is_reserved(ial,
req->is_reserved.start,
req->is_reserved.end);
resp->is_reserved.reserved = reserved;
alloc_info("<is reserved> [tid: %ld] ahnd: %" PRIx64
", ctx: %u, vm: %u"
", start: 0x%" PRIx64 ", end: 0x%" PRIx64
", reserved: %d\n",
(long) req->tid, req->allocator_handle,
al->ctx, al->vm, req->is_reserved.start,
req->is_reserved.end, reserved);
break;
case REQ_RESERVE_IF_NOT_ALLOCATED:
resp->response_type = RESP_RESERVE_IF_NOT_ALLOCATED;
size = req->reserve.end - req->reserve.start;
allocated = ial->is_allocated(ial, req->reserve.handle,
size, req->reserve.start);
if (allocated) {
resp->reserve_if_not_allocated.allocated = allocated;
alloc_info("<reserve if not allocated> [tid: %ld] "
"ahnd: %" PRIx64 ", ctx: %u, vm: %u"
", handle: %u, size: 0x%lx"
", start: 0x%" PRIx64 ", end: 0x%" PRIx64
", allocated: %d, reserved: %d\n",
(long) req->tid, req->allocator_handle,
al->ctx, al->vm, req->reserve.handle,
(long) size, req->reserve.start,
req->reserve.end, allocated, false);
break;
}
reserved = ial->reserve(ial,
req->reserve.handle,
req->reserve.start,
req->reserve.end);
resp->reserve_if_not_allocated.reserved = reserved;
alloc_info("<reserve if not allocated> [tid: %ld] "
"ahnd: %" PRIx64 ", ctx: %u, vm: %u"
", handle: %u, start: 0x%" PRIx64 ", end: 0x%" PRIx64
", allocated: %d, reserved: %d\n",
(long) req->tid, req->allocator_handle,
al->ctx, al->vm,
req->reserve.handle,
req->reserve.start, req->reserve.end,
false, reserved);
break;
}
if (req->request_type > REQ_CLOSE)
pthread_mutex_unlock(&ial->mutex);
return 0;
}
igt_assert_f(channel->ready,
"Allocator must be called in multiprocess mode, "
"use intel_allocator_multiprocess_(start|stop)()\n");
ret = send_req_recv_resp(channel, req, resp);
if (ret < 0)
exit(0);
return ret;
}
static void *allocator_thread_loop(void *data)
{
struct alloc_req req;
struct alloc_resp resp;
int ret;
(void) data;
alloc_info("Allocator pid: %ld, tid: %ld\n",
(long) allocator_pid, (long) gettid());
alloc_info("Entering allocator loop\n");
WRITE_ONCE(allocator_thread_running, true);
while (1) {
ret = recv_req(channel, &req);
if (ret == -1) {
igt_warn("Error receiving request in thread, ret = %d [%s]\n",
ret, strerror(errno));
igt_waitchildren_timeout(1, "Stopping children, error receiving request\n");
return (void *) -1;
}
/* Fake message to stop the thread */
if (req.request_type == REQ_STOP) {
alloc_info("<stop request>\n");
break;
}
ret = handle_request(&req, &resp);
if (ret) {
igt_warn("Error handling request in thread, ret = %d [%s]\n",
ret, strerror(errno));
break;
}
ret = send_resp(channel, req.tid, &resp);
if (ret) {
igt_warn("Error sending response in thread, ret = %d [%s]\n",
ret, strerror(errno));
igt_waitchildren_timeout(1, "Stopping children, error sending response\n");
return (void *) -1;
}
}
WRITE_ONCE(allocator_thread_running, false);
return NULL;
}
/**
* __intel_allocator_multiprocess_prepare:
*
* Prepares allocator infrastructure to work in multiprocess mode.
*
* Some description is required why prepare/start steps are separated.
* When we write the code and we don't use address sanitizer simple
* intel_allocator_multiprocess_start() call is enough. With address
* sanitizer and using forking we can encounter situation where one
* forked child called allocator alloc() (so parent has some poisoned
* memory in shadow map), then second fork occurs. Second child will
* get poisoned shadow map from parent (there allocator thread reside).
* Checking shadow map in this child will report memory leak.
*
* How to separate initialization steps take a look into api_intel_allocator.c
* fork_simple_stress() function.
*/
void __intel_allocator_multiprocess_prepare(void)
{
intel_allocator_init();
multiprocess = true;
channel->init(channel);
}
#define START_TIMEOUT_MS 100
void __intel_allocator_multiprocess_start(void)
{
int time_left = START_TIMEOUT_MS;
pthread_create(&allocator_thread, NULL,
allocator_thread_loop, NULL);
/* Wait unless allocator thread get started */
while (time_left-- > 0 && !READ_ONCE(allocator_thread_running))
usleep(1000);
}
/**
* intel_allocator_multiprocess_start:
*
* Function turns on intel_allocator multiprocess mode what means
* all allocations from children processes are performed in a separate thread
* within main igt process. Children are aware of the situation and use
* some interprocess communication channel to send/receive messages
* (open, close, alloc, free, ...) to/from allocator thread.
*
* Must be used when you want to use an allocator in non single-process code.
* All allocations in threads spawned in main igt process are handled by
* mutexing, not by sending/receiving messages to/from allocator thread.
*
* Note. This destroys all previously created allocators and theirs content.
*/
void intel_allocator_multiprocess_start(void)
{
alloc_info("allocator multiprocess start\n");
igt_assert_f(child_pid == -1,
"Allocator thread can be spawned only in main IGT process\n");
__intel_allocator_multiprocess_prepare();
__intel_allocator_multiprocess_start();
}
/**
* intel_allocator_multiprocess_stop:
*
* Function turns off intel_allocator multiprocess mode what means
* stopping allocator thread and deinitializing its data.
*/
#define STOP_TIMEOUT_MS 100
void intel_allocator_multiprocess_stop(void)
{
int time_left = STOP_TIMEOUT_MS;
alloc_info("allocator multiprocess stop\n");
if (multiprocess) {
send_alloc_stop(channel);
/* Give allocator thread time to complete */
while (time_left-- > 0 && READ_ONCE(allocator_thread_running))
usleep(1000); /* coarse calculation */
/* Deinit, this should stop all blocked syscalls, if any */
channel->deinit(channel);
pthread_join(allocator_thread, NULL);
/* But we're not sure does child will stuck */
igt_waitchildren_timeout(5, "Stopping children");
multiprocess = false;
}
}
static void track_ahnd(int fd, uint64_t ahnd, uint32_t vm)
{
struct ahnd_info *ainfo;
pthread_mutex_lock(&ahnd_map_mutex);
ainfo = igt_map_search(ahnd_map, &ahnd);
if (!ainfo) {
ainfo = malloc(sizeof(*ainfo));
ainfo->fd = fd;
ainfo->ahnd = ahnd;
ainfo->vm = vm;
ainfo->driver = get_intel_driver(fd);
ainfo->bind_map = igt_map_create(igt_map_hash_32, igt_map_equal_32);
pthread_mutex_init(&ainfo->bind_map_mutex, NULL);
bind_debug("[TRACK AHND] pid: %d, tid: %d, create <fd: %d, "
"ahnd: %llx, vm: %u, driver: %d, ahnd_map: %p, bind_map: %p>\n",
getpid(), gettid(), ainfo->fd,
(long long)ainfo->ahnd, ainfo->vm,
ainfo->driver, ahnd_map, ainfo->bind_map);
igt_map_insert(ahnd_map, &ainfo->ahnd, ainfo);
}
pthread_mutex_unlock(&ahnd_map_mutex);
}
static void untrack_ahnd(uint64_t ahnd)
{
struct ahnd_info *ainfo;
pthread_mutex_lock(&ahnd_map_mutex);
ainfo = igt_map_search(ahnd_map, &ahnd);
if (ainfo) {
bind_debug("[UNTRACK AHND]: pid: %d, tid: %d, removing ahnd: %llx\n",
getpid(), gettid(), (long long)ahnd);
igt_map_remove(ahnd_map, &ahnd, map_entry_free_func);
}
pthread_mutex_unlock(&ahnd_map_mutex);
}
static uint64_t __intel_allocator_open_full(int fd, uint32_t ctx,
uint32_t vm,
uint64_t start, uint64_t end,
uint8_t allocator_type,
enum allocator_strategy strategy,
uint64_t default_alignment)
{
struct alloc_req req = { .request_type = REQ_OPEN,
.open.fd = fd,
.open.ctx = ctx,
.open.vm = vm,
.open.start = start,
.open.end = end,
.open.allocator_type = allocator_type,
.open.allocator_strategy = strategy,
.open.default_alignment = default_alignment };
struct alloc_resp resp;
uint64_t gtt_size;
if (is_i915_device(fd)) {
if (!start)
req.open.start = gem_detect_safe_start_offset(fd);
if (!end) {
igt_assert_f(can_report_gtt_size(fd), "Invalid fd\n");
gtt_size = gem_aperture_size(fd);
if (!gem_uses_full_ppgtt(fd))
gtt_size /= 2;
else
gtt_size -= RESERVED;
req.open.end = gtt_size;
}
if (!default_alignment)
req.open.default_alignment = gem_detect_safe_alignment(fd);
req.open.start = ALIGN(req.open.start, req.open.default_alignment);
} else {
struct xe_device *xe_dev = xe_device_get(fd);
igt_assert(xe_dev);
if (!default_alignment)
req.open.default_alignment = xe_get_default_alignment(fd);
if (!end)
req.open.end = 1ull << xe_dev->va_bits;
}
/* Get child_tid only once at open() */
if (child_tid == -1)
child_tid = gettid();
igt_assert(handle_request(&req, &resp) == 0);
igt_assert(resp.open.allocator_handle);
igt_assert(resp.response_type == RESP_OPEN);
/*
* Igts mostly uses ctx as id when opening the allocator (i915 legacy).
* If ctx is passed let's use it as an vm id, otherwise use vm.
*/
track_ahnd(fd, resp.open.allocator_handle, ctx ?: vm);
return resp.open.allocator_handle;
}
/**
* intel_allocator_open_full:
* @fd: i915 descriptor
* @ctx: context
* @start: address of the beginning
* @end: address of the end
* @allocator_type: one of INTEL_ALLOCATOR_* define
* @strategy: passed to the allocator to define the strategy (like order
* of allocation, see notes below)
* @default_alignment: default objects alignment - power-of-two requested
* alignment, if 0 then safe alignment will be chosen
*
* Function opens an allocator instance within <@start, @end) vm for given
* @fd and @ctx and returns its handle. If the allocator for such pair
* doesn't exist it is created with refcount = 1.
* Parallel opens returns same handle bumping its refcount.
*
* Returns: unique handle to the currently opened allocator.
*
* Notes:
*
* If start = end = 0, the allocator is opened for the whole available gtt.
*
* Strategy is generally used internally by the underlying allocator:
*
* For SIMPLE allocator:
* - ALLOC_STRATEGY_HIGH_TO_LOW means topmost addresses are allocated first,
* - ALLOC_STRATEGY_LOW_TO_HIGH opposite, allocation starts from lowest
* addresses.
*
* For RANDOM allocator:
* - no strategy is currently implemented.
*/
uint64_t intel_allocator_open_full(int fd, uint32_t ctx,
uint64_t start, uint64_t end,
uint8_t allocator_type,
enum allocator_strategy strategy,
uint64_t default_alignment)
{
return __intel_allocator_open_full(fd, ctx, 0, start, end,
allocator_type, strategy,
default_alignment);
}
uint64_t intel_allocator_open_vm_full(int fd, uint32_t vm,
uint64_t start, uint64_t end,
uint8_t allocator_type,
enum allocator_strategy strategy,
uint64_t default_alignment)
{
igt_assert(vm != 0);
return __intel_allocator_open_full(fd, 0, vm, start, end,
allocator_type, strategy,
default_alignment);
}
/**
* intel_allocator_open:
* @fd: i915 or xe descriptor
* @ctx: context
* @allocator_type: one of INTEL_ALLOCATOR_* define
*
* Function opens an allocator instance for given @fd and @ctx and returns
* its handle. If the allocator for such pair doesn't exist it is created
* with refcount = 1. Parallel opens returns same handle bumping its refcount.
*
* Returns: unique handle to the currently opened allocator.
*
* Notes: we pass ALLOC_STRATEGY_HIGH_TO_LOW as default, playing with higher
* addresses makes easier to find addressing issues (like passing non-canonical
* offsets, which won't be catched unless 47-bit is set).
*/
uint64_t intel_allocator_open(int fd, uint32_t ctx, uint8_t allocator_type)
{
return intel_allocator_open_full(fd, ctx, 0, 0, allocator_type,
ALLOC_STRATEGY_HIGH_TO_LOW, 0);
}
uint64_t intel_allocator_open_vm(int fd, uint32_t vm, uint8_t allocator_type)
{
return intel_allocator_open_vm_full(fd, vm, 0, 0, allocator_type,
ALLOC_STRATEGY_HIGH_TO_LOW, 0);
}
/**
* intel_allocator_close:
* @allocator_handle: handle to the allocator that will be closed
*
* Function decreases an allocator refcount for the given @handle.
* When refcount reaches zero allocator is closed (destroyed) and all
* allocated / reserved areas are freed.
*
* Returns: true if closed allocator was empty, false otherwise.
*/
bool intel_allocator_close(uint64_t allocator_handle)
{
struct alloc_req req = { .request_type = REQ_CLOSE,
.allocator_handle = allocator_handle };
struct alloc_resp resp;
igt_assert(handle_request(&req, &resp) == 0);
igt_assert(resp.response_type == RESP_CLOSE);
untrack_ahnd(allocator_handle);
return resp.close.is_empty;
}
/**
* intel_allocator_get_address_range:
* @allocator_handle: handle to an allocator
* @startp: pointer to the variable where function writes starting offset
* @endp: pointer to the variable where function writes ending offset
*
* Function fills @startp, @endp with respectively, starting and ending offset
* of the allocator working virtual address space range.
*
* Note. Allocators working ranges can differ depending on the device or
* the allocator type so before reserving a specific offset a good practise
* is to ensure that address is between accepted range.
*/
void intel_allocator_get_address_range(uint64_t allocator_handle,
uint64_t *startp, uint64_t *endp)
{
struct alloc_req req = { .request_type = REQ_ADDRESS_RANGE,
.allocator_handle = allocator_handle };
struct alloc_resp resp;
igt_assert(handle_request(&req, &resp) == 0);
igt_assert(resp.response_type == RESP_ADDRESS_RANGE);
if (startp)
*startp = resp.address_range.start;
if (endp)
*endp = resp.address_range.end;
}
static bool is_same(struct allocator_object *obj,
uint32_t handle, uint64_t offset, uint64_t size,
uint8_t pat_index, enum allocator_bind_op bind_op)
{
return obj->handle == handle && obj->offset == offset && obj->size == size &&
obj->pat_index == pat_index && (obj->bind_op == bind_op || obj->bind_op == BOUND);
}
static void track_object(uint64_t allocator_handle, uint32_t handle,
uint64_t offset, uint64_t size, uint8_t pat_index,
enum allocator_bind_op bind_op)
{
struct ahnd_info *ainfo;
struct allocator_object *obj;
bind_debug("[TRACK OBJECT]: [%s] pid: %d, tid: %d, ahnd: %llx, handle: %u, offset: %llx, size: %llx, pat_index: %u\n",
bind_op == TO_BIND ? "BIND" : "UNBIND",
getpid(), gettid(),
(long long)allocator_handle,
handle, (long long)offset, (long long)size, pat_index);
if (offset == ALLOC_INVALID_ADDRESS) {
bind_debug("[TRACK OBJECT] => invalid address %llx, skipping tracking\n",
(long long)offset);
return;
}
pthread_mutex_lock(&ahnd_map_mutex);
ainfo = igt_map_search(ahnd_map, &allocator_handle);
pthread_mutex_unlock(&ahnd_map_mutex);
igt_assert_f(ainfo, "[TRACK OBJECT] => MISSING ahnd %llx <=\n",
(long long)allocator_handle);
if (ainfo->driver == INTEL_DRIVER_I915)
return; /* no-op for i915, at least for now */
pthread_mutex_lock(&ainfo->bind_map_mutex);
obj = igt_map_search(ainfo->bind_map, &handle);
if (obj) {
/*
* User may call alloc() couple of times, check object is the
* same. For free() there's simple case, just remove from
* bind_map.
*/
if (bind_op == TO_BIND) {
igt_assert_eq(is_same(obj, handle, offset, size, pat_index, bind_op), true);
} else if (bind_op == TO_UNBIND) {
if (obj->bind_op == TO_BIND)
igt_map_remove(ainfo->bind_map, &obj->handle, map_entry_free_func);
else if (obj->bind_op == BOUND)
obj->bind_op = bind_op;
}
} else {
/* Ignore to unbind bo which wasn't previously inserted */
if (bind_op == TO_UNBIND)
goto out;
obj = calloc(1, sizeof(*obj));
obj->handle = handle;
obj->offset = offset;
obj->size = size;
obj->pat_index = pat_index;
obj->bind_op = bind_op;
igt_map_insert(ainfo->bind_map, &obj->handle, obj);
}
out:
pthread_mutex_unlock(&ainfo->bind_map_mutex);
}
/**
* __intel_allocator_alloc:
* @allocator_handle: handle to an allocator
* @handle: handle to an object
* @size: size of an object
* @alignment: determines object alignment
* @pat_index: chosen pat_index for the binding
* @strategy: chosen allocator strategy
*
* Function finds and returns the most suitable offset with given @alignment
* for an object with @size identified by the @handle.
*
* Returns: currently assigned address for a given object. If an object was
* already allocated returns same address. If allocator can't find suitable
* range returns ALLOC_INVALID_ADDRESS.
*/
uint64_t __intel_allocator_alloc(uint64_t allocator_handle, uint32_t handle,
uint64_t size, uint64_t alignment,
uint8_t pat_index, enum allocator_strategy strategy)
{
struct alloc_req req = { .request_type = REQ_ALLOC,
.allocator_handle = allocator_handle,
.alloc.handle = handle,
.alloc.size = size,
.alloc.strategy = strategy,
.alloc.alignment = alignment,
.alloc.pat_index = pat_index,
};
struct alloc_resp resp;
igt_assert((alignment & (alignment-1)) == 0);
igt_assert(handle_request(&req, &resp) == 0);
igt_assert(resp.response_type == RESP_ALLOC);
track_object(allocator_handle, handle, resp.alloc.offset, size, pat_index,
TO_BIND);
return resp.alloc.offset;
}
/**
* intel_allocator_alloc:
* @allocator_handle: handle to an allocator
* @handle: handle to an object
* @size: size of an object
* @alignment: determines object alignment
*
* Same as __intel_allocator_alloc() but asserts if allocator can't return
* valid address. Uses default allocation strategy chosen during opening
* the allocator.
*/
uint64_t intel_allocator_alloc(uint64_t allocator_handle, uint32_t handle,
uint64_t size, uint64_t alignment)
{
uint64_t offset;
offset = __intel_allocator_alloc(allocator_handle, handle,
size, alignment, DEFAULT_PAT_INDEX,
ALLOC_STRATEGY_NONE);
igt_assert(offset != ALLOC_INVALID_ADDRESS);
return offset;
}
/**
* intel_allocator_alloc_with_strategy:
* @allocator_handle: handle to an allocator
* @handle: handle to an object
* @size: size of an object
* @alignment: determines object alignment
* @strategy: strategy of allocation
*
* Same as __intel_allocator_alloc() but asserts if allocator can't return
* valid address. Use @strategy instead of default chosen during opening
* the allocator.
*/
uint64_t intel_allocator_alloc_with_strategy(uint64_t allocator_handle,
uint32_t handle,
uint64_t size, uint64_t alignment,
enum allocator_strategy strategy)
{
uint64_t offset;
offset = __intel_allocator_alloc(allocator_handle, handle,
size, alignment, DEFAULT_PAT_INDEX,
strategy);
igt_assert(offset != ALLOC_INVALID_ADDRESS);
return offset;
}
/**
* intel_allocator_free:
* @allocator_handle: handle to an allocator
* @handle: handle to an object to be freed
*
* Function free object identified by the @handle in allocator what makes it
* offset again allocable.
*
* Note. Reserved objects can only be freed by an #intel_allocator_unreserve
* function.
*
* Returns: true if the object was successfully freed, otherwise false.
*/
bool intel_allocator_free(uint64_t allocator_handle, uint32_t handle)
{
struct alloc_req req = { .request_type = REQ_FREE,
.allocator_handle = allocator_handle,
.free.handle = handle };
struct alloc_resp resp;
igt_assert(handle_request(&req, &resp) == 0);
igt_assert(resp.response_type == RESP_FREE);
track_object(allocator_handle, handle, 0, 0, 0, TO_UNBIND);
return resp.free.freed;
}
/**
* intel_allocator_is_allocated:
* @allocator_handle: handle to an allocator
* @handle: handle to an object
* @size: size of an object
* @offset: address of an object
*
* Function checks whether the object identified by the @handle and @size
* is allocated at the @offset.
*
* Returns: true if the object is currently allocated at the @offset,
* otherwise false.
*/
bool intel_allocator_is_allocated(uint64_t allocator_handle, uint32_t handle,
uint64_t size, uint64_t offset)
{
struct alloc_req req = { .request_type = REQ_IS_ALLOCATED,
.allocator_handle = allocator_handle,
.is_allocated.handle = handle,
.is_allocated.size = size,
.is_allocated.offset = offset };
struct alloc_resp resp;
igt_assert(handle_request(&req, &resp) == 0);
igt_assert(resp.response_type == RESP_IS_ALLOCATED);
return resp.is_allocated.allocated;
}
/**
* intel_allocator_reserve:
* @allocator_handle: handle to an allocator
* @handle: handle to an object
* @size: size of an object
* @offset: address of an object
*
* Function reserves space that starts at the @offset and has @size.
* Optionally we can pass @handle to mark that space is for a specific
* object, otherwise pass -1.
*
* Note. Reserved space is identified by offset and size, not a handle.
* So an object can have multiple reserved spaces with its handle.
*
* Returns: true if space is successfully reserved, otherwise false.
*/
bool intel_allocator_reserve(uint64_t allocator_handle, uint32_t handle,
uint64_t size, uint64_t offset)
{
struct alloc_req req = { .request_type = REQ_RESERVE,
.allocator_handle = allocator_handle,
.reserve.handle = handle,
.reserve.start = offset,
.reserve.end = offset + size };
struct alloc_resp resp;
igt_assert(handle_request(&req, &resp) == 0);
igt_assert(resp.response_type == RESP_RESERVE);
return resp.reserve.reserved;
}
/**
* intel_allocator_unreserve:
* @allocator_handle: handle to an allocator
* @handle: handle to an object
* @size: size of an object
* @offset: address of an object
*
* Function unreserves space that starts at the @offset, @size and @handle.
*
* Note. @handle, @size and @offset have to match those used in reservation.
* i.e. check with the same offset but even smaller size will fail.
*
* Returns: true if the space is successfully unreserved, otherwise false.
*/
bool intel_allocator_unreserve(uint64_t allocator_handle, uint32_t handle,
uint64_t size, uint64_t offset)
{
struct alloc_req req = { .request_type = REQ_UNRESERVE,
.allocator_handle = allocator_handle,
.unreserve.handle = handle,
.unreserve.start = offset,
.unreserve.end = offset + size };
struct alloc_resp resp;
igt_assert(handle_request(&req, &resp) == 0);
igt_assert(resp.response_type == RESP_UNRESERVE);
return resp.unreserve.unreserved;
}
/**
* intel_allocator_is_reserved:
* @allocator_handle: handle to an allocator
* @size: size of an object
* @offset: address of an object
*
* Function checks whether space starting at the @offset and @size is
* currently under reservation.
*
* Note. @size and @offset have to match those used in reservation,
* i.e. check with the same offset but even smaller size will fail.
*
* Returns: true if space is reserved, othwerise false.
*/
bool intel_allocator_is_reserved(uint64_t allocator_handle,
uint64_t size, uint64_t offset)
{
struct alloc_req req = { .request_type = REQ_IS_RESERVED,
.allocator_handle = allocator_handle,
.is_reserved.start = offset,
.is_reserved.end = offset + size };
struct alloc_resp resp;
igt_assert(handle_request(&req, &resp) == 0);
igt_assert(resp.response_type == RESP_IS_RESERVED);
return resp.is_reserved.reserved;
}
/**
* intel_allocator_reserve_if_not_allocated:
* @allocator_handle: handle to an allocator
* @handle: handle to an object
* @size: size of an object
* @offset: address of an object
* @is_allocatedp: if not NULL function writes there object allocation status
* (true/false)
*
* Function checks whether the object identified by the @handle and @size
* is allocated at the @offset and writes the result to @is_allocatedp.
* If it's not it reserves it at the given @offset.
*
* Returns: true if the space for an object was reserved, otherwise false.
*/
bool intel_allocator_reserve_if_not_allocated(uint64_t allocator_handle,
uint32_t handle,
uint64_t size, uint64_t offset,
bool *is_allocatedp)
{
struct alloc_req req = { .request_type = REQ_RESERVE_IF_NOT_ALLOCATED,
.allocator_handle = allocator_handle,
.reserve.handle = handle,
.reserve.start = offset,
.reserve.end = offset + size };
struct alloc_resp resp;
igt_assert(handle_request(&req, &resp) == 0);
igt_assert(resp.response_type == RESP_RESERVE_IF_NOT_ALLOCATED);
if (is_allocatedp)
*is_allocatedp = resp.reserve_if_not_allocated.allocated;
return resp.reserve_if_not_allocated.reserved;
}
/**
* intel_allocator_print:
* @allocator_handle: handle to an allocator
*
* Function prints statistics and content of the allocator.
* Mainly for debugging purposes.
*
* Note. Printing possible only in the main process.
**/
void intel_allocator_print(uint64_t allocator_handle)
{
igt_assert(allocator_handle);
if (!multiprocess || is_same_process()) {
struct allocator *al;
al = __allocator_find_by_handle(allocator_handle);
pthread_mutex_lock(&map_mutex);
al->ial->print(al->ial, true);
pthread_mutex_unlock(&map_mutex);
} else {
igt_warn("Print stats is in main process only\n");
}
}
static void __xe_op_bind(struct ahnd_info *ainfo, uint32_t sync_in, uint32_t sync_out)
{
struct allocator_object *obj;
struct igt_map_entry *pos;
struct igt_list_head obj_list;
struct xe_object *entry, *tmp;
IGT_INIT_LIST_HEAD(&obj_list);
pthread_mutex_lock(&ainfo->bind_map_mutex);
igt_map_foreach(ainfo->bind_map, pos) {
obj = pos->data;
if (obj->bind_op == BOUND)
continue;
bind_info("= [vm: %u] %s => %u %lx %lx %u\n",
ainfo->vm,
obj->bind_op == TO_BIND ? "TO BIND" : "TO UNBIND",
obj->handle, obj->offset,
obj->size, obj->pat_index);
entry = malloc(sizeof(*entry));
entry->handle = obj->handle;
entry->offset = obj->offset;
entry->size = obj->size;
entry->pat_index = obj->pat_index;
entry->bind_op = obj->bind_op == TO_BIND ? XE_OBJECT_BIND :
XE_OBJECT_UNBIND;
igt_list_add(&entry->link, &obj_list);
/*
* We clean bind_map even before calling bind/unbind
* as all binding operations asserts in case of error.
*/
if (obj->bind_op == TO_BIND)
obj->bind_op = BOUND;
else
igt_map_remove(ainfo->bind_map, &obj->handle,
map_entry_free_func);
}
pthread_mutex_unlock(&ainfo->bind_map_mutex);
xe_bind_unbind_async(ainfo->fd, ainfo->vm, 0, &obj_list, sync_in, sync_out);
igt_list_for_each_entry_safe(entry, tmp, &obj_list, link) {
igt_list_del(&entry->link);
free(entry);
}
}
uint64_t get_offset_pat_index(uint64_t ahnd, uint32_t handle, uint64_t size,
uint64_t alignment, uint8_t pat_index)
{
uint64_t offset;
offset = __intel_allocator_alloc(ahnd, handle, size, alignment,
pat_index, ALLOC_STRATEGY_NONE);
igt_assert(offset != ALLOC_INVALID_ADDRESS);
return offset;
}
/**
* intel_allocator_bind:
* @allocator_handle: handle to an allocator
* @sync_in: syncobj (fence-in)
* @sync_out: syncobj (fence-out)
*
* Function binds and unbinds all objects added to the allocator which weren't
* previously binded/unbinded.
*
**/
void intel_allocator_bind(uint64_t allocator_handle,
uint32_t sync_in, uint32_t sync_out)
{
struct ahnd_info *ainfo;
pthread_mutex_lock(&ahnd_map_mutex);
ainfo = igt_map_search(ahnd_map, &allocator_handle);
pthread_mutex_unlock(&ahnd_map_mutex);
igt_assert(ainfo);
/*
* We collect bind/unbind operations on alloc()/free() to do group
* operation getting @sync_in as syncobj handle (fence-in). If user
* passes 0 as @sync_out we bind/unbind synchronously.
*/
__xe_op_bind(ainfo, sync_in, sync_out);
}
static int equal_handles(const void *key1, const void *key2)
{
const struct handle_entry *h1 = key1, *h2 = key2;
return h1->handle == h2->handle;
}
static int equal_ctx(const void *key1, const void *key2)
{
const struct allocator *a1 = key1, *a2 = key2;
return a1->fd == a2->fd && a1->ctx == a2->ctx;
}
static int equal_vm(const void *key1, const void *key2)
{
const struct allocator *a1 = key1, *a2 = key2;
return a1->fd == a2->fd && a1->vm == a2->vm;
}
/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
#define GOLDEN_RATIO_PRIME_32 0x9e370001UL
static inline uint32_t hash_handles(const void *val)
{
uint32_t hash = ((struct handle_entry *) val)->handle;
hash = hash * GOLDEN_RATIO_PRIME_32;
return hash;
}
static inline uint32_t hash_instance(const void *val)
{
uint64_t hash = ((struct allocator *) val)->fd;
hash = hash * GOLDEN_RATIO_PRIME_32;
return hash;
}
static void __free_maps(struct igt_map *map, bool close_allocators)
{
struct igt_map_entry *pos;
const struct handle_entry *h;
if (!map)
return;
if (close_allocators)
igt_map_foreach(map, pos) {
h = pos->key;
allocator_close(h->handle);
}
igt_map_destroy(map, map_entry_free_func);
}
static void __free_ahnd_map(void)
{
struct igt_map_entry *pos;
struct ahnd_info *ainfo;
if (!ahnd_map)
return;
igt_map_foreach(ahnd_map, pos) {
ainfo = pos->data;
igt_map_destroy(ainfo->bind_map, map_entry_free_func);
}
igt_map_destroy(ahnd_map, map_entry_free_func);
}
/**
* intel_allocator_init:
*
* Function initializes the allocators infrastructure. The second call will
* override current infra and destroy existing there allocators. It is called
* in igt_constructor.
**/
void intel_allocator_init(void)
{
alloc_info("Prepare an allocator infrastructure\n");
allocator_pid = getpid();
alloc_info("Allocator pid: %ld\n", (long) allocator_pid);
__free_maps(handles, true);
__free_maps(ctx_map, false);
__free_maps(vm_map, false);
__free_ahnd_map();
atomic_init(&next_handle, 1);
handles = igt_map_create(hash_handles, equal_handles);
ctx_map = igt_map_create(hash_instance, equal_ctx);
vm_map = igt_map_create(hash_instance, equal_vm);
pthread_mutex_init(&ahnd_map_mutex, NULL);
ahnd_map = igt_map_create(igt_map_hash_64, igt_map_equal_64);
igt_assert(handles && ctx_map && vm_map && ahnd_map);
channel = intel_allocator_get_msgchannel(CHANNEL_SYSVIPC_MSGQUEUE);
}
igt_constructor {
intel_allocator_init();
}
|