forked from Intel-BMC/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmci_queue_pair.c
3263 lines (2815 loc) · 92.7 KB
/
vmci_queue_pair.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
// SPDX-License-Identifier: GPL-2.0-only
/*
* VMware VMCI Driver
*
* Copyright (C) 2012 VMware, Inc. All rights reserved.
*/
#include <linux/vmw_vmci_defs.h>
#include <linux/vmw_vmci_api.h>
#include <linux/highmem.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/pagemap.h>
#include <linux/pci.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/uio.h>
#include <linux/wait.h>
#include <linux/vmalloc.h>
#include <linux/skbuff.h>
#include "vmci_handle_array.h"
#include "vmci_queue_pair.h"
#include "vmci_datagram.h"
#include "vmci_resource.h"
#include "vmci_context.h"
#include "vmci_driver.h"
#include "vmci_event.h"
#include "vmci_route.h"
/*
* In the following, we will distinguish between two kinds of VMX processes -
* the ones with versions lower than VMCI_VERSION_NOVMVM that use specialized
* VMCI page files in the VMX and supporting VM to VM communication and the
* newer ones that use the guest memory directly. We will in the following
* refer to the older VMX versions as old-style VMX'en, and the newer ones as
* new-style VMX'en.
*
* The state transition datagram is as follows (the VMCIQPB_ prefix has been
* removed for readability) - see below for more details on the transtions:
*
* -------------- NEW -------------
* | |
* \_/ \_/
* CREATED_NO_MEM <-----------------> CREATED_MEM
* | | |
* | o-----------------------o |
* | | |
* \_/ \_/ \_/
* ATTACHED_NO_MEM <----------------> ATTACHED_MEM
* | | |
* | o----------------------o |
* | | |
* \_/ \_/ \_/
* SHUTDOWN_NO_MEM <----------------> SHUTDOWN_MEM
* | |
* | |
* -------------> gone <-------------
*
* In more detail. When a VMCI queue pair is first created, it will be in the
* VMCIQPB_NEW state. It will then move into one of the following states:
*
* - VMCIQPB_CREATED_NO_MEM: this state indicates that either:
*
* - the created was performed by a host endpoint, in which case there is
* no backing memory yet.
*
* - the create was initiated by an old-style VMX, that uses
* vmci_qp_broker_set_page_store to specify the UVAs of the queue pair at
* a later point in time. This state can be distinguished from the one
* above by the context ID of the creator. A host side is not allowed to
* attach until the page store has been set.
*
* - VMCIQPB_CREATED_MEM: this state is the result when the queue pair
* is created by a VMX using the queue pair device backend that
* sets the UVAs of the queue pair immediately and stores the
* information for later attachers. At this point, it is ready for
* the host side to attach to it.
*
* Once the queue pair is in one of the created states (with the exception of
* the case mentioned for older VMX'en above), it is possible to attach to the
* queue pair. Again we have two new states possible:
*
* - VMCIQPB_ATTACHED_MEM: this state can be reached through the following
* paths:
*
* - from VMCIQPB_CREATED_NO_MEM when a new-style VMX allocates a queue
* pair, and attaches to a queue pair previously created by the host side.
*
* - from VMCIQPB_CREATED_MEM when the host side attaches to a queue pair
* already created by a guest.
*
* - from VMCIQPB_ATTACHED_NO_MEM, when an old-style VMX calls
* vmci_qp_broker_set_page_store (see below).
*
* - VMCIQPB_ATTACHED_NO_MEM: If the queue pair already was in the
* VMCIQPB_CREATED_NO_MEM due to a host side create, an old-style VMX will
* bring the queue pair into this state. Once vmci_qp_broker_set_page_store
* is called to register the user memory, the VMCIQPB_ATTACH_MEM state
* will be entered.
*
* From the attached queue pair, the queue pair can enter the shutdown states
* when either side of the queue pair detaches. If the guest side detaches
* first, the queue pair will enter the VMCIQPB_SHUTDOWN_NO_MEM state, where
* the content of the queue pair will no longer be available. If the host
* side detaches first, the queue pair will either enter the
* VMCIQPB_SHUTDOWN_MEM, if the guest memory is currently mapped, or
* VMCIQPB_SHUTDOWN_NO_MEM, if the guest memory is not mapped
* (e.g., the host detaches while a guest is stunned).
*
* New-style VMX'en will also unmap guest memory, if the guest is
* quiesced, e.g., during a snapshot operation. In that case, the guest
* memory will no longer be available, and the queue pair will transition from
* *_MEM state to a *_NO_MEM state. The VMX may later map the memory once more,
* in which case the queue pair will transition from the *_NO_MEM state at that
* point back to the *_MEM state. Note that the *_NO_MEM state may have changed,
* since the peer may have either attached or detached in the meantime. The
* values are laid out such that ++ on a state will move from a *_NO_MEM to a
* *_MEM state, and vice versa.
*/
/* The Kernel specific component of the struct vmci_queue structure. */
struct vmci_queue_kern_if {
struct mutex __mutex; /* Protects the queue. */
struct mutex *mutex; /* Shared by producer and consumer queues. */
size_t num_pages; /* Number of pages incl. header. */
bool host; /* Host or guest? */
union {
struct {
dma_addr_t *pas;
void **vas;
} g; /* Used by the guest. */
struct {
struct page **page;
struct page **header_page;
} h; /* Used by the host. */
} u;
};
/*
* This structure is opaque to the clients.
*/
struct vmci_qp {
struct vmci_handle handle;
struct vmci_queue *produce_q;
struct vmci_queue *consume_q;
u64 produce_q_size;
u64 consume_q_size;
u32 peer;
u32 flags;
u32 priv_flags;
bool guest_endpoint;
unsigned int blocked;
unsigned int generation;
wait_queue_head_t event;
};
enum qp_broker_state {
VMCIQPB_NEW,
VMCIQPB_CREATED_NO_MEM,
VMCIQPB_CREATED_MEM,
VMCIQPB_ATTACHED_NO_MEM,
VMCIQPB_ATTACHED_MEM,
VMCIQPB_SHUTDOWN_NO_MEM,
VMCIQPB_SHUTDOWN_MEM,
VMCIQPB_GONE
};
#define QPBROKERSTATE_HAS_MEM(_qpb) (_qpb->state == VMCIQPB_CREATED_MEM || \
_qpb->state == VMCIQPB_ATTACHED_MEM || \
_qpb->state == VMCIQPB_SHUTDOWN_MEM)
/*
* In the queue pair broker, we always use the guest point of view for
* the produce and consume queue values and references, e.g., the
* produce queue size stored is the guests produce queue size. The
* host endpoint will need to swap these around. The only exception is
* the local queue pairs on the host, in which case the host endpoint
* that creates the queue pair will have the right orientation, and
* the attaching host endpoint will need to swap.
*/
struct qp_entry {
struct list_head list_item;
struct vmci_handle handle;
u32 peer;
u32 flags;
u64 produce_size;
u64 consume_size;
u32 ref_count;
};
struct qp_broker_entry {
struct vmci_resource resource;
struct qp_entry qp;
u32 create_id;
u32 attach_id;
enum qp_broker_state state;
bool require_trusted_attach;
bool created_by_trusted;
bool vmci_page_files; /* Created by VMX using VMCI page files */
struct vmci_queue *produce_q;
struct vmci_queue *consume_q;
struct vmci_queue_header saved_produce_q;
struct vmci_queue_header saved_consume_q;
vmci_event_release_cb wakeup_cb;
void *client_data;
void *local_mem; /* Kernel memory for local queue pair */
};
struct qp_guest_endpoint {
struct vmci_resource resource;
struct qp_entry qp;
u64 num_ppns;
void *produce_q;
void *consume_q;
struct ppn_set ppn_set;
};
struct qp_list {
struct list_head head;
struct mutex mutex; /* Protect queue list. */
};
static struct qp_list qp_broker_list = {
.head = LIST_HEAD_INIT(qp_broker_list.head),
.mutex = __MUTEX_INITIALIZER(qp_broker_list.mutex),
};
static struct qp_list qp_guest_endpoints = {
.head = LIST_HEAD_INIT(qp_guest_endpoints.head),
.mutex = __MUTEX_INITIALIZER(qp_guest_endpoints.mutex),
};
#define INVALID_VMCI_GUEST_MEM_ID 0
#define QPE_NUM_PAGES(_QPE) ((u32) \
(DIV_ROUND_UP(_QPE.produce_size, PAGE_SIZE) + \
DIV_ROUND_UP(_QPE.consume_size, PAGE_SIZE) + 2))
#define QP_SIZES_ARE_VALID(_prod_qsize, _cons_qsize) \
((_prod_qsize) + (_cons_qsize) >= max(_prod_qsize, _cons_qsize) && \
(_prod_qsize) + (_cons_qsize) <= VMCI_MAX_GUEST_QP_MEMORY)
/*
* Frees kernel VA space for a given queue and its queue header, and
* frees physical data pages.
*/
static void qp_free_queue(void *q, u64 size)
{
struct vmci_queue *queue = q;
if (queue) {
u64 i;
/* Given size does not include header, so add in a page here. */
for (i = 0; i < DIV_ROUND_UP(size, PAGE_SIZE) + 1; i++) {
dma_free_coherent(&vmci_pdev->dev, PAGE_SIZE,
queue->kernel_if->u.g.vas[i],
queue->kernel_if->u.g.pas[i]);
}
vfree(queue);
}
}
/*
* Allocates kernel queue pages of specified size with IOMMU mappings,
* plus space for the queue structure/kernel interface and the queue
* header.
*/
static void *qp_alloc_queue(u64 size, u32 flags)
{
u64 i;
struct vmci_queue *queue;
size_t pas_size;
size_t vas_size;
size_t queue_size = sizeof(*queue) + sizeof(*queue->kernel_if);
u64 num_pages;
if (size > SIZE_MAX - PAGE_SIZE)
return NULL;
num_pages = DIV_ROUND_UP(size, PAGE_SIZE) + 1;
if (num_pages >
(SIZE_MAX - queue_size) /
(sizeof(*queue->kernel_if->u.g.pas) +
sizeof(*queue->kernel_if->u.g.vas)))
return NULL;
pas_size = num_pages * sizeof(*queue->kernel_if->u.g.pas);
vas_size = num_pages * sizeof(*queue->kernel_if->u.g.vas);
queue_size += pas_size + vas_size;
queue = vmalloc(queue_size);
if (!queue)
return NULL;
queue->q_header = NULL;
queue->saved_header = NULL;
queue->kernel_if = (struct vmci_queue_kern_if *)(queue + 1);
queue->kernel_if->mutex = NULL;
queue->kernel_if->num_pages = num_pages;
queue->kernel_if->u.g.pas = (dma_addr_t *)(queue->kernel_if + 1);
queue->kernel_if->u.g.vas =
(void **)((u8 *)queue->kernel_if->u.g.pas + pas_size);
queue->kernel_if->host = false;
for (i = 0; i < num_pages; i++) {
queue->kernel_if->u.g.vas[i] =
dma_alloc_coherent(&vmci_pdev->dev, PAGE_SIZE,
&queue->kernel_if->u.g.pas[i],
GFP_KERNEL);
if (!queue->kernel_if->u.g.vas[i]) {
/* Size excl. the header. */
qp_free_queue(queue, i * PAGE_SIZE);
return NULL;
}
}
/* Queue header is the first page. */
queue->q_header = queue->kernel_if->u.g.vas[0];
return queue;
}
/*
* Copies from a given buffer or iovector to a VMCI Queue. Uses
* kmap()/kunmap() to dynamically map/unmap required portions of the queue
* by traversing the offset -> page translation structure for the queue.
* Assumes that offset + size does not wrap around in the queue.
*/
static int qp_memcpy_to_queue_iter(struct vmci_queue *queue,
u64 queue_offset,
struct iov_iter *from,
size_t size)
{
struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
size_t bytes_copied = 0;
while (bytes_copied < size) {
const u64 page_index =
(queue_offset + bytes_copied) / PAGE_SIZE;
const size_t page_offset =
(queue_offset + bytes_copied) & (PAGE_SIZE - 1);
void *va;
size_t to_copy;
if (kernel_if->host)
va = kmap(kernel_if->u.h.page[page_index]);
else
va = kernel_if->u.g.vas[page_index + 1];
/* Skip header. */
if (size - bytes_copied > PAGE_SIZE - page_offset)
/* Enough payload to fill up from this page. */
to_copy = PAGE_SIZE - page_offset;
else
to_copy = size - bytes_copied;
if (!copy_from_iter_full((u8 *)va + page_offset, to_copy,
from)) {
if (kernel_if->host)
kunmap(kernel_if->u.h.page[page_index]);
return VMCI_ERROR_INVALID_ARGS;
}
bytes_copied += to_copy;
if (kernel_if->host)
kunmap(kernel_if->u.h.page[page_index]);
}
return VMCI_SUCCESS;
}
/*
* Copies to a given buffer or iovector from a VMCI Queue. Uses
* kmap()/kunmap() to dynamically map/unmap required portions of the queue
* by traversing the offset -> page translation structure for the queue.
* Assumes that offset + size does not wrap around in the queue.
*/
static int qp_memcpy_from_queue_iter(struct iov_iter *to,
const struct vmci_queue *queue,
u64 queue_offset, size_t size)
{
struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
size_t bytes_copied = 0;
while (bytes_copied < size) {
const u64 page_index =
(queue_offset + bytes_copied) / PAGE_SIZE;
const size_t page_offset =
(queue_offset + bytes_copied) & (PAGE_SIZE - 1);
void *va;
size_t to_copy;
int err;
if (kernel_if->host)
va = kmap(kernel_if->u.h.page[page_index]);
else
va = kernel_if->u.g.vas[page_index + 1];
/* Skip header. */
if (size - bytes_copied > PAGE_SIZE - page_offset)
/* Enough payload to fill up this page. */
to_copy = PAGE_SIZE - page_offset;
else
to_copy = size - bytes_copied;
err = copy_to_iter((u8 *)va + page_offset, to_copy, to);
if (err != to_copy) {
if (kernel_if->host)
kunmap(kernel_if->u.h.page[page_index]);
return VMCI_ERROR_INVALID_ARGS;
}
bytes_copied += to_copy;
if (kernel_if->host)
kunmap(kernel_if->u.h.page[page_index]);
}
return VMCI_SUCCESS;
}
/*
* Allocates two list of PPNs --- one for the pages in the produce queue,
* and the other for the pages in the consume queue. Intializes the list
* of PPNs with the page frame numbers of the KVA for the two queues (and
* the queue headers).
*/
static int qp_alloc_ppn_set(void *prod_q,
u64 num_produce_pages,
void *cons_q,
u64 num_consume_pages, struct ppn_set *ppn_set)
{
u64 *produce_ppns;
u64 *consume_ppns;
struct vmci_queue *produce_q = prod_q;
struct vmci_queue *consume_q = cons_q;
u64 i;
if (!produce_q || !num_produce_pages || !consume_q ||
!num_consume_pages || !ppn_set)
return VMCI_ERROR_INVALID_ARGS;
if (ppn_set->initialized)
return VMCI_ERROR_ALREADY_EXISTS;
produce_ppns =
kmalloc_array(num_produce_pages, sizeof(*produce_ppns),
GFP_KERNEL);
if (!produce_ppns)
return VMCI_ERROR_NO_MEM;
consume_ppns =
kmalloc_array(num_consume_pages, sizeof(*consume_ppns),
GFP_KERNEL);
if (!consume_ppns) {
kfree(produce_ppns);
return VMCI_ERROR_NO_MEM;
}
for (i = 0; i < num_produce_pages; i++)
produce_ppns[i] =
produce_q->kernel_if->u.g.pas[i] >> PAGE_SHIFT;
for (i = 0; i < num_consume_pages; i++)
consume_ppns[i] =
consume_q->kernel_if->u.g.pas[i] >> PAGE_SHIFT;
ppn_set->num_produce_pages = num_produce_pages;
ppn_set->num_consume_pages = num_consume_pages;
ppn_set->produce_ppns = produce_ppns;
ppn_set->consume_ppns = consume_ppns;
ppn_set->initialized = true;
return VMCI_SUCCESS;
}
/*
* Frees the two list of PPNs for a queue pair.
*/
static void qp_free_ppn_set(struct ppn_set *ppn_set)
{
if (ppn_set->initialized) {
/* Do not call these functions on NULL inputs. */
kfree(ppn_set->produce_ppns);
kfree(ppn_set->consume_ppns);
}
memset(ppn_set, 0, sizeof(*ppn_set));
}
/*
* Populates the list of PPNs in the hypercall structure with the PPNS
* of the produce queue and the consume queue.
*/
static int qp_populate_ppn_set(u8 *call_buf, const struct ppn_set *ppn_set)
{
if (vmci_use_ppn64()) {
memcpy(call_buf, ppn_set->produce_ppns,
ppn_set->num_produce_pages *
sizeof(*ppn_set->produce_ppns));
memcpy(call_buf +
ppn_set->num_produce_pages *
sizeof(*ppn_set->produce_ppns),
ppn_set->consume_ppns,
ppn_set->num_consume_pages *
sizeof(*ppn_set->consume_ppns));
} else {
int i;
u32 *ppns = (u32 *) call_buf;
for (i = 0; i < ppn_set->num_produce_pages; i++)
ppns[i] = (u32) ppn_set->produce_ppns[i];
ppns = &ppns[ppn_set->num_produce_pages];
for (i = 0; i < ppn_set->num_consume_pages; i++)
ppns[i] = (u32) ppn_set->consume_ppns[i];
}
return VMCI_SUCCESS;
}
/*
* Allocates kernel VA space of specified size plus space for the queue
* and kernel interface. This is different from the guest queue allocator,
* because we do not allocate our own queue header/data pages here but
* share those of the guest.
*/
static struct vmci_queue *qp_host_alloc_queue(u64 size)
{
struct vmci_queue *queue;
size_t queue_page_size;
u64 num_pages;
const size_t queue_size = sizeof(*queue) + sizeof(*(queue->kernel_if));
if (size > min_t(size_t, VMCI_MAX_GUEST_QP_MEMORY, SIZE_MAX - PAGE_SIZE))
return NULL;
num_pages = DIV_ROUND_UP(size, PAGE_SIZE) + 1;
if (num_pages > (SIZE_MAX - queue_size) /
sizeof(*queue->kernel_if->u.h.page))
return NULL;
queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
if (queue_size + queue_page_size > KMALLOC_MAX_SIZE)
return NULL;
queue = kzalloc(queue_size + queue_page_size, GFP_KERNEL);
if (queue) {
queue->q_header = NULL;
queue->saved_header = NULL;
queue->kernel_if = (struct vmci_queue_kern_if *)(queue + 1);
queue->kernel_if->host = true;
queue->kernel_if->mutex = NULL;
queue->kernel_if->num_pages = num_pages;
queue->kernel_if->u.h.header_page =
(struct page **)((u8 *)queue + queue_size);
queue->kernel_if->u.h.page =
&queue->kernel_if->u.h.header_page[1];
}
return queue;
}
/*
* Frees kernel memory for a given queue (header plus translation
* structure).
*/
static void qp_host_free_queue(struct vmci_queue *queue, u64 queue_size)
{
kfree(queue);
}
/*
* Initialize the mutex for the pair of queues. This mutex is used to
* protect the q_header and the buffer from changing out from under any
* users of either queue. Of course, it's only any good if the mutexes
* are actually acquired. Queue structure must lie on non-paged memory
* or we cannot guarantee access to the mutex.
*/
static void qp_init_queue_mutex(struct vmci_queue *produce_q,
struct vmci_queue *consume_q)
{
/*
* Only the host queue has shared state - the guest queues do not
* need to synchronize access using a queue mutex.
*/
if (produce_q->kernel_if->host) {
produce_q->kernel_if->mutex = &produce_q->kernel_if->__mutex;
consume_q->kernel_if->mutex = &produce_q->kernel_if->__mutex;
mutex_init(produce_q->kernel_if->mutex);
}
}
/*
* Cleans up the mutex for the pair of queues.
*/
static void qp_cleanup_queue_mutex(struct vmci_queue *produce_q,
struct vmci_queue *consume_q)
{
if (produce_q->kernel_if->host) {
produce_q->kernel_if->mutex = NULL;
consume_q->kernel_if->mutex = NULL;
}
}
/*
* Acquire the mutex for the queue. Note that the produce_q and
* the consume_q share a mutex. So, only one of the two need to
* be passed in to this routine. Either will work just fine.
*/
static void qp_acquire_queue_mutex(struct vmci_queue *queue)
{
if (queue->kernel_if->host)
mutex_lock(queue->kernel_if->mutex);
}
/*
* Release the mutex for the queue. Note that the produce_q and
* the consume_q share a mutex. So, only one of the two need to
* be passed in to this routine. Either will work just fine.
*/
static void qp_release_queue_mutex(struct vmci_queue *queue)
{
if (queue->kernel_if->host)
mutex_unlock(queue->kernel_if->mutex);
}
/*
* Helper function to release pages in the PageStoreAttachInfo
* previously obtained using get_user_pages.
*/
static void qp_release_pages(struct page **pages,
u64 num_pages, bool dirty)
{
int i;
for (i = 0; i < num_pages; i++) {
if (dirty)
set_page_dirty_lock(pages[i]);
put_page(pages[i]);
pages[i] = NULL;
}
}
/*
* Lock the user pages referenced by the {produce,consume}Buffer
* struct into memory and populate the {produce,consume}Pages
* arrays in the attach structure with them.
*/
static int qp_host_get_user_memory(u64 produce_uva,
u64 consume_uva,
struct vmci_queue *produce_q,
struct vmci_queue *consume_q)
{
int retval;
int err = VMCI_SUCCESS;
retval = get_user_pages_fast((uintptr_t) produce_uva,
produce_q->kernel_if->num_pages,
FOLL_WRITE,
produce_q->kernel_if->u.h.header_page);
if (retval < (int)produce_q->kernel_if->num_pages) {
pr_debug("get_user_pages_fast(produce) failed (retval=%d)",
retval);
if (retval > 0)
qp_release_pages(produce_q->kernel_if->u.h.header_page,
retval, false);
err = VMCI_ERROR_NO_MEM;
goto out;
}
retval = get_user_pages_fast((uintptr_t) consume_uva,
consume_q->kernel_if->num_pages,
FOLL_WRITE,
consume_q->kernel_if->u.h.header_page);
if (retval < (int)consume_q->kernel_if->num_pages) {
pr_debug("get_user_pages_fast(consume) failed (retval=%d)",
retval);
if (retval > 0)
qp_release_pages(consume_q->kernel_if->u.h.header_page,
retval, false);
qp_release_pages(produce_q->kernel_if->u.h.header_page,
produce_q->kernel_if->num_pages, false);
err = VMCI_ERROR_NO_MEM;
}
out:
return err;
}
/*
* Registers the specification of the user pages used for backing a queue
* pair. Enough information to map in pages is stored in the OS specific
* part of the struct vmci_queue structure.
*/
static int qp_host_register_user_memory(struct vmci_qp_page_store *page_store,
struct vmci_queue *produce_q,
struct vmci_queue *consume_q)
{
u64 produce_uva;
u64 consume_uva;
/*
* The new style and the old style mapping only differs in
* that we either get a single or two UVAs, so we split the
* single UVA range at the appropriate spot.
*/
produce_uva = page_store->pages;
consume_uva = page_store->pages +
produce_q->kernel_if->num_pages * PAGE_SIZE;
return qp_host_get_user_memory(produce_uva, consume_uva, produce_q,
consume_q);
}
/*
* Releases and removes the references to user pages stored in the attach
* struct. Pages are released from the page cache and may become
* swappable again.
*/
static void qp_host_unregister_user_memory(struct vmci_queue *produce_q,
struct vmci_queue *consume_q)
{
qp_release_pages(produce_q->kernel_if->u.h.header_page,
produce_q->kernel_if->num_pages, true);
memset(produce_q->kernel_if->u.h.header_page, 0,
sizeof(*produce_q->kernel_if->u.h.header_page) *
produce_q->kernel_if->num_pages);
qp_release_pages(consume_q->kernel_if->u.h.header_page,
consume_q->kernel_if->num_pages, true);
memset(consume_q->kernel_if->u.h.header_page, 0,
sizeof(*consume_q->kernel_if->u.h.header_page) *
consume_q->kernel_if->num_pages);
}
/*
* Once qp_host_register_user_memory has been performed on a
* queue, the queue pair headers can be mapped into the
* kernel. Once mapped, they must be unmapped with
* qp_host_unmap_queues prior to calling
* qp_host_unregister_user_memory.
* Pages are pinned.
*/
static int qp_host_map_queues(struct vmci_queue *produce_q,
struct vmci_queue *consume_q)
{
int result;
if (!produce_q->q_header || !consume_q->q_header) {
struct page *headers[2];
if (produce_q->q_header != consume_q->q_header)
return VMCI_ERROR_QUEUEPAIR_MISMATCH;
if (produce_q->kernel_if->u.h.header_page == NULL ||
*produce_q->kernel_if->u.h.header_page == NULL)
return VMCI_ERROR_UNAVAILABLE;
headers[0] = *produce_q->kernel_if->u.h.header_page;
headers[1] = *consume_q->kernel_if->u.h.header_page;
produce_q->q_header = vmap(headers, 2, VM_MAP, PAGE_KERNEL);
if (produce_q->q_header != NULL) {
consume_q->q_header =
(struct vmci_queue_header *)((u8 *)
produce_q->q_header +
PAGE_SIZE);
result = VMCI_SUCCESS;
} else {
pr_warn("vmap failed\n");
result = VMCI_ERROR_NO_MEM;
}
} else {
result = VMCI_SUCCESS;
}
return result;
}
/*
* Unmaps previously mapped queue pair headers from the kernel.
* Pages are unpinned.
*/
static int qp_host_unmap_queues(u32 gid,
struct vmci_queue *produce_q,
struct vmci_queue *consume_q)
{
if (produce_q->q_header) {
if (produce_q->q_header < consume_q->q_header)
vunmap(produce_q->q_header);
else
vunmap(consume_q->q_header);
produce_q->q_header = NULL;
consume_q->q_header = NULL;
}
return VMCI_SUCCESS;
}
/*
* Finds the entry in the list corresponding to a given handle. Assumes
* that the list is locked.
*/
static struct qp_entry *qp_list_find(struct qp_list *qp_list,
struct vmci_handle handle)
{
struct qp_entry *entry;
if (vmci_handle_is_invalid(handle))
return NULL;
list_for_each_entry(entry, &qp_list->head, list_item) {
if (vmci_handle_is_equal(entry->handle, handle))
return entry;
}
return NULL;
}
/*
* Finds the entry in the list corresponding to a given handle.
*/
static struct qp_guest_endpoint *
qp_guest_handle_to_entry(struct vmci_handle handle)
{
struct qp_guest_endpoint *entry;
struct qp_entry *qp = qp_list_find(&qp_guest_endpoints, handle);
entry = qp ? container_of(
qp, struct qp_guest_endpoint, qp) : NULL;
return entry;
}
/*
* Finds the entry in the list corresponding to a given handle.
*/
static struct qp_broker_entry *
qp_broker_handle_to_entry(struct vmci_handle handle)
{
struct qp_broker_entry *entry;
struct qp_entry *qp = qp_list_find(&qp_broker_list, handle);
entry = qp ? container_of(
qp, struct qp_broker_entry, qp) : NULL;
return entry;
}
/*
* Dispatches a queue pair event message directly into the local event
* queue.
*/
static int qp_notify_peer_local(bool attach, struct vmci_handle handle)
{
u32 context_id = vmci_get_context_id();
struct vmci_event_qp ev;
ev.msg.hdr.dst = vmci_make_handle(context_id, VMCI_EVENT_HANDLER);
ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
VMCI_CONTEXT_RESOURCE_ID);
ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
ev.msg.event_data.event =
attach ? VMCI_EVENT_QP_PEER_ATTACH : VMCI_EVENT_QP_PEER_DETACH;
ev.payload.peer_id = context_id;
ev.payload.handle = handle;
return vmci_event_dispatch(&ev.msg.hdr);
}
/*
* Allocates and initializes a qp_guest_endpoint structure.
* Allocates a queue_pair rid (and handle) iff the given entry has
* an invalid handle. 0 through VMCI_RESERVED_RESOURCE_ID_MAX
* are reserved handles. Assumes that the QP list mutex is held
* by the caller.
*/
static struct qp_guest_endpoint *
qp_guest_endpoint_create(struct vmci_handle handle,
u32 peer,
u32 flags,
u64 produce_size,
u64 consume_size,
void *produce_q,
void *consume_q)
{
int result;
struct qp_guest_endpoint *entry;
/* One page each for the queue headers. */
const u64 num_ppns = DIV_ROUND_UP(produce_size, PAGE_SIZE) +
DIV_ROUND_UP(consume_size, PAGE_SIZE) + 2;
if (vmci_handle_is_invalid(handle)) {
u32 context_id = vmci_get_context_id();
handle = vmci_make_handle(context_id, VMCI_INVALID_ID);
}
entry = kzalloc(sizeof(*entry), GFP_KERNEL);
if (entry) {
entry->qp.peer = peer;
entry->qp.flags = flags;
entry->qp.produce_size = produce_size;
entry->qp.consume_size = consume_size;
entry->qp.ref_count = 0;
entry->num_ppns = num_ppns;
entry->produce_q = produce_q;
entry->consume_q = consume_q;
INIT_LIST_HEAD(&entry->qp.list_item);
/* Add resource obj */
result = vmci_resource_add(&entry->resource,
VMCI_RESOURCE_TYPE_QPAIR_GUEST,
handle);
entry->qp.handle = vmci_resource_handle(&entry->resource);
if ((result != VMCI_SUCCESS) ||
qp_list_find(&qp_guest_endpoints, entry->qp.handle)) {
pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
handle.context, handle.resource, result);
kfree(entry);
entry = NULL;
}
}
return entry;
}
/*
* Frees a qp_guest_endpoint structure.
*/
static void qp_guest_endpoint_destroy(struct qp_guest_endpoint *entry)
{
qp_free_ppn_set(&entry->ppn_set);
qp_cleanup_queue_mutex(entry->produce_q, entry->consume_q);
qp_free_queue(entry->produce_q, entry->qp.produce_size);
qp_free_queue(entry->consume_q, entry->qp.consume_size);
/* Unlink from resource hash table and free callback */
vmci_resource_remove(&entry->resource);
kfree(entry);
}
/*
* Helper to make a queue_pairAlloc hypercall when the driver is
* supporting a guest device.
*/
static int qp_alloc_hypercall(const struct qp_guest_endpoint *entry)
{
struct vmci_qp_alloc_msg *alloc_msg;
size_t msg_size;
size_t ppn_size;
int result;
if (!entry || entry->num_ppns <= 2)
return VMCI_ERROR_INVALID_ARGS;
ppn_size = vmci_use_ppn64() ? sizeof(u64) : sizeof(u32);
msg_size = sizeof(*alloc_msg) +
(size_t) entry->num_ppns * ppn_size;
alloc_msg = kmalloc(msg_size, GFP_KERNEL);
if (!alloc_msg)
return VMCI_ERROR_NO_MEM;
alloc_msg->hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
VMCI_QUEUEPAIR_ALLOC);
alloc_msg->hdr.src = VMCI_ANON_SRC_HANDLE;
alloc_msg->hdr.payload_size = msg_size - VMCI_DG_HEADERSIZE;
alloc_msg->handle = entry->qp.handle;
alloc_msg->peer = entry->qp.peer;
alloc_msg->flags = entry->qp.flags;
alloc_msg->produce_size = entry->qp.produce_size;
alloc_msg->consume_size = entry->qp.consume_size;
alloc_msg->num_ppns = entry->num_ppns;
result = qp_populate_ppn_set((u8 *)alloc_msg + sizeof(*alloc_msg),
&entry->ppn_set);
if (result == VMCI_SUCCESS)
result = vmci_send_datagram(&alloc_msg->hdr);
kfree(alloc_msg);
return result;
}
/*
* Helper to make a queue_pairDetach hypercall when the driver is
* supporting a guest device.
*/
static int qp_detatch_hypercall(struct vmci_handle handle)
{
struct vmci_qp_detach_msg detach_msg;
detach_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
VMCI_QUEUEPAIR_DETACH);
detach_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
detach_msg.hdr.payload_size = sizeof(handle);
detach_msg.handle = handle;
return vmci_send_datagram(&detach_msg.hdr);
}
/*
* Adds the given entry to the list. Assumes that the list is locked.