-
Notifications
You must be signed in to change notification settings - Fork 4
/
zhpe_memreg.c
1344 lines (1172 loc) · 42.5 KB
/
zhpe_memreg.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
/*
* Copyright (C) 2018-2020 Hewlett Packard Enterprise Development LP.
* All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* BSD license below:
*
* 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.
*
* 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.
*/
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/hugetlb.h>
#include <linux/sched/signal.h>
#include <zhpe.h>
#include <zhpe_driver.h>
static void umem_kref_free(struct kref *ref); /* forward reference */
static void umem_free(struct zhpe_umem *umem);
static void umem_free_zmmu(struct zhpe_umem *umem);
static void zhpe_mmun_signal(struct file_data *fdata, struct zhpe_umem *umem,
unsigned long start, unsigned long end);
void zhpe_pte_info_dbg(uint debug_flag, const char *callf, uint line,
struct zhpe_pte_info *info)
{
uint64_t access = info->access;
bool local = !!(access & (ZHPE_MR_GET|ZHPE_MR_PUT));
bool remote = !!(access &
(ZHPE_MR_GET_REMOTE|ZHPE_MR_PUT_REMOTE));
bool cpu_visible = !!(access & ZHPE_MR_REQ_CPU);
bool individual = !!(access & ZHPE_MR_INDIVIDUAL);
bool zmmu_only = !!(access & ZHPE_MR_ZMMU_ONLY);
debug_caller(debug_flag, callf, line,
"info = %px vaddr = 0x%016llx/0x%016llx, len = 0x%lx/0x%llx"
", access = 0x%llx, local = %u, remote = %u, cpu_visible = %u"
", individual = %u, zmmu_only = %u\n",
info, info->addr, info->addr_aligned, info->length,
info->length_adjusted, info->access, local, remote,
cpu_visible, individual, zmmu_only);
}
static inline int umem_cmp(uint64_t vaddr, uint64_t length, uint64_t access,
const struct zhpe_umem *u)
{
int cmp;
const struct zhpe_pte_info *info = &u->pte_info;
cmp = arithcmp(vaddr, u->vaddr);
if (cmp)
return cmp;
cmp = arithcmp(length, info->length);
if (cmp)
return cmp;
return arithcmp(access, info->access);
}
static uint64_t umem_rsp_zaddr(struct zhpe_umem *umem)
{
uint64_t ret = BASE_ADDR_ERROR;
struct zhpe_pte_info *info = &umem->pte_info;
struct zhpe_umem *bigu;
/* caller must already hold fdata->mr_lock */
if (!(info->access & (ZHPE_MR_GET_REMOTE|ZHPE_MR_PUT_REMOTE)))
goto out;
if (info->access & ZHPE_MR_INDIVIDUAL) {
ret = zhpe_zmmu_pte_addr(&umem->pte_info);
goto out;
}
bigu = umem->fdata->big_rsp_umem;
if (!bigu)
goto out;
ret = zhpe_zmmu_pte_addr(&bigu->pte_info) + umem->vaddr - bigu->vaddr;
out:
return ret;
}
static struct zhpe_umem *
umem_range_search_and_unmap(struct file_data *fdata, uint64_t start,
uint64_t end)
{
struct zhpe_umem *ret;
struct zhpe_umem *u;
struct zhpe_umem *next;
uint64_t old;
spin_lock(&fdata->mr_lock);
rbtree_postorder_for_each_entry_safe(u, next, &fdata->mr_tree, node) {
if (start < (u->vaddr + u->pte_info.length) && u->vaddr < end) {
if (unlikely(u == fdata->big_rsp_umem))
continue;
/* active_kptr valid? */
if (unlikely(!u->active_kptr))
/* No, nothing to do, but clean up. */
goto cleanup;
/* Yes, bit 0 is the shoot down flag, set it; clean up if active. */
old = __sync_fetch_and_or(u->active_kptr, 1);
if (likely(!(old & 1)) && unlikely(old))
goto cleanup;
}
}
spin_unlock(&fdata->mr_lock);
return NULL;
cleanup:
ret = u;
kref_get(&ret->refcount);
rbtree_postorder_for_each_entry_safe(u, next, &fdata->mr_tree, node)
umem_free_zmmu(u);
spin_unlock(&fdata->mr_lock);
zhpe_zmmu_rsp_take_snapshot(fdata->bridge);
return ret;
}
static struct zhpe_umem *umem_search(struct file_data *fdata,
uint64_t vaddr, uint64_t length,
uint64_t access, uint64_t rsp_zaddr)
{
struct zhpe_umem *unode;
struct rb_node *rnode;
struct rb_root *root = &fdata->mr_tree;
/* caller must already hold fdata->mr_lock */
rnode = root->rb_node;
while (rnode) {
int result;
unode = container_of(rnode, struct zhpe_umem, node);
result = umem_cmp(vaddr, length, access, unode);
if (result < 0) {
rnode = rnode->rb_left;
} else if (result > 0) {
rnode = rnode->rb_right;
} else {
if (rsp_zaddr == umem_rsp_zaddr(unode))
goto out;
else
goto fail;
}
}
fail:
unode = NULL;
out:
return unode;
}
static struct zhpe_umem *umem_insert(struct zhpe_umem *umem)
{
struct zhpe_pte_info *info = &umem->pte_info;
struct file_data *fdata = umem->fdata;
struct rb_root *root = &fdata->mr_tree;
struct rb_node **new = &root->rb_node, *parent = NULL;
spin_lock(&fdata->mr_lock);
/* figure out where to put new node */
while (*new) {
struct zhpe_umem *this =
container_of(*new, struct zhpe_umem, node);
int64_t result = umem_cmp(umem->vaddr, info->length, info->access,
this);
parent = *new;
if (result < 0) {
new = &((*new)->rb_left);
} else if (result > 0) {
new = &((*new)->rb_right);
} else { /* already there */
umem = this;
kref_get(&umem->refcount);
goto out;
}
}
/* add new node and rebalance tree */
rb_link_node(&umem->node, parent, new);
rb_insert_color(&umem->node, root);
out:
spin_unlock(&fdata->mr_lock);
return umem;
}
static inline void umem_remove(struct zhpe_umem *umem)
{
kref_put(&umem->refcount, umem_kref_free);
}
/* Returns the offset of the umem start relative to the first page */
static inline int zhpe_umem_offset(struct zhpe_umem *umem)
{
return umem->vaddr & (BIT(umem->page_shift) - 1);
}
/* Returns the first page of a umem */
static inline unsigned long zhpe_umem_start(struct zhpe_umem *umem)
{
return umem->vaddr - zhpe_umem_offset(umem);
}
/* Returns the address of the page after the last one of a umem */
static inline unsigned long zhpe_umem_end(struct zhpe_umem *umem)
{
return ALIGN(umem->vaddr + umem->pte_info.length, BIT(umem->page_shift));
}
static inline size_t zhpe_umem_num_pages(struct zhpe_umem *umem)
{
return (zhpe_umem_end(umem) - zhpe_umem_start(umem)) >> umem->page_shift;
}
/* Revisit: not in public header file */
struct iommu_domain;
extern int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid);
/**
* zhpe_dma_map_sg_attrs - Map a scatter/gather list to DMA addresses
* @br: The bridge for which the DMA addresses are to be created
* @sg: The array of scatter/gather entries
* @nents: The number of scatter/gather entries
* @direction: The direction of the DMA
* @dma_attrs: The DMA attributes
*/
static inline int zhpe_dma_map_sg_attrs(struct bridge *br,
struct scatterlist *sg, int nents,
enum dma_data_direction direction,
unsigned long dma_attrs, uint pasid)
{
int sl, ret = 0;
#ifdef HAVE_RHEL
struct dma_attrs dattrs = {
.flags[0] = dma_attrs
};
struct dma_attrs *attrs = &dattrs;
#else
unsigned long attrs = dma_attrs;
#endif
/* Revisit: add PASID support */
for (sl = 0; sl < SLICES; sl++) {
if (!SLICE_VALID(&br->slice[sl]))
continue;
ret = dma_map_sg_attrs(&br->slice[sl].pdev->dev, sg, nents,
direction, attrs);
/* Revisit: handle ret > 0 but different amongst the slices? */
if (ret <= 0) {
while (--sl >= 0) /* undo the ones we already did */
dma_unmap_sg(&br->slice[sl].pdev->dev, sg, nents, direction);
break;
}
/* Revisit: workaround for iommu bug */
if (!no_iommu)
amd_iommu_flush_tlb(br->slice[sl].dom, pasid);
}
return ret;
}
/**
* zhpe_dma_unmap_sg - Unmap a scatter/gather list of DMA addresses
* @br: The bridge for which the DMA addresses were created
* @sg: The array of scatter/gather entries
* @nents: The number of scatter/gather entries
* @direction: The direction of the DMA
*/
static inline void zhpe_dma_unmap_sg(struct bridge *br,
struct scatterlist *sg, int nents,
enum dma_data_direction direction)
{
int sl;
for (sl = 0; sl < SLICES; sl++)
if (SLICE_VALID(&br->slice[sl]))
dma_unmap_sg(&br->slice[sl].pdev->dev, sg, nents, direction);
}
static void _zhpe_umem_release(struct zhpe_umem *umem)
{
struct file_data *fdata = umem->fdata;
struct scatterlist *sg;
struct page *page;
int i;
if (!umem->need_release)
return;
if (umem->nmap > 0)
zhpe_dma_unmap_sg(fdata->bridge, umem->sg_head.sgl,
umem->npages,
DMA_BIDIRECTIONAL);
for_each_sg(umem->sg_head.sgl, sg, umem->npages, i) {
page = sg_page(sg);
if (!PageDirty(page) && umem->writable && umem->dirty)
set_page_dirty_lock(page);
put_page(page);
}
sg_free_table(&umem->sg_head);
/* No mm if called from process cleanup */
if (current->mm) {
down_write(¤t->mm->mmap_sem);
current->mm->pinned_vm -= umem->npages;
up_write(¤t->mm->mmap_sem);
}
}
static inline long
get_user_pages_compat(unsigned long start, unsigned long nr_pages,
bool write, bool force, struct page **pages,
struct vm_area_struct **vmas)
{
#ifdef HAVE_RHEL
return get_user_pages(current, current->mm, start, nr_pages,
write, force, pages, vmas);
#else
unsigned int gup_flags;
gup_flags = (write ? FOLL_WRITE : 0) | (force ? FOLL_FORCE : 0);
return get_user_pages(start, nr_pages, gup_flags, pages, vmas);
#endif
}
/**
* zhpe_umem_get - Pin and DMA map userspace memory.
*
* @fdata: userspace context to pin memory for
* @vaddr: userspace virtual address to start at
* @size: length of region to pin
* @access: ZHPE_MR_xxx flags for memory being pinned
* @dmasync: flush in-flight DMA when the memory region is written
*/
static noinline // Revisit: debug
struct zhpe_umem *zhpe_umem_get(struct file_data *fdata, uint64_t vaddr,
size_t size, uint64_t access, bool dmasync,
int64_t *active_uptr)
{
struct zhpe_umem *umem;
struct zhpe_pte_info *info;
struct page **page_list;
struct vm_area_struct **vma_list;
unsigned long locked;
unsigned long lock_limit;
unsigned long cur_base;
unsigned long offset; /* Revisit: temporary - remove when IOMMU working */
unsigned long npages;
int ret;
int i;
unsigned long dma_attrs = 0;
struct scatterlist *sg, *sg_list_start;
bool first_page = true; /* Revisit: temporary */
if (dmasync)
dma_attrs |= DMA_ATTR_WRITE_BARRIER;
/*
* If the combination of the addr and size requested for this memory
* region causes an integer overflow, return error.
*/
if (((vaddr + size) < vaddr) ||
PAGE_ALIGN(vaddr + size) < (vaddr + size))
return ERR_PTR(-EINVAL);
if (!can_do_mlock())
return ERR_PTR(-EPERM);
umem = do_kmalloc(sizeof(struct zhpe_umem), GFP_KERNEL, true);
if (!umem)
return ERR_PTR(-ENOMEM);
info = &umem->pte_info;
umem->fdata = fdata;
get_file_data(umem->fdata);
umem->vaddr = vaddr;
info->addr = vaddr;
info->length = size;
info->access = access;
info->space_type = GENZ_DATA; /* the only supported type */
umem->page_shift = PAGE_SHIFT;
umem->pid = get_task_pid(current, PIDTYPE_PID);
umem->writable = !!(access & (ZHPE_MR_GET|ZHPE_MR_PUT_REMOTE));
/* We assume the memory is from hugetlb until proven otherwise */
umem->hugetlb = 1;
kref_init(&umem->refcount);
umem->active_page = NULL;
umem->active_kptr = NULL;
umem->active_uptr = NULL;
debug(DEBUG_MEMREG, "vaddr = 0x%016llx, size = 0x%zx, access = 0x%llx\n",
vaddr, size, access);
if (access & ZHPE_MR_ZMMU_ONLY)
return umem;
page_list = (struct page **)do__get_free_page(GFP_KERNEL, false);
if (!page_list) {
debug(DEBUG_MEMREG, "failed to allocate page_list\n");
umem_free(umem);
return ERR_PTR(-ENOMEM);
}
/*
* if we can't alloc the vma_list, it's not so bad;
* just assume the memory is not hugetlb memory
*/
vma_list = (struct vm_area_struct **)do__get_free_page(GFP_KERNEL, false);
if (!vma_list)
umem->hugetlb = 0;
npages = zhpe_umem_num_pages(umem);
down_write(¤t->mm->mmap_sem);
locked = npages + current->mm->pinned_vm + 1;
lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
ret = -ENOMEM;
debug(DEBUG_MEMREG, "locked (%lu) > lock_limit (%lu)\n",
locked, lock_limit);
goto out;
}
if (active_uptr) {
umem->active_uptr = active_uptr;
cur_base = (uintptr_t)active_uptr & PAGE_MASK;
ret = get_user_pages_compat(cur_base, 1, true, false,
&umem->active_page, vma_list);
if (ret < 0) {
debug(DEBUG_MEMREG, "get_user_pages(0x%lx, %lu) failed\n",
cur_base, (ulong)1);
umem->active_page = NULL;
goto out;
}
umem->active_kptr = (page_address(umem->active_page) +
((uintptr_t)active_uptr & ~PAGE_MASK));
}
cur_base = vaddr & PAGE_MASK;
if (npages == 0 || npages > UINT_MAX) {
ret = -EINVAL;
debug(DEBUG_MEMREG, "invalid npages (%lu)\n", npages);
goto out;
}
ret = sg_alloc_table(&umem->sg_head, npages, GFP_KERNEL);
if (ret) {
debug(DEBUG_MEMREG, "sg_alloc_table failed\n");
goto out;
}
umem->need_release = true;
sg_list_start = umem->sg_head.sgl;
while (npages) {
/* Revisit: new code shouldn't call get_user_pages */
ret = get_user_pages_compat(cur_base,
min_t(unsigned long, npages,
PAGE_SIZE / sizeof (struct page *)),
true, !umem->writable, page_list, vma_list);
if (ret < 0) {
debug(DEBUG_MEMREG, "get_user_pages(0x%lx, %lu) failed\n",
cur_base, npages);
goto out;
}
umem->npages += ret;
current->mm->pinned_vm += ret;
cur_base += ret * PAGE_SIZE;
npages -= ret;
for_each_sg(sg_list_start, sg, ret, i) {
if (vma_list && !is_vm_hugetlb_page(vma_list[i]))
umem->hugetlb = 0;
sg_set_page(sg, page_list[i], PAGE_SIZE, 0);
/* Revisit: temporary - remove when IOMMU is working */
if (first_page) {
if (vma_list)
offset = vaddr & (vma_kernel_pagesize(vma_list[i]) - 1);
else
offset = vaddr & ~PAGE_MASK;
umem->physaddr = PFN_PHYS(page_to_pfn(page_list[i])) | offset;
if (no_iommu)
info->addr = umem->physaddr;
first_page = false;
}
}
/* preparing for next loop */
sg_list_start = sg;
}
/* Revisit: set DMA direction based on access flags? */
umem->nmap = zhpe_dma_map_sg_attrs(fdata->bridge,
umem->sg_head.sgl,
umem->npages,
DMA_BIDIRECTIONAL,
dma_attrs, fdata->pasid);
if (umem->nmap <= 0) {
ret = -ENOMEM;
debug(DEBUG_MEMREG, "zhpe_dma_map_sg_attrs failed\n");
goto out;
}
ret = 0;
out:
up_write(¤t->mm->mmap_sem);
if (ret < 0)
umem_free(umem);
else
umem->dirty = true;
if (vma_list)
do_free_page(vma_list);
do_free_page(page_list);
return ret < 0 ? ERR_PTR(ret) : umem;
}
static void umem_free_zmmu(struct zhpe_umem *umem)
{
struct zhpe_pte_info *info = &umem->pte_info;
uint64_t access = info->access;
bool remote, individual, zmmu_only;
zhpe_pte_info_dbg(DEBUG_MEMREG, __func__, __LINE__, info);
remote = !!(access & (ZHPE_MR_GET_REMOTE|ZHPE_MR_PUT_REMOTE));
individual = !!(access & ZHPE_MR_INDIVIDUAL);
zmmu_only = !!(access & ZHPE_MR_ZMMU_ONLY);
if (remote && (individual || zmmu_only))
zhpe_zmmu_rsp_pte_free(info);
}
static void umem_free(struct zhpe_umem *umem)
{
zhpe_pte_info_dbg(DEBUG_MEMREG, __func__, __LINE__, &umem->pte_info);
_zhpe_umem_release(umem);
put_pid(umem->pid);
put_file_data(umem->fdata);
if (umem->active_page)
put_page(umem->active_page);
do_kfree(umem);
}
static void umem_kref_free(struct kref *ref)
{
struct zhpe_umem *umem = container_of(ref, struct zhpe_umem, refcount);
umem_free_zmmu(umem);
#if 0
zhpe_zmmu_rsp_take_snapshot(umem->fdata->bridge);
#endif
umem_free(umem);
}
void zhpe_umem_free_all(struct file_data *fdata)
{
struct zhpe_umem *umem, *next;
struct rb_root root;
debug(DEBUG_MEMREG, "\n");
/*
* This will be called once during teardown There should be no other threads
* trying to operate on the tree.
*
* The internals of the rbtree code have been necessarily exposed for
* performance reasons; it is known that the rb_root contains just
* a single pointer and there are no back references to it.
* Therefore, it should be safe to make a copy of the mr_tree's rb_root
* and reset it to an empty tree and then traverse the tree without locks.
* Which is useful, because umem_free_unlocked() can sleep.
*
* We do want to grab the mr_lock while copying the root, to make sure
* all the proper barriers have been used before we access the tree.
*/
spin_lock(&fdata->mr_lock);
root = fdata->mr_tree;
fdata->mr_tree = RB_ROOT;
spin_unlock(&fdata->mr_lock);
if (!RB_EMPTY_ROOT(&root)) {
/* First pass to free all the ZMMU entries. */
rbtree_postorder_for_each_entry_safe(umem, next, &root, node)
umem_free_zmmu(umem);
/* Do snapshot to ensure all memory transactions are complete. */
zhpe_zmmu_rsp_take_snapshot(fdata->bridge);
/* Second pass to free all the user mappings and data structures. */
rbtree_postorder_for_each_entry_safe(umem, next, &root, node) {
WARN_ON(kref_read(&umem->refcount) != 1);
umem_free(umem);
}
}
}
static inline int rmr_cmp(uint32_t dgcid, uint64_t rsp_zaddr,
uint64_t length, uint64_t access,
const struct zhpe_rmr *r)
{
int cmp;
const struct zhpe_pte_info *info = r->pte_info;
cmp = arithcmp(dgcid, r->dgcid);
if (cmp)
return cmp;
cmp = arithcmp(rsp_zaddr, r->rsp_zaddr);
if (cmp)
return cmp;
cmp = arithcmp(length, info->length);
if (cmp)
return cmp;
return arithcmp(access, info->access);
}
static inline int64_t rmr_uu_cmp(uint64_t rsp_zaddr,
uint64_t length, uint64_t access,
struct file_data *fdata,
const struct zhpe_rmr *r)
{
int cmp;
const struct zhpe_pte_info *info = r->pte_info;
cmp = arithcmp(rsp_zaddr, r->rsp_zaddr);
if (cmp)
return cmp;
cmp = arithcmp(length, info->length);
if (cmp)
return cmp;
cmp = arithcmp(access, info->access);
if (cmp)
return cmp;
return zhpe_uuid_cmp(&fdata->local_uuid->uuid,
&r->fdata->local_uuid->uuid);
}
static struct zhpe_rmr *rmr_search(struct file_data *fdata,
uint32_t dgcid, uint64_t rsp_zaddr,
uint64_t length, uint64_t access,
uint64_t req_addr)
{
struct zhpe_rmr *rmr;
struct rb_node *rnode;
struct rb_root *root = &fdata->fd_rmr_tree;
/* caller must already hold fdata->mr_lock */
rnode = root->rb_node;
while (rnode) {
int64_t result;
rmr = container_of(rnode, struct zhpe_rmr, fd_node);
result = rmr_cmp(dgcid, rsp_zaddr, length, access, rmr);
if (result < 0) {
rnode = rnode->rb_left;
} else if (result > 0) {
rnode = rnode->rb_right;
} else {
if (req_addr == zhpe_zmmu_pte_addr(rmr->pte_info))
goto out;
else
goto fail;
}
}
fail:
rmr = NULL;
out:
return rmr;
}
static struct zhpe_rmr *rmr_insert(struct zhpe_rmr *rmr)
{
struct zhpe_pte_info *info = rmr->pte_info;
struct file_data *fdata = rmr->fdata;
struct rb_root *root = &fdata->fd_rmr_tree;
struct rb_node **new = &root->rb_node, *parent = NULL;
spin_lock(&fdata->mr_lock);
/* figure out where to put new node in fdata->fd_rmr_tree */
while (*new) {
struct zhpe_rmr *this =
container_of(*new, struct zhpe_rmr, fd_node);
int64_t result = rmr_cmp(rmr->dgcid, rmr->rsp_zaddr,
info->length, info->access, this);
parent = *new;
if (result < 0) {
new = &((*new)->rb_left);
} else if (result > 0) {
new = &((*new)->rb_right);
} else { /* already there */
rmr = this;
kref_get(&rmr->refcount);
goto unlock;
}
}
/* add new node and rebalance tree */
rb_link_node(&rmr->fd_node, parent, new);
rb_insert_color(&rmr->fd_node, root);
rmr->fd_erase = true;
rmr->un_erase = true;
/* figure out where to put new node in unode->un_rmr_tree */
root = &rmr->unode->un_rmr_tree;
new = &root->rb_node;
parent = NULL;
while (*new) {
struct zhpe_rmr *this =
container_of(*new, struct zhpe_rmr, un_node);
int64_t result = rmr_uu_cmp(rmr->rsp_zaddr,
info->length, info->access, fdata, this);
parent = *new;
if (result < 0) {
new = &((*new)->rb_left);
} else if (result > 0) {
new = &((*new)->rb_right);
} else { /* already there - should never happen */
goto unlock;
}
}
/* add new node and rebalance tree */
rb_link_node(&rmr->un_node, parent, new);
rb_insert_color(&rmr->un_node, root);
unlock:
spin_unlock(&fdata->mr_lock);
return rmr;
}
static void rmr_free(struct kref *ref)
{
/* caller must already hold fdata->mr_lock */
struct zhpe_rmr *rmr = container_of(ref, struct zhpe_rmr, refcount);
struct file_data *fdata = rmr->fdata;
if (rmr->zmap)
zmap_fdata_free(fdata, rmr->zmap);
zhpe_zmmu_req_pte_free(rmr);
if (rmr->fd_erase)
rb_erase(&rmr->fd_node, &fdata->fd_rmr_tree);
if (rmr->un_erase)
rb_erase(&rmr->un_node, &rmr->unode->un_rmr_tree);
zhpe_uuid_remove(rmr->uu); /* remove reference to uu */
put_file_data(rmr->fdata);
do_kfree(rmr);
}
static inline void rmr_remove(struct zhpe_rmr *rmr, bool lock)
{
struct file_data *fdata = rmr->fdata;
if (lock)
spin_lock(&fdata->mr_lock);
kref_put(&rmr->refcount, rmr_free);
if (lock)
spin_unlock(&fdata->mr_lock);
}
void zhpe_rmr_remove_unode(struct file_data *fdata, struct uuid_node *unode)
{
struct rb_root *root = &unode->un_rmr_tree;
struct rb_node *rb, *next;
struct zhpe_rmr *rmr;
struct zhpe_pte_info *info;
char str[GCID_STRING_LEN+1];
spin_lock(&fdata->mr_lock);
for (rb = rb_first_postorder(root); rb; rb = next) {
rmr = container_of(rb, struct zhpe_rmr, un_node);
info = rmr->pte_info;
debug(DEBUG_MEMREG,
"dgcid = %s, rsp_zaddr = 0x%016llx, "
"len = 0x%zx, access = 0x%llx\n",
zhpe_gcid_str(rmr->dgcid, str, sizeof(str)), rmr->rsp_zaddr,
info->length, info->access);
next = rb_next_postorder(rb); /* must precede rmr_free() */
rmr->fd_erase = true;
rmr->un_erase = false;
rmr_free(&rmr->refcount);
}
spin_unlock(&fdata->mr_lock);
}
void zhpe_rmr_free_all(struct file_data *fdata)
{
struct rb_node *rb, *next;
struct zhpe_rmr *rmr;
struct zhpe_pte_info *info;
char str[GCID_STRING_LEN+1];
spin_lock(&fdata->mr_lock);
for (rb = rb_first_postorder(&fdata->fd_rmr_tree); rb; rb = next) {
rmr = container_of(rb, struct zhpe_rmr, fd_node);
info = rmr->pte_info;
debug(DEBUG_MEMREG,
"dgcid = %s, rsp_zaddr = 0x%016llx, "
"len = 0x%zx, access = 0x%llx\n",
zhpe_gcid_str(rmr->dgcid, str, sizeof(str)), rmr->rsp_zaddr,
info->length, info->access);
next = rb_next_postorder(rb); /* must precede rmr_free() */
rmr->fd_erase = false;
rmr->un_erase = true;
rmr_free(&rmr->refcount);
}
fdata->fd_rmr_tree = RB_ROOT;
spin_unlock(&fdata->mr_lock);
}
static struct zmap *rmr_zmap_alloc(struct file_data *fdata,
struct zhpe_rmr *rmr)
{
union zpages *zpages;
struct zmap *zmap;
if (zhpe_reqz_phy_cpuvisible_off & 1)
return ERR_PTR(-ENODEV);
zpages = rmr_zpages_alloc(rmr);
if (!zpages)
return ERR_PTR(-ENOMEM);
zmap = zmap_alloc(fdata, zpages);
if (IS_ERR(zmap)) {
zpages_free(zpages);
goto out;
}
rmr->zmap = zmap;
zmap->owner = fdata;
out:
return zmap;
}
/* Revisit: add a "uuid_free_rmr" function */
static int do_mr_reg(struct file_data *fdata, uint64_t vaddr, uint64_t len,
uint64_t access, int64_t *active_uptr,
struct zhpe_rsp_MR_REG *rsp_mr_reg)
{
int status = 0;
uint64_t rsp_zaddr = BASE_ADDR_ERROR;
uint64_t physaddr = BASE_ADDR_ERROR;
uint32_t pg_ps = 0;
bool local, remote, cpu_visible, individual, dmasync;
struct zhpe_umem *umem = NULL, *found;
bool zmmu_valid = false;
bool zmmu_only;
access &= ZHPE_MR_USER_MASK;
local = !!(access & (ZHPE_MR_GET|ZHPE_MR_PUT));
remote = !!(access & (ZHPE_MR_GET_REMOTE|ZHPE_MR_PUT_REMOTE));
cpu_visible = !!(access & ZHPE_MR_REQ_CPU);
individual = !!(access & ZHPE_MR_INDIVIDUAL);
zmmu_only = !!(access & ZHPE_MR_ZMMU_ONLY);
dmasync = false; /* Revisit: fix this */
debug(DEBUG_MEMREG, "vaddr = 0x%016llx, len = 0x%llx, access = 0x%llx, "
"local = %u, remote = %u, cpu_visible = %u, individual = %u, "
"zmmu_only %u\n",
vaddr, len, access, local, remote, cpu_visible, individual,
zmmu_only);
if (zmmu_only) {
if (local || !remote || cpu_visible) {
status = -EINVAL;
goto out;
}
}
else if (cpu_visible) {
status = -EINVAL;
goto out;
}
/* pin memory range and create IOMMU entries */
umem = zhpe_umem_get(fdata, vaddr, len, access, dmasync, active_uptr);
if (IS_ERR(umem)) {
status = PTR_ERR(umem);
umem = NULL;
goto out;
}
physaddr = umem->physaddr;
/* create responder ZMMU entries, if necessary */
if (remote) {
if (individual || zmmu_only) {
status = zhpe_zmmu_rsp_pte_alloc(&umem->pte_info, &rsp_zaddr,
&pg_ps);
if (status >= 0) {
zmmu_valid = true;
if (zmmu_only) {
spin_lock(&fdata->mr_lock);
if (fdata->big_rsp_umem)
status = -EEXIST;
else
fdata->big_rsp_umem = umem;
spin_unlock(&fdata->mr_lock);
}
}
} else {
/* Compute rsp_zaddr as offset into big_rsp_umem */
spin_lock(&fdata->mr_lock);
rsp_zaddr = umem_rsp_zaddr(umem);
if (rsp_zaddr != BASE_ADDR_ERROR)
status = 0;
else
status = -ENOENT;
spin_unlock(&fdata->mr_lock);
}
if (status < 0)
goto out;
}
/* The last things is to insert the umem in the tree. */
found = umem_insert(umem);
if (found != umem) {
/* Remove extra ref from found. */
umem_remove(found);
status = -EEXIST;
goto out;
}
rsp_mr_reg->rsp_zaddr = rsp_zaddr;
rsp_mr_reg->pg_ps = pg_ps;
rsp_mr_reg->physaddr = physaddr;
out:
if (status < 0 && umem) {
if (zmmu_valid) {
umem_free_zmmu(umem);
#if 0
zhpe_zmmu_rsp_take_snapshot(umem->fdata->bridge);
#endif
}
umem_free(umem);
}
debug(DEBUG_MEMREG, "ret = %d rsp_zaddr = 0x%016llx, "
"pg_ps=%u, physaddr = 0x%016llx\n",
status, rsp_zaddr, pg_ps, physaddr);
return status;
}
int zhpe_user_req_MR_REG(struct io_entry *entry)
{
union zhpe_req *req = &entry->op.req;
union zhpe_rsp *rsp = &entry->op.rsp;
int status = 0;
CHECK_INIT_STATE(entry, status, out);