1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2012 VMware, Inc. All rights reserved.
8 #include <linux/vmw_vmci_defs.h>
9 #include <linux/vmw_vmci_api.h>
10 #include <linux/highmem.h>
11 #include <linux/kernel.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>
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"
34 * In the following, we will distinguish between two kinds of VMX processes -
35 * the ones with versions lower than VMCI_VERSION_NOVMVM that use specialized
36 * VMCI page files in the VMX and supporting VM to VM communication and the
37 * newer ones that use the guest memory directly. We will in the following
38 * refer to the older VMX versions as old-style VMX'en, and the newer ones as
41 * The state transition datagram is as follows (the VMCIQPB_ prefix has been
42 * removed for readability) - see below for more details on the transtions:
44 * -------------- NEW -------------
47 * CREATED_NO_MEM <-----------------> CREATED_MEM
49 * | o-----------------------o |
52 * ATTACHED_NO_MEM <----------------> ATTACHED_MEM
54 * | o----------------------o |
57 * SHUTDOWN_NO_MEM <----------------> SHUTDOWN_MEM
60 * -------------> gone <-------------
62 * In more detail. When a VMCI queue pair is first created, it will be in the
63 * VMCIQPB_NEW state. It will then move into one of the following states:
65 * - VMCIQPB_CREATED_NO_MEM: this state indicates that either:
67 * - the created was performed by a host endpoint, in which case there is
68 * no backing memory yet.
70 * - the create was initiated by an old-style VMX, that uses
71 * vmci_qp_broker_set_page_store to specify the UVAs of the queue pair at
72 * a later point in time. This state can be distinguished from the one
73 * above by the context ID of the creator. A host side is not allowed to
74 * attach until the page store has been set.
76 * - VMCIQPB_CREATED_MEM: this state is the result when the queue pair
77 * is created by a VMX using the queue pair device backend that
78 * sets the UVAs of the queue pair immediately and stores the
79 * information for later attachers. At this point, it is ready for
80 * the host side to attach to it.
82 * Once the queue pair is in one of the created states (with the exception of
83 * the case mentioned for older VMX'en above), it is possible to attach to the
84 * queue pair. Again we have two new states possible:
86 * - VMCIQPB_ATTACHED_MEM: this state can be reached through the following
89 * - from VMCIQPB_CREATED_NO_MEM when a new-style VMX allocates a queue
90 * pair, and attaches to a queue pair previously created by the host side.
92 * - from VMCIQPB_CREATED_MEM when the host side attaches to a queue pair
93 * already created by a guest.
95 * - from VMCIQPB_ATTACHED_NO_MEM, when an old-style VMX calls
96 * vmci_qp_broker_set_page_store (see below).
98 * - VMCIQPB_ATTACHED_NO_MEM: If the queue pair already was in the
99 * VMCIQPB_CREATED_NO_MEM due to a host side create, an old-style VMX will
100 * bring the queue pair into this state. Once vmci_qp_broker_set_page_store
101 * is called to register the user memory, the VMCIQPB_ATTACH_MEM state
104 * From the attached queue pair, the queue pair can enter the shutdown states
105 * when either side of the queue pair detaches. If the guest side detaches
106 * first, the queue pair will enter the VMCIQPB_SHUTDOWN_NO_MEM state, where
107 * the content of the queue pair will no longer be available. If the host
108 * side detaches first, the queue pair will either enter the
109 * VMCIQPB_SHUTDOWN_MEM, if the guest memory is currently mapped, or
110 * VMCIQPB_SHUTDOWN_NO_MEM, if the guest memory is not mapped
111 * (e.g., the host detaches while a guest is stunned).
113 * New-style VMX'en will also unmap guest memory, if the guest is
114 * quiesced, e.g., during a snapshot operation. In that case, the guest
115 * memory will no longer be available, and the queue pair will transition from
116 * *_MEM state to a *_NO_MEM state. The VMX may later map the memory once more,
117 * in which case the queue pair will transition from the *_NO_MEM state at that
118 * point back to the *_MEM state. Note that the *_NO_MEM state may have changed,
119 * since the peer may have either attached or detached in the meantime. The
120 * values are laid out such that ++ on a state will move from a *_NO_MEM to a
121 * *_MEM state, and vice versa.
124 /* The Kernel specific component of the struct vmci_queue structure. */
125 struct vmci_queue_kern_if {
126 struct mutex __mutex; /* Protects the queue. */
127 struct mutex *mutex; /* Shared by producer and consumer queues. */
128 size_t num_pages; /* Number of pages incl. header. */
129 bool host; /* Host or guest? */
134 } g; /* Used by the guest. */
137 struct page **header_page;
138 } h; /* Used by the host. */
143 * This structure is opaque to the clients.
146 struct vmci_handle handle;
147 struct vmci_queue *produce_q;
148 struct vmci_queue *consume_q;
155 unsigned int blocked;
156 unsigned int generation;
157 wait_queue_head_t event;
160 enum qp_broker_state {
162 VMCIQPB_CREATED_NO_MEM,
164 VMCIQPB_ATTACHED_NO_MEM,
165 VMCIQPB_ATTACHED_MEM,
166 VMCIQPB_SHUTDOWN_NO_MEM,
167 VMCIQPB_SHUTDOWN_MEM,
171 #define QPBROKERSTATE_HAS_MEM(_qpb) (_qpb->state == VMCIQPB_CREATED_MEM || \
172 _qpb->state == VMCIQPB_ATTACHED_MEM || \
173 _qpb->state == VMCIQPB_SHUTDOWN_MEM)
176 * In the queue pair broker, we always use the guest point of view for
177 * the produce and consume queue values and references, e.g., the
178 * produce queue size stored is the guests produce queue size. The
179 * host endpoint will need to swap these around. The only exception is
180 * the local queue pairs on the host, in which case the host endpoint
181 * that creates the queue pair will have the right orientation, and
182 * the attaching host endpoint will need to swap.
185 struct list_head list_item;
186 struct vmci_handle handle;
194 struct qp_broker_entry {
195 struct vmci_resource resource;
199 enum qp_broker_state state;
200 bool require_trusted_attach;
201 bool created_by_trusted;
202 bool vmci_page_files; /* Created by VMX using 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;
209 void *local_mem; /* Kernel memory for local queue pair */
212 struct qp_guest_endpoint {
213 struct vmci_resource resource;
218 struct ppn_set ppn_set;
222 struct list_head head;
223 struct mutex mutex; /* Protect queue list. */
226 static struct qp_list qp_broker_list = {
227 .head = LIST_HEAD_INIT(qp_broker_list.head),
228 .mutex = __MUTEX_INITIALIZER(qp_broker_list.mutex),
231 static struct qp_list qp_guest_endpoints = {
232 .head = LIST_HEAD_INIT(qp_guest_endpoints.head),
233 .mutex = __MUTEX_INITIALIZER(qp_guest_endpoints.mutex),
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))
243 * Frees kernel VA space for a given queue and its queue header, and
244 * frees physical data pages.
246 static void qp_free_queue(void *q, u64 size)
248 struct vmci_queue *queue = q;
253 /* Given size does not include header, so add in a page here. */
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]);
265 * Allocates kernel queue pages of specified size with IOMMU mappings,
266 * plus space for the queue structure/kernel interface and the queue
269 static void *qp_alloc_queue(u64 size, u32 flags)
272 struct vmci_queue *queue;
275 size_t queue_size = sizeof(*queue) + sizeof(*queue->kernel_if);
278 if (size > SIZE_MAX - PAGE_SIZE)
280 num_pages = DIV_ROUND_UP(size, PAGE_SIZE) + 1;
282 (SIZE_MAX - queue_size) /
283 (sizeof(*queue->kernel_if->u.g.pas) +
284 sizeof(*queue->kernel_if->u.g.vas)))
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;
291 queue = vmalloc(queue_size);
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;
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],
310 if (!queue->kernel_if->u.g.vas[i]) {
311 /* Size excl. the header. */
312 qp_free_queue(queue, i * PAGE_SIZE);
317 /* Queue header is the first page. */
318 queue->q_header = queue->kernel_if->u.g.vas[0];
324 * Copies from a given buffer or iovector to a VMCI Queue. Uses
325 * kmap()/kunmap() to dynamically map/unmap required portions of the queue
326 * by traversing the offset -> page translation structure for the queue.
327 * Assumes that offset + size does not wrap around in the queue.
329 static int qp_memcpy_to_queue_iter(struct vmci_queue *queue,
331 struct iov_iter *from,
334 struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
335 size_t bytes_copied = 0;
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);
346 va = kmap(kernel_if->u.h.page[page_index]);
348 va = kernel_if->u.g.vas[page_index + 1];
351 if (size - bytes_copied > PAGE_SIZE - page_offset)
352 /* Enough payload to fill up from this page. */
353 to_copy = PAGE_SIZE - page_offset;
355 to_copy = size - bytes_copied;
357 if (!copy_from_iter_full((u8 *)va + page_offset, to_copy,
360 kunmap(kernel_if->u.h.page[page_index]);
361 return VMCI_ERROR_INVALID_ARGS;
363 bytes_copied += to_copy;
365 kunmap(kernel_if->u.h.page[page_index]);
372 * Copies to a given buffer or iovector from a VMCI Queue. Uses
373 * kmap()/kunmap() to dynamically map/unmap required portions of the queue
374 * by traversing the offset -> page translation structure for the queue.
375 * Assumes that offset + size does not wrap around in the queue.
377 static int qp_memcpy_from_queue_iter(struct iov_iter *to,
378 const struct vmci_queue *queue,
379 u64 queue_offset, size_t size)
381 struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
382 size_t bytes_copied = 0;
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);
394 va = kmap(kernel_if->u.h.page[page_index]);
396 va = kernel_if->u.g.vas[page_index + 1];
399 if (size - bytes_copied > PAGE_SIZE - page_offset)
400 /* Enough payload to fill up this page. */
401 to_copy = PAGE_SIZE - page_offset;
403 to_copy = size - bytes_copied;
405 err = copy_to_iter((u8 *)va + page_offset, to_copy, to);
406 if (err != to_copy) {
408 kunmap(kernel_if->u.h.page[page_index]);
409 return VMCI_ERROR_INVALID_ARGS;
411 bytes_copied += to_copy;
413 kunmap(kernel_if->u.h.page[page_index]);
420 * Allocates two list of PPNs --- one for the pages in the produce queue,
421 * and the other for the pages in the consume queue. Intializes the list
422 * of PPNs with the page frame numbers of the KVA for the two queues (and
423 * the queue headers).
425 static int qp_alloc_ppn_set(void *prod_q,
426 u64 num_produce_pages,
428 u64 num_consume_pages, struct ppn_set *ppn_set)
432 struct vmci_queue *produce_q = prod_q;
433 struct vmci_queue *consume_q = cons_q;
436 if (!produce_q || !num_produce_pages || !consume_q ||
437 !num_consume_pages || !ppn_set)
438 return VMCI_ERROR_INVALID_ARGS;
440 if (ppn_set->initialized)
441 return VMCI_ERROR_ALREADY_EXISTS;
444 kmalloc_array(num_produce_pages, sizeof(*produce_ppns),
447 return VMCI_ERROR_NO_MEM;
450 kmalloc_array(num_consume_pages, sizeof(*consume_ppns),
454 return VMCI_ERROR_NO_MEM;
457 for (i = 0; i < num_produce_pages; i++)
459 produce_q->kernel_if->u.g.pas[i] >> PAGE_SHIFT;
461 for (i = 0; i < num_consume_pages; i++)
463 consume_q->kernel_if->u.g.pas[i] >> PAGE_SHIFT;
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;
474 * Frees the two list of PPNs for a queue pair.
476 static void qp_free_ppn_set(struct ppn_set *ppn_set)
478 if (ppn_set->initialized) {
479 /* Do not call these functions on NULL inputs. */
480 kfree(ppn_set->produce_ppns);
481 kfree(ppn_set->consume_ppns);
483 memset(ppn_set, 0, sizeof(*ppn_set));
487 * Populates the list of PPNs in the hypercall structure with the PPNS
488 * of the produce queue and the consume queue.
490 static int qp_populate_ppn_set(u8 *call_buf, const struct ppn_set *ppn_set)
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));
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));
504 u32 *ppns = (u32 *) call_buf;
506 for (i = 0; i < ppn_set->num_produce_pages; i++)
507 ppns[i] = (u32) ppn_set->produce_ppns[i];
509 ppns = &ppns[ppn_set->num_produce_pages];
511 for (i = 0; i < ppn_set->num_consume_pages; i++)
512 ppns[i] = (u32) ppn_set->consume_ppns[i];
519 * Allocates kernel VA space of specified size plus space for the queue
520 * and kernel interface. This is different from the guest queue allocator,
521 * because we do not allocate our own queue header/data pages here but
522 * share those of the guest.
524 static struct vmci_queue *qp_host_alloc_queue(u64 size)
526 struct vmci_queue *queue;
527 size_t queue_page_size;
529 const size_t queue_size = sizeof(*queue) + sizeof(*(queue->kernel_if));
531 if (size > SIZE_MAX - PAGE_SIZE)
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))
538 queue_page_size = num_pages * sizeof(*queue->kernel_if->u.h.page);
540 if (queue_size + queue_page_size > KMALLOC_MAX_SIZE)
543 queue = kzalloc(queue_size + queue_page_size, GFP_KERNEL);
545 queue->q_header = NULL;
546 queue->saved_header = NULL;
547 queue->kernel_if = (struct vmci_queue_kern_if *)(queue + 1);
548 queue->kernel_if->host = true;
549 queue->kernel_if->mutex = NULL;
550 queue->kernel_if->num_pages = num_pages;
551 queue->kernel_if->u.h.header_page =
552 (struct page **)((u8 *)queue + queue_size);
553 queue->kernel_if->u.h.page =
554 &queue->kernel_if->u.h.header_page[1];
561 * Frees kernel memory for a given queue (header plus translation
564 static void qp_host_free_queue(struct vmci_queue *queue, u64 queue_size)
570 * Initialize the mutex for the pair of queues. This mutex is used to
571 * protect the q_header and the buffer from changing out from under any
572 * users of either queue. Of course, it's only any good if the mutexes
573 * are actually acquired. Queue structure must lie on non-paged memory
574 * or we cannot guarantee access to the mutex.
576 static void qp_init_queue_mutex(struct vmci_queue *produce_q,
577 struct vmci_queue *consume_q)
580 * Only the host queue has shared state - the guest queues do not
581 * need to synchronize access using a queue mutex.
584 if (produce_q->kernel_if->host) {
585 produce_q->kernel_if->mutex = &produce_q->kernel_if->__mutex;
586 consume_q->kernel_if->mutex = &produce_q->kernel_if->__mutex;
587 mutex_init(produce_q->kernel_if->mutex);
592 * Cleans up the mutex for the pair of queues.
594 static void qp_cleanup_queue_mutex(struct vmci_queue *produce_q,
595 struct vmci_queue *consume_q)
597 if (produce_q->kernel_if->host) {
598 produce_q->kernel_if->mutex = NULL;
599 consume_q->kernel_if->mutex = NULL;
604 * Acquire the mutex for the queue. Note that the produce_q and
605 * the consume_q share a mutex. So, only one of the two need to
606 * be passed in to this routine. Either will work just fine.
608 static void qp_acquire_queue_mutex(struct vmci_queue *queue)
610 if (queue->kernel_if->host)
611 mutex_lock(queue->kernel_if->mutex);
615 * Release the mutex for the queue. Note that the produce_q and
616 * the consume_q share a mutex. So, only one of the two need to
617 * be passed in to this routine. Either will work just fine.
619 static void qp_release_queue_mutex(struct vmci_queue *queue)
621 if (queue->kernel_if->host)
622 mutex_unlock(queue->kernel_if->mutex);
626 * Helper function to release pages in the PageStoreAttachInfo
627 * previously obtained using get_user_pages.
629 static void qp_release_pages(struct page **pages,
630 u64 num_pages, bool dirty)
634 for (i = 0; i < num_pages; i++) {
636 set_page_dirty_lock(pages[i]);
644 * Lock the user pages referenced by the {produce,consume}Buffer
645 * struct into memory and populate the {produce,consume}Pages
646 * arrays in the attach structure with them.
648 static int qp_host_get_user_memory(u64 produce_uva,
650 struct vmci_queue *produce_q,
651 struct vmci_queue *consume_q)
654 int err = VMCI_SUCCESS;
656 retval = get_user_pages_fast((uintptr_t) produce_uva,
657 produce_q->kernel_if->num_pages,
659 produce_q->kernel_if->u.h.header_page);
660 if (retval < (int)produce_q->kernel_if->num_pages) {
661 pr_debug("get_user_pages_fast(produce) failed (retval=%d)",
664 qp_release_pages(produce_q->kernel_if->u.h.header_page,
666 err = VMCI_ERROR_NO_MEM;
670 retval = get_user_pages_fast((uintptr_t) consume_uva,
671 consume_q->kernel_if->num_pages,
673 consume_q->kernel_if->u.h.header_page);
674 if (retval < (int)consume_q->kernel_if->num_pages) {
675 pr_debug("get_user_pages_fast(consume) failed (retval=%d)",
678 qp_release_pages(consume_q->kernel_if->u.h.header_page,
680 qp_release_pages(produce_q->kernel_if->u.h.header_page,
681 produce_q->kernel_if->num_pages, false);
682 err = VMCI_ERROR_NO_MEM;
690 * Registers the specification of the user pages used for backing a queue
691 * pair. Enough information to map in pages is stored in the OS specific
692 * part of the struct vmci_queue structure.
694 static int qp_host_register_user_memory(struct vmci_qp_page_store *page_store,
695 struct vmci_queue *produce_q,
696 struct vmci_queue *consume_q)
702 * The new style and the old style mapping only differs in
703 * that we either get a single or two UVAs, so we split the
704 * single UVA range at the appropriate spot.
706 produce_uva = page_store->pages;
707 consume_uva = page_store->pages +
708 produce_q->kernel_if->num_pages * PAGE_SIZE;
709 return qp_host_get_user_memory(produce_uva, consume_uva, produce_q,
714 * Releases and removes the references to user pages stored in the attach
715 * struct. Pages are released from the page cache and may become
718 static void qp_host_unregister_user_memory(struct vmci_queue *produce_q,
719 struct vmci_queue *consume_q)
721 qp_release_pages(produce_q->kernel_if->u.h.header_page,
722 produce_q->kernel_if->num_pages, true);
723 memset(produce_q->kernel_if->u.h.header_page, 0,
724 sizeof(*produce_q->kernel_if->u.h.header_page) *
725 produce_q->kernel_if->num_pages);
726 qp_release_pages(consume_q->kernel_if->u.h.header_page,
727 consume_q->kernel_if->num_pages, true);
728 memset(consume_q->kernel_if->u.h.header_page, 0,
729 sizeof(*consume_q->kernel_if->u.h.header_page) *
730 consume_q->kernel_if->num_pages);
734 * Once qp_host_register_user_memory has been performed on a
735 * queue, the queue pair headers can be mapped into the
736 * kernel. Once mapped, they must be unmapped with
737 * qp_host_unmap_queues prior to calling
738 * qp_host_unregister_user_memory.
741 static int qp_host_map_queues(struct vmci_queue *produce_q,
742 struct vmci_queue *consume_q)
746 if (!produce_q->q_header || !consume_q->q_header) {
747 struct page *headers[2];
749 if (produce_q->q_header != consume_q->q_header)
750 return VMCI_ERROR_QUEUEPAIR_MISMATCH;
752 if (produce_q->kernel_if->u.h.header_page == NULL ||
753 *produce_q->kernel_if->u.h.header_page == NULL)
754 return VMCI_ERROR_UNAVAILABLE;
756 headers[0] = *produce_q->kernel_if->u.h.header_page;
757 headers[1] = *consume_q->kernel_if->u.h.header_page;
759 produce_q->q_header = vmap(headers, 2, VM_MAP, PAGE_KERNEL);
760 if (produce_q->q_header != NULL) {
761 consume_q->q_header =
762 (struct vmci_queue_header *)((u8 *)
763 produce_q->q_header +
765 result = VMCI_SUCCESS;
767 pr_warn("vmap failed\n");
768 result = VMCI_ERROR_NO_MEM;
771 result = VMCI_SUCCESS;
778 * Unmaps previously mapped queue pair headers from the kernel.
779 * Pages are unpinned.
781 static int qp_host_unmap_queues(u32 gid,
782 struct vmci_queue *produce_q,
783 struct vmci_queue *consume_q)
785 if (produce_q->q_header) {
786 if (produce_q->q_header < consume_q->q_header)
787 vunmap(produce_q->q_header);
789 vunmap(consume_q->q_header);
791 produce_q->q_header = NULL;
792 consume_q->q_header = NULL;
799 * Finds the entry in the list corresponding to a given handle. Assumes
800 * that the list is locked.
802 static struct qp_entry *qp_list_find(struct qp_list *qp_list,
803 struct vmci_handle handle)
805 struct qp_entry *entry;
807 if (vmci_handle_is_invalid(handle))
810 list_for_each_entry(entry, &qp_list->head, list_item) {
811 if (vmci_handle_is_equal(entry->handle, handle))
819 * Finds the entry in the list corresponding to a given handle.
821 static struct qp_guest_endpoint *
822 qp_guest_handle_to_entry(struct vmci_handle handle)
824 struct qp_guest_endpoint *entry;
825 struct qp_entry *qp = qp_list_find(&qp_guest_endpoints, handle);
827 entry = qp ? container_of(
828 qp, struct qp_guest_endpoint, qp) : NULL;
833 * Finds the entry in the list corresponding to a given handle.
835 static struct qp_broker_entry *
836 qp_broker_handle_to_entry(struct vmci_handle handle)
838 struct qp_broker_entry *entry;
839 struct qp_entry *qp = qp_list_find(&qp_broker_list, handle);
841 entry = qp ? container_of(
842 qp, struct qp_broker_entry, qp) : NULL;
847 * Dispatches a queue pair event message directly into the local event
850 static int qp_notify_peer_local(bool attach, struct vmci_handle handle)
852 u32 context_id = vmci_get_context_id();
853 struct vmci_event_qp ev;
855 memset(&ev, 0, sizeof(ev));
856 ev.msg.hdr.dst = vmci_make_handle(context_id, VMCI_EVENT_HANDLER);
857 ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
858 VMCI_CONTEXT_RESOURCE_ID);
859 ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
860 ev.msg.event_data.event =
861 attach ? VMCI_EVENT_QP_PEER_ATTACH : VMCI_EVENT_QP_PEER_DETACH;
862 ev.payload.peer_id = context_id;
863 ev.payload.handle = handle;
865 return vmci_event_dispatch(&ev.msg.hdr);
869 * Allocates and initializes a qp_guest_endpoint structure.
870 * Allocates a queue_pair rid (and handle) iff the given entry has
871 * an invalid handle. 0 through VMCI_RESERVED_RESOURCE_ID_MAX
872 * are reserved handles. Assumes that the QP list mutex is held
875 static struct qp_guest_endpoint *
876 qp_guest_endpoint_create(struct vmci_handle handle,
885 struct qp_guest_endpoint *entry;
886 /* One page each for the queue headers. */
887 const u64 num_ppns = DIV_ROUND_UP(produce_size, PAGE_SIZE) +
888 DIV_ROUND_UP(consume_size, PAGE_SIZE) + 2;
890 if (vmci_handle_is_invalid(handle)) {
891 u32 context_id = vmci_get_context_id();
893 handle = vmci_make_handle(context_id, VMCI_INVALID_ID);
896 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
898 entry->qp.peer = peer;
899 entry->qp.flags = flags;
900 entry->qp.produce_size = produce_size;
901 entry->qp.consume_size = consume_size;
902 entry->qp.ref_count = 0;
903 entry->num_ppns = num_ppns;
904 entry->produce_q = produce_q;
905 entry->consume_q = consume_q;
906 INIT_LIST_HEAD(&entry->qp.list_item);
908 /* Add resource obj */
909 result = vmci_resource_add(&entry->resource,
910 VMCI_RESOURCE_TYPE_QPAIR_GUEST,
912 entry->qp.handle = vmci_resource_handle(&entry->resource);
913 if ((result != VMCI_SUCCESS) ||
914 qp_list_find(&qp_guest_endpoints, entry->qp.handle)) {
915 pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
916 handle.context, handle.resource, result);
925 * Frees a qp_guest_endpoint structure.
927 static void qp_guest_endpoint_destroy(struct qp_guest_endpoint *entry)
929 qp_free_ppn_set(&entry->ppn_set);
930 qp_cleanup_queue_mutex(entry->produce_q, entry->consume_q);
931 qp_free_queue(entry->produce_q, entry->qp.produce_size);
932 qp_free_queue(entry->consume_q, entry->qp.consume_size);
933 /* Unlink from resource hash table and free callback */
934 vmci_resource_remove(&entry->resource);
940 * Helper to make a queue_pairAlloc hypercall when the driver is
941 * supporting a guest device.
943 static int qp_alloc_hypercall(const struct qp_guest_endpoint *entry)
945 struct vmci_qp_alloc_msg *alloc_msg;
950 if (!entry || entry->num_ppns <= 2)
951 return VMCI_ERROR_INVALID_ARGS;
953 ppn_size = vmci_use_ppn64() ? sizeof(u64) : sizeof(u32);
954 msg_size = sizeof(*alloc_msg) +
955 (size_t) entry->num_ppns * ppn_size;
956 alloc_msg = kmalloc(msg_size, GFP_KERNEL);
958 return VMCI_ERROR_NO_MEM;
960 alloc_msg->hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
961 VMCI_QUEUEPAIR_ALLOC);
962 alloc_msg->hdr.src = VMCI_ANON_SRC_HANDLE;
963 alloc_msg->hdr.payload_size = msg_size - VMCI_DG_HEADERSIZE;
964 alloc_msg->handle = entry->qp.handle;
965 alloc_msg->peer = entry->qp.peer;
966 alloc_msg->flags = entry->qp.flags;
967 alloc_msg->produce_size = entry->qp.produce_size;
968 alloc_msg->consume_size = entry->qp.consume_size;
969 alloc_msg->num_ppns = entry->num_ppns;
971 result = qp_populate_ppn_set((u8 *)alloc_msg + sizeof(*alloc_msg),
973 if (result == VMCI_SUCCESS)
974 result = vmci_send_datagram(&alloc_msg->hdr);
982 * Helper to make a queue_pairDetach hypercall when the driver is
983 * supporting a guest device.
985 static int qp_detatch_hypercall(struct vmci_handle handle)
987 struct vmci_qp_detach_msg detach_msg;
989 detach_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
990 VMCI_QUEUEPAIR_DETACH);
991 detach_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
992 detach_msg.hdr.payload_size = sizeof(handle);
993 detach_msg.handle = handle;
995 return vmci_send_datagram(&detach_msg.hdr);
999 * Adds the given entry to the list. Assumes that the list is locked.
1001 static void qp_list_add_entry(struct qp_list *qp_list, struct qp_entry *entry)
1004 list_add(&entry->list_item, &qp_list->head);
1008 * Removes the given entry from the list. Assumes that the list is locked.
1010 static void qp_list_remove_entry(struct qp_list *qp_list,
1011 struct qp_entry *entry)
1014 list_del(&entry->list_item);
1018 * Helper for VMCI queue_pair detach interface. Frees the physical
1019 * pages for the queue pair.
1021 static int qp_detatch_guest_work(struct vmci_handle handle)
1024 struct qp_guest_endpoint *entry;
1025 u32 ref_count = ~0; /* To avoid compiler warning below */
1027 mutex_lock(&qp_guest_endpoints.mutex);
1029 entry = qp_guest_handle_to_entry(handle);
1031 mutex_unlock(&qp_guest_endpoints.mutex);
1032 return VMCI_ERROR_NOT_FOUND;
1035 if (entry->qp.flags & VMCI_QPFLAG_LOCAL) {
1036 result = VMCI_SUCCESS;
1038 if (entry->qp.ref_count > 1) {
1039 result = qp_notify_peer_local(false, handle);
1041 * We can fail to notify a local queuepair
1042 * because we can't allocate. We still want
1043 * to release the entry if that happens, so
1044 * don't bail out yet.
1048 result = qp_detatch_hypercall(handle);
1049 if (result < VMCI_SUCCESS) {
1051 * We failed to notify a non-local queuepair.
1052 * That other queuepair might still be
1053 * accessing the shared memory, so don't
1054 * release the entry yet. It will get cleaned
1055 * up by VMCIqueue_pair_Exit() if necessary
1056 * (assuming we are going away, otherwise why
1060 mutex_unlock(&qp_guest_endpoints.mutex);
1066 * If we get here then we either failed to notify a local queuepair, or
1067 * we succeeded in all cases. Release the entry if required.
1070 entry->qp.ref_count--;
1071 if (entry->qp.ref_count == 0)
1072 qp_list_remove_entry(&qp_guest_endpoints, &entry->qp);
1074 /* If we didn't remove the entry, this could change once we unlock. */
1076 ref_count = entry->qp.ref_count;
1078 mutex_unlock(&qp_guest_endpoints.mutex);
1081 qp_guest_endpoint_destroy(entry);
1087 * This functions handles the actual allocation of a VMCI queue
1088 * pair guest endpoint. Allocates physical pages for the queue
1089 * pair. It makes OS dependent calls through generic wrappers.
1091 static int qp_alloc_guest_work(struct vmci_handle *handle,
1092 struct vmci_queue **produce_q,
1094 struct vmci_queue **consume_q,
1100 const u64 num_produce_pages =
1101 DIV_ROUND_UP(produce_size, PAGE_SIZE) + 1;
1102 const u64 num_consume_pages =
1103 DIV_ROUND_UP(consume_size, PAGE_SIZE) + 1;
1104 void *my_produce_q = NULL;
1105 void *my_consume_q = NULL;
1107 struct qp_guest_endpoint *queue_pair_entry = NULL;
1109 if (priv_flags != VMCI_NO_PRIVILEGE_FLAGS)
1110 return VMCI_ERROR_NO_ACCESS;
1112 mutex_lock(&qp_guest_endpoints.mutex);
1114 queue_pair_entry = qp_guest_handle_to_entry(*handle);
1115 if (queue_pair_entry) {
1116 if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) {
1117 /* Local attach case. */
1118 if (queue_pair_entry->qp.ref_count > 1) {
1119 pr_devel("Error attempting to attach more than once\n");
1120 result = VMCI_ERROR_UNAVAILABLE;
1121 goto error_keep_entry;
1124 if (queue_pair_entry->qp.produce_size != consume_size ||
1125 queue_pair_entry->qp.consume_size !=
1127 queue_pair_entry->qp.flags !=
1128 (flags & ~VMCI_QPFLAG_ATTACH_ONLY)) {
1129 pr_devel("Error mismatched queue pair in local attach\n");
1130 result = VMCI_ERROR_QUEUEPAIR_MISMATCH;
1131 goto error_keep_entry;
1135 * Do a local attach. We swap the consume and
1136 * produce queues for the attacher and deliver
1139 result = qp_notify_peer_local(true, *handle);
1140 if (result < VMCI_SUCCESS)
1141 goto error_keep_entry;
1143 my_produce_q = queue_pair_entry->consume_q;
1144 my_consume_q = queue_pair_entry->produce_q;
1148 result = VMCI_ERROR_ALREADY_EXISTS;
1149 goto error_keep_entry;
1152 my_produce_q = qp_alloc_queue(produce_size, flags);
1153 if (!my_produce_q) {
1154 pr_warn("Error allocating pages for produce queue\n");
1155 result = VMCI_ERROR_NO_MEM;
1159 my_consume_q = qp_alloc_queue(consume_size, flags);
1160 if (!my_consume_q) {
1161 pr_warn("Error allocating pages for consume queue\n");
1162 result = VMCI_ERROR_NO_MEM;
1166 queue_pair_entry = qp_guest_endpoint_create(*handle, peer, flags,
1167 produce_size, consume_size,
1168 my_produce_q, my_consume_q);
1169 if (!queue_pair_entry) {
1170 pr_warn("Error allocating memory in %s\n", __func__);
1171 result = VMCI_ERROR_NO_MEM;
1175 result = qp_alloc_ppn_set(my_produce_q, num_produce_pages, my_consume_q,
1177 &queue_pair_entry->ppn_set);
1178 if (result < VMCI_SUCCESS) {
1179 pr_warn("qp_alloc_ppn_set failed\n");
1184 * It's only necessary to notify the host if this queue pair will be
1185 * attached to from another context.
1187 if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) {
1188 /* Local create case. */
1189 u32 context_id = vmci_get_context_id();
1192 * Enforce similar checks on local queue pairs as we
1193 * do for regular ones. The handle's context must
1194 * match the creator or attacher context id (here they
1195 * are both the current context id) and the
1196 * attach-only flag cannot exist during create. We
1197 * also ensure specified peer is this context or an
1200 if (queue_pair_entry->qp.handle.context != context_id ||
1201 (queue_pair_entry->qp.peer != VMCI_INVALID_ID &&
1202 queue_pair_entry->qp.peer != context_id)) {
1203 result = VMCI_ERROR_NO_ACCESS;
1207 if (queue_pair_entry->qp.flags & VMCI_QPFLAG_ATTACH_ONLY) {
1208 result = VMCI_ERROR_NOT_FOUND;
1212 result = qp_alloc_hypercall(queue_pair_entry);
1213 if (result < VMCI_SUCCESS) {
1214 pr_warn("qp_alloc_hypercall result = %d\n", result);
1219 qp_init_queue_mutex((struct vmci_queue *)my_produce_q,
1220 (struct vmci_queue *)my_consume_q);
1222 qp_list_add_entry(&qp_guest_endpoints, &queue_pair_entry->qp);
1225 queue_pair_entry->qp.ref_count++;
1226 *handle = queue_pair_entry->qp.handle;
1227 *produce_q = (struct vmci_queue *)my_produce_q;
1228 *consume_q = (struct vmci_queue *)my_consume_q;
1231 * We should initialize the queue pair header pages on a local
1232 * queue pair create. For non-local queue pairs, the
1233 * hypervisor initializes the header pages in the create step.
1235 if ((queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) &&
1236 queue_pair_entry->qp.ref_count == 1) {
1237 vmci_q_header_init((*produce_q)->q_header, *handle);
1238 vmci_q_header_init((*consume_q)->q_header, *handle);
1241 mutex_unlock(&qp_guest_endpoints.mutex);
1243 return VMCI_SUCCESS;
1246 mutex_unlock(&qp_guest_endpoints.mutex);
1247 if (queue_pair_entry) {
1248 /* The queues will be freed inside the destroy routine. */
1249 qp_guest_endpoint_destroy(queue_pair_entry);
1251 qp_free_queue(my_produce_q, produce_size);
1252 qp_free_queue(my_consume_q, consume_size);
1257 /* This path should only be used when an existing entry was found. */
1258 mutex_unlock(&qp_guest_endpoints.mutex);
1263 * The first endpoint issuing a queue pair allocation will create the state
1264 * of the queue pair in the queue pair broker.
1266 * If the creator is a guest, it will associate a VMX virtual address range
1267 * with the queue pair as specified by the page_store. For compatibility with
1268 * older VMX'en, that would use a separate step to set the VMX virtual
1269 * address range, the virtual address range can be registered later using
1270 * vmci_qp_broker_set_page_store. In that case, a page_store of NULL should be
1273 * If the creator is the host, a page_store of NULL should be used as well,
1274 * since the host is not able to supply a page store for the queue pair.
1276 * For older VMX and host callers, the queue pair will be created in the
1277 * VMCIQPB_CREATED_NO_MEM state, and for current VMX callers, it will be
1278 * created in VMCOQPB_CREATED_MEM state.
1280 static int qp_broker_create(struct vmci_handle handle,
1286 struct vmci_qp_page_store *page_store,
1287 struct vmci_ctx *context,
1288 vmci_event_release_cb wakeup_cb,
1289 void *client_data, struct qp_broker_entry **ent)
1291 struct qp_broker_entry *entry = NULL;
1292 const u32 context_id = vmci_ctx_get_id(context);
1293 bool is_local = flags & VMCI_QPFLAG_LOCAL;
1295 u64 guest_produce_size;
1296 u64 guest_consume_size;
1298 /* Do not create if the caller asked not to. */
1299 if (flags & VMCI_QPFLAG_ATTACH_ONLY)
1300 return VMCI_ERROR_NOT_FOUND;
1303 * Creator's context ID should match handle's context ID or the creator
1304 * must allow the context in handle's context ID as the "peer".
1306 if (handle.context != context_id && handle.context != peer)
1307 return VMCI_ERROR_NO_ACCESS;
1309 if (VMCI_CONTEXT_IS_VM(context_id) && VMCI_CONTEXT_IS_VM(peer))
1310 return VMCI_ERROR_DST_UNREACHABLE;
1313 * Creator's context ID for local queue pairs should match the
1314 * peer, if a peer is specified.
1316 if (is_local && peer != VMCI_INVALID_ID && context_id != peer)
1317 return VMCI_ERROR_NO_ACCESS;
1319 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1321 return VMCI_ERROR_NO_MEM;
1323 if (vmci_ctx_get_id(context) == VMCI_HOST_CONTEXT_ID && !is_local) {
1325 * The queue pair broker entry stores values from the guest
1326 * point of view, so a creating host side endpoint should swap
1327 * produce and consume values -- unless it is a local queue
1328 * pair, in which case no swapping is necessary, since the local
1329 * attacher will swap queues.
1332 guest_produce_size = consume_size;
1333 guest_consume_size = produce_size;
1335 guest_produce_size = produce_size;
1336 guest_consume_size = consume_size;
1339 entry->qp.handle = handle;
1340 entry->qp.peer = peer;
1341 entry->qp.flags = flags;
1342 entry->qp.produce_size = guest_produce_size;
1343 entry->qp.consume_size = guest_consume_size;
1344 entry->qp.ref_count = 1;
1345 entry->create_id = context_id;
1346 entry->attach_id = VMCI_INVALID_ID;
1347 entry->state = VMCIQPB_NEW;
1348 entry->require_trusted_attach =
1349 !!(context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED);
1350 entry->created_by_trusted =
1351 !!(priv_flags & VMCI_PRIVILEGE_FLAG_TRUSTED);
1352 entry->vmci_page_files = false;
1353 entry->wakeup_cb = wakeup_cb;
1354 entry->client_data = client_data;
1355 entry->produce_q = qp_host_alloc_queue(guest_produce_size);
1356 if (entry->produce_q == NULL) {
1357 result = VMCI_ERROR_NO_MEM;
1360 entry->consume_q = qp_host_alloc_queue(guest_consume_size);
1361 if (entry->consume_q == NULL) {
1362 result = VMCI_ERROR_NO_MEM;
1366 qp_init_queue_mutex(entry->produce_q, entry->consume_q);
1368 INIT_LIST_HEAD(&entry->qp.list_item);
1373 entry->local_mem = kcalloc(QPE_NUM_PAGES(entry->qp),
1374 PAGE_SIZE, GFP_KERNEL);
1375 if (entry->local_mem == NULL) {
1376 result = VMCI_ERROR_NO_MEM;
1379 entry->state = VMCIQPB_CREATED_MEM;
1380 entry->produce_q->q_header = entry->local_mem;
1381 tmp = (u8 *)entry->local_mem + PAGE_SIZE *
1382 (DIV_ROUND_UP(entry->qp.produce_size, PAGE_SIZE) + 1);
1383 entry->consume_q->q_header = (struct vmci_queue_header *)tmp;
1384 } else if (page_store) {
1386 * The VMX already initialized the queue pair headers, so no
1387 * need for the kernel side to do that.
1389 result = qp_host_register_user_memory(page_store,
1392 if (result < VMCI_SUCCESS)
1395 entry->state = VMCIQPB_CREATED_MEM;
1398 * A create without a page_store may be either a host
1399 * side create (in which case we are waiting for the
1400 * guest side to supply the memory) or an old style
1401 * queue pair create (in which case we will expect a
1402 * set page store call as the next step).
1404 entry->state = VMCIQPB_CREATED_NO_MEM;
1407 qp_list_add_entry(&qp_broker_list, &entry->qp);
1411 /* Add to resource obj */
1412 result = vmci_resource_add(&entry->resource,
1413 VMCI_RESOURCE_TYPE_QPAIR_HOST,
1415 if (result != VMCI_SUCCESS) {
1416 pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
1417 handle.context, handle.resource, result);
1421 entry->qp.handle = vmci_resource_handle(&entry->resource);
1423 vmci_q_header_init(entry->produce_q->q_header,
1425 vmci_q_header_init(entry->consume_q->q_header,
1429 vmci_ctx_qp_create(context, entry->qp.handle);
1431 return VMCI_SUCCESS;
1434 if (entry != NULL) {
1435 qp_host_free_queue(entry->produce_q, guest_produce_size);
1436 qp_host_free_queue(entry->consume_q, guest_consume_size);
1444 * Enqueues an event datagram to notify the peer VM attached to
1445 * the given queue pair handle about attach/detach event by the
1446 * given VM. Returns Payload size of datagram enqueued on
1447 * success, error code otherwise.
1449 static int qp_notify_peer(bool attach,
1450 struct vmci_handle handle,
1455 struct vmci_event_qp ev;
1457 if (vmci_handle_is_invalid(handle) || my_id == VMCI_INVALID_ID ||
1458 peer_id == VMCI_INVALID_ID)
1459 return VMCI_ERROR_INVALID_ARGS;
1462 * In vmci_ctx_enqueue_datagram() we enforce the upper limit on
1463 * number of pending events from the hypervisor to a given VM
1464 * otherwise a rogue VM could do an arbitrary number of attach
1465 * and detach operations causing memory pressure in the host
1469 memset(&ev, 0, sizeof(ev));
1470 ev.msg.hdr.dst = vmci_make_handle(peer_id, VMCI_EVENT_HANDLER);
1471 ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
1472 VMCI_CONTEXT_RESOURCE_ID);
1473 ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
1474 ev.msg.event_data.event = attach ?
1475 VMCI_EVENT_QP_PEER_ATTACH : VMCI_EVENT_QP_PEER_DETACH;
1476 ev.payload.handle = handle;
1477 ev.payload.peer_id = my_id;
1479 rv = vmci_datagram_dispatch(VMCI_HYPERVISOR_CONTEXT_ID,
1480 &ev.msg.hdr, false);
1481 if (rv < VMCI_SUCCESS)
1482 pr_warn("Failed to enqueue queue_pair %s event datagram for context (ID=0x%x)\n",
1483 attach ? "ATTACH" : "DETACH", peer_id);
1489 * The second endpoint issuing a queue pair allocation will attach to
1490 * the queue pair registered with the queue pair broker.
1492 * If the attacher is a guest, it will associate a VMX virtual address
1493 * range with the queue pair as specified by the page_store. At this
1494 * point, the already attach host endpoint may start using the queue
1495 * pair, and an attach event is sent to it. For compatibility with
1496 * older VMX'en, that used a separate step to set the VMX virtual
1497 * address range, the virtual address range can be registered later
1498 * using vmci_qp_broker_set_page_store. In that case, a page_store of
1499 * NULL should be used, and the attach event will be generated once
1500 * the actual page store has been set.
1502 * If the attacher is the host, a page_store of NULL should be used as
1503 * well, since the page store information is already set by the guest.
1505 * For new VMX and host callers, the queue pair will be moved to the
1506 * VMCIQPB_ATTACHED_MEM state, and for older VMX callers, it will be
1507 * moved to the VMCOQPB_ATTACHED_NO_MEM state.
1509 static int qp_broker_attach(struct qp_broker_entry *entry,
1515 struct vmci_qp_page_store *page_store,
1516 struct vmci_ctx *context,
1517 vmci_event_release_cb wakeup_cb,
1519 struct qp_broker_entry **ent)
1521 const u32 context_id = vmci_ctx_get_id(context);
1522 bool is_local = flags & VMCI_QPFLAG_LOCAL;
1525 if (entry->state != VMCIQPB_CREATED_NO_MEM &&
1526 entry->state != VMCIQPB_CREATED_MEM)
1527 return VMCI_ERROR_UNAVAILABLE;
1530 if (!(entry->qp.flags & VMCI_QPFLAG_LOCAL) ||
1531 context_id != entry->create_id) {
1532 return VMCI_ERROR_INVALID_ARGS;
1534 } else if (context_id == entry->create_id ||
1535 context_id == entry->attach_id) {
1536 return VMCI_ERROR_ALREADY_EXISTS;
1539 if (VMCI_CONTEXT_IS_VM(context_id) &&
1540 VMCI_CONTEXT_IS_VM(entry->create_id))
1541 return VMCI_ERROR_DST_UNREACHABLE;
1544 * If we are attaching from a restricted context then the queuepair
1545 * must have been created by a trusted endpoint.
1547 if ((context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
1548 !entry->created_by_trusted)
1549 return VMCI_ERROR_NO_ACCESS;
1552 * If we are attaching to a queuepair that was created by a restricted
1553 * context then we must be trusted.
1555 if (entry->require_trusted_attach &&
1556 (!(priv_flags & VMCI_PRIVILEGE_FLAG_TRUSTED)))
1557 return VMCI_ERROR_NO_ACCESS;
1560 * If the creator specifies VMCI_INVALID_ID in "peer" field, access
1561 * control check is not performed.
1563 if (entry->qp.peer != VMCI_INVALID_ID && entry->qp.peer != context_id)
1564 return VMCI_ERROR_NO_ACCESS;
1566 if (entry->create_id == VMCI_HOST_CONTEXT_ID) {
1568 * Do not attach if the caller doesn't support Host Queue Pairs
1569 * and a host created this queue pair.
1572 if (!vmci_ctx_supports_host_qp(context))
1573 return VMCI_ERROR_INVALID_RESOURCE;
1575 } else if (context_id == VMCI_HOST_CONTEXT_ID) {
1576 struct vmci_ctx *create_context;
1577 bool supports_host_qp;
1580 * Do not attach a host to a user created queue pair if that
1581 * user doesn't support host queue pair end points.
1584 create_context = vmci_ctx_get(entry->create_id);
1585 supports_host_qp = vmci_ctx_supports_host_qp(create_context);
1586 vmci_ctx_put(create_context);
1588 if (!supports_host_qp)
1589 return VMCI_ERROR_INVALID_RESOURCE;
1592 if ((entry->qp.flags & ~VMCI_QP_ASYMM) != (flags & ~VMCI_QP_ASYMM_PEER))
1593 return VMCI_ERROR_QUEUEPAIR_MISMATCH;
1595 if (context_id != VMCI_HOST_CONTEXT_ID) {
1597 * The queue pair broker entry stores values from the guest
1598 * point of view, so an attaching guest should match the values
1599 * stored in the entry.
1602 if (entry->qp.produce_size != produce_size ||
1603 entry->qp.consume_size != consume_size) {
1604 return VMCI_ERROR_QUEUEPAIR_MISMATCH;
1606 } else if (entry->qp.produce_size != consume_size ||
1607 entry->qp.consume_size != produce_size) {
1608 return VMCI_ERROR_QUEUEPAIR_MISMATCH;
1611 if (context_id != VMCI_HOST_CONTEXT_ID) {
1613 * If a guest attached to a queue pair, it will supply
1614 * the backing memory. If this is a pre NOVMVM vmx,
1615 * the backing memory will be supplied by calling
1616 * vmci_qp_broker_set_page_store() following the
1617 * return of the vmci_qp_broker_alloc() call. If it is
1618 * a vmx of version NOVMVM or later, the page store
1619 * must be supplied as part of the
1620 * vmci_qp_broker_alloc call. Under all circumstances
1621 * must the initially created queue pair not have any
1622 * memory associated with it already.
1625 if (entry->state != VMCIQPB_CREATED_NO_MEM)
1626 return VMCI_ERROR_INVALID_ARGS;
1628 if (page_store != NULL) {
1630 * Patch up host state to point to guest
1631 * supplied memory. The VMX already
1632 * initialized the queue pair headers, so no
1633 * need for the kernel side to do that.
1636 result = qp_host_register_user_memory(page_store,
1639 if (result < VMCI_SUCCESS)
1642 entry->state = VMCIQPB_ATTACHED_MEM;
1644 entry->state = VMCIQPB_ATTACHED_NO_MEM;
1646 } else if (entry->state == VMCIQPB_CREATED_NO_MEM) {
1648 * The host side is attempting to attach to a queue
1649 * pair that doesn't have any memory associated with
1650 * it. This must be a pre NOVMVM vmx that hasn't set
1651 * the page store information yet, or a quiesced VM.
1654 return VMCI_ERROR_UNAVAILABLE;
1656 /* The host side has successfully attached to a queue pair. */
1657 entry->state = VMCIQPB_ATTACHED_MEM;
1660 if (entry->state == VMCIQPB_ATTACHED_MEM) {
1662 qp_notify_peer(true, entry->qp.handle, context_id,
1664 if (result < VMCI_SUCCESS)
1665 pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
1666 entry->create_id, entry->qp.handle.context,
1667 entry->qp.handle.resource);
1670 entry->attach_id = context_id;
1671 entry->qp.ref_count++;
1673 entry->wakeup_cb = wakeup_cb;
1674 entry->client_data = client_data;
1678 * When attaching to local queue pairs, the context already has
1679 * an entry tracking the queue pair, so don't add another one.
1682 vmci_ctx_qp_create(context, entry->qp.handle);
1687 return VMCI_SUCCESS;
1691 * queue_pair_Alloc for use when setting up queue pair endpoints
1694 static int qp_broker_alloc(struct vmci_handle handle,
1700 struct vmci_qp_page_store *page_store,
1701 struct vmci_ctx *context,
1702 vmci_event_release_cb wakeup_cb,
1704 struct qp_broker_entry **ent,
1707 const u32 context_id = vmci_ctx_get_id(context);
1709 struct qp_broker_entry *entry = NULL;
1710 bool is_local = flags & VMCI_QPFLAG_LOCAL;
1713 if (vmci_handle_is_invalid(handle) ||
1714 (flags & ~VMCI_QP_ALL_FLAGS) || is_local ||
1715 !(produce_size || consume_size) ||
1716 !context || context_id == VMCI_INVALID_ID ||
1717 handle.context == VMCI_INVALID_ID) {
1718 return VMCI_ERROR_INVALID_ARGS;
1721 if (page_store && !VMCI_QP_PAGESTORE_IS_WELLFORMED(page_store))
1722 return VMCI_ERROR_INVALID_ARGS;
1725 * In the initial argument check, we ensure that non-vmkernel hosts
1726 * are not allowed to create local queue pairs.
1729 mutex_lock(&qp_broker_list.mutex);
1731 if (!is_local && vmci_ctx_qp_exists(context, handle)) {
1732 pr_devel("Context (ID=0x%x) already attached to queue pair (handle=0x%x:0x%x)\n",
1733 context_id, handle.context, handle.resource);
1734 mutex_unlock(&qp_broker_list.mutex);
1735 return VMCI_ERROR_ALREADY_EXISTS;
1738 if (handle.resource != VMCI_INVALID_ID)
1739 entry = qp_broker_handle_to_entry(handle);
1744 qp_broker_create(handle, peer, flags, priv_flags,
1745 produce_size, consume_size, page_store,
1746 context, wakeup_cb, client_data, ent);
1750 qp_broker_attach(entry, peer, flags, priv_flags,
1751 produce_size, consume_size, page_store,
1752 context, wakeup_cb, client_data, ent);
1755 mutex_unlock(&qp_broker_list.mutex);
1758 *swap = (context_id == VMCI_HOST_CONTEXT_ID) &&
1759 !(create && is_local);
1765 * This function implements the kernel API for allocating a queue
1768 static int qp_alloc_host_work(struct vmci_handle *handle,
1769 struct vmci_queue **produce_q,
1771 struct vmci_queue **consume_q,
1776 vmci_event_release_cb wakeup_cb,
1779 struct vmci_handle new_handle;
1780 struct vmci_ctx *context;
1781 struct qp_broker_entry *entry;
1785 if (vmci_handle_is_invalid(*handle)) {
1786 new_handle = vmci_make_handle(
1787 VMCI_HOST_CONTEXT_ID, VMCI_INVALID_ID);
1789 new_handle = *handle;
1791 context = vmci_ctx_get(VMCI_HOST_CONTEXT_ID);
1794 qp_broker_alloc(new_handle, peer, flags, priv_flags,
1795 produce_size, consume_size, NULL, context,
1796 wakeup_cb, client_data, &entry, &swap);
1797 if (result == VMCI_SUCCESS) {
1800 * If this is a local queue pair, the attacher
1801 * will swap around produce and consume
1805 *produce_q = entry->consume_q;
1806 *consume_q = entry->produce_q;
1808 *produce_q = entry->produce_q;
1809 *consume_q = entry->consume_q;
1812 *handle = vmci_resource_handle(&entry->resource);
1814 *handle = VMCI_INVALID_HANDLE;
1815 pr_devel("queue pair broker failed to alloc (result=%d)\n",
1818 vmci_ctx_put(context);
1823 * Allocates a VMCI queue_pair. Only checks validity of input
1824 * arguments. The real work is done in the host or guest
1825 * specific function.
1827 int vmci_qp_alloc(struct vmci_handle *handle,
1828 struct vmci_queue **produce_q,
1830 struct vmci_queue **consume_q,
1835 bool guest_endpoint,
1836 vmci_event_release_cb wakeup_cb,
1839 if (!handle || !produce_q || !consume_q ||
1840 (!produce_size && !consume_size) || (flags & ~VMCI_QP_ALL_FLAGS))
1841 return VMCI_ERROR_INVALID_ARGS;
1843 if (guest_endpoint) {
1844 return qp_alloc_guest_work(handle, produce_q,
1845 produce_size, consume_q,
1849 return qp_alloc_host_work(handle, produce_q,
1850 produce_size, consume_q,
1851 consume_size, peer, flags,
1852 priv_flags, wakeup_cb, client_data);
1857 * This function implements the host kernel API for detaching from
1860 static int qp_detatch_host_work(struct vmci_handle handle)
1863 struct vmci_ctx *context;
1865 context = vmci_ctx_get(VMCI_HOST_CONTEXT_ID);
1867 result = vmci_qp_broker_detach(handle, context);
1869 vmci_ctx_put(context);
1874 * Detaches from a VMCI queue_pair. Only checks validity of input argument.
1875 * Real work is done in the host or guest specific function.
1877 static int qp_detatch(struct vmci_handle handle, bool guest_endpoint)
1879 if (vmci_handle_is_invalid(handle))
1880 return VMCI_ERROR_INVALID_ARGS;
1883 return qp_detatch_guest_work(handle);
1885 return qp_detatch_host_work(handle);
1889 * Returns the entry from the head of the list. Assumes that the list is
1892 static struct qp_entry *qp_list_get_head(struct qp_list *qp_list)
1894 if (!list_empty(&qp_list->head)) {
1895 struct qp_entry *entry =
1896 list_first_entry(&qp_list->head, struct qp_entry,
1904 void vmci_qp_broker_exit(void)
1906 struct qp_entry *entry;
1907 struct qp_broker_entry *be;
1909 mutex_lock(&qp_broker_list.mutex);
1911 while ((entry = qp_list_get_head(&qp_broker_list))) {
1912 be = (struct qp_broker_entry *)entry;
1914 qp_list_remove_entry(&qp_broker_list, entry);
1918 mutex_unlock(&qp_broker_list.mutex);
1922 * Requests that a queue pair be allocated with the VMCI queue
1923 * pair broker. Allocates a queue pair entry if one does not
1924 * exist. Attaches to one if it exists, and retrieves the page
1925 * files backing that queue_pair. Assumes that the queue pair
1926 * broker lock is held.
1928 int vmci_qp_broker_alloc(struct vmci_handle handle,
1934 struct vmci_qp_page_store *page_store,
1935 struct vmci_ctx *context)
1937 return qp_broker_alloc(handle, peer, flags, priv_flags,
1938 produce_size, consume_size,
1939 page_store, context, NULL, NULL, NULL, NULL);
1943 * VMX'en with versions lower than VMCI_VERSION_NOVMVM use a separate
1944 * step to add the UVAs of the VMX mapping of the queue pair. This function
1945 * provides backwards compatibility with such VMX'en, and takes care of
1946 * registering the page store for a queue pair previously allocated by the
1947 * VMX during create or attach. This function will move the queue pair state
1948 * to either from VMCIQBP_CREATED_NO_MEM to VMCIQBP_CREATED_MEM or
1949 * VMCIQBP_ATTACHED_NO_MEM to VMCIQBP_ATTACHED_MEM. If moving to the
1950 * attached state with memory, the queue pair is ready to be used by the
1951 * host peer, and an attached event will be generated.
1953 * Assumes that the queue pair broker lock is held.
1955 * This function is only used by the hosted platform, since there is no
1956 * issue with backwards compatibility for vmkernel.
1958 int vmci_qp_broker_set_page_store(struct vmci_handle handle,
1961 struct vmci_ctx *context)
1963 struct qp_broker_entry *entry;
1965 const u32 context_id = vmci_ctx_get_id(context);
1967 if (vmci_handle_is_invalid(handle) || !context ||
1968 context_id == VMCI_INVALID_ID)
1969 return VMCI_ERROR_INVALID_ARGS;
1972 * We only support guest to host queue pairs, so the VMX must
1973 * supply UVAs for the mapped page files.
1976 if (produce_uva == 0 || consume_uva == 0)
1977 return VMCI_ERROR_INVALID_ARGS;
1979 mutex_lock(&qp_broker_list.mutex);
1981 if (!vmci_ctx_qp_exists(context, handle)) {
1982 pr_warn("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
1983 context_id, handle.context, handle.resource);
1984 result = VMCI_ERROR_NOT_FOUND;
1988 entry = qp_broker_handle_to_entry(handle);
1990 result = VMCI_ERROR_NOT_FOUND;
1995 * If I'm the owner then I can set the page store.
1997 * Or, if a host created the queue_pair and I'm the attached peer
1998 * then I can set the page store.
2000 if (entry->create_id != context_id &&
2001 (entry->create_id != VMCI_HOST_CONTEXT_ID ||
2002 entry->attach_id != context_id)) {
2003 result = VMCI_ERROR_QUEUEPAIR_NOTOWNER;
2007 if (entry->state != VMCIQPB_CREATED_NO_MEM &&
2008 entry->state != VMCIQPB_ATTACHED_NO_MEM) {
2009 result = VMCI_ERROR_UNAVAILABLE;
2013 result = qp_host_get_user_memory(produce_uva, consume_uva,
2014 entry->produce_q, entry->consume_q);
2015 if (result < VMCI_SUCCESS)
2018 result = qp_host_map_queues(entry->produce_q, entry->consume_q);
2019 if (result < VMCI_SUCCESS) {
2020 qp_host_unregister_user_memory(entry->produce_q,
2025 if (entry->state == VMCIQPB_CREATED_NO_MEM)
2026 entry->state = VMCIQPB_CREATED_MEM;
2028 entry->state = VMCIQPB_ATTACHED_MEM;
2030 entry->vmci_page_files = true;
2032 if (entry->state == VMCIQPB_ATTACHED_MEM) {
2034 qp_notify_peer(true, handle, context_id, entry->create_id);
2035 if (result < VMCI_SUCCESS) {
2036 pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
2037 entry->create_id, entry->qp.handle.context,
2038 entry->qp.handle.resource);
2042 result = VMCI_SUCCESS;
2044 mutex_unlock(&qp_broker_list.mutex);
2049 * Resets saved queue headers for the given QP broker
2050 * entry. Should be used when guest memory becomes available
2051 * again, or the guest detaches.
2053 static void qp_reset_saved_headers(struct qp_broker_entry *entry)
2055 entry->produce_q->saved_header = NULL;
2056 entry->consume_q->saved_header = NULL;
2060 * The main entry point for detaching from a queue pair registered with the
2061 * queue pair broker. If more than one endpoint is attached to the queue
2062 * pair, the first endpoint will mainly decrement a reference count and
2063 * generate a notification to its peer. The last endpoint will clean up
2064 * the queue pair state registered with the broker.
2066 * When a guest endpoint detaches, it will unmap and unregister the guest
2067 * memory backing the queue pair. If the host is still attached, it will
2068 * no longer be able to access the queue pair content.
2070 * If the queue pair is already in a state where there is no memory
2071 * registered for the queue pair (any *_NO_MEM state), it will transition to
2072 * the VMCIQPB_SHUTDOWN_NO_MEM state. This will also happen, if a guest
2073 * endpoint is the first of two endpoints to detach. If the host endpoint is
2074 * the first out of two to detach, the queue pair will move to the
2075 * VMCIQPB_SHUTDOWN_MEM state.
2077 int vmci_qp_broker_detach(struct vmci_handle handle, struct vmci_ctx *context)
2079 struct qp_broker_entry *entry;
2080 const u32 context_id = vmci_ctx_get_id(context);
2082 bool is_local = false;
2085 if (vmci_handle_is_invalid(handle) || !context ||
2086 context_id == VMCI_INVALID_ID) {
2087 return VMCI_ERROR_INVALID_ARGS;
2090 mutex_lock(&qp_broker_list.mutex);
2092 if (!vmci_ctx_qp_exists(context, handle)) {
2093 pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
2094 context_id, handle.context, handle.resource);
2095 result = VMCI_ERROR_NOT_FOUND;
2099 entry = qp_broker_handle_to_entry(handle);
2101 pr_devel("Context (ID=0x%x) reports being attached to queue pair(handle=0x%x:0x%x) that isn't present in broker\n",
2102 context_id, handle.context, handle.resource);
2103 result = VMCI_ERROR_NOT_FOUND;
2107 if (context_id != entry->create_id && context_id != entry->attach_id) {
2108 result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
2112 if (context_id == entry->create_id) {
2113 peer_id = entry->attach_id;
2114 entry->create_id = VMCI_INVALID_ID;
2116 peer_id = entry->create_id;
2117 entry->attach_id = VMCI_INVALID_ID;
2119 entry->qp.ref_count--;
2121 is_local = entry->qp.flags & VMCI_QPFLAG_LOCAL;
2123 if (context_id != VMCI_HOST_CONTEXT_ID) {
2124 bool headers_mapped;
2127 * Pre NOVMVM vmx'en may detach from a queue pair
2128 * before setting the page store, and in that case
2129 * there is no user memory to detach from. Also, more
2130 * recent VMX'en may detach from a queue pair in the
2134 qp_acquire_queue_mutex(entry->produce_q);
2135 headers_mapped = entry->produce_q->q_header ||
2136 entry->consume_q->q_header;
2137 if (QPBROKERSTATE_HAS_MEM(entry)) {
2139 qp_host_unmap_queues(INVALID_VMCI_GUEST_MEM_ID,
2142 if (result < VMCI_SUCCESS)
2143 pr_warn("Failed to unmap queue headers for queue pair (handle=0x%x:0x%x,result=%d)\n",
2144 handle.context, handle.resource,
2147 qp_host_unregister_user_memory(entry->produce_q,
2152 if (!headers_mapped)
2153 qp_reset_saved_headers(entry);
2155 qp_release_queue_mutex(entry->produce_q);
2157 if (!headers_mapped && entry->wakeup_cb)
2158 entry->wakeup_cb(entry->client_data);
2161 if (entry->wakeup_cb) {
2162 entry->wakeup_cb = NULL;
2163 entry->client_data = NULL;
2167 if (entry->qp.ref_count == 0) {
2168 qp_list_remove_entry(&qp_broker_list, &entry->qp);
2171 kfree(entry->local_mem);
2173 qp_cleanup_queue_mutex(entry->produce_q, entry->consume_q);
2174 qp_host_free_queue(entry->produce_q, entry->qp.produce_size);
2175 qp_host_free_queue(entry->consume_q, entry->qp.consume_size);
2176 /* Unlink from resource hash table and free callback */
2177 vmci_resource_remove(&entry->resource);
2181 vmci_ctx_qp_destroy(context, handle);
2183 qp_notify_peer(false, handle, context_id, peer_id);
2184 if (context_id == VMCI_HOST_CONTEXT_ID &&
2185 QPBROKERSTATE_HAS_MEM(entry)) {
2186 entry->state = VMCIQPB_SHUTDOWN_MEM;
2188 entry->state = VMCIQPB_SHUTDOWN_NO_MEM;
2192 vmci_ctx_qp_destroy(context, handle);
2195 result = VMCI_SUCCESS;
2197 mutex_unlock(&qp_broker_list.mutex);
2202 * Establishes the necessary mappings for a queue pair given a
2203 * reference to the queue pair guest memory. This is usually
2204 * called when a guest is unquiesced and the VMX is allowed to
2205 * map guest memory once again.
2207 int vmci_qp_broker_map(struct vmci_handle handle,
2208 struct vmci_ctx *context,
2211 struct qp_broker_entry *entry;
2212 const u32 context_id = vmci_ctx_get_id(context);
2215 if (vmci_handle_is_invalid(handle) || !context ||
2216 context_id == VMCI_INVALID_ID)
2217 return VMCI_ERROR_INVALID_ARGS;
2219 mutex_lock(&qp_broker_list.mutex);
2221 if (!vmci_ctx_qp_exists(context, handle)) {
2222 pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
2223 context_id, handle.context, handle.resource);
2224 result = VMCI_ERROR_NOT_FOUND;
2228 entry = qp_broker_handle_to_entry(handle);
2230 pr_devel("Context (ID=0x%x) reports being attached to queue pair (handle=0x%x:0x%x) that isn't present in broker\n",
2231 context_id, handle.context, handle.resource);
2232 result = VMCI_ERROR_NOT_FOUND;
2236 if (context_id != entry->create_id && context_id != entry->attach_id) {
2237 result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
2241 result = VMCI_SUCCESS;
2243 if (context_id != VMCI_HOST_CONTEXT_ID &&
2244 !QPBROKERSTATE_HAS_MEM(entry)) {
2245 struct vmci_qp_page_store page_store;
2247 page_store.pages = guest_mem;
2248 page_store.len = QPE_NUM_PAGES(entry->qp);
2250 qp_acquire_queue_mutex(entry->produce_q);
2251 qp_reset_saved_headers(entry);
2253 qp_host_register_user_memory(&page_store,
2256 qp_release_queue_mutex(entry->produce_q);
2257 if (result == VMCI_SUCCESS) {
2258 /* Move state from *_NO_MEM to *_MEM */
2262 if (entry->wakeup_cb)
2263 entry->wakeup_cb(entry->client_data);
2268 mutex_unlock(&qp_broker_list.mutex);
2273 * Saves a snapshot of the queue headers for the given QP broker
2274 * entry. Should be used when guest memory is unmapped.
2276 * VMCI_SUCCESS on success, appropriate error code if guest memory
2277 * can't be accessed..
2279 static int qp_save_headers(struct qp_broker_entry *entry)
2283 if (entry->produce_q->saved_header != NULL &&
2284 entry->consume_q->saved_header != NULL) {
2286 * If the headers have already been saved, we don't need to do
2287 * it again, and we don't want to map in the headers
2291 return VMCI_SUCCESS;
2294 if (NULL == entry->produce_q->q_header ||
2295 NULL == entry->consume_q->q_header) {
2296 result = qp_host_map_queues(entry->produce_q, entry->consume_q);
2297 if (result < VMCI_SUCCESS)
2301 memcpy(&entry->saved_produce_q, entry->produce_q->q_header,
2302 sizeof(entry->saved_produce_q));
2303 entry->produce_q->saved_header = &entry->saved_produce_q;
2304 memcpy(&entry->saved_consume_q, entry->consume_q->q_header,
2305 sizeof(entry->saved_consume_q));
2306 entry->consume_q->saved_header = &entry->saved_consume_q;
2308 return VMCI_SUCCESS;
2312 * Removes all references to the guest memory of a given queue pair, and
2313 * will move the queue pair from state *_MEM to *_NO_MEM. It is usually
2314 * called when a VM is being quiesced where access to guest memory should
2317 int vmci_qp_broker_unmap(struct vmci_handle handle,
2318 struct vmci_ctx *context,
2321 struct qp_broker_entry *entry;
2322 const u32 context_id = vmci_ctx_get_id(context);
2325 if (vmci_handle_is_invalid(handle) || !context ||
2326 context_id == VMCI_INVALID_ID)
2327 return VMCI_ERROR_INVALID_ARGS;
2329 mutex_lock(&qp_broker_list.mutex);
2331 if (!vmci_ctx_qp_exists(context, handle)) {
2332 pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
2333 context_id, handle.context, handle.resource);
2334 result = VMCI_ERROR_NOT_FOUND;
2338 entry = qp_broker_handle_to_entry(handle);
2340 pr_devel("Context (ID=0x%x) reports being attached to queue pair (handle=0x%x:0x%x) that isn't present in broker\n",
2341 context_id, handle.context, handle.resource);
2342 result = VMCI_ERROR_NOT_FOUND;
2346 if (context_id != entry->create_id && context_id != entry->attach_id) {
2347 result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
2351 if (context_id != VMCI_HOST_CONTEXT_ID &&
2352 QPBROKERSTATE_HAS_MEM(entry)) {
2353 qp_acquire_queue_mutex(entry->produce_q);
2354 result = qp_save_headers(entry);
2355 if (result < VMCI_SUCCESS)
2356 pr_warn("Failed to save queue headers for queue pair (handle=0x%x:0x%x,result=%d)\n",
2357 handle.context, handle.resource, result);
2359 qp_host_unmap_queues(gid, entry->produce_q, entry->consume_q);
2362 * On hosted, when we unmap queue pairs, the VMX will also
2363 * unmap the guest memory, so we invalidate the previously
2364 * registered memory. If the queue pair is mapped again at a
2365 * later point in time, we will need to reregister the user
2366 * memory with a possibly new user VA.
2368 qp_host_unregister_user_memory(entry->produce_q,
2372 * Move state from *_MEM to *_NO_MEM.
2376 qp_release_queue_mutex(entry->produce_q);
2379 result = VMCI_SUCCESS;
2382 mutex_unlock(&qp_broker_list.mutex);
2387 * Destroys all guest queue pair endpoints. If active guest queue
2388 * pairs still exist, hypercalls to attempt detach from these
2389 * queue pairs will be made. Any failure to detach is silently
2392 void vmci_qp_guest_endpoints_exit(void)
2394 struct qp_entry *entry;
2395 struct qp_guest_endpoint *ep;
2397 mutex_lock(&qp_guest_endpoints.mutex);
2399 while ((entry = qp_list_get_head(&qp_guest_endpoints))) {
2400 ep = (struct qp_guest_endpoint *)entry;
2402 /* Don't make a hypercall for local queue_pairs. */
2403 if (!(entry->flags & VMCI_QPFLAG_LOCAL))
2404 qp_detatch_hypercall(entry->handle);
2406 /* We cannot fail the exit, so let's reset ref_count. */
2407 entry->ref_count = 0;
2408 qp_list_remove_entry(&qp_guest_endpoints, entry);
2410 qp_guest_endpoint_destroy(ep);
2413 mutex_unlock(&qp_guest_endpoints.mutex);
2417 * Helper routine that will lock the queue pair before subsequent
2419 * Note: Non-blocking on the host side is currently only implemented in ESX.
2420 * Since non-blocking isn't yet implemented on the host personality we
2421 * have no reason to acquire a spin lock. So to avoid the use of an
2422 * unnecessary lock only acquire the mutex if we can block.
2424 static void qp_lock(const struct vmci_qp *qpair)
2426 qp_acquire_queue_mutex(qpair->produce_q);
2430 * Helper routine that unlocks the queue pair after calling
2433 static void qp_unlock(const struct vmci_qp *qpair)
2435 qp_release_queue_mutex(qpair->produce_q);
2439 * The queue headers may not be mapped at all times. If a queue is
2440 * currently not mapped, it will be attempted to do so.
2442 static int qp_map_queue_headers(struct vmci_queue *produce_q,
2443 struct vmci_queue *consume_q)
2447 if (NULL == produce_q->q_header || NULL == consume_q->q_header) {
2448 result = qp_host_map_queues(produce_q, consume_q);
2449 if (result < VMCI_SUCCESS)
2450 return (produce_q->saved_header &&
2451 consume_q->saved_header) ?
2452 VMCI_ERROR_QUEUEPAIR_NOT_READY :
2453 VMCI_ERROR_QUEUEPAIR_NOTATTACHED;
2456 return VMCI_SUCCESS;
2460 * Helper routine that will retrieve the produce and consume
2461 * headers of a given queue pair. If the guest memory of the
2462 * queue pair is currently not available, the saved queue headers
2463 * will be returned, if these are available.
2465 static int qp_get_queue_headers(const struct vmci_qp *qpair,
2466 struct vmci_queue_header **produce_q_header,
2467 struct vmci_queue_header **consume_q_header)
2471 result = qp_map_queue_headers(qpair->produce_q, qpair->consume_q);
2472 if (result == VMCI_SUCCESS) {
2473 *produce_q_header = qpair->produce_q->q_header;
2474 *consume_q_header = qpair->consume_q->q_header;
2475 } else if (qpair->produce_q->saved_header &&
2476 qpair->consume_q->saved_header) {
2477 *produce_q_header = qpair->produce_q->saved_header;
2478 *consume_q_header = qpair->consume_q->saved_header;
2479 result = VMCI_SUCCESS;
2486 * Callback from VMCI queue pair broker indicating that a queue
2487 * pair that was previously not ready, now either is ready or
2490 static int qp_wakeup_cb(void *client_data)
2492 struct vmci_qp *qpair = (struct vmci_qp *)client_data;
2495 while (qpair->blocked > 0) {
2497 qpair->generation++;
2498 wake_up(&qpair->event);
2502 return VMCI_SUCCESS;
2506 * Makes the calling thread wait for the queue pair to become
2507 * ready for host side access. Returns true when thread is
2508 * woken up after queue pair state change, false otherwise.
2510 static bool qp_wait_for_ready_queue(struct vmci_qp *qpair)
2512 unsigned int generation;
2515 generation = qpair->generation;
2517 wait_event(qpair->event, generation != qpair->generation);
2524 * Enqueues a given buffer to the produce queue using the provided
2525 * function. As many bytes as possible (space available in the queue)
2526 * are enqueued. Assumes the queue->mutex has been acquired. Returns
2527 * VMCI_ERROR_QUEUEPAIR_NOSPACE if no space was available to enqueue
2528 * data, VMCI_ERROR_INVALID_SIZE, if any queue pointer is outside the
2529 * queue (as defined by the queue size), VMCI_ERROR_INVALID_ARGS, if
2530 * an error occured when accessing the buffer,
2531 * VMCI_ERROR_QUEUEPAIR_NOTATTACHED, if the queue pair pages aren't
2532 * available. Otherwise, the number of bytes written to the queue is
2533 * returned. Updates the tail pointer of the produce queue.
2535 static ssize_t qp_enqueue_locked(struct vmci_queue *produce_q,
2536 struct vmci_queue *consume_q,
2537 const u64 produce_q_size,
2538 struct iov_iter *from)
2542 size_t buf_size = iov_iter_count(from);
2546 result = qp_map_queue_headers(produce_q, consume_q);
2547 if (unlikely(result != VMCI_SUCCESS))
2550 free_space = vmci_q_header_free_space(produce_q->q_header,
2551 consume_q->q_header,
2553 if (free_space == 0)
2554 return VMCI_ERROR_QUEUEPAIR_NOSPACE;
2556 if (free_space < VMCI_SUCCESS)
2557 return (ssize_t) free_space;
2559 written = (size_t) (free_space > buf_size ? buf_size : free_space);
2560 tail = vmci_q_header_producer_tail(produce_q->q_header);
2561 if (likely(tail + written < produce_q_size)) {
2562 result = qp_memcpy_to_queue_iter(produce_q, tail, from, written);
2564 /* Tail pointer wraps around. */
2566 const size_t tmp = (size_t) (produce_q_size - tail);
2568 result = qp_memcpy_to_queue_iter(produce_q, tail, from, tmp);
2569 if (result >= VMCI_SUCCESS)
2570 result = qp_memcpy_to_queue_iter(produce_q, 0, from,
2574 if (result < VMCI_SUCCESS)
2577 vmci_q_header_add_producer_tail(produce_q->q_header, written,
2583 * Dequeues data (if available) from the given consume queue. Writes data
2584 * to the user provided buffer using the provided function.
2585 * Assumes the queue->mutex has been acquired.
2587 * VMCI_ERROR_QUEUEPAIR_NODATA if no data was available to dequeue.
2588 * VMCI_ERROR_INVALID_SIZE, if any queue pointer is outside the queue
2589 * (as defined by the queue size).
2590 * VMCI_ERROR_INVALID_ARGS, if an error occured when accessing the buffer.
2591 * Otherwise the number of bytes dequeued is returned.
2593 * Updates the head pointer of the consume queue.
2595 static ssize_t qp_dequeue_locked(struct vmci_queue *produce_q,
2596 struct vmci_queue *consume_q,
2597 const u64 consume_q_size,
2598 struct iov_iter *to,
2599 bool update_consumer)
2601 size_t buf_size = iov_iter_count(to);
2607 result = qp_map_queue_headers(produce_q, consume_q);
2608 if (unlikely(result != VMCI_SUCCESS))
2611 buf_ready = vmci_q_header_buf_ready(consume_q->q_header,
2612 produce_q->q_header,
2615 return VMCI_ERROR_QUEUEPAIR_NODATA;
2617 if (buf_ready < VMCI_SUCCESS)
2618 return (ssize_t) buf_ready;
2620 read = (size_t) (buf_ready > buf_size ? buf_size : buf_ready);
2621 head = vmci_q_header_consumer_head(produce_q->q_header);
2622 if (likely(head + read < consume_q_size)) {
2623 result = qp_memcpy_from_queue_iter(to, consume_q, head, read);
2625 /* Head pointer wraps around. */
2627 const size_t tmp = (size_t) (consume_q_size - head);
2629 result = qp_memcpy_from_queue_iter(to, consume_q, head, tmp);
2630 if (result >= VMCI_SUCCESS)
2631 result = qp_memcpy_from_queue_iter(to, consume_q, 0,
2636 if (result < VMCI_SUCCESS)
2639 if (update_consumer)
2640 vmci_q_header_add_consumer_head(produce_q->q_header,
2641 read, consume_q_size);
2647 * vmci_qpair_alloc() - Allocates a queue pair.
2648 * @qpair: Pointer for the new vmci_qp struct.
2649 * @handle: Handle to track the resource.
2650 * @produce_qsize: Desired size of the producer queue.
2651 * @consume_qsize: Desired size of the consumer queue.
2652 * @peer: ContextID of the peer.
2653 * @flags: VMCI flags.
2654 * @priv_flags: VMCI priviledge flags.
2656 * This is the client interface for allocating the memory for a
2657 * vmci_qp structure and then attaching to the underlying
2658 * queue. If an error occurs allocating the memory for the
2659 * vmci_qp structure no attempt is made to attach. If an
2660 * error occurs attaching, then the structure is freed.
2662 int vmci_qpair_alloc(struct vmci_qp **qpair,
2663 struct vmci_handle *handle,
2670 struct vmci_qp *my_qpair;
2672 struct vmci_handle src = VMCI_INVALID_HANDLE;
2673 struct vmci_handle dst = vmci_make_handle(peer, VMCI_INVALID_ID);
2674 enum vmci_route route;
2675 vmci_event_release_cb wakeup_cb;
2679 * Restrict the size of a queuepair. The device already
2680 * enforces a limit on the total amount of memory that can be
2681 * allocated to queuepairs for a guest. However, we try to
2682 * allocate this memory before we make the queuepair
2683 * allocation hypercall. On Linux, we allocate each page
2684 * separately, which means rather than fail, the guest will
2685 * thrash while it tries to allocate, and will become
2686 * increasingly unresponsive to the point where it appears to
2687 * be hung. So we place a limit on the size of an individual
2688 * queuepair here, and leave the device to enforce the
2689 * restriction on total queuepair memory. (Note that this
2690 * doesn't prevent all cases; a user with only this much
2691 * physical memory could still get into trouble.) The error
2692 * used by the device is NO_RESOURCES, so use that here too.
2695 if (produce_qsize + consume_qsize < max(produce_qsize, consume_qsize) ||
2696 produce_qsize + consume_qsize > VMCI_MAX_GUEST_QP_MEMORY)
2697 return VMCI_ERROR_NO_RESOURCES;
2699 retval = vmci_route(&src, &dst, false, &route);
2700 if (retval < VMCI_SUCCESS)
2701 route = vmci_guest_code_active() ?
2702 VMCI_ROUTE_AS_GUEST : VMCI_ROUTE_AS_HOST;
2704 if (flags & (VMCI_QPFLAG_NONBLOCK | VMCI_QPFLAG_PINNED)) {
2705 pr_devel("NONBLOCK OR PINNED set");
2706 return VMCI_ERROR_INVALID_ARGS;
2709 my_qpair = kzalloc(sizeof(*my_qpair), GFP_KERNEL);
2711 return VMCI_ERROR_NO_MEM;
2713 my_qpair->produce_q_size = produce_qsize;
2714 my_qpair->consume_q_size = consume_qsize;
2715 my_qpair->peer = peer;
2716 my_qpair->flags = flags;
2717 my_qpair->priv_flags = priv_flags;
2722 if (VMCI_ROUTE_AS_HOST == route) {
2723 my_qpair->guest_endpoint = false;
2724 if (!(flags & VMCI_QPFLAG_LOCAL)) {
2725 my_qpair->blocked = 0;
2726 my_qpair->generation = 0;
2727 init_waitqueue_head(&my_qpair->event);
2728 wakeup_cb = qp_wakeup_cb;
2729 client_data = (void *)my_qpair;
2732 my_qpair->guest_endpoint = true;
2735 retval = vmci_qp_alloc(handle,
2736 &my_qpair->produce_q,
2737 my_qpair->produce_q_size,
2738 &my_qpair->consume_q,
2739 my_qpair->consume_q_size,
2742 my_qpair->priv_flags,
2743 my_qpair->guest_endpoint,
2744 wakeup_cb, client_data);
2746 if (retval < VMCI_SUCCESS) {
2752 my_qpair->handle = *handle;
2756 EXPORT_SYMBOL_GPL(vmci_qpair_alloc);
2759 * vmci_qpair_detach() - Detatches the client from a queue pair.
2760 * @qpair: Reference of a pointer to the qpair struct.
2762 * This is the client interface for detaching from a VMCIQPair.
2763 * Note that this routine will free the memory allocated for the
2764 * vmci_qp structure too.
2766 int vmci_qpair_detach(struct vmci_qp **qpair)
2769 struct vmci_qp *old_qpair;
2771 if (!qpair || !(*qpair))
2772 return VMCI_ERROR_INVALID_ARGS;
2775 result = qp_detatch(old_qpair->handle, old_qpair->guest_endpoint);
2778 * The guest can fail to detach for a number of reasons, and
2779 * if it does so, it will cleanup the entry (if there is one).
2780 * The host can fail too, but it won't cleanup the entry
2781 * immediately, it will do that later when the context is
2782 * freed. Either way, we need to release the qpair struct
2783 * here; there isn't much the caller can do, and we don't want
2787 memset(old_qpair, 0, sizeof(*old_qpair));
2788 old_qpair->handle = VMCI_INVALID_HANDLE;
2789 old_qpair->peer = VMCI_INVALID_ID;
2795 EXPORT_SYMBOL_GPL(vmci_qpair_detach);
2798 * vmci_qpair_get_produce_indexes() - Retrieves the indexes of the producer.
2799 * @qpair: Pointer to the queue pair struct.
2800 * @producer_tail: Reference used for storing producer tail index.
2801 * @consumer_head: Reference used for storing the consumer head index.
2803 * This is the client interface for getting the current indexes of the
2804 * QPair from the point of the view of the caller as the producer.
2806 int vmci_qpair_get_produce_indexes(const struct vmci_qp *qpair,
2810 struct vmci_queue_header *produce_q_header;
2811 struct vmci_queue_header *consume_q_header;
2815 return VMCI_ERROR_INVALID_ARGS;
2819 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2820 if (result == VMCI_SUCCESS)
2821 vmci_q_header_get_pointers(produce_q_header, consume_q_header,
2822 producer_tail, consumer_head);
2825 if (result == VMCI_SUCCESS &&
2826 ((producer_tail && *producer_tail >= qpair->produce_q_size) ||
2827 (consumer_head && *consumer_head >= qpair->produce_q_size)))
2828 return VMCI_ERROR_INVALID_SIZE;
2832 EXPORT_SYMBOL_GPL(vmci_qpair_get_produce_indexes);
2835 * vmci_qpair_get_consume_indexes() - Retrieves the indexes of the consumer.
2836 * @qpair: Pointer to the queue pair struct.
2837 * @consumer_tail: Reference used for storing consumer tail index.
2838 * @producer_head: Reference used for storing the producer head index.
2840 * This is the client interface for getting the current indexes of the
2841 * QPair from the point of the view of the caller as the consumer.
2843 int vmci_qpair_get_consume_indexes(const struct vmci_qp *qpair,
2847 struct vmci_queue_header *produce_q_header;
2848 struct vmci_queue_header *consume_q_header;
2852 return VMCI_ERROR_INVALID_ARGS;
2856 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2857 if (result == VMCI_SUCCESS)
2858 vmci_q_header_get_pointers(consume_q_header, produce_q_header,
2859 consumer_tail, producer_head);
2862 if (result == VMCI_SUCCESS &&
2863 ((consumer_tail && *consumer_tail >= qpair->consume_q_size) ||
2864 (producer_head && *producer_head >= qpair->consume_q_size)))
2865 return VMCI_ERROR_INVALID_SIZE;
2869 EXPORT_SYMBOL_GPL(vmci_qpair_get_consume_indexes);
2872 * vmci_qpair_produce_free_space() - Retrieves free space in producer queue.
2873 * @qpair: Pointer to the queue pair struct.
2875 * This is the client interface for getting the amount of free
2876 * space in the QPair from the point of the view of the caller as
2877 * the producer which is the common case. Returns < 0 if err, else
2878 * available bytes into which data can be enqueued if > 0.
2880 s64 vmci_qpair_produce_free_space(const struct vmci_qp *qpair)
2882 struct vmci_queue_header *produce_q_header;
2883 struct vmci_queue_header *consume_q_header;
2887 return VMCI_ERROR_INVALID_ARGS;
2891 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2892 if (result == VMCI_SUCCESS)
2893 result = vmci_q_header_free_space(produce_q_header,
2895 qpair->produce_q_size);
2903 EXPORT_SYMBOL_GPL(vmci_qpair_produce_free_space);
2906 * vmci_qpair_consume_free_space() - Retrieves free space in consumer queue.
2907 * @qpair: Pointer to the queue pair struct.
2909 * This is the client interface for getting the amount of free
2910 * space in the QPair from the point of the view of the caller as
2911 * the consumer which is not the common case. Returns < 0 if err, else
2912 * available bytes into which data can be enqueued if > 0.
2914 s64 vmci_qpair_consume_free_space(const struct vmci_qp *qpair)
2916 struct vmci_queue_header *produce_q_header;
2917 struct vmci_queue_header *consume_q_header;
2921 return VMCI_ERROR_INVALID_ARGS;
2925 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2926 if (result == VMCI_SUCCESS)
2927 result = vmci_q_header_free_space(consume_q_header,
2929 qpair->consume_q_size);
2937 EXPORT_SYMBOL_GPL(vmci_qpair_consume_free_space);
2940 * vmci_qpair_produce_buf_ready() - Gets bytes ready to read from
2942 * @qpair: Pointer to the queue pair struct.
2944 * This is the client interface for getting the amount of
2945 * enqueued data in the QPair from the point of the view of the
2946 * caller as the producer which is not the common case. Returns < 0 if err,
2947 * else available bytes that may be read.
2949 s64 vmci_qpair_produce_buf_ready(const struct vmci_qp *qpair)
2951 struct vmci_queue_header *produce_q_header;
2952 struct vmci_queue_header *consume_q_header;
2956 return VMCI_ERROR_INVALID_ARGS;
2960 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2961 if (result == VMCI_SUCCESS)
2962 result = vmci_q_header_buf_ready(produce_q_header,
2964 qpair->produce_q_size);
2972 EXPORT_SYMBOL_GPL(vmci_qpair_produce_buf_ready);
2975 * vmci_qpair_consume_buf_ready() - Gets bytes ready to read from
2977 * @qpair: Pointer to the queue pair struct.
2979 * This is the client interface for getting the amount of
2980 * enqueued data in the QPair from the point of the view of the
2981 * caller as the consumer which is the normal case. Returns < 0 if err,
2982 * else available bytes that may be read.
2984 s64 vmci_qpair_consume_buf_ready(const struct vmci_qp *qpair)
2986 struct vmci_queue_header *produce_q_header;
2987 struct vmci_queue_header *consume_q_header;
2991 return VMCI_ERROR_INVALID_ARGS;
2995 qp_get_queue_headers(qpair, &produce_q_header, &consume_q_header);
2996 if (result == VMCI_SUCCESS)
2997 result = vmci_q_header_buf_ready(consume_q_header,
2999 qpair->consume_q_size);
3007 EXPORT_SYMBOL_GPL(vmci_qpair_consume_buf_ready);
3010 * vmci_qpair_enqueue() - Throw data on the queue.
3011 * @qpair: Pointer to the queue pair struct.
3012 * @buf: Pointer to buffer containing data
3013 * @buf_size: Length of buffer.
3014 * @buf_type: Buffer type (Unused).
3016 * This is the client interface for enqueueing data into the queue.
3017 * Returns number of bytes enqueued or < 0 on error.
3019 ssize_t vmci_qpair_enqueue(struct vmci_qp *qpair,
3025 struct iov_iter from;
3026 struct kvec v = {.iov_base = (void *)buf, .iov_len = buf_size};
3029 return VMCI_ERROR_INVALID_ARGS;
3031 iov_iter_kvec(&from, WRITE, &v, 1, buf_size);
3036 result = qp_enqueue_locked(qpair->produce_q,
3038 qpair->produce_q_size,
3041 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3042 !qp_wait_for_ready_queue(qpair))
3043 result = VMCI_ERROR_WOULD_BLOCK;
3045 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3051 EXPORT_SYMBOL_GPL(vmci_qpair_enqueue);
3054 * vmci_qpair_dequeue() - Get data from the queue.
3055 * @qpair: Pointer to the queue pair struct.
3056 * @buf: Pointer to buffer for the data
3057 * @buf_size: Length of buffer.
3058 * @buf_type: Buffer type (Unused).
3060 * This is the client interface for dequeueing data from the queue.
3061 * Returns number of bytes dequeued or < 0 on error.
3063 ssize_t vmci_qpair_dequeue(struct vmci_qp *qpair,
3070 struct kvec v = {.iov_base = buf, .iov_len = buf_size};
3073 return VMCI_ERROR_INVALID_ARGS;
3075 iov_iter_kvec(&to, READ, &v, 1, buf_size);
3080 result = qp_dequeue_locked(qpair->produce_q,
3082 qpair->consume_q_size,
3085 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3086 !qp_wait_for_ready_queue(qpair))
3087 result = VMCI_ERROR_WOULD_BLOCK;
3089 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3095 EXPORT_SYMBOL_GPL(vmci_qpair_dequeue);
3098 * vmci_qpair_peek() - Peek at the data in the queue.
3099 * @qpair: Pointer to the queue pair struct.
3100 * @buf: Pointer to buffer for the data
3101 * @buf_size: Length of buffer.
3102 * @buf_type: Buffer type (Unused on Linux).
3104 * This is the client interface for peeking into a queue. (I.e.,
3105 * copy data from the queue without updating the head pointer.)
3106 * Returns number of bytes dequeued or < 0 on error.
3108 ssize_t vmci_qpair_peek(struct vmci_qp *qpair,
3114 struct kvec v = {.iov_base = buf, .iov_len = buf_size};
3118 return VMCI_ERROR_INVALID_ARGS;
3120 iov_iter_kvec(&to, READ, &v, 1, buf_size);
3125 result = qp_dequeue_locked(qpair->produce_q,
3127 qpair->consume_q_size,
3130 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3131 !qp_wait_for_ready_queue(qpair))
3132 result = VMCI_ERROR_WOULD_BLOCK;
3134 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3140 EXPORT_SYMBOL_GPL(vmci_qpair_peek);
3143 * vmci_qpair_enquev() - Throw data on the queue using iov.
3144 * @qpair: Pointer to the queue pair struct.
3145 * @iov: Pointer to buffer containing data
3146 * @iov_size: Length of buffer.
3147 * @buf_type: Buffer type (Unused).
3149 * This is the client interface for enqueueing data into the queue.
3150 * This function uses IO vectors to handle the work. Returns number
3151 * of bytes enqueued or < 0 on error.
3153 ssize_t vmci_qpair_enquev(struct vmci_qp *qpair,
3161 return VMCI_ERROR_INVALID_ARGS;
3166 result = qp_enqueue_locked(qpair->produce_q,
3168 qpair->produce_q_size,
3171 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3172 !qp_wait_for_ready_queue(qpair))
3173 result = VMCI_ERROR_WOULD_BLOCK;
3175 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3181 EXPORT_SYMBOL_GPL(vmci_qpair_enquev);
3184 * vmci_qpair_dequev() - Get data from the queue using iov.
3185 * @qpair: Pointer to the queue pair struct.
3186 * @iov: Pointer to buffer for the data
3187 * @iov_size: Length of buffer.
3188 * @buf_type: Buffer type (Unused).
3190 * This is the client interface for dequeueing data from the queue.
3191 * This function uses IO vectors to handle the work. Returns number
3192 * of bytes dequeued or < 0 on error.
3194 ssize_t vmci_qpair_dequev(struct vmci_qp *qpair,
3202 return VMCI_ERROR_INVALID_ARGS;
3207 result = qp_dequeue_locked(qpair->produce_q,
3209 qpair->consume_q_size,
3210 &msg->msg_iter, true);
3212 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3213 !qp_wait_for_ready_queue(qpair))
3214 result = VMCI_ERROR_WOULD_BLOCK;
3216 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3222 EXPORT_SYMBOL_GPL(vmci_qpair_dequev);
3225 * vmci_qpair_peekv() - Peek at the data in the queue using iov.
3226 * @qpair: Pointer to the queue pair struct.
3227 * @iov: Pointer to buffer for the data
3228 * @iov_size: Length of buffer.
3229 * @buf_type: Buffer type (Unused on Linux).
3231 * This is the client interface for peeking into a queue. (I.e.,
3232 * copy data from the queue without updating the head pointer.)
3233 * This function uses IO vectors to handle the work. Returns number
3234 * of bytes peeked or < 0 on error.
3236 ssize_t vmci_qpair_peekv(struct vmci_qp *qpair,
3244 return VMCI_ERROR_INVALID_ARGS;
3249 result = qp_dequeue_locked(qpair->produce_q,
3251 qpair->consume_q_size,
3252 &msg->msg_iter, false);
3254 if (result == VMCI_ERROR_QUEUEPAIR_NOT_READY &&
3255 !qp_wait_for_ready_queue(qpair))
3256 result = VMCI_ERROR_WOULD_BLOCK;
3258 } while (result == VMCI_ERROR_QUEUEPAIR_NOT_READY);
3263 EXPORT_SYMBOL_GPL(vmci_qpair_peekv);