1
2
3
4
5
6
7
8#include <linux/vmw_vmci_defs.h>
9#include <linux/vmw_vmci_api.h>
10#include <linux/highmem.h>
11#include <linux/kernel.h>
12#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/mutex.h>
15#include <linux/pagemap.h>
16#include <linux/pci.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/uio.h>
20#include <linux/wait.h>
21#include <linux/vmalloc.h>
22#include <linux/skbuff.h>
23
24#include "vmci_handle_array.h"
25#include "vmci_queue_pair.h"
26#include "vmci_datagram.h"
27#include "vmci_resource.h"
28#include "vmci_context.h"
29#include "vmci_driver.h"
30#include "vmci_event.h"
31#include "vmci_route.h"
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
125struct vmci_queue_kern_if {
126 struct mutex __mutex;
127 struct mutex *mutex;
128 size_t num_pages;
129 bool host;
130 union {
131 struct {
132 dma_addr_t *pas;
133 void **vas;
134 } g;
135 struct {
136 struct page **page;
137 struct page **header_page;
138 } h;
139 } u;
140};
141
142
143
144
145struct vmci_qp {
146 struct vmci_handle handle;
147 struct vmci_queue *produce_q;
148 struct vmci_queue *consume_q;
149 u64 produce_q_size;
150 u64 consume_q_size;
151 u32 peer;
152 u32 flags;
153 u32 priv_flags;
154 bool guest_endpoint;
155 unsigned int blocked;
156 unsigned int generation;
157 wait_queue_head_t event;
158};
159
160enum qp_broker_state {
161 VMCIQPB_NEW,
162 VMCIQPB_CREATED_NO_MEM,
163 VMCIQPB_CREATED_MEM,
164 VMCIQPB_ATTACHED_NO_MEM,
165 VMCIQPB_ATTACHED_MEM,
166 VMCIQPB_SHUTDOWN_NO_MEM,
167 VMCIQPB_SHUTDOWN_MEM,
168 VMCIQPB_GONE
169};
170
171#define QPBROKERSTATE_HAS_MEM(_qpb) (_qpb->state == VMCIQPB_CREATED_MEM || \
172 _qpb->state == VMCIQPB_ATTACHED_MEM || \
173 _qpb->state == VMCIQPB_SHUTDOWN_MEM)
174
175
176
177
178
179
180
181
182
183
184struct qp_entry {
185 struct list_head list_item;
186 struct vmci_handle handle;
187 u32 peer;
188 u32 flags;
189 u64 produce_size;
190 u64 consume_size;
191 u32 ref_count;
192};
193
194struct qp_broker_entry {
195 struct vmci_resource resource;
196 struct qp_entry qp;
197 u32 create_id;
198 u32 attach_id;
199 enum qp_broker_state state;
200 bool require_trusted_attach;
201 bool created_by_trusted;
202 bool vmci_page_files;
203 struct vmci_queue *produce_q;
204 struct vmci_queue *consume_q;
205 struct vmci_queue_header saved_produce_q;
206 struct vmci_queue_header saved_consume_q;
207 vmci_event_release_cb wakeup_cb;
208 void *client_data;
209 void *local_mem;
210};
211
212struct qp_guest_endpoint {
213 struct vmci_resource resource;
214 struct qp_entry qp;
215 u64 num_ppns;
216 void *produce_q;
217 void *consume_q;
218 struct ppn_set ppn_set;
219};
220
221struct qp_list {
222 struct list_head head;
223 struct mutex mutex;
224};
225
226static struct qp_list qp_broker_list = {
227 .head = LIST_HEAD_INIT(qp_broker_list.head),
228 .mutex = __MUTEX_INITIALIZER(qp_broker_list.mutex),
229};
230
231static struct qp_list qp_guest_endpoints = {
232 .head = LIST_HEAD_INIT(qp_guest_endpoints.head),
233 .mutex = __MUTEX_INITIALIZER(qp_guest_endpoints.mutex),
234};
235
236#define INVALID_VMCI_GUEST_MEM_ID 0
237#define QPE_NUM_PAGES(_QPE) ((u32) \
238 (DIV_ROUND_UP(_QPE.produce_size, PAGE_SIZE) + \
239 DIV_ROUND_UP(_QPE.consume_size, PAGE_SIZE) + 2))
240
241
242
243
244
245
246static void qp_free_queue(void *q, u64 size)
247{
248 struct vmci_queue *queue = q;
249
250 if (queue) {
251 u64 i;
252
253
254 for (i = 0; i < DIV_ROUND_UP(size, PAGE_SIZE) + 1; i++) {
255 dma_free_coherent(&vmci_pdev->dev, PAGE_SIZE,
256 queue->kernel_if->u.g.vas[i],
257 queue->kernel_if->u.g.pas[i]);
258 }
259
260 vfree(queue);
261 }
262}
263
264
265
266
267
268
269static void *qp_alloc_queue(u64 size, u32 flags)
270{
271 u64 i;
272 struct vmci_queue *queue;
273 size_t pas_size;
274 size_t vas_size;
275 size_t queue_size = sizeof(*queue) + sizeof(*queue->kernel_if);
276 u64 num_pages;
277
278 if (size > SIZE_MAX - PAGE_SIZE)
279 return NULL;
280 num_pages = DIV_ROUND_UP(size, PAGE_SIZE) + 1;
281 if (num_pages >
282 (SIZE_MAX - queue_size) /
283 (sizeof(*queue->kernel_if->u.g.pas) +
284 sizeof(*queue->kernel_if->u.g.vas)))
285 return NULL;
286
287 pas_size = num_pages * sizeof(*queue->kernel_if->u.g.pas);
288 vas_size = num_pages * sizeof(*queue->kernel_if->u.g.vas);
289 queue_size += pas_size + vas_size;
290
291 queue = vmalloc(queue_size);
292 if (!queue)
293 return NULL;
294
295 queue->q_header = NULL;
296 queue->saved_header = NULL;
297 queue->kernel_if = (struct vmci_queue_kern_if *)(queue + 1);
298 queue->kernel_if->mutex = NULL;
299 queue->kernel_if->num_pages = num_pages;
300 queue->kernel_if->u.g.pas = (dma_addr_t *)(queue->kernel_if + 1);
301 queue->kernel_if->u.g.vas =
302 (void **)((u8 *)queue->kernel_if->u.g.pas + pas_size);
303 queue->kernel_if->host = false;
304
305 for (i = 0; i < num_pages; i++) {
306 queue->kernel_if->u.g.vas[i] =
307 dma_alloc_coherent(&vmci_pdev->dev, PAGE_SIZE,
308 &queue->kernel_if->u.g.pas[i],
309 GFP_KERNEL);
310 if (!queue->kernel_if->u.g.vas[i]) {
311
312 qp_free_queue(queue, i * PAGE_SIZE);
313 return NULL;
314 }
315 }
316
317
318 queue->q_header = queue->kernel_if->u.g.vas[0];
319
320 return queue;
321}
322
323
324
325
326
327
328
329static int qp_memcpy_to_queue_iter(struct vmci_queue *queue,
330 u64 queue_offset,
331 struct iov_iter *from,
332 size_t size)
333{
334 struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
335 size_t bytes_copied = 0;
336
337 while (bytes_copied < size) {
338 const u64 page_index =
339 (queue_offset + bytes_copied) / PAGE_SIZE;
340 const size_t page_offset =
341 (queue_offset + bytes_copied) & (PAGE_SIZE - 1);
342 void *va;
343 size_t to_copy;
344
345 if (kernel_if->host)
346 va = kmap(kernel_if->u.h.page[page_index]);
347 else
348 va = kernel_if->u.g.vas[page_index + 1];
349
350
351 if (size - bytes_copied > PAGE_SIZE - page_offset)
352
353 to_copy = PAGE_SIZE - page_offset;
354 else
355 to_copy = size - bytes_copied;
356
357 if (!copy_from_iter_full((u8 *)va + page_offset, to_copy,
358 from)) {
359 if (kernel_if->host)
360 kunmap(kernel_if->u.h.page[page_index]);
361 return VMCI_ERROR_INVALID_ARGS;
362 }
363 bytes_copied += to_copy;
364 if (kernel_if->host)
365 kunmap(kernel_if->u.h.page[page_index]);
366 }
367
368 return VMCI_SUCCESS;
369}
370
371
372
373
374
375
376
377static int qp_memcpy_from_queue_iter(struct iov_iter *to,
378 const struct vmci_queue *queue,
379 u64 queue_offset, size_t size)
380{
381 struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
382 size_t bytes_copied = 0;
383
384 while (bytes_copied < size) {
385 const u64 page_index =
386 (queue_offset + bytes_copied) / PAGE_SIZE;
387 const size_t page_offset =
388 (queue_offset + bytes_copied) & (PAGE_SIZE - 1);
389 void *va;
390 size_t to_copy;
391 int err;
392
393 if (kernel_if->host)
394 va = kmap(kernel_if->u.h.page[page_index]);
395 else
396 va = kernel_if->u.g.vas[page_index + 1];
397
398
399 if (size - bytes_copied > PAGE_SIZE - page_offset)
400
401 to_copy = PAGE_SIZE - page_offset;
402 else
403 to_copy = size - bytes_copied;
404
405 err = copy_to_iter((u8 *)va + page_offset, to_copy, to);
406 if (err != to_copy) {
407 if (kernel_if->host)
408 kunmap(kernel_if->u.h.page[page_index]);
409 return VMCI_ERROR_INVALID_ARGS;
410 }
411 bytes_copied += to_copy;
412 if (kernel_if->host)
413 kunmap(kernel_if->u.h.page[page_index]);
414 }
415
416 return VMCI_SUCCESS;
417}
418
419
420
421
422
423
424
425static int qp_alloc_ppn_set(void *prod_q,
426 u64 num_produce_pages,
427 void *cons_q,
428 u64 num_consume_pages, struct ppn_set *ppn_set)
429{
430 u64 *produce_ppns;
431 u64 *consume_ppns;
432 struct vmci_queue *produce_q = prod_q;
433 struct vmci_queue *consume_q = cons_q;
434 u64 i;
435
436 if (!produce_q || !num_produce_pages || !consume_q ||
437 !num_consume_pages || !ppn_set)
438 return VMCI_ERROR_INVALID_ARGS;
439
440 if (ppn_set->initialized)
441 return VMCI_ERROR_ALREADY_EXISTS;
442
443 produce_ppns =
444 kmalloc_array(num_produce_pages, sizeof(*produce_ppns),
445 GFP_KERNEL);
446 if (!produce_ppns)
447 return VMCI_ERROR_NO_MEM;
448
449 consume_ppns =
450 kmalloc_array(num_consume_pages, sizeof(*consume_ppns),
451 GFP_KERNEL);
452 if (!consume_ppns) {
453 kfree(produce_ppns);
454 return VMCI_ERROR_NO_MEM;
455 }
456
457 for (i = 0; i < num_produce_pages; i++)
458 produce_ppns[i] =
459 produce_q->kernel_if->u.g.pas[i] >> PAGE_SHIFT;
460
461 for (i = 0; i < num_consume_pages; i++)
462 consume_ppns[i] =
463 consume_q->kernel_if->u.g.pas[i] >> PAGE_SHIFT;
464
465 ppn_set->num_produce_pages = num_produce_pages;
466 ppn_set->num_consume_pages = num_consume_pages;
467 ppn_set->produce_ppns = produce_ppns;
468 ppn_set->consume_ppns = consume_ppns;
469 ppn_set->initialized = true;
470 return VMCI_SUCCESS;
471}
472
473
474
475
476static void qp_free_ppn_set(struct ppn_set *ppn_set)
477{
478 if (ppn_set->initialized) {
479
480 kfree(ppn_set->produce_ppns);
481 kfree(ppn_set->consume_ppns);
482 }
483 memset(ppn_set, 0, sizeof(*ppn_set));
484}
485
486
487
488
489
490static int qp_populate_ppn_set(u8 *call_buf, const struct ppn_set *ppn_set)
491{
492 if (vmci_use_ppn64()) {
493 memcpy(call_buf, ppn_set->produce_ppns,
494 ppn_set->num_produce_pages *
495 sizeof(*ppn_set->produce_ppns));
496 memcpy(call_buf +
497 ppn_set->num_produce_pages *
498 sizeof(*ppn_set->produce_ppns),
499 ppn_set->consume_ppns,
500 ppn_set->num_consume_pages *
501 sizeof(*ppn_set->consume_ppns));
502 } else {
503 int i;
504 u32 *ppns = (u32 *) call_buf;
505
506 for (i = 0; i < ppn_set->num_produce_pages; i++)
507 ppns[i] = (u32) ppn_set->produce_ppns[i];
508
509 ppns = &ppns[ppn_set->num_produce_pages];
510
511 for (i = 0; i < ppn_set->num_consume_pages; i++)
512 ppns[i] = (u32) ppn_set->consume_ppns[i];
513 }
514
515 return VMCI_SUCCESS;
516}
517
518
519
520
521
522
523
524static struct vmci_queue *qp_host_alloc_queue(u64 size)
525{
526 struct vmci_queue *queue;
527 size_t queue_page_size;
528 u64 num_pages;
529 const size_t queue_size = sizeof(*queue) + sizeof(*(queue->kernel_if));
530
531 if (size > SIZE_MAX - PAGE_SIZE)
532 return NULL;
533 num_pages = DIV_ROUND_UP(size, PAGE_SIZE) + 1;
534 if (num_pages > (SIZE_MAX - queue_size) /
535 sizeof(*queue->kernel_if->u.h.page))
536 return NULL;
537
538 queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
539
540 queue = kzalloc(queue_size + queue_page_size, GFP_KERNEL);
541 if (queue) {
542 queue->q_header = NULL;
543 queue->saved_header = NULL;
544 queue->kernel_if = (struct vmci_queue_kern_if *)(queue + 1);
545 queue->kernel_if->host = true;
546 queue->kernel_if->mutex = NULL;
547 queue->kernel_if->num_pages = num_pages;
548 queue->kernel_if->u.h.header_page =
549 (struct page **)((u8 *)queue + queue_size);
550 queue->kernel_if->u.h.page =
551 &queue->kernel_if->u.h.header_page[1];
552 }
553
554 return queue;
555}
556
557
558
559
560
561static void qp_host_free_queue(struct vmci_queue *queue, u64 queue_size)
562{
563 kfree(queue);
564}
565
566
567
568
569
570
571
572
573static void qp_init_queue_mutex(struct vmci_queue *produce_q,
574 struct vmci_queue *consume_q)
575{
576
577
578
579
580
581 if (produce_q->kernel_if->host) {
582 produce_q->kernel_if->mutex = &produce_q->kernel_if->__mutex;
583 consume_q->kernel_if->mutex = &produce_q->kernel_if->__mutex;
584 mutex_init(produce_q->kernel_if->mutex);
585 }
586}
587
588
589
590
591static void qp_cleanup_queue_mutex(struct vmci_queue *produce_q,
592 struct vmci_queue *consume_q)
593{
594 if (produce_q->kernel_if->host) {
595 produce_q->kernel_if->mutex = NULL;
596 consume_q->kernel_if->mutex = NULL;
597 }
598}
599
600
601
602
603
604
605static void qp_acquire_queue_mutex(struct vmci_queue *queue)
606{
607 if (queue->kernel_if->host)
608 mutex_lock(queue->kernel_if->mutex);
609}
610
611
612
613
614
615
616static void qp_release_queue_mutex(struct vmci_queue *queue)
617{
618 if (queue->kernel_if->host)
619 mutex_unlock(queue->kernel_if->mutex);
620}
621
622
623
624
625
626static void qp_release_pages(struct page **pages,
627 u64 num_pages, bool dirty)
628{
629 int i;
630
631 for (i = 0; i < num_pages; i++) {
632 if (dirty)
633 set_page_dirty(pages[i]);
634
635 put_page(pages[i]);
636 pages[i] = NULL;
637 }
638}
639
640
641
642
643
644
645static int qp_host_get_user_memory(u64 produce_uva,
646 u64 consume_uva,
647 struct vmci_queue *produce_q,
648 struct vmci_queue *consume_q)
649{
650 int retval;
651 int err = VMCI_SUCCESS;
652
653 retval = get_user_pages_fast((uintptr_t) produce_uva,
654 produce_q->kernel_if->num_pages,
655 FOLL_WRITE,
656 produce_q->kernel_if->u.h.header_page);
657 if (retval < (int)produce_q->kernel_if->num_pages) {
658 pr_debug("get_user_pages_fast(produce) failed (retval=%d)",
659 retval);
660 if (retval > 0)
661 qp_release_pages(produce_q->kernel_if->u.h.header_page,
662 retval, false);
663 err = VMCI_ERROR_NO_MEM;
664 goto out;
665 }
666
667 retval = get_user_pages_fast((uintptr_t) consume_uva,
668 consume_q->kernel_if->num_pages,
669 FOLL_WRITE,
670 consume_q->kernel_if->u.h.header_page);
671 if (retval < (int)consume_q->kernel_if->num_pages) {
672 pr_debug("get_user_pages_fast(consume) failed (retval=%d)",
673 retval);
674 if (retval > 0)
675 qp_release_pages(consume_q->kernel_if->u.h.header_page,
676 retval, false);
677 qp_release_pages(produce_q->kernel_if->u.h.header_page,
678 produce_q->kernel_if->num_pages, false);
679 err = VMCI_ERROR_NO_MEM;
680 }
681
682 out:
683 return err;
684}
685
686
687
688
689
690
691static int qp_host_register_user_memory(struct vmci_qp_page_store *page_store,
692 struct vmci_queue *produce_q,
693 struct vmci_queue *consume_q)
694{
695 u64 produce_uva;
696 u64 consume_uva;
697
698
699
700
701
702
703 produce_uva = page_store->pages;
704 consume_uva = page_store->pages +
705 produce_q->kernel_if->num_pages * PAGE_SIZE;
706 return qp_host_get_user_memory(produce_uva, consume_uva, produce_q,
707 consume_q);
708}
709
710
711
712
713
714
715static void qp_host_unregister_user_memory(struct vmci_queue *produce_q,
716 struct vmci_queue *consume_q)
717{
718 qp_release_pages(produce_q->kernel_if->u.h.header_page,
719 produce_q->kernel_if->num_pages, true);
720 memset(produce_q->kernel_if->u.h.header_page, 0,
721 sizeof(*produce_q->kernel_if->u.h.header_page) *
722 produce_q->kernel_if->num_pages);
723 qp_release_pages(consume_q->kernel_if->u.h.header_page,
724 consume_q->kernel_if->num_pages, true);
725 memset(consume_q->kernel_if->u.h.header_page, 0,
726 sizeof(*consume_q->kernel_if->u.h.header_page) *
727 consume_q->kernel_if->num_pages);
728}
729
730
731
732
733
734
735
736
737
738static int qp_host_map_queues(struct vmci_queue *produce_q,
739 struct vmci_queue *consume_q)
740{
741 int result;
742
743 if (!produce_q->q_header || !consume_q->q_header) {
744 struct page *headers[2];
745
746 if (produce_q->q_header != consume_q->q_header)
747 return VMCI_ERROR_QUEUEPAIR_MISMATCH;
748
749 if (produce_q->kernel_if->u.h.header_page == NULL ||
750 *produce_q->kernel_if->u.h.header_page == NULL)
751 return VMCI_ERROR_UNAVAILABLE;
752
753 headers[0] = *produce_q->kernel_if->u.h.header_page;
754 headers[1] = *consume_q->kernel_if->u.h.header_page;
755
756 produce_q->q_header = vmap(headers, 2, VM_MAP, PAGE_KERNEL);
757 if (produce_q->q_header != NULL) {
758 consume_q->q_header =
759 (struct vmci_queue_header *)((u8 *)
760 produce_q->q_header +
761 PAGE_SIZE);
762 result = VMCI_SUCCESS;
763 } else {
764 pr_warn("vmap failed\n");
765 result = VMCI_ERROR_NO_MEM;
766 }
767 } else {
768 result = VMCI_SUCCESS;
769 }
770
771 return result;
772}
773
774
775
776
777
778static int qp_host_unmap_queues(u32 gid,
779 struct vmci_queue *produce_q,
780 struct vmci_queue *consume_q)
781{
782 if (produce_q->q_header) {
783 if (produce_q->q_header < consume_q->q_header)
784 vunmap(produce_q->q_header);
785 else
786 vunmap(consume_q->q_header);
787
788 produce_q->q_header = NULL;
789 consume_q->q_header = NULL;
790 }
791
792 return VMCI_SUCCESS;
793}
794
795
796
797
798
799static struct qp_entry *qp_list_find(struct qp_list *qp_list,
800 struct vmci_handle handle)
801{
802 struct qp_entry *entry;
803
804 if (vmci_handle_is_invalid(handle))
805 return NULL;
806
807 list_for_each_entry(entry, &qp_list->head, list_item) {
808 if (vmci_handle_is_equal(entry->handle, handle))
809 return entry;
810 }
811
812 return NULL;
813}
814
815
816
817
818static struct qp_guest_endpoint *
819qp_guest_handle_to_entry(struct vmci_handle handle)
820{
821 struct qp_guest_endpoint *entry;
822 struct qp_entry *qp = qp_list_find(&qp_guest_endpoints, handle);
823
824 entry = qp ? container_of(
825 qp, struct qp_guest_endpoint, qp) : NULL;
826 return entry;
827}
828
829
830
831
832static struct qp_broker_entry *
833qp_broker_handle_to_entry(struct vmci_handle handle)
834{
835 struct qp_broker_entry *entry;
836 struct qp_entry *qp = qp_list_find(&qp_broker_list, handle);
837
838 entry = qp ? container_of(
839 qp, struct qp_broker_entry, qp) : NULL;
840 return entry;
841}
842
843
844
845
846
847static int qp_notify_peer_local(bool attach, struct vmci_handle handle)
848{
849 u32 context_id = vmci_get_context_id();
850 struct vmci_event_qp ev;
851
852 ev.msg.hdr.dst = vmci_make_handle(context_id, VMCI_EVENT_HANDLER);
853 ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
854 VMCI_CONTEXT_RESOURCE_ID);
855 ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
856 ev.msg.event_data.event =
857 attach ? VMCI_EVENT_QP_PEER_ATTACH : VMCI_EVENT_QP_PEER_DETACH;
858 ev.payload.peer_id = context_id;
859 ev.payload.handle = handle;
860
861 return vmci_event_dispatch(&ev.msg.hdr);
862}
863
864
865
866
867
868
869
870
871static struct qp_guest_endpoint *
872qp_guest_endpoint_create(struct vmci_handle handle,
873 u32 peer,
874 u32 flags,
875 u64 produce_size,
876 u64 consume_size,
877 void *produce_q,
878 void *consume_q)
879{
880 int result;
881 struct qp_guest_endpoint *entry;
882
883 const u64 num_ppns = DIV_ROUND_UP(produce_size, PAGE_SIZE) +
884 DIV_ROUND_UP(consume_size, PAGE_SIZE) + 2;
885
886 if (vmci_handle_is_invalid(handle)) {
887 u32 context_id = vmci_get_context_id();
888
889 handle = vmci_make_handle(context_id, VMCI_INVALID_ID);
890 }
891
892 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
893 if (entry) {
894 entry->qp.peer = peer;
895 entry->qp.flags = flags;
896 entry->qp.produce_size = produce_size;
897 entry->qp.consume_size = consume_size;
898 entry->qp.ref_count = 0;
899 entry->num_ppns = num_ppns;
900 entry->produce_q = produce_q;
901 entry->consume_q = consume_q;
902 INIT_LIST_HEAD(&entry->qp.list_item);
903
904
905 result = vmci_resource_add(&entry->resource,
906 VMCI_RESOURCE_TYPE_QPAIR_GUEST,
907 handle);
908 entry->qp.handle = vmci_resource_handle(&entry->resource);
909 if ((result != VMCI_SUCCESS) ||
910 qp_list_find(&qp_guest_endpoints, entry->qp.handle)) {
911 pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
912 handle.context, handle.resource, result);
913 kfree(entry);
914 entry = NULL;
915 }
916 }
917 return entry;
918}
919
920
921
922
923static void qp_guest_endpoint_destroy(struct qp_guest_endpoint *entry)
924{
925 qp_free_ppn_set(&entry->ppn_set);
926 qp_cleanup_queue_mutex(entry->produce_q, entry->consume_q);
927 qp_free_queue(entry->produce_q, entry->qp.produce_size);
928 qp_free_queue(entry->consume_q, entry->qp.consume_size);
929
930 vmci_resource_remove(&entry->resource);
931
932 kfree(entry);
933}
934
935
936
937
938
939static int qp_alloc_hypercall(const struct qp_guest_endpoint *entry)
940{
941 struct vmci_qp_alloc_msg *alloc_msg;
942 size_t msg_size;
943 size_t ppn_size;
944 int result;
945
946 if (!entry || entry->num_ppns <= 2)
947 return VMCI_ERROR_INVALID_ARGS;
948
949 ppn_size = vmci_use_ppn64() ? sizeof(u64) : sizeof(u32);
950 msg_size = sizeof(*alloc_msg) +
951 (size_t) entry->num_ppns * ppn_size;
952 alloc_msg = kmalloc(msg_size, GFP_KERNEL);
953 if (!alloc_msg)
954 return VMCI_ERROR_NO_MEM;
955
956 alloc_msg->hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
957 VMCI_QUEUEPAIR_ALLOC);
958 alloc_msg->hdr.src = VMCI_ANON_SRC_HANDLE;
959 alloc_msg->hdr.payload_size = msg_size - VMCI_DG_HEADERSIZE;
960 alloc_msg->handle = entry->qp.handle;
961 alloc_msg->peer = entry->qp.peer;
962 alloc_msg->flags = entry->qp.flags;
963 alloc_msg->produce_size = entry->qp.produce_size;
964 alloc_msg->consume_size = entry->qp.consume_size;
965 alloc_msg->num_ppns = entry->num_ppns;
966
967 result = qp_populate_ppn_set((u8 *)alloc_msg + sizeof(*alloc_msg),
968 &entry->ppn_set);
969 if (result == VMCI_SUCCESS)
970 result = vmci_send_datagram(&alloc_msg->hdr);
971
972 kfree(alloc_msg);
973
974 return result;
975}
976
977
978
979
980
981static int qp_detatch_hypercall(struct vmci_handle handle)
982{
983 struct vmci_qp_detach_msg detach_msg;
984
985 detach_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
986 VMCI_QUEUEPAIR_DETACH);
987 detach_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
988 detach_msg.hdr.payload_size = sizeof(handle);
989 detach_msg.handle = handle;
990
991 return vmci_send_datagram(&detach_msg.hdr);
992}
993
994
995
996
997static void qp_list_add_entry(struct qp_list *qp_list, struct qp_entry *entry)
998{
999 if (entry)
1000 list_add(&entry->list_item, &qp_list->head);
1001}
1002
1003
1004
1005
1006static void qp_list_remove_entry(struct qp_list *qp_list,
1007 struct qp_entry *entry)
1008{
1009 if (entry)
1010 list_del(&entry->list_item);
1011}
1012
1013
1014
1015
1016
1017static int qp_detatch_guest_work(struct vmci_handle handle)
1018{
1019 int result;
1020 struct qp_guest_endpoint *entry;
1021 u32 ref_count = ~0;
1022
1023 mutex_lock(&qp_guest_endpoints.mutex);
1024
1025 entry = qp_guest_handle_to_entry(handle);
1026 if (!entry) {
1027 mutex_unlock(&qp_guest_endpoints.mutex);
1028 return VMCI_ERROR_NOT_FOUND;
1029 }
1030
1031 if (entry->qp.flags & VMCI_QPFLAG_LOCAL) {
1032 result = VMCI_SUCCESS;
1033
1034 if (entry->qp.ref_count > 1) {
1035 result = qp_notify_peer_local(false, handle);
1036
1037
1038
1039
1040
1041
1042 }
1043 } else {
1044 result = qp_detatch_hypercall(handle);
1045 if (result < VMCI_SUCCESS) {
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056 mutex_unlock(&qp_guest_endpoints.mutex);
1057 return result;
1058 }
1059 }
1060
1061
1062
1063
1064
1065
1066 entry->qp.ref_count--;
1067 if (entry->qp.ref_count == 0)
1068 qp_list_remove_entry(&qp_guest_endpoints, &entry->qp);
1069
1070
1071 if (entry)
1072 ref_count = entry->qp.ref_count;
1073
1074 mutex_unlock(&qp_guest_endpoints.mutex);
1075
1076 if (ref_count == 0)
1077 qp_guest_endpoint_destroy(entry);
1078
1079 return result;
1080}
1081
1082
1083
1084
1085
1086
1087static int qp_alloc_guest_work(struct vmci_handle *handle,
1088 struct vmci_queue **produce_q,
1089 u64 produce_size,
1090 struct vmci_queue **consume_q,
1091 u64 consume_size,
1092 u32 peer,
1093 u32 flags,
1094 u32 priv_flags)
1095{
1096 const u64 num_produce_pages =
1097 DIV_ROUND_UP(produce_size, PAGE_SIZE) + 1;
1098 const u64 num_consume_pages =
1099 DIV_ROUND_UP(consume_size, PAGE_SIZE) + 1;
1100 void *my_produce_q = NULL;
1101 void *my_consume_q = NULL;
1102 int result;
1103 struct qp_guest_endpoint *queue_pair_entry = NULL;
1104
1105 if (priv_flags != VMCI_NO_PRIVILEGE_FLAGS)
1106 return VMCI_ERROR_NO_ACCESS;
1107
1108 mutex_lock(&qp_guest_endpoints.mutex);
1109
1110 queue_pair_entry = qp_guest_handle_to_entry(*handle);
1111 if (queue_pair_entry) {
1112 if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) {
1113
1114 if (queue_pair_entry->qp.ref_count > 1) {
1115 pr_devel("Error attempting to attach more than once\n");
1116 result = VMCI_ERROR_UNAVAILABLE;
1117 goto error_keep_entry;
1118 }
1119
1120 if (queue_pair_entry->qp.produce_size != consume_size ||
1121 queue_pair_entry->qp.consume_size !=
1122 produce_size ||
1123 queue_pair_entry->qp.flags !=
1124 (flags & ~VMCI_QPFLAG_ATTACH_ONLY)) {
1125 pr_devel("Error mismatched queue pair in local attach\n");
1126 result = VMCI_ERROR_QUEUEPAIR_MISMATCH;
1127 goto error_keep_entry;
1128 }
1129
1130
1131
1132
1133
1134
1135 result = qp_notify_peer_local(true, *handle);
1136 if (result < VMCI_SUCCESS)
1137 goto error_keep_entry;
1138
1139 my_produce_q = queue_pair_entry->consume_q;
1140 my_consume_q = queue_pair_entry->produce_q;
1141 goto out;
1142 }
1143
1144 result = VMCI_ERROR_ALREADY_EXISTS;
1145 goto error_keep_entry;
1146 }
1147
1148 my_produce_q = qp_alloc_queue(produce_size, flags);
1149 if (!my_produce_q) {
1150 pr_warn("Error allocating pages for produce queue\n");
1151 result = VMCI_ERROR_NO_MEM;
1152 goto error;
1153 }
1154
1155 my_consume_q = qp_alloc_queue(consume_size, flags);
1156 if (!my_consume_q) {
1157 pr_warn("Error allocating pages for consume queue\n");
1158 result = VMCI_ERROR_NO_MEM;
1159 goto error;
1160 }
1161
1162 queue_pair_entry = qp_guest_endpoint_create(*handle, peer, flags,
1163 produce_size, consume_size,
1164 my_produce_q, my_consume_q);
1165 if (!queue_pair_entry) {
1166 pr_warn("Error allocating memory in %s\n", __func__);
1167 result = VMCI_ERROR_NO_MEM;
1168 goto error;
1169 }
1170
1171 result = qp_alloc_ppn_set(my_produce_q, num_produce_pages, my_consume_q,
1172 num_consume_pages,
1173 &queue_pair_entry->ppn_set);
1174 if (result < VMCI_SUCCESS) {
1175 pr_warn("qp_alloc_ppn_set failed\n");
1176 goto error;
1177 }
1178
1179
1180
1181
1182
1183 if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) {
1184
1185 u32 context_id = vmci_get_context_id();
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196 if (queue_pair_entry->qp.handle.context != context_id ||
1197 (queue_pair_entry->qp.peer != VMCI_INVALID_ID &&
1198 queue_pair_entry->qp.peer != context_id)) {
1199 result = VMCI_ERROR_NO_ACCESS;
1200 goto error;
1201 }
1202
1203 if (queue_pair_entry->qp.flags & VMCI_QPFLAG_ATTACH_ONLY) {
1204 result = VMCI_ERROR_NOT_FOUND;
1205 goto error;
1206 }
1207 } else {
1208 result = qp_alloc_hypercall(queue_pair_entry);
1209 if (result < VMCI_SUCCESS) {
1210 pr_warn("qp_alloc_hypercall result = %d\n", result);
1211 goto error;
1212 }
1213 }
1214
1215 qp_init_queue_mutex((struct vmci_queue *)my_produce_q,
1216 (struct vmci_queue *)my_consume_q);
1217
1218 qp_list_add_entry(&qp_guest_endpoints, &queue_pair_entry->qp);
1219
1220 out:
1221 queue_pair_entry->qp.ref_count++;
1222 *handle = queue_pair_entry->qp.handle;
1223 *produce_q = (struct vmci_queue *)my_produce_q;
1224 *consume_q = (struct vmci_queue *)my_consume_q;
1225
1226
1227
1228
1229
1230
1231 if ((queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) &&
1232 queue_pair_entry->qp.ref_count == 1) {
1233 vmci_q_header_init((*produce_q)->q_header, *handle);
1234 vmci_q_header_init((*consume_q)->q_header, *handle);
1235 }
1236
1237 mutex_unlock(&qp_guest_endpoints.mutex);
1238
1239 return VMCI_SUCCESS;
1240
1241 error:
1242 mutex_unlock(&qp_guest_endpoints.mutex);
1243 if (queue_pair_entry) {
1244
1245 qp_guest_endpoint_destroy(queue_pair_entry);
1246 } else {
1247 qp_free_queue(my_produce_q, produce_size);
1248 qp_free_queue(my_consume_q, consume_size);
1249 }
1250 return result;
1251
1252 error_keep_entry:
1253
1254 mutex_unlock(&qp_guest_endpoints.mutex);
1255 return result;
1256}
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276static int qp_broker_create(struct vmci_handle handle,
1277 u32 peer,
1278 u32 flags,
1279 u32 priv_flags,
1280 u64 produce_size,
1281 u64 consume_size,
1282 struct vmci_qp_page_store *page_store,
1283 struct vmci_ctx *context,
1284 vmci_event_release_cb wakeup_cb,
1285 void *client_data, struct qp_broker_entry **ent)
1286{
1287 struct qp_broker_entry *entry = NULL;
1288 const u32 context_id = vmci_ctx_get_id(context);
1289 bool is_local = flags & VMCI_QPFLAG_LOCAL;
1290 int result;
1291 u64 guest_produce_size;
1292 u64 guest_consume_size;
1293
1294
1295 if (flags & VMCI_QPFLAG_ATTACH_ONLY)
1296 return VMCI_ERROR_NOT_FOUND;
1297
1298
1299
1300
1301
1302 if (handle.context != context_id && handle.context != peer)
1303 return VMCI_ERROR_NO_ACCESS;
1304
1305 if (VMCI_CONTEXT_IS_VM(context_id) && VMCI_CONTEXT_IS_VM(peer))
1306 return VMCI_ERROR_DST_UNREACHABLE;
1307
1308
1309
1310
1311
1312 if (is_local && peer != VMCI_INVALID_ID && context_id != peer)
1313 return VMCI_ERROR_NO_ACCESS;
1314
1315 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1316 if (!entry)
1317 return VMCI_ERROR_NO_MEM;
1318
1319 if (vmci_ctx_get_id(context) == VMCI_HOST_CONTEXT_ID && !is_local) {
1320
1321
1322
1323
1324
1325
1326
1327
1328 guest_produce_size = consume_size;
1329 guest_consume_size = produce_size;
1330 } else {
1331 guest_produce_size = produce_size;
1332 guest_consume_size = consume_size;
1333 }
1334
1335 entry->qp.handle = handle;
1336 entry->qp.peer = peer;
1337 entry->qp.flags = flags;
1338 entry->qp.produce_size = guest_produce_size;
1339 entry->qp.consume_size = guest_consume_size;
1340 entry->qp.ref_count = 1;
1341 entry->create_id = context_id;
1342 entry->attach_id = VMCI_INVALID_ID;
1343 entry->state = VMCIQPB_NEW;
1344 entry->require_trusted_attach =
1345 !!(context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED);
1346 entry->created_by_trusted =
1347 !!(priv_flags & VMCI_PRIVILEGE_FLAG_TRUSTED);
1348 entry->vmci_page_files = false;
1349 entry->wakeup_cb = wakeup_cb;
1350 entry->client_data = client_data;
1351 entry->produce_q = qp_host_alloc_queue(guest_produce_size);
1352 if (entry->produce_q == NULL) {
1353 result = VMCI_ERROR_NO_MEM;
1354 goto error;
1355 }
1356 entry->consume_q = qp_host_alloc_queue(guest_consume_size);
1357 if (entry->consume_q == NULL) {
1358 result = VMCI_ERROR_NO_MEM;
1359 goto error;
1360 }
1361
1362 qp_init_queue_mutex(entry->produce_q, entry->consume_q);
1363
1364 INIT_LIST_HEAD(&entry->qp.list_item);
1365
1366 if (is_local) {
1367 u8 *tmp;
1368
1369 entry->local_mem = kcalloc(QPE_NUM_PAGES(entry->qp),
1370 PAGE_SIZE, GFP_KERNEL);
1371 if (entry->local_mem == NULL) {
1372 result = VMCI_ERROR_NO_MEM;
1373 goto error;
1374 }
1375 entry->state = VMCIQPB_CREATED_MEM;
1376 entry->produce_q->q_header = entry->local_mem;
1377 tmp = (u8 *)entry->local_mem + PAGE_SIZE *
1378 (DIV_ROUND_UP(entry->qp.produce_size, PAGE_SIZE) + 1);
1379 entry->consume_q->q_header = (struct vmci_queue_header *)tmp;
1380 } else if (page_store) {
1381
1382
1383
1384
1385 result = qp_host_register_user_memory(page_store,
1386 entry->produce_q,
1387 entry->consume_q);
1388 if (result < VMCI_SUCCESS)
1389 goto error;
1390
1391 entry->state = VMCIQPB_CREATED_MEM;
1392 } else {
1393
1394
1395
1396
1397
1398
1399
1400 entry->state = VMCIQPB_CREATED_NO_MEM;
1401 }
1402
1403 qp_list_add_entry(&qp_broker_list, &entry->qp);
1404 if (ent != NULL)
1405 *ent = entry;
1406
1407
1408 result = vmci_resource_add(&entry->resource,
1409 VMCI_RESOURCE_TYPE_QPAIR_HOST,
1410 handle);
1411 if (result != VMCI_SUCCESS) {
1412 pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
1413 handle.context, handle.resource, result);
1414 goto error;
1415 }
1416
1417 entry->qp.handle = vmci_resource_handle(&entry->resource);
1418 if (is_local) {
1419 vmci_q_header_init(entry->produce_q->q_header,
1420 entry->qp.handle);
1421 vmci_q_header_init(entry->consume_q->q_header,
1422 entry->qp.handle);
1423 }
1424
1425 vmci_ctx_qp_create(context, entry->qp.handle);
1426
1427 return VMCI_SUCCESS;
1428
1429 error:
1430 if (entry != NULL) {
1431 qp_host_free_queue(entry->produce_q, guest_produce_size);
1432 qp_host_free_queue(entry->consume_q, guest_consume_size);
1433 kfree(entry);
1434 }
1435
1436 return result;
1437}
1438
1439
1440
1441
1442
1443
1444
1445static int qp_notify_peer(bool attach,
1446 struct vmci_handle handle,
1447 u32 my_id,
1448 u32 peer_id)
1449{
1450 int rv;
1451 struct vmci_event_qp ev;
1452
1453 if (vmci_handle_is_invalid(handle) || my_id == VMCI_INVALID_ID ||
1454 peer_id == VMCI_INVALID_ID)
1455 return VMCI_ERROR_INVALID_ARGS;
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465 ev.msg.hdr.dst = vmci_make_handle(peer_id, VMCI_EVENT_HANDLER);
1466 ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
1467 VMCI_CONTEXT_RESOURCE_ID);
1468 ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
1469 ev.msg.event_data.event = attach ?
1470 VMCI_EVENT_QP_PEER_ATTACH : VMCI_EVENT_QP_PEER_DETACH;
1471 ev.payload.handle = handle;
1472 ev.payload.peer_id = my_id;
1473
1474 rv = vmci_datagram_dispatch(VMCI_HYPERVISOR_CONTEXT_ID,
1475 &ev.msg.hdr, false);
1476 if (rv < VMCI_SUCCESS)
1477 pr_warn("Failed to enqueue queue_pair %s event datagram for context (ID=0x%x)\n",
1478 attach ? "ATTACH" : "DETACH", peer_id);
1479
1480 return rv;
1481}
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504static int qp_broker_attach(struct qp_broker_entry *entry,
1505 u32 peer,
1506 u32 flags,
1507 u32 priv_flags,
1508 u64 produce_size,
1509 u64 consume_size,
1510 struct vmci_qp_page_store *page_store,
1511 struct vmci_ctx *context,
1512 vmci_event_release_cb wakeup_cb,
1513 void *client_data,
1514 struct qp_broker_entry **ent)
1515{
1516 const u32 context_id = vmci_ctx_get_id(context);
1517 bool is_local = flags & VMCI_QPFLAG_LOCAL;
1518 int result;
1519
1520 if (entry->state != VMCIQPB_CREATED_NO_MEM &&
1521 entry->state != VMCIQPB_CREATED_MEM)
1522 return VMCI_ERROR_UNAVAILABLE;
1523
1524 if (is_local) {
1525 if (!(entry->qp.flags & VMCI_QPFLAG_LOCAL) ||
1526 context_id != entry->create_id) {
1527 return VMCI_ERROR_INVALID_ARGS;
1528 }
1529 } else if (context_id == entry->create_id ||
1530 context_id == entry->attach_id) {
1531 return VMCI_ERROR_ALREADY_EXISTS;
1532 }
1533
1534 if (VMCI_CONTEXT_IS_VM(context_id) &&
1535 VMCI_CONTEXT_IS_VM(entry->create_id))
1536 return VMCI_ERROR_DST_UNREACHABLE;
1537
1538
1539
1540
1541
1542 if ((context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
1543 !entry->created_by_trusted)
1544 return VMCI_ERROR_NO_ACCESS;
1545
1546
1547
1548
1549
1550 if (entry->require_trusted_attach &&
1551 (!(priv_flags & VMCI_PRIVILEGE_FLAG_TRUSTED)))
1552 return VMCI_ERROR_NO_ACCESS;
1553
1554
1555
1556
1557
1558 if (entry->qp.peer != VMCI_INVALID_ID && entry->qp.peer != context_id)
1559 return VMCI_ERROR_NO_ACCESS;
1560
1561 if (entry->create_id == VMCI_HOST_CONTEXT_ID) {
1562
1563
1564
1565
1566
1567 if (!vmci_ctx_supports_host_qp(context))
1568 return VMCI_ERROR_INVALID_RESOURCE;
1569
1570 } else if (context_id == VMCI_HOST_CONTEXT_ID) {
1571 struct vmci_ctx *create_context;
1572 bool supports_host_qp;
1573
1574
1575
1576
1577
1578
1579 create_context = vmci_ctx_get(entry->create_id);
1580 supports_host_qp = vmci_ctx_supports_host_qp(create_context);
1581 vmci_ctx_put(create_context);
1582
1583 if (!supports_host_qp)
1584 return VMCI_ERROR_INVALID_RESOURCE;
1585 }
1586
1587 if ((entry->qp.flags & ~VMCI_QP_ASYMM) != (flags & ~VMCI_QP_ASYMM_PEER))
1588 return VMCI_ERROR_QUEUEPAIR_MISMATCH;
1589
1590 if (context_id != VMCI_HOST_CONTEXT_ID) {
1591
1592
1593
1594
1595
1596
1597 if (entry->qp.produce_size != produce_size ||
1598 entry->qp.consume_size != consume_size) {
1599 return VMCI_ERROR_QUEUEPAIR_MISMATCH;
1600 }
1601 } else if (entry->qp.produce_size != consume_size ||
1602 entry->qp.consume_size != produce_size) {
1603 return VMCI_ERROR_QUEUEPAIR_MISMATCH;
1604 }
1605
1606 if (context_id != VMCI_HOST_CONTEXT_ID) {
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620 if (entry->state != VMCIQPB_CREATED_NO_MEM)
1621 return VMCI_ERROR_INVALID_ARGS;
1622
1623 if (page_store != NULL) {
1624
1625
1626
1627
1628
1629
1630
1631 result = qp_host_register_user_memory(page_store,
1632 entry->produce_q,
1633 entry->consume_q);
1634 if (result < VMCI_SUCCESS)
1635 return result;
1636
1637 entry->state = VMCIQPB_ATTACHED_MEM;
1638 } else {
1639 entry->state = VMCIQPB_ATTACHED_NO_MEM;
1640 }
1641 } else if (entry->state == VMCIQPB_CREATED_NO_MEM) {
1642
1643
1644
1645
1646
1647
1648
1649 return VMCI_ERROR_UNAVAILABLE;
1650 } else {
1651
1652 entry->state = VMCIQPB_ATTACHED_MEM;
1653 }
1654
1655 if (entry->state == VMCIQPB_ATTACHED_MEM) {
1656 result =
1657 qp_notify_peer(true, entry->qp.handle, context_id,
1658 entry->create_id);
1659 if (result < VMCI_SUCCESS)
1660 pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
1661 entry->create_id, entry->qp.handle.context,
1662 entry->qp.handle.resource);
1663 }
1664
1665 entry->attach_id = context_id;
1666 entry->qp.ref_count++;
1667 if (wakeup_cb) {
1668 entry->wakeup_cb = wakeup_cb;
1669 entry->client_data = client_data;
1670 }
1671
1672
1673
1674
1675
1676 if (!is_local)
1677 vmci_ctx_qp_create(context, entry->qp.handle);
1678
1679 if (ent != NULL)
1680 *ent = entry;
1681
1682 return VMCI_SUCCESS;
1683}
1684
1685
1686
1687
1688
1689static int qp_broker_alloc(struct vmci_handle handle,
1690 u32 peer,
1691 u32 flags,
1692 u32 priv_flags,
1693 u64 produce_size,
1694 u64 consume_size,
1695 struct vmci_qp_page_store *page_store,
1696 struct vmci_ctx *context,
1697 vmci_event_release_cb wakeup_cb,
1698 void *client_data,
1699 struct qp_broker_entry **ent,
1700 bool *swap)
1701{
1702 const u32 context_id = vmci_ctx_get_id(context);
1703 bool create;
1704 struct qp_broker_entry *entry = NULL;
1705 bool is_local = flags & VMCI_QPFLAG_LOCAL;
1706 int result;
1707
1708 if (vmci_handle_is_invalid(handle) ||
1709 (flags & ~VMCI_QP_ALL_FLAGS) || is_local ||
1710 !(produce_size || consume_size) ||
1711 !context || context_id == VMCI_INVALID_ID ||
1712 handle.context == VMCI_INVALID_ID) {
1713 return VMCI_ERROR_INVALID_ARGS;
1714 }
1715
1716 if (page_store && !VMCI_QP_PAGESTORE_IS_WELLFORMED(page_store))
1717 return VMCI_ERROR_INVALID_ARGS;
1718
1719
1720
1721
1722
1723
1724 mutex_lock(&qp_broker_list.mutex);
1725
1726 if (!is_local && vmci_ctx_qp_exists(context, handle)) {
1727 pr_devel("Context (ID=0x%x) already attached to queue pair (handle=0x%x:0x%x)\n",
1728 context_id, handle.context, handle.resource);
1729 mutex_unlock(&qp_broker_list.mutex);
1730 return VMCI_ERROR_ALREADY_EXISTS;
1731 }
1732
1733 if (handle.resource != VMCI_INVALID_ID)
1734 entry = qp_broker_handle_to_entry(handle);
1735
1736 if (!entry) {
1737 create = true;
1738 result =
1739 qp_broker_create(handle, peer, flags, priv_flags,
1740 produce_size, consume_size, page_store,
1741 context, wakeup_cb, client_data, ent);
1742 } else {
1743 create = false;
1744 result =
1745 qp_broker_attach(entry, peer, flags, priv_flags,
1746 produce_size, consume_size, page_store,
1747 context, wakeup_cb, client_data, ent);
1748 }
1749
1750 mutex_unlock(&qp_broker_list.mutex);
1751
1752 if (swap)
1753 *swap = (context_id == VMCI_HOST_CONTEXT_ID) &&
1754 !(create && is_local);
1755
1756 return result;
1757}
1758
1759
1760
1761
1762
1763static int qp_alloc_host_work(struct vmci_handle *handle,
1764 struct vmci_queue **produce_q,
1765 u64 produce_size,
1766 struct vmci_queue **consume_q,
1767 u64 consume_size,
1768 u32 peer,
1769 u32 flags,
1770 u32 priv_flags,
1771 vmci_event_release_cb wakeup_cb,
1772 void *client_data)
1773{
1774 struct vmci_handle new_handle;
1775 struct vmci_ctx *context;
1776 struct qp_broker_entry *entry;
1777 int result;
1778 bool swap;
1779
1780 if (vmci_handle_is_invalid(*handle)) {
1781 new_handle = vmci_make_handle(
1782 VMCI_HOST_CONTEXT_ID, VMCI_INVALID_ID);
1783 } else
1784 new_handle = *handle;
1785
1786 context = vmci_ctx_get(VMCI_HOST_CONTEXT_ID);
1787 entry = NULL;
1788 result =
1789 qp_broker_alloc(new_handle, peer, flags, priv_flags,
1790 produce_size, consume_size, NULL, context,
1791 wakeup_cb, client_data, &entry, &swap);
1792 if (result == VMCI_SUCCESS) {
1793 if (swap) {
1794
1795
1796
1797
1798
1799
1800 *produce_q = entry->consume_q;
1801 *consume_q = entry->produce_q;
1802 } else {
1803 *produce_q = entry->produce_q;
1804 *consume_q = entry->consume_q;
1805 }
1806
1807 *handle = vmci_resource_handle(&entry->resource);
1808 } else {
1809 *handle = VMCI_INVALID_HANDLE;
1810 pr_devel("queue pair broker failed to alloc (result=%d)\n",
1811 result);
1812 }
1813 vmci_ctx_put(context);
1814 return result;
1815}
1816
1817
1818
1819
1820
1821
1822int vmci_qp_alloc(struct vmci_handle *handle,
1823 struct vmci_queue **produce_q,
1824 u64 produce_size,
1825 struct vmci_queue **consume_q,
1826 u64 consume_size,
1827 u32 peer,
1828 u32 flags,
1829 u32 priv_flags,
1830 bool guest_endpoint,
1831 vmci_event_release_cb wakeup_cb,
1832 void *client_data)
1833{
1834 if (!handle || !produce_q || !consume_q ||
1835 (!produce_size && !consume_size) || (flags & ~VMCI_QP_ALL_FLAGS))
1836 return VMCI_ERROR_INVALID_ARGS;
1837
1838 if (guest_endpoint) {
1839 return qp_alloc_guest_work(handle, produce_q,
1840 produce_size, consume_q,
1841 consume_size, peer,
1842 flags, priv_flags);
1843 } else {
1844 return qp_alloc_host_work(handle, produce_q,
1845 produce_size, consume_q,
1846 consume_size, peer, flags,
1847 priv_flags, wakeup_cb, client_data);
1848 }
1849}
1850
1851
1852
1853
1854
1855static int qp_detatch_host_work(struct vmci_handle handle)
1856{
1857 int result;
1858 struct vmci_ctx *context;
1859
1860 context = vmci_ctx_get(VMCI_HOST_CONTEXT_ID);
1861
1862 result = vmci_qp_broker_detach(handle, context);
1863
1864 vmci_ctx_put(context);
1865 return result;
1866}
1867
1868
1869
1870
1871
1872static int qp_detatch(struct vmci_handle handle, bool guest_endpoint)
1873{
1874 if (vmci_handle_is_invalid(handle))
1875 return VMCI_ERROR_INVALID_ARGS;
1876
1877 if (guest_endpoint)
1878 return qp_detatch_guest_work(handle);
1879 else
1880 return qp_detatch_host_work(handle);
1881}
1882
1883
1884
1885
1886
1887static struct qp_entry *qp_list_get_head(struct qp_list *qp_list)
1888{
1889 if (!list_empty(&qp_list->head)) {
1890 struct qp_entry *entry =
1891 list_first_entry(&qp_list->head, struct qp_entry,
1892 list_item);
1893 return entry;
1894 }
1895
1896 return NULL;
1897}
1898
1899void vmci_qp_broker_exit(void)
1900{
1901 struct qp_entry *entry;
1902 struct qp_broker_entry *be;
1903
1904 mutex_lock(&qp_broker_list.mutex);
1905
1906 while ((entry = qp_list_get_head(&qp_broker_list))) {
1907 be = (struct qp_broker_entry *)entry;
1908
1909 qp_list_remove_entry(&qp_broker_list, entry);
1910 kfree(be);
1911 }
1912
1913 mutex_unlock(&qp_broker_list.mutex);
1914}
1915
1916
1917
1918
1919
1920
1921
1922
1923int vmci_qp_broker_alloc(struct vmci_handle handle,
1924 u32 peer,
1925 u32 flags,
1926 u32 priv_flags,
1927 u64 produce_size,
1928 u64 consume_size,
1929 struct vmci_qp_page_store *page_store,
1930 struct vmci_ctx *context)
1931{
1932 return qp_broker_alloc(handle, peer, flags, priv_flags,
1933 produce_size, consume_size,
1934 page_store, context, NULL, NULL, NULL, NULL);
1935}
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953int vmci_qp_broker_set_page_store(struct vmci_handle handle,
1954 u64 produce_uva,
1955 u64 consume_uva,
1956 struct vmci_ctx *context)
1957{
1958 struct qp_broker_entry *entry;
1959 int result;
1960 const u32 context_id = vmci_ctx_get_id(context);
1961
1962 if (vmci_handle_is_invalid(handle) || !context ||
1963 context_id == VMCI_INVALID_ID)
1964 return VMCI_ERROR_INVALID_ARGS;
1965
1966
1967
1968
1969
1970
1971 if (produce_uva == 0 || consume_uva == 0)
1972 return VMCI_ERROR_INVALID_ARGS;
1973
1974 mutex_lock(&qp_broker_list.mutex);
1975
1976 if (!vmci_ctx_qp_exists(context, handle)) {
1977 pr_warn("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
1978 context_id, handle.context, handle.resource);
1979 result = VMCI_ERROR_NOT_FOUND;
1980 goto out;
1981 }
1982
1983 entry = qp_broker_handle_to_entry(handle);
1984 if (!entry) {
1985 result = VMCI_ERROR_NOT_FOUND;
1986 goto out;
1987 }
1988
1989
1990
1991
1992
1993
1994
1995 if (entry->create_id != context_id &&
1996 (entry->create_id != VMCI_HOST_CONTEXT_ID ||
1997 entry->attach_id != context_id)) {
1998 result = VMCI_ERROR_QUEUEPAIR_NOTOWNER;
1999 goto out;
2000 }
2001
2002 if (entry->state != VMCIQPB_CREATED_NO_MEM &&
2003 entry->state != VMCIQPB_ATTACHED_NO_MEM) {
2004 result = VMCI_ERROR_UNAVAILABLE;
2005 goto out;
2006 }
2007
2008 result = qp_host_get_user_memory(produce_uva, consume_uva,
2009 entry->produce_q, entry->consume_q);
2010 if (result < VMCI_SUCCESS)
2011 goto out;
2012
2013 result = qp_host_map_queues(entry->produce_q, entry->consume_q);
2014 if (result < VMCI_SUCCESS) {
2015 qp_host_unregister_user_memory(entry->produce_q,
2016 entry->consume_q);
2017 goto out;
2018 }
2019
2020 if (entry->state == VMCIQPB_CREATED_NO_MEM)
2021 entry->state = VMCIQPB_CREATED_MEM;
2022 else
2023 entry->state = VMCIQPB_ATTACHED_MEM;
2024
2025 entry->vmci_page_files = true;
2026
2027 if (entry->state == VMCIQPB_ATTACHED_MEM) {
2028 result =
2029 qp_notify_peer(true, handle, context_id, entry->create_id);
2030 if (result < VMCI_SUCCESS) {
2031 pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
2032 entry->create_id, entry->qp.handle.context,
2033 entry->qp.handle.resource);
2034 }
2035 }
2036
2037 result = VMCI_SUCCESS;
2038 out:
2039 mutex_unlock(&qp_broker_list.mutex);
2040 return result;
2041}
2042
2043
2044
2045
2046
2047
2048static void qp_reset_saved_headers(struct qp_broker_entry *entry)
2049{
2050 entry->produce_q->saved_header = NULL;
2051 entry->consume_q->saved_header = NULL;
2052}
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072int vmci_qp_broker_detach(struct vmci_handle handle, struct vmci_ctx *context)
2073{
2074 struct qp_broker_entry *entry;
2075 const u32 context_id = vmci_ctx_get_id(context);
2076 u32 peer_id;
2077 bool is_local = false;
2078 int result;
2079
2080 if (vmci_handle_is_invalid(handle) || !context ||
2081 context_id == VMCI_INVALID_ID) {
2082 return VMCI_ERROR_INVALID_ARGS;
2083 }
2084
2085 mutex_lock(&qp_broker_list.mutex);
2086
2087 if (!vmci_ctx_qp_exists(context, handle)) {
2088 pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
2089 context_id, handle.context, handle.resource);
2090 result = VMCI_ERROR_NOT_FOUND;
2091 goto out;
2092 }
2093
2094 entry = qp_broker_handle_to_entry(handle);
2095 if (!entry) {
2096 pr_devel("Context (ID=0x%x) reports being attached to queue pair(handle=0x%x:0x%x) that isn't present in broker\n",
2097 context_id, handle.context, handle.resource);
2098 result = VMCI_ERROR_NOT_FOUND;
2099 goto out;
2100 }
2101
2102 if (context_id != entry->create_id && context_id != entry->attach_id) {
2103 result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
2104 goto out;
2105 }
2106
2107 if (context_id == entry->create_id) {
2108 peer_id = entry->attach_id;
2109 entry->create_id = VMCI_INVALID_ID;
2110 } else {
2111 peer_id = entry->create_id;
2112 entry->attach_id = VMCI_INVALID_ID;
2113 }
2114 entry->qp.ref_count--;
2115
2116 is_local = entry->qp.flags & VMCI_QPFLAG_LOCAL;
2117
2118 if (context_id != VMCI_HOST_CONTEXT_ID) {
2119 bool headers_mapped;
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129 qp_acquire_queue_mutex(entry->produce_q);
2130 headers_mapped = entry->produce_q->q_header ||
2131 entry->consume_q->q_header;
2132 if (QPBROKERSTATE_HAS_MEM(entry)) {
2133 result =
2134 qp_host_unmap_queues(INVALID_VMCI_GUEST_MEM_ID,
2135 entry->produce_q,
2136 entry->consume_q);
2137 if (result < VMCI_SUCCESS)
2138 pr_warn("Failed to unmap queue headers for queue pair (handle=0x%x:0x%x,result=%d)\n",
2139 handle.context, handle.resource,
2140 result);
2141
2142 qp_host_unregister_user_memory(entry->produce_q,
2143 entry->consume_q);
2144
2145 }
2146
2147 if (!headers_mapped)
2148 qp_reset_saved_headers(entry);
2149
2150 qp_release_queue_mutex(entry->produce_q);
2151
2152 if (!headers_mapped && entry->wakeup_cb)
2153 entry->wakeup_cb(entry->client_data);
2154
2155 } else {
2156 if (entry->wakeup_cb) {
2157 entry->wakeup_cb = NULL;
2158 entry->client_data = NULL;
2159 }
2160 }
2161
2162 if (entry->qp.ref_count == 0) {
2163 qp_list_remove_entry(&qp_broker_list, &entry->qp);
2164
2165 if (is_local)
2166 kfree(entry->local_mem);
2167
2168 qp_cleanup_queue_mutex(entry->produce_q, entry->consume_q);
2169 qp_host_free_queue(entry->produce_q, entry->qp.produce_size);
2170 qp_host_free_queue(entry->consume_q, entry->qp.consume_size);
2171
2172 vmci_resource_remove(&entry->resource);
2173
2174 kfree(entry);
2175
2176 vmci_ctx_qp_destroy(context, handle);
2177 } else {
2178 qp_notify_peer(false, handle, context_id, peer_id);
2179 if (context_id == VMCI_HOST_CONTEXT_ID &&
2180 QPBROKERSTATE_HAS_MEM(entry)) {
2181 entry->state = VMCIQPB_SHUTDOWN_MEM;
2182 } else {
2183 entry->state = VMCIQPB_SHUTDOWN_NO_MEM;
2184 }
2185
2186 if (!is_local)
2187 vmci_ctx_qp_destroy(context, handle);
2188
2189 }
2190 result = VMCI_SUCCESS;
2191 out:
2192 mutex_unlock(&qp_broker_list.mutex);
2193 return result;
2194}
2195
2196
2197
2198
2199
2200
2201
2202int vmci_qp_broker_map(struct vmci_handle handle,
2203 struct vmci_ctx *context,
2204 u64 guest_mem)
2205{
2206 struct qp_broker_entry *entry;
2207 const u32 context_id = vmci_ctx_get_id(context);
2208 int result;
2209
2210 if (vmci_handle_is_invalid(handle) || !context ||
2211 context_id == VMCI_INVALID_ID)
2212 return VMCI_ERROR_INVALID_ARGS;
2213
2214 mutex_lock(&qp_broker_list.mutex);
2215
2216 if (!vmci_ctx_qp_exists(context, handle)) {
2217 pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
2218 context_id, handle.context, handle.resource);
2219 result = VMCI_ERROR_NOT_FOUND;
2220 goto out;
2221 }
2222
2223 entry = qp_broker_handle_to_entry(handle);
2224 if (!entry) {
2225 pr_devel("Context (ID=0x%x) reports being attached to queue pair (handle=0x%x:0x%x) that isn't present in broker\n",
2226 context_id, handle.context, handle.resource);
2227 result = VMCI_ERROR_NOT_FOUND;
2228 goto out;
2229 }
2230
2231 if (context_id != entry->create_id && context_id != entry->attach_id) {
2232 result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
2233 goto out;
2234 }
2235
2236 result = VMCI_SUCCESS;
2237
2238 if (context_id != VMCI_HOST_CONTEXT_ID) {
2239 struct vmci_qp_page_store page_store;
2240
2241 page_store.pages = guest_mem;
2242 page_store.len = QPE_NUM_PAGES(entry->qp);
2243
2244 qp_acquire_queue_mutex(entry->produce_q);
2245 qp_reset_saved_headers(entry);
2246 result =
2247 qp_host_register_user_memory(&page_store,
2248 entry->produce_q,
2249 entry->consume_q);
2250 qp_release_queue_mutex(entry->produce_q);
2251 if (result == VMCI_SUCCESS) {
2252
2253
2254 entry->state++;
2255
2256 if (entry->wakeup_cb)
2257 entry->wakeup_cb(entry->client_data);
2258 }
2259 }
2260
2261 out:
2262 mutex_unlock(&qp_broker_list.mutex);
2263 return result;
2264}
2265
2266
2267
2268
2269
2270
2271
2272
2273static int qp_save_headers(struct qp_broker_entry *entry)
2274{
2275 int result;
2276
2277 if (entry->produce_q->saved_header != NULL &&
2278 entry->consume_q->saved_header != NULL) {
2279
2280
2281
2282
2283
2284
2285 return VMCI_SUCCESS;
2286 }
2287
2288 if (NULL == entry->produce_q->q_header ||
2289 NULL == entry->consume_q->q_header) {
2290 result = qp_host_map_queues(entry->produce_q, entry->consume_q);
2291 if (result < VMCI_SUCCESS)
2292 return result;
2293 }
2294
2295 memcpy(&entry->saved_produce_q, entry->produce_q->q_header,
2296 sizeof(entry->saved_produce_q));
2297 entry->produce_q->saved_header = &entry->saved_produce_q;
2298 memcpy(&entry->saved_consume_q, entry->consume_q->q_header,
2299 sizeof(entry->saved_consume_q));
2300 entry->consume_q->saved_header = &entry->saved_consume_q;
2301
2302 return VMCI_SUCCESS;
2303}
2304
2305
2306
2307
2308
2309
2310
2311int vmci_qp_broker_unmap(struct vmci_handle handle,
2312 struct vmci_ctx *context,
2313 u32 gid)
2314{
2315 struct qp_broker_entry *entry;
2316 const u32 context_id = vmci_ctx_get_id(context);
2317 int result;
2318
2319 if (vmci_handle_is_invalid(handle) || !context ||
2320 context_id == VMCI_INVALID_ID)
2321 return VMCI_ERROR_INVALID_ARGS;
2322
2323 mutex_lock(&qp_broker_list.mutex);
2324
2325 if (!vmci_ctx_qp_exists(context, handle)) {
2326 pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
2327 context_id, handle.context, handle.resource);
2328 result = VMCI_ERROR_NOT_FOUND;
2329 goto out;
2330 }
2331
2332 entry = qp_broker_handle_to_entry(handle);
2333 if (!entry) {
2334 pr_devel("Context (ID=0x%x) reports being attached to queue pair (handle=0x%x:0x%x) that isn't present in broker\n",
2335 context_id, handle.context, handle.resource);
2336 result = VMCI_ERROR_NOT_FOUND;
2337 goto out;
2338 }
2339
2340 if (context_id != entry->create_id && context_id != entry->attach_id) {
2341 result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
2342 goto out;
2343 }
2344
2345 if (context_id != VMCI_HOST_CONTEXT_ID) {
2346 qp_acquire_queue_mutex(entry->produce_q);
2347 result = qp_save_headers(entry);
2348 if (result < VMCI_SUCCESS)
2349 pr_warn("Failed to save queue headers for queue pair (handle=0x%x:0x%x,result=%d)\n",
2350 handle.context, handle.resource, result);
2351
2352 qp_host_unmap_queues(gid, entry->produce_q, entry->consume_q);
2353
2354
2355
2356
2357
2358
2359
2360
2361 qp_host_unregister_user_memory(entry->produce_q,
2362 entry->consume_q);
2363
2364
2365
2366
2367 entry->state--;
2368
2369 qp_release_queue_mutex(entry->produce_q);
2370 }
2371
2372 result = VMCI_SUCCESS;
2373
2374 out:
2375 mutex_unlock(&qp_broker_list.mutex);
2376 return result;
2377}
2378
2379
2380
2381
2382
2383
2384
2385void vmci_qp_guest_endpoints_exit(void)
2386{
2387 struct qp_entry *entry;
2388 struct qp_guest_endpoint *ep;
2389
2390 mutex_lock(&qp_guest_endpoints.mutex);
2391
2392 while ((entry = qp_list_get_head(&qp_guest_endpoints))) {
2393 ep = (struct qp_guest_endpoint *)entry;
2394
2395
2396 if (!(entry->flags & VMCI_QPFLAG_LOCAL))
2397 qp_detatch_hypercall(entry->handle);
2398
2399
2400 entry->ref_count = 0;
2401 qp_list_remove_entry(&qp_guest_endpoints, entry);
2402
2403 qp_guest_endpoint_destroy(ep);
2404 }
2405
2406 mutex_unlock(&qp_guest_endpoints.mutex);
2407}
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417static void qp_lock(const struct vmci_qp *qpair)
2418{
2419 qp_acquire_queue_mutex(qpair->produce_q);
2420}
2421
2422
2423
2424
2425
2426static void qp_unlock(const struct vmci_qp *qpair)
2427{
2428 qp_release_queue_mutex(qpair->produce_q);
2429}
2430
2431
2432
2433
2434
2435static int qp_map_queue_headers(struct vmci_queue *produce_q,
2436 struct vmci_queue *consume_q)
2437{
2438 int result;
2439
2440 if (NULL == produce_q->q_header || NULL == consume_q->q_header) {
2441 result = qp_host_map_queues(produce_q, consume_q);
2442 if (result < VMCI_SUCCESS)
2443 return (produce_q->saved_header &&
2444 consume_q->saved_header) ?
2445 VMCI_ERROR_QUEUEPAIR_NOT_READY :
2446 VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
2447 }
2448
2449 return VMCI_SUCCESS;
2450}
2451
2452
2453
2454
2455
2456
2457
2458static int qp_get_queue_headers(const struct vmci_qp *qpair,
2459 struct vmci_queue_header **produce_q_header,
2460 struct vmci_queue_header **consume_q_header)
2461{
2462 int result;
2463
2464 result = qp_map_queue_headers(qpair->produce_q, qpair->consume_q);
2465 if (result == VMCI_SUCCESS) {
2466 *produce_q_header = qpair->produce_q->q_header;
2467 *consume_q_header = qpair->consume_q->q_header;
2468 } else if (qpair->produce_q->saved_header &&
2469 qpair->consume_q->saved_header) {
2470 *produce_q_header = qpair->produce_q->saved_header;
2471 *consume_q_header = qpair->consume_q->saved_header;
2472 result = VMCI_SUCCESS;
2473 }
2474
2475 return result;
2476}
2477
2478
2479
2480
2481
2482
2483static int qp_wakeup_cb(void *client_data)
2484{
2485 struct vmci_qp *qpair = (struct vmci_qp *)client_data;
2486
2487 qp_lock(qpair);
2488 while (qpair->blocked > 0) {
2489 qpair->blocked--;
2490 qpair->generation++;
2491 wake_up(&qpair->event);
2492 }
2493 qp_unlock(qpair);
2494
2495 return VMCI_SUCCESS;
2496}
2497
2498
2499
2500
2501
2502
2503static bool qp_wait_for_ready_queue(struct vmci_qp *qpair)
2504{
2505 unsigned int generation;
2506
2507 qpair->blocked++;
2508 generation = qpair->generation;
2509 qp_unlock(qpair);
2510 wait_event(qpair->event, generation != qpair->generation);
2511 qp_lock(qpair);
2512
2513 return true;
2514}
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528static ssize_t qp_enqueue_locked(struct vmci_queue *produce_q,
2529 struct vmci_queue *consume_q,
2530 const u64 produce_q_size,
2531 struct iov_iter *from)
2532{
2533 s64 free_space;
2534 u64 tail;
2535 size_t buf_size = iov_iter_count(from);
2536 size_t written;
2537 ssize_t result;
2538
2539 result = qp_map_queue_headers(produce_q, consume_q);
2540 if (unlikely(result != VMCI_SUCCESS))
2541 return result;
2542
2543 free_space = vmci_q_header_free_space(produce_q->q_header,
2544 consume_q->q_header,
2545 produce_q_size);
2546 if (free_space == 0)
2547 return VMCI_ERROR_QUEUEPAIR_NOSPACE;
2548
2549 if (free_space < VMCI_SUCCESS)
2550 return (ssize_t) free_space;
2551
2552 written = (size_t) (free_space > buf_size ? buf_size : free_space);
2553 tail = vmci_q_header_producer_tail(produce_q->q_header);
2554 if (likely(tail + written < produce_q_size)) {
2555 result = qp_memcpy_to_queue_iter(produce_q, tail, from, written);
2556 } else {
2557
2558
2559 const size_t tmp = (size_t) (produce_q_size - tail);
2560
2561 result = qp_memcpy_to_queue_iter(produce_q, tail, from, tmp);
2562 if (result >= VMCI_SUCCESS)
2563 result = qp_memcpy_to_queue_iter(produce_q, 0, from,
2564 written - tmp);
2565 }
2566
2567 if (result < VMCI_SUCCESS)
2568 return result;
2569
2570 vmci_q_header_add_producer_tail(produce_q->q_header, written,
2571 produce_q_size);
2572 return written;
2573}
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588static ssize_t qp_dequeue_locked(struct vmci_queue *produce_q,
2589 struct vmci_queue *consume_q,
2590 const u64 consume_q_size,
2591 struct iov_iter *to,
2592 bool update_consumer)
2593{
2594 size_t buf_size = iov_iter_count(to);
2595 s64 buf_ready;
2596 u64 head;
2597 size_t read;
2598 ssize_t result;
2599
2600 result = qp_map_queue_headers(produce_q, consume_q);
2601 if (unlikely(result != VMCI_SUCCESS))
2602 return result;
2603
2604 buf_ready = vmci_q_header_buf_ready(consume_q->q_header,
2605 produce_q->q_header,
2606 consume_q_size);
2607 if (buf_ready == 0)
2608 return VMCI_ERROR_QUEUEPAIR_NODATA;
2609
2610 if (buf_ready < VMCI_SUCCESS)
2611 return (ssize_t) buf_ready;
2612
2613 read = (size_t) (buf_ready > buf_size ? buf_size : buf_ready);
2614 head = vmci_q_header_consumer_head(produce_q->q_header);
2615 if (likely(head + read < consume_q_size)) {
2616 result = qp_memcpy_from_queue_iter(to, consume_q, head, read);
2617 } else {
2618
2619
2620 const size_t tmp = (size_t) (consume_q_size - head);
2621
2622 result = qp_memcpy_from_queue_iter(to, consume_q, head, tmp);
2623 if (result >= VMCI_SUCCESS)
2624 result = qp_memcpy_from_queue_iter(to, consume_q, 0,
2625 read - tmp);
2626
2627 }
2628
2629 if (result < VMCI_SUCCESS)
2630 return result;
2631
2632 if (update_consumer)
2633 vmci_q_header_add_consumer_head(produce_q->q_header,
2634 read, consume_q_size);
2635
2636 return read;
2637}
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655int vmci_qpair_alloc(struct vmci_qp **qpair,
2656 struct vmci_handle *handle,
2657 u64 produce_qsize,
2658 u64 consume_qsize,
2659 u32 peer,
2660 u32 flags,
2661 u32 priv_flags)
2662{
2663 struct vmci_qp *my_qpair;
2664 int retval;
2665 struct vmci_handle src = VMCI_INVALID_HANDLE;
2666 struct vmci_handle dst = vmci_make_handle(peer, VMCI_INVALID_ID);
2667 enum vmci_route route;
2668 vmci_event_release_cb wakeup_cb;
2669 void *client_data;
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688 if (produce_qsize + consume_qsize < max(produce_qsize, consume_qsize) ||
2689 produce_qsize + consume_qsize > VMCI_MAX_GUEST_QP_MEMORY)
2690 return VMCI_ERROR_NO_RESOURCES;
2691
2692 retval = vmci_route(&src, &dst, false, &route);
2693 if (retval < VMCI_SUCCESS)
2694 route = vmci_guest_code_active() ?
2695 VMCI_ROUTE_AS_GUEST : VMCI_ROUTE_AS_HOST;
2696
2697 if (flags & (VMCI_QPFLAG_NONBLOCK | VMCI_QPFLAG_PINNED)) {
2698 pr_devel("NONBLOCK OR PINNED set");
2699 return VMCI_ERROR_INVALID_ARGS;
2700 }
2701
2702 my_qpair = kzalloc(sizeof(*my_qpair), GFP_KERNEL);
2703 if (!my_qpair)
2704 return VMCI_ERROR_NO_MEM;
2705
2706 my_qpair->produce_q_size = produce_qsize;
2707 my_qpair->consume_q_size = consume_qsize;
2708 my_qpair->peer = peer;
2709 my_qpair->flags = flags;
2710 my_qpair->priv_flags = priv_flags;
2711
2712 wakeup_cb = NULL;
2713 client_data = NULL;
2714
2715 if (VMCI_ROUTE_AS_HOST == route) {
2716 my_qpair->guest_endpoint = false;
2717 if (!(flags & VMCI_QPFLAG_LOCAL)) {
2718 my_qpair->blocked = 0;
2719 my_qpair->generation = 0;
2720 init_waitqueue_head(&my_qpair->event);
2721 wakeup_cb = qp_wakeup_cb;
2722 client_data = (void *)my_qpair;
2723 }
2724 } else {
2725 my_qpair->guest_endpoint = true;
2726 }
2727
2728 retval = vmci_qp_alloc(handle,
2729 &my_qpair->produce_q,
2730 my_qpair->produce_q_size,
2731 &my_qpair->consume_q,
2732 my_qpair->consume_q_size,
2733 my_qpair->peer,
2734 my_qpair->flags,
2735 my_qpair->priv_flags,
2736 my_qpair->guest_endpoint,
2737 wakeup_cb, client_data);
2738
2739 if (retval < VMCI_SUCCESS) {
2740 kfree(my_qpair);
2741 return retval;
2742 }
2743
2744 *qpair = my_qpair;
2745 my_qpair->handle = *handle;
2746
2747 return retval;
2748}
2749EXPORT_SYMBOL_GPL(vmci_qpair_alloc);
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759int vmci_qpair_detach(struct vmci_qp **qpair)
2760{
2761 int result;
2762 struct vmci_qp *old_qpair;
2763
2764 if (!qpair || !(*qpair))
2765 return VMCI_ERROR_INVALID_ARGS;
2766
2767 old_qpair = *qpair;
2768 result = qp_detatch(old_qpair->handle, old_qpair->guest_endpoint);
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780 memset(old_qpair, 0, sizeof(*old_qpair));
2781 old_qpair->handle = VMCI_INVALID_HANDLE;
2782 old_qpair->peer = VMCI_INVALID_ID;
2783 kfree(old_qpair);
2784 *qpair = NULL;
2785
2786 return result;
2787}
2788EXPORT_SYMBOL_GPL(vmci_qpair_detach);
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799int vmci_qpair_get_produce_indexes(const struct vmci_qp *qpair,
2800 u64 *producer_tail,
2801 u64 *consumer_head)
2802{
2803 struct vmci_queue_header *produce_q_header;
2804 struct vmci_queue_header *consume_q_header;
2805 int result;
2806
2807 if (!qpair)
2808 return VMCI_ERROR_INVALID_ARGS;
2809
2810 qp_lock(qpair);
2811 result =
2812 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2813 if (result == VMCI_SUCCESS)
2814 vmci_q_header_get_pointers(produce_q_header, consume_q_header,
2815 producer_tail, consumer_head);
2816 qp_unlock(qpair);
2817
2818 if (result == VMCI_SUCCESS &&
2819 ((producer_tail && *producer_tail >= qpair->produce_q_size) ||
2820 (consumer_head && *consumer_head >= qpair->produce_q_size)))
2821 return VMCI_ERROR_INVALID_SIZE;
2822
2823 return result;
2824}
2825EXPORT_SYMBOL_GPL(vmci_qpair_get_produce_indexes);
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836int vmci_qpair_get_consume_indexes(const struct vmci_qp *qpair,
2837 u64 *consumer_tail,
2838 u64 *producer_head)
2839{
2840 struct vmci_queue_header *produce_q_header;
2841 struct vmci_queue_header *consume_q_header;
2842 int result;
2843
2844 if (!qpair)
2845 return VMCI_ERROR_INVALID_ARGS;
2846
2847 qp_lock(qpair);
2848 result =
2849 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2850 if (result == VMCI_SUCCESS)
2851 vmci_q_header_get_pointers(consume_q_header, produce_q_header,
2852 consumer_tail, producer_head);
2853 qp_unlock(qpair);
2854
2855 if (result == VMCI_SUCCESS &&
2856 ((consumer_tail && *consumer_tail >= qpair->consume_q_size) ||
2857 (producer_head && *producer_head >= qpair->consume_q_size)))
2858 return VMCI_ERROR_INVALID_SIZE;
2859
2860 return result;
2861}
2862EXPORT_SYMBOL_GPL(vmci_qpair_get_consume_indexes);
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873s64 vmci_qpair_produce_free_space(const struct vmci_qp *qpair)
2874{
2875 struct vmci_queue_header *produce_q_header;
2876 struct vmci_queue_header *consume_q_header;
2877 s64 result;
2878
2879 if (!qpair)
2880 return VMCI_ERROR_INVALID_ARGS;
2881
2882 qp_lock(qpair);
2883 result =
2884 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2885 if (result == VMCI_SUCCESS)
2886 result = vmci_q_header_free_space(produce_q_header,
2887 consume_q_header,
2888 qpair->produce_q_size);
2889 else
2890 result = 0;
2891
2892 qp_unlock(qpair);
2893
2894 return result;
2895}
2896EXPORT_SYMBOL_GPL(vmci_qpair_produce_free_space);
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907s64 vmci_qpair_consume_free_space(const struct vmci_qp *qpair)
2908{
2909 struct vmci_queue_header *produce_q_header;
2910 struct vmci_queue_header *consume_q_header;
2911 s64 result;
2912
2913 if (!qpair)
2914 return VMCI_ERROR_INVALID_ARGS;
2915
2916 qp_lock(qpair);
2917 result =
2918 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2919 if (result == VMCI_SUCCESS)
2920 result = vmci_q_header_free_space(consume_q_header,
2921 produce_q_header,
2922 qpair->consume_q_size);
2923 else
2924 result = 0;
2925
2926 qp_unlock(qpair);
2927
2928 return result;
2929}
2930EXPORT_SYMBOL_GPL(vmci_qpair_consume_free_space);
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942s64 vmci_qpair_produce_buf_ready(const struct vmci_qp *qpair)
2943{
2944 struct vmci_queue_header *produce_q_header;
2945 struct vmci_queue_header *consume_q_header;
2946 s64 result;
2947
2948 if (!qpair)
2949 return VMCI_ERROR_INVALID_ARGS;
2950
2951 qp_lock(qpair);
2952 result =
2953 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2954 if (result == VMCI_SUCCESS)
2955 result = vmci_q_header_buf_ready(produce_q_header,
2956 consume_q_header,
2957 qpair->produce_q_size);
2958 else
2959 result = 0;
2960
2961 qp_unlock(qpair);
2962
2963 return result;
2964}
2965EXPORT_SYMBOL_GPL(vmci_qpair_produce_buf_ready);
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977s64 vmci_qpair_consume_buf_ready(const struct vmci_qp *qpair)
2978{
2979 struct vmci_queue_header *produce_q_header;
2980 struct vmci_queue_header *consume_q_header;
2981 s64 result;
2982
2983 if (!qpair)
2984 return VMCI_ERROR_INVALID_ARGS;
2985
2986 qp_lock(qpair);
2987 result =
2988 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2989 if (result == VMCI_SUCCESS)
2990 result = vmci_q_header_buf_ready(consume_q_header,
2991 produce_q_header,
2992 qpair->consume_q_size);
2993 else
2994 result = 0;
2995
2996 qp_unlock(qpair);
2997
2998 return result;
2999}
3000EXPORT_SYMBOL_GPL(vmci_qpair_consume_buf_ready);
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012ssize_t vmci_qpair_enqueue(struct vmci_qp *qpair,
3013 const void *buf,
3014 size_t buf_size,
3015 int buf_type)
3016{
3017 ssize_t result;
3018 struct iov_iter from;
3019 struct kvec v = {.iov_base = (void *)buf, .iov_len = buf_size};
3020
3021 if (!qpair || !buf)
3022 return VMCI_ERROR_INVALID_ARGS;
3023
3024 iov_iter_kvec(&from, WRITE, &v, 1, buf_size);
3025
3026 qp_lock(qpair);
3027
3028 do {
3029 result = qp_enqueue_locked(qpair->produce_q,
3030 qpair->consume_q,
3031 qpair->produce_q_size,
3032 &from);
3033
3034 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3035 !qp_wait_for_ready_queue(qpair))
3036 result = VMCI_ERROR_WOULD_BLOCK;
3037
3038 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3039
3040 qp_unlock(qpair);
3041
3042 return result;
3043}
3044EXPORT_SYMBOL_GPL(vmci_qpair_enqueue);
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056ssize_t vmci_qpair_dequeue(struct vmci_qp *qpair,
3057 void *buf,
3058 size_t buf_size,
3059 int buf_type)
3060{
3061 ssize_t result;
3062 struct iov_iter to;
3063 struct kvec v = {.iov_base = buf, .iov_len = buf_size};
3064
3065 if (!qpair || !buf)
3066 return VMCI_ERROR_INVALID_ARGS;
3067
3068 iov_iter_kvec(&to, READ, &v, 1, buf_size);
3069
3070 qp_lock(qpair);
3071
3072 do {
3073 result = qp_dequeue_locked(qpair->produce_q,
3074 qpair->consume_q,
3075 qpair->consume_q_size,
3076 &to, true);
3077
3078 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3079 !qp_wait_for_ready_queue(qpair))
3080 result = VMCI_ERROR_WOULD_BLOCK;
3081
3082 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3083
3084 qp_unlock(qpair);
3085
3086 return result;
3087}
3088EXPORT_SYMBOL_GPL(vmci_qpair_dequeue);
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101ssize_t vmci_qpair_peek(struct vmci_qp *qpair,
3102 void *buf,
3103 size_t buf_size,
3104 int buf_type)
3105{
3106 struct iov_iter to;
3107 struct kvec v = {.iov_base = buf, .iov_len = buf_size};
3108 ssize_t result;
3109
3110 if (!qpair || !buf)
3111 return VMCI_ERROR_INVALID_ARGS;
3112
3113 iov_iter_kvec(&to, READ, &v, 1, buf_size);
3114
3115 qp_lock(qpair);
3116
3117 do {
3118 result = qp_dequeue_locked(qpair->produce_q,
3119 qpair->consume_q,
3120 qpair->consume_q_size,
3121 &to, false);
3122
3123 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3124 !qp_wait_for_ready_queue(qpair))
3125 result = VMCI_ERROR_WOULD_BLOCK;
3126
3127 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3128
3129 qp_unlock(qpair);
3130
3131 return result;
3132}
3133EXPORT_SYMBOL_GPL(vmci_qpair_peek);
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146ssize_t vmci_qpair_enquev(struct vmci_qp *qpair,
3147 struct msghdr *msg,
3148 size_t iov_size,
3149 int buf_type)
3150{
3151 ssize_t result;
3152
3153 if (!qpair)
3154 return VMCI_ERROR_INVALID_ARGS;
3155
3156 qp_lock(qpair);
3157
3158 do {
3159 result = qp_enqueue_locked(qpair->produce_q,
3160 qpair->consume_q,
3161 qpair->produce_q_size,
3162 &msg->msg_iter);
3163
3164 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3165 !qp_wait_for_ready_queue(qpair))
3166 result = VMCI_ERROR_WOULD_BLOCK;
3167
3168 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3169
3170 qp_unlock(qpair);
3171
3172 return result;
3173}
3174EXPORT_SYMBOL_GPL(vmci_qpair_enquev);
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187ssize_t vmci_qpair_dequev(struct vmci_qp *qpair,
3188 struct msghdr *msg,
3189 size_t iov_size,
3190 int buf_type)
3191{
3192 ssize_t result;
3193
3194 if (!qpair)
3195 return VMCI_ERROR_INVALID_ARGS;
3196
3197 qp_lock(qpair);
3198
3199 do {
3200 result = qp_dequeue_locked(qpair->produce_q,
3201 qpair->consume_q,
3202 qpair->consume_q_size,
3203 &msg->msg_iter, true);
3204
3205 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3206 !qp_wait_for_ready_queue(qpair))
3207 result = VMCI_ERROR_WOULD_BLOCK;
3208
3209 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3210
3211 qp_unlock(qpair);
3212
3213 return result;
3214}
3215EXPORT_SYMBOL_GPL(vmci_qpair_dequev);
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229ssize_t vmci_qpair_peekv(struct vmci_qp *qpair,
3230 struct msghdr *msg,
3231 size_t iov_size,
3232 int buf_type)
3233{
3234 ssize_t result;
3235
3236 if (!qpair)
3237 return VMCI_ERROR_INVALID_ARGS;
3238
3239 qp_lock(qpair);
3240
3241 do {
3242 result = qp_dequeue_locked(qpair->produce_q,
3243 qpair->consume_q,
3244 qpair->consume_q_size,
3245 &msg->msg_iter, false);
3246
3247 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3248 !qp_wait_for_ready_queue(qpair))
3249 result = VMCI_ERROR_WOULD_BLOCK;
3250
3251 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3252
3253 qp_unlock(qpair);
3254 return result;
3255}
3256EXPORT_SYMBOL_GPL(vmci_qpair_peekv);
3257